@btc-vision/bitcoin 6.3.6 → 6.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/.mocharc.json +13 -0
  2. package/browser/address.d.ts +1 -1
  3. package/browser/index.js +1 -1
  4. package/browser/index.js.LICENSE.txt +3 -3
  5. package/browser/networks.d.ts +1 -0
  6. package/build/address.d.ts +2 -1
  7. package/build/address.js +68 -13
  8. package/build/block.js +2 -2
  9. package/build/bufferutils.js +5 -5
  10. package/build/networks.d.ts +1 -0
  11. package/build/networks.js +11 -0
  12. package/build/psbt/psbtutils.js +2 -2
  13. package/build/psbt.js +3 -7
  14. package/package.json +26 -26
  15. package/src/address.ts +91 -15
  16. package/src/block.ts +2 -2
  17. package/src/bufferutils.ts +15 -7
  18. package/src/index.ts +86 -86
  19. package/src/networks.ts +12 -0
  20. package/src/psbt/bip371.ts +441 -441
  21. package/src/psbt/psbtutils.ts +320 -319
  22. package/src/psbt.ts +8 -8
  23. package/test/address.spec.ts +55 -77
  24. package/test/bitcoin.core.spec.ts +47 -69
  25. package/test/block.spec.ts +23 -46
  26. package/test/bufferutils.spec.ts +32 -95
  27. package/test/crypto.spec.ts +9 -15
  28. package/test/fixtures/address.json +3 -3
  29. package/test/integration/addresses.spec.ts +12 -24
  30. package/test/integration/bip32.spec.ts +10 -31
  31. package/test/integration/blocks.spec.ts +2 -2
  32. package/test/integration/cltv.spec.ts +21 -63
  33. package/test/integration/csv.spec.ts +30 -105
  34. package/test/integration/payments.spec.ts +16 -41
  35. package/test/integration/taproot.spec.ts +31 -75
  36. package/test/integration/transactions.spec.ts +37 -138
  37. package/test/payments.spec.ts +95 -106
  38. package/test/payments.utils.ts +20 -63
  39. package/test/psbt.spec.ts +100 -229
  40. package/test/script.spec.ts +26 -50
  41. package/test/script_number.spec.ts +6 -9
  42. package/test/script_signature.spec.ts +7 -7
  43. package/test/transaction.spec.ts +46 -96
  44. package/test/ts-node-register.js +3 -1
  45. package/test/tsconfig.json +4 -1
  46. package/test/types.spec.ts +7 -12
  47. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  48. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  49. package/.nyc_output/processinfo/index.json +0 -1
  50. package/test/address.spec.js +0 -124
  51. package/test/bitcoin.core.spec.js +0 -170
  52. package/test/block.spec.js +0 -141
  53. package/test/bufferutils.spec.js +0 -427
  54. package/test/crypto.spec.js +0 -41
  55. package/test/integration/_regtest.js +0 -7
  56. package/test/integration/addresses.spec.js +0 -116
  57. package/test/integration/bip32.spec.js +0 -85
  58. package/test/integration/blocks.spec.js +0 -26
  59. package/test/integration/cltv.spec.js +0 -199
  60. package/test/integration/csv.spec.js +0 -362
  61. package/test/integration/payments.spec.js +0 -98
  62. package/test/integration/taproot.spec.js +0 -532
  63. package/test/integration/transactions.spec.js +0 -561
  64. package/test/payments.spec.js +0 -97
  65. package/test/payments.utils.js +0 -190
  66. package/test/psbt.spec.js +0 -1044
  67. package/test/script.spec.js +0 -151
  68. package/test/script_number.spec.js +0 -24
  69. package/test/script_signature.spec.js +0 -52
  70. package/test/transaction.spec.js +0 -269
  71. package/test/types.spec.js +0 -46
