-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtaproot.spec.ts
1076 lines (960 loc) · 32.7 KB
/
taproot.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, { BIP32Interface } from 'bip32';
import * as bip39 from 'bip39';
import * as ecc from 'tiny-secp256k1';
import { describe, it } from 'mocha';
import { PsbtInput, TapLeaf, TapLeafScript } from 'bip174';
import { regtestUtils } from './_regtest.js';
import * as bitcoin from 'bitcoinjs-lib';
import { Taptree } from 'bitcoinjs-lib/src/types';
import {
LEAF_VERSION_TAPSCRIPT,
tapleafHash,
} from 'bitcoinjs-lib/src/payments/bip341';
import {
toXOnly,
tapTreeToList,
tapTreeFromList,
} from 'bitcoinjs-lib/src/psbt/bip371';
import { witnessStackToScriptWitness } from 'bitcoinjs-lib/src/psbt/psbtutils';
import * as tools from 'uint8array-tools';
import { sha256 } from '@noble/hashes/sha256';
import { randomBytes } from 'crypto';
const regtest = regtestUtils.network;
bitcoin.initEccLib(ecc);
const bip32 = BIP32Factory(ecc);
const rng = (size: number) => randomBytes(size);
describe('bitcoinjs-lib (transaction with taproot)', () => {
it('can verify the BIP86 HD wallet vectors for taproot single sig (& sending example)', async () => {
// Values taken from BIP86 document
const mnemonic =
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
const xprv =
'xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu';
const path = `m/86'/0'/0'/0/0`; // Path to first child of receiving wallet on first account
const internalPubkey = Buffer.from(
'cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115',
'hex',
);
const expectedAddress =
'bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr';
// Verify the above (Below is no different than other HD wallets)
const seed = await bip39.mnemonicToSeed(mnemonic);
const rootKey = bip32.fromSeed(seed);
assert.strictEqual(rootKey.toBase58(), xprv);
const childNode = rootKey.derivePath(path);
// Since internalKey is an xOnly pubkey, we drop the DER header byte
const childNodeXOnlyPubkey = toXOnly(childNode.publicKey);
assert.deepEqual(childNodeXOnlyPubkey, internalPubkey);
// This is new for taproot
// Note: we are using mainnet here to get the correct address
// The output is the same no matter what the network is.
const { address, output } = bitcoin.payments.p2tr({
internalPubkey,
});
assert.ok(!!output);
assert.strictEqual(address, expectedAddress);
// Used for signing, since the output and address are using a tweaked key
// We must tweak the signer in the same way.
const tweakedChildNode = childNode.tweak(
bitcoin.crypto.taggedHash('TapTweak', childNodeXOnlyPubkey),
);
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// Send some sats to the address via faucet. Get the hash and index. (txid/vout)
const { txId: hash, vout: index } = await regtestUtils.faucetComplex(
Buffer.from(output),
amount,
);
// Sent 420000 sats to taproot address
const psbt = new bitcoin.Psbt({ network: regtest })
.addInput({
hash,
index,
witnessUtxo: { value: BigInt(amount), script: output },
tapInternalKey: childNodeXOnlyPubkey,
})
.addOutput({
value: BigInt(sendAmount),
address: regtestUtils.RANDOM_ADDRESS,
})
.signInput(0, tweakedChildNode)
.finalizeAllInputs();
const tx = psbt.extractTransaction();
await regtestUtils.broadcast(tx.toHex());
await regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot key-path spend Transaction', async () => {
const internalKey = bip32.fromSeed(rng(64), regtest);
const p2pkhKey = bip32.fromSeed(rng(64), regtest);
const { output } = bitcoin.payments.p2tr({
internalPubkey: toXOnly(internalKey.publicKey),
network: regtest,
});
const { output: p2pkhOutput } = bitcoin.payments.p2pkh({
pubkey: p2pkhKey.publicKey,
network: regtest,
});
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(output!),
amount,
);
// non segwit utxo
const p2pkhUnspent = await regtestUtils.faucetComplex(
Buffer.from(p2pkhOutput!),
amount,
);
const utx = await regtestUtils.fetch(p2pkhUnspent.txId);
const nonWitnessUtxo = Buffer.from(utx.txHex, 'hex');
const psbt = new bitcoin.Psbt({ network: regtest });
psbt.addInput({
hash: unspent.txId,
index: 0,
witnessUtxo: { value: BigInt(amount), script: output! },
tapInternalKey: toXOnly(internalKey.publicKey),
});
psbt.addInput({ index: 0, hash: p2pkhUnspent.txId, nonWitnessUtxo });
const sendInternalKey = bip32.fromSeed(rng(64), regtest);
const sendPubKey = toXOnly(sendInternalKey.publicKey);
const { address: sendAddress } = bitcoin.payments.p2tr({
internalPubkey: sendPubKey,
network: regtest,
});
psbt.addOutput({
value: BigInt(sendAmount),
address: sendAddress!,
tapInternalKey: sendPubKey,
});
const tweakedSigner = internalKey.tweak(
bitcoin.crypto.taggedHash('TapTweak', toXOnly(internalKey.publicKey)),
);
await psbt.signInputAsync(0, tweakedSigner);
await psbt.signInputAsync(1, p2pkhKey);
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
address: sendAddress!,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot key-path spend Transaction (with unused scriptTree)', async () => {
const internalKey = bip32.fromSeed(rng(64), regtest);
const leafKey = bip32.fromSeed(rng(64), regtest);
const leafScriptAsm = `${tools.toHex(
toXOnly(leafKey.publicKey),
)} OP_CHECKSIG`;
const leafScript = bitcoin.script.fromASM(leafScriptAsm);
const scriptTree = {
output: leafScript,
};
const { output, address, hash } = bitcoin.payments.p2tr({
internalPubkey: toXOnly(internalKey.publicKey),
scriptTree,
network: regtest,
});
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(output!),
amount,
);
const psbt = new bitcoin.Psbt({ network: regtest });
psbt.addInput({
hash: unspent.txId,
index: 0,
witnessUtxo: { value: BigInt(amount), script: output! },
tapInternalKey: toXOnly(internalKey.publicKey),
tapMerkleRoot: hash,
});
psbt.addOutput({ value: BigInt(sendAmount), address: address! });
const tweakedSigner = internalKey.tweak(
bitcoin.crypto.taggedHash(
'TapTweak',
Buffer.concat([toXOnly(internalKey.publicKey), hash!]),
),
);
psbt.signInput(0, tweakedSigner);
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
address: address!,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSIG', async () => {
const internalKey = bip32.fromSeed(rng(64), regtest);
const leafKey = bip32.fromSeed(rng(64), regtest);
const leafScriptAsm = `${tools.toHex(
toXOnly(leafKey.publicKey),
)} OP_CHECKSIG`;
const leafScript = bitcoin.script.fromASM(leafScriptAsm);
const scriptTree: Taptree = [
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0 OP_CHECKSIG',
),
},
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac1 OP_CHECKSIG',
),
},
{
output: bitcoin.script.fromASM(
'2258b1c3160be0864a541854eec9164a572f094f7562628281a8073bb89173a7 OP_CHECKSIG',
),
},
],
],
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac2 OP_CHECKSIG',
),
},
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac3 OP_CHECKSIG',
),
},
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac4 OP_CHECKSIG',
),
},
{
output: leafScript,
},
],
],
],
];
const redeem = {
output: leafScript,
redeemVersion: LEAF_VERSION_TAPSCRIPT,
};
const { output, witness } = bitcoin.payments.p2tr({
internalPubkey: toXOnly(internalKey.publicKey),
scriptTree,
redeem,
network: regtest,
});
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(output!),
amount,
);
const psbt = new bitcoin.Psbt({ network: regtest });
psbt.addInput({
hash: unspent.txId,
index: 0,
witnessUtxo: { value: BigInt(amount), script: output! },
});
psbt.updateInput(0, {
tapLeafScript: [
{
leafVersion: redeem.redeemVersion,
script: redeem.output,
controlBlock: witness![witness!.length - 1],
},
],
});
const sendInternalKey = bip32.fromSeed(rng(64), regtest);
const sendPubKey = toXOnly(sendInternalKey.publicKey);
const { address: sendAddress } = bitcoin.payments.p2tr({
internalPubkey: sendPubKey,
scriptTree,
network: regtest,
});
psbt.addOutput({
value: BigInt(sendAmount),
address: sendAddress!,
tapInternalKey: sendPubKey,
tapTree: { leaves: tapTreeToList(scriptTree) },
});
psbt.signInput(0, leafKey);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
address: sendAddress!,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSEQUENCEVERIFY', async () => {
const internalKey = bip32.fromSeed(rng(64), regtest);
const leafKey = bip32.fromSeed(rng(64), regtest);
const leafPubkey = tools.toHex(toXOnly(leafKey.publicKey));
const leafScriptAsm = `OP_10 OP_CHECKSEQUENCEVERIFY OP_DROP ${leafPubkey} OP_CHECKSIG`;
const leafScript = bitcoin.script.fromASM(leafScriptAsm);
const scriptTree: Taptree = [
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0 OP_CHECKSIG',
),
},
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0 OP_CHECKSIG',
),
},
{
output: leafScript,
},
],
];
const redeem = {
output: leafScript,
redeemVersion: LEAF_VERSION_TAPSCRIPT,
};
const { output, witness } = bitcoin.payments.p2tr({
internalPubkey: toXOnly(internalKey.publicKey),
scriptTree,
redeem,
network: regtest,
});
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(output!),
amount,
);
const psbt = new bitcoin.Psbt({ network: regtest });
psbt.addInput({
hash: unspent.txId,
index: 0,
sequence: 10,
witnessUtxo: { value: BigInt(amount), script: output! },
});
psbt.updateInput(0, {
tapLeafScript: [
{
leafVersion: redeem.redeemVersion,
script: redeem.output,
controlBlock: witness![witness!.length - 1],
},
],
});
const sendInternalKey = bip32.fromSeed(rng(64), regtest);
const sendPubKey = toXOnly(sendInternalKey.publicKey);
const { address: sendAddress } = bitcoin.payments.p2tr({
internalPubkey: sendPubKey,
scriptTree,
network: regtest,
});
psbt.addOutput({ value: BigInt(sendAmount), address: sendAddress! });
// just to test that updateOutput works as expected
psbt.updateOutput(0, {
tapInternalKey: sendPubKey,
tapTree: { leaves: tapTreeToList(scriptTree) },
});
await psbt.signInputAsync(0, leafKey);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
try {
// broadcast before the confirmation period has expired
await regtestUtils.broadcast(hex);
throw new Error('Broadcast should fail.');
} catch (err) {
if ((err as any).message !== 'non-BIP68-final')
throw new Error(
'Expected OP_CHECKSEQUENCEVERIFY validation to fail. But it faild with: ' +
err,
);
}
await regtestUtils.mine(10);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
address: sendAddress!,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSIGADD (3-of-3)', async () => {
const internalKey = bip32.fromSeed(rng(64), regtest);
const leafKeys: BIP32Interface[] = [];
const leafPubkeys: string[] = [];
for (let i = 0; i < 3; i++) {
const leafKey = bip32.fromSeed(rng(64), regtest);
leafKeys.push(leafKey);
leafPubkeys.push(tools.toHex(toXOnly(leafKey.publicKey)));
}
const leafScriptAsm = `${leafPubkeys[2]} OP_CHECKSIG ${leafPubkeys[1]} OP_CHECKSIGADD ${leafPubkeys[0]} OP_CHECKSIGADD OP_3 OP_NUMEQUAL`;
const leafScript = bitcoin.script.fromASM(leafScriptAsm);
const scriptTree: Taptree = [
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0 OP_CHECKSIG',
),
},
[
{
output: bitcoin.script.fromASM(
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0 OP_CHECKSIG',
),
},
{
output: leafScript,
},
],
];
const redeem = {
output: leafScript,
redeemVersion: LEAF_VERSION_TAPSCRIPT,
};
const { output, address, witness } = bitcoin.payments.p2tr({
internalPubkey: toXOnly(internalKey.publicKey),
scriptTree,
redeem,
network: regtest,
});
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(output!),
amount,
);
const psbt = new bitcoin.Psbt({ network: regtest });
psbt.addInput({
hash: unspent.txId,
index: 0,
witnessUtxo: { value: BigInt(amount), script: output! },
});
psbt.updateInput(0, {
tapLeafScript: [
{
leafVersion: redeem.redeemVersion,
script: redeem.output,
controlBlock: witness![witness!.length - 1],
},
],
});
psbt.addOutput({ value: BigInt(sendAmount), address: address! });
// random order for signers
psbt.signInput(0, leafKeys[1]);
psbt.signInput(0, leafKeys[2]);
psbt.signInput(0, leafKeys[0]);
psbt.finalizeInput(0);
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
address: address!,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSIGADD (2-of-3) and verify unspendable internalKey', async () => {
const leafKeys: BIP32Interface[] = [];
const leafPubkeys: Uint8Array[] = [];
for (let i = 0; i < 3; i++) {
const leafKey = bip32.fromSeed(rng(64), regtest);
leafKeys.push(leafKey);
leafPubkeys.push(toXOnly(leafKey.publicKey));
}
// The only thing that differs between the wallets is the private key.
// So we will use the first wallet for all the Psbt stuff.
const [wallet, wallet2, wallet3] = leafKeys.map(key =>
new TaprootMultisigWallet(
leafPubkeys,
2, // Number of required signatures
key.privateKey!,
LEAF_VERSION_TAPSCRIPT,
).setNetwork(regtest),
);
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(wallet.output),
amount,
);
const psbt = new bitcoin.Psbt({ network: regtest });
// Adding an input is a bit special in this case,
// So we contain it in the wallet class
// Any wallet can do this, wallet2 or wallet3 could be used.
wallet.addInput(psbt, unspent.txId, unspent.vout, BigInt(unspent.value));
psbt.addOutput({ value: BigInt(sendAmount), address: wallet.address });
// Sign with at least 2 of the 3 wallets.
// Verify that there is a matching leaf script
// (which includes the unspendable internalPubkey,
// so we verify that no one can key-spend it)
wallet3.verifyInputScript(psbt, 0);
wallet2.verifyInputScript(psbt, 0);
psbt.signInput(0, wallet3);
psbt.signInput(0, wallet2);
// Before finalizing, we need to add dummy signatures for all that did not sign.
// Any wallet can do this, wallet2 or wallet3 could be used.
wallet.addDummySigs(psbt);
psbt.finalizeAllInputs();
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
// Any wallet can do this, wallet2 or wallet3 could be used.
address: wallet.address,
vout: 0,
value: sendAmount,
});
});
it('can create (and broadcast via 3PBP) a taproot script-path spend Transaction - custom finalizer', async () => {
const leafCount = 8;
const leaves = Array.from({ length: leafCount }).map(
(_, index) =>
({
depth: 3,
leafVersion: LEAF_VERSION_TAPSCRIPT,
script: bitcoin.script.fromASM(`OP_ADD OP_${index * 2} OP_EQUAL`),
}) as TapLeaf,
);
const scriptTree = tapTreeFromList(leaves);
for (let leafIndex = 1; leafIndex < leafCount; leafIndex++) {
const redeem = {
output: bitcoin.script.fromASM(`OP_ADD OP_${leafIndex * 2} OP_EQUAL`),
redeemVersion: LEAF_VERSION_TAPSCRIPT,
};
const internalKey = bip32.fromSeed(rng(64), regtest);
const { output, witness } = bitcoin.payments.p2tr({
internalPubkey: toXOnly(internalKey.publicKey),
scriptTree,
redeem,
network: regtest,
});
// amount from faucet
const amount = 42e4;
// amount to send
const sendAmount = amount - 1e4;
// get faucet
const unspent = await regtestUtils.faucetComplex(
Buffer.from(output!),
amount,
);
const psbt = new bitcoin.Psbt({ network: regtest });
psbt.addInput({
hash: unspent.txId,
index: 0,
witnessUtxo: { value: BigInt(amount), script: output! },
});
const tapLeafScript: TapLeafScript = {
leafVersion: redeem.redeemVersion,
script: redeem.output,
controlBlock: witness![witness!.length - 1],
};
psbt.updateInput(0, { tapLeafScript: [tapLeafScript] });
const sendAddress =
'bcrt1pqknex3jwpsaatu5e5dcjw70nac3fr5k5y3hcxr4hgg6rljzp59nqs6a0vh';
psbt.addOutput({
value: BigInt(sendAmount),
address: sendAddress,
});
const leafIndexFinalizerFn = buildLeafIndexFinalizer(
tapLeafScript,
leafIndex,
);
psbt.finalizeInput(0, leafIndexFinalizerFn);
const tx = psbt.extractTransaction();
const rawTx = tx.toBuffer();
const hex = tools.toHex(rawTx);
await regtestUtils.broadcast(hex);
await regtestUtils.verify({
txId: tx.getId(),
address: sendAddress!,
vout: 0,
value: sendAmount,
});
}
});
it('should fail validating invalid signatures for taproot (See issue #1931)', () => {
const schnorrValidator = (
pubkey: Uint8Array,
msghash: Uint8Array,
signature: Uint8Array,
) => {
return ecc.verifySchnorr(msghash, pubkey, signature);
};
const psbtBase64 =
`cHNidP8BAFICAAAAAe1h73A6zedruNERV6JU7Ty1IlYZh2KO1cBklZqCMEy8AAAAAAD/////ARA
nAAAAAAAAFgAUS0GlfqWSeEWIpwPwrvRIjBbJQroAAAAAAAEA/TgBAQAAAAABAnGJ6st1FIvYLEV
bJMQaZ3HSOJnkw5C+ViCuJYiFEYosAAAAAAD9////xuZd0xArNSaBuElLX3nzjwtZW95O7L/wbz9
4v+v0vuYAAAAAAP3///8CECcAAAAAAAAiUSAVbMSHgwYVdyBgfNy0syr6TMaFOGhFjXJYuQcRLlp
DS8hgBwAAAAAAIlEgthWGz3o2R7WpgjIK52ODoEaA/0HcImSUjVk6agZgghwBQIP9WWErMfeBBYy
uHuSZS7MdXVICtlFgNveDrvuXeQGSZl1gGG6/r3Aw7h9TifGtoA+7JwYBjLMcEG6hbeyQGXIBQNS
qKH1p/NFzO9bxe9vpvBZQIaX5Qa9SY2NfNCgSRNabmX5EiaihWcLC+ALgchm7DUfYrAmi1r4uSI/
YaQ1lq8gAAAAAAQErECcAAAAAAAAiUSAVbMSHgwYVdyBgfNy0syr6TMaFOGhFjXJYuQcRLlpDSwE
DBIMAAAABCEMBQZUpv6e1Hwfpi/PpglkkK/Rx40vZIIHwtJ7dXWFZ5TcZUEelCnfKOAWZ4xWjauY
M2y+JcgFcVsuPzPuiM+z5AH+DARNBlSm/p7UfB+mL8+mCWSQr9HHjS9kggfC0nt1dYVnlNxlQR6U
Kd8o4BZnjFaNq5gzbL4lyAVxWy4/M+6Iz7PkAf4MBFyC6ZCT2zZVrEbkw/T1fyS8eLKQaP2MH6rz
dlMauGvQzLQAA`.replace(/\s+/g, '');
const psbt = bitcoin.Psbt.fromBase64(psbtBase64);
assert.ok(
!psbt.validateSignaturesOfAllInputs(schnorrValidator),
'Should fail validation',
);
});
it('should succeed validating valid signatures for taproot (See issue #1934)', () => {
const schnorrValidator = (
pubkey: Uint8Array,
msghash: Uint8Array,
signature: Uint8Array,
) => {
return ecc.verifySchnorr(msghash, pubkey, signature);
};
const psbtBase64 =
`cHNidP8BAF4CAAAAAU6UzYPa7tES0HoS+obnRJuXX41Ob64Zs59qDEyKsu1ZAAAAAAD/////AYA
zAjsAAAAAIlEgIlIzfR+flIWYTyewD9v+1N84IubZ/7qg6oHlYLzv1aYAAAAAAAEAXgEAAAAB8f+
afEJBun7sRQLFE1Olc/gK9LBaduUpz3vB4fjXVF0AAAAAAP3///8BECcAAAAAAAAiUSAiUjN9H5+
UhZhPJ7AP2/7U3zgi5tn/uqDqgeVgvO/VpgAAAAABASsQJwAAAAAAACJRICJSM30fn5SFmE8nsA/
b/tTfOCLm2f+6oOqB5WC879WmAQMEgwAAAAETQWQwNOao3RMOBWPuAQ9Iph7Qzk47MvroTHbJR49
MxKJmQ6hfhZa5wVVrdKYea5BW/loqa7al2pYYZMlGvdS06wODARcgjuYXxIpyOMVTYEvl35gDidC
m/vUICZyuNNZKaPz9dxAAAQUgjuYXxIpyOMVTYEvl35gDidCm/vUICZyuNNZKaPz9dxAA`.replace(
/\s+/g,
'',
);
const psbt = bitcoin.Psbt.fromBase64(psbtBase64);
assert.ok(
psbt.validateSignaturesOfAllInputs(schnorrValidator),
'Should succeed validation',
);
});
});
function buildLeafIndexFinalizer(
tapLeafScript: TapLeafScript,
leafIndex: number,
): (
inputIndex: number,
_input: PsbtInput,
_tapLeafHashToFinalize?: Uint8Array,
) => {
finalScriptWitness: Uint8Array | undefined;
} {
return (
inputIndex: number,
_input: PsbtInput,
_tapLeafHashToFinalize?: Uint8Array,
): {
finalScriptWitness: Uint8Array | undefined;
} => {
try {
const scriptSolution = [
Uint8Array.from([leafIndex]),
Uint8Array.from([leafIndex]),
];
const witness = scriptSolution
.concat(tapLeafScript.script)
.concat(tapLeafScript.controlBlock);
return { finalScriptWitness: witnessStackToScriptWitness(witness) };
} catch (err) {
throw new Error(`Can not finalize taproot input #${inputIndex}: ${err}`);
}
};
}
function makeUnspendableInternalKey(provableNonce?: Uint8Array): Uint8Array {
// This is the generator point of secp256k1. Private key is known (equal to 1)
const G = Buffer.from(
'0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
'hex',
);
// This is the hash of the uncompressed generator point.
// It is also a valid X value on the curve, but we don't know what the private key is.
// Since we know this X value (a fake "public key") is made from a hash of a well known value,
// We can prove that the internalKey is unspendable.
const Hx = sha256(G);
// This "Nothing Up My Sleeve" value is mentioned in BIP341 so we verify it here:
assert.strictEqual(
tools.toHex(Hx),
'50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0',
);
if (provableNonce) {
if (provableNonce.length !== 32) {
throw new Error(
'provableNonce must be a 32 byte random value shared between script holders',
);
}
// Using a shared random value, we create an unspendable internalKey
// P = H + int(hash_taptweak(provableNonce))*G
// Since we don't know H's private key (see explanation above), we can't know P's private key
const tapHash = bitcoin.crypto.taggedHash('TapTweak', provableNonce);
const ret = ecc.xOnlyPointAddTweak(Hx, tapHash);
if (!ret) {
throw new Error(
'provableNonce produced an invalid key when tweaking the G hash',
);
}
return Buffer.from(ret.xOnlyPubkey);
} else {
// The downside to using no shared provable nonce is that anyone viewing a spend
// on the blockchain can KNOW that you CAN'T use key spend.
// Most people would be ok with this being public, but some wallets (exchanges etc)
// might not want ANY details about how their wallet works public.
return Hx;
}
}
class TaprootMultisigWallet {
private leafScriptCache: Uint8Array | null = null;
private internalPubkeyCache: Uint8Array | null = null;
private paymentCache: bitcoin.Payment | null = null;
private readonly publicKeyCache: Uint8Array;
network: bitcoin.Network;
constructor(
/**
* A list of all the (x-only) pubkeys in the multisig
*/
private readonly pubkeys: Uint8Array[],
/**
* The number of required signatures
*/
private readonly requiredSigs: number,
/**
* The private key you hold.
*/
private readonly privateKey: Uint8Array,
/**
* leaf version (0xc0 currently)
*/
readonly leafVersion: number,
/**
* Optional shared nonce. This should be used in wallets where
* the fact that key-spend is unspendable should not be public,
* BUT each signer must verify that it is unspendable to be safe.
*/
private readonly sharedNonce?: Uint8Array,
) {
this.network = bitcoin.networks.bitcoin;
assert.ok(pubkeys.length > 0, 'Need pubkeys');
assert.ok(
pubkeys.every(p => p.length === 32),
'Pubkeys must be 32 bytes (x-only)',
);
assert.ok(
requiredSigs > 0 && requiredSigs <= pubkeys.length,
'Invalid requiredSigs',
);
assert.ok(
leafVersion <= 0xff && (leafVersion & 1) === 0,
'Invalid leafVersion',
);
if (sharedNonce) {
assert.ok(
sharedNonce.length === 32 && ecc.isPrivate(sharedNonce),
'Invalid sharedNonce',
);
}
const pubkey = ecc.pointFromScalar(privateKey);
assert.ok(pubkey, 'Invalid pubkey');
this.publicKeyCache = Buffer.from(pubkey);
assert.ok(
pubkeys.some(p => tools.compare(p, toXOnly(this.publicKeyCache))),
'At least one pubkey must match your private key',
);
// IMPORTANT: Make sure the pubkeys are sorted (To prevent ordering issues between wallet signers)
this.pubkeys.sort((a, b) => tools.compare(a, b));
}
setNetwork(network: bitcoin.Network): this {
this.network = network;
return this;
}
// Required for Signer interface.
// Prevent setting by using a getter.
get publicKey(): Uint8Array {
return this.publicKeyCache;
}
/**
* Lazily build the leafScript. A 2 of 3 would look like:
* key1 OP_CHECKSIG key2 OP_CHECKSIGADD key3 OP_CHECKSIGADD OP_2 OP_GREATERTHANOREQUAL
*/
get leafScript(): Uint8Array {
if (this.leafScriptCache) {
return this.leafScriptCache;
}
const ops: bitcoin.Stack = [];
this.pubkeys.forEach(pubkey => {
if (ops.length === 0) {
ops.push(pubkey);
ops.push(bitcoin.opcodes.OP_CHECKSIG);
} else {
ops.push(pubkey);
ops.push(bitcoin.opcodes.OP_CHECKSIGADD);
}
});
if (this.requiredSigs > 16) {
ops.push(bitcoin.script.number.encode(this.requiredSigs));
} else {
ops.push(bitcoin.opcodes.OP_1 - 1 + this.requiredSigs);
}
ops.push(bitcoin.opcodes.OP_GREATERTHANOREQUAL);
this.leafScriptCache = bitcoin.script.compile(ops);
return this.leafScriptCache;
}
get internalPubkey(): Uint8Array {
if (this.internalPubkeyCache) {
return this.internalPubkeyCache;
}
// See the helper function for explanation
this.internalPubkeyCache = makeUnspendableInternalKey(this.sharedNonce);
return this.internalPubkeyCache;
}
get scriptTree(): Taptree {
// If more complicated, maybe it should be cached.
// (ie. if other scripts are created only to create the tree
// and will only be stored in the tree.)
return {
output: this.leafScript,
};
}
get redeem(): {
output: Uint8Array;
redeemVersion: number;
} {
return {
output: this.leafScript,
redeemVersion: this.leafVersion,
};
}
private get payment(): bitcoin.Payment {
if (this.paymentCache) {
return this.paymentCache;
}
this.paymentCache = bitcoin.payments.p2tr({
internalPubkey: this.internalPubkey,
scriptTree: this.scriptTree,
redeem: this.redeem,
network: this.network,
});
return this.paymentCache;
}
get output(): Uint8Array {
return this.payment.output!;
}
get address(): string {
return this.payment.address!;
}
get controlBlock(): Uint8Array {
const witness = this.payment.witness!;
return witness[witness.length - 1];
}
verifyInputScript(psbt: bitcoin.Psbt, index: number) {