Skip to content

Commit

Permalink
Merge pull request #5434 from BitGo/BTC-1786.fix-tr-change-outputs
Browse files Browse the repository at this point in the history
BTC 1786.fix tr change outputs
  • Loading branch information
lcovar authored Jan 27, 2025
2 parents 7ddc69b + 758777c commit 66a7436
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { findDescriptorForInput, findDescriptorForOutput } from '../../../../src

import { mockPsbt } from './mock.utils';

function describeWithTemplates(tA: DescriptorTemplate, tB: DescriptorTemplate) {
describe(`parsePsbt [${tA},${tB}]`, function () {
const descriptorA = getDescriptor(tA, getDefaultXPubs('a'));
const descriptorB = getDescriptor(tB, getDefaultXPubs('b'));
function describeWithTemplates(templateSelf: DescriptorTemplate, templateOther: DescriptorTemplate) {
describe(`parsePsbt [${templateSelf},${templateOther}]`, function () {
const descriptorA = getDescriptor(templateSelf, getDefaultXPubs('a'));
const descriptorB = getDescriptor(templateOther, getDefaultXPubs('b'));
const descriptorMap = new Map([
['a', descriptorA],
['b', descriptorB],
Expand Down Expand Up @@ -41,3 +41,4 @@ function describeWithTemplates(tA: DescriptorTemplate, tB: DescriptorTemplate) {

describeWithTemplates('Wsh2Of3', 'Wsh2Of3');
describeWithTemplates('Wsh2Of3', 'Tr2Of3-NoKeyPath');
describeWithTemplates('Tr2Of3-NoKeyPath', 'Tr2Of3-NoKeyPath');

0 comments on commit 66a7436

Please sign in to comment.