From c1c8c190538acadb6625815034259f5f9cf14341 Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 03:18:07 +0000 Subject: [PATCH 1/2] fix(sdk-coin-ton): reject non-exact send modes Guard every parsed action before extracting native or jetton outputs.\nUse nominalAmount for the new parser contract and keep a strict mode-3\ncompatibility read for the pinned 1.1.1 package.\n\nThis prevents recipient amount checks from approving balance-carrying\nor account-destroying TON sends.\n\nTicket: WCN-1888 Session-Id: c3c1a2b6-ca94-47c4-81d9-b16cf4920171 Task-Id: aec26d89-87b2-4383-8624-e3f7972c32e7 --- .../src/lib/explainTransactionWasm.ts | 51 +++++++++++++++-- .../test/unit/explainTransactionWasm.ts | 57 ++++++++++++++++++- 2 files changed, 102 insertions(+), 6 deletions(-) diff --git a/modules/sdk-coin-ton/src/lib/explainTransactionWasm.ts b/modules/sdk-coin-ton/src/lib/explainTransactionWasm.ts index bfaaeb5c1d7..c1e38cff8c2 100644 --- a/modules/sdk-coin-ton/src/lib/explainTransactionWasm.ts +++ b/modules/sdk-coin-ton/src/lib/explainTransactionWasm.ts @@ -35,8 +35,49 @@ export interface ExplainTonTransactionWasmOptions { toAddressBounceable?: boolean; } -function extractOutputs( - parsed: WasmParsedTransaction, +type EffectiveAmountKind = 'Exact' | 'CarryInboundValue' | 'AllRemainingBalance'; + +interface ParsedTonSendAction { + mode: number; + nominalAmount?: bigint; + effectiveAmountKind?: EffectiveAmountKind; + carriesInboundValue?: boolean; + carriesAllBalance?: boolean; + destroyAccountIfZero?: boolean; + /** @bitgo/wasm-ton@1.1.1 calls mode-3 nominal grams `amount`. */ + amount?: bigint; + destination: string; + destinationBounceable: string; + jettonTransfer?: { destination: string; amount: bigint }; + withdrawAmount?: bigint; +} + +type ParsedTonTransaction = Omit & { + sendActions: ParsedTonSendAction[]; +}; + +function exactNominalAmount(action: ParsedTonSendAction): bigint { + if ( + action.mode !== 3 || + (action.effectiveAmountKind !== undefined && action.effectiveAmountKind !== 'Exact') || + action.carriesInboundValue === true || + action.carriesAllBalance === true || + action.destroyAccountIfZero === true + ) { + throw new Error('TON send actions must use exact, non-destructive mode 3'); + } + + // Only the strict mode-3 allowlist may use the legacy field while deployed + // @bitgo/wasm-ton@1.1.1 is being replaced by the nominalAmount API. + const amount = action.nominalAmount ?? action.amount; + if (amount === undefined) { + throw new Error('TON send action is missing its nominal amount'); + } + return amount; +} + +export function extractOutputs( + parsed: Pick, toAddressBounceable: boolean ): { outputs: { address: string; amount: string }[]; @@ -47,6 +88,8 @@ function extractOutputs( let withdrawAmount: string | undefined; for (const action of parsed.sendActions) { + const nominalAmount = exactNominalAmount(action); + if (action.jettonTransfer) { outputs.push({ address: action.jettonTransfer.destination, @@ -57,7 +100,7 @@ function extractOutputs( // destination respects the original bounce flag (UQ... when bounce=false) outputs.push({ address: toAddressBounceable ? action.destinationBounceable : action.destination, - amount: String(action.amount), + amount: String(nominalAmount), }); } @@ -83,7 +126,7 @@ export async function explainTonTransaction(params: ExplainTonTransactionWasmOpt const { Transaction: WasmTonTransaction, parseTransaction } = await loadWasmTon(); const toAddressBounceable = params.toAddressBounceable !== false; const tx = WasmTonTransaction.fromBytes(Buffer.from(params.txBase64, 'base64')); - const parsed: WasmParsedTransaction = parseTransaction(tx); + const parsed = parseTransaction(tx) as ParsedTonTransaction; const { outputs, outputAmount, withdrawAmount } = extractOutputs(parsed, toAddressBounceable); diff --git a/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts b/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts index d6ee3145bc6..34020134d03 100644 --- a/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts +++ b/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts @@ -1,9 +1,61 @@ +import assert from 'assert'; import should from 'should'; import { Transaction as WasmTonTransaction, parseTransaction } from '@bitgo/wasm-ton'; -import { explainTonTransaction } from '../../src/lib/explainTransactionWasm'; +import { explainTonTransaction, extractOutputs } from '../../src/lib/explainTransactionWasm'; import * as testData from '../resources/ton'; describe('TON WASM explainTransaction', function () { + describe('send mode semantics', function () { + const action = (mode: number, jettonTransfer?: { destination: string; amount: bigint }) => ({ + mode, + nominalAmount: 10n, + effectiveAmountKind: mode === 3 ? ('Exact' as const) : ('AllRemainingBalance' as const), + carriesInboundValue: false, + carriesAllBalance: mode !== 3, + destroyAccountIfZero: mode === 160, + destination: 'native-destination', + destinationBounceable: 'native-bounceable-destination', + ...(jettonTransfer ? { jettonTransfer } : {}), + }); + + it('uses nominalAmount for an exact mode-3 native send', function () { + const explained = extractOutputs({ sendActions: [action(3)] }, true); + + assert.deepStrictEqual(explained.outputs, [{ address: 'native-bounceable-destination', amount: '10' }]); + explained.outputAmount.should.equal('10'); + }); + + it('retains the jetton amount for an exact mode-3 token send', function () { + const explained = extractOutputs( + { sendActions: [action(3, { destination: 'jetton-destination', amount: 25n })] }, + true + ); + + assert.deepStrictEqual(explained.outputs, [{ address: 'jetton-destination', amount: '25' }]); + explained.outputAmount.should.equal('25'); + }); + + for (const mode of [128, 160]) { + it(`rejects mode ${mode} jetton sends before exposing token amounts`, function () { + const sendAction = action(mode, { destination: 'attacker', amount: 1n }); + + assert.throws(() => extractOutputs({ sendActions: [sendAction] }, true), /mode 3/); + }); + } + + it('rejects a destruction marker even when mode claims an exact amount', function () { + const actionWithDestruction = { ...action(3), destroyAccountIfZero: true }; + + assert.throws(() => extractOutputs({ sendActions: [actionWithDestruction] }, true), /mode 3/); + }); + + it('checks every action, including a dangerous later jetton action', function () { + const actions = [action(3), action(128, { destination: 'attacker', amount: 1n })]; + + assert.throws(() => extractOutputs({ sendActions: actions }, true), /mode 3/); + }); + }); + describe('explainTonTransaction', function () { it('should explain a signed send transaction', async function () { const txBase64 = testData.signedSendTransaction.tx; @@ -80,7 +132,8 @@ describe('TON WASM explainTransaction', function () { parsed.transactionType.should.equal('Transfer'); parsed.sendActions.length.should.be.greaterThan(0); - (typeof parsed.sendActions[0].amount).should.equal('bigint'); + const parsedAction = parsed.sendActions[0] as unknown as { nominalAmount?: bigint; amount?: bigint }; + (typeof (parsedAction.nominalAmount ?? parsedAction.amount)).should.equal('bigint'); parsed.seqno.should.be.a.Number(); (typeof parsed.expireAt).should.equal('bigint'); }); From 7c9696346e158c2e1ea79570e291b27d3671fb20 Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Thu, 24 Sep 2026 03:31:15 +0000 Subject: [PATCH 2/2] test(sdk-coin-ton): reject contradictory TON mode fields Cover carry-inbound and carry-all flags that conflict with an\notherwise exact mode, alongside conditional account destruction.\n\nThese divergences must fail closed before recipient output checks.\n\nTicket: WCN-1888 Session-Id: c3c1a2b6-ca94-47c4-81d9-b16cf4920171 Task-Id: aec26d89-87b2-4383-8624-e3f7972c32e7 --- .../test/unit/explainTransactionWasm.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts b/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts index 34020134d03..b6bba95955a 100644 --- a/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts +++ b/modules/sdk-coin-ton/test/unit/explainTransactionWasm.ts @@ -43,6 +43,22 @@ describe('TON WASM explainTransaction', function () { }); } + it('rejects carry semantics that conflict with an exact mode', function () { + const carriesInboundValue = { + ...action(3), + effectiveAmountKind: 'CarryInboundValue' as const, + carriesInboundValue: true, + }; + const carriesAllBalance = { + ...action(3), + effectiveAmountKind: 'AllRemainingBalance' as const, + carriesAllBalance: true, + }; + + assert.throws(() => extractOutputs({ sendActions: [carriesInboundValue] }, true), /mode 3/); + assert.throws(() => extractOutputs({ sendActions: [carriesAllBalance] }, true), /mode 3/); + }); + it('rejects a destruction marker even when mode claims an exact amount', function () { const actionWithDestruction = { ...action(3), destroyAccountIfZero: true };