@bitcoinerlab/descriptors 3.0.6 → 3.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.
Files changed (44) hide show
  1. package/README.md +9 -487
  2. package/index.d.ts +13 -0
  3. package/index.js +16 -0
  4. package/package.json +22 -53
  5. package/dist/applyPR2137.d.ts +0 -2
  6. package/dist/applyPR2137.js +0 -153
  7. package/dist/bitcoinjs-lib-internals.d.ts +0 -10
  8. package/dist/bitcoinjs-lib-internals.js +0 -60
  9. package/dist/checksum.d.ts +0 -6
  10. package/dist/checksum.js +0 -58
  11. package/dist/descriptors.d.ts +0 -433
  12. package/dist/descriptors.js +0 -1743
  13. package/dist/index.d.ts +0 -21
  14. package/dist/index.js +0 -85
  15. package/dist/keyExpressions.d.ts +0 -83
  16. package/dist/keyExpressions.js +0 -247
  17. package/dist/ledger.d.ts +0 -167
  18. package/dist/ledger.js +0 -580
  19. package/dist/miniscript.d.ts +0 -123
  20. package/dist/miniscript.js +0 -305
  21. package/dist/multipath.d.ts +0 -13
  22. package/dist/multipath.js +0 -76
  23. package/dist/networkUtils.d.ts +0 -3
  24. package/dist/networkUtils.js +0 -16
  25. package/dist/parseUtils.d.ts +0 -7
  26. package/dist/parseUtils.js +0 -46
  27. package/dist/psbt.d.ts +0 -44
  28. package/dist/psbt.js +0 -193
  29. package/dist/re.d.ts +0 -31
  30. package/dist/re.js +0 -79
  31. package/dist/resourceLimits.d.ts +0 -25
  32. package/dist/resourceLimits.js +0 -89
  33. package/dist/scriptExpressions.d.ts +0 -95
  34. package/dist/scriptExpressions.js +0 -89
  35. package/dist/signers.d.ts +0 -84
  36. package/dist/signers.js +0 -215
  37. package/dist/stackResourceLimits.d.ts +0 -17
  38. package/dist/stackResourceLimits.js +0 -35
  39. package/dist/tapMiniscript.d.ts +0 -220
  40. package/dist/tapMiniscript.js +0 -510
  41. package/dist/tapTree.d.ts +0 -86
  42. package/dist/tapTree.js +0 -166
  43. package/dist/types.d.ts +0 -238
  44. package/dist/types.js +0 -4
