@bitcoinerlab/descriptors 3.0.5 → 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.
- package/README.md +9 -487
- package/index.d.ts +13 -0
- package/index.js +16 -0
- package/package.json +22 -53
- package/dist/applyPR2137.d.ts +0 -2
- package/dist/applyPR2137.js +0 -153
- package/dist/bitcoinjs-lib-internals.d.ts +0 -10
- package/dist/bitcoinjs-lib-internals.js +0 -50
- package/dist/checksum.d.ts +0 -6
- package/dist/checksum.js +0 -58
- package/dist/descriptors.d.ts +0 -433
- package/dist/descriptors.js +0 -1743
- package/dist/index.d.ts +0 -21
- package/dist/index.js +0 -85
- package/dist/keyExpressions.d.ts +0 -83
- package/dist/keyExpressions.js +0 -248
- package/dist/ledger.d.ts +0 -167
- package/dist/ledger.js +0 -580
- package/dist/miniscript.d.ts +0 -123
- package/dist/miniscript.js +0 -305
- package/dist/multipath.d.ts +0 -13
- package/dist/multipath.js +0 -76
- package/dist/networkUtils.d.ts +0 -3
- package/dist/networkUtils.js +0 -16
- package/dist/parseUtils.d.ts +0 -7
- package/dist/parseUtils.js +0 -46
- package/dist/psbt.d.ts +0 -44
- package/dist/psbt.js +0 -193
- package/dist/re.d.ts +0 -31
- package/dist/re.js +0 -79
- package/dist/resourceLimits.d.ts +0 -25
- package/dist/resourceLimits.js +0 -89
- package/dist/scriptExpressions.d.ts +0 -95
- package/dist/scriptExpressions.js +0 -89
- package/dist/signers.d.ts +0 -84
- package/dist/signers.js +0 -215
- package/dist/tapMiniscript.d.ts +0 -215
- package/dist/tapMiniscript.js +0 -515
- package/dist/tapTree.d.ts +0 -86
- package/dist/tapTree.js +0 -167
- package/dist/types.d.ts +0 -238
- package/dist/types.js +0 -4
package/dist/applyPR2137.js
DELETED
|
@@ -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 * as Bip341 from 'bitcoinjs-lib/src/cjs/payments/bip341';
|
|
2
|
-
import type * as Bip371 from 'bitcoinjs-lib/src/cjs/psbt/bip371';
|
|
3
|
-
import type * as PsbtUtils from 'bitcoinjs-lib/src/cjs/psbt/psbtutils';
|
|
4
|
-
export declare const findScriptPath: typeof Bip341.findScriptPath;
|
|
5
|
-
export declare const tapleafHash: typeof Bip341.tapleafHash;
|
|
6
|
-
export declare const tapTweakHash: typeof Bip341.tapTweakHash;
|
|
7
|
-
export declare const toHashTree: typeof Bip341.toHashTree;
|
|
8
|
-
export declare const tweakKey: typeof Bip341.tweakKey;
|
|
9
|
-
export declare const witnessStackToScriptWitness: typeof PsbtUtils.witnessStackToScriptWitness;
|
|
10
|
-
export declare const isTaprootInput: typeof Bip371.isTaprootInput;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
3
|
-
/*
|
|
4
|
-
* bitcoinjs-lib v7 does not export all the taproot/psbt helpers we need from
|
|
5
|
-
* its top-level API, so this module centralizes only the required deep imports.
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.isTaprootInput = exports.witnessStackToScriptWitness = exports.tweakKey = exports.toHashTree = exports.tapTweakHash = exports.tapleafHash = exports.findScriptPath = void 0;
|
|
9
|
-
function resolveAbsoluteCjsPath(relativePath) {
|
|
10
|
-
try {
|
|
11
|
-
const entryPoint = require.resolve('bitcoinjs-lib');
|
|
12
|
-
const root = entryPoint.replace(/src[\\/]+cjs[\\/]+index\.cjs$/, '');
|
|
13
|
-
if (root === entryPoint)
|
|
14
|
-
return undefined;
|
|
15
|
-
return `${root}src/cjs/${relativePath}.cjs`;
|
|
16
|
-
}
|
|
17
|
-
catch (_err) {
|
|
18
|
-
void _err;
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
function requireBitcoinJsInternal(relativePath) {
|
|
23
|
-
const candidatePaths = [
|
|
24
|
-
`bitcoinjs-lib/src/${relativePath}`,
|
|
25
|
-
`bitcoinjs-lib/src/cjs/${relativePath}.cjs`
|
|
26
|
-
];
|
|
27
|
-
const absoluteCjsPath = resolveAbsoluteCjsPath(relativePath);
|
|
28
|
-
if (absoluteCjsPath)
|
|
29
|
-
candidatePaths.push(absoluteCjsPath);
|
|
30
|
-
let lastError;
|
|
31
|
-
for (const modulePath of candidatePaths) {
|
|
32
|
-
try {
|
|
33
|
-
return require(modulePath);
|
|
34
|
-
}
|
|
35
|
-
catch (err) {
|
|
36
|
-
lastError = err;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
throw lastError;
|
|
40
|
-
}
|
|
41
|
-
const bip341 = requireBitcoinJsInternal('payments/bip341');
|
|
42
|
-
const bip371 = requireBitcoinJsInternal('psbt/bip371');
|
|
43
|
-
const psbtUtils = requireBitcoinJsInternal('psbt/psbtutils');
|
|
44
|
-
exports.findScriptPath = bip341.findScriptPath;
|
|
45
|
-
exports.tapleafHash = bip341.tapleafHash;
|
|
46
|
-
exports.tapTweakHash = bip341.tapTweakHash;
|
|
47
|
-
exports.toHashTree = bip341.toHashTree;
|
|
48
|
-
exports.tweakKey = bip341.tweakKey;
|
|
49
|
-
exports.witnessStackToScriptWitness = psbtUtils.witnessStackToScriptWitness;
|
|
50
|
-
exports.isTaprootInput = bip371.isTaprootInput;
|
package/dist/checksum.d.ts
DELETED
|
@@ -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;
|