From 1c2f1ad3f9eeedc9756cc462879d0b1cbda845d8 Mon Sep 17 00:00:00 2001 From: overtorment Date: Sat, 17 Aug 2024 11:12:22 +0100 Subject: [PATCH] REF: ts convert payjoin-transaction --- ...-transaction.js => payjoin-transaction.ts} | 25 +++++++++++++------ screen/send/Confirm.tsx | 3 ++- 2 files changed, 19 insertions(+), 9 deletions(-) rename class/{payjoin-transaction.js => payjoin-transaction.ts} (73%) diff --git a/class/payjoin-transaction.js b/class/payjoin-transaction.ts similarity index 73% rename from class/payjoin-transaction.js rename to class/payjoin-transaction.ts index d275992da..dfd121fea 100644 --- a/class/payjoin-transaction.js +++ b/class/payjoin-transaction.ts @@ -4,14 +4,21 @@ import { ECPairFactory } from 'ecpair'; import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback'; import ecc from '../blue_modules/noble_ecc'; import presentAlert from '../components/Alert'; +import { HDSegwitBech32Wallet } from './wallets/hd-segwit-bech32-wallet'; +import assert from 'assert'; const ECPair = ECPairFactory(ecc); -const delay = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds)); +const delay = (milliseconds: number) => new Promise(resolve => setTimeout(resolve, milliseconds)); // Implements IPayjoinClientWallet // https://github.com/bitcoinjs/payjoin-client/blob/master/ts_src/wallet.ts export default class PayjoinTransaction { - constructor(psbt, broadcast, wallet) { + private _psbt: bitcoin.Psbt; + private _broadcast: (txhex: string) => Promise; + private _wallet: HDSegwitBech32Wallet; + private _payjoinPsbt: any; + + constructor(psbt: bitcoin.Psbt, broadcast: (txhex: string) => Promise, wallet: HDSegwitBech32Wallet) { this._psbt = psbt; this._broadcast = broadcast; this._wallet = wallet; @@ -24,6 +31,7 @@ export default class PayjoinTransaction { for (const [index, input] of unfinalized.data.inputs.entries()) { delete input.finalScriptWitness; + assert(input.witnessUtxo, 'Internal error: input.witnessUtxo is not set'); const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script); const wif = this._wallet._getWifForAddress(address); const keyPair = ECPair.fromWIF(wif); @@ -37,16 +45,17 @@ export default class PayjoinTransaction { /** * Doesnt conform to spec but needed for user-facing wallet software to find out txid of payjoined transaction * - * @returns {boolean|Psbt} + * @returns {Psbt} */ getPayjoinPsbt() { return this._payjoinPsbt; } - async signPsbt(payjoinPsbt) { + async signPsbt(payjoinPsbt: bitcoin.Psbt) { // Do this without relying on private methods for (const [index, input] of payjoinPsbt.data.inputs.entries()) { + assert(input.witnessUtxo, 'Internal error: input.witnessUtxo is not set'); const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script); try { const wif = this._wallet._getWifForAddress(address); @@ -58,19 +67,19 @@ export default class PayjoinTransaction { return this._payjoinPsbt; } - async broadcastTx(txHex) { + async broadcastTx(txHex: string) { try { const result = await this._broadcast(txHex); if (!result) { throw new Error(`Broadcast failed`); } return ''; - } catch (e) { + } catch (e: any) { return 'Error: ' + e.message; } } - async scheduleBroadcastTx(txHex, milliseconds) { + async scheduleBroadcastTx(txHex: string, milliseconds: number) { delay(milliseconds).then(async () => { const result = await this.broadcastTx(txHex); if (result === '') { @@ -81,7 +90,7 @@ export default class PayjoinTransaction { }); } - async isOwnOutputScript(outputScript) { + async isOwnOutputScript(outputScript: Buffer) { const address = bitcoin.address.fromOutputScript(outputScript); return this._wallet.weOwnAddress(address); diff --git a/screen/send/Confirm.tsx b/screen/send/Confirm.tsx index 52e3cc578..4afbfa6eb 100644 --- a/screen/send/Confirm.tsx +++ b/screen/send/Confirm.tsx @@ -24,6 +24,7 @@ import { SendDetailsStackParamList } from '../../navigation/SendDetailsStackPara import { useExtendedNavigation } from '../../hooks/useExtendedNavigation'; import { ContactList } from '../../class/contact-list'; import { useStorage } from '../../hooks/context/useStorage'; +import { HDSegwitBech32Wallet } from '../../class'; enum ActionType { SET_LOADING = 'SET_LOADING', @@ -177,7 +178,7 @@ const Confirm: React.FC = () => { if (!state.isPayjoinEnabled) { await broadcast(tx); } else { - const payJoinWallet = new PayjoinTransaction(psbt, (txHex: string) => broadcast(txHex), wallet); + const payJoinWallet = new PayjoinTransaction(psbt, (txHex: string) => broadcast(txHex), wallet as HDSegwitBech32Wallet); const paymentScript = getPaymentScript(); if (!paymentScript) { throw new Error('Invalid payment script');