-
Notifications
You must be signed in to change notification settings - Fork 14
/
descriptors.ts
768 lines (747 loc) · 28.9 KB
/
descriptors.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
// Copyright (c) 2023 Jose-Luis Landabaso - https://bitcoinerlab.com
// Distributed under the MIT software license
import {
address,
networks,
payments,
script as bscript,
Network,
Transaction,
Payment,
Psbt
} from 'bitcoinjs-lib';
import type { PartialSig } from 'bip174/src/lib/interfaces';
const { p2sh, p2wpkh, p2pkh, p2pk, p2wsh, p2tr } = payments;
import { BIP32Factory, BIP32API } from 'bip32';
import { ECPairFactory, ECPairAPI } from 'ecpair';
import type {
TinySecp256k1Interface,
Preimage,
TimeConstraints,
ExpansionMap,
ParseKeyExpression,
DescriptorInterfaceConstructor,
DescriptorInterface,
DescriptorInfo
} from './types';
import { finalScriptsFuncFactory, updatePsbt } from './psbt';
import { DescriptorChecksum } from './checksum';
import { parseKeyExpression as globalParseKeyExpression } from './keyExpressions';
import * as RE from './re';
import {
expandMiniscript as globalExpandMiniscript,
miniscript2Script,
satisfyMiniscript
} from './miniscript';
//See "Resource limitations" https://bitcoin.sipa.be/miniscript/
//https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-September/017306.html
const MAX_SCRIPT_ELEMENT_SIZE = 520;
const MAX_STANDARD_P2WSH_SCRIPT_SIZE = 3600;
const MAX_OPS_PER_SCRIPT = 201;
function countNonPushOnlyOPs(script: Buffer): number {
const decompile = bscript.decompile(script);
if (!decompile) throw new Error(`Error: cound not decompile ${script}`);
return decompile.filter(op => op > bscript.OPS['OP_16']!).length;
}
/*
* Returns a bare descriptor without checksum and particularized for a certain
* index (if desc was a range descriptor)
*/
function evaluate({
expression,
checksumRequired,
index
}: {
expression: string;
checksumRequired: boolean;
index?: number;
}): string {
const mChecksum = expression.match(String.raw`(${RE.reChecksum})$`);
if (mChecksum === null && checksumRequired === true)
throw new Error(`Error: descriptor ${expression} has not checksum`);
//evaluatedExpression: a bare desc without checksum and particularized for a certain
//index (if desc was a range descriptor)
let evaluatedExpression = expression;
if (mChecksum !== null) {
const checksum = mChecksum[0].substring(1); //remove the leading #
evaluatedExpression = expression.substring(
0,
expression.length - mChecksum[0].length
);
if (checksum !== DescriptorChecksum(evaluatedExpression)) {
throw new Error(`Error: invalid descriptor checksum for ${expression}`);
}
}
const mWildcard = evaluatedExpression.match(/\*/g);
if (mWildcard && mWildcard.length > 0) {
if (index === undefined)
throw new Error(`Error: index was not provided for ranged descriptor`);
if (!Number.isInteger(index) || index < 0)
throw new Error(`Error: invalid index ${index}`);
//From https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md
//To prevent a combinatorial explosion of the search space, if more than
//one of the multi() key arguments is a BIP32 wildcard path ending in /* or
//*', the multi() expression only matches multisig scripts with the ith
//child key from each wildcard path in lockstep, rather than scripts with
//any combination of child keys from each wildcard path.
//We extend this reasoning for musig for all cases
evaluatedExpression = evaluatedExpression.replaceAll('*', index.toString());
}
return evaluatedExpression;
}
/**
* Builds the functions needed to operate with descriptors using an external elliptic curve (ecc) library.
* @param {Object} ecc - an object containing elliptic curve operations, such as [tiny-secp256k1](https://github.com/bitcoinjs/tiny-secp256k1) or [@bitcoinerlab/secp256k1](https://github.com/bitcoinerlab/secp256k1).
* @namespace
*/
export function DescriptorsFactory(ecc: TinySecp256k1Interface): {
Descriptor: DescriptorInterfaceConstructor;
ECPair: ECPairAPI;
parseKeyExpression: ParseKeyExpression;
BIP32: BIP32API;
} {
const BIP32: BIP32API = BIP32Factory(ecc);
const ECPair: ECPairAPI = ECPairFactory(ecc);
const signatureValidator = (
pubkey: Buffer,
msghash: Buffer,
signature: Buffer
): boolean => ECPair.fromPublicKey(pubkey).verify(msghash, signature);
/*
* Takes a string key expression (xpub, xprv, pubkey or wif) and parses it
*/
const parseKeyExpression: ParseKeyExpression = ({
keyExpression,
isSegwit,
network = networks.bitcoin
}) => {
return globalParseKeyExpression({
keyExpression,
network,
...(typeof isSegwit === 'boolean' ? { isSegwit } : {}),
ECPair,
BIP32
});
};
/**
* Expand a miniscript to a generalized form using variables instead of key
* expressions. Variables will be of this form: @0, @1, ...
* This is done so that it can be compiled with compileMiniscript and
* satisfied with satisfier.
* Also compute pubkeys from descriptors to use them later.
*/
function expandMiniscript({
miniscript,
isSegwit,
network = networks.bitcoin
}: {
miniscript: string;
isSegwit: boolean;
network?: Network;
}): {
expandedMiniscript: string;
expansionMap: ExpansionMap;
} {
return globalExpandMiniscript({
miniscript,
isSegwit,
network,
BIP32,
ECPair
});
}
class Descriptor implements DescriptorInterface {
readonly #payment: Payment;
readonly #preimages: Preimage[] = [];
readonly #signersPubKeys: Buffer[];
readonly #miniscript?: string;
readonly #witnessScript?: Buffer;
readonly #redeemScript?: Buffer;
//isSegwit true if witnesses are needed to the spend coins sent to this descriptor.
//may be unset because we may get addr(P2SH) which we don't know if they have segwit.
readonly #isSegwit?: boolean;
readonly #expandedExpression?: string;
readonly #expandedMiniscript?: string;
readonly #expansionMap?: ExpansionMap;
readonly #network: Network;
/**
* @param {DescriptorInfo} params
* @param {string} params.expression - The descriptor string in ASCII format. It may include a "*" to denote an arbitrary index.
* @param {number} params.index - The descriptor's index in the case of a range descriptor (must be an interger >=0).
* @param {boolean} [params.checksumRequired=false] - A flag indicating whether the descriptor is required to include a checksum.
* @param {boolean} [params.allowMiniscriptInP2SH=false] - A flag indicating whether this instance can parse and generate script satisfactions for sh(miniscript) top-level expressions of miniscripts. This is not recommended.
* @param {object} [params.network=networks.bitcoin] One of bitcoinjs-lib [`networks`](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/networks.js) (or another one following the same interface).
* @param {Preimage[]} [params.preimages=[]] An array of preimages. This info is necessary to finalize Psbts.
* @param {Buffer[]} [params.signersPubKeys] - An array of public keys that will be used to sign the output described by this descriptor. If all the keys in the descriptor's `expression` will sign the transaction, you can leave this parameter `undefined`. This parameter is useful in miniscript-based expressions where there are different spending paths depending on the keys that are known. In that case, set this parameter to an array of the public keys that will be used to sign the output.
*
* @throws {Error} - when descriptor is invalid
*/
constructor({
expression,
index,
checksumRequired = false,
allowMiniscriptInP2SH = false,
network = networks.bitcoin,
preimages = [],
signersPubKeys
}: DescriptorInfo) {
this.#network = network;
this.#preimages = preimages;
if (typeof expression !== 'string')
throw new Error(`Error: invalid descriptor type`);
//Verify and remove checksum (if exists) and
//particularize range descriptor for index (if desc is range descriptor)
const evaluatedExpression = evaluate({
expression,
...(index !== undefined ? { index } : {}),
checksumRequired
});
//addr(ADDR)
if (evaluatedExpression.match(RE.reAddrAnchored)) {
const matchedAddress = evaluatedExpression.match(
RE.reAddrAnchored
)?.[1]; //[1]-> whatever is found addr(->HERE<-)
if (!matchedAddress)
throw new Error(
`Error: could not get an address in ${evaluatedExpression}`
);
let output;
let payment;
try {
output = address.toOutputScript(matchedAddress, network);
} catch (e) {
throw new Error(`Error: invalid address ${matchedAddress}`);
}
try {
payment = p2pkh({ output, network });
} catch (e) {}
try {
payment = p2sh({ output, network });
} catch (e) {}
try {
payment = p2wpkh({ output, network });
} catch (e) {}
try {
payment = p2wsh({ output, network });
} catch (e) {}
try {
payment = p2tr({ output, network });
} catch (e) {}
if (!payment) {
throw new Error(`Error: invalid address ${matchedAddress}`);
}
this.#payment = payment;
}
//pk(KEY)
else if (evaluatedExpression.match(RE.rePkAnchored)) {
this.#isSegwit = false;
const keyExpression = evaluatedExpression.match(RE.reKeyExp)?.[0];
if (!keyExpression)
throw new Error(`Error: keyExpression could not me extracted`);
if (evaluatedExpression !== `pk(${keyExpression})`)
throw new Error(`Error: invalid expression ${expression}`);
this.#expandedExpression = 'pk(@0)';
this.#expansionMap = {
'@0': parseKeyExpression({
keyExpression,
network,
isSegwit: this.#isSegwit
})
};
const pubkey = this.#expansionMap['@0']!.pubkey;
//Note there exists no address for p2pk, but we can still use the script
this.#payment = p2pk({ pubkey, network });
}
//pkh(KEY) - legacy
else if (evaluatedExpression.match(RE.rePkhAnchored)) {
this.#isSegwit = false;
const keyExpression = evaluatedExpression.match(RE.reKeyExp)?.[0];
if (!keyExpression)
throw new Error(`Error: keyExpression could not me extracted`);
if (evaluatedExpression !== `pkh(${keyExpression})`)
throw new Error(`Error: invalid expression ${expression}`);
this.#expandedExpression = 'pkh(@0)';
this.#expansionMap = {
'@0': parseKeyExpression({
keyExpression,
network,
isSegwit: this.#isSegwit
})
};
const pubkey = this.#expansionMap['@0']!.pubkey;
this.#payment = p2pkh({ pubkey, network });
}
//sh(wpkh(KEY)) - nested segwit
else if (evaluatedExpression.match(RE.reShWpkhAnchored)) {
this.#isSegwit = true;
const keyExpression = evaluatedExpression.match(RE.reKeyExp)?.[0];
if (!keyExpression)
throw new Error(`Error: keyExpression could not me extracted`);
if (evaluatedExpression !== `sh(wpkh(${keyExpression}))`)
throw new Error(`Error: invalid expression ${expression}`);
this.#expandedExpression = 'sh(wpkh(@0))';
this.#expansionMap = {
'@0': parseKeyExpression({
keyExpression,
network,
isSegwit: this.#isSegwit
})
};
const pubkey = this.#expansionMap['@0']!.pubkey;
this.#payment = p2sh({ redeem: p2wpkh({ pubkey, network }), network });
const redeemScript = this.#payment.redeem?.output;
if (!redeemScript)
throw new Error(
`Error: could not calculate redeemScript for ${expression}`
);
this.#redeemScript = redeemScript;
}
//wpkh(KEY) - native segwit
else if (evaluatedExpression.match(RE.reWpkhAnchored)) {
this.#isSegwit = true;
const keyExpression = evaluatedExpression.match(RE.reKeyExp)?.[0];
if (!keyExpression)
throw new Error(`Error: keyExpression could not me extracted`);
if (evaluatedExpression !== `wpkh(${keyExpression})`)
throw new Error(`Error: invalid expression ${expression}`);
this.#expandedExpression = 'wpkh(@0)';
this.#expansionMap = {
'@0': parseKeyExpression({
keyExpression,
network,
isSegwit: this.#isSegwit
})
};
const pubkey = this.#expansionMap['@0']!.pubkey;
this.#payment = p2wpkh({ pubkey, network });
}
//sh(wsh(miniscript))
else if (evaluatedExpression.match(RE.reShWshMiniscriptAnchored)) {
this.#isSegwit = true;
const miniscript = evaluatedExpression.match(
RE.reShWshMiniscriptAnchored
)?.[1]; //[1]-> whatever is found sh(wsh(->HERE<-))
if (!miniscript)
throw new Error(
`Error: could not get miniscript in ${evaluatedExpression}`
);
this.#miniscript = miniscript;
({
expandedMiniscript: this.#expandedMiniscript,
expansionMap: this.#expansionMap
} = expandMiniscript({
miniscript,
isSegwit: this.#isSegwit,
network
}));
this.#expandedExpression = `sh(wsh(${this.#expandedMiniscript}))`;
const script = miniscript2Script({
expandedMiniscript: this.#expandedMiniscript,
expansionMap: this.#expansionMap
});
this.#witnessScript = script;
if (script.byteLength > MAX_STANDARD_P2WSH_SCRIPT_SIZE) {
throw new Error(
`Error: script is too large, ${script.byteLength} bytes is larger than ${MAX_STANDARD_P2WSH_SCRIPT_SIZE} bytes`
);
}
const nonPushOnlyOps = countNonPushOnlyOPs(script);
if (nonPushOnlyOps > MAX_OPS_PER_SCRIPT) {
throw new Error(
`Error: too many non-push ops, ${nonPushOnlyOps} non-push ops is larger than ${MAX_OPS_PER_SCRIPT}`
);
}
this.#payment = p2sh({
redeem: p2wsh({ redeem: { output: script, network }, network }),
network
});
const redeemScript = this.#payment.redeem?.output;
if (!redeemScript)
throw new Error(
`Error: could not calculate redeemScript for ${expression}`
);
this.#redeemScript = redeemScript;
}
//sh(miniscript)
else if (evaluatedExpression.match(RE.reShMiniscriptAnchored)) {
//isSegwit false because we know it's a P2SH of a miniscript and not a
//P2SH that embeds a witness payment.
this.#isSegwit = false;
const miniscript = evaluatedExpression.match(
RE.reShMiniscriptAnchored
)?.[1]; //[1]-> whatever is found sh(->HERE<-)
if (!miniscript)
throw new Error(
`Error: could not get miniscript in ${evaluatedExpression}`
);
if (
allowMiniscriptInP2SH === false &&
//These top-level expressions within sh are allowed within sh.
//They can be parsed with miniscript2Script, but first we must make sure
//that other expressions are not accepted (unless forced with allowMiniscriptInP2SH).
miniscript.search(
/^(pk\(|pkh\(|wpkh\(|combo\(|multi\(|sortedmulti\(|multi_a\(|sortedmulti_a\()/
) !== 0
) {
throw new Error(
`Error: Miniscript expressions can only be used in wsh`
);
}
this.#miniscript = miniscript;
({
expandedMiniscript: this.#expandedMiniscript,
expansionMap: this.#expansionMap
} = expandMiniscript({
miniscript,
isSegwit: this.#isSegwit,
network
}));
this.#expandedExpression = `sh(${this.#expandedMiniscript})`;
const script = miniscript2Script({
expandedMiniscript: this.#expandedMiniscript,
expansionMap: this.#expansionMap
});
this.#redeemScript = script;
if (script.byteLength > MAX_SCRIPT_ELEMENT_SIZE) {
throw new Error(
`Error: P2SH script is too large, ${script.byteLength} bytes is larger than ${MAX_SCRIPT_ELEMENT_SIZE} bytes`
);
}
const nonPushOnlyOps = countNonPushOnlyOPs(script);
if (nonPushOnlyOps > MAX_OPS_PER_SCRIPT) {
throw new Error(
`Error: too many non-push ops, ${nonPushOnlyOps} non-push ops is larger than ${MAX_OPS_PER_SCRIPT}`
);
}
this.#payment = p2sh({ redeem: { output: script, network }, network });
if (Buffer.compare(script, this.getRedeemScript()!) !== 0)
throw new Error(
`Error: redeemScript was not correctly set to the payment in expression ${expression}`
);
}
//wsh(miniscript)
else if (evaluatedExpression.match(RE.reWshMiniscriptAnchored)) {
this.#isSegwit = true;
const miniscript = evaluatedExpression.match(
RE.reWshMiniscriptAnchored
)?.[1]; //[1]-> whatever is found wsh(->HERE<-)
if (!miniscript)
throw new Error(
`Error: could not get miniscript in ${evaluatedExpression}`
);
this.#miniscript = miniscript;
({
expandedMiniscript: this.#expandedMiniscript,
expansionMap: this.#expansionMap
} = expandMiniscript({
miniscript,
isSegwit: this.#isSegwit,
network
}));
this.#expandedExpression = `wsh(${this.#expandedMiniscript})`;
const script = miniscript2Script({
expandedMiniscript: this.#expandedMiniscript,
expansionMap: this.#expansionMap
});
this.#witnessScript = script;
if (script.byteLength > MAX_STANDARD_P2WSH_SCRIPT_SIZE) {
throw new Error(
`Error: script is too large, ${script.byteLength} bytes is larger than ${MAX_STANDARD_P2WSH_SCRIPT_SIZE} bytes`
);
}
const nonPushOnlyOps = countNonPushOnlyOPs(script);
if (nonPushOnlyOps > MAX_OPS_PER_SCRIPT) {
throw new Error(
`Error: too many non-push ops, ${nonPushOnlyOps} non-push ops is larger than ${MAX_OPS_PER_SCRIPT}`
);
}
this.#payment = p2wsh({ redeem: { output: script, network }, network });
} else {
throw new Error(`Error: Could not parse descriptor ${expression}`);
}
if (signersPubKeys) {
this.#signersPubKeys = signersPubKeys;
} else {
if (this.#expansionMap) {
this.#signersPubKeys = Object.values(this.#expansionMap).map(
keyInfo => keyInfo.pubkey
);
} else {
//We should only miss expansionMap in addr() expressions:
if (!evaluatedExpression.match(RE.reAddrAnchored)) {
throw new Error(
`Error: expansionMap not available for expression ${expression} that is not an address`
);
}
this.#signersPubKeys = [this.getScriptPubKey()];
}
}
}
/**
* Gets the TimeConstraints (nSequence and nLockTime) of the miniscript
* descriptor as passed in the constructor, just using the expression,
* the signersPubKeys and preimages.
*
* We just need to know which will be the signatures that will be
* used (signersPubKeys) but final signatures are not necessary for
* obtaning nLockTime and nSequence.
*
* Remember: nSequence and nLockTime are part of the hash that is signed.
* Thus, they must not change after computing the signatures.
* When running getScriptSatisfaction, using the final signatures,
* satisfyMiniscript verifies that the time constraints did not change.
*/
#getTimeConstraints(): TimeConstraints | undefined {
const miniscript = this.#miniscript;
const preimages = this.#preimages;
const expandedMiniscript = this.#expandedMiniscript;
const expansionMap = this.#expansionMap;
const signersPubKeys = this.#signersPubKeys;
//Create a method. solvePreimages to solve them.
if (miniscript) {
if (expandedMiniscript === undefined || expansionMap === undefined)
throw new Error(
`Error: cannot get time constraints from not expanded miniscript ${miniscript}`
);
//We create some fakeSignatures since we may not have them yet.
//We only want to retrieve the nLockTime and nSequence of the satisfaction and
//signatures don't matter
const fakeSignatures = signersPubKeys.map(pubkey => ({
pubkey,
signature: Buffer.alloc(64, 0)
}));
const { nLockTime, nSequence } = satisfyMiniscript({
expandedMiniscript,
expansionMap,
signatures: fakeSignatures,
preimages
});
return { nLockTime, nSequence };
} else return undefined;
}
getPayment(): Payment {
return this.#payment;
}
/**
* Returns the Bitcoin Address
*/
getAddress(): string {
if (!this.#payment.address)
throw new Error(`Error: could extract an address from the payment`);
return this.#payment.address;
}
getScriptPubKey(): Buffer {
if (!this.#payment.output)
throw new Error(`Error: could extract output.script from the payment`);
return this.#payment.output;
}
/**
* Returns the compiled script satisfaction
* @param {PartialSig[]} signatures An array of signatures using this format: `interface PartialSig { pubkey: Buffer; signature: Buffer; }`
* @returns {Buffer}
*/
getScriptSatisfaction(signatures: PartialSig[]): Buffer {
const miniscript = this.#miniscript;
const expandedMiniscript = this.#expandedMiniscript;
const expansionMap = this.#expansionMap;
if (
miniscript === undefined ||
expandedMiniscript === undefined ||
expansionMap === undefined
)
throw new Error(
`Error: cannot get satisfaction from not expanded miniscript ${miniscript}`
);
//Note that we pass the nLockTime and nSequence that is deduced
//using preimages and signersPubKeys.
//satisfyMiniscript will make sure
//that the actual solution given, using real signatures, still meets the
//same nLockTime and nSequence constraints
const scriptSatisfaction = satisfyMiniscript({
expandedMiniscript,
expansionMap,
signatures,
preimages: this.#preimages,
//Here we pass the TimeConstraints obtained using signersPubKeys to
//verify that the solutions found using the final signatures have not
//changed
timeConstraints: {
nLockTime: this.getLockTime(),
nSequence: this.getSequence()
}
}).scriptSatisfaction;
if (!scriptSatisfaction)
throw new Error(`Error: could not produce a valid satisfaction`);
return scriptSatisfaction;
}
getSequence(): number | undefined {
return this.#getTimeConstraints()?.nSequence;
}
getLockTime(): number | undefined {
return this.#getTimeConstraints()?.nLockTime;
}
getWitnessScript(): Buffer | undefined {
return this.#witnessScript;
}
getRedeemScript(): Buffer | undefined {
return this.#redeemScript;
}
getNetwork(): Network {
return this.#network;
}
isSegwit(): boolean | undefined {
return this.#isSegwit;
}
/**
* Updates a Psbt where the descriptor describes an utxo.
* The txHex (nonWitnessUtxo) and vout of the utxo must be passed.
*
* updatePsbt adds an input to the psbt and updates the tx locktime if needed.
* It also adds a new input to the Psbt based on txHex
* It returns the number of the input that is added.
* psbt and vout are mandatory. Also pass txHex.
*
* The following is not recommended but, alternatively, ONLY for Segwit inputs,
* you can pass txId and value, instead of txHex.
* If you do so, it is your responsibility to make sure that `value` is
* correct to avoid possible fee vulnerability attacks:
* https://github.com/bitcoinjs/bitcoinjs-lib/issues/1625
* Note that HW wallets require the full txHex also for Segwit anyways:
* https://blog.trezor.io/details-of-firmware-updates-for-trezor-one-version-1-9-1-and-trezor-model-t-version-2-3-1-1eba8f60f2dd
*
* In doubt, simply pass txHex (and you can skip passing txId and value) and
* you shall be fine.
*/
updatePsbt({
psbt,
txHex,
txId,
value,
vout //vector output index
}: {
psbt: Psbt;
txHex?: string;
txId?: string;
value?: number;
vout: number;
}): number {
if (txHex === undefined) {
console.warn(`Warning: missing txHex may allow fee attacks`);
}
const isSegwit = this.isSegwit();
if (isSegwit === undefined) {
//This should only happen when using addr() expressions
throw new Error(
`Error: could not determine whether this is a segwit descriptor`
);
}
return updatePsbt({
psbt,
vout,
...(txHex !== undefined ? { txHex } : {}),
...(txId !== undefined ? { txId } : {}),
...(value !== undefined ? { value } : {}),
sequence: this.getSequence(),
locktime: this.getLockTime(),
keysInfo: this.#expansionMap ? Object.values(this.#expansionMap) : [],
scriptPubKey: this.getScriptPubKey(),
isSegwit,
witnessScript: this.getWitnessScript(),
redeemScript: this.getRedeemScript()
});
}
#assertPsbtInput({ psbt, index }: { psbt: Psbt; index: number }): void {
const input = psbt.data.inputs[index];
const txInput = psbt.txInputs[index];
if (!input || !txInput)
throw new Error(`Error: invalid input or txInput`);
const { sequence: inputSequence, index: vout } = txInput;
let scriptPubKey;
if (input.witnessUtxo) scriptPubKey = input.witnessUtxo.script;
else {
if (!input.nonWitnessUtxo)
throw new Error(
`Error: input should have either witnessUtxo or nonWitnessUtxo`
);
const tx = Transaction.fromBuffer(input.nonWitnessUtxo);
const out = tx.outs[vout];
if (!out) throw new Error(`Error: utxo should exist`);
scriptPubKey = out.script;
}
const locktime = this.getLockTime() || 0;
let sequence = this.getSequence();
if (sequence === undefined && locktime !== 0) sequence = 0xfffffffe;
if (sequence === undefined && locktime === 0) sequence = 0xffffffff;
if (
Buffer.compare(scriptPubKey, this.getScriptPubKey()) !== 0 ||
sequence !== inputSequence ||
locktime !== psbt.locktime ||
this.getWitnessScript() !== input.witnessScript ||
this.getRedeemScript() !== input.redeemScript
) {
throw new Error(
`Error: cannot finalize psbt index ${index} since it does not correspond to this descriptor`
);
}
}
finalizePsbtInput({
index,
psbt,
validate = true
}: {
index: number;
psbt: Psbt;
validate?: boolean | undefined;
}): void {
if (
validate &&
!psbt.validateSignaturesOfInput(index, signatureValidator)
) {
throw new Error(`Error: invalid signatures on input ${index}`);
}
//An index must be passed since finding the index in the psbt cannot be
//done:
//Imagine the case where you received money twice to
//the same miniscript-based address. You would have the same scriptPubKey,
//same sequences, ... The descriptor does not store the hash of the previous
//transaction since it is a general Descriptor object. Indices must be kept
//out of the scope of this class and then passed.
const signatures = psbt.data.inputs[index]?.partialSig;
if (!signatures)
throw new Error(`Error: cannot finalize without signatures`);
this.#assertPsbtInput({ index, psbt });
if (!this.#miniscript) {
//Use standard finalizers
psbt.finalizeInput(index);
} else {
const scriptSatisfaction = this.getScriptSatisfaction(signatures);
psbt.finalizeInput(
index,
finalScriptsFuncFactory(scriptSatisfaction, this.#network)
);
}
}
expand(): {
expandedExpression?: string;
miniscript?: string;
expandedMiniscript?: string;
expansionMap?: ExpansionMap;
} {
return {
...(this.#expandedExpression !== undefined
? { expandedExpression: this.#expandedExpression }
: {}),
...(this.#miniscript !== undefined
? { miniscript: this.#miniscript }
: {}),
...(this.#expandedMiniscript !== undefined
? { expandedMiniscript: this.#expandedMiniscript }
: {}),
...(this.#expansionMap !== undefined
? { expansionMap: this.#expansionMap }
: {})
};
}
}
return { Descriptor, parseKeyExpression, ECPair, BIP32 };
}