-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayjoin-transaction.js
88 lines (75 loc) · 2.71 KB
/
payjoin-transaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as bitcoin from 'bitcoinjs-lib';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import alert from '../components/Alert';
import { ECPairFactory } from 'ecpair';
const ecc = require('tiny-secp256k1');
const ECPair = ECPairFactory(ecc);
const delay = milliseconds => 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) {
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()) {
delete input.finalScriptWitness;
const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script);
const wif = this._wallet._getWifForAddress(address);
const keyPair = ECPair.fromWIF(wif);
unfinalized.signInput(index, keyPair);
}
return unfinalized;
}
/**
* Doesnt conform to spec but needed for user-facing wallet software to find out txid of payjoined transaction
*
* @returns {boolean|Psbt}
*/
getPayjoinPsbt() {
return this._payjoinPsbt;
}
async signPsbt(payjoinPsbt) {
// Do this without relying on private methods
for (const [index, input] of payjoinPsbt.data.inputs.entries()) {
const address = bitcoin.address.fromOutputScript(input.witnessUtxo.script);
try {
const wif = this._wallet._getWifForAddress(address);
const keyPair = ECPair.fromWIF(wif);
payjoinPsbt.signInput(index, keyPair).finalizeInput(index);
} catch (e) {}
}
this._payjoinPsbt = payjoinPsbt;
return this._payjoinPsbt;
}
async broadcastTx(txHex) {
try {
const result = await this._broadcast(txHex);
if (!result) {
throw new Error(`Broadcast failed`);
}
return '';
} catch (e) {
return 'Error: ' + e.message;
}
}
async scheduleBroadcastTx(txHex, milliseconds) {
delay(milliseconds).then(async () => {
const result = await this.broadcastTx(txHex);
if (result === '') {
// TODO: Improve the wording of this error message
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
alert('Something was wrong with the payjoin transaction, the original transaction sucessfully broadcast.');
}
});
}
async isOwnOutputScript(outputScript) {
const address = bitcoin.address.fromOutputScript(outputScript);
return this._wallet.weOwnAddress(address);
}
}