forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.spec.ts
359 lines (299 loc) · 9.83 KB
/
transaction.spec.ts
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
import * as assert from 'assert';
import { beforeEach, describe, it } from 'mocha';
import { Transaction } from '..';
import * as bscript from '../src/script';
import * as fixtures from './fixtures/transaction.json';
describe('Transaction', () => {
function fromRaw(raw: any, noWitness?: boolean): Transaction {
const tx = new Transaction();
tx.version = raw.version;
tx.locktime = raw.locktime;
raw.ins.forEach((txIn: any, i: number) => {
const txHash = Buffer.from(txIn.hash, 'hex');
let scriptSig;
if (txIn.data) {
scriptSig = Buffer.from(txIn.data, 'hex');
} else if (txIn.script) {
scriptSig = bscript.fromASM(txIn.script);
}
tx.addInput(txHash, txIn.index, txIn.sequence, scriptSig);
if (!noWitness && txIn.witness) {
const witness = txIn.witness.map((x: string) => {
return Buffer.from(x, 'hex');
});
tx.setWitness(i, witness);
}
});
raw.outs.forEach((txOut: any) => {
let script: Buffer;
if (txOut.data) {
script = Buffer.from(txOut.data, 'hex');
} else if (txOut.script) {
script = bscript.fromASM(txOut.script);
}
tx.addOutput(script!, txOut.value);
});
return tx;
}
describe('fromBuffer/fromHex', () => {
function importExport(f: any): void {
const id = f.id || f.hash;
const txHex = f.hex || f.txHex;
it('imports ' + f.description + ' (' + id + ')', () => {
const actual = Transaction.fromHex(txHex);
assert.strictEqual(actual.toHex(), txHex);
});
if (f.whex) {
it('imports ' + f.description + ' (' + id + ') as witness', () => {
const actual = Transaction.fromHex(f.whex);
assert.strictEqual(actual.toHex(), f.whex);
});
}
}
fixtures.valid.forEach(importExport);
fixtures.hashForSignature.forEach(importExport);
fixtures.hashForWitnessV0.forEach(importExport);
fixtures.invalid.fromBuffer.forEach(f => {
it('throws on ' + f.exception, () => {
assert.throws(() => {
Transaction.fromHex(f.hex);
}, new RegExp(f.exception));
});
});
it('.version should be interpreted as an int32le', () => {
const txHex = 'ffffffff0000ffffffff';
const tx = Transaction.fromHex(txHex);
assert.strictEqual(-1, tx.version);
assert.strictEqual(0xffffffff, tx.locktime);
});
});
describe('toBuffer/toHex', () => {
fixtures.valid.forEach(f => {
it('exports ' + f.description + ' (' + f.id + ')', () => {
const actual = fromRaw(f.raw, true);
assert.strictEqual(actual.toHex(), f.hex);
});
if (f.whex) {
it('exports ' + f.description + ' (' + f.id + ') as witness', () => {
const wactual = fromRaw(f.raw);
assert.strictEqual(wactual.toHex(), f.whex);
});
}
});
it('accepts target Buffer and offset parameters', () => {
const f = fixtures.valid[0];
const actual = fromRaw(f.raw);
const byteLength = actual.byteLength();
const target = Buffer.alloc(byteLength * 2);
const a = actual.toBuffer(target, 0);
const b = actual.toBuffer(target, byteLength);
assert.strictEqual(a.length, byteLength);
assert.strictEqual(b.length, byteLength);
assert.strictEqual(a.toString('hex'), f.hex);
assert.strictEqual(b.toString('hex'), f.hex);
assert.deepStrictEqual(a, b);
assert.deepStrictEqual(a, target.slice(0, byteLength));
assert.deepStrictEqual(b, target.slice(byteLength));
});
});
describe('hasWitnesses', () => {
fixtures.valid.forEach(f => {
it(
'detects if the transaction has witnesses: ' +
(f.whex ? 'true' : 'false'),
() => {
assert.strictEqual(
Transaction.fromHex(f.whex ? f.whex : f.hex).hasWitnesses(),
!!f.whex,
);
},
);
});
});
describe('weight/virtualSize', () => {
it('computes virtual size', () => {
fixtures.valid.forEach(f => {
const transaction = Transaction.fromHex(f.whex ? f.whex : f.hex);
assert.strictEqual(transaction.virtualSize(), f.virtualSize);
});
});
it('computes weight', () => {
fixtures.valid.forEach(f => {
const transaction = Transaction.fromHex(f.whex ? f.whex : f.hex);
assert.strictEqual(transaction.weight(), f.weight);
});
});
});
describe('addInput', () => {
let prevTxHash: Buffer;
beforeEach(() => {
prevTxHash = Buffer.from(
'ffffffff00ffff000000000000000000000000000000000000000000101010ff',
'hex',
);
});
it('returns an index', () => {
const tx = new Transaction();
assert.strictEqual(tx.addInput(prevTxHash, 0), 0);
assert.strictEqual(tx.addInput(prevTxHash, 0), 1);
});
it('defaults to empty script, witness and 0xffffffff SEQUENCE number', () => {
const tx = new Transaction();
tx.addInput(prevTxHash, 0);
assert.strictEqual(tx.ins[0].script.length, 0);
assert.strictEqual(tx.ins[0].witness.length, 0);
assert.strictEqual(tx.ins[0].sequence, 0xffffffff);
});
fixtures.invalid.addInput.forEach(f => {
it('throws on ' + f.exception, () => {
const tx = new Transaction();
const hash = Buffer.from(f.hash, 'hex');
assert.throws(() => {
tx.addInput(hash, f.index);
}, new RegExp(f.exception));
});
});
});
describe('addOutput', () => {
it('returns an index', () => {
const tx = new Transaction();
assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 0);
assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 1);
});
});
describe('clone', () => {
fixtures.valid.forEach(f => {
let actual: Transaction;
let expected: Transaction;
beforeEach(() => {
expected = Transaction.fromHex(f.hex);
actual = expected.clone();
});
it('should have value equality', () => {
assert.deepStrictEqual(actual, expected);
});
it('should not have reference equality', () => {
assert.notStrictEqual(actual, expected);
});
});
});
describe('getHash/getId', () => {
function verify(f: any): void {
it('should return the id for ' + f.id + '(' + f.description + ')', () => {
const tx = Transaction.fromHex(f.whex || f.hex);
assert.strictEqual(tx.getHash().toString('hex'), f.hash);
assert.strictEqual(tx.getId(), f.id);
});
}
fixtures.valid.forEach(verify);
});
describe('isCoinbase', () => {
function verify(f: any): void {
it(
'should return ' +
f.coinbase +
' for ' +
f.id +
'(' +
f.description +
')',
() => {
const tx = Transaction.fromHex(f.hex);
assert.strictEqual(tx.isCoinbase(), f.coinbase);
},
);
}
fixtures.valid.forEach(verify);
});
describe('hashForSignature', () => {
it('does not use Witness serialization', () => {
const randScript = Buffer.from('6a', 'hex');
const tx = new Transaction();
tx.addInput(
Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000000',
'hex',
),
0,
);
tx.addOutput(randScript, 5000000000);
const original = (tx as any).__toBuffer;
(tx as any).__toBuffer = function(
this: Transaction,
a: any,
b: any,
c: any,
): any {
if (c !== false) throw new Error('hashForSignature MUST pass false');
return original.call(this, a, b, c);
};
assert.throws(() => {
(tx as any).__toBuffer(undefined, undefined, true);
}, /hashForSignature MUST pass false/);
// assert hashForSignature does not pass false
assert.doesNotThrow(() => {
tx.hashForSignature(0, randScript, 1);
});
});
fixtures.hashForSignature.forEach(f => {
it(
'should return ' +
f.hash +
' for ' +
(f.description ? 'case "' + f.description + '"' : f.script),
() => {
const tx = Transaction.fromHex(f.txHex);
const script = bscript.fromASM(f.script);
assert.strictEqual(
tx.hashForSignature(f.inIndex, script, f.type).toString('hex'),
f.hash,
);
},
);
});
});
describe('hashForWitnessV0', () => {
fixtures.hashForWitnessV0.forEach(f => {
it(
'should return ' +
f.hash +
' for ' +
(f.description ? 'case "' + f.description + '"' : ''),
() => {
const tx = Transaction.fromHex(f.txHex);
const script = bscript.fromASM(f.script);
assert.strictEqual(
tx
.hashForWitnessV0(f.inIndex, script, f.value, f.type)
.toString('hex'),
f.hash,
);
},
);
});
});
describe('taprootSigning', () => {
fixtures.taprootSigning.forEach(f => {
const tx = Transaction.fromHex(f.txHex);
const prevOutScripts = f.utxos.map(({ scriptHex }) =>
Buffer.from(scriptHex, 'hex'),
);
const values = f.utxos.map(({ value }) => value);
f.cases.forEach(c => {
let hash: Buffer;
it(`should hash to ${c.hash} for ${f.description}:${c.vin}`, () => {
const hashType = Buffer.from(c.typeHex, 'hex').readUInt8(0);
hash = tx.hashForWitnessV1(c.vin, prevOutScripts, values, hashType);
assert.strictEqual(hash.toString('hex'), c.hash);
});
});
});
});
describe('setWitness', () => {
it('only accepts a a witness stack (Array of Buffers)', () => {
assert.throws(() => {
(new Transaction().setWitness as any)(0, 'foobar');
}, /Expected property "1" of type \[Buffer], got String "foobar"/);
});
});
});