Skip to content

Commit

Permalink
fix(abstract-utxo): fix findDescriptorForOutput for taproot outputs
Browse files Browse the repository at this point in the history
Issue: BTC-1786
  • Loading branch information
OttoAllmendinger authored and lcovar committed Jan 27, 2025
1 parent 437eb7e commit 758777c
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions modules/abstract-utxo/src/core/descriptor/psbt/findDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ function findDescriptorForAnyDerivationPath(
return undefined;
}

type WithBip32Derivation = { bip32Derivation?: { path: string }[] };
type WithTapBip32Derivation = { tapBip32Derivation?: { path: string }[] };

function getDerivationPaths(v: WithBip32Derivation | WithTapBip32Derivation): string[] | undefined {
if ('bip32Derivation' in v && v.bip32Derivation) {
return v.bip32Derivation.map((v) => v.path);
}
if ('tapBip32Derivation' in v && v.tapBip32Derivation) {
return v.tapBip32Derivation.map((v) => v.path).filter((v) => v !== '' && v !== 'm');
}
return undefined;
}

/**
* @param input
* @param descriptorMap
Expand All @@ -84,21 +97,11 @@ export function findDescriptorForInput(
if (!script) {
throw new Error('Missing script');
}
if (input.bip32Derivation !== undefined) {
return findDescriptorForAnyDerivationPath(
script,
input.bip32Derivation.map((v) => v.path),
descriptorMap
);
}
if (input.tapBip32Derivation !== undefined) {
return findDescriptorForAnyDerivationPath(
script,
input.tapBip32Derivation.filter((v) => v.path !== '' && v.path !== 'm').map((v) => v.path),
descriptorMap
);
const derivationPaths = getDerivationPaths(input);
if (!derivationPaths) {
throw new Error('Missing derivation paths');
}
throw new Error('Missing derivation path');
return findDescriptorForAnyDerivationPath(script, derivationPaths, descriptorMap);
}

/**
Expand All @@ -112,12 +115,9 @@ export function findDescriptorForOutput(
output: PsbtOutput,
descriptorMap: DescriptorMap
): DescriptorWithIndex | undefined {
if (!output.bip32Derivation) {
const derivationPaths = getDerivationPaths(output);
if (!derivationPaths) {
return undefined;
}
return findDescriptorForAnyDerivationPath(
script,
output.bip32Derivation.map((d) => d.path),
descriptorMap
);
return findDescriptorForAnyDerivationPath(script, derivationPaths, descriptorMap);
}

0 comments on commit 758777c

Please sign in to comment.