@@ -1,153 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyPR2137 = void 0;
4
- //While this PR is not merged: https://github.com/bitcoinjs/bitcoinjs-lib/pull/2137
5
- //The Async functions have not been "fixed"
6
- //Note that a further fix (look for FIX BITCOINERLAB) was done
7
- const bitcoinjs_lib_1 = require("bitcoinjs-lib");
8
- const bip174_1 = require("bip174");
9
- const bitcoinjs_lib_internals_1 = require("./bitcoinjs-lib-internals");
10
- const uint8array_tools_1 = require("uint8array-tools");
11
- const toXOnly = (pubKey) => pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
12
- function range(n) {
13
- return [...Array(n).keys()];
14
- }
15
- function tapBranchHash(a, b) {
16
- return bitcoinjs_lib_1.crypto.taggedHash('TapBranch', (0, uint8array_tools_1.concat)([a, b]));
17
- }
18
- function calculateScriptTreeMerkleRoot(leafHashes) {
19
- if (!leafHashes || leafHashes.length === 0) {
20
- return undefined;
21
- }
22
- const leafHashCopies = leafHashes.map(leafHash => Uint8Array.from(leafHash));
23
- // sort the leaf nodes
24
- leafHashCopies.sort(uint8array_tools_1.compare);
25
- // create the initial hash node
26
- let currentLevel = leafHashCopies;
27
- // build Merkle Tree
28
- while (currentLevel.length > 1) {
29
- const nextLevel = [];
30
- for (let i = 0; i < currentLevel.length; i += 2) {
31
- const left = currentLevel[i];
32
- if (!left)
33
- throw new Error('Invalid tapleaf hash tree level');
34
- const right = i + 1 < currentLevel.length ? (currentLevel[i + 1] ?? left) : left;
35
- nextLevel.push(i + 1 < currentLevel.length ? tapBranchHash(left, right) : left);
36
- }
37
- currentLevel = nextLevel;
38
- }
39
- return currentLevel[0];
40
- }
41
- function getTweakSignersFromHD(inputIndex, inputs, hdKeyPair) {
42
- const input = (0, bip174_1.checkForInput)(inputs, inputIndex);
43
- if (!input.tapBip32Derivation || input.tapBip32Derivation.length === 0) {
44
- throw new Error('Need tapBip32Derivation to sign with HD');
45
- }
46
- const myDerivations = input.tapBip32Derivation
47
- .map(bipDv => {
48
- if ((0, uint8array_tools_1.compare)(bipDv.masterFingerprint, hdKeyPair.fingerprint) === 0) {
49
- return bipDv;
50
- }
51
- else {
52
- return;
53
- }
54
- })
55
- .filter(v => !!v);
56
- if (myDerivations.length === 0) {
57
- throw new Error('Need one tapBip32Derivation masterFingerprint to match the HDSigner fingerprint');
58
- }
59
- const signers = myDerivations.map(bipDv => {
60
- const node = hdKeyPair.derivePath(bipDv.path);
61
- if ((0, uint8array_tools_1.compare)(bipDv.pubkey, toXOnly(node.publicKey)) !== 0) {
62
- throw new Error('pubkey did not match tapBip32Derivation');
63
- }
64
- //FIX BITCOINERLAB:
65
- //The 3 lines below detect script-path spends and disable key-path tweaking.
66
- //Reasoning:
67
- //- In Taproot, key-path spends require tweaking the internal key.
68
- //- Script-path spends MUST NOT tweak the key; signatures use the raw internal key.
69
- const input = inputs[inputIndex];
70
- if (!input)
71
- throw new Error('could not find the input');
72
- if (input.tapLeafScript && input.tapLeafScript.length > 0)
73
- return node;
74
- const h = calculateScriptTreeMerkleRoot(bipDv.leafHashes);
75
- const tweakValue = (0, bitcoinjs_lib_internals_1.tapTweakHash)(toXOnly(node.publicKey), h);
76
- return node.tweak(tweakValue);
77
- });
78
- return signers;
79
- }
80
- function getSignersFromHD(inputIndex, inputs, hdKeyPair) {
81
- const input = (0, bip174_1.checkForInput)(inputs, inputIndex);
82
- if ((0, bitcoinjs_lib_internals_1.isTaprootInput)(input)) {
83
- return getTweakSignersFromHD(inputIndex, inputs, hdKeyPair);
84
- }
85
- if (!input.bip32Derivation || input.bip32Derivation.length === 0) {
86
- throw new Error('Need bip32Derivation to sign with HD');
87
- }
88
- const myDerivations = input.bip32Derivation
89
- .map(bipDv => {
90
- if ((0, uint8array_tools_1.compare)(bipDv.masterFingerprint, hdKeyPair.fingerprint) === 0) {
91
- return bipDv;
92
- }
93
- else {
94
- return;
95
- }
96
- })
97
- .filter(v => !!v);
98
- if (myDerivations.length === 0) {
99
- throw new Error('Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint');
100
- }
101
- const signers = myDerivations.map(bipDv => {
102
- const node = hdKeyPair.derivePath(bipDv.path);
103
- if ((0, uint8array_tools_1.compare)(bipDv.pubkey, node.publicKey) !== 0) {
104
- throw new Error('pubkey did not match bip32Derivation');
105
- }
106
- return node;
107
- });
108
- return signers;
109
- }
110
- const applyPR2137 = (psbt) => {
111
- psbt.signInputHD = function signInputHD(inputIndex, hdKeyPair, sighashTypes) {
112
- if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
113
- throw new Error('Need HDSigner to sign input');
114
- }
115
- const signers = getSignersFromHD(inputIndex, this.data.inputs, hdKeyPair);
116
- const results = [];
117
- for (const signer of signers) {
118
- try {
119
- this.signInput(inputIndex, signer, sighashTypes);
120
- results.push(true);
121
- }
122
- catch (err) {
123
- void err;
124
- results.push(false);
125
- }
126
- }
127
- if (results.every(v => v === false)) {
128
- throw new Error('No inputs were signed');
129
- }
130
- return this;
131
- };
132
- psbt.signAllInputsHD = function signAllInputsHD(hdKeyPair, sighashTypes) {
133
- if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
134
- throw new Error('Need HDSigner to sign input');
135
- }
136
- const results = [];
137
- for (const i of range(psbt.data.inputs.length)) {
138
- try {
139
- psbt.signInputHD(i, hdKeyPair, sighashTypes);
140
- results.push(true);
141
- }
142
- catch (err) {
143
- void err;
144
- results.push(false);
145
- }
146
- }
147
- if (results.every(v => v === false)) {
148
- throw new Error('No inputs were signed');
149
- }
150
- return psbt;
151
- };
152
- };
153
- exports.applyPR2137 = applyPR2137;
@@ -1,10 +0,0 @@
1
- import type { PsbtInput } from 'bip174';
2
- type Tapleaf = {
3
- output: Uint8Array;
4
- version?: number;
5
- };
6
- export declare function tapleafHash(leaf: Tapleaf): Uint8Array;
7
- export declare function tapTweakHash(pubKey: Uint8Array, h?: Uint8Array): Uint8Array;
8
- export declare function witnessStackToScriptWitness(witness: Uint8Array[]): Uint8Array;
9
- export declare function isTaprootInput(input: PsbtInput | undefined): boolean;
10
- export {};
@@ -1,60 +0,0 @@
1
- "use strict";
2
- /*
3
- * Reimplements a small subset of bitcoinjs-lib internal helpers.
4
- * Keep this module free of deep imports (for example `bitcoinjs-lib/src/*`)
5
- * so it works consistently across Node.js, browser bundlers (including
6
- * CodeSandbox), and React Native/Metro.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.tapleafHash = tapleafHash;
10
- exports.tapTweakHash = tapTweakHash;
11
- exports.witnessStackToScriptWitness = witnessStackToScriptWitness;
12
- exports.isTaprootInput = isTaprootInput;
13
- const bitcoinjs_lib_1 = require("bitcoinjs-lib");
14
- const varuint_bitcoin_1 = require("varuint-bitcoin");
15
- const uint8array_tools_1 = require("uint8array-tools");
16
- const TAPROOT_LEAF_VERSION_TAPSCRIPT = 0xc0;
17
- const OP_1 = 0x51;
18
- const PUSH_DATA_32 = 0x20;
19
- function serializeScript(script) {
20
- const { buffer: encodedLength } = (0, varuint_bitcoin_1.encode)(script.length);
21
- return (0, uint8array_tools_1.concat)([encodedLength, script]);
22
- }
23
- function isP2TRScript(script) {
24
- return (script instanceof Uint8Array &&
25
- script.length === 34 &&
26
- script[0] === OP_1 &&
27
- script[1] === PUSH_DATA_32);
28
- }
29
- function tapleafHash(leaf) {
30
- const version = leaf.version || TAPROOT_LEAF_VERSION_TAPSCRIPT;
31
- return bitcoinjs_lib_1.crypto.taggedHash('TapLeaf', (0, uint8array_tools_1.concat)([Uint8Array.from([version]), serializeScript(leaf.output)]));
32
- }
33
- function tapTweakHash(pubKey, h) {
34
- return bitcoinjs_lib_1.crypto.taggedHash('TapTweak', (0, uint8array_tools_1.concat)(h ? [pubKey, h] : [pubKey]));
35
- }
36
- function witnessStackToScriptWitness(witness) {
37
- let buffer = new Uint8Array(0);
38
- const writeSlice = (slice) => {
39
- buffer = (0, uint8array_tools_1.concat)([buffer, slice]);
40
- };
41
- const writeVarInt = (value) => {
42
- const { buffer: encoded } = (0, varuint_bitcoin_1.encode)(value);
43
- writeSlice(encoded);
44
- };
45
- const writeVarSlice = (slice) => {
46
- writeVarInt(slice.length);
47
- writeSlice(slice);
48
- };
49
- writeVarInt(witness.length);
50
- witness.forEach(writeVarSlice);
51
- return buffer;
52
- }
53
- function isTaprootInput(input) {
54
- return (!!input &&
55
- !!(input.tapInternalKey ||
56
- input.tapMerkleRoot ||
57
- (input.tapLeafScript && input.tapLeafScript.length > 0) ||
58
- (input.tapBip32Derivation && input.tapBip32Derivation.length > 0) ||
59
- isP2TRScript(input.witnessUtxo?.script)));
60
- }
@@ -1,6 +0,0 @@
1
- export declare const CHECKSUM_CHARSET: string;
2
- /**
3
- * Implements the Bitcoin descriptor's checksum algorithm described in
4
- * {@link https://github.com/bitcoin/bitcoin/blob/master/src/script/descriptor.cpp}
5
- */
6
- export declare const DescriptorChecksum: (span: string) => string;
package/dist/checksum.js DELETED
@@ -1,58 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DescriptorChecksum = exports.CHECKSUM_CHARSET = void 0;
4
- // Converted to Javascript by Jose-Luis Landabaso, 2023 - https://bitcoinerlab.com
5
- // Source: https://github.com/bitcoin/bitcoin/blob/master/src/script/descriptor.cpp
6
- // Distributed under the MIT software license
7
- const PolyMod = (c, val) => {
8
- const c0 = c >> 35n;
9
- c = ((c & 0x7ffffffffn) << 5n) ^ val;
10
- if (c0 & 1n)
11
- c ^= 0xf5dee51989n;
12
- if (c0 & 2n)
13
- c ^= 0xa9fdca3312n;
14
- if (c0 & 4n)
15
- c ^= 0x1bab10e32dn;
16
- if (c0 & 8n)
17
- c ^= 0x3706b1677an;
18
- if (c0 & 16n)
19
- c ^= 0x644d626ffdn;
20
- return c;
21
- };
22
- exports.CHECKSUM_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
23
- /**
24
- * Implements the Bitcoin descriptor's checksum algorithm described in
25
- * {@link https://github.com/bitcoin/bitcoin/blob/master/src/script/descriptor.cpp}
26
- */
27
- const DescriptorChecksum = (span) => {
28
- const INPUT_CHARSET = '0123456789()[],\'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#"\\ ';
29
- let c = 1n;
30
- let cls = 0n;
31
- let clscount = 0n;
32
- for (const ch of span) {
33
- const pos = BigInt(INPUT_CHARSET.indexOf(ch));
34
- if (pos === -1n)
35
- return '';
36
- c = PolyMod(c, pos & 31n);
37
- cls = cls * 3n + (pos >> 5n);
38
- if (++clscount === 3n) {
39
- c = PolyMod(c, cls);
40
- cls = 0n;
41
- clscount = 0n;
42
- }
43
- }
44
- if (clscount > 0n)
45
- c = PolyMod(c, cls);
46
- for (let j = 0; j < 8; ++j)
47
- c = PolyMod(c, 0n);
48
- c ^= 1n;
49
- let ret = '';
50
- for (let j = 0; j < 8; ++j) {
51
- const index = (c >> (5n * (7n - BigInt(j)))) & 31n;
52
- if (index < 0 || index > Number.MAX_SAFE_INTEGER)
53
- throw new Error(`Error: could not compute checksum, invalid index ${index}`);
54
- ret += exports.CHECKSUM_CHARSET[Number(index)];
55
- }
56
- return ret;
57
- };
58
- exports.DescriptorChecksum = DescriptorChecksum;