forked from provable-things/liquidjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtransaction.spec.ts
1680 lines (1536 loc) · 53.4 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
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
import * as assert from 'assert';
import { BIP32Factory } from 'bip32';
import * as regtestUtils from './_regtest';
import { describe, it } from 'mocha';
import { createPayment, getInputData, nonWitnessUtxoBuffer } from './utils';
import { payments, networks as NETWORKS, ElementsValue } from '../../ts_src';
import { Psbt } from '../../ts_src/psbt';
import { Confidential } from '../../ts_src/confidential';
import { ECPair, ecc } from '../ecc';
import secp256k1 from '@vulpemventures/secp256k1-zkp';
const rng = require('randombytes');
const { regtest } = NETWORKS;
// See bottom of file for some helper functions used to make the payment objects needed.
describe.skip('liquidjs-lib (transactions with psbt)', () => {
const alice = ECPair.fromWIF(
'cPNMJD4VyFnQjGbGs3kcydRzAbDCXrLAbvH6wTCqs88qg1SkZT3J',
regtest,
);
const bob = ECPair.fromWIF(
'cQ7z41awTvKtmiD9p6zkjbgvYbV8g5EDDNcTnKZS9aZ8XdjQiZMU',
regtest,
);
const nonce = Buffer.from('00', 'hex');
const asset = Buffer.concat([
Buffer.from('01', 'hex'),
Buffer.from(regtest.assetHash, 'hex').reverse(),
]);
it('can create a 1-to-1 Transaction', () => {
const psbt = new Psbt();
psbt.setVersion(2); // These are defaults. This line is not needed.
psbt.setLocktime(0); // These are defaults. This line is not needed.
psbt.addInput({
// if hash is string, txid, if hash is Buffer, is reversed compared to txid
hash: '9d64f0343e264f9992aa024185319b349586ec4cbbfcedcda5a05678ab10e580',
index: 0,
// non-segwit inputs now require passing the whole previous tx as Buffer
nonWitnessUtxo: Buffer.from(
'0200000000010caf381d44f094661f2da71a11946251a27d656d6c141577e27c483a6' +
'd428f01010000006a47304402205ac99f5988d699d6d9f72004098c2e52c8f342838e' +
'9009dde33d204108cc930d022077238cd40a4e4234f1e70ceab8fd6b51c5325954387' +
'2e5d9f4bad544918b82ce012102b5214a4f0d6962fe547f0b9cbb241f9df1b61c3c40' +
'1dbfb04cdd59efd552bea1ffffffff020125b251070e29ca19043cf33ccd7324e2dda' +
'b03ecc4ae0b5e77c4fc0e5cf6c95a010000000005f5df70001976a914659bedb5d3d3' +
'c7ab12d7f85323c3a1b6c060efbe88ac0125b251070e29ca19043cf33ccd7324e2dda' +
'b03ecc4ae0b5e77c4fc0e5cf6c95a010000000000000190000000000000',
'hex',
),
});
psbt.addOutputs([
{
nonce: Buffer.from('00', 'hex'),
value: ElementsValue.fromNumber(50000000).bytes,
script: Buffer.from(
'76a91439397080b51ef22c59bd7469afacffbeec0da12e88ac',
'hex',
),
asset: Buffer.concat([
Buffer.from('01', 'hex'),
Buffer.from(
'5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225',
'hex',
).reverse(),
]),
},
{
nonce: Buffer.from('00', 'hex'),
value: ElementsValue.fromNumber(49999100).bytes,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
asset: Buffer.concat([
Buffer.from('01', 'hex'),
Buffer.from(
'5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225',
'hex',
).reverse(),
]),
},
{
nonce: Buffer.from('00', 'hex'),
value: ElementsValue.fromNumber(500).bytes,
script: Buffer.alloc(0),
asset: Buffer.concat([
Buffer.from('01', 'hex'),
Buffer.from(
'5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225',
'hex',
).reverse(),
]),
},
]);
psbt.signInput(0, alice);
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc));
psbt.finalizeAllInputs();
assert.strictEqual(
psbt.extractTransaction().toHex(),
'02000000000180e510ab7856a0a5cdedfcbb4cec8695349b31854102aa92994f263e34f' +
'0649d000000006a47304402201e868b2bea22df05229746a27e7df2ca0f584880546f7f' +
'6d55dad71cbd50d35302203a04a4cc49fca739c8974c97d3de924c99835e15ad1d85b96' +
'ad24ea072d2e63e01210251464420fcc98a2e4cd347afe28a32d769287dacd861476ab8' +
'58baa43bd308f3ffffffff030125b251070e29ca19043cf33ccd7324e2ddab03ecc4ae0' +
'b5e77c4fc0e5cf6c95a010000000002faf080001976a91439397080b51ef22c59bd7469' +
'afacffbeec0da12e88ac0125b251070e29ca19043cf33ccd7324e2ddab03ecc4ae0b5e7' +
'7c4fc0e5cf6c95a010000000002faecfc001976a914659bedb5d3d3c7ab12d7f85323c3' +
'a1b6c060efbe88ac0125b251070e29ca19043cf33ccd7324e2ddab03ecc4ae0b5e77c4f' +
'c0e5cf6c95a0100000000000001f4000000000000',
);
});
it('can create a 1-to-1 confidential Transaction', () => {
const blindingPrivkeys = [
Buffer.from(
'13d4dbfdb5074705e6b9758d1542d7dd8c03055086c0da421620eaa04717a9f7',
'hex',
),
];
const blindingPubkeys = [''].map(
() => ECPair.makeRandom({ network: regtest }).publicKey,
);
const psbt = new Psbt();
psbt.setVersion(2); // These are defaults. This line is not needed.
psbt.setLocktime(0); // These are defaults. This line is not needed.
psbt.addInput({
// if hash is string, txid, if hash is Buffer, is reversed compared to txid
hash: 'dd983c9c0419fce6bcc0eaf875b54a2c19f9d6e761faa58b1afd199638275475',
index: 0,
// non-segwit inputs now require passing the whole previous tx as Buffer
nonWitnessUtxo: nonWitnessUtxoBuffer,
});
psbt.addOutputs([
{
nonce,
asset,
value: ElementsValue.fromNumber(99996500).bytes,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
},
{
nonce,
asset,
value: ElementsValue.fromNumber(3500).bytes,
script: Buffer.alloc(0),
},
]);
psbt.blindOutputs(
Psbt.ECCKeysGenerator(ecc),
blindingPrivkeys,
blindingPubkeys,
);
psbt.signInput(0, bob);
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc));
psbt.finalizeAllInputs();
psbt.extractTransaction();
});
it('can create (and broadcast via 3PBP) a typical Transaction', async () => {
// these are { payment: Payment; keys: ECPair[] }
const alice1 = createPayment('p2pkh');
const alice2 = createPayment('p2pkh');
// give Alice 2 unspent outputs
const inputData1 = await getInputData(alice1.payment, false, 'noredeem');
const inputData2 = await getInputData(alice2.payment, false, 'noredeem');
{
const {
hash, // string of txid or Buffer of tx hash. (txid and hash are reverse order)
index, // the output index of the txo you are spending
nonWitnessUtxo, // the full previous transaction as a Buffer
} = inputData1;
assert.deepStrictEqual({ hash, index, nonWitnessUtxo }, inputData1);
}
// network is only needed if you pass an address to addOutput
// using script (Buffer of scriptPubkey) instead will avoid needed network.
const psbt = new Psbt({ network: regtest })
.addInput(inputData1) // alice1 unspent
.addInput(inputData2) // alice2 unspent
.addOutput({
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(150000000).bytes,
}) // the actual spend
.addOutput({
asset,
nonce,
script: alice2.payment.output,
value: ElementsValue.fromNumber(49999300).bytes,
}) // Alice's change
.addOutput({
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(700).bytes,
}); // fees in Liquid are explicit
// Let's show a new feature with PSBT.
// We can have multiple signers sign in parrallel and combine them.
// (this is not necessary, but a nice feature)
// encode to send out to the signers
const psbtBaseText = psbt.toBase64();
// each signer imports
const signer1 = Psbt.fromBase64(psbtBaseText);
const signer2 = Psbt.fromBase64(psbtBaseText);
// Alice signs each input with the respective private keys
// signInput and signInputAsync are better
// (They take the input index explicitly as the first arg)
signer1.signAllInputs(alice1.keys[0]);
signer2.signAllInputs(alice2.keys[0]);
// If your signer object's sign method returns a promise, use the following
// await signer2.signAllInputsAsync(alice2.keys[0])
// encode to send back to combiner (signer 1 and 2 are not near each other)
const s1text = signer1.toBase64();
const s2text = signer2.toBase64();
const final1 = Psbt.fromBase64(s1text);
const final2 = Psbt.fromBase64(s2text);
// final1.combine(final2) would give the exact same result
psbt.combine(final1, final2);
// Finalizer wants to check all signatures are valid before finalizing.
// If the finalizer wants to check for specific pubkeys, the second arg
// can be passed. See the first multisig example below.
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
assert.strictEqual(
psbt.validateSignaturesOfInput(1, Psbt.ECDSASigValidator(ecc)),
true,
);
// This step it new. Since we separate the signing operation and
// the creation of the scriptSig and witness stack, we are able to
psbt.finalizeAllInputs();
// build and broadcast our RegTest network
await regtestUtils.broadcast(psbt.extractTransaction().toHex());
// to build and broadcast to the actual Bitcoin network, see https://github.com/bitcoinjs/bitcoinjs-lib/issues/839
});
it('can create (and broadcast via 3PBP) a confidential Transaction', async () => {
// these are { payment: Payment; keys: ECPair[] }
const alice1 = createPayment('p2pkh', undefined, undefined, true);
const blindingPubkeys = ['', ''].map(
() => ECPair.makeRandom({ network: regtest }).publicKey,
);
// give Alice 2 unspent outputs
const inputData1 = await getInputData(alice1.payment, false, 'noredeem');
{
const {
hash, // string of txid or Buffer of tx hash. (txid and hash are reverse order)
index, // the output index of the txo you are spending
nonWitnessUtxo, // the full previous transaction as a Buffer
} = inputData1;
assert.deepStrictEqual({ hash, index, nonWitnessUtxo }, inputData1);
}
// network is only needed if you pass an address to addOutput
// using script (Buffer of scriptPubkey) instead will avoid needed network.
let psbt = await new Psbt({ network: regtest })
.addInput(inputData1) // alice1 unspent
.addOutput({
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(50000000).bytes,
}) // the actual spend
.addOutput({
asset,
nonce,
script: alice1.payment.output,
value: ElementsValue.fromNumber(49993000).bytes,
}) // Alice's change
.addOutput({
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(7000).bytes,
}) // fees in Liquid are explicit
.blindOutputs(
Psbt.ECCKeysGenerator(ecc),
alice1.blindingKeys,
blindingPubkeys,
);
psbt = psbt.signAllInputs(alice1.keys[0]);
// Finalizer wants to check all signatures are valid before finalizing.
// If the finalizer wants to check for specific pubkeys, the second arg
// can be passed. See the first multisig example below.
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
// This step it new. Since we separate the signing operation and
// the creation of the scriptSig and witness stack, we are able to
psbt.finalizeAllInputs();
// build and broadcast our RegTest network
await regtestUtils.broadcast(psbt.extractTransaction().toHex());
// to build and broadcast to the actual Bitcoin network, see https://github.com/bitcoinjs/bitcoinjs-lib/issues/839
});
it(
'can create (and broadcast via 3PBP) a confidential Transaction' +
' with confidential AND unconfidential outputs',
async () => {
// these are { payment: Payment; keys: ECPair[] }
const alicePayment = createPayment('p2pkh', undefined, undefined, true); // confidential
const bobPayment = createPayment('p2pkh', undefined, undefined, false); // unconfidential
const aliceBlindingPubKey = ECPair.fromPrivateKey(
alicePayment.blindingKeys[0],
).publicKey!;
const aliceBlindingPrivateKey: Buffer = alicePayment.blindingKeys[0];
// give Alice unspent outputs
const inputData1 = await getInputData(
alicePayment.payment,
false,
'noredeem',
);
{
const {
hash, // string of txid or Buffer of tx hash. (txid and hash are reverse order)
index, // the output index of the txo you are spending
nonWitnessUtxo, // the full previous transaction as a Buffer
} = inputData1;
assert.deepStrictEqual({ hash, index, nonWitnessUtxo }, inputData1);
}
// network is only needed if you pass an address to addOutput
// using script (Buffer of scriptPubkey) instead will avoid needed network.
let psbt = await new Psbt({ network: regtest })
.addInput(inputData1) // alice unspent
.addOutput({
asset,
nonce,
script: bobPayment.payment.output,
value: ElementsValue.fromNumber(50000000).bytes,
}) // the actual spend to bob
.addOutput({
asset,
nonce,
script: alicePayment.payment.output,
value: ElementsValue.fromNumber(29993000).bytes,
}) // Alice's change
.addOutput({
asset,
nonce,
script: alicePayment.payment.output,
value: ElementsValue.fromNumber(20000000).bytes,
}) // Alice's change bis
.addOutput({
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(7000).bytes,
}) // fees in Liquid are explicit
.blindOutputsByIndex(
Psbt.ECCKeysGenerator(ecc),
new Map().set(0, aliceBlindingPrivateKey),
new Map().set(1, aliceBlindingPubKey).set(2, aliceBlindingPubKey),
);
psbt = psbt.signAllInputs(alicePayment.keys[0]);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
psbt.finalizeAllInputs();
// build and broadcast our RegTest network
await regtestUtils.broadcast(psbt.extractTransaction().toHex());
},
);
it(
'can create (and broadcast via 3PBP) a confidential Transaction' +
' with confidential AND unconfidential inputs',
async () => {
// these are { payment: Payment; keys: ECPair[] }
const alicePaymentUnconfidential = createPayment(
'p2pkh',
undefined,
undefined,
false,
); // unconfidential
const alicePaymentConfidential = createPayment(
'p2pkh',
undefined,
undefined,
true,
); // confidential
const bobPayment = createPayment('p2pkh', undefined, undefined, false); // unconfidential
const aliceBlindingPubKey = ECPair.fromPrivateKey(
alicePaymentConfidential.blindingKeys[0],
).publicKey!;
const aliceBlindingPrivateKey: Buffer =
alicePaymentConfidential.blindingKeys[0];
// give Alice unspent outputs
const inputDataUnconfidential = await getInputData(
alicePaymentUnconfidential.payment,
false,
'noredeem',
);
const inputDataConfidential = await getInputData(
alicePaymentConfidential.payment,
false,
'noredeem',
);
// network is only needed if you pass an address to addOutput
// using script (Buffer of scriptPubkey) instead will avoid needed network.
let psbt = await new Psbt({ network: regtest })
.addInput(inputDataUnconfidential) // alice unspent (unconfidential)
.addInput(inputDataConfidential) // alice unspent (confidential)
.addOutput({
asset,
nonce,
script: bobPayment.payment.output,
value: ElementsValue.fromNumber(99993000).bytes,
}) // the actual spend to bob
.addOutput({
asset,
nonce,
script: alicePaymentConfidential.payment.output,
value: ElementsValue.fromNumber(99999000).bytes,
}) // Alice's change
.addOutput({
asset,
nonce,
script: alicePaymentConfidential.payment.output,
value: ElementsValue.fromNumber(1000).bytes,
}) // Alice's change bis (need two blind outputs)
.addOutput({
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(7000).bytes,
}) // fees in Liquid are explicit
.blindOutputsByIndex(
Psbt.ECCKeysGenerator(ecc),
new Map().set(1, aliceBlindingPrivateKey),
new Map().set(1, aliceBlindingPubKey).set(2, aliceBlindingPubKey),
);
psbt = psbt
.signInput(0, alicePaymentUnconfidential.keys[0])
.signInput(1, alicePaymentConfidential.keys[0]);
assert.doesNotThrow(() => Psbt.fromBase64(psbt.toBase64()));
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
assert.strictEqual(
psbt.validateSignaturesOfInput(1, Psbt.ECDSASigValidator(ecc)),
true,
);
psbt.finalizeAllInputs();
// build and broadcast our RegTest network
await regtestUtils.broadcast(psbt.extractTransaction().toHex());
},
);
it(
'can create (and broadcast via 3PBP) a confidential Transaction' +
' using blinders',
async () => {
// these are { payment: Payment; keys: ECPair[] }
const alicePaymentConfidential = createPayment(
'p2wpkh',
undefined,
undefined,
true,
); // confidential
const bobPayment = createPayment('p2pkh', undefined, undefined, false); // unconfidential
const aliceBlindingPubKey = ECPair.fromPrivateKey(
alicePaymentConfidential.blindingKeys[0],
).publicKey!;
const aliceBlindingPrivateKey: Buffer =
alicePaymentConfidential.blindingKeys[0];
const inputDataConfidential = await getInputData(
alicePaymentConfidential.payment,
true,
'noredeem',
);
const zkpLib = await secp256k1();
const confidential = new Confidential(zkpLib);
const inputBlindingData = confidential.unblindOutputWithKey(
inputDataConfidential.witnessUtxo,
aliceBlindingPrivateKey,
);
// network is only needed if you pass an address to addOutput
// using script (Buffer of scriptPubkey) instead will avoid needed network.
const psbt = await new Psbt({ network: regtest })
.addInput(inputDataConfidential) // alice unspent (confidential)
.addOutput({
asset,
nonce,
script: bobPayment.payment.output,
value: ElementsValue.fromNumber(1000).bytes,
}) // the actual spend to bob
.addOutput({
asset,
nonce,
script: alicePaymentConfidential.payment.output,
value: ElementsValue.fromNumber(99991000).bytes,
}) // Alice's change
.addOutput({
asset,
nonce,
script: alicePaymentConfidential.payment.output,
value: ElementsValue.fromNumber(1000).bytes,
}) // Alice's change bis (need two blind outputs)
.addOutput({
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(7000).bytes,
}) // fees in Liquid are explicit
.blindOutputsByIndex(
Psbt.ECCKeysGenerator(ecc),
new Map().set(0, inputBlindingData),
new Map().set(1, aliceBlindingPubKey).set(2, aliceBlindingPubKey),
);
psbt.signInput(0, alicePaymentConfidential.keys[0]);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
psbt.finalizeAllInputs();
// build and broadcast our RegTest network
await regtestUtils.broadcast(psbt.extractTransaction().toHex());
},
);
it('can create (and broadcast via 3PBP) a Transaction with an OP_RETURN output', async () => {
const alice1 = createPayment('p2pkh');
const inputData1 = await getInputData(alice1.payment, false, 'noredeem');
const data = Buffer.from('bitcoinjs-lib', 'utf8');
const embed = payments.embed({ data: [data] });
const psbt = new Psbt({ network: regtest })
.addInput(inputData1)
.addOutput({
asset,
nonce,
script: embed.output!,
value: ElementsValue.fromNumber(500).bytes,
})
.addOutput({
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(99999000).bytes,
})
.addOutput({
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(500).bytes,
})
.signInput(0, alice1.keys[0]);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
psbt.finalizeAllInputs();
// build and broadcast to the RegTest network
await regtestUtils.broadcast(psbt.extractTransaction().toHex());
});
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2MS(2 of 4)) (multisig) input', async () => {
const multisig = createPayment('p2sh-p2ms(2 of 4)');
const inputData1 = await getInputData(multisig.payment, false, 'p2sh');
{
const {
hash,
index,
nonWitnessUtxo,
redeemScript, // NEW: P2SH needs to give redeemScript when adding an input.
} = inputData1;
assert.deepStrictEqual(
{ hash, index, nonWitnessUtxo, redeemScript },
inputData1,
);
}
const psbt = new Psbt({ network: regtest })
.addInput(inputData1)
.addOutputs([
{
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(99999500).bytes,
},
{
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(500).bytes,
},
])
.signInput(0, multisig.keys[0])
.signInput(0, multisig.keys[2]);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
assert.strictEqual(
psbt.validateSignaturesOfInput(
0,
Psbt.ECDSASigValidator(ecc),
multisig.keys[0].publicKey,
),
true,
);
assert.throws(() => {
psbt.validateSignaturesOfInput(
0,
Psbt.ECDSASigValidator(ecc),
multisig.keys[3].publicKey,
);
}, new RegExp('No signatures for this pubkey'));
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex());
});
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input', async () => {
const p2sh = createPayment('p2sh-p2wpkh');
const inputData = await getInputData(p2sh.payment, true, 'p2sh');
const inputData2 = await getInputData(p2sh.payment, true, 'p2sh');
{
const {
hash,
index,
witnessUtxo, // NEW: this is an object of the output being spent { script: Buffer; value: Satoshis; }
redeemScript,
} = inputData;
assert.deepStrictEqual(
{ hash, index, witnessUtxo, redeemScript },
inputData,
);
}
const keyPair = p2sh.keys[0];
const outputData = {
asset: inputData.witnessUtxo.asset,
nonce,
script: p2sh.payment.output, // sending to myself for fun
value: ElementsValue.fromNumber(199999300).bytes,
};
const outputData2 = {
asset: inputData.witnessUtxo.asset,
nonce,
script: Buffer.alloc(0), // fees
value: ElementsValue.fromNumber(700).bytes,
};
const tx = new Psbt()
.addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2])
.signAllInputs(keyPair)
.finalizeAllInputs()
.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex());
});
it('can create (and broadcast via 3PBP) a confidential Transaction, w/ a P2SH(P2WPKH) input', async () => {
const p2sh = createPayment('p2sh-p2wpkh', undefined, undefined, true);
const inputData = await getInputData(p2sh.payment, true, 'p2sh');
const inputData2 = await getInputData(p2sh.payment, true, 'p2sh');
const blindingKeys = ['', ''].map(() => p2sh.blindingKeys[0]);
const blindingPubkeys = ['', ''].map(
() => ECPair.makeRandom({ network: regtest }).publicKey,
);
{
const { hash, index, witnessUtxo, redeemScript } = inputData;
assert.deepStrictEqual(
{ hash, index, witnessUtxo, redeemScript },
inputData,
);
}
const keyPair = p2sh.keys[0];
const outputData = {
asset,
nonce,
script: p2sh.payment.output, // change
value: ElementsValue.fromNumber(159993000).bytes,
};
const outputData2 = {
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
), // actual spend
value: ElementsValue.fromNumber(40000000).bytes,
};
const outputData3 = {
asset,
nonce,
script: Buffer.alloc(0), // fees
value: ElementsValue.fromNumber(7000).bytes,
};
const psbt = await new Psbt()
.addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2, outputData3])
.blindOutputs(Psbt.ECCKeysGenerator(ecc), blindingKeys, blindingPubkeys);
const tx = psbt.signAllInputs(keyPair);
const toBroadcast = Psbt.fromBase64(tx.toBase64())
.finalizeAllInputs()
.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(toBroadcast.toHex());
});
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input with nonWitnessUtxo', async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2sh = createPayment('p2sh-p2wpkh');
const inputData = await getInputData(p2sh.payment, false, 'p2sh');
const inputData2 = await getInputData(p2sh.payment, false, 'p2sh');
const keyPair = p2sh.keys[0];
const outputData = {
asset,
nonce,
script: p2sh.payment.output,
value: ElementsValue.fromNumber(199999300).bytes,
};
const outputData2 = {
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(700).bytes,
};
const tx = new Psbt()
.addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2])
.signAllInputs(keyPair)
.finalizeAllInputs()
.extractTransaction();
await regtestUtils.broadcast(tx.toHex());
});
it(
'can create (and broadcast via 3PBP) a confidential Transaction, w/ a' +
'P2SH(P2WPKH) input with nonWitnessUtxo',
async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2sh = createPayment('p2sh-p2wpkh', undefined, undefined, true);
const inputData = await getInputData(p2sh.payment, false, 'p2sh');
const inputData2 = await getInputData(p2sh.payment, false, 'p2sh');
const blindingKeys = ['', ''].map(() => p2sh.blindingKeys[0]);
const blindingPubkeys = [''].map(
() => ECPair.makeRandom({ network: regtest }).publicKey,
);
const keyPair = p2sh.keys[0];
const outputData = {
asset,
nonce,
script: p2sh.payment.output,
value: ElementsValue.fromNumber(199996500).bytes,
};
const outputData2 = {
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(3500).bytes,
};
const psbt = await new Psbt()
.addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2])
.blindOutputs(
Psbt.ECCKeysGenerator(ecc),
blindingKeys,
blindingPubkeys,
);
const tx = psbt
.signAllInputs(keyPair)
.finalizeAllInputs()
.extractTransaction();
await regtestUtils.broadcast(tx.toHex());
},
);
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input', async () => {
// the only thing that changes is you don't give a redeemscript for input data
const p2wpkh = createPayment('p2wpkh');
const inputData = await getInputData(p2wpkh.payment, true, 'noredeem');
{
const { hash, index, witnessUtxo } = inputData;
assert.deepStrictEqual({ hash, index, witnessUtxo }, inputData);
}
const psbt = new Psbt({ network: regtest })
.addInput(inputData)
.addOutputs([
{
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(99999500).bytes,
},
{
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(500).bytes,
},
])
.signInput(0, p2wpkh.keys[0]);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex());
});
it('can create (and broadcast via 3PBP) a confidential Transaction, w/ a P2WPKH input', async () => {
// the only thing that changes is you don't give a redeemscript for input data
const p2wpkh = createPayment('p2wpkh', undefined, undefined, true);
const blindingPubkeys = [''].map(
() => ECPair.makeRandom({ network: regtest }).publicKey,
);
const inputData = await getInputData(p2wpkh.payment, true, 'noredeem');
{
const { hash, index, witnessUtxo } = inputData;
assert.deepStrictEqual({ hash, index, witnessUtxo }, inputData);
}
let psbt = await new Psbt({ network: regtest })
.addInput(inputData)
.addOutputs([
{
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(99996500).bytes,
},
{
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(3500).bytes,
},
])
.blindOutputs(
Psbt.ECCKeysGenerator(ecc),
p2wpkh.blindingKeys,
blindingPubkeys,
);
psbt = psbt.signInput(0, p2wpkh.keys[0]);
assert.strictEqual(
psbt.validateSignaturesOfInput(0, Psbt.ECDSASigValidator(ecc)),
true,
);
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
// build and broadcast to the Bitcoin RegTest network
await regtestUtils.broadcast(tx.toHex());
});
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input with nonWitnessUtxo', async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2wpkh = createPayment('p2wpkh');
const inputData = await getInputData(p2wpkh.payment, false, 'noredeem');
const psbt = new Psbt({ network: regtest })
.addInput(inputData)
.addOutputs([
{
asset,
nonce,
script: Buffer.from(
'76a914659bedb5d3d3c7ab12d7f85323c3a1b6c060efbe88ac',
'hex',
),
value: ElementsValue.fromNumber(99999500).bytes,
},
{
asset,
nonce,
script: Buffer.alloc(0),
value: ElementsValue.fromNumber(500).bytes,
},
])
.signInput(0, p2wpkh.keys[0]);
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
await regtestUtils.broadcast(tx.toHex());
});
it(
'can create (and broadcast via 3PBP) a confidential Transaction, w/ a' +
'P2WPKH input with nonWitnessUtxo',
async () => {
// For learning purposes, ignore this test.
// REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
const p2wpkh = createPayment('p2wpkh', undefined, undefined, true);
const blindingPubkeys = [''].map(
() => ECPair.makeRandom({ network: regtest }).publicKey,
);
const inputData = await getInputData(p2wpkh.payment, false, 'noredeem');
let psbt = await new Psbt({ network: regtest })
.addInput(inputData)
.addOutputs([
{
asset,
nonce,
script: Buffer.from(