Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tr(KEY) support #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,26 @@ export function DescriptorsFactory(ecc: TinySecp256k1Interface) {
payment = p2wpkh({ pubkey, network });
}
}
//tr(KEY) - taproot
else if (canonicalExpression.match(RE.rePtrAnchored)) {
isSegwit = true;
const keyExpression = canonicalExpression.match(RE.reKeyExp)?.[0];
if (!keyExpression)
throw new Error(`Error: keyExpression could not me extracted`);
if (canonicalExpression !== `tr(${keyExpression})`)
throw new Error(`Error: invalid expression ${expression}`);
expandedExpression = 'tr(@0)';
const pKE = parseKeyExpression({ keyExpression, network, isSegwit });
expansionMap = { '@0': pKE };
if (!isCanonicalRanged) {
const pubkey = pKE.pubkey;
if (!pubkey)
throw new Error(
`Error: could not extract a pubkey from ${expression}`
);
payment = p2tr({ pubkey, network });
}
}
//sh(wsh(miniscript))
else if (canonicalExpression.match(RE.reShWshMiniscriptAnchored)) {
isSegwit = true;
Expand Down
3 changes: 3 additions & 0 deletions src/re.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const reAddr = String.raw`addr\((.*?)\)`; //Matches anything. We assert later in
const rePkh = String.raw`pkh\(${reKeyExp}\)`;
const reWpkh = String.raw`wpkh\(${reKeyExp}\)`;
const reShWpkh = String.raw`sh\(wpkh\(${reKeyExp}\)\)`;
const rePtr = String.raw`tr\(${reKeyExp}\)`;


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

Expand All @@ -72,6 +74,7 @@ export const reAddrAnchored = anchorStartAndEnd(composeChecksum(reAddr));
export const rePkhAnchored = anchorStartAndEnd(composeChecksum(rePkh));
export const reWpkhAnchored = anchorStartAndEnd(composeChecksum(reWpkh));
export const reShWpkhAnchored = anchorStartAndEnd(composeChecksum(reShWpkh));
export const rePtrAnchored = anchorStartAndEnd(composeChecksum(rePtr));

export const reShMiniscriptAnchored = anchorStartAndEnd(
composeChecksum(makeReSh(reMiniscript))
Expand Down