BlueWallet/class/payjoin-transaction.ts

99 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-09-21 21:32:20 +02:00
import * as bitcoin from 'bitcoinjs-lib';
2021-12-20 18:11:07 +01:00
import { ECPairFactory } from 'ecpair';
2024-05-20 11:54:13 +02:00
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
2024-05-20 11:54:13 +02:00
import ecc from '../blue_modules/noble_ecc';
import presentAlert from '../components/Alert';
2024-08-17 12:12:22 +02:00
import { HDSegwitBech32Wallet } from './wallets/hd-segwit-bech32-wallet';
import assert from 'assert';
2021-12-20 18:11:07 +01:00
const ECPair = ECPairFactory(ecc);
2020-09-21 21:32:20 +02:00
2024-08-17 12:12:22 +02:00
const delay = (milliseconds: number) => new Promise(resolve => setTimeout(resolve, milliseconds));
2020-09-21 21:32:20 +02:00
// Implements IPayjoinClientWallet
// https://github.com/bitcoinjs/payjoin-client/blob/master/ts_src/wallet.ts
export default class PayjoinTransaction {
2024-08-17 12:12:22 +02:00
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) {
2020-09-21 21:32:20 +02:00
this._psbt = psbt;
this._broadcast = broadcast;
this._wallet = wallet;
this._payjoinPsbt = false;
}
async getPsbt() {
// Nasty hack to get this working for now
const unfinalized = this._psbt.clone();
for (const [index, input] of unfinalized.data.inputs.entries()) {
2020-09-21 21:32:20 +02:00
delete input.finalScriptWitness;
2024-08-17 12:12:22 +02:00
assert(input.witnessUtxo, 'Internal error: input.witnessUtxo is not set');
2020-09-21 21:32:20 +02:00
const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script);
const wif = this._wallet._getWifForAddress(address);
2021-11-25 16:50:40 +01:00
const keyPair = ECPair.fromWIF(wif);
2020-09-21 21:32:20 +02:00
unfinalized.signInput(index, keyPair);
}
2020-09-21 21:32:20 +02:00
return unfinalized;
}
/**
* Doesnt conform to spec but needed for user-facing wallet software to find out txid of payjoined transaction
*
2024-08-17 12:12:22 +02:00
* @returns {Psbt}
2020-09-21 21:32:20 +02:00
*/
getPayjoinPsbt() {
return this._payjoinPsbt;
}
2024-08-17 12:12:22 +02:00
async signPsbt(payjoinPsbt: bitcoin.Psbt) {
2020-09-21 21:32:20 +02:00
// Do this without relying on private methods
for (const [index, input] of payjoinPsbt.data.inputs.entries()) {
2024-08-17 12:12:22 +02:00
assert(input.witnessUtxo, 'Internal error: input.witnessUtxo is not set');
2020-09-21 21:32:20 +02:00
const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script);
try {
const wif = this._wallet._getWifForAddress(address);
2021-11-25 16:50:40 +01:00
const keyPair = ECPair.fromWIF(wif);
2020-09-21 21:32:20 +02:00
payjoinPsbt.signInput(index, keyPair).finalizeInput(index);
} catch (e) {}
}
2020-09-21 21:32:20 +02:00
this._payjoinPsbt = payjoinPsbt;
return this._payjoinPsbt;
}
2024-08-17 12:12:22 +02:00
async broadcastTx(txHex: string) {
2020-09-21 21:32:20 +02:00
try {
const result = await this._broadcast(txHex);
if (!result) {
throw new Error(`Broadcast failed`);
}
return '';
2024-08-17 12:12:22 +02:00
} catch (e: any) {
2020-09-21 21:32:20 +02:00
return 'Error: ' + e.message;
}
}
2024-08-17 12:12:22 +02:00
async scheduleBroadcastTx(txHex: string, milliseconds: number) {
2020-09-21 21:32:20 +02:00
delay(milliseconds).then(async () => {
const result = await this.broadcastTx(txHex);
if (result === '') {
// TODO: Improve the wording of this error message
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
presentAlert({ message: 'Something was wrong with the payjoin transaction, the original transaction successfully broadcast.' });
2020-09-21 21:32:20 +02:00
}
});
}
2024-08-17 12:12:22 +02:00
async isOwnOutputScript(outputScript: Buffer) {
2020-09-21 21:32:20 +02:00
const address = bitcoin.address.fromOutputScript(outputScript);
return this._wallet.weOwnAddress(address);
}
}