Skip to content
Draft
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
6 changes: 6 additions & 0 deletions packages/bitcoin-wallet-snap/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Keep the template output order when filling a PSBT ([#157](https://github.com/MetaMask/internal-snaps/pull/157))
- A template output belonging to the wallet is now only used as the drain output when it is the last output. Previously any such output was moved to the end of the transaction, silently reordering templates that place change before another output.
- Filling a PSBT now fails with a `ValidationError` when the built transaction does not reproduce every template output, at its original index, with its original value. Previously only the output count was compared, so a divergent transaction could be signed and broadcast.

## [2.0.1]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcoin-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "jV649WZbbfbj3FpOMD5U/xDPuRD0t4F+pxCoy08a/O0=",
"shasum": "B9w8DmGsWcU8IBkEJemc1WCP46/jbdO4uBwcmDgtvg4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
140 changes: 140 additions & 0 deletions packages/bitcoin-wallet-snap/src/use-cases/AccountUseCases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,146 @@ describe('AccountUseCases', () => {
// Result should be the rebuilt PSBT with all outputs preserved
expect(result).toBe(rebuiltPsbt);
});

const identifiableOutput = (scriptHex: string, sats: bigint): TxOut => {
const scriptPubkey = mock<ScriptBuf>();
scriptPubkey.to_hex_string.mockReturnValue(scriptHex);
const value = mock<Amount>();
value.to_sat.mockReturnValue(sats);

return mock<TxOut>({ script_pubkey: scriptPubkey, value });
};

const accountOwning = (owned: ScriptBuf[]): BitcoinAccount => {
const account = mock<BitcoinAccount>({
id: 'account-id',
network: 'bitcoin',
isMine: (script: ScriptBuf) => owned.includes(script),
capabilities: [AccountCapability.FillPsbt],
});
account.buildTx.mockReturnValue(mockTxBuilder);
return account;
};

it('adds every template output as a fixed recipient when the wallet-owned output is not last', async () => {
const changeOutput = identifiableOutput('0014aaaa', 2548n);
const depositOutput = identifiableOutput('5120bbbb', 496774n);
const template = mock<Psbt>({
unsigned_tx: { output: [changeOutput, depositOutput] },
toString: () => 'templateBase64',
});
mockTxBuilder.finish.mockReturnValue(
mock<Psbt>({
unsigned_tx: { output: [changeOutput, depositOutput] },
}),
);
mockRepository.get.mockResolvedValueOnce(
accountOwning([changeOutput.script_pubkey]),
);

await useCases.fillPsbt('account-id', template);

expect(mockTxBuilder.drainToByScript).not.toHaveBeenCalled();
expect(mockTxBuilder.addRecipientByScript).toHaveBeenCalledTimes(2);
expect(mockTxBuilder.addRecipientByScript).toHaveBeenNthCalledWith(
1,
changeOutput.value,
changeOutput.script_pubkey,
);
expect(mockTxBuilder.addRecipientByScript).toHaveBeenNthCalledWith(
2,
depositOutput.value,
depositOutput.script_pubkey,
);
});

it('throws when the built outputs are reordered against the template', async () => {
const depositOutput = identifiableOutput('5120bbbb', 496774n);
const opReturnOutput = identifiableOutput('6a3ecccc', 0n);
const template = mock<Psbt>({
unsigned_tx: { output: [depositOutput, opReturnOutput] },
toString: () => 'templateBase64',
});
mockTxBuilder.finish.mockReturnValue(
mock<Psbt>({
unsigned_tx: {
output: [
opReturnOutput,
identifiableOutput('0014aaaa', 2548n),
depositOutput,
],
},
}),
);
mockRepository.get.mockResolvedValueOnce(accountOwning([]));

await expect(useCases.fillPsbt('account-id', template)).rejects.toThrow(
'Built PSBT does not preserve the template outputs',
);
});

it('throws when a built output value diverges from the template', async () => {
const depositOutput = identifiableOutput('5120bbbb', 496774n);
const template = mock<Psbt>({
unsigned_tx: { output: [depositOutput] },
toString: () => 'templateBase64',
});
mockTxBuilder.finish.mockReturnValue(
mock<Psbt>({
unsigned_tx: { output: [identifiableOutput('5120bbbb', 1n)] },
}),
);
mockRepository.get.mockResolvedValueOnce(accountOwning([]));

await expect(useCases.fillPsbt('account-id', template)).rejects.toThrow(
'Built PSBT does not preserve the template outputs',
);
});

it('accepts a built PSBT that appends a change output after the template outputs', async () => {
const depositOutput = identifiableOutput('5120bbbb', 496774n);
const opReturnOutput = identifiableOutput('6a3ecccc', 0n);
const template = mock<Psbt>({
unsigned_tx: { output: [depositOutput, opReturnOutput] },
toString: () => 'templateBase64',
});
const builtPsbt = mock<Psbt>({
unsigned_tx: {
output: [
depositOutput,
opReturnOutput,
identifiableOutput('0014aaaa', 2548n),
],
},
});
mockTxBuilder.finish.mockReturnValue(builtPsbt);
mockRepository.get.mockResolvedValueOnce(accountOwning([]));

expect(await useCases.fillPsbt('account-id', template)).toBe(builtPsbt);
});

it('accepts the drained output taking a value the template did not specify', async () => {
const depositOutput = identifiableOutput('5120bbbb', 496774n);
const changeOutput = identifiableOutput('0014aaaa', 1000n);
const template = mock<Psbt>({
unsigned_tx: { output: [depositOutput, changeOutput] },
toString: () => 'templateBase64',
});
const builtPsbt = mock<Psbt>({
unsigned_tx: {
output: [depositOutput, identifiableOutput('0014aaaa', 2548n)],
},
});
mockTxBuilder.finish.mockReturnValue(builtPsbt);
mockRepository.get.mockResolvedValueOnce(
accountOwning([changeOutput.script_pubkey]),
);

expect(await useCases.fillPsbt('account-id', template)).toBe(builtPsbt);
expect(mockTxBuilder.drainToByScript).toHaveBeenCalledWith(
changeOutput.script_pubkey,
);
});
});

