forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhd-segwit-bech32-transaction.js
371 lines (321 loc) · 12.9 KB
/
hd-segwit-bech32-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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import BigNumber from 'bignumber.js';
import * as bitcoin from 'bitcoinjs-lib';
import * as BlueElectrum from '../blue_modules/BlueElectrum';
import { HDSegwitBech32Wallet } from './wallets/hd-segwit-bech32-wallet';
import { SegwitBech32Wallet } from './wallets/segwit-bech32-wallet';
/**
* Represents transaction of a BIP84 wallet.
* Helpers for RBF, CPFP etc.
*/
export class HDSegwitBech32Transaction {
/**
* @param txhex {string|null} Object is initialized with txhex
* @param txid {string|null} If txhex not present - txid whould be present
* @param wallet {HDSegwitBech32Wallet|null} If set - a wallet object to which transacton belongs
*/
constructor(txhex, txid, wallet) {
if (!txhex && !txid) throw new Error('Bad arguments');
this._txhex = txhex;
this._txid = txid;
if (wallet) {
if (wallet.type === HDSegwitBech32Wallet.type) {
/** @type {HDSegwitBech32Wallet} */
this._wallet = wallet;
} else {
throw new Error('Only HD Bech32 wallets supported');
}
}
if (this._txhex) this._txDecoded = bitcoin.Transaction.fromHex(this._txhex);
this._remoteTx = null;
}
/**
* If only txid present - we fetch hex
*
* @returns {Promise<void>}
* @private
*/
async _fetchTxhexAndDecode() {
const hexes = await BlueElectrum.multiGetTransactionByTxid([this._txid], false, 10);
this._txhex = hexes[this._txid];
if (!this._txhex) throw new Error("Transaction can't be found in mempool");
this._txDecoded = bitcoin.Transaction.fromHex(this._txhex);
}
/**
* Returns max used sequence for this transaction. Next RBF transaction
* should have this sequence + 1
*
* @returns {Promise<number>}
*/
async getMaxUsedSequence() {
if (!this._txDecoded) await this._fetchTxhexAndDecode();
let max = 0;
for (const inp of this._txDecoded.ins) {
max = Math.max(inp.sequence, max);
}
return max;
}
/**
* Basic check that Sequence num for this TX is replaceable
*
* @returns {Promise<boolean>}
*/
async isSequenceReplaceable() {
return (await this.getMaxUsedSequence()) < bitcoin.Transaction.DEFAULT_SEQUENCE;
}
/**
* If internal extended tx data not set - this is a method
* to fetch and set this data from electrum. Its different data from
* decoded hex - it contains confirmations etc.
*
* @returns {Promise<void>}
* @private
*/
async _fetchRemoteTx() {
const result = await BlueElectrum.multiGetTransactionByTxid([this._txid || this._txDecoded.getId()], true);
this._remoteTx = Object.values(result)[0];
}
/**
* Fetches from electrum actual confirmations number for this tx
*
* @returns {Promise<Number>}
*/
async getRemoteConfirmationsNum() {
if (!this._remoteTx) await this._fetchRemoteTx();
return this._remoteTx.confirmations || 0; // stupid undefined
}
/**
* Checks that tx belongs to a wallet and also
* tx value is < 0, which means its a spending transaction
* definitely initiated by us, can be RBF'ed.
*
* @returns {Promise<boolean>}
*/
async isOurTransaction() {
if (!this._wallet) throw new Error('Wallet required for this method');
let found = false;
for (const tx of this._wallet.getTransactions()) {
if (tx.txid === (this._txid || this._txDecoded.getId())) {
// its our transaction, and its spending transaction, which means we initiated it
if (tx.value < 0) found = true;
}
}
return found;
}
/**
* Checks that tx belongs to a wallet and also
* tx value is > 0, which means its a receiving transaction and thus
* can be CPFP'ed.
*
* @returns {Promise<boolean>}
*/
async isToUsTransaction() {
if (!this._wallet) throw new Error('Wallet required for this method');
let found = false;
for (const tx of this._wallet.getTransactions()) {
if (tx.txid === (this._txid || this._txDecoded.getId())) {
if (tx.value > 0) found = true;
}
}
return found;
}
/**
* Returns all the info about current transaction which is needed to do a replacement TX
* * fee - current tx fee
* * utxos - UTXOs current tx consumes
* * changeAmount - amount of satoshis that sent to change address (or addresses) we control
* * feeRate - sat/byte for current tx
* * targets - destination(s) of funds (outputs we do not control)
* * unconfirmedUtxos - UTXOs created by this transaction (only the ones we control)
*
* @returns {Promise<{fee: number, utxos: Array, unconfirmedUtxos: Array, changeAmount: number, feeRate: number, targets: Array}>}
*/
async getInfo() {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._remoteTx) await this._fetchRemoteTx();
if (!this._txDecoded) await this._fetchTxhexAndDecode();
const prevInputs = [];
for (const inp of this._txDecoded.ins) {
let reversedHash = Buffer.from(inp.hash).reverse();
reversedHash = reversedHash.toString('hex');
prevInputs.push(reversedHash);
}
const prevTransactions = await BlueElectrum.multiGetTransactionByTxid(prevInputs, true);
// fetched, now lets count how much satoshis went in
let wentIn = 0;
const utxos = [];
for (const inp of this._txDecoded.ins) {
let reversedHash = Buffer.from(inp.hash).reverse();
reversedHash = reversedHash.toString('hex');
if (prevTransactions[reversedHash] && prevTransactions[reversedHash].vout && prevTransactions[reversedHash].vout[inp.index]) {
let value = prevTransactions[reversedHash].vout[inp.index].value;
value = new BigNumber(value).multipliedBy(100000000).toNumber();
wentIn += value;
const address = SegwitBech32Wallet.witnessToAddress(inp.witness[inp.witness.length - 1]);
utxos.push({ vout: inp.index, value, txid: reversedHash, address });
}
}
// counting how much went into actual outputs
let wasSpent = 0;
for (const outp of this._txDecoded.outs) {
wasSpent += +outp.value;
}
const fee = wentIn - wasSpent;
let feeRate = Math.floor(fee / this._txDecoded.virtualSize());
if (feeRate === 0) feeRate = 1;
// lets take a look at change
let changeAmount = 0;
const targets = [];
for (const outp of this._remoteTx.vout) {
const address = outp.scriptPubKey.addresses[0];
const value = new BigNumber(outp.value).multipliedBy(100000000).toNumber();
if (this._wallet.weOwnAddress(address)) {
changeAmount += value;
} else {
// this is target
targets.push({ value, address });
}
}
// lets find outputs we own that current transaction creates. can be used in CPFP
const unconfirmedUtxos = [];
for (const outp of this._remoteTx.vout) {
const address = outp.scriptPubKey.addresses[0];
const value = new BigNumber(outp.value).multipliedBy(100000000).toNumber();
if (this._wallet.weOwnAddress(address)) {
unconfirmedUtxos.push({
vout: outp.n,
value,
txid: this._txid || this._txDecoded.getId(),
address,
});
}
}
return { fee, feeRate, targets, changeAmount, utxos, unconfirmedUtxos };
}
/**
* We get _all_ our UTXOs (even spent kek),
* and see if each input in this transaction's UTXO is in there. If its not there - its an unknown
* input, we dont own it (possibly a payjoin transaction), and we cant do RBF
*
* @returns {Promise<boolean>}
*/
async thereAreUnknownInputsInTx() {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._txDecoded) await this._fetchTxhexAndDecode();
const spentUtxos = this._wallet.getDerivedUtxoFromOurTransaction(true);
for (const inp of this._txDecoded.ins) {
const txidInUtxo = Buffer.from(inp.hash).reverse().toString('hex');
let found = false;
for (const spentU of spentUtxos) {
if (spentU.txid === txidInUtxo && spentU.vout === inp.index) found = true;
}
if (!found) {
return true;
}
}
}
/**
* Checks if all outputs belong to us, that
* means we already canceled this tx and we can only bump fees
*
* @returns {Promise<boolean>}
*/
async canCancelTx() {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._txDecoded) await this._fetchTxhexAndDecode();
if (await this.thereAreUnknownInputsInTx()) return false;
// if theres at least one output we dont own - we can cancel this transaction!
for (const outp of this._txDecoded.outs) {
if (!this._wallet.weOwnAddress(SegwitBech32Wallet.scriptPubKeyToAddress(outp.script))) return true;
}
return false;
}
async canBumpTx() {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._txDecoded) await this._fetchTxhexAndDecode();
if (await this.thereAreUnknownInputsInTx()) return false;
return true;
}
/**
* Creates an RBF transaction that can replace previous one and basically cancel it (rewrite
* output to the one our wallet controls). Note, this cannot add more utxo in RBF transaction if
* newFeerate is too high
*
* @param newFeerate {number} Sat/byte. Should be greater than previous tx feerate
* @returns {Promise<{outputs: Array, tx: Transaction, inputs: Array, fee: Number}>}
*/
async createRBFcancelTx(newFeerate) {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._remoteTx) await this._fetchRemoteTx();
const { feeRate, utxos } = await this.getInfo();
if (newFeerate <= feeRate) throw new Error('New feerate should be bigger than the old one');
const myAddress = await this._wallet.getChangeAddressAsync();
return this._wallet.createTransaction(
utxos,
[{ address: myAddress }],
newFeerate,
/* meaningless in this context */ myAddress,
(await this.getMaxUsedSequence()) + 1,
);
}
/**
* Creates an RBF transaction that can bumps fee of previous one. Note, this cannot add more utxo in RBF
* transaction if newFeerate is too high
*
* @param newFeerate {number} Sat/byte
* @returns {Promise<{outputs: Array, tx: Transaction, inputs: Array, fee: Number}>}
*/
async createRBFbumpFee(newFeerate) {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._remoteTx) await this._fetchRemoteTx();
const { feeRate, targets, changeAmount, utxos } = await this.getInfo();
if (newFeerate <= feeRate) throw new Error('New feerate should be bigger than the old one');
const myAddress = await this._wallet.getChangeAddressAsync();
if (changeAmount === 0) delete targets[0].value;
// looks like this was sendMAX transaction (because there was no change), so we cant reuse amount in this
// target since fee wont change. removing the amount so `createTransaction` will sendMAX correctly with new feeRate
if (targets.length === 0) {
// looks like this was cancelled tx with single change output, so it wasnt included in `this.getInfo()` targets
// so we add output paying ourselves:
targets.push({ address: this._wallet._getInternalAddressByIndex(this._wallet.next_free_change_address_index) });
// not checking emptiness on purpose: it could unpredictably generate too far address because of unconfirmed tx.
}
return this._wallet.createTransaction(utxos, targets, newFeerate, myAddress, (await this.getMaxUsedSequence()) + 1);
}
/**
* Creates a CPFP transaction that can bumps fee of previous one (spends created but not confirmed outputs
* that belong to us). Note, this cannot add more utxo in CPFP transaction if newFeerate is too high
*
* @param newFeerate {number} sat/byte
* @returns {Promise<{outputs: Array, tx: Transaction, inputs: Array, fee: Number}>}
*/
async createCPFPbumpFee(newFeerate) {
if (!this._wallet) throw new Error('Wallet required for this method');
if (!this._remoteTx) await this._fetchRemoteTx();
const { feeRate, fee: oldFee, unconfirmedUtxos } = await this.getInfo();
if (newFeerate <= feeRate) throw new Error('New feerate should be bigger than the old one');
const myAddress = await this._wallet.getChangeAddressAsync();
// calculating feerate for CPFP tx so that average between current and CPFP tx will equal newFeerate.
// this works well if both txs are +/- equal size in bytes
const targetFeeRate = 2 * newFeerate - feeRate;
let add = 0;
while (add <= 128) {
// eslint-disable-next-line no-var
var { tx, inputs, outputs, fee } = this._wallet.createTransaction(
unconfirmedUtxos,
[{ address: myAddress }],
targetFeeRate + add,
myAddress,
HDSegwitBech32Wallet.defaultRBFSequence,
);
const combinedFeeRate = (oldFee + fee) / (this._txDecoded.virtualSize() + tx.virtualSize()); // avg
if (Math.round(combinedFeeRate) < newFeerate) {
add *= 2;
if (!add) add = 2;
} else {
// reached target feerate
break;
}
}
return { tx, inputs, outputs, fee };
}
}