Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bitcoinerlab/descriptors",
"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.",
"homepage": "https://github.com/bitcoinerlab/descriptors",
"version": "2.3.4",
"version": "2.3.5",
"author": "Jose-Luis Landabaso",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -36,13 +36,13 @@
"build": "npm run build:src && npm run build:test",
"lint": "./node_modules/@bitcoinerlab/configs/scripts/lint.sh",
"ensureTester": "./node_modules/@bitcoinerlab/configs/scripts/ensureTester.sh",
"test:integration:soft": "npm run ensureTester && node test/integration/standardOutputs.js && echo \"\\n\\n\" && node test/integration/miniscript.js",
"test:integration:soft": "npm run ensureTester && node test/integration/standardOutputs.js && echo \"\n\n\" && node test/integration/miniscript.js && echo \"\n\n\" && node test/integration/sortedmulti.js",
"test:integration:ledger": "npm run ensureTester && node test/integration/ledger.js",
"test:integration:deprecated": "npm run ensureTester && node test/integration/standardOutputs-deprecated.js && echo \"\\n\\n\" && node test/integration/miniscript-deprecated.js && echo \"\\n\\n\" && node test/integration/ledger-deprecated.js",
"test:integration:deprecated": "npm run ensureTester && node test/integration/standardOutputs-deprecated.js && echo \"\n\n\" && node test/integration/miniscript-deprecated.js && echo \"\n\n\" && node test/integration/ledger-deprecated.js",
"test:unit": "jest",
"test": "npm run lint && npm run build && npm run test:unit && npm run test:integration:soft",
"testledger": "npm run lint && npm run build && npm run test:integration:ledger",
"prepublishOnly": "npm run test && echo \"\\n\\n\" && npm run test:integration:deprecated && npm run test:integration:ledger"
"prepublishOnly": "npm run test && echo \"\n\n\" && npm run test:integration:deprecated && npm run test:integration:ledger"
},
"files": [
"dist"
Expand Down
145 changes: 145 additions & 0 deletions src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ function evaluate({
return evaluatedDescriptor;
}

// Helper: parse sortedmulti(M, k1, k2,...)
function parseSortedMulti(inner: string) {
// inner: "2,key1,key2,key3"

const parts = inner.split(',').map(p => p.trim());
if (parts.length < 2)
throw new Error(
`sortedmulti(): must contain M and at least one key: ${inner}`
);

const m = Number(parts[0]);
if (!Number.isInteger(m) || m < 1 || m > 20)
throw new Error(`sortedmulti(): invalid M=${parts[0]}`);

const keyExpressions = parts.slice(1);
if (keyExpressions.length < m)
throw new Error(`sortedmulti(): M cannot exceed number of keys: ${inner}`);

if (keyExpressions.length > 20)
throw new Error(
`sortedmulti(): descriptors support up to 20 keys (per BIP 380/383).`
);

return { m, keyExpressions };
}

/**
* Constructs the necessary functions and classes for working with descriptors
* using an external elliptic curve (ecc) library.
Expand Down Expand Up @@ -451,6 +477,125 @@ export function DescriptorsFactory(ecc: TinySecp256k1Interface) {
payment = p2wpkh({ pubkey, network });
}
}
// sortedmulti script expressions
// sh(sortedmulti())
else if (canonicalExpression.match(RE.reShSortedMultiAnchored)) {
isSegwit = false;
isTaproot = false;

const inner = canonicalExpression.match(RE.reShSortedMultiAnchored)?.[1];
if (!inner)
throw new Error(`Error extracting sortedmulti() in ${descriptor}`);

const { m, keyExpressions } = parseSortedMulti(inner);

const pKEs = keyExpressions.map(k =>
parseKeyExpression({ keyExpression: k, network, isSegwit: false })
);

const map: ExpansionMap = {};
pKEs.forEach((pke, i) => (map['@' + i] = pke));
expansionMap = map;

expandedExpression =
'sh(sortedmulti(' +
[m, ...Object.keys(expansionMap).map(k => k)].join(',') +
'))';

if (!isCanonicalRanged) {
const pubkeys = pKEs.map(p => {
if (!p.pubkey) throw new Error(`Error: key has no pubkey`);
return p.pubkey;
});
pubkeys.sort((a, b) => a.compare(b));

const redeem = payments.p2ms({ m, pubkeys, network });
redeemScript = redeem.output;
if (!redeemScript) throw new Error(`Error creating redeemScript`);

payment = payments.p2sh({ redeem, network });
}
}
// wsh(sortedmulti())
else if (canonicalExpression.match(RE.reWshSortedMultiAnchored)) {
isSegwit = true;
isTaproot = false;

const inner = canonicalExpression.match(RE.reWshSortedMultiAnchored)?.[1];
if (!inner)
throw new Error(`Error extracting sortedmulti() in ${descriptor}`);

const { m, keyExpressions } = parseSortedMulti(inner);

const pKEs = keyExpressions.map(k =>
parseKeyExpression({ keyExpression: k, network, isSegwit: true })
);

const map: ExpansionMap = {};
pKEs.forEach((pke, i) => (map['@' + i] = pke));
expansionMap = map;

expandedExpression =
'wsh(sortedmulti(' +
[m, ...Object.keys(expansionMap).map(k => k)].join(',') +
'))';

if (!isCanonicalRanged) {
const pubkeys = pKEs.map(p => {
if (!p.pubkey) throw new Error(`Error: key has no pubkey`);
return p.pubkey;
});
pubkeys.sort((a, b) => a.compare(b));

const redeem = payments.p2ms({ m, pubkeys, network });
witnessScript = redeem.output;
if (!witnessScript) throw new Error(`Error computing witnessScript`);

payment = payments.p2wsh({ redeem, network });
}
}
// sh(wsh(sortedmulti()))
else if (canonicalExpression.match(RE.reShWshSortedMultiAnchored)) {
isSegwit = true;
isTaproot = false;

const inner = canonicalExpression.match(
RE.reShWshSortedMultiAnchored
)?.[1];
if (!inner)
throw new Error(`Error extracting sortedmulti() in ${descriptor}`);

const { m, keyExpressions } = parseSortedMulti(inner);

const pKEs = keyExpressions.map(k =>
parseKeyExpression({ keyExpression: k, network, isSegwit: true })
);

const map: ExpansionMap = {};
pKEs.forEach((pke, i) => (map['@' + i] = pke));
expansionMap = map;

expandedExpression =
'sh(wsh(sortedmulti(' +
[m, ...Object.keys(expansionMap).map(k => k)].join(',') +
')))';

if (!isCanonicalRanged) {
const pubkeys = pKEs.map(p => {
if (!p.pubkey) throw new Error(`Error: key has no pubkey`);
return p.pubkey;
});
pubkeys.sort((a, b) => a.compare(b));

const redeem = payments.p2ms({ m, pubkeys, network });
const wsh = payments.p2wsh({ redeem, network });

witnessScript = redeem.output;
redeemScript = wsh.output;

payment = payments.p2sh({ redeem: wsh, network });
}
}
//sh(wsh(miniscript))
else if (canonicalExpression.match(RE.reShWshMiniscriptAnchored)) {
isSegwit = true;
Expand Down
13 changes: 13 additions & 0 deletions src/re.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const reWpkh = String.raw`wpkh\(${reSegwitKeyExp}\)`;
const reShWpkh = String.raw`sh\(wpkh\(${reSegwitKeyExp}\)\)`;
const reTrSingleKey = String.raw`tr\(${reTaprootKeyExp}\)`; // TODO: tr(KEY,TREE) not yet supported. TrSingleKey used for tr(KEY)

export const reNonSegwitSortedMulti = String.raw`sortedmulti\(((?:1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20)(?:,${reNonSegwitKeyExp})+)\)`;
export const reSegwitSortedMulti = String.raw`sortedmulti\(((?:1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20)(?:,${reSegwitKeyExp})+)\)`;

const reMiniscript = String.raw`(.*?)`; //Matches anything. We assert later in the code that miniscripts are valid and sane.

//RegExp makers:
Expand All @@ -84,6 +87,16 @@ export const reTrSingleKeyAnchored = anchorStartAndEnd(
composeChecksum(reTrSingleKey)
);

export const reShSortedMultiAnchored = anchorStartAndEnd(
composeChecksum(makeReSh(reNonSegwitSortedMulti))
);
export const reWshSortedMultiAnchored = anchorStartAndEnd(
composeChecksum(makeReWsh(reSegwitSortedMulti))
);
export const reShWshSortedMultiAnchored = anchorStartAndEnd(
composeChecksum(makeReShWsh(reSegwitSortedMulti))
);

export const reShMiniscriptAnchored = anchorStartAndEnd(
composeChecksum(makeReSh(reMiniscript))
);
Expand Down
52 changes: 52 additions & 0 deletions test/fixtures/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,58 @@ export const fixtures = {
}
}
}
},
{
note: 'Native SegWit (P2WSH) sortedmulti descriptor with 2 keys',
descriptor:
'wsh(sortedmulti(2,03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,0260b2003c386519fc9eadf2b5cf124dd8eea4c4e68d5e154050a9346ea98ce600))',
checksumRequired: false,
script:
'0020a18e1931caa82844ddb8294107de1b3e15f1c603983df8d9b6caa0ef6419c5d2',
address: 'bc1q5x8pjvw24q5yfhdc99qs0hsm8c2lr3srnq7l3kdke2sw7eqechfq62zxqh',
expansion: {
expandedExpression: 'wsh(sortedmulti(2,@0,@1))',
expansionMap: {
'@0': {
keyExpression:
'03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd'
},
'@1': {
keyExpression:
'0260b2003c386519fc9eadf2b5cf124dd8eea4c4e68d5e154050a9346ea98ce600'
}
}
}
},
{
note: 'Nested SegWit (P2SH-P2WSH) sortedmulti descriptor with 2 of 3',
descriptor:
"sh(wsh(sortedmulti(2,[d34db33f/44'/0'/0]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/0/0,[00000000/44'/0'/0]xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0/0/0,[11111111/44'/0'/0]xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/0/0/0)))",
checksumRequired: false,
address: '3FZBUGnutPpKWrhGSRXwcMCv7M2ekUva5c',
expansion: {
expandedExpression: 'sh(wsh(sortedmulti(2,@0,@1,@2)))',
expansionMap: {
'@0': {
keyExpression:
"[d34db33f/44'/0'/0]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/0/0",
originPath: "/44'/0'/0",
path: "m/44'/0'/0/1/0/0"
},
'@1': {
keyExpression:
"[00000000/44'/0'/0]xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0/0/0",
originPath: "/44'/0'/0",
path: "m/44'/0'/0/0/0/0"
},
'@2': {
keyExpression:
"[11111111/44'/0'/0]xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/0/0/0",
originPath: "/44'/0'/0",
path: "m/44'/0'/0/0/0/0"
}
}
}
}
],
invalid: [
Expand Down
Loading