package/test/psbt.spec.js DELETED
@@ -1,1044 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const assert = require("assert");
4
- const bip32_1 = require("bip32");
5
- const ecc = require("tiny-secp256k1");
6
- const crypto = require("crypto");
7
- const ecpair_1 = require("ecpair");
8
- const mocha_1 = require("mocha");
9
- const payments_utils_1 = require("./payments.utils");
10
- const bip341_1 = require("../src/payments/bip341");
11
- const bip371_1 = require("../src/psbt/bip371");
12
- const src_1 = require("../src");
13
- const __1 = require("..");
14
- const preFixtures = require("./fixtures/psbt.json");
15
- const taprootFixtures = require("./fixtures/p2tr.json");
16
- const bip32 = (0, bip32_1.default)(ecc);
17
- const ECPair = (0, ecpair_1.default)(ecc);
18
- const validator = (pubkey, msghash, signature) => ECPair.fromPublicKey(pubkey).verify(msghash, signature);
19
- const schnorrValidator = (pubkey, msghash, signature) => ecc.verifySchnorr(msghash, pubkey, signature);
20
- const initBuffers = (object) => JSON.parse(JSON.stringify(object), (_, value) => {
21
- const regex = new RegExp(/^Buffer.from\(['"](.*)['"], ['"](.*)['"]\)$/);
22
- const result = regex.exec(value);
23
- if (!result)
24
- return value;
25
- const data = result[1];
26
- const encoding = result[2];
27
- return Buffer.from(data, encoding);
28
- });
29
- const fixtures = initBuffers(preFixtures);
30
- const upperCaseFirstLetter = (str) => str.replace(/^./, s => s.toUpperCase());
31
- const toAsyncSigner = (signer) => {
32
- return {
33
- publicKey: signer.publicKey,
34
- sign: (hash, lowerR) => {
35
- return new Promise((resolve, rejects) => {
36
- setTimeout(() => {
37
- try {
38
- const r = signer.sign(hash, lowerR);
39
- resolve(r);
40
- }
41
- catch (e) {
42
- rejects(e);
43
- }
44
- }, 10);
45
- });
46
- },
47
- };
48
- };
49
- const failedAsyncSigner = (publicKey) => {
50
- return {
51
- publicKey,
52
- sign: (__) => {
53
- return new Promise((_, reject) => {
54
- setTimeout(() => {
55
- reject(new Error('sign failed'));
56
- }, 10);
57
- });
58
- },
59
- };
60
- };
61
- // const b = (hex: string) => Buffer.from(hex, 'hex');
62
- (0, mocha_1.describe)(`Psbt`, () => {
63
- beforeEach(() => {
64
- // provide the ECC lib only when required
65
- (0, src_1.initEccLib)(undefined);
66
- });
67
- (0, mocha_1.describe)('BIP174 Test Vectors', () => {
68
- fixtures.bip174.invalid.forEach(f => {
69
- (0, mocha_1.it)(`Invalid: ${f.description}`, () => {
70
- assert.throws(() => {
71
- __1.Psbt.fromBase64(f.psbt);
72
- }, new RegExp(f.errorMessage));
73
- });
74
- });
75
- fixtures.bip174.valid.forEach(f => {
76
- (0, mocha_1.it)(`Valid: ${f.description}`, () => {
77
- assert.doesNotThrow(() => {
78
- __1.Psbt.fromBase64(f.psbt);
79
- });
80
- });
81
- });
82
- fixtures.bip174.failSignChecks.forEach(f => {
83
- const keyPair = ECPair.makeRandom();
84
- (0, mocha_1.it)(`Fails Signer checks: ${f.description}`, () => {
85
- const psbt = __1.Psbt.fromBase64(f.psbt);
86
- assert.throws(() => {
87
- psbt.signInput(f.inputToCheck, keyPair);
88
- }, new RegExp(f.errorMessage));
89
- });
90
- });
91
- fixtures.bip174.creator.forEach(f => {
92
- (0, mocha_1.it)('Creates expected PSBT', () => {
93
- const psbt = new __1.Psbt();
94
- for (const input of f.inputs) {
95
- psbt.addInput(input);
96
- }
97
- for (const output of f.outputs) {
98
- const script = Buffer.from(output.script, 'hex');
99
- psbt.addOutput({ ...output, script });
100
- }
101
- assert.strictEqual(psbt.toBase64(), f.result);
102
- });
103
- });
104
- fixtures.bip174.updater.forEach(f => {
105
- (0, mocha_1.it)('Updates PSBT to the expected result', () => {
106
- if (f.isTaproot)
107
- (0, src_1.initEccLib)(ecc);
108
- const psbt = __1.Psbt.fromBase64(f.psbt);
109
- for (const inputOrOutput of ['input', 'output']) {
110
- const fixtureData = f[`${inputOrOutput}Data`];
111
- if (fixtureData) {
112
- for (const [i, data] of fixtureData.entries()) {
113
- const txt = upperCaseFirstLetter(inputOrOutput);
114
- psbt[`update${txt}`](i, data);
115
- }
116
- }
117
- }
118
- assert.strictEqual(psbt.toBase64(), f.result);
119
- });
120
- });
121
- fixtures.bip174.signer.forEach(f => {
122
- (0, mocha_1.it)('Signs PSBT to the expected result', () => {
123
- if (f.isTaproot)
124
- (0, src_1.initEccLib)(ecc);
125
- const psbt = __1.Psbt.fromBase64(f.psbt);
126
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
127
- // @ts-ignore // cannot find tapLeafHashToSign
128
- f.keys.forEach(({ inputToSign, tapLeafHashToSign, WIF }) => {
129
- const keyPair = ECPair.fromWIF(WIF, __1.networks.testnet);
130
- if (tapLeafHashToSign)
131
- psbt.signTaprootInput(inputToSign, keyPair, Buffer.from(tapLeafHashToSign, 'hex'));
132
- else
133
- psbt.signInput(inputToSign, keyPair);
134
- });
135
- assert.strictEqual(psbt.toBase64(), f.result);
136
- });
137
- });
138
- fixtures.bip174.combiner.forEach(f => {
139
- (0, mocha_1.it)('Combines two PSBTs to the expected result', () => {
140
- const psbts = f.psbts.map(psbt => __1.Psbt.fromBase64(psbt));
141
- psbts[0].combine(psbts[1]);
142
- // Produces a different Base64 string due to implemetation specific key-value ordering.
143
- // That means this test will fail:
144
- // assert.strictEqual(psbts[0].toBase64(), f.result)
145
- // However, if we compare the actual PSBT properties we can see they are logically identical:
146
- assert.deepStrictEqual(psbts[0], __1.Psbt.fromBase64(f.result));
147
- });
148
- });
149
- fixtures.bip174.finalizer.forEach(f => {
150
- (0, mocha_1.it)('Finalizes inputs and gives the expected PSBT', () => {
151
- if (f.isTaproot)
152
- (0, src_1.initEccLib)(ecc);
153
- const psbt = __1.Psbt.fromBase64(f.psbt);
154
- psbt.finalizeAllInputs();
155
- assert.strictEqual(psbt.toBase64(), f.result);
156
- });
157
- });
158
- fixtures.bip174.extractor.forEach(f => {
159
- (0, mocha_1.it)('Extracts the expected transaction from a PSBT', () => {
160
- const psbt1 = __1.Psbt.fromBase64(f.psbt);
161
- const transaction1 = psbt1.extractTransaction(true).toHex();
162
- const psbt2 = __1.Psbt.fromBase64(f.psbt);
163
- const transaction2 = psbt2.extractTransaction().toHex();
164
- assert.strictEqual(transaction1, transaction2);
165
- assert.strictEqual(transaction1, f.transaction);
166
- const psbt3 = __1.Psbt.fromBase64(f.psbt);
167
- delete psbt3.data.inputs[0].finalScriptSig;
168
- delete psbt3.data.inputs[0].finalScriptWitness;
169
- assert.throws(() => {
170
- psbt3.extractTransaction();
171
- }, new RegExp('Not finalized'));
172
- const psbt4 = __1.Psbt.fromBase64(f.psbt);
173
- psbt4.setMaximumFeeRate(1);
174
- assert.throws(() => {
175
- psbt4.extractTransaction();
176
- }, new RegExp('Warning: You are paying around [\\d.]+ in fees'));
177
- const psbt5 = __1.Psbt.fromBase64(f.psbt);
178
- psbt5.extractTransaction(true);
179
- const fr1 = psbt5.getFeeRate();
180
- const fr2 = psbt5.getFeeRate();
181
- assert.strictEqual(fr1, fr2);
182
- const psbt6 = __1.Psbt.fromBase64(f.psbt);
183
- const f1 = psbt6.getFee();
184
- const f2 = psbt6.getFee();
185
- assert.strictEqual(f1, f2);
186
- });
187
- });
188
- });
189
- (0, mocha_1.describe)('signInputAsync', () => {
190
- fixtures.signInput.checks.forEach(f => {
191
- (0, mocha_1.it)(f.description, async () => {
192
- if (f.isTaproot)
193
- (0, src_1.initEccLib)(ecc);
194
- if (f.shouldSign) {
195
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
196
- await assert.doesNotReject(async () => {
197
- await psbtThatShouldsign.signInputAsync(f.shouldSign.inputToCheck, ECPair.fromWIF(f.shouldSign.WIF), f.shouldSign.sighashTypes || undefined);
198
- if (f.shouldSign.result) {
199
- assert.strictEqual(psbtThatShouldsign.toBase64(), f.shouldSign.result);
200
- }
201
- });
202
- const failMessage = f.isTaproot
203
- ? /Need Schnorr Signer to sign taproot input #0./
204
- : /sign failed/;
205
- await assert.rejects(async () => {
206
- await psbtThatShouldsign.signInputAsync(f.shouldSign.inputToCheck, failedAsyncSigner(ECPair.fromWIF(f.shouldSign.WIF).publicKey), f.shouldSign.sighashTypes || undefined);
207
- }, failMessage);
208
- }
209
- if (f.shouldThrow) {
210
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
211
- await assert.rejects(async () => {
212
- await psbtThatShouldThrow.signInputAsync(f.shouldThrow.inputToCheck, ECPair.fromWIF(f.shouldThrow.WIF), f.shouldThrow.sighashTypes || undefined);
213
- }, new RegExp(f.shouldThrow.errorMessage));
214
- await assert.rejects(async () => {
215
- await psbtThatShouldThrow.signInputAsync(f.shouldThrow.inputToCheck, toAsyncSigner(ECPair.fromWIF(f.shouldThrow.WIF)), f.shouldThrow.sighashTypes || undefined);
216
- }, new RegExp(f.shouldThrow.errorMessage));
217
- await assert.rejects(async () => {
218
- await psbtThatShouldThrow.signInputAsync(f.shouldThrow.inputToCheck);
219
- }, new RegExp('Need Signer to sign input'));
220
- }
221
- });
222
- });
223
- });
224
- (0, mocha_1.describe)('signInput', () => {
225
- fixtures.signInput.checks.forEach(f => {
226
- (0, mocha_1.it)(f.description, () => {
227
- if (f.isTaproot)
228
- (0, src_1.initEccLib)(ecc);
229
- if (f.shouldSign) {
230
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
231
- assert.doesNotThrow(() => {
232
- psbtThatShouldsign.signInput(f.shouldSign.inputToCheck, ECPair.fromWIF(f.shouldSign.WIF), f.shouldSign.sighashTypes || undefined);
233
- });
234
- }
235
- if (f.shouldThrow) {
236
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
237
- assert.throws(() => {
238
- psbtThatShouldThrow.signInput(f.shouldThrow.inputToCheck, ECPair.fromWIF(f.shouldThrow.WIF), f.shouldThrow.sighashTypes || undefined);
239
- }, new RegExp(f.shouldThrow.errorMessage));
240
- assert.throws(() => {
241
- psbtThatShouldThrow.signInput(f.shouldThrow.inputToCheck);
242
- }, new RegExp('Need Signer to sign input'));
243
- }
244
- });
245
- });
246
- });
247
- (0, mocha_1.describe)('signAllInputsAsync', () => {
248
- fixtures.signInput.checks.forEach(f => {
249
- if (f.description === 'checks the input exists')
250
- return;
251
- (0, mocha_1.it)(f.description, async () => {
252
- if (f.isTaproot)
253
- (0, src_1.initEccLib)(ecc);
254
- if (f.shouldSign) {
255
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
256
- await assert.doesNotReject(async () => {
257
- await psbtThatShouldsign.signAllInputsAsync(ECPair.fromWIF(f.shouldSign.WIF), f.shouldSign.sighashTypes || undefined);
258
- });
259
- }
260
- if (f.shouldThrow) {
261
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
262
- await assert.rejects(async () => {
263
- await psbtThatShouldThrow.signAllInputsAsync(ECPair.fromWIF(f.shouldThrow.WIF), f.shouldThrow.sighashTypes || undefined);
264
- }, new RegExp('No inputs were signed'));
265
- await assert.rejects(async () => {
266
- await psbtThatShouldThrow.signAllInputsAsync();
267
- }, new RegExp('Need Signer to sign input'));
268
- }
269
- });
270
- });
271
- });
272
- (0, mocha_1.describe)('signAllInputs', () => {
273
- fixtures.signInput.checks.forEach(f => {
274
- if (f.description === 'checks the input exists')
275
- return;
276
- (0, mocha_1.it)(f.description, () => {
277
- if (f.isTaproot)
278
- (0, src_1.initEccLib)(ecc);
279
- if (f.shouldSign) {
280
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
281
- assert.doesNotThrow(() => {
282
- psbtThatShouldsign.signAllInputs(ECPair.fromWIF(f.shouldSign.WIF), f.shouldSign.sighashTypes || undefined);
283
- });
284
- }
285
- if (f.shouldThrow) {
286
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
287
- assert.throws(() => {
288
- psbtThatShouldThrow.signAllInputs(ECPair.fromWIF(f.shouldThrow.WIF), f.shouldThrow.sighashTypes || undefined);
289
- }, new RegExp('No inputs were signed'));
290
- assert.throws(() => {
291
- psbtThatShouldThrow.signAllInputs();
292
- }, new RegExp('Need Signer to sign input'));
293
- }
294
- });
295
- });
296
- });
297
- (0, mocha_1.describe)('signInputHDAsync', () => {
298
- fixtures.signInputHD.checks.forEach(f => {
299
- (0, mocha_1.it)(f.description, async () => {
300
- if (f.shouldSign) {
301
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
302
- await assert.doesNotReject(async () => {
303
- await psbtThatShouldsign.signInputHDAsync(f.shouldSign.inputToCheck, bip32.fromBase58(f.shouldSign.xprv), f.shouldSign.sighashTypes || undefined);
304
- });
305
- }
306
- if (f.shouldThrow) {
307
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
308
- await assert.rejects(async () => {
309
- await psbtThatShouldThrow.signInputHDAsync(f.shouldThrow.inputToCheck, bip32.fromBase58(f.shouldThrow.xprv), f.shouldThrow.sighashTypes || undefined);
310
- }, new RegExp(f.shouldThrow.errorMessage));
311
- await assert.rejects(async () => {
312
- await psbtThatShouldThrow.signInputHDAsync(f.shouldThrow.inputToCheck);
313
- }, new RegExp('Need HDSigner to sign input'));
314
- }
315
- });
316
- });
317
- });
318
- (0, mocha_1.describe)('signInputHD', () => {
319
- fixtures.signInputHD.checks.forEach(f => {
320
- (0, mocha_1.it)(f.description, () => {
321
- if (f.shouldSign) {
322
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
323
- assert.doesNotThrow(() => {
324
- psbtThatShouldsign.signInputHD(f.shouldSign.inputToCheck, bip32.fromBase58(f.shouldSign.xprv), f.shouldSign.sighashTypes || undefined);
325
- });
326
- }
327
- if (f.shouldThrow) {
328
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
329
- assert.throws(() => {
330
- psbtThatShouldThrow.signInputHD(f.shouldThrow.inputToCheck, bip32.fromBase58(f.shouldThrow.xprv), f.shouldThrow.sighashTypes || undefined);
331
- }, new RegExp(f.shouldThrow.errorMessage));
332
- assert.throws(() => {
333
- psbtThatShouldThrow.signInputHD(f.shouldThrow.inputToCheck);
334
- }, new RegExp('Need HDSigner to sign input'));
335
- }
336
- });
337
- });
338
- });
339
- (0, mocha_1.describe)('signAllInputsHDAsync', () => {
340
- fixtures.signInputHD.checks.forEach(f => {
341
- (0, mocha_1.it)(f.description, async () => {
342
- if (f.shouldSign) {
343
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
344
- await assert.doesNotReject(async () => {
345
- await psbtThatShouldsign.signAllInputsHDAsync(bip32.fromBase58(f.shouldSign.xprv), f.shouldSign.sighashTypes || undefined);
346
- });
347
- }
348
- if (f.shouldThrow) {
349
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
350
- await assert.rejects(async () => {
351
- await psbtThatShouldThrow.signAllInputsHDAsync(bip32.fromBase58(f.shouldThrow.xprv), f.shouldThrow.sighashTypes || undefined);
352
- }, new RegExp('No inputs were signed'));
353
- await assert.rejects(async () => {
354
- await psbtThatShouldThrow.signAllInputsHDAsync();
355
- }, new RegExp('Need HDSigner to sign input'));
356
- }
357
- });
358
- });
359
- });
360
- (0, mocha_1.describe)('signAllInputsHD', () => {
361
- fixtures.signInputHD.checks.forEach(f => {
362
- (0, mocha_1.it)(f.description, () => {
363
- if (f.shouldSign) {
364
- const psbtThatShouldsign = __1.Psbt.fromBase64(f.shouldSign.psbt);
365
- assert.doesNotThrow(() => {
366
- psbtThatShouldsign.signAllInputsHD(bip32.fromBase58(f.shouldSign.xprv), f.shouldSign.sighashTypes || undefined);
367
- });
368
- }
369
- if (f.shouldThrow) {
370
- const psbtThatShouldThrow = __1.Psbt.fromBase64(f.shouldThrow.psbt);
371
- assert.throws(() => {
372
- psbtThatShouldThrow.signAllInputsHD(bip32.fromBase58(f.shouldThrow.xprv), f.shouldThrow.sighashTypes || undefined);
373
- }, new RegExp('No inputs were signed'));
374
- assert.throws(() => {
375
- psbtThatShouldThrow.signAllInputsHD();
376
- }, new RegExp('Need HDSigner to sign input'));
377
- }
378
- });
379
- });
380
- });
381
- (0, mocha_1.describe)('finalizeInput', () => {
382
- (0, mocha_1.it)(`Finalizes tapleaf by hash`, () => {
383
- const f = fixtures.finalizeInput.finalizeTapleafByHash;
384
- const psbt = __1.Psbt.fromBase64(f.psbt);
385
- psbt.finalizeTaprootInput(f.index, Buffer.from(f.leafHash, 'hex'));
386
- assert.strictEqual(psbt.toBase64(), f.result);
387
- });
388
- (0, mocha_1.it)(`fails if tapleaf hash not found`, () => {
389
- const f = fixtures.finalizeInput.finalizeTapleafByHash;
390
- const psbt = __1.Psbt.fromBase64(f.psbt);
391
- assert.throws(() => {
392
- psbt.finalizeTaprootInput(f.index, Buffer.from(f.leafHash, 'hex').reverse());
393
- }, new RegExp('Can not finalize taproot input #0. Signature for tapleaf script not found.'));
394
- });
395
- (0, mocha_1.it)(`fails if trying to finalzie non-taproot input`, () => {
396
- const psbt = new __1.Psbt();
397
- psbt.addInput({
398
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
399
- index: 0,
400
- });
401
- assert.throws(() => {
402
- psbt.finalizeTaprootInput(0);
403
- }, new RegExp('Cannot finalize input #0. Not Taproot.'));
404
- });
405
- });
406
- (0, mocha_1.describe)('finalizeAllInputs', () => {
407
- fixtures.finalizeAllInputs.forEach(f => {
408
- (0, mocha_1.it)(`Finalizes inputs of type "${f.type}"`, () => {
409
- const psbt = __1.Psbt.fromBase64(f.psbt);
410
- psbt.finalizeAllInputs();
411
- assert.strictEqual(psbt.toBase64(), f.result);
412
- });
413
- });
414
- (0, mocha_1.it)('fails if no script found', () => {
415
- const psbt = new __1.Psbt();
416
- psbt.addInput({
417
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
418
- index: 0,
419
- });
420
- assert.throws(() => {
421
- psbt.finalizeAllInputs();
422
- }, new RegExp('No script found for input #0'));
423
- psbt.updateInput(0, {
424
- witnessUtxo: {
425
- script: Buffer.from('0014d85c2b71d0060b09c9886aeb815e50991dda124d', 'hex'),
426
- value: 2e5,
427
- },
428
- });
429
- assert.throws(() => {
430
- psbt.finalizeAllInputs();
431
- }, new RegExp('Can not finalize input #0'));
432
- });
433
- });
434
- (0, mocha_1.describe)('addInput', () => {
435
- fixtures.addInput.checks.forEach(f => {
436
- (0, mocha_1.it)(f.description, () => {
437
- const psbt = new __1.Psbt();
438
- if (f.exception) {
439
- assert.throws(() => {
440
- psbt.addInput(f.inputData);
441
- }, new RegExp(f.exception));
442
- assert.throws(() => {
443
- psbt.addInputs([f.inputData]);
444
- }, new RegExp(f.exception));
445
- }
446
- else {
447
- assert.doesNotThrow(() => {
448
- psbt.addInputs([f.inputData]);
449
- if (f.equals) {
450
- assert.strictEqual(psbt.toBase64(), f.equals);
451
- }
452
- });
453
- assert.throws(() => {
454
- psbt.addInput(f.inputData);
455
- }, new RegExp('Duplicate input detected.'));
456
- }
457
- });
458
- });
459
- });
460
- (0, mocha_1.describe)('updateInput', () => {
461
- fixtures.updateInput.checks.forEach(f => {
462
- (0, mocha_1.it)(f.description, () => {
463
- const psbt = __1.Psbt.fromBase64(f.psbt);
464
- if (f.exception) {
465
- assert.throws(() => {
466
- psbt.updateInput(f.index, f.inputData);
467
- }, new RegExp(f.exception));
468
- }
469
- });
470
- });
471
- });
472
- (0, mocha_1.describe)('addOutput', () => {
473
- fixtures.addOutput.checks.forEach(f => {
474
- (0, mocha_1.it)(f.description, () => {
475
- if (f.isTaproot)
476
- (0, src_1.initEccLib)(ecc);
477
- const psbt = f.psbt ? __1.Psbt.fromBase64(f.psbt) : new __1.Psbt();
478
- if (f.exception) {
479
- assert.throws(() => {
480
- psbt.addOutput(f.outputData);
481
- }, new RegExp(f.exception));
482
- assert.throws(() => {
483
- psbt.addOutputs([f.outputData]);
484
- }, new RegExp(f.exception));
485
- }
486
- else {
487
- assert.doesNotThrow(() => {
488
- psbt.addOutput(f.outputData);
489
- });
490
- if (f.result) {
491
- assert.strictEqual(psbt.toBase64(), f.result);
492
- }
493
- assert.doesNotThrow(() => {
494
- psbt.addOutputs([f.outputData]);
495
- });
496
- }
497
- });
498
- });
499
- });
500
- (0, mocha_1.describe)('setVersion', () => {
501
- (0, mocha_1.it)('Sets the version value of the unsigned transaction', () => {
502
- const psbt = new __1.Psbt();
503
- assert.strictEqual(psbt.extractTransaction().version, 2);
504
- psbt.setVersion(1);
505
- assert.strictEqual(psbt.extractTransaction().version, 1);
506
- });
507
- });
508
- (0, mocha_1.describe)('setLocktime', () => {
509
- (0, mocha_1.it)('Sets the nLockTime value of the unsigned transaction', () => {
510
- const psbt = new __1.Psbt();
511
- assert.strictEqual(psbt.extractTransaction().locktime, 0);
512
- psbt.setLocktime(1);
513
- assert.strictEqual(psbt.extractTransaction().locktime, 1);
514
- });
515
- });
516
- (0, mocha_1.describe)('setInputSequence', () => {
517
- (0, mocha_1.it)('Sets the sequence number for a given input', () => {
518
- const psbt = new __1.Psbt();
519
- psbt.addInput({
520
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
521
- index: 0,
522
- });
523
- assert.strictEqual(psbt.inputCount, 1);
524
- assert.strictEqual(psbt.txInputs[0].sequence, 0xffffffff);
525
- psbt.setInputSequence(0, 0);
526
- assert.strictEqual(psbt.txInputs[0].sequence, 0);
527
- });
528
- (0, mocha_1.it)('throws if input index is too high', () => {
529
- const psbt = new __1.Psbt();
530
- psbt.addInput({
531
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
532
- index: 0,
533
- });
534
- assert.throws(() => {
535
- psbt.setInputSequence(1, 0);
536
- }, new RegExp('Input index too high'));
537
- });
538
- });
539
- (0, mocha_1.describe)('getInputType', () => {
540
- const key = ECPair.makeRandom();
541
- const { publicKey } = key;
542
- const p2wpkhPub = (pubkey) => __1.payments.p2wpkh({
543
- pubkey,
544
- }).output;
545
- const p2pkhPub = (pubkey) => __1.payments.p2pkh({
546
- pubkey,
547
- }).output;
548
- const p2shOut = (output) => __1.payments.p2sh({
549
- redeem: { output },
550
- }).output;
551
- const p2wshOut = (output) => __1.payments.p2wsh({
552
- redeem: { output },
553
- }).output;
554
- const p2shp2wshOut = (output) => p2shOut(p2wshOut(output));
555
- const noOuter = (output) => output;
556
- function getInputTypeTest({ innerScript, outerScript, redeemGetter, witnessGetter, expectedType, finalize, }) {
557
- const psbt = new __1.Psbt();
558
- psbt.addInput({
559
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
560
- index: 0,
561
- witnessUtxo: {
562
- script: outerScript(innerScript(publicKey)),
563
- value: 2e3,
564
- },
565
- ...(redeemGetter
566
- ? { redeemScript: redeemGetter(publicKey) }
567
- : {}),
568
- ...(witnessGetter
569
- ? { witnessScript: witnessGetter(publicKey) }
570
- : {}),
571
- }).addOutput({
572
- script: Buffer.from('0014d85c2b71d0060b09c9886aeb815e50991dda124d'),
573
- value: 1800,
574
- });
575
- if (finalize)
576
- psbt.signInput(0, key).finalizeInput(0);
577
- const type = psbt.getInputType(0);
578
- assert.strictEqual(type, expectedType, 'incorrect input type');
579
- }
580
- [
581
- {
582
- innerScript: p2pkhPub,
583
- outerScript: noOuter,
584
- redeemGetter: null,
585
- witnessGetter: null,
586
- expectedType: 'pubkeyhash',
587
- },
588
- {
589
- innerScript: p2wpkhPub,
590
- outerScript: noOuter,
591
- redeemGetter: null,
592
- witnessGetter: null,
593
- expectedType: 'witnesspubkeyhash',
594
- },
595
- {
596
- innerScript: p2pkhPub,
597
- outerScript: p2shOut,
598
- redeemGetter: p2pkhPub,
599
- witnessGetter: null,
600
- expectedType: 'p2sh-pubkeyhash',
601
- },
602
- {
603
- innerScript: p2wpkhPub,
604
- outerScript: p2shOut,
605
- redeemGetter: p2wpkhPub,
606
- witnessGetter: null,
607
- expectedType: 'p2sh-witnesspubkeyhash',
608
- finalize: true,
609
- },
610
- {
611
- innerScript: p2pkhPub,
612
- outerScript: p2wshOut,
613
- redeemGetter: null,
614
- witnessGetter: p2pkhPub,
615
- expectedType: 'p2wsh-pubkeyhash',
616
- finalize: true,
617
- },
618
- {
619
- innerScript: p2pkhPub,
620
- outerScript: p2shp2wshOut,
621
- redeemGetter: (pk) => p2wshOut(p2pkhPub(pk)),
622
- witnessGetter: p2pkhPub,
623
- expectedType: 'p2sh-p2wsh-pubkeyhash',
624
- },
625
- ].forEach(getInputTypeTest);
626
- });
627
- (0, mocha_1.describe)('inputHasHDKey', () => {
628
- (0, mocha_1.it)('should return true if HD key is present', () => {
629
- const root = bip32.fromSeed(crypto.randomBytes(32));
630
- const root2 = bip32.fromSeed(crypto.randomBytes(32));
631
- const path = "m/0'/0";
632
- const psbt = new __1.Psbt();
633
- psbt.addInput({
634
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
635
- index: 0,
636
- bip32Derivation: [
637
- {
638
- masterFingerprint: root.fingerprint,
639
- path,
640
- pubkey: root.derivePath(path).publicKey,
641
- },
642
- ],
643
- });
644
- assert.strictEqual(psbt.inputHasHDKey(0, root), true);
645
- assert.strictEqual(psbt.inputHasHDKey(0, root2), false);
646
- });
647
- });
648
- (0, mocha_1.describe)('inputHasPubkey', () => {
649
- (0, mocha_1.it)('should throw', () => {
650
- const psbt = new __1.Psbt();
651
- psbt.addInput({
652
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
653
- index: 0,
654
- });
655
- assert.throws(() => {
656
- psbt.inputHasPubkey(0, Buffer.from([]));
657
- }, new RegExp("Can't find pubkey in input without Utxo data"));
658
- psbt.updateInput(0, {
659
- witnessUtxo: {
660
- value: 1337,
661
- script: __1.payments.p2sh({
662
- redeem: { output: Buffer.from([0x51]) },
663
- }).output,
664
- },
665
- });
666
- assert.throws(() => {
667
- psbt.inputHasPubkey(0, Buffer.from([]));
668
- }, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
669
- delete psbt.data.inputs[0].witnessUtxo;
670
- psbt.updateInput(0, {
671
- witnessUtxo: {
672
- value: 1337,
673
- script: __1.payments.p2wsh({
674
- redeem: { output: Buffer.from([0x51]) },
675
- }).output,
676
- },
677
- });
678
- assert.throws(() => {
679
- psbt.inputHasPubkey(0, Buffer.from([]));
680
- }, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
681
- delete psbt.data.inputs[0].witnessUtxo;
682
- psbt.updateInput(0, {
683
- witnessUtxo: {
684
- value: 1337,
685
- script: __1.payments.p2sh({
686
- redeem: __1.payments.p2wsh({
687
- redeem: { output: Buffer.from([0x51]) },
688
- }),
689
- }).output,
690
- },
691
- redeemScript: __1.payments.p2wsh({
692
- redeem: { output: Buffer.from([0x51]) },
693
- }).output,
694
- });
695
- assert.throws(() => {
696
- psbt.inputHasPubkey(0, Buffer.from([]));
697
- }, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
698
- psbt.updateInput(0, {
699
- witnessScript: Buffer.from([0x51]),
700
- });
701
- assert.doesNotThrow(() => {
702
- psbt.inputHasPubkey(0, Buffer.from([0x51]));
703
- });
704
- });
705
- });
706
- (0, mocha_1.describe)('outputHasHDKey', () => {
707
- (0, mocha_1.it)('should return true if HD key is present', () => {
708
- const root = bip32.fromSeed(crypto.randomBytes(32));
709
- const root2 = bip32.fromSeed(crypto.randomBytes(32));
710
- const path = "m/0'/0";
711
- const psbt = new __1.Psbt();
712
- psbt.addInput({
713
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
714
- index: 0,
715
- }).addOutput({
716
- script: Buffer.from('0014000102030405060708090a0b0c0d0e0f00010203', 'hex'),
717
- value: 2000,
718
- bip32Derivation: [
719
- {
720
- masterFingerprint: root.fingerprint,
721
- path,
722
- pubkey: root.derivePath(path).publicKey,
723
- },
724
- ],
725
- });
726
- assert.strictEqual(psbt.outputHasHDKey(0, root), true);
727
- assert.strictEqual(psbt.outputHasHDKey(0, root2), false);
728
- });
729
- });
730
- (0, mocha_1.describe)('outputHasPubkey', () => {
731
- (0, mocha_1.it)('should throw', () => {
732
- const psbt = new __1.Psbt();
733
- psbt.addInput({
734
- hash: '0000000000000000000000000000000000000000000000000000000000000000',
735
- index: 0,
736
- }).addOutput({
737
- script: __1.payments.p2sh({
738
- redeem: { output: Buffer.from([0x51]) },
739
- }).output,
740
- value: 1337,
741
- });
742
- assert.throws(() => {
743
- psbt.outputHasPubkey(0, Buffer.from([]));
744
- }, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
745
- psbt.__CACHE.__TX.outs[0].script = __1.payments.p2wsh({
746
- redeem: { output: Buffer.from([0x51]) },
747
- }).output;
748
- assert.throws(() => {
749
- psbt.outputHasPubkey(0, Buffer.from([]));
750
- }, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
751
- psbt.__CACHE.__TX.outs[0].script = __1.payments.p2sh({
752
- redeem: __1.payments.p2wsh({
753
- redeem: { output: Buffer.from([0x51]) },
754
- }),
755
- }).output;
756
- psbt.updateOutput(0, {
757
- redeemScript: __1.payments.p2wsh({
758
- redeem: { output: Buffer.from([0x51]) },
759
- }).output,
760
- });
761
- assert.throws(() => {
762
- psbt.outputHasPubkey(0, Buffer.from([]));
763
- }, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
764
- delete psbt.data.outputs[0].redeemScript;
765
- psbt.updateOutput(0, {
766
- witnessScript: Buffer.from([0x51]),
767
- });
768
- assert.throws(() => {
769
- psbt.outputHasPubkey(0, Buffer.from([]));
770
- }, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
771
- psbt.updateOutput(0, {
772
- redeemScript: __1.payments.p2wsh({
773
- redeem: { output: Buffer.from([0x51]) },
774
- }).output,
775
- });
776
- assert.doesNotThrow(() => {
777
- psbt.outputHasPubkey(0, Buffer.from([0x51]));
778
- });
779
- });
780
- });
781
- (0, mocha_1.describe)('clone', () => {
782
- (0, mocha_1.it)('Should clone a psbt exactly with no reference', () => {
783
- const f = fixtures.clone;
784
- const psbt = __1.Psbt.fromBase64(f.psbt);
785
- const notAClone = Object.assign(new __1.Psbt(), psbt); // references still active
786
- const clone = psbt.clone();
787
- assert.strictEqual(psbt.validateSignaturesOfAllInputs(validator), true);
788
- assert.strictEqual(clone.toBase64(), psbt.toBase64());
789
- assert.strictEqual(clone.toBase64(), notAClone.toBase64());
790
- assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
791
- psbt.__CACHE.__TX.version |= 0xff0000;
792
- assert.notStrictEqual(clone.toBase64(), psbt.toBase64());
793
- assert.notStrictEqual(clone.toBase64(), notAClone.toBase64());
794
- assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
795
- });
796
- });
797
- (0, mocha_1.describe)('setMaximumFeeRate', () => {
798
- (0, mocha_1.it)('Sets the maximumFeeRate value', () => {
799
- const psbt = new __1.Psbt();
800
- assert.strictEqual(psbt.opts.maximumFeeRate, 5000);
801
- psbt.setMaximumFeeRate(6000);
802
- assert.strictEqual(psbt.opts.maximumFeeRate, 6000);
803
- });
804
- });
805
- (0, mocha_1.describe)('validateSignaturesOfInput', () => {
806
- const f = fixtures.validateSignaturesOfInput;
807
- (0, mocha_1.it)('Correctly validates a signature', () => {
808
- const psbt = __1.Psbt.fromBase64(f.psbt);
809
- assert.strictEqual(psbt.validateSignaturesOfInput(f.index, validator), true);
810
- assert.throws(() => {
811
- psbt.validateSignaturesOfInput(f.nonExistantIndex, validator);
812
- }, new RegExp('No signatures to validate'));
813
- });
814
- (0, mocha_1.it)('Correctly validates a signature against a pubkey', () => {
815
- const psbt = __1.Psbt.fromBase64(f.psbt);
816
- assert.strictEqual(psbt.validateSignaturesOfInput(f.index, validator, f.pubkey), true);
817
- assert.throws(() => {
818
- psbt.validateSignaturesOfInput(f.index, validator, f.incorrectPubkey);
819
- }, new RegExp('No signatures for this pubkey'));
820
- });
821
- });
822
- (0, mocha_1.describe)('validateSignaturesOfTapKeyInput', () => {
823
- const f = fixtures.validateSignaturesOfTapKeyInput;
824
- (0, mocha_1.it)('Correctly validates all signatures', () => {
825
- (0, src_1.initEccLib)(ecc);
826
- const psbt = __1.Psbt.fromBase64(f.psbt);
827
- assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator), true);
828
- });
829
- (0, mocha_1.it)('Correctly validates a signature against a pubkey', () => {
830
- (0, src_1.initEccLib)(ecc);
831
- const psbt = __1.Psbt.fromBase64(f.psbt);
832
- assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.pubkey), true);
833
- assert.throws(() => {
834
- psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.incorrectPubkey);
835
- }, new RegExp('No signatures for this pubkey'));
836
- });
837
- });
838
- (0, mocha_1.describe)('validateSignaturesOfTapScriptInput', () => {
839
- const f = fixtures.validateSignaturesOfTapScriptInput;
840
- (0, mocha_1.it)('Correctly validates all signatures', () => {
841
- (0, src_1.initEccLib)(ecc);
842
- const psbt = __1.Psbt.fromBase64(f.psbt);
843
- assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator), true);
844
- });
845
- (0, mocha_1.it)('Correctly validates a signature against a pubkey', () => {
846
- (0, src_1.initEccLib)(ecc);
847
- const psbt = __1.Psbt.fromBase64(f.psbt);
848
- assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.pubkey), true);
849
- assert.throws(() => {
850
- psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.incorrectPubkey);
851
- }, new RegExp('No signatures for this pubkey'));
852
- });
853
- });
854
- (0, mocha_1.describe)('tapTreeToList/tapTreeFromList', () => {
855
- (0, mocha_1.it)('Correctly converts a Taptree to a Tapleaf list and back', () => {
856
- taprootFixtures.valid
857
- .filter(f => f.arguments.scriptTree)
858
- .map(f => f.arguments.scriptTree)
859
- .forEach(scriptTree => {
860
- const originalTree = (0, payments_utils_1.convertScriptTree)(scriptTree, bip341_1.LEAF_VERSION_TAPSCRIPT);
861
- const list = (0, bip371_1.tapTreeToList)(originalTree);
862
- const treeFromList = (0, bip371_1.tapTreeFromList)(list);
863
- assert.deepStrictEqual(treeFromList, originalTree);
864
- });
865
- });
866
- (0, mocha_1.it)('Throws if too many leaves on a given level', () => {
867
- const list = Array.from({ length: 5 }).map(() => ({
868
- depth: 2,
869
- leafVersion: bip341_1.LEAF_VERSION_TAPSCRIPT,
870
- script: Buffer.from([]),
871
- }));
872
- assert.throws(() => {
873
- (0, bip371_1.tapTreeFromList)(list);
874
- }, new RegExp('No room left to insert tapleaf in tree'));
875
- });
876
- (0, mocha_1.it)('Throws if taptree depth is exceeded', () => {
877
- let tree = [
878
- { output: Buffer.from([]) },
879
- { output: Buffer.from([]) },
880
- ];
881
- Array.from({ length: 129 }).forEach(() => (tree = [tree, { output: Buffer.from([]) }]));
882
- assert.throws(() => {
883
- (0, bip371_1.tapTreeToList)(tree);
884
- }, new RegExp('Max taptree depth exceeded.'));
885
- });
886
- (0, mocha_1.it)('Throws if tapleaf depth is to high', () => {
887
- const list = [
888
- {
889
- depth: 129,
890
- leafVersion: bip341_1.LEAF_VERSION_TAPSCRIPT,
891
- script: Buffer.from([]),
892
- },
893
- ];
894
- assert.throws(() => {
895
- (0, bip371_1.tapTreeFromList)(list);
896
- }, new RegExp('Max taptree depth exceeded.'));
897
- });
898
- (0, mocha_1.it)('Throws if not a valid taptree structure', () => {
899
- const tree = Array.from({ length: 3 }).map(() => ({
900
- output: Buffer.from([]),
901
- }));
902
- assert.throws(() => {
903
- (0, bip371_1.tapTreeToList)(tree);
904
- }, new RegExp('Cannot convert taptree to tapleaf list. Expecting a tapree structure.'));
905
- });
906
- });
907
- (0, mocha_1.describe)('getFeeRate', () => {
908
- (0, mocha_1.it)('Throws error if called before inputs are finalized', () => {
909
- const f = fixtures.getFeeRate;
910
- const psbt = __1.Psbt.fromBase64(f.psbt);
911
- assert.throws(() => {
912
- psbt.getFeeRate();
913
- }, new RegExp('PSBT must be finalized to calculate fee rate'));
914
- psbt.finalizeAllInputs();
915
- assert.strictEqual(psbt.getFeeRate(), f.fee);
916
- psbt.__CACHE.__FEE_RATE = undefined;
917
- assert.strictEqual(psbt.getFeeRate(), f.fee);
918
- });
919
- });
920
- (0, mocha_1.describe)('create 1-to-1 transaction', () => {
921
- const alice = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr');
922
- const psbt = new __1.Psbt();
923
- psbt.addInput({
924
- hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
925
- index: 0,
926
- nonWitnessUtxo: Buffer.from('0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
927
- '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
928
- 'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
929
- '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
930
- '631e5e1e66009ce3710ceea5b1ad13ffffffff01905f0100000000001976a9148bb' +
931
- 'c95d2709c71607c60ee3f097c1217482f518d88ac00000000', 'hex'),
932
- sighashType: 1,
933
- });
934
- psbt.addOutput({
935
- address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
936
- value: 80000,
937
- });
938
- psbt.signInput(0, alice);
939
- assert.throws(() => {
940
- psbt.setVersion(3);
941
- }, new RegExp('Can not modify transaction, signatures exist.'));
942
- psbt.validateSignaturesOfInput(0, validator);
943
- psbt.finalizeAllInputs();
944
- assert.throws(() => {
945
- psbt.setVersion(3);
946
- }, new RegExp('Can not modify transaction, signatures exist.'));
947
- assert.strictEqual(psbt.inputHasPubkey(0, alice.publicKey), true);
948
- assert.strictEqual(psbt.outputHasPubkey(0, alice.publicKey), false);
949
- assert.strictEqual(psbt.extractTransaction().toHex(), '02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
950
- 'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' +
951
- 'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' +
952
- '9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' +
953
- 'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' +
954
- '08a22724efa6f6a07b0ec4c79aa88ac00000000');
955
- });
956
- (0, mocha_1.describe)('Method return types', () => {
957
- (0, mocha_1.it)('fromBuffer returns Psbt type (not base class)', () => {
958
- const psbt = __1.Psbt.fromBuffer(Buffer.from('70736274ff01000a01000000000000000000000000', 'hex'));
959
- assert.strictEqual(psbt instanceof __1.Psbt, true);
960
- assert.ok(psbt.__CACHE.__TX);
961
- });
962
- (0, mocha_1.it)('fromBase64 returns Psbt type (not base class)', () => {
963
- const psbt = __1.Psbt.fromBase64('cHNidP8BAAoBAAAAAAAAAAAAAAAA');
964
- assert.strictEqual(psbt instanceof __1.Psbt, true);
965
- assert.ok(psbt.__CACHE.__TX);
966
- });
967
- (0, mocha_1.it)('fromHex returns Psbt type (not base class)', () => {
968
- const psbt = __1.Psbt.fromHex('70736274ff01000a01000000000000000000000000');
969
- assert.strictEqual(psbt instanceof __1.Psbt, true);
970
- assert.ok(psbt.__CACHE.__TX);
971
- });
972
- });
973
- (0, mocha_1.describe)('Cache', () => {
974
- (0, mocha_1.it)('non-witness UTXOs are cached', () => {
975
- const f = fixtures.cache.nonWitnessUtxo;
976
- const psbt = __1.Psbt.fromBase64(f.psbt);
977
- const index = f.inputIndex;
978
- // Cache is empty
979
- assert.strictEqual(psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index], undefined);
980
- // Cache is populated
981
- psbt.updateInput(index, {
982
- nonWitnessUtxo: f.nonWitnessUtxo,
983
- });
984
- const value = psbt.data.inputs[index].nonWitnessUtxo;
985
- assert.ok(psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(value));
986
- assert.ok(psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(f.nonWitnessUtxo));
987
- // Cache is rebuilt from internal transaction object when cleared
988
- psbt.data.inputs[index].nonWitnessUtxo = Buffer.from([1, 2, 3]);
989
- psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index] =
990
- undefined;
991
- assert.ok(psbt.data.inputs[index].nonWitnessUtxo.equals(value));
992
- });
993
- });
994
- (0, mocha_1.describe)('Transaction properties', () => {
995
- (0, mocha_1.it)('.version is exposed and is settable', () => {
996
- const psbt = new __1.Psbt();
997
- assert.strictEqual(psbt.version, 2);
998
- assert.strictEqual(psbt.version, psbt.__CACHE.__TX.version);
999
- psbt.version = 1;
1000
- assert.strictEqual(psbt.version, 1);
1001
- assert.strictEqual(psbt.version, psbt.__CACHE.__TX.version);
1002
- });
1003
- (0, mocha_1.it)('.locktime is exposed and is settable', () => {
1004
- const psbt = new __1.Psbt();
1005
- assert.strictEqual(psbt.locktime, 0);
1006
- assert.strictEqual(psbt.locktime, psbt.__CACHE.__TX.locktime);
1007
- psbt.locktime = 123;
1008
- assert.strictEqual(psbt.locktime, 123);
1009
- assert.strictEqual(psbt.locktime, psbt.__CACHE.__TX.locktime);
1010
- });
1011
- (0, mocha_1.it)('.txInputs is exposed as a readonly clone', () => {
1012
- const psbt = new __1.Psbt();
1013
- const hash = Buffer.alloc(32);
1014
- const index = 0;
1015
- psbt.addInput({ hash, index });
1016
- const input = psbt.txInputs[0];
1017
- const internalInput = psbt.__CACHE.__TX.ins[0];
1018
- assert.ok(input.hash.equals(internalInput.hash));
1019
- assert.strictEqual(input.index, internalInput.index);
1020
- assert.strictEqual(input.sequence, internalInput.sequence);
1021
- input.hash[0] = 123;
1022
- input.index = 123;
1023
- input.sequence = 123;
1024
- assert.ok(!input.hash.equals(internalInput.hash));
1025
- assert.notEqual(input.index, internalInput.index);
1026
- assert.notEqual(input.sequence, internalInput.sequence);
1027
- });
1028
- (0, mocha_1.it)('.txOutputs is exposed as a readonly clone', () => {
1029
- const psbt = new __1.Psbt();
1030
- const address = '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj';
1031
- const value = 100000;
1032
- psbt.addOutput({ address, value });
1033
- const output = psbt.txOutputs[0];
1034
- const internalInput = psbt.__CACHE.__TX.outs[0];
1035
- assert.strictEqual(output.address, address);
1036
- assert.ok(output.script.equals(internalInput.script));
1037
- assert.strictEqual(output.value, internalInput.value);
1038
- output.script[0] = 123;
1039
- output.value = 123;
1040
- assert.ok(!output.script.equals(internalInput.script));
1041
- assert.notEqual(output.value, internalInput.value);
1042
- });
1043
- });
1044
- });