Merge pull request #1168 from motorina0/broadcast_signed_psbt

Watchonly wallet: allow broadcast of already signed transation
This commit is contained in:
Vlad Stan 2022-12-02 14:06:13 +02:00 committed by GitHub
commit 1ff76defc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 11 deletions

View File

@ -76,9 +76,13 @@ class CreatePsbt(BaseModel):
tx_size: int tx_size: int
class SerializedTransaction(BaseModel):
tx_hex: str
class ExtractPsbt(BaseModel): class ExtractPsbt(BaseModel):
psbtBase64 = "" # // todo snake case psbtBase64 = "" # // todo snake case
inputs: List[TransactionInput] inputs: List[SerializedTransaction]
network = "Mainnet" network = "Mainnet"
@ -87,10 +91,6 @@ class SignedTransaction(BaseModel):
tx_json: Optional[str] tx_json: Optional[str]
class BroadcastTransaction(BaseModel):
tx_hex: str
class Config(BaseModel): class Config(BaseModel):
mempool_endpoint = "https://mempool.space" mempool_endpoint = "https://mempool.space"
receive_gap_limit = 20 receive_gap_limit = 20

View File

@ -272,15 +272,35 @@ async function payment(path) {
this.showChecking = false this.showChecking = false
} }
}, },
fetchUtxoHexForPsbt: async function (psbtBase64) {
if (this.tx?.inputs && this.tx?.inputs.length) return this.tx.inputs
const {data: psbtUtxos} = await LNbits.api.request(
'PUT',
'/watchonly/api/v1/psbt/utxos',
this.adminkey,
{psbtBase64}
)
const inputs = []
for (const utxo of psbtUtxos) {
const txHex = await this.fetchTxHex(utxo.tx_id)
inputs.push({tx_hex: txHex})
}
return inputs
},
extractTxFromPsbt: async function (psbtBase64) { extractTxFromPsbt: async function (psbtBase64) {
try { try {
const inputs = await this.fetchUtxoHexForPsbt(psbtBase64)
const {data} = await LNbits.api.request( const {data} = await LNbits.api.request(
'PUT', 'PUT',
'/watchonly/api/v1/psbt/extract', '/watchonly/api/v1/psbt/extract',
this.adminkey, this.adminkey,
{ {
psbtBase64, psbtBase64,
inputs: this.tx.inputs, inputs,
network: this.network network: this.network
} }
) )

View File

@ -54,7 +54,10 @@ const watchOnly = async () => {
showPayment: false, showPayment: false,
fetchedUtxos: false, fetchedUtxos: false,
utxosFilter: '', utxosFilter: '',
network: null network: null,
showEnterSignedPsbt: false,
signedBase64Psbt: null
} }
}, },
computed: { computed: {
@ -173,6 +176,15 @@ const watchOnly = async () => {
this.$refs.paymentRef.updateSignedPsbt(psbtBase64) this.$refs.paymentRef.updateSignedPsbt(psbtBase64)
}, },
showEnterSignedPsbtDialog: function () {
this.signedBase64Psbt = ''
this.showEnterSignedPsbt = true
},
checkPsbt: function () {
this.$refs.paymentRef.updateSignedPsbt(this.signedBase64Psbt)
},
//################### UTXOs ################### //################### UTXOs ###################
scanAllAddresses: async function () { scanAllAddresses: async function () {
await this.refreshAddresses() await this.refreshAddresses()

View File

@ -52,14 +52,38 @@
></q-spinner> ></q-spinner>
</div> </div>
<div class="col-md-3 col-sm-5 q-pr-md"> <div class="col-md-3 col-sm-5 q-pr-md">
<q-btn <q-btn-dropdown
v-if="!showPayment" v-if="!showPayment"
split
unelevated unelevated
label="New Payment"
color="secondary" color="secondary"
class="btn-full" class="btn-full"
@click="goToPaymentView" @click="goToPaymentView"
>New Payment</q-btn
> >
<q-list>
<q-item @click="goToPaymentView" clickable v-close-popup>
<q-item-section>
<q-item-label>New Payment</q-item-label>
<q-item-label caption
>Create a new payment by selecting Inputs and
Outputs</q-item-label
>
</q-item-section>
</q-item>
<q-item
@click="showEnterSignedPsbtDialog"
clickable
v-close-popup
>
<q-item-section>
<q-item-label>From Signed PSBT</q-item-label>
<q-item-label caption> Paste a signed PSBT</q-item-label>
</q-item-section>
</q-item>
</q-list>
</q-btn-dropdown>
<q-btn <q-btn
v-if="showPayment" v-if="showPayment"
outline outline
@ -226,6 +250,36 @@
</q-card> </q-card>
</q-dialog> </q-dialog>
<q-dialog v-model="showEnterSignedPsbt" position="top">
<q-card class="q-pa-lg lnbits__dialog-card">
<h5 class="text-subtitle1 q-my-none">Enter the Signed PSBT</h5>
<q-separator></q-separator><br />
<p>
<q-input
filled
dense
v-model.trim="signedBase64Psbt"
type="textarea"
label="Signed PSBT"
></q-input>
</p>
<div class="row q-mt-lg q-gutter-sm">
<q-btn
outline
v-close-popup
color="grey"
@click="checkPsbt"
class="q-ml-sm"
>Check PSBT</q-btn
>
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Close</q-btn>
</div>
<div class="row q-mt-lg q-gutter-sm"></div>
</q-card>
</q-dialog>
{% endraw %} {% endraw %}
</div> </div>

View File

@ -31,11 +31,11 @@ from .crud import (
) )
from .helpers import parse_key from .helpers import parse_key
from .models import ( from .models import (
BroadcastTransaction,
Config, Config,
CreatePsbt, CreatePsbt,
CreateWallet, CreateWallet,
ExtractPsbt, ExtractPsbt,
SerializedTransaction,
SignedTransaction, SignedTransaction,
WalletAccount, WalletAccount,
) )
@ -291,6 +291,24 @@ async def api_psbt_create(
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e)) raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
@watchonly_ext.put("/api/v1/psbt/utxos")
async def api_psbt_extract_tx(
req: Request, w: WalletTypeInfo = Depends(require_admin_key)
):
"""Extract previous unspent transaction outputs (tx_id, vout) from PSBT"""
body = await req.json()
try:
psbt = PSBT.from_base64(body["psbtBase64"])
res = []
for _, inp in enumerate(psbt.inputs):
res.append({"tx_id": inp.txid.hex(), "vout": inp.vout})
return res
except Exception as e:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
@watchonly_ext.put("/api/v1/psbt/extract") @watchonly_ext.put("/api/v1/psbt/extract")
async def api_psbt_extract_tx( async def api_psbt_extract_tx(
data: ExtractPsbt, w: WalletTypeInfo = Depends(require_admin_key) data: ExtractPsbt, w: WalletTypeInfo = Depends(require_admin_key)
@ -327,7 +345,7 @@ async def api_psbt_extract_tx(
@watchonly_ext.post("/api/v1/tx") @watchonly_ext.post("/api/v1/tx")
async def api_tx_broadcast( async def api_tx_broadcast(
data: BroadcastTransaction, w: WalletTypeInfo = Depends(require_admin_key) data: SerializedTransaction, w: WalletTypeInfo = Depends(require_admin_key)
): ):
try: try:
config = await get_config(w.wallet.user) config = await get_config(w.wallet.user)