From f712f97e49b3cc2c5d5305cafecf4389a9ffaf5d Mon Sep 17 00:00:00 2001 From: Evan Kaloudis Date: Sun, 24 Mar 2024 20:58:43 -0400 Subject: [PATCH] Embedded LND: coin control endpoints --- backends/EmbeddedLND.ts | 29 +++++++++--- lndmobile/LndMobileInjection.ts | 29 +++++++++++- lndmobile/wallet.ts | 78 +++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 8 deletions(-) diff --git a/backends/EmbeddedLND.ts b/backends/EmbeddedLND.ts index 1de6b2bbe0..c3c9ab471b 100644 --- a/backends/EmbeddedLND.ts +++ b/backends/EmbeddedLND.ts @@ -28,8 +28,14 @@ const { closeChannel, openChannel } = lndMobile.channel; -const { signMessageNodePubkey, verifyMessageNodePubkey, bumpFee } = - lndMobile.wallet; +const { + signMessageNodePubkey, + verifyMessageNodePubkey, + bumpFee, + fundPsbt, + finalizePsbt, + publishTransaction +} = lndMobile.wallet; const { walletBalance, newAddress, getTransactions, sendCoins } = lndMobile.onchain; @@ -146,10 +152,21 @@ export default class EmbeddedLND extends LND { urlParams && (await queryRoutes(urlParams[0], urlParams[1])); // getForwardingHistory = () => N/A // // Coin Control - // fundPsbt = (data: any) => this.postRequest('/v2/wallet/psbt/fund', data); - // finalizePsbt = (data: any) => - // this.postRequest('/v2/wallet/psbt/finalize', data); - // publishTransaction = (data: any) => this.postRequest('/v2/wallet/tx', data); + fundPsbt = async (data: any) => + await fundPsbt({ + raw: data.raw, + spend_unconfirmed: data.spend_unconfirmed, + sat_per_vbyte: data.sat_per_vbyte + }); + finalizePsbt = async (data: any) => + await finalizePsbt({ + funded_psbt: data.funded_psbt + }); + publishTransaction = async (data: any) => + await publishTransaction({ + tx_hex: data.tx_hex + }); + getUTXOs = async () => await listUnspent(); bumpFee = async (data: any) => await bumpFee(data); lookupInvoice = async (data: any) => await lookupInvoice(data.r_hash); diff --git a/lndmobile/LndMobileInjection.ts b/lndmobile/LndMobileInjection.ts index c63a745e00..75767899db 100644 --- a/lndmobile/LndMobileInjection.ts +++ b/lndmobile/LndMobileInjection.ts @@ -75,7 +75,10 @@ import { verifyMessageNodePubkey, signMessage, signMessageNodePubkey, - bumpFee + bumpFee, + fundPsbt, + finalizePsbt, + publishTransaction } from './wallet'; import { status, modifyStatus, queryScores, setScores } from './autopilot'; import { checkScheduledSyncWorkStatus } from './scheduled-sync'; // TODO(hsjoberg): This could be its own injection "LndMobileScheduledSync" @@ -331,6 +334,25 @@ export interface ILndMobileInjections { force?: boolean; sat_per_vbyte?: Long; }) => Promise; + fundPsbt: ({ + raw, + spend_unconfirmed, + sat_per_vbyte + }: { + raw: walletrpc.TxTemplate; + spend_unconfirmed?: boolean; + sat_per_vbyte?: Long; + }) => Promise; + finalizePsbt: ({ + funded_psbt + }: { + funded_psbt: Uint8Array; + }) => Promise; + publishTransaction: ({ + tx_hex + }: { + tx_hex: Uint8Array; + }) => Promise; }; autopilot: { status: () => Promise; @@ -421,7 +443,10 @@ export default { verifyMessageNodePubkey, signMessage, signMessageNodePubkey, - bumpFee + bumpFee, + fundPsbt, + finalizePsbt, + publishTransaction }, autopilot: { status, diff --git a/lndmobile/wallet.ts b/lndmobile/wallet.ts index 8dbbe1ae2b..a6937db38e 100644 --- a/lndmobile/wallet.ts +++ b/lndmobile/wallet.ts @@ -40,6 +40,84 @@ export const bumpFee = async ({ return response; }; +/** + * @throws + */ +export const fundPsbt = async ({ + raw, + spend_unconfirmed, + sat_per_vbyte +}: { + raw: walletrpc.TxTemplate; + spend_unconfirmed?: boolean; + sat_per_vbyte?: Long; +}): Promise => { + const options: walletrpc.IFundPsbtRequest = { + raw, + spend_unconfirmed, + sat_per_vbyte + }; + const response = await sendCommand< + walletrpc.IFundPsbtRequest, + walletrpc.FundPsbtRequest, + walletrpc.FundPsbtResponse + >({ + request: walletrpc.FundPsbtRequest, + response: walletrpc.FundPsbtResponse, + method: 'FundPsbt', + options + }); + return response; +}; + +/** + * @throws + */ +export const finalizePsbt = async ({ + funded_psbt +}: { + funded_psbt: Uint8Array; +}): Promise => { + const options: walletrpc.IFinalizePsbtRequest = { + funded_psbt + }; + const response = await sendCommand< + walletrpc.IFinalizePsbtRequest, + walletrpc.FinalizePsbtRequest, + walletrpc.FinalizePsbtResponse + >({ + request: walletrpc.FinalizePsbtRequest, + response: walletrpc.FinalizePsbtResponse, + method: 'FinalizePsbt', + options + }); + return response; +}; + +/** + * @throws + */ +export const publishTransaction = async ({ + tx_hex +}: { + tx_hex: Uint8Array; +}): Promise => { + const options: walletrpc.ITransaction = { + tx_hex + }; + const response = await sendCommand< + walletrpc.ITransaction, + walletrpc.Transaction, + walletrpc.PublishResponse + >({ + request: walletrpc.Transaction, + response: walletrpc.PublishResponse, + method: 'PublishTransaction', + options + }); + return response; +}; + // Base wallet /**