@bitcoinerlab/descriptors 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/descriptors.d.ts +64 -0
- package/dist/descriptors.js +270 -0
- package/dist/psbt.js +3 -26
- package/package.json +6 -3
package/dist/descriptors.d.ts
CHANGED
|
@@ -115,8 +115,40 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
|
|
|
115
115
|
getNetwork(): Network;
|
|
116
116
|
/**
|
|
117
117
|
* Whether this `Output` is Segwit.
|
|
118
|
+
*
|
|
119
|
+
* *NOTE:* When the descriptor in an input is `addr(address)`, it is assumed
|
|
120
|
+
* that any `addr(SH_TYPE_ADDRESS)` is in fact a Segwit `SH_WPKH`
|
|
121
|
+
* (Script Hash-Witness Public Key Hash).
|
|
122
|
+
* For inputs using arbitrary scripts (not standard addresses),
|
|
123
|
+
* use a descriptor in the format `sh(MINISCRIPT)`.
|
|
124
|
+
*
|
|
118
125
|
*/
|
|
119
126
|
isSegwit(): boolean | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Returns the tuple: `{ isPKH: boolean; isWPKH: boolean; isSH: boolean; }`
|
|
129
|
+
* for this Output.
|
|
130
|
+
*/
|
|
131
|
+
guessOutput(): {
|
|
132
|
+
isPKH: boolean;
|
|
133
|
+
isWPKH: boolean;
|
|
134
|
+
isSH: boolean;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Computes the Weight Unit contributions of this Output as if it were the
|
|
138
|
+
* input in a tx.
|
|
139
|
+
*
|
|
140
|
+
* *NOTE:* When the descriptor in an input is `addr(address)`, it is assumed
|
|
141
|
+
* that any `addr(SH_TYPE_ADDRESS)` is in fact a Segwit `SH_WPKH`
|
|
142
|
+
* (Script Hash-Witness Public Key Hash).
|
|
143
|
+
* For inputs using arbitrary scripts (not standard addresses),
|
|
144
|
+
* use a descriptor in the format `sh(MINISCRIPT)`.
|
|
145
|
+
*/
|
|
146
|
+
inputWeight(isSegwitTx: boolean, signatures: PartialSig[] | 'DANGEROUSLY_USE_FAKE_SIGNATURES'): number;
|
|
147
|
+
/**
|
|
148
|
+
* Computes the Weight Unit contributions of this Output as if it were the
|
|
149
|
+
* output in a tx.
|
|
150
|
+
*/
|
|
151
|
+
outputWeight(): number;
|
|
120
152
|
/** @deprecated - Use updatePsbtAsInput instead
|
|
121
153
|
* @hidden
|
|
122
154
|
*/
|
|
@@ -365,8 +397,40 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
|
|
|
365
397
|
getNetwork(): Network;
|
|
366
398
|
/**
|
|
367
399
|
* Whether this `Output` is Segwit.
|
|
400
|
+
*
|
|
401
|
+
* *NOTE:* When the descriptor in an input is `addr(address)`, it is assumed
|
|
402
|
+
* that any `addr(SH_TYPE_ADDRESS)` is in fact a Segwit `SH_WPKH`
|
|
403
|
+
* (Script Hash-Witness Public Key Hash).
|
|
404
|
+
* For inputs using arbitrary scripts (not standard addresses),
|
|
405
|
+
* use a descriptor in the format `sh(MINISCRIPT)`.
|
|
406
|
+
*
|
|
368
407
|
*/
|
|
369
408
|
isSegwit(): boolean | undefined;
|
|
409
|
+
/**
|
|
410
|
+
* Returns the tuple: `{ isPKH: boolean; isWPKH: boolean; isSH: boolean; }`
|
|
411
|
+
* for this Output.
|
|
412
|
+
*/
|
|
413
|
+
guessOutput(): {
|
|
414
|
+
isPKH: boolean;
|
|
415
|
+
isWPKH: boolean;
|
|
416
|
+
isSH: boolean;
|
|
417
|
+
};
|
|
418
|
+
/**
|
|
419
|
+
* Computes the Weight Unit contributions of this Output as if it were the
|
|
420
|
+
* input in a tx.
|
|
421
|
+
*
|
|
422
|
+
* *NOTE:* When the descriptor in an input is `addr(address)`, it is assumed
|
|
423
|
+
* that any `addr(SH_TYPE_ADDRESS)` is in fact a Segwit `SH_WPKH`
|
|
424
|
+
* (Script Hash-Witness Public Key Hash).
|
|
425
|
+
* For inputs using arbitrary scripts (not standard addresses),
|
|
426
|
+
* use a descriptor in the format `sh(MINISCRIPT)`.
|
|
427
|
+
*/
|
|
428
|
+
inputWeight(isSegwitTx: boolean, signatures: PartialSig[] | 'DANGEROUSLY_USE_FAKE_SIGNATURES'): number;
|
|
429
|
+
/**
|
|
430
|
+
* Computes the Weight Unit contributions of this Output as if it were the
|
|
431
|
+
* output in a tx.
|
|
432
|
+
*/
|
|
433
|
+
outputWeight(): number;
|
|
370
434
|
/** @deprecated - Use updatePsbtAsInput instead
|
|
371
435
|
* @hidden
|
|
372
436
|
*/
|
package/dist/descriptors.js
CHANGED
|
@@ -35,9 +35,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
35
35
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
36
36
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
37
37
|
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
38
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
42
|
exports.DescriptorsFactory = void 0;
|
|
43
|
+
const lodash_memoize_1 = __importDefault(require("lodash.memoize"));
|
|
40
44
|
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
45
|
+
const varuint_bitcoin_1 = require("varuint-bitcoin");
|
|
41
46
|
const { p2sh, p2wpkh, p2pkh, p2pk, p2wsh, p2tr } = bitcoinjs_lib_1.payments;
|
|
42
47
|
const bip32_1 = require("bip32");
|
|
43
48
|
const ecpair_1 = require("ecpair");
|
|
@@ -57,6 +62,28 @@ function countNonPushOnlyOPs(script) {
|
|
|
57
62
|
throw new Error(`Error: cound not decompile ${script}`);
|
|
58
63
|
return decompile.filter(op => typeof op === 'number' && op > bitcoinjs_lib_1.script.OPS['OP_16']).length;
|
|
59
64
|
}
|
|
65
|
+
function vectorSize(someVector) {
|
|
66
|
+
const length = someVector.length;
|
|
67
|
+
return ((0, varuint_bitcoin_1.encodingLength)(length) +
|
|
68
|
+
someVector.reduce((sum, witness) => {
|
|
69
|
+
return sum + varSliceSize(witness);
|
|
70
|
+
}, 0));
|
|
71
|
+
}
|
|
72
|
+
function varSliceSize(someScript) {
|
|
73
|
+
const length = someScript.length;
|
|
74
|
+
return (0, varuint_bitcoin_1.encodingLength)(length) + length;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* This function will typically return 73; since it assumes a signature size of
|
|
78
|
+
* 72 bytes (this is the max size of a DER encoded signature) and it adds 1
|
|
79
|
+
* extra byte for encoding its length
|
|
80
|
+
*/
|
|
81
|
+
function signatureSize(signature) {
|
|
82
|
+
const length = signature === 'DANGEROUSLY_USE_FAKE_SIGNATURES'
|
|
83
|
+
? 72
|
|
84
|
+
: signature.signature.length;
|
|
85
|
+
return (0, varuint_bitcoin_1.encodingLength)(length) + length;
|
|
86
|
+
}
|
|
60
87
|
/*
|
|
61
88
|
* Returns a bare descriptor without checksum and particularized for a certain
|
|
62
89
|
* index (if desc was a range descriptor)
|
|
@@ -184,22 +211,28 @@ function DescriptorsFactory(ecc) {
|
|
|
184
211
|
}
|
|
185
212
|
try {
|
|
186
213
|
payment = p2pkh({ output, network });
|
|
214
|
+
isSegwit = false;
|
|
187
215
|
}
|
|
188
216
|
catch (e) { }
|
|
189
217
|
try {
|
|
190
218
|
payment = p2sh({ output, network });
|
|
219
|
+
// It assumes that an addr(SH_ADDRESS) is always a add(SH_WPKH) address
|
|
220
|
+
isSegwit = true;
|
|
191
221
|
}
|
|
192
222
|
catch (e) { }
|
|
193
223
|
try {
|
|
194
224
|
payment = p2wpkh({ output, network });
|
|
225
|
+
isSegwit = true;
|
|
195
226
|
}
|
|
196
227
|
catch (e) { }
|
|
197
228
|
try {
|
|
198
229
|
payment = p2wsh({ output, network });
|
|
230
|
+
isSegwit = true;
|
|
199
231
|
}
|
|
200
232
|
catch (e) { }
|
|
201
233
|
try {
|
|
202
234
|
payment = p2tr({ output, network });
|
|
235
|
+
isSegwit = true;
|
|
203
236
|
}
|
|
204
237
|
catch (e) { }
|
|
205
238
|
if (!payment) {
|
|
@@ -480,6 +513,25 @@ function DescriptorsFactory(ecc) {
|
|
|
480
513
|
__classPrivateFieldSet(this, _Output_signersPubKeys, [this.getScriptPubKey()], "f");
|
|
481
514
|
}
|
|
482
515
|
}
|
|
516
|
+
this.getSequence = (0, lodash_memoize_1.default)(this.getSequence);
|
|
517
|
+
this.getLockTime = (0, lodash_memoize_1.default)(this.getLockTime);
|
|
518
|
+
const getSignaturesKey = (signatures) => signatures === 'DANGEROUSLY_USE_FAKE_SIGNATURES'
|
|
519
|
+
? signatures
|
|
520
|
+
: signatures
|
|
521
|
+
.map(s => `${s.pubkey.toString('hex')}-${s.signature.toString('hex')}`)
|
|
522
|
+
.join('|');
|
|
523
|
+
this.getScriptSatisfaction = (0, lodash_memoize_1.default)(this.getScriptSatisfaction,
|
|
524
|
+
// resolver function:
|
|
525
|
+
getSignaturesKey);
|
|
526
|
+
this.guessOutput = (0, lodash_memoize_1.default)(this.guessOutput);
|
|
527
|
+
this.inputWeight = (0, lodash_memoize_1.default)(this.inputWeight,
|
|
528
|
+
// resolver function:
|
|
529
|
+
(isSegwitTx, signatures) => {
|
|
530
|
+
const segwitKey = isSegwitTx ? 'segwit' : 'non-segwit';
|
|
531
|
+
const signaturesKey = getSignaturesKey(signatures);
|
|
532
|
+
return `${segwitKey}-${signaturesKey}`;
|
|
533
|
+
});
|
|
534
|
+
this.outputWeight = (0, lodash_memoize_1.default)(this.outputWeight);
|
|
483
535
|
}
|
|
484
536
|
/**
|
|
485
537
|
* Creates and returns an instance of bitcoinjs-lib
|
|
@@ -607,10 +659,228 @@ function DescriptorsFactory(ecc) {
|
|
|
607
659
|
}
|
|
608
660
|
/**
|
|
609
661
|
* Whether this `Output` is Segwit.
|
|
662
|
+
*
|
|
663
|
+
* *NOTE:* When the descriptor in an input is `addr(address)`, it is assumed
|
|
664
|
+
* that any `addr(SH_TYPE_ADDRESS)` is in fact a Segwit `SH_WPKH`
|
|
665
|
+
* (Script Hash-Witness Public Key Hash).
|
|
666
|
+
* For inputs using arbitrary scripts (not standard addresses),
|
|
667
|
+
* use a descriptor in the format `sh(MINISCRIPT)`.
|
|
668
|
+
*
|
|
610
669
|
*/
|
|
611
670
|
isSegwit() {
|
|
612
671
|
return __classPrivateFieldGet(this, _Output_isSegwit, "f");
|
|
613
672
|
}
|
|
673
|
+
/**
|
|
674
|
+
* Returns the tuple: `{ isPKH: boolean; isWPKH: boolean; isSH: boolean; }`
|
|
675
|
+
* for this Output.
|
|
676
|
+
*/
|
|
677
|
+
guessOutput() {
|
|
678
|
+
function guessSH(output) {
|
|
679
|
+
try {
|
|
680
|
+
bitcoinjs_lib_1.payments.p2sh({ output });
|
|
681
|
+
return true;
|
|
682
|
+
}
|
|
683
|
+
catch (err) {
|
|
684
|
+
return false;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
function guessWPKH(output) {
|
|
688
|
+
try {
|
|
689
|
+
bitcoinjs_lib_1.payments.p2wpkh({ output });
|
|
690
|
+
return true;
|
|
691
|
+
}
|
|
692
|
+
catch (err) {
|
|
693
|
+
return false;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
function guessPKH(output) {
|
|
697
|
+
try {
|
|
698
|
+
bitcoinjs_lib_1.payments.p2pkh({ output });
|
|
699
|
+
return true;
|
|
700
|
+
}
|
|
701
|
+
catch (err) {
|
|
702
|
+
return false;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const isPKH = guessPKH(this.getScriptPubKey());
|
|
706
|
+
const isWPKH = guessWPKH(this.getScriptPubKey());
|
|
707
|
+
const isSH = guessSH(this.getScriptPubKey());
|
|
708
|
+
if ([isPKH, isWPKH, isSH].filter(Boolean).length > 1)
|
|
709
|
+
throw new Error('Cannot have multiple output types.');
|
|
710
|
+
return { isPKH, isWPKH, isSH };
|
|
711
|
+
}
|
|
712
|
+
// References for inputWeight & outputWeight:
|
|
713
|
+
// https://gist.github.com/junderw/b43af3253ea5865ed52cb51c200ac19c
|
|
714
|
+
// https://bitcoinops.org/en/tools/calc-size/
|
|
715
|
+
// Look for byteLength: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/transaction.ts
|
|
716
|
+
// https://github.com/bitcoinjs/coinselect/blob/master/utils.js
|
|
717
|
+
/**
|
|
718
|
+
* Computes the Weight Unit contributions of this Output as if it were the
|
|
719
|
+
* input in a tx.
|
|
720
|
+
*
|
|
721
|
+
* *NOTE:* When the descriptor in an input is `addr(address)`, it is assumed
|
|
722
|
+
* that any `addr(SH_TYPE_ADDRESS)` is in fact a Segwit `SH_WPKH`
|
|
723
|
+
* (Script Hash-Witness Public Key Hash).
|
|
724
|
+
* For inputs using arbitrary scripts (not standard addresses),
|
|
725
|
+
* use a descriptor in the format `sh(MINISCRIPT)`.
|
|
726
|
+
*/
|
|
727
|
+
inputWeight(
|
|
728
|
+
/**
|
|
729
|
+
* Indicates if the transaction is a Segwit transaction.
|
|
730
|
+
* If a transaction isSegwitTx, a single byte is then also required for
|
|
731
|
+
* non-witness inputs to encode the length of the empty witness stack:
|
|
732
|
+
* encodeLength(0) + 0 = 1
|
|
733
|
+
* Read more:
|
|
734
|
+
* https://gist.github.com/junderw/b43af3253ea5865ed52cb51c200ac19c?permalink_comment_id=4760512#gistcomment-4760512
|
|
735
|
+
*/
|
|
736
|
+
isSegwitTx,
|
|
737
|
+
/*
|
|
738
|
+
* Array of `PartialSig`. Each `PartialSig` includes
|
|
739
|
+
* a public key and its corresponding signature. This parameter
|
|
740
|
+
* enables the accurate calculation of signature sizes.
|
|
741
|
+
* Pass 'DANGEROUSLY_USE_FAKE_SIGNATURES' to assume 72 bytes in length.
|
|
742
|
+
* Mainly used for testing.
|
|
743
|
+
*/
|
|
744
|
+
signatures) {
|
|
745
|
+
if (this.isSegwit() && !isSegwitTx)
|
|
746
|
+
throw new Error(`a tx is segwit if at least one input is segwit`);
|
|
747
|
+
const errorMsg = 'Input type not implemented. Currently supported: pkh(KEY), wpkh(KEY), \
|
|
748
|
+
sh(wpkh(KEY)), sh(wsh(MINISCRIPT)), sh(MINISCRIPT), wsh(MINISCRIPT), \
|
|
749
|
+
addr(PKH_ADDRESS), addr(WPKH_ADDRESS), addr(SH_WPKH_ADDRESS).';
|
|
750
|
+
//expand any miniscript-based descriptor. If not miniscript-based, then it's
|
|
751
|
+
//an addr() descriptor. For those, we can only guess their type.
|
|
752
|
+
const expansion = this.expand().expandedExpression;
|
|
753
|
+
const { isPKH, isWPKH, isSH } = this.guessOutput();
|
|
754
|
+
if (!expansion && !isPKH && !isWPKH && !isSH)
|
|
755
|
+
throw new Error(errorMsg);
|
|
756
|
+
const firstSignature = signatures && typeof signatures[0] === 'object'
|
|
757
|
+
? signatures[0]
|
|
758
|
+
: 'DANGEROUSLY_USE_FAKE_SIGNATURES';
|
|
759
|
+
if (expansion ? expansion.startsWith('pkh(') : isPKH) {
|
|
760
|
+
return (
|
|
761
|
+
// Non-segwit: (txid:32) + (vout:4) + (sequence:4) + (script_len:1) + (sig:73) + (pubkey:34)
|
|
762
|
+
(32 + 4 + 4 + 1 + signatureSize(firstSignature) + 34) * 4 +
|
|
763
|
+
//Segwit:
|
|
764
|
+
(isSegwitTx ? 1 : 0));
|
|
765
|
+
}
|
|
766
|
+
else if (expansion ? expansion.startsWith('wpkh(') : isWPKH) {
|
|
767
|
+
if (!isSegwitTx)
|
|
768
|
+
throw new Error('Should be SegwitTx');
|
|
769
|
+
return (
|
|
770
|
+
// Non-segwit: (txid:32) + (vout:4) + (sequence:4) + (script_len:1)
|
|
771
|
+
41 * 4 +
|
|
772
|
+
// Segwit: (push_count:1) + (sig:73) + (pubkey:34)
|
|
773
|
+
(1 + signatureSize(firstSignature) + 34));
|
|
774
|
+
}
|
|
775
|
+
else if (expansion ? expansion.startsWith('sh(wpkh(') : isSH) {
|
|
776
|
+
if (!isSegwitTx)
|
|
777
|
+
throw new Error('Should be SegwitTx');
|
|
778
|
+
return (
|
|
779
|
+
// Non-segwit: (txid:32) + (vout:4) + (sequence:4) + (script_len:1) + (p2wpkh:23)
|
|
780
|
+
// -> p2wpkh_script: OP_0 OP_PUSH20 <public_key_hash>
|
|
781
|
+
// -> p2wpkh: (script_len:1) + (script:22)
|
|
782
|
+
64 * 4 +
|
|
783
|
+
// Segwit: (push_count:1) + (sig:73) + (pubkey:34)
|
|
784
|
+
(1 + signatureSize(firstSignature) + 34));
|
|
785
|
+
}
|
|
786
|
+
else if (expansion?.startsWith('sh(wsh(')) {
|
|
787
|
+
if (!isSegwitTx)
|
|
788
|
+
throw new Error('Should be SegwitTx');
|
|
789
|
+
const witnessScript = this.getWitnessScript();
|
|
790
|
+
if (!witnessScript)
|
|
791
|
+
throw new Error('sh(wsh) must provide witnessScript');
|
|
792
|
+
const payment = bitcoinjs_lib_1.payments.p2sh({
|
|
793
|
+
redeem: bitcoinjs_lib_1.payments.p2wsh({
|
|
794
|
+
redeem: {
|
|
795
|
+
input: this.getScriptSatisfaction(signatures || 'DANGEROUSLY_USE_FAKE_SIGNATURES'),
|
|
796
|
+
output: witnessScript
|
|
797
|
+
}
|
|
798
|
+
})
|
|
799
|
+
});
|
|
800
|
+
if (!payment || !payment.input || !payment.witness)
|
|
801
|
+
throw new Error('Could not create payment');
|
|
802
|
+
return (
|
|
803
|
+
//Non-segwit
|
|
804
|
+
4 * (40 + varSliceSize(payment.input)) +
|
|
805
|
+
//Segwit
|
|
806
|
+
vectorSize(payment.witness));
|
|
807
|
+
}
|
|
808
|
+
else if (expansion?.startsWith('sh(')) {
|
|
809
|
+
const redeemScript = this.getRedeemScript();
|
|
810
|
+
if (!redeemScript)
|
|
811
|
+
throw new Error('sh() must provide redeemScript');
|
|
812
|
+
const payment = bitcoinjs_lib_1.payments.p2sh({
|
|
813
|
+
redeem: {
|
|
814
|
+
input: this.getScriptSatisfaction(signatures || 'DANGEROUSLY_USE_FAKE_SIGNATURES'),
|
|
815
|
+
output: redeemScript
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
if (!payment || !payment.input)
|
|
819
|
+
throw new Error('Could not create payment');
|
|
820
|
+
if (payment.witness?.length)
|
|
821
|
+
throw new Error('A legacy p2sh payment should not cointain a witness');
|
|
822
|
+
return (
|
|
823
|
+
//Non-segwit
|
|
824
|
+
4 * (40 + varSliceSize(payment.input)) +
|
|
825
|
+
//Segwit:
|
|
826
|
+
(isSegwitTx ? 1 : 0));
|
|
827
|
+
}
|
|
828
|
+
else if (expansion?.startsWith('wsh(')) {
|
|
829
|
+
const witnessScript = this.getWitnessScript();
|
|
830
|
+
if (!witnessScript)
|
|
831
|
+
throw new Error('wsh must provide witnessScript');
|
|
832
|
+
const payment = bitcoinjs_lib_1.payments.p2wsh({
|
|
833
|
+
redeem: {
|
|
834
|
+
input: this.getScriptSatisfaction(signatures || 'DANGEROUSLY_USE_FAKE_SIGNATURES'),
|
|
835
|
+
output: witnessScript
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
if (!payment || !payment.input || !payment.witness)
|
|
839
|
+
throw new Error('Could not create payment');
|
|
840
|
+
return (
|
|
841
|
+
//Non-segwit
|
|
842
|
+
4 * (40 + varSliceSize(payment.input)) +
|
|
843
|
+
//Segwit
|
|
844
|
+
vectorSize(payment.witness));
|
|
845
|
+
}
|
|
846
|
+
else {
|
|
847
|
+
throw new Error(errorMsg);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Computes the Weight Unit contributions of this Output as if it were the
|
|
852
|
+
* output in a tx.
|
|
853
|
+
*/
|
|
854
|
+
outputWeight() {
|
|
855
|
+
const errorMsg = 'Output type not implemented. Currently supported: pkh(KEY), wpkh(KEY), \
|
|
856
|
+
sh(ANYTHING), wsh(ANYTHING), addr(PKH_ADDRESS), addr(WPKH_ADDRESS), \
|
|
857
|
+
addr(SH_WPKH_ADDRESS)';
|
|
858
|
+
//expand any miniscript-based descriptor. If not miniscript-based, then it's
|
|
859
|
+
//an addr() descriptor. For those, we can only guess their type.
|
|
860
|
+
const expansion = this.expand().expandedExpression;
|
|
861
|
+
const { isPKH, isWPKH, isSH } = this.guessOutput();
|
|
862
|
+
if (!expansion && !isPKH && !isWPKH && !isSH)
|
|
863
|
+
throw new Error(errorMsg);
|
|
864
|
+
if (expansion ? expansion.startsWith('pkh(') : isPKH) {
|
|
865
|
+
// (p2pkh:26) + (amount:8)
|
|
866
|
+
return 34 * 4;
|
|
867
|
+
}
|
|
868
|
+
else if (expansion ? expansion.startsWith('wpkh(') : isWPKH) {
|
|
869
|
+
// (p2wpkh:23) + (amount:8)
|
|
870
|
+
return 31 * 4;
|
|
871
|
+
}
|
|
872
|
+
else if (expansion ? expansion.startsWith('sh(') : isSH) {
|
|
873
|
+
// (p2sh:24) + (amount:8)
|
|
874
|
+
return 32 * 4;
|
|
875
|
+
}
|
|
876
|
+
else if (expansion?.startsWith('wsh(')) {
|
|
877
|
+
// (p2wsh:35) + (amount:8)
|
|
878
|
+
return 43 * 4;
|
|
879
|
+
}
|
|
880
|
+
else {
|
|
881
|
+
throw new Error(errorMsg);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
614
884
|
/** @deprecated - Use updatePsbtAsInput instead
|
|
615
885
|
* @hidden
|
|
616
886
|
*/
|
package/dist/psbt.js
CHANGED
|
@@ -1,33 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) 2023 Jose-Luis Landabaso - https://bitcoinerlab.com
|
|
3
3
|
// Distributed under the MIT software license
|
|
4
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
-
if (k2 === undefined) k2 = k;
|
|
6
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(o, k2, desc);
|
|
11
|
-
}) : (function(o, m, k, k2) {
|
|
12
|
-
if (k2 === undefined) k2 = k;
|
|
13
|
-
o[k2] = m[k];
|
|
14
|
-
}));
|
|
15
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
-
}) : function(o, v) {
|
|
18
|
-
o["default"] = v;
|
|
19
|
-
});
|
|
20
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
-
if (mod && mod.__esModule) return mod;
|
|
22
|
-
var result = {};
|
|
23
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
-
__setModuleDefault(result, mod);
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
5
|
exports.updatePsbt = exports.finalScriptsFuncFactory = void 0;
|
|
29
6
|
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
30
|
-
const
|
|
7
|
+
const varuint_bitcoin_1 = require("varuint-bitcoin");
|
|
31
8
|
function reverseBuffer(buffer) {
|
|
32
9
|
if (buffer.length < 1)
|
|
33
10
|
return buffer;
|
|
@@ -48,9 +25,9 @@ function witnessStackToScriptWitness(witness) {
|
|
|
48
25
|
}
|
|
49
26
|
function writeVarInt(i) {
|
|
50
27
|
const currentLen = buffer.length;
|
|
51
|
-
const varintLen =
|
|
28
|
+
const varintLen = (0, varuint_bitcoin_1.encodingLength)(i);
|
|
52
29
|
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
|
|
53
|
-
|
|
30
|
+
(0, varuint_bitcoin_1.encode)(i, buffer, currentLen);
|
|
54
31
|
}
|
|
55
32
|
function writeVarSlice(slice) {
|
|
56
33
|
writeVarInt(slice.length);
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@bitcoinerlab/descriptors",
|
|
3
3
|
"description": "This library parses and creates Bitcoin Miniscript Descriptors and generates Partially Signed Bitcoin Transactions (PSBTs). It provides PSBT finalizers and signers for single-signature, BIP32 and Hardware Wallets.",
|
|
4
4
|
"homepage": "https://github.com/bitcoinerlab/descriptors",
|
|
5
|
-
"version": "2.0
|
|
5
|
+
"version": "2.1.0",
|
|
6
6
|
"author": "Jose-Luis Landabaso",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"repository": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"docs": "typedoc --options ./node_modules/@bitcoinerlab/configs/typedoc.json",
|
|
33
33
|
"build:src": "tsc --project ./node_modules/@bitcoinerlab/configs/tsconfig.src.json",
|
|
34
34
|
"build:fixtures": "node test/tools/generateBitcoinCoreFixtures.js -i test/fixtures/descriptor_tests.cpp | npx prettier --parser typescript > test/fixtures/bitcoinCore.ts",
|
|
35
|
-
"build:test": "npm run build:fixtures && tsc --project ./node_modules/@bitcoinerlab/configs/tsconfig.test.json",
|
|
35
|
+
"build:test": "npm run build:fixtures && tsc --project ./node_modules/@bitcoinerlab/configs/tsconfig.test.json --resolveJsonModule",
|
|
36
36
|
"build": "npm run build:src && npm run build:test",
|
|
37
37
|
"lint": "eslint --ignore-path .gitignore --ext .ts src/ test/",
|
|
38
38
|
"ensureTester": "./node_modules/@bitcoinerlab/configs/scripts/ensureTester.sh",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@bitcoinerlab/configs": "github:bitcoinerlab/configs",
|
|
60
60
|
"@ledgerhq/hw-transport-node-hid": "^6.27.12",
|
|
61
|
+
"@types/lodash.memoize": "^4.1.9",
|
|
61
62
|
"bip39": "^3.0.4",
|
|
62
63
|
"bip65": "^1.0.3",
|
|
63
64
|
"bip68": "^1.0.4",
|
|
@@ -70,6 +71,8 @@
|
|
|
70
71
|
"@bitcoinerlab/secp256k1": "^1.0.5",
|
|
71
72
|
"bip32": "^4.0.0",
|
|
72
73
|
"bitcoinjs-lib": "^6.1.3",
|
|
73
|
-
"ecpair": "^2.1.0"
|
|
74
|
+
"ecpair": "^2.1.0",
|
|
75
|
+
"lodash.memoize": "^4.1.2",
|
|
76
|
+
"varuint-bitcoin": "^1.1.2"
|
|
74
77
|
}
|
|
75
78
|
}
|