forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx-test.js
1114 lines (910 loc) · 30.4 KB
/
tx-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const {inspect} = require('util');
const {encoding} = require('bufio');
const assert = require('./util/assert');
const random = require('bcrypto/lib/random');
const util = require('../lib/utils/util');
const consensus = require('../lib/protocol/consensus');
const TX = require('../lib/primitives/tx');
const MTX = require('../lib/primitives/mtx');
const Output = require('../lib/primitives/output');
const Outpoint = require('../lib/primitives/outpoint');
const Script = require('../lib/script/script');
const Witness = require('../lib/script/witness');
const Opcode = require('../lib/script/opcode');
const Input = require('../lib/primitives/input');
const CoinView = require('../lib/coins/coinview');
const KeyRing = require('../lib/primitives/keyring');
const Address = require('../lib/primitives/address');
const BufferWriter = require('bufio').BufferWriter;
const common = require('./util/common');
const validTests = require('./data/tx-valid.json');
const invalidTests = require('./data/tx-invalid.json');
const sighashTests = require('./data/sighash-tests.json');
const tx1 = common.readTX('tx1');
const tx2 = common.readTX('tx2');
const tx3 = common.readTX('tx3');
const tx4 = common.readTX('tx4');
const tx5 = common.readTX('tx5');
const tx6 = common.readTX('tx6');
const tx7 = common.readTX('tx7');
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
const MAX_SAFE_ADDITION = 0xfffffffffffff;
function clearCache(tx, noCache) {
if (noCache) {
tx.refresh();
return;
}
const copy = tx.clone();
assert.bufferEqual(tx.hash(), copy.hash());
assert.bufferEqual(tx.witnessHash(), copy.witnessHash());
}
function parseTXTest(data) {
const coins = data[0];
const hex = data[1];
const names = data[2] || 'NONE';
let flags = 0;
for (const name of names.split(',')) {
const flag = Script.flags[`VERIFY_${name}`];
if (flag == null)
throw new Error(`Unknown flag: ${name}.`);
flags |= flag;
}
const view = new CoinView();
for (const [txid, index, str, amount] of coins) {
const hash = util.fromRev(txid);
const script = Script.fromString(str);
const value = parseInt(amount || '0', 10);
// Ignore the coinbase tests.
// They should all fail.
if ((index >>> 0) === 0xffffffff)
continue;
const prevout = new Outpoint(hash, index);
const output = new Output({script, value});
view.addOutput(prevout, output);
}
const raw = Buffer.from(hex, 'hex');
const tx = TX.fromRaw(raw);
const coin = view.getOutputFor(tx.inputs[0]);
return {
tx: tx,
flags: flags,
view: view,
comments: coin
? inspect(coin.script)
: 'coinbase',
data: data
};
}
function parseSighashTest(data) {
const [txHex, scriptHex, index, type, hash] = data;
const tx = TX.fromRaw(txHex, 'hex');
const script = Script.fromRaw(scriptHex, 'hex');
const expected = util.fromRev(hash);
let hex = type & 3;
if (type & 0x80)
hex |= 0x80;
hex = hex.toString(16);
if (hex.length % 2 !== 0)
hex = '0' + hex;
return {
tx: tx,
script: script,
index: index,
type: type,
hash: hash,
expected: expected,
hex: hex
};
}
function createInput(value, view) {
const hash = random.randomBytes(32);
const input = {
prevout: {
hash: hash,
index: 0
}
};
const output = new Output();
output.value = value;
if (!view)
view = new CoinView();
view.addOutput(new Outpoint(hash, 0), output);
return [input, view];
};
function sigopContext(scriptSig, witness, scriptPubkey) {
const fund = new TX();
{
fund.version = 1;
const input = new Input();
fund.inputs.push(input);
const output = new Output();
output.value = 1;
output.script = scriptPubkey;
fund.outputs.push(output);
fund.refresh();
}
const spend = new TX();
{
spend.version = 1;
const input = new Input();
input.prevout.hash = fund.hash();
input.prevout.index = 0;
input.script = scriptSig;
input.witness = witness;
spend.inputs.push(input);
const output = new Output();
output.value = 1;
spend.outputs.push(output);
spend.refresh();
}
const view = new CoinView();
view.addTX(fund, 0);
return {
fund: fund,
spend: spend,
view: view
};
}
describe('TX', function() {
for (const noCache of [false, true]) {
const suffix = noCache ? 'without cache' : 'with cache';
it(`should verify non-minimal output ${suffix}`, () => {
const [tx, view] = tx1.getTX();
clearCache(tx, noCache);
assert(tx.verify(view, Script.flags.VERIFY_P2SH));
});
it(`should verify tx.version == 0 ${suffix}`, () => {
const [tx, view] = tx2.getTX();
clearCache(tx, noCache);
assert(tx.verify(view, Script.flags.VERIFY_P2SH));
});
it(`should verify sighash_single bug w/ findanddelete ${suffix}`, () => {
const [tx, view] = tx3.getTX();
clearCache(tx, noCache);
assert(tx.verify(view, Script.flags.VERIFY_P2SH));
});
it(`should verify high S value with only DERSIG enabled ${suffix}`, () => {
const [tx, view] = tx4.getTX();
const coin = view.getOutputFor(tx.inputs[0]);
const flags = Script.flags.VERIFY_P2SH | Script.flags.VERIFY_DERSIG;
clearCache(tx, noCache);
assert(tx.verifyInput(0, coin, flags));
});
it(`should parse witness tx properly ${suffix}`, () => {
const [tx] = tx5.getTX();
clearCache(tx, noCache);
assert.strictEqual(tx.inputs.length, 5);
assert.strictEqual(tx.outputs.length, 1980);
assert(tx.hasWitness());
assert.notStrictEqual(tx.txid(), tx.wtxid());
assert.strictEqual(tx.witnessHash().toString('hex'),
'088c919cd8408005f255c411f786928385688a9e8fdb2db4c9bc3578ce8c94cf');
assert.strictEqual(tx.getSize(), 62138);
assert.strictEqual(tx.getVirtualSize(), 61813);
assert.strictEqual(tx.getWeight(), 247250);
const raw1 = tx.toRaw();
tx.refresh();
const raw2 = tx.toRaw();
assert.bufferEqual(raw1, raw2);
const tx2 = TX.fromRaw(raw2);
clearCache(tx2, noCache);
assert.strictEqual(tx.txid(), tx2.txid());
assert.strictEqual(tx.wtxid(), tx2.wtxid());
assert.notStrictEqual(tx.txid(), tx2.wtxid());
});
it(`should verify the coolest tx ever sent ${suffix}`, () => {
const [tx, view] = tx6.getTX();
clearCache(tx, noCache);
assert(tx.verify(view, Script.flags.VERIFY_NONE));
});
it(`should verify a historical transaction ${suffix}`, () => {
const [tx, view] = tx7.getTX();
clearCache(tx, noCache);
assert(tx.verify(view));
});
for (const tests of [validTests, invalidTests]) {
let comment = '';
for (const json of tests) {
if (json.length === 1) {
comment += ' ' + json[0];
continue;
}
const data = parseTXTest(json);
const {tx, view, flags} = data;
const comments = comment.trim() || data.comments;
comment = '';
if (tests === validTests) {
if (comments.indexOf('Coinbase') === 0) {
it(`should handle valid tx test ${suffix}: ${comments}`, () => {
clearCache(tx, noCache);
assert.ok(tx.isSane());
});
continue;
}
it(`should handle valid tx test ${suffix}: ${comments}`, () => {
clearCache(tx, noCache);
assert.ok(tx.verify(view, flags));
});
} else {
if (comments === 'Duplicate inputs') {
it(`should handle invalid tx test ${suffix}: ${comments}`, () => {
clearCache(tx, noCache);
assert.ok(tx.verify(view, flags));
assert.ok(!tx.isSane());
});
continue;
}
if (comments === 'Negative output') {
it(`should handle invalid tx test ${suffix}: ${comments}`, () => {
clearCache(tx, noCache);
assert.ok(tx.verify(view, flags));
assert.ok(!tx.isSane());
});
continue;
}
if (comments.indexOf('Coinbase') === 0) {
it(`should handle invalid tx test ${suffix}: ${comments}`, () => {
clearCache(tx, noCache);
assert.ok(!tx.isSane());
});
continue;
}
it(`should handle invalid tx test ${suffix}: ${comments}`, () => {
clearCache(tx, noCache);
assert.ok(!tx.verify(view, flags));
});
}
}
}
for (const json of sighashTests) {
if (json.length === 1)
continue;
const test = parseSighashTest(json);
const {tx, script, index, type} = test;
const {hash, hex, expected} = test;
clearCache(tx, noCache);
it(`should get sighash of ${hash} (${hex}) ${suffix}`, () => {
const subscript = script.getSubscript(0).removeSeparators();
const hash = tx.signatureHash(index, subscript, 0, type, 0);
assert.bufferEqual(hash, expected);
});
}
}
it('should fail on >51 bit coin values', () => {
const [input, view] = createInput(consensus.MAX_MONEY + 1);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: consensus.MAX_MONEY
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should handle 51 bit coin values', () => {
const [input, view] = createInput(consensus.MAX_MONEY);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: consensus.MAX_MONEY
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(tx.verifyInputs(view, 0));
});
it('should fail on >51 bit output values', () => {
const [input, view] = createInput(consensus.MAX_MONEY);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: consensus.MAX_MONEY + 1
}],
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should handle 51 bit output values', () => {
const [input, view] = createInput(consensus.MAX_MONEY);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: consensus.MAX_MONEY
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(tx.verifyInputs(view, 0));
});
it('should fail on >51 bit fees', () => {
const [input, view] = createInput(consensus.MAX_MONEY + 1);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: 0
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >51 bit values from multiple', () => {
const view = new CoinView();
const tx = new TX({
version: 1,
inputs: [
createInput(Math.floor(consensus.MAX_MONEY / 2), view)[0],
createInput(Math.floor(consensus.MAX_MONEY / 2), view)[0],
createInput(Math.floor(consensus.MAX_MONEY / 2), view)[0]
],
outputs: [{
script: [],
value: consensus.MAX_MONEY
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >51 bit output values from multiple', () => {
const [input, view] = createInput(consensus.MAX_MONEY);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [
{
script: [],
value: Math.floor(consensus.MAX_MONEY / 2)
},
{
script: [],
value: Math.floor(consensus.MAX_MONEY / 2)
},
{
script: [],
value: Math.floor(consensus.MAX_MONEY / 2)
}
],
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >51 bit fees from multiple', () => {
const view = new CoinView();
const tx = new TX({
version: 1,
inputs: [
createInput(Math.floor(consensus.MAX_MONEY / 2), view)[0],
createInput(Math.floor(consensus.MAX_MONEY / 2), view)[0],
createInput(Math.floor(consensus.MAX_MONEY / 2), view)[0]
],
outputs: [{
script: [],
value: 0
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail to parse >53 bit values', () => {
const [input] = createInput(Math.floor(consensus.MAX_MONEY / 2));
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: 0xdeadbeef
}],
locktime: 0
});
let raw = tx.toRaw();
assert.strictEqual(encoding.readU64(raw, 47), 0xdeadbeef);
raw[54] = 0x7f;
assert.throws(() => TX.fromRaw(raw));
tx.outputs[0].value = 0;
tx.refresh();
raw = tx.toRaw();
assert.strictEqual(encoding.readU64(raw, 47), 0x00);
raw[54] = 0x80;
assert.throws(() => TX.fromRaw(raw));
});
it('should fail on 53 bit coin values', () => {
const [input, view] = createInput(MAX_SAFE_INTEGER);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: consensus.MAX_MONEY
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on 53 bit output values', () => {
const [input, view] = createInput(consensus.MAX_MONEY);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: MAX_SAFE_INTEGER
}],
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on 53 bit fees', () => {
const [input, view] = createInput(MAX_SAFE_INTEGER);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: 0
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
for (const value of [MAX_SAFE_ADDITION, MAX_SAFE_INTEGER]) {
it('should fail on >53 bit values from multiple', () => {
const view = new CoinView();
const tx = new TX({
version: 1,
inputs: [
createInput(value, view)[0],
createInput(value, view)[0],
createInput(value, view)[0]
],
outputs: [{
script: [],
value: consensus.MAX_MONEY
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >53 bit output values from multiple', () => {
const [input, view] = createInput(consensus.MAX_MONEY);
const tx = new TX({
version: 1,
inputs: [input],
outputs: [
{
script: [],
value: value
},
{
script: [],
value: value
},
{
script: [],
value: value
}
],
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >53 bit fees from multiple', () => {
const view = new CoinView();
const tx = new TX({
version: 1,
inputs: [
createInput(value, view)[0],
createInput(value, view)[0],
createInput(value, view)[0]
],
outputs: [{
script: [],
value: 0
}],
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.verifyInputs(view, 0));
});
}
it('should count sigops for multisig', () => {
const flags = Script.flags.VERIFY_WITNESS | Script.flags.VERIFY_P2SH;
const key = KeyRing.generate();
const pub = key.publicKey;
const output = Script.fromMultisig(1, 2, [pub, pub]);
const input = new Script([
Opcode.fromInt(0),
Opcode.fromInt(0)
]);
const witness = new Witness();
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 0);
assert.strictEqual(ctx.fund.getSigopsCost(ctx.view, flags),
consensus.MAX_MULTISIG_PUBKEYS * consensus.WITNESS_SCALE_FACTOR);
});
it('should count sigops for p2sh multisig', () => {
const flags = Script.flags.VERIFY_WITNESS | Script.flags.VERIFY_P2SH;
const key = KeyRing.generate();
const pub = key.publicKey;
const redeem = Script.fromMultisig(1, 2, [pub, pub]);
const output = Script.fromScripthash(redeem.hash160());
const input = new Script([
Opcode.fromInt(0),
Opcode.fromInt(0),
Opcode.fromData(redeem.toRaw())
]);
const witness = new Witness();
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags),
2 * consensus.WITNESS_SCALE_FACTOR);
});
it('should count sigops for p2wpkh', () => {
const flags = Script.flags.VERIFY_WITNESS | Script.flags.VERIFY_P2SH;
const key = KeyRing.generate();
const witness = new Witness([
Buffer.from([0]),
Buffer.from([0])
]);
const input = new Script();
{
const output = Script.fromProgram(0, key.getKeyHash());
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 1);
assert.strictEqual(
ctx.spend.getSigopsCost(ctx.view, flags & ~Script.flags.VERIFY_WITNESS),
0);
}
{
const output = Script.fromProgram(1, key.getKeyHash());
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 0);
}
{
const output = Script.fromProgram(0, key.getKeyHash());
const ctx = sigopContext(input, witness, output);
ctx.spend.inputs[0].prevout.hash = consensus.ZERO_HASH;
ctx.spend.inputs[0].prevout.index = 0xffffffff;
ctx.spend.refresh();
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 0);
}
});
it('should count sigops for nested p2wpkh', () => {
const flags = Script.flags.VERIFY_WITNESS | Script.flags.VERIFY_P2SH;
const key = KeyRing.generate();
const redeem = Script.fromProgram(0, key.getKeyHash());
const output = Script.fromScripthash(redeem.hash160());
const input = new Script([
Opcode.fromData(redeem.toRaw())
]);
const witness = new Witness([
Buffer.from([0]),
Buffer.from([0])
]);
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 1);
});
it('should count sigops for p2wsh', () => {
const flags = Script.flags.VERIFY_WITNESS | Script.flags.VERIFY_P2SH;
const key = KeyRing.generate();
const pub = key.publicKey;
const redeem = Script.fromMultisig(1, 2, [pub, pub]);
const output = Script.fromProgram(0, redeem.sha256());
const input = new Script();
const witness = new Witness([
Buffer.from([0]),
Buffer.from([0]),
redeem.toRaw()
]);
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 2);
assert.strictEqual(
ctx.spend.getSigopsCost(ctx.view, flags & ~Script.flags.VERIFY_WITNESS),
0);
});
it('should count sigops for nested p2wsh', () => {
const flags = Script.flags.VERIFY_WITNESS | Script.flags.VERIFY_P2SH;
const key = KeyRing.generate();
const pub = key.publicKey;
const wscript = Script.fromMultisig(1, 2, [pub, pub]);
const redeem = Script.fromProgram(0, wscript.sha256());
const output = Script.fromScripthash(redeem.hash160());
const input = new Script([
Opcode.fromData(redeem.toRaw())
]);
const witness = new Witness([
Buffer.from([0]),
Buffer.from([0]),
wscript.toRaw()
]);
const ctx = sigopContext(input, witness, output);
assert.strictEqual(ctx.spend.getSigopsCost(ctx.view, flags), 2);
});
it('should return addresses for standard inputs', () => {
// txid: 7ef7cde4e4a7829ea6feaf377c924b36d0958e2231a31ff268bd33a59ac9e178
const [tx, view] = tx2.getTX();
const inputAddresses = [
Address.fromBase58('1Wjrrc2DrtB2CXRiPa3c8528fDdNHnQ2K')
];
const inputAddressesView = tx.getInputAddresses(view);
const inputAddressesNoView = tx.getInputAddresses();
assert.strictEqual(inputAddresses.length, inputAddressesView.length);
assert.strictEqual(inputAddresses.length, inputAddressesNoView.length);
inputAddresses.forEach((inputAddr, i) => {
assert(inputAddr.equals(inputAddressesView[i]));
assert(inputAddr.equals(inputAddressesNoView[i]));
});
});
it('should return addresses for standard outputs', () => {
// txid: 7f2dc9bcc0b1b0d19a4cb62d0f6474990c12a5b996d2fa2c4be54ca1beb5d339
const [tx] = tx7.getTX();
// If you fetch only outputs they should be sorted
// by vouts, not merged.
const outputAddresses = [
Address.fromBase58('1fLeMazoEy8FfgeFcppRxNYZs54jyLccw'),
Address.fromBase58('1EeREnzQujX7CLgmzDSaebS48jxjeyBHQM')
];
const getOutputAddresses = tx.getOutputAddresses();
assert.strictEqual(outputAddresses.length, getOutputAddresses.length);
outputAddresses.forEach((outputAddr, i) => {
assert(outputAddr.equals(getOutputAddresses[i]));
});
});
it('should return addresses for standard inputs and outputs', () => {
// txid: 7ef7cde4e4a7829ea6feaf377c924b36d0958e2231a31ff268bd33a59ac9e178
const [tx, view] = tx2.getTX();
const addresses = [
// inputs
Address.fromBase58('1Wjrrc2DrtB2CXRiPa3c8528fDdNHnQ2K'),
// outputs
Address.fromBase58('1GcKLBv6iFSCkbhht2m44qnZTYK8xV8nNA'),
Address.fromBase58('1EpKnnMo1rSkktYw8vPLtXGBRNLraXWd73')
];
const addressesView = tx.getAddresses(view);
const addressesNoView = tx.getAddresses();
assert.strictEqual(addresses.length, addressesView.length);
assert.strictEqual(addresses.length, addressesNoView.length);
addresses.forEach((addr, i) => {
assert(addr.equals(addressesView[i]));
assert(addr.equals(addressesNoView[i]));
});
});
it('should return merged addresses for same input/output address', () => {
// txid: 7f2dc9bcc0b1b0d19a4cb62d0f6474990c12a5b996d2fa2c4be54ca1beb5d339
const [tx, view] = tx7.getTX();
const addresses = [
// this is input and output
Address.fromBase58('1EeREnzQujX7CLgmzDSaebS48jxjeyBHQM'),
Address.fromBase58('1fLeMazoEy8FfgeFcppRxNYZs54jyLccw')
];
const addressesView = tx.getAddresses(view);
const addressesNoView = tx.getAddresses();
assert.strictEqual(addresses.length, addressesView.length);
assert.strictEqual(addresses.length, addressesNoView.length);
addresses.forEach((addr, i) => {
assert(addr.equals(addressesView[i]));
assert(addr.equals(addressesNoView[i]));
});
});
it('should return addresses with witness data', () => {
const [tx, view] = tx5.getTX();
const addresses = [
// inputs
Address.fromBech32('bc1qnjhhj5g8u46fvhnm34me52ahnx5vhhhuk6m7ng'),
Address.fromBech32('bc1q3ehzk5qa02sf05zyll0thth5t92kg6twah8hj3'),
Address.fromBase58('1C4irrkJiHhjKq62uPBw9huZnQsFSRHtjn'),
Address.fromBech32('bc1q4gzv2jkfnym3s8f69kj55l4yfh7aallphtzutp'),
Address.fromBech32('bc1q8838eem5cqqlxn34neay8sd7ru4nnm7yfv66xv'),
// outputs
Address.fromBech32('bc1q4uxyx3qaanm5elq4w2kxytvkpufa33s08vldx7'),
Address.fromBech32('bc1q8m66kw3789mvpfpcxxh880zg6jjwpyntspcnmy')
];
const addressesView = tx.getAddresses(view);
const addressesNoView = tx.getAddresses();
assert.strictEqual(addresses.length, addressesView.length);
assert.strictEqual(addresses.length, addressesNoView.length);
addresses.forEach((addr, i) => {
assert(addr.equals(addressesView[i]));
assert(addr.equals(addressesNoView[i]));
});
});
it('should return address hashes for standard inputs and outputs', () => {
// txid: 7ef7cde4e4a7829ea6feaf377c924b36d0958e2231a31ff268bd33a59ac9e178
const [tx, view] = tx2.getTX();
const hashes = [
// inputs
Address.fromBase58('1Wjrrc2DrtB2CXRiPa3c8528fDdNHnQ2K').getHash(),
// outputs
Address.fromBase58('1GcKLBv6iFSCkbhht2m44qnZTYK8xV8nNA').getHash(),
Address.fromBase58('1EpKnnMo1rSkktYw8vPLtXGBRNLraXWd73').getHash()
];
const hashesBuf = tx.getHashes(view);
assert.strictEqual(hashes.length, hashesBuf.length);
hashes.forEach((hash, i) => {
assert.bufferEqual(hash, hashesBuf[i]);
});
});
it('should return address hashes for standard inputs', () => {
// txid: 7ef7cde4e4a7829ea6feaf377c924b36d0958e2231a31ff268bd33a59ac9e178
const [tx, view] = tx2.getTX();
const inputHashes = [
Address.fromBase58('1Wjrrc2DrtB2CXRiPa3c8528fDdNHnQ2K').getHash()
];
const hashesBuf = tx.getInputHashes(view);
assert.strictEqual(inputHashes.length, hashesBuf.length);
inputHashes.forEach((hash, i) => {
assert.bufferEqual(hash, hashesBuf[i]);
});
});
it('should return address hashes for standard outputs', () => {
// txid: 7ef7cde4e4a7829ea6feaf377c924b36d0958e2231a31ff268bd33a59ac9e178
const [tx] = tx2.getTX();
const outputHashes = [
Address.fromBase58('1GcKLBv6iFSCkbhht2m44qnZTYK8xV8nNA').getHash(),
Address.fromBase58('1EpKnnMo1rSkktYw8vPLtXGBRNLraXWd73').getHash()
];
const hashesBuf = tx.getOutputHashes();
assert.strictEqual(outputHashes.length, hashesBuf.length);
outputHashes.forEach((hash, i) => {
assert.bufferEqual(hash, hashesBuf[i]);
});
});
it('should return all prevouts', () => {
const [tx] = tx3.getTX();
const expectedPrevouts = [
'2f196cf1e5bd426a04f07b882c893b5b5edebad67da6eb50f066c372ed736d5f',
'ff8755f073f1170c0d519457ffc4acaa7cb2988148163b5dc457fae0fe42aa19'
];
const prevouts = tx.getPrevout();
assert(expectedPrevouts.length, prevouts.length);
expectedPrevouts.forEach((prevout, i) => {
assert.strictEqual(prevout, prevouts[i].toString('hex'));
});
});
it('should serialize without witness data', () => {
const [tx] = tx2.getTX();
const [txWit] = tx5.getTX();
const bw1 = new BufferWriter();
const bw2 = new BufferWriter();
tx.toNormalWriter(bw1);
txWit.toNormalWriter(bw2);
const tx1normal = TX.fromRaw(bw1.render());
const tx2normal = TX.fromRaw(bw2.render());
assert.strictEqual(tx1normal.hasWitness(), false);
assert.strictEqual(tx2normal.hasWitness(), false);
});
it('should check if tx is free', () => {
const value = 100000000; // 1 btc
const height = 100;
const [input, view] = createInput(value);
// hack height into coinEntry
const entry = view.getEntry(input.prevout);
entry.height = height;
const tx = new TX({
version: 1,
inputs: [input],
outputs: [{
script: [],
value: value
}],
locktime: 0
});
// Priority should be more than FREE_THRESHOLD
// txsize: 60, value: 1 btc
// freeAfter: 144/250*txsize = 34.56
const size = tx.getSize();