describe('computeFee', () => {
Expand Down
86 changes: 67 additions & 19 deletions packages/bitcoin-wallet-snap/src/use-cases/AccountUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,47 +779,47 @@ export class AccountUseCases {
const frozenUTXOs = await this.#repository.getFrozenUTXOs(account.id);
const feeRateToUse = feeRate ?? (await this.getFallbackFeeRate(account));

const templateOutputs = templatePsbt.unsigned_tx.output;
const lastOutput = templateOutputs[templateOutputs.length - 1];
// The drain output is always appended last, so only a trailing output of ours can keep its position. Any other output of ours stays a fixed recipient.
const drainIndex =
lastOutput && account.isMine(lastOutput.script_pubkey)
? templateOutputs.length - 1
: -1;

let builtPsbt: Psbt;
try {
let builder = account
.buildTx()
.feeRate(feeRateToUse)
.unspendable(frozenUTXOs)
.untouchedOrdering(); // we need to strictly adhere to the template output order. Many protocols use the order (e.g: 1: deposit, 2: OP_RETURN, 3: change)

for (const txout of templatePsbt.unsigned_tx.output) {
// if the PSBT contains an output that is sending to ourselves, we change its value. If the PSBT contains no change outputs, one will automatically be added.
if (account.isMine(txout.script_pubkey)) {
builder = builder.drainToByScript(txout.script_pubkey);
} else {
builder = builder.addRecipientByScript(
txout.value,
txout.script_pubkey,
);
}
}
let builtPsbt = builder.finish();
templateOutputs.forEach((txout, index) => {
// the drain output takes the excess, so its template value is replaced. If the template has no output of ours, one will automatically be added.
builder =
index === drainIndex
? builder.drainToByScript(txout.script_pubkey)
: builder.addRecipientByScript(txout.value, txout.script_pubkey);
});
builtPsbt = builder.finish();

if (
builtPsbt.unsigned_tx.output.length <
templatePsbt.unsigned_tx.output.length
) {
if (builtPsbt.unsigned_tx.output.length < templateOutputs.length) {
// Second attempt: use fixed recipients for all outputs
builder = account
.buildTx()
.feeRate(feeRateToUse)
.unspendable(frozenUTXOs)
.untouchedOrdering();

for (const txout of templatePsbt.unsigned_tx.output) {
for (const txout of templateOutputs) {
builder = builder.addRecipientByScript(
txout.value,
txout.script_pubkey,
);
}
builtPsbt = builder.finish();
}

return builtPsbt;
} catch (error) {
const causeMessage = (error as Error)?.message ?? 'unknown cause';
throw new ValidationError(
Expand All @@ -832,6 +832,54 @@ export class AccountUseCases {
error,
);
}

this.#assertTemplateOutputsPreserved(
account,
templatePsbt,
builtPsbt,
drainIndex,
feeRateToUse,
);

return builtPsbt;
}

#assertTemplateOutputsPreserved(
account: BitcoinAccount,
templatePsbt: Psbt,
builtPsbt: Psbt,
drainIndex: number,
feeRate: number,
): void {
const templateOutputs = templatePsbt.unsigned_tx.output;
const builtOutputs = builtPsbt.unsigned_tx.output;

const preserved =
builtOutputs.length >= templateOutputs.length &&
templateOutputs.every((txout, index) => {
const builtOutput = builtOutputs[index];
if (!builtOutput) {
return false;
}
return (
builtOutput.script_pubkey.to_hex_string() ===
txout.script_pubkey.to_hex_string() &&
(index === drainIndex ||
builtOutput.value.to_sat() === txout.value.to_sat())
);
});

if (!preserved) {
throw new ValidationError(
'Built PSBT does not preserve the template outputs',
{
id: account.id,
templatePsbt: templatePsbt.toString(),
builtPsbt: builtPsbt.toString(),
feeRate,
},
);
}
}

async #broadcast(
Expand Down