@btc-vision/bitcoin 6.3.5 → 6.4.0

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 (72) 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/browser/psbt/psbtutils.d.ts +1 -1
  7. package/build/address.d.ts +1 -1
  8. package/build/address.js +12 -5
  9. package/build/block.js +2 -2
  10. package/build/bufferutils.js +5 -5
  11. package/build/networks.d.ts +1 -0
  12. package/build/networks.js +11 -0
  13. package/build/psbt/psbtutils.js +2 -2
  14. package/build/psbt.js +3 -7
  15. package/package.json +26 -26
  16. package/src/address.ts +20 -6
  17. package/src/block.ts +233 -233
  18. package/src/bufferutils.ts +188 -180
  19. package/src/index.ts +86 -86
  20. package/src/networks.ts +12 -0
  21. package/src/psbt/bip371.ts +441 -441
  22. package/src/psbt/psbtutils.ts +4 -3
  23. package/src/psbt.ts +2187 -2187
  24. package/test/address.spec.ts +155 -177
  25. package/test/bitcoin.core.spec.ts +212 -234
  26. package/test/block.spec.ts +171 -194
  27. package/test/bufferutils.spec.ts +450 -513
  28. package/test/crypto.spec.ts +49 -55
  29. package/test/fixtures/address.json +3 -3
  30. package/test/integration/addresses.spec.ts +142 -154
  31. package/test/integration/bip32.spec.ts +130 -151
  32. package/test/integration/blocks.spec.ts +28 -28
  33. package/test/integration/cltv.spec.ts +241 -283
  34. package/test/integration/csv.spec.ts +452 -527
  35. package/test/integration/payments.spec.ts +110 -135
  36. package/test/integration/taproot.spec.ts +663 -707
  37. package/test/integration/transactions.spec.ts +668 -769
  38. package/test/payments.spec.ts +114 -125
  39. package/test/payments.utils.ts +165 -208
  40. package/test/psbt.spec.ts +1285 -1414
  41. package/test/script.spec.ts +186 -210
  42. package/test/script_number.spec.ts +26 -29
  43. package/test/script_signature.spec.ts +66 -66
  44. package/test/transaction.spec.ts +337 -387
  45. package/test/ts-node-register.js +7 -5
  46. package/test/tsconfig.json +4 -1
  47. package/test/types.spec.ts +53 -58
  48. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  49. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  50. package/.nyc_output/processinfo/index.json +0 -1
  51. package/test/address.spec.js +0 -124
  52. package/test/bitcoin.core.spec.js +0 -170
  53. package/test/block.spec.js +0 -141
  54. package/test/bufferutils.spec.js +0 -427
  55. package/test/crypto.spec.js +0 -41
  56. package/test/integration/_regtest.js +0 -7
  57. package/test/integration/addresses.spec.js +0 -116
  58. package/test/integration/bip32.spec.js +0 -85
  59. package/test/integration/blocks.spec.js +0 -26
  60. package/test/integration/cltv.spec.js +0 -199
  61. package/test/integration/csv.spec.js +0 -362
  62. package/test/integration/payments.spec.js +0 -98
  63. package/test/integration/taproot.spec.js +0 -532
  64. package/test/integration/transactions.spec.js +0 -561
  65. package/test/payments.spec.js +0 -97
  66. package/test/payments.utils.js +0 -190
  67. package/test/psbt.spec.js +0 -1044
  68. package/test/script.spec.js +0 -151
  69. package/test/script_number.spec.js +0 -24
  70. package/test/script_signature.spec.js +0 -52
  71. package/test/transaction.spec.js +0 -269
  72. package/test/types.spec.js +0 -46
@@ -1,387 +1,337 @@
1
- import * as assert from 'assert';
2
- import { beforeEach, describe, it } from 'mocha';
3
- import { Transaction } from '..';
4
- import * as bscript from '../src/script';
5
- import * as fixtures from './fixtures/transaction.json';
6
-
7
- describe('Transaction', () => {
8
- function fromRaw(raw: any, noWitness?: boolean): Transaction {
9
- const tx = new Transaction();
10
- tx.version = raw.version;
11
- tx.locktime = raw.locktime;
12
-
13
- raw.ins.forEach((txIn: any, i: number) => {
14
- const txHash = Buffer.from(txIn.hash, 'hex');
15
- let scriptSig;
16
-
17
- if (txIn.data) {
18
- scriptSig = Buffer.from(txIn.data, 'hex');
19
- } else if (txIn.script) {
20
- scriptSig = bscript.fromASM(txIn.script);
21
- }
22
-
23
- tx.addInput(txHash, txIn.index, txIn.sequence, scriptSig);
24
-
25
- if (!noWitness && txIn.witness) {
26
- const witness = txIn.witness.map((x: string) => {
27
- return Buffer.from(x, 'hex');
28
- });
29
-
30
- tx.setWitness(i, witness);
31
- }
32
- });
33
-
34
- raw.outs.forEach((txOut: any) => {
35
- let script: Buffer;
36
-
37
- if (txOut.data) {
38
- script = Buffer.from(txOut.data, 'hex');
39
- } else if (txOut.script) {
40
- script = bscript.fromASM(txOut.script);
41
- }
42
-
43
- tx.addOutput(script!, txOut.value);
44
- });
45
-
46
- return tx;
47
- }
48
-
49
- describe('fromBuffer/fromHex', () => {
50
- function importExport(f: any): void {
51
- const id = f.id || f.hash;
52
- const txHex = f.hex || f.txHex;
53
-
54
- it('imports ' + f.description + ' (' + id + ')', () => {
55
- const actual = Transaction.fromHex(txHex);
56
-
57
- assert.strictEqual(actual.toHex(), txHex);
58
- });
59
-
60
- if (f.whex) {
61
- it(
62
- 'imports ' + f.description + ' (' + id + ') as witness',
63
- () => {
64
- const actual = Transaction.fromHex(f.whex);
65
-
66
- assert.strictEqual(actual.toHex(), f.whex);
67
- },
68
- );
69
- }
70
- }
71
-
72
- fixtures.valid.forEach(importExport);
73
- fixtures.hashForSignature.forEach(importExport);
74
- fixtures.hashForWitnessV0.forEach(importExport);
75
-
76
- fixtures.invalid.fromBuffer.forEach(f => {
77
- it('throws on ' + f.exception, () => {
78
- assert.throws(() => {
79
- Transaction.fromHex(f.hex);
80
- }, new RegExp(f.exception));
81
- });
82
- });
83
-
84
- it('.version should be interpreted as an int32le', () => {
85
- const txHex = 'ffffffff0000ffffffff';
86
- const tx = Transaction.fromHex(txHex);
87
- assert.strictEqual(-1, tx.version);
88
- assert.strictEqual(0xffffffff, tx.locktime);
89
- });
90
- });
91
-
92
- describe('toBuffer/toHex', () => {
93
- fixtures.valid.forEach(f => {
94
- it('exports ' + f.description + ' (' + f.id + ')', () => {
95
- const actual = fromRaw(f.raw, true);
96
- assert.strictEqual(actual.toHex(), f.hex);
97
- });
98
-
99
- if (f.whex) {
100
- it(
101
- 'exports ' + f.description + ' (' + f.id + ') as witness',
102
- () => {
103
- const wactual = fromRaw(f.raw);
104
- assert.strictEqual(wactual.toHex(), f.whex);
105
- },
106
- );
107
- }
108
- });
109
-
110
- it('accepts target Buffer and offset parameters', () => {
111
- const f = fixtures.valid[0];
112
- const actual = fromRaw(f.raw);
113
- const byteLength = actual.byteLength();
114
-
115
- const target = Buffer.alloc(byteLength * 2);
116
- const a = actual.toBuffer(target, 0);
117
- const b = actual.toBuffer(target, byteLength);
118
-
119
- assert.strictEqual(a.length, byteLength);
120
- assert.strictEqual(b.length, byteLength);
121
- assert.strictEqual(a.toString('hex'), f.hex);
122
- assert.strictEqual(b.toString('hex'), f.hex);
123
- assert.deepStrictEqual(a, b);
124
- assert.deepStrictEqual(a, target.slice(0, byteLength));
125
- assert.deepStrictEqual(b, target.slice(byteLength));
126
- });
127
- });
128
-
129
- describe('hasWitnesses', () => {
130
- fixtures.valid.forEach(f => {
131
- it(
132
- 'detects if the transaction has witnesses: ' +
133
- (f.whex ? 'true' : 'false'),
134
- () => {
135
- assert.strictEqual(
136
- Transaction.fromHex(
137
- f.whex ? f.whex : f.hex,
138
- ).hasWitnesses(),
139
- !!f.whex,
140
- );
141
- },
142
- );
143
- });
144
- });
145
-
146
- describe('weight/virtualSize', () => {
147
- it('computes virtual size', () => {
148
- fixtures.valid.forEach(f => {
149
- const transaction = Transaction.fromHex(
150
- f.whex ? f.whex : f.hex,
151
- );
152
-
153
- assert.strictEqual(transaction.virtualSize(), f.virtualSize);
154
- });
155
- });
156
-
157
- it('computes weight', () => {
158
- fixtures.valid.forEach(f => {
159
- const transaction = Transaction.fromHex(
160
- f.whex ? f.whex : f.hex,
161
- );
162
-
163
- assert.strictEqual(transaction.weight(), f.weight);
164
- });
165
- });
166
- });
167
-
168
- describe('addInput', () => {
169
- let prevTxHash: Buffer;
170
- beforeEach(() => {
171
- prevTxHash = Buffer.from(
172
- 'ffffffff00ffff000000000000000000000000000000000000000000101010ff',
173
- 'hex',
174
- );
175
- });
176
-
177
- it('returns an index', () => {
178
- const tx = new Transaction();
179
- assert.strictEqual(tx.addInput(prevTxHash, 0), 0);
180
- assert.strictEqual(tx.addInput(prevTxHash, 0), 1);
181
- });
182
-
183
- it('defaults to empty script, witness and 0xffffffff SEQUENCE number', () => {
184
- const tx = new Transaction();
185
- tx.addInput(prevTxHash, 0);
186
-
187
- assert.strictEqual(tx.ins[0].script.length, 0);
188
- assert.strictEqual(tx.ins[0].witness.length, 0);
189
- assert.strictEqual(tx.ins[0].sequence, 0xffffffff);
190
- });
191
-
192
- fixtures.invalid.addInput.forEach(f => {
193
- it('throws on ' + f.exception, () => {
194
- const tx = new Transaction();
195
- const hash = Buffer.from(f.hash, 'hex');
196
-
197
- assert.throws(() => {
198
- tx.addInput(hash, f.index);
199
- }, new RegExp(f.exception));
200
- });
201
- });
202
- });
203
-
204
- describe('addOutput', () => {
205
- it('returns an index', () => {
206
- const tx = new Transaction();
207
- assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 0);
208
- assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 1);
209
- });
210
- });
211
-
212
- describe('clone', () => {
213
- fixtures.valid.forEach(f => {
214
- let actual: Transaction;
215
- let expected: Transaction;
216
-
217
- beforeEach(() => {
218
- expected = Transaction.fromHex(f.hex);
219
- actual = expected.clone();
220
- });
221
-
222
- it('should have value equality', () => {
223
- assert.deepStrictEqual(actual, expected);
224
- });
225
-
226
- it('should not have reference equality', () => {
227
- assert.notStrictEqual(actual, expected);
228
- });
229
- });
230
- });
231
-
232
- describe('getHash/getId', () => {
233
- function verify(f: any): void {
234
- it(
235
- 'should return the id for ' + f.id + '(' + f.description + ')',
236
- () => {
237
- const tx = Transaction.fromHex(f.whex || f.hex);
238
-
239
- assert.strictEqual(tx.getHash().toString('hex'), f.hash);
240
- assert.strictEqual(tx.getId(), f.id);
241
- },
242
- );
243
- }
244
-
245
- fixtures.valid.forEach(verify);
246
- });
247
-
248
- describe('isCoinbase', () => {
249
- function verify(f: any): void {
250
- it(
251
- 'should return ' +
252
- f.coinbase +
253
- ' for ' +
254
- f.id +
255
- '(' +
256
- f.description +
257
- ')',
258
- () => {
259
- const tx = Transaction.fromHex(f.hex);
260
-
261
- assert.strictEqual(tx.isCoinbase(), f.coinbase);
262
- },
263
- );
264
- }
265
-
266
- fixtures.valid.forEach(verify);
267
- });
268
-
269
- describe('hashForSignature', () => {
270
- it('does not use Witness serialization', () => {
271
- const randScript = Buffer.from('6a', 'hex');
272
-
273
- const tx = new Transaction();
274
- tx.addInput(
275
- Buffer.from(
276
- '0000000000000000000000000000000000000000000000000000000000000000',
277
- 'hex',
278
- ),
279
- 0,
280
- );
281
- tx.addOutput(randScript, 5000000000);
282
-
283
- const original = (tx as any).__toBuffer;
284
- (tx as any).__toBuffer = function (
285
- this: Transaction,
286
- a: any,
287
- b: any,
288
- c: any,
289
- ): any {
290
- if (c !== false)
291
- throw new Error('hashForSignature MUST pass false');
292
-
293
- return original.call(this, a, b, c);
294
- };
295
-
296
- assert.throws(() => {
297
- (tx as any).__toBuffer(undefined, undefined, true);
298
- }, /hashForSignature MUST pass false/);
299
-
300
- // assert hashForSignature does not pass false
301
- assert.doesNotThrow(() => {
302
- tx.hashForSignature(0, randScript, 1);
303
- });
304
- });
305
-
306
- fixtures.hashForSignature.forEach(f => {
307
- it(
308
- 'should return ' +
309
- f.hash +
310
- ' for ' +
311
- (f.description ? 'case "' + f.description + '"' : f.script),
312
- () => {
313
- const tx = Transaction.fromHex(f.txHex);
314
- const script = bscript.fromASM(f.script);
315
-
316
- assert.strictEqual(
317
- tx
318
- .hashForSignature(f.inIndex, script, f.type)
319
- .toString('hex'),
320
- f.hash,
321
- );
322
- },
323
- );
324
- });
325
- });
326
-
327
- describe('hashForWitnessV0', () => {
328
- fixtures.hashForWitnessV0.forEach(f => {
329
- it(
330
- 'should return ' +
331
- f.hash +
332
- ' for ' +
333
- (f.description ? 'case "' + f.description + '"' : ''),
334
- () => {
335
- const tx = Transaction.fromHex(f.txHex);
336
- const script = bscript.fromASM(f.script);
337
-
338
- assert.strictEqual(
339
- tx
340
- .hashForWitnessV0(
341
- f.inIndex,
342
- script,
343
- f.value,
344
- f.type,
345
- )
346
- .toString('hex'),
347
- f.hash,
348
- );
349
- },
350
- );
351
- });
352
- });
353
-
354
- describe('taprootSigning', () => {
355
- fixtures.taprootSigning.forEach(f => {
356
- const tx = Transaction.fromHex(f.txHex);
357
- const prevOutScripts = f.utxos.map(({ scriptHex }) =>
358
- Buffer.from(scriptHex, 'hex'),
359
- );
360
- const values = f.utxos.map(({ value }) => value);
361
-
362
- f.cases.forEach(c => {
363
- let hash: Buffer;
364
-
365
- it(`should hash to ${c.hash} for ${f.description}:${c.vin}`, () => {
366
- const hashType = Buffer.from(c.typeHex, 'hex').readUInt8(0);
367
-
368
- hash = tx.hashForWitnessV1(
369
- c.vin,
370
- prevOutScripts,
371
- values,
372
- hashType,
373
- );
374
- assert.strictEqual(hash.toString('hex'), c.hash);
375
- });
376
- });
377
- });
378
- });
379
-
380
- describe('setWitness', () => {
381
- it('only accepts a witness stack (Array of Buffers)', () => {
382
- assert.throws(() => {
383
- (new Transaction().setWitness as any)(0, 'foobar');
384
- }, /Expected property "1" of type \[Buffer], got String "foobar"/);
385
- });
386
- });
387
- });
1
+ import assert from 'assert';
2
+ import { beforeEach, describe, it } from 'mocha';
3
+ import { Transaction } from '../src/index.js';
4
+ import * as bscript from '../src/script.js';
5
+ import fixtures from './fixtures/transaction.json' with { type: 'json' };
6
+
7
+ describe('Transaction', () => {
8
+ function fromRaw(raw: any, noWitness?: boolean): Transaction {
9
+ const tx = new Transaction();
10
+ tx.version = raw.version;
11
+ tx.locktime = raw.locktime;
12
+
13
+ raw.ins.forEach((txIn: any, i: number) => {
14
+ const txHash = Buffer.from(txIn.hash, 'hex');
15
+ let scriptSig;
16
+
17
+ if (txIn.data) {
18
+ scriptSig = Buffer.from(txIn.data, 'hex');
19
+ } else if (txIn.script) {
20
+ scriptSig = bscript.fromASM(txIn.script);
21
+ }
22
+
23
+ tx.addInput(txHash, txIn.index, txIn.sequence, scriptSig);
24
+
25
+ if (!noWitness && txIn.witness) {
26
+ const witness = txIn.witness.map((x: string) => {
27
+ return Buffer.from(x, 'hex');
28
+ });
29
+
30
+ tx.setWitness(i, witness);
31
+ }
32
+ });
33
+
34
+ raw.outs.forEach((txOut: any) => {
35
+ let script: Buffer;
36
+
37
+ if (txOut.data) {
38
+ script = Buffer.from(txOut.data, 'hex');
39
+ } else if (txOut.script) {
40
+ script = bscript.fromASM(txOut.script);
41
+ }
42
+
43
+ tx.addOutput(script!, txOut.value);
44
+ });
45
+
46
+ return tx;
47
+ }
48
+
49
+ describe('fromBuffer/fromHex', () => {
50
+ function importExport(f: any): void {
51
+ const id = f.id || f.hash;
52
+ const txHex = f.hex || f.txHex;
53
+
54
+ it('imports ' + f.description + ' (' + id + ')', () => {
55
+ const actual = Transaction.fromHex(txHex);
56
+
57
+ assert.strictEqual(actual.toHex(), txHex);
58
+ });
59
+
60
+ if (f.whex) {
61
+ it('imports ' + f.description + ' (' + id + ') as witness', () => {
62
+ const actual = Transaction.fromHex(f.whex);
63
+
64
+ assert.strictEqual(actual.toHex(), f.whex);
65
+ });
66
+ }
67
+ }
68
+
69
+ fixtures.valid.forEach(importExport);
70
+ fixtures.hashForSignature.forEach(importExport);
71
+ fixtures.hashForWitnessV0.forEach(importExport);
72
+
73
+ fixtures.invalid.fromBuffer.forEach((f) => {
74
+ it('throws on ' + f.exception, () => {
75
+ assert.throws(() => {
76
+ Transaction.fromHex(f.hex);
77
+ }, new RegExp(f.exception));
78
+ });
79
+ });
80
+
81
+ it('.version should be interpreted as an int32le', () => {
82
+ const txHex = 'ffffffff0000ffffffff';
83
+ const tx = Transaction.fromHex(txHex);
84
+ assert.strictEqual(-1, tx.version);
85
+ assert.strictEqual(0xffffffff, tx.locktime);
86
+ });
87
+ });
88
+
89
+ describe('toBuffer/toHex', () => {
90
+ fixtures.valid.forEach((f) => {
91
+ it('exports ' + f.description + ' (' + f.id + ')', () => {
92
+ const actual = fromRaw(f.raw, true);
93
+ assert.strictEqual(actual.toHex(), f.hex);
94
+ });
95
+
96
+ if (f.whex) {
97
+ it('exports ' + f.description + ' (' + f.id + ') as witness', () => {
98
+ const wactual = fromRaw(f.raw);
99
+ assert.strictEqual(wactual.toHex(), f.whex);
100
+ });
101
+ }
102
+ });
103
+
104
+ it('accepts target Buffer and offset parameters', () => {
105
+ const f = fixtures.valid[0];
106
+ const actual = fromRaw(f.raw);
107
+ const byteLength = actual.byteLength();
108
+
109
+ const target = Buffer.alloc(byteLength * 2);
110
+ const a = actual.toBuffer(target, 0);
111
+ const b = actual.toBuffer(target, byteLength);
112
+
113
+ assert.strictEqual(a.length, byteLength);
114
+ assert.strictEqual(b.length, byteLength);
115
+ assert.strictEqual(a.toString('hex'), f.hex);
116
+ assert.strictEqual(b.toString('hex'), f.hex);
117
+ assert.deepStrictEqual(a, b);
118
+ assert.deepStrictEqual(a, target.slice(0, byteLength));
119
+ assert.deepStrictEqual(b, target.slice(byteLength));
120
+ });
121
+ });
122
+
123
+ describe('hasWitnesses', () => {
124
+ fixtures.valid.forEach((f) => {
125
+ it('detects if the transaction has witnesses: ' + (f.whex ? 'true' : 'false'), () => {
126
+ assert.strictEqual(
127
+ Transaction.fromHex(f.whex ? f.whex : f.hex).hasWitnesses(),
128
+ !!f.whex,
129
+ );
130
+ });
131
+ });
132
+ });
133
+
134
+ describe('weight/virtualSize', () => {
135
+ it('computes virtual size', () => {
136
+ fixtures.valid.forEach((f) => {
137
+ const transaction = Transaction.fromHex(f.whex ? f.whex : f.hex);
138
+
139
+ assert.strictEqual(transaction.virtualSize(), f.virtualSize);
140
+ });
141
+ });
142
+
143
+ it('computes weight', () => {
144
+ fixtures.valid.forEach((f) => {
145
+ const transaction = Transaction.fromHex(f.whex ? f.whex : f.hex);
146
+
147
+ assert.strictEqual(transaction.weight(), f.weight);
148
+ });
149
+ });
150
+ });
151
+
152
+ describe('addInput', () => {
153
+ let prevTxHash: Buffer;
154
+ beforeEach(() => {
155
+ prevTxHash = Buffer.from(
156
+ 'ffffffff00ffff000000000000000000000000000000000000000000101010ff',
157
+ 'hex',
158
+ );
159
+ });
160
+
161
+ it('returns an index', () => {
162
+ const tx = new Transaction();
163
+ assert.strictEqual(tx.addInput(prevTxHash, 0), 0);
164
+ assert.strictEqual(tx.addInput(prevTxHash, 0), 1);
165
+ });
166
+
167
+ it('defaults to empty script, witness and 0xffffffff SEQUENCE number', () => {
168
+ const tx = new Transaction();
169
+ tx.addInput(prevTxHash, 0);
170
+
171
+ assert.strictEqual(tx.ins[0].script.length, 0);
172
+ assert.strictEqual(tx.ins[0].witness.length, 0);
173
+ assert.strictEqual(tx.ins[0].sequence, 0xffffffff);
174
+ });
175
+
176
+ fixtures.invalid.addInput.forEach((f) => {
177
+ it('throws on ' + f.exception, () => {
178
+ const tx = new Transaction();
179
+ const hash = Buffer.from(f.hash, 'hex');
180
+
181
+ assert.throws(() => {
182
+ tx.addInput(hash, f.index);
183
+ }, new RegExp(f.exception));
184
+ });
185
+ });
186
+ });
187
+
188
+ describe('addOutput', () => {
189
+ it('returns an index', () => {
190
+ const tx = new Transaction();
191
+ assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 0);
192
+ assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 1);
193
+ });
194
+ });
195
+
196
+ describe('clone', () => {
197
+ fixtures.valid.forEach((f) => {
198
+ let actual: Transaction;
199
+ let expected: Transaction;
200
+
201
+ beforeEach(() => {
202
+ expected = Transaction.fromHex(f.hex);
203
+ actual = expected.clone();
204
+ });
205
+
206
+ it('should have value equality', () => {
207
+ assert.deepStrictEqual(actual, expected);
208
+ });
209
+
210
+ it('should not have reference equality', () => {
211
+ assert.notStrictEqual(actual, expected);
212
+ });
213
+ });
214
+ });
215
+
216
+ describe('getHash/getId', () => {
217
+ function verify(f: any): void {
218
+ it('should return the id for ' + f.id + '(' + f.description + ')', () => {
219
+ const tx = Transaction.fromHex(f.whex || f.hex);
220
+
221
+ assert.strictEqual(tx.getHash().toString('hex'), f.hash);
222
+ assert.strictEqual(tx.getId(), f.id);
223
+ });
224
+ }
225
+
226
+ fixtures.valid.forEach(verify);
227
+ });
228
+
229
+ describe('isCoinbase', () => {
230
+ function verify(f: any): void {
231
+ it('should return ' + f.coinbase + ' for ' + f.id + '(' + f.description + ')', () => {
232
+ const tx = Transaction.fromHex(f.hex);
233
+
234
+ assert.strictEqual(tx.isCoinbase(), f.coinbase);
235
+ });
236
+ }
237
+
238
+ fixtures.valid.forEach(verify);
239
+ });
240
+
241
+ describe('hashForSignature', () => {
242
+ it('does not use Witness serialization', () => {
243
+ const randScript = Buffer.from('6a', 'hex');
244
+
245
+ const tx = new Transaction();
246
+ tx.addInput(
247
+ Buffer.from(
248
+ '0000000000000000000000000000000000000000000000000000000000000000',
249
+ 'hex',
250
+ ),
251
+ 0,
252
+ );
253
+ tx.addOutput(randScript, 5000000000);
254
+
255
+ const original = (tx as any).__toBuffer;
256
+ (tx as any).__toBuffer = function (this: Transaction, a: any, b: any, c: any): any {
257
+ if (c !== false) throw new Error('hashForSignature MUST pass false');
258
+
259
+ return original.call(this, a, b, c);
260
+ };
261
+
262
+ assert.throws(() => {
263
+ (tx as any).__toBuffer(undefined, undefined, true);
264
+ }, /hashForSignature MUST pass false/);
265
+
266
+ // assert hashForSignature does not pass false
267
+ assert.doesNotThrow(() => {
268
+ tx.hashForSignature(0, randScript, 1);
269
+ });
270
+ });
271
+
272
+ fixtures.hashForSignature.forEach((f) => {
273
+ it(
274
+ 'should return ' +
275
+ f.hash +
276
+ ' for ' +
277
+ (f.description ? 'case "' + f.description + '"' : f.script),
278
+ () => {
279
+ const tx = Transaction.fromHex(f.txHex);
280
+ const script = bscript.fromASM(f.script);
281
+
282
+ assert.strictEqual(
283
+ tx.hashForSignature(f.inIndex, script, f.type).toString('hex'),
284
+ f.hash,
285
+ );
286
+ },
287
+ );
288
+ });
289
+ });
290
+
291
+ describe('hashForWitnessV0', () => {
292
+ fixtures.hashForWitnessV0.forEach((f) => {
293
+ it(
294
+ 'should return ' +
295
+ f.hash +
296
+ ' for ' +
297
+ (f.description ? 'case "' + f.description + '"' : ''),
298
+ () => {
299
+ const tx = Transaction.fromHex(f.txHex);
300
+ const script = bscript.fromASM(f.script);
301
+
302
+ assert.strictEqual(
303
+ tx.hashForWitnessV0(f.inIndex, script, f.value, f.type).toString('hex'),
304
+ f.hash,
305
+ );
306
+ },
307
+ );
308
+ });
309
+ });
310
+
311
+ describe('taprootSigning', () => {
312
+ fixtures.taprootSigning.forEach((f) => {
313
+ const tx = Transaction.fromHex(f.txHex);
314
+ const prevOutScripts = f.utxos.map(({ scriptHex }) => Buffer.from(scriptHex, 'hex'));
315
+ const values = f.utxos.map(({ value }) => value);
316
+
317
+ f.cases.forEach((c) => {
318
+ let hash: Buffer;
319
+
320
+ it(`should hash to ${c.hash} for ${f.description}:${c.vin}`, () => {
321
+ const hashType = Buffer.from(c.typeHex, 'hex').readUInt8(0);
322
+
323
+ hash = tx.hashForWitnessV1(c.vin, prevOutScripts, values, hashType);
324
+ assert.strictEqual(hash.toString('hex'), c.hash);
325
+ });
326
+ });
327
+ });
328
+ });
329
+
330
+ describe('setWitness', () => {
331
+ it('only accepts a witness stack (Array of Buffers)', () => {
332
+ assert.throws(() => {
333
+ (new Transaction().setWitness as any)(0, 'foobar');
334
+ }, /Expected property "1" of type \[Buffer], got String "foobar"/);
335
+ });
336
+ });
337
+ });