Skip to content

Commit 66a7436

Browse files
authored
Merge pull request #5434 from BitGo/BTC-1786.fix-tr-change-outputs
BTC 1786.fix tr change outputs
2 parents 7ddc69b + 758777c commit 66a7436

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

modules/abstract-utxo/src/core/descriptor/psbt/findDescriptors.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ function findDescriptorForAnyDerivationPath(
7171
return undefined;
7272
}
7373

74+
type WithBip32Derivation = { bip32Derivation?: { path: string }[] };
75+
type WithTapBip32Derivation = { tapBip32Derivation?: { path: string }[] };
76+
77+
function getDerivationPaths(v: WithBip32Derivation | WithTapBip32Derivation): string[] | undefined {
78+
if ('bip32Derivation' in v && v.bip32Derivation) {
79+
return v.bip32Derivation.map((v) => v.path);
80+
}
81+
if ('tapBip32Derivation' in v && v.tapBip32Derivation) {
82+
return v.tapBip32Derivation.map((v) => v.path).filter((v) => v !== '' && v !== 'm');
83+
}
84+
return undefined;
85+
}
86+
7487
/**
7588
* @param input
7689
* @param descriptorMap
@@ -84,21 +97,11 @@ export function findDescriptorForInput(
8497
if (!script) {
8598
throw new Error('Missing script');
8699
}
87-
if (input.bip32Derivation !== undefined) {
88-
return findDescriptorForAnyDerivationPath(
89-
script,
90-
input.bip32Derivation.map((v) => v.path),
91-
descriptorMap
92-
);
93-
}
94-
if (input.tapBip32Derivation !== undefined) {
95-
return findDescriptorForAnyDerivationPath(
96-
script,
97-
input.tapBip32Derivation.filter((v) => v.path !== '' && v.path !== 'm').map((v) => v.path),
98-
descriptorMap
99-
);
100+
const derivationPaths = getDerivationPaths(input);
101+
if (!derivationPaths) {
102+
throw new Error('Missing derivation paths');
100103
}
101-
throw new Error('Missing derivation path');
104+
return findDescriptorForAnyDerivationPath(script, derivationPaths, descriptorMap);
102105
}
103106

104107
/**
@@ -112,12 +115,9 @@ export function findDescriptorForOutput(
112115
output: PsbtOutput,
113116
descriptorMap: DescriptorMap
114117
): DescriptorWithIndex | undefined {
115-
if (!output.bip32Derivation) {
118+
const derivationPaths = getDerivationPaths(output);
119+
if (!derivationPaths) {
116120
return undefined;
117121
}
118-
return findDescriptorForAnyDerivationPath(
119-
script,
120-
output.bip32Derivation.map((d) => d.path),
121-
descriptorMap
122-
);
122+
return findDescriptorForAnyDerivationPath(script, derivationPaths, descriptorMap);
123123
}

modules/abstract-utxo/test/core/descriptor/psbt/findDescriptors.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { findDescriptorForInput, findDescriptorForOutput } from '../../../../src
55

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

8-
function describeWithTemplates(tA: DescriptorTemplate, tB: DescriptorTemplate) {
9-
describe(`parsePsbt [${tA},${tB}]`, function () {
10-
const descriptorA = getDescriptor(tA, getDefaultXPubs('a'));
11-
const descriptorB = getDescriptor(tB, getDefaultXPubs('b'));
8+
function describeWithTemplates(templateSelf: DescriptorTemplate, templateOther: DescriptorTemplate) {
9+
describe(`parsePsbt [${templateSelf},${templateOther}]`, function () {
10+
const descriptorA = getDescriptor(templateSelf, getDefaultXPubs('a'));
11+
const descriptorB = getDescriptor(templateOther, getDefaultXPubs('b'));
1212
const descriptorMap = new Map([
1313
['a', descriptorA],
1414
['b', descriptorB],
@@ -41,3 +41,4 @@ function describeWithTemplates(tA: DescriptorTemplate, tB: DescriptorTemplate) {
4141

4242
describeWithTemplates('Wsh2Of3', 'Wsh2Of3');
4343
describeWithTemplates('Wsh2Of3', 'Tr2Of3-NoKeyPath');
44+
describeWithTemplates('Tr2Of3-NoKeyPath', 'Tr2Of3-NoKeyPath');

0 commit comments

Comments
 (0)