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
class SerializedTransaction(BaseModel):
tx_hex: str
class ExtractPsbt(BaseModel):
psbtBase64 = "" # // todo snake case
inputs: List[TransactionInput]
inputs: List[SerializedTransaction]
network = "Mainnet"
@ -87,10 +91,6 @@ class SignedTransaction(BaseModel):
tx_json: Optional[str]
class BroadcastTransaction(BaseModel):
tx_hex: str
class Config(BaseModel):
mempool_endpoint = "https://mempool.space"
receive_gap_limit = 20

View File

@ -272,15 +272,35 @@ async function payment(path) {
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) {
try {
const inputs = await this.fetchUtxoHexForPsbt(psbtBase64)
const {data} = await LNbits.api.request(
'PUT',
'/watchonly/api/v1/psbt/extract',
this.adminkey,
{
psbtBase64,
inputs: this.tx.inputs,
inputs,
network: this.network
}
)

View File

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

View File

@ -52,14 +52,38 @@
></q-spinner>
</div>
<div class="col-md-3 col-sm-5 q-pr-md">
<q-btn
<q-btn-dropdown
v-if="!showPayment"
split
unelevated
label="New Payment"
color="secondary"
class="btn-full"
@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
v-if="showPayment"
outline
@ -226,6 +250,36 @@
</q-card>
</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 %}
</div>

View File

@ -31,11 +31,11 @@ from .crud import (
)
from .helpers import parse_key
from .models import (
BroadcastTransaction,
Config,
CreatePsbt,
CreateWallet,
ExtractPsbt,
SerializedTransaction,
SignedTransaction,
WalletAccount,
)
@ -291,6 +291,24 @@ async def api_psbt_create(
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")
async def api_psbt_extract_tx(
data: ExtractPsbt, w: WalletTypeInfo = Depends(require_admin_key)
@ -327,7 +345,7 @@ async def api_psbt_extract_tx(
@watchonly_ext.post("/api/v1/tx")
async def api_tx_broadcast(
data: BroadcastTransaction, w: WalletTypeInfo = Depends(require_admin_key)
data: SerializedTransaction, w: WalletTypeInfo = Depends(require_admin_key)
):
try:
config = await get_config(w.wallet.user)