REF: ts convert payjoin-transaction

This commit is contained in:
overtorment 2024-08-17 11:12:22 +01:00
parent df9c7b2811
commit 1c2f1ad3f9
2 changed files with 19 additions and 9 deletions

View File

@ -4,14 +4,21 @@ import { ECPairFactory } from 'ecpair';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback'; import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
import ecc from '../blue_modules/noble_ecc'; import ecc from '../blue_modules/noble_ecc';
import presentAlert from '../components/Alert'; import presentAlert from '../components/Alert';
import { HDSegwitBech32Wallet } from './wallets/hd-segwit-bech32-wallet';
import assert from 'assert';
const ECPair = ECPairFactory(ecc); 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 // Implements IPayjoinClientWallet
// https://github.com/bitcoinjs/payjoin-client/blob/master/ts_src/wallet.ts // https://github.com/bitcoinjs/payjoin-client/blob/master/ts_src/wallet.ts
export default class PayjoinTransaction { export default class PayjoinTransaction {
constructor(psbt, broadcast, wallet) { private _psbt: bitcoin.Psbt;
private _broadcast: (txhex: string) => Promise<true | undefined>;
private _wallet: HDSegwitBech32Wallet;
private _payjoinPsbt: any;
constructor(psbt: bitcoin.Psbt, broadcast: (txhex: string) => Promise<true | undefined>, wallet: HDSegwitBech32Wallet) {
this._psbt = psbt; this._psbt = psbt;
this._broadcast = broadcast; this._broadcast = broadcast;
this._wallet = wallet; this._wallet = wallet;
@ -24,6 +31,7 @@ export default class PayjoinTransaction {
for (const [index, input] of unfinalized.data.inputs.entries()) { for (const [index, input] of unfinalized.data.inputs.entries()) {
delete input.finalScriptWitness; delete input.finalScriptWitness;
assert(input.witnessUtxo, 'Internal error: input.witnessUtxo is not set');
const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script); const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script);
const wif = this._wallet._getWifForAddress(address); const wif = this._wallet._getWifForAddress(address);
const keyPair = ECPair.fromWIF(wif); 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 * Doesnt conform to spec but needed for user-facing wallet software to find out txid of payjoined transaction
* *
* @returns {boolean|Psbt} * @returns {Psbt}
*/ */
getPayjoinPsbt() { getPayjoinPsbt() {
return this._payjoinPsbt; return this._payjoinPsbt;
} }
async signPsbt(payjoinPsbt) { async signPsbt(payjoinPsbt: bitcoin.Psbt) {
// Do this without relying on private methods // Do this without relying on private methods
for (const [index, input] of payjoinPsbt.data.inputs.entries()) { 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); const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script);
try { try {
const wif = this._wallet._getWifForAddress(address); const wif = this._wallet._getWifForAddress(address);
@ -58,19 +67,19 @@ export default class PayjoinTransaction {
return this._payjoinPsbt; return this._payjoinPsbt;
} }
async broadcastTx(txHex) { async broadcastTx(txHex: string) {
try { try {
const result = await this._broadcast(txHex); const result = await this._broadcast(txHex);
if (!result) { if (!result) {
throw new Error(`Broadcast failed`); throw new Error(`Broadcast failed`);
} }
return ''; return '';
} catch (e) { } catch (e: any) {
return 'Error: ' + e.message; return 'Error: ' + e.message;
} }
} }
async scheduleBroadcastTx(txHex, milliseconds) { async scheduleBroadcastTx(txHex: string, milliseconds: number) {
delay(milliseconds).then(async () => { delay(milliseconds).then(async () => {
const result = await this.broadcastTx(txHex); const result = await this.broadcastTx(txHex);
if (result === '') { if (result === '') {
@ -81,7 +90,7 @@ export default class PayjoinTransaction {
}); });
} }
async isOwnOutputScript(outputScript) { async isOwnOutputScript(outputScript: Buffer) {
const address = bitcoin.address.fromOutputScript(outputScript); const address = bitcoin.address.fromOutputScript(outputScript);
return this._wallet.weOwnAddress(address); return this._wallet.weOwnAddress(address);

View File

@ -24,6 +24,7 @@ import { SendDetailsStackParamList } from '../../navigation/SendDetailsStackPara
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation'; import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
import { ContactList } from '../../class/contact-list'; import { ContactList } from '../../class/contact-list';
import { useStorage } from '../../hooks/context/useStorage'; import { useStorage } from '../../hooks/context/useStorage';
import { HDSegwitBech32Wallet } from '../../class';
enum ActionType { enum ActionType {
SET_LOADING = 'SET_LOADING', SET_LOADING = 'SET_LOADING',
@ -177,7 +178,7 @@ const Confirm: React.FC = () => {
if (!state.isPayjoinEnabled) { if (!state.isPayjoinEnabled) {
await broadcast(tx); await broadcast(tx);
} else { } 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(); const paymentScript = getPaymentScript();
if (!paymentScript) { if (!paymentScript) {
throw new Error('Invalid payment script'); throw new Error('Invalid payment script');