Skip to content

Commit 7ca4628

Browse files
OttoAllmendingerllm-git
andcommitted
feat(abstract-utxo): improve parseTransaction expected outputs logic
Move transaction explanation logic after expected outputs calculation to improve logical flow. Change how external change addresses are handled in expectedOutputs to use 'max' amount instead of adding all outputs. Issue: BTC-2732 Co-authored-by: llm-git <[email protected]>
1 parent 195c1eb commit 7ca4628

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

modules/abstract-utxo/src/transaction/fixedScript/parseTransaction.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ function toExpectedOutputs(
5454
recipients?: ITransactionRecipient[];
5555
allowExternalChangeAddress?: boolean;
5656
changeAddress?: string;
57-
},
58-
allOutputs: Output[]
57+
}
5958
): Output[] {
6059
// verify that each recipient from txParams has their own output
6160
const expectedOutputs = (txParams.recipients ?? []).flatMap((output) => {
@@ -70,17 +69,7 @@ function toExpectedOutputs(
7069
if (txParams.allowExternalChangeAddress && txParams.changeAddress) {
7170
// when an external change address is explicitly specified, count all outputs going towards that
7271
// address in the expected outputs (regardless of the output amount)
73-
expectedOutputs.push(
74-
...allOutputs.flatMap((output) => {
75-
if (
76-
output.address === undefined ||
77-
output.address !== coin.canonicalAddress(txParams.changeAddress as string)
78-
) {
79-
return [];
80-
}
81-
return [{ ...output, address: coin.canonicalAddress(output.address) }];
82-
})
83-
);
72+
expectedOutputs.push({ address: coin.canonicalAddress(txParams.changeAddress), amount: 'max' });
8473
}
8574
return expectedOutputs;
8675
}
@@ -120,16 +109,7 @@ export async function parseTransaction<TNumber extends bigint | number>(
120109
throw new Error('missing required txPrebuild property txHex');
121110
}
122111

123-
// obtain all outputs
124-
const explanation: TransactionExplanation = await coin.explainTransaction<TNumber>({
125-
txHex: txPrebuild.txHex,
126-
txInfo: txPrebuild.txInfo,
127-
pubs: keychainArray.map((k) => k.pub) as Triple<string>,
128-
});
129-
130-
const allOutputs = [...explanation.outputs, ...explanation.changeOutputs];
131-
132-
const expectedOutputs = toExpectedOutputs(coin, txParams, allOutputs);
112+
const expectedOutputs = toExpectedOutputs(coin, txParams);
133113

134114
// get the keychains from the custom change wallet if needed
135115
let customChange: CustomChangeOptions | undefined;
@@ -159,6 +139,15 @@ export async function parseTransaction<TNumber extends bigint | number>(
159139
}
160140
}
161141

142+
// obtain all outputs
143+
const explanation: TransactionExplanation = await coin.explainTransaction<TNumber>({
144+
txHex: txPrebuild.txHex,
145+
txInfo: txPrebuild.txInfo,
146+
pubs: keychainArray.map((k) => k.pub) as Triple<string>,
147+
});
148+
149+
const allOutputs = [...explanation.outputs, ...explanation.changeOutputs];
150+
162151
/**
163152
* Loop through all the outputs and classify each of them as either internal spends
164153
* or external spends by setting the "external" property to true or false on the output object.

0 commit comments

Comments
 (0)