@btc-vision/bitcoin 6.3.6 → 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.
- package/.mocharc.json +13 -0
- package/browser/address.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/index.js.LICENSE.txt +3 -3
- package/browser/networks.d.ts +1 -0
- package/build/address.d.ts +1 -1
- package/build/address.js +12 -5
- package/build/block.js +2 -2
- package/build/bufferutils.js +5 -5
- package/build/networks.d.ts +1 -0
- package/build/networks.js +11 -0
- package/build/psbt/psbtutils.js +2 -2
- package/build/psbt.js +3 -7
- package/package.json +26 -26
- package/src/address.ts +20 -6
- package/src/block.ts +233 -233
- package/src/bufferutils.ts +188 -180
- package/src/index.ts +86 -86
- package/src/networks.ts +12 -0
- package/src/psbt/bip371.ts +441 -441
- package/src/psbt/psbtutils.ts +4 -3
- package/src/psbt.ts +2187 -2187
- package/test/address.spec.ts +155 -177
- package/test/bitcoin.core.spec.ts +212 -234
- package/test/block.spec.ts +171 -194
- package/test/bufferutils.spec.ts +450 -513
- package/test/crypto.spec.ts +49 -55
- package/test/fixtures/address.json +3 -3
- package/test/integration/addresses.spec.ts +142 -154
- package/test/integration/bip32.spec.ts +130 -151
- package/test/integration/blocks.spec.ts +28 -28
- package/test/integration/cltv.spec.ts +241 -283
- package/test/integration/csv.spec.ts +452 -527
- package/test/integration/payments.spec.ts +110 -135
- package/test/integration/taproot.spec.ts +663 -707
- package/test/integration/transactions.spec.ts +668 -769
- package/test/payments.spec.ts +114 -125
- package/test/payments.utils.ts +165 -208
- package/test/psbt.spec.ts +1285 -1414
- package/test/script.spec.ts +186 -210
- package/test/script_number.spec.ts +26 -29
- package/test/script_signature.spec.ts +66 -66
- package/test/transaction.spec.ts +337 -387
- package/test/ts-node-register.js +7 -5
- package/test/tsconfig.json +4 -1
- package/test/types.spec.ts +53 -58
- package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
- package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/test/address.spec.js +0 -124
- package/test/bitcoin.core.spec.js +0 -170
- package/test/block.spec.js +0 -141
- package/test/bufferutils.spec.js +0 -427
- package/test/crypto.spec.js +0 -41
- package/test/integration/_regtest.js +0 -7
- package/test/integration/addresses.spec.js +0 -116
- package/test/integration/bip32.spec.js +0 -85
- package/test/integration/blocks.spec.js +0 -26
- package/test/integration/cltv.spec.js +0 -199
- package/test/integration/csv.spec.js +0 -362
- package/test/integration/payments.spec.js +0 -98
- package/test/integration/taproot.spec.js +0 -532
- package/test/integration/transactions.spec.js +0 -561
- package/test/payments.spec.js +0 -97
- package/test/payments.utils.js +0 -190
- package/test/psbt.spec.js +0 -1044
- package/test/script.spec.js +0 -151
- package/test/script_number.spec.js +0 -24
- package/test/script_signature.spec.js +0 -52
- package/test/transaction.spec.js +0 -269
- package/test/types.spec.js +0 -46
package/test/transaction.spec.ts
CHANGED
|
@@ -1,387 +1,337 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { beforeEach, describe, it } from 'mocha';
|
|
3
|
-
import { Transaction } from '
|
|
4
|
-
import * as bscript from '../src/script';
|
|
5
|
-
import
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
fixtures.
|
|
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
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
assert.
|
|
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
|
-
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
|
+
});
|