From eb88e3123d57613e5f74dfcc949a1bd20a835f35 Mon Sep 17 00:00:00 2001 From: harman-bitgo Date: Wed, 11 Nov 2026 23:13:08 +0530 Subject: [PATCH] fix(sdk-coin-trx): validate consolidation destination in TransferContract and TriggerSmartContract validateTransferContract and validateTriggerSmartContract skipped destination validation when txParams.recipients was absent (consolidation flow), returning true after checking only that amount > 0. A compromised platform could return a consolidation prebuild sending funds to an attacker address, and verifyTransaction would sign it without inspecting the destination. Now: - When recipients is absent/empty, validate the destination matches the wallet's base address (via wallet.coinSpecific().baseAddress). - When recipients has >1 entry (sendMany/bulk withdrawal), validate the prebuild matches at least one recipient. - Extract shared validateConsolidationDestination helper for both paths. - Pass wallet from verifyTransaction down to both validators. TICKET: CHALO-2125 Co-Authored-By: Claude Opus 4.6 (1M context) --- modules/sdk-coin-trx/src/trx.ts | 81 ++- modules/sdk-coin-trx/src/trxToken.ts | 2 +- modules/sdk-coin-trx/test/unit/trxToken.ts | 16 +- .../test/unit/verifyTransaction.ts | 508 +++++++++++++++++- 4 files changed, 587 insertions(+), 20 deletions(-) diff --git a/modules/sdk-coin-trx/src/trx.ts b/modules/sdk-coin-trx/src/trx.ts index 16abaf4b04b..35ac0945d99 100644 --- a/modules/sdk-coin-trx/src/trx.ts +++ b/modules/sdk-coin-trx/src/trx.ts @@ -41,6 +41,11 @@ import { import { auditEcdsaPrivateKey } from '@bitgo/sdk-lib-mpc'; import { Enum, Interface, Utils, WrappedBuilder, KeyPair as TronKeyPair } from './lib'; import { ValueFields, TransactionReceipt, RawData } from './lib/iface'; + +/** Narrow wallet shape needed by consolidation destination validation. */ +interface WalletWithCoinSpecific { + coinSpecific?(): { baseAddress?: string; rootAddress?: string } | undefined; +} import { getBuilder } from './lib/builder'; import { isInteger, isUndefined } from 'lodash'; @@ -394,7 +399,7 @@ export class Trx extends BaseCoin { } async verifyTransaction(params: VerifyTransactionOptions): Promise { - const { txParams, txPrebuild, walletType } = params; + const { txParams, txPrebuild, walletType, wallet } = params; if (!txParams) { throw new Error('missing txParams'); @@ -425,7 +430,7 @@ export class Trx extends BaseCoin { if (!Array.isArray(decodedTx.contract) || decodedTx.contract.length !== 1) { throw new Error('Invalid Transfer contract structure.'); } - return this.validateTransferContract(decodedTx.contract[0], txParams, true); + return this.validateTransferContract(decodedTx.contract[0], txParams, wallet, true); } if (decodedTx.contractType === Enum.ContractType.TriggerSmartContract) { @@ -434,7 +439,7 @@ export class Trx extends BaseCoin { // trx: wallet), which routes their verification here, so validate the recipient // and amount instead of throwing (CECHO-2248). This is the same validation performed by // TrxToken.verifyTransaction and still fails closed on any mismatch. - return this.validateTriggerSmartContract(decodedTx, txParams, txPrebuild); + return this.validateTriggerSmartContract(decodedTx, txParams, txPrebuild, wallet); } return true; @@ -454,7 +459,7 @@ export class Trx extends BaseCoin { const contract = txJson.raw_data.contract[0]; if (contract.type === 'TransferContract') { - return this.validateTransferContract(contract, txParams); + return this.validateTransferContract(contract, txParams, wallet); } else { return true; } @@ -484,10 +489,16 @@ export class Trx extends BaseCoin { * * @param contract The contract object from the transaction * @param txParams The original transaction parameters + * @param wallet The wallet object, used to verify consolidation destinations * @param addressesInBase58 When true, addresses are already in base58 format (TSS path via protobuf decoding). * When false (default), addresses are in hex and need conversion (on-chain multisig path via builder JSON). */ - private validateTransferContract(contract: any, txParams: any, addressesInBase58 = false): boolean { + private validateTransferContract( + contract: any, + txParams: any, + wallet: WalletWithCoinSpecific | undefined, + addressesInBase58 = false + ): boolean { if (!('parameter' in contract) || !contract.parameter?.value) { throw new Error('Invalid Transfer contract structure'); } @@ -499,16 +510,17 @@ export class Trx extends BaseCoin { throw new Error('Invalid transfer amount'); } - // If txParams has recipients, validate against expected values + const actualDestination = addressesInBase58 + ? Utils.getBase58AddressFromHexAddress(value.to_address) + : Utils.getBase58AddressFromHex(value.to_address); + if (txParams.recipients && txParams.recipients.length === 1) { + // Client-supplied recipients: validate destination and amount match exactly const recipient = txParams.recipients[0]; const expectedAmount = recipient.amount.toString(); // Canonicalize client-supplied address (base58 / 0x... / 41...) before comparing (COINS-1575). const expectedDestination = Utils.getBase58AddressFromHexAddress(recipient.address); const actualAmount = value.amount.toString(); - const actualDestination = addressesInBase58 - ? Utils.getBase58AddressFromHexAddress(value.to_address) - : Utils.getBase58AddressFromHex(value.to_address); if (expectedAmount !== actualAmount) { throw new Error('transaction amount in txPrebuild does not match the value given by client'); @@ -517,24 +529,59 @@ export class Trx extends BaseCoin { if (expectedDestination.toLowerCase() !== actualDestination.toLowerCase()) { throw new Error('destination address does not match with the recipient address'); } + } else if (txParams.recipients && txParams.recipients.length > 1) { + // Bulk withdrawal (sendMany): the platform builds one TransferContract per recipient, + // so txParams.recipients has all entries but this prebuild matches only one. + // Verify the prebuild's destination + amount matches at least one recipient. + const actualAmount = value.amount.toString(); + const matched = txParams.recipients.some((recipient: Recipient) => { + const expectedDestination = Utils.getBase58AddressFromHexAddress(recipient.address); + const expectedAmount = recipient.amount.toString(); + return expectedAmount === actualAmount && expectedDestination.toLowerCase() === actualDestination.toLowerCase(); + }); + if (!matched) { + throw new Error('transaction in txPrebuild does not match any recipient in txParams'); + } + } else { + // No recipients (consolidation flow): destination must be a wallet-owned address (CHALO-2125) + this.validateConsolidationDestination(wallet, actualDestination); } return true; } + /** + * Validate that a consolidation destination is the wallet's own base address. + * Shared by both native TRX and TRC20 validation paths (CHALO-2125). + */ + private validateConsolidationDestination( + wallet: WalletWithCoinSpecific | undefined, + actualDestination: string + ): void { + const coinSpecific = wallet?.coinSpecific?.(); + const baseAddress = coinSpecific?.baseAddress || coinSpecific?.rootAddress; + if (!baseAddress) { + throw new Error('missing wallet base address for consolidation destination validation'); + } + if (Utils.getBase58AddressFromHexAddress(baseAddress).toLowerCase() !== actualDestination.toLowerCase()) { + throw new Error('consolidation destination address does not match wallet base address'); + } + } + /** * Validate a TriggerSmartContract (TRC20 token transfer) on the TSS path. * Shared by native Trx.verifyTransaction (which receives token transfers whose verification * was routed through the native coin) and TrxToken.verifyTransaction. * - * Always verifies contract structure and ABI decodability. Intent validation (address + amount - * comparison) is performed only when recipients are present — absent recipients indicates a - * server-determined transfer (e.g. consolidation) where the server owns the intent. + * Always verifies contract structure and ABI decodability. When recipients are present, + * validates destination and amount against client intent. When absent (consolidation), + * validates the destination is the wallet's base address (CHALO-2125). */ protected validateTriggerSmartContract( decodedTx: RawData, txParams: TransactionParams, - txPrebuild: BaseTransactionPrebuild + txPrebuild: BaseTransactionPrebuild, + wallet?: WalletWithCoinSpecific ): boolean { if (!Array.isArray(decodedTx.contract) || decodedTx.contract.length !== 1) { throw new Error('Invalid TriggerSmartContract structure'); @@ -555,19 +602,21 @@ export class Trx extends BaseCoin { throw new Error(`Failed to decode TRC20 transfer ABI data: ${e instanceof Error ? e.message : String(e)}`); } + // recipientHex has '41' hex prefix; convert to base58 for comparison. + const actualDestination = Utils.getBase58AddressFromHex(recipientHex); + const recipients = txParams.recipients || (txPrebuild.txInfo as { recipients?: Recipient[] } | undefined)?.recipients; if (!recipients || recipients.length === 0) { - // No recipients — server-determined transfer (e.g. consolidation); structural check above is sufficient. + // No recipients (consolidation flow): destination must be a wallet-owned address (CHALO-2125) + this.validateConsolidationDestination(wallet, actualDestination); return true; } if (recipients.length !== 1) { throw new Error('invalid required property recipients'); } - // recipientHex has '41' hex prefix; convert to base58 for comparison. // Canonicalize the client-supplied address too so 0x... / 41... match base58 outputs (COINS-1575). - const actualDestination = Utils.getBase58AddressFromHex(recipientHex); const actualAmount = transferAmount.toString(); const expectedDestination = Utils.getBase58AddressFromHexAddress(recipients[0].address); const expectedAmount = recipients[0].amount.toString(); diff --git a/modules/sdk-coin-trx/src/trxToken.ts b/modules/sdk-coin-trx/src/trxToken.ts index 98801bb7d15..00fc4e326f4 100644 --- a/modules/sdk-coin-trx/src/trxToken.ts +++ b/modules/sdk-coin-trx/src/trxToken.ts @@ -111,7 +111,7 @@ export class TrxToken extends Trx { ); } - return this.validateTriggerSmartContract(decodedTx, txParams, txPrebuild); + return this.validateTriggerSmartContract(decodedTx, txParams, txPrebuild, params.wallet); } const rawTx = txPrebuild.txHex; diff --git a/modules/sdk-coin-trx/test/unit/trxToken.ts b/modules/sdk-coin-trx/test/unit/trxToken.ts index d7358c6587f..2c940f7c247 100644 --- a/modules/sdk-coin-trx/test/unit/trxToken.ts +++ b/modules/sdk-coin-trx/test/unit/trxToken.ts @@ -105,15 +105,29 @@ describe('TrxToken verifyTransaction:', function () { ); }); - it('should return true when recipients is empty array (consolidation path)', async function () { + it('should return true when recipients is empty and destination matches wallet base address (consolidation path)', async function () { + const recipientBase58 = Utils.getBase58AddressFromHex(TRC20_RECIPIENT_HEX); const result = await tokenCoin.verifyTransaction({ txPrebuild: { txHex: TRC20_RAW_DATA_HEX }, txParams: { recipients: [] }, + wallet: { coinSpecific: () => ({ baseAddress: recipientBase58 }) }, walletType: 'tss', } as any); assert.strictEqual(result, true); }); + it('should fail consolidation when destination does not match wallet base address', async function () { + await assert.rejects( + tokenCoin.verifyTransaction({ + txPrebuild: { txHex: TRC20_RAW_DATA_HEX }, + txParams: { recipients: [] }, + wallet: { coinSpecific: () => ({ baseAddress: 'TLWh67P93KgtnZNCtGnEHM1H33Nhq2uvvN' }) }, + walletType: 'tss', + } as any), + { message: 'consolidation destination address does not match wallet base address' } + ); + }); + it('should throw when contract type is not TriggerSmartContract', async function () { // Use a native TRX Transfer protobuf as txHex — TrxToken only handles TriggerSmartContract const recipientBase58 = Utils.getBase58AddressFromHex(TRC20_RECIPIENT_HEX); diff --git a/modules/sdk-coin-trx/test/unit/verifyTransaction.ts b/modules/sdk-coin-trx/test/unit/verifyTransaction.ts index 8f0730d0c99..9ccad2c3b00 100644 --- a/modules/sdk-coin-trx/test/unit/verifyTransaction.ts +++ b/modules/sdk-coin-trx/test/unit/verifyTransaction.ts @@ -474,6 +474,337 @@ describe('TRON Verify Transaction:', function () { message: 'destination address does not match with the recipient address', }); }); + + it('should validate consolidation when destination matches wallet base address', async function () { + const timestamp = Date.now(); + const toAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const toAddressBase58 = Utils.getBase58AddressFromHex(toAddressHex); + const transferContract = { + parameter: { + value: { + amount: 1000000, + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + to_address: toAddressHex, + }, + type_url: 'type.googleapis.com/protocol.TransferContract', + }, + type: 'TransferContract', + }; + + const rawData = { + contract: [transferContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }; + + const transformedRawData = { + contract: rawData.contract as any, + refBlockBytes: rawData.ref_block_bytes, + refBlockHash: rawData.ref_block_hash, + expiration: rawData.expiration, + timestamp: rawData.timestamp, + feeLimit: rawData.fee_limit, + }; + + const rawDataHex = Utils.generateRawDataHex(transformedRawData); + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + + const params = { + txParams: {}, + txPrebuild: { + txHex: JSON.stringify({ + txID, + raw_data: rawData, + raw_data_hex: rawDataHex, + }), + }, + wallet: { coinSpecific: () => ({ baseAddress: toAddressBase58 }) }, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail consolidation when destination does not match wallet base address', async function () { + const timestamp = Date.now(); + const toAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const transferContract = { + parameter: { + value: { + amount: 1000000, + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + to_address: toAddressHex, + }, + type_url: 'type.googleapis.com/protocol.TransferContract', + }, + type: 'TransferContract', + }; + + const rawData = { + contract: [transferContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }; + + const transformedRawData = { + contract: rawData.contract as any, + refBlockBytes: rawData.ref_block_bytes, + refBlockHash: rawData.ref_block_hash, + expiration: rawData.expiration, + timestamp: rawData.timestamp, + feeLimit: rawData.fee_limit, + }; + + const rawDataHex = Utils.generateRawDataHex(transformedRawData); + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + + const params = { + txParams: {}, + txPrebuild: { + txHex: JSON.stringify({ + txID, + raw_data: rawData, + raw_data_hex: rawDataHex, + }), + }, + wallet: { coinSpecific: () => ({ baseAddress: 'TLWh67P93KgtnZNCtGnEHM1H33Nhq2uvvN' }) }, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'consolidation destination address does not match wallet base address', + }); + }); + + it('should validate consolidation using rootAddress fallback', async function () { + const timestamp = Date.now(); + const toAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const toAddressBase58 = Utils.getBase58AddressFromHex(toAddressHex); + const transferContract = { + parameter: { + value: { + amount: 1000000, + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + to_address: toAddressHex, + }, + type_url: 'type.googleapis.com/protocol.TransferContract', + }, + type: 'TransferContract', + }; + + const rawData = { + contract: [transferContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }; + + const transformedRawData = { + contract: rawData.contract as any, + refBlockBytes: rawData.ref_block_bytes, + refBlockHash: rawData.ref_block_hash, + expiration: rawData.expiration, + timestamp: rawData.timestamp, + feeLimit: rawData.fee_limit, + }; + + const rawDataHex = Utils.generateRawDataHex(transformedRawData); + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + + const params = { + txParams: {}, + txPrebuild: { + txHex: JSON.stringify({ + txID, + raw_data: rawData, + raw_data_hex: rawDataHex, + }), + }, + wallet: { coinSpecific: () => ({ rootAddress: toAddressBase58 }) }, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail consolidation when wallet base address is missing', async function () { + const timestamp = Date.now(); + const toAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const transferContract = { + parameter: { + value: { + amount: 1000000, + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + to_address: toAddressHex, + }, + type_url: 'type.googleapis.com/protocol.TransferContract', + }, + type: 'TransferContract', + }; + + const rawData = { + contract: [transferContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }; + + const transformedRawData = { + contract: rawData.contract as any, + refBlockBytes: rawData.ref_block_bytes, + refBlockHash: rawData.ref_block_hash, + expiration: rawData.expiration, + timestamp: rawData.timestamp, + feeLimit: rawData.fee_limit, + }; + + const rawDataHex = Utils.generateRawDataHex(transformedRawData); + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + + const params = { + txParams: {}, + txPrebuild: { + txHex: JSON.stringify({ + txID, + raw_data: rawData, + raw_data_hex: rawDataHex, + }), + }, + wallet: { coinSpecific: () => ({}) }, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'missing wallet base address for consolidation destination validation', + }); + }); + + it('should validate when prebuild matches one of multiple recipients (sendMany)', async function () { + const timestamp = Date.now(); + const toAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const toAddressBase58 = Utils.getBase58AddressFromHex(toAddressHex); + const transferContract = { + parameter: { + value: { + amount: 1000000, + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + to_address: toAddressHex, + }, + type_url: 'type.googleapis.com/protocol.TransferContract', + }, + type: 'TransferContract', + }; + + const rawData = { + contract: [transferContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }; + + const transformedRawData = { + contract: rawData.contract as any, + refBlockBytes: rawData.ref_block_bytes, + refBlockHash: rawData.ref_block_hash, + expiration: rawData.expiration, + timestamp: rawData.timestamp, + feeLimit: rawData.fee_limit, + }; + + const rawDataHex = Utils.generateRawDataHex(transformedRawData); + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + + const params = { + txParams: { + recipients: [ + { address: toAddressBase58, amount: '1000000' }, + { address: 'TQFxDSoXy2yXRE5HtKwAwrNRXGxYxkeSGk', amount: '500000' }, + ], + }, + txPrebuild: { + txHex: JSON.stringify({ + txID, + raw_data: rawData, + raw_data_hex: rawDataHex, + }), + }, + wallet: {}, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail when prebuild does not match any of multiple recipients', async function () { + const timestamp = Date.now(); + const toAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + // Use different hex addresses that don't match the prebuild destination + const otherAddress1 = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const otherAddress2 = '41c25420255c2c5a2dd54ef69f92ef261e6bd4216a'; + const transferContract = { + parameter: { + value: { + amount: 1000000, + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + to_address: toAddressHex, + }, + type_url: 'type.googleapis.com/protocol.TransferContract', + }, + type: 'TransferContract', + }; + + const rawData = { + contract: [transferContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }; + + const transformedRawData = { + contract: rawData.contract as any, + refBlockBytes: rawData.ref_block_bytes, + refBlockHash: rawData.ref_block_hash, + expiration: rawData.expiration, + timestamp: rawData.timestamp, + feeLimit: rawData.fee_limit, + }; + + const rawDataHex = Utils.generateRawDataHex(transformedRawData); + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + + const params = { + txParams: { + recipients: [ + { address: otherAddress1, amount: '500000' }, + { address: otherAddress2, amount: '500000' }, + ], + }, + txPrebuild: { + txHex: JSON.stringify({ + txID, + raw_data: rawData, + raw_data_hex: rawDataHex, + }), + }, + wallet: {}, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'transaction in txPrebuild does not match any recipient in txParams', + }); + }); }); }); @@ -597,6 +928,145 @@ describe('TRON Verify Transaction:', function () { message: 'destination address does not match with the recipient address', }); }); + + it('should validate TSS consolidation when destination matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const toHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const toBase58 = Utils.getBase58AddressFromHex(toHex); + const amount = 1000000; + + const rawDataHex = buildTssTransferTxHex({ ownerAddress: ownerHex, toAddress: toHex, amount }); + + const params = { + txParams: {}, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({ baseAddress: toBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail TSS consolidation when destination does not match wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const toHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const amount = 1000000; + + const rawDataHex = buildTssTransferTxHex({ ownerAddress: ownerHex, toAddress: toHex, amount }); + + const params = { + txParams: {}, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'consolidation destination address does not match wallet base address', + }); + }); + + it('should validate TSS consolidation using rootAddress fallback', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const toHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const toBase58 = Utils.getBase58AddressFromHex(toHex); + const amount = 1000000; + + const rawDataHex = buildTssTransferTxHex({ ownerAddress: ownerHex, toAddress: toHex, amount }); + + const params = { + txParams: {}, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({ rootAddress: toBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail TSS consolidation when wallet base address is missing', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const toHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const amount = 1000000; + + const rawDataHex = buildTssTransferTxHex({ ownerAddress: ownerHex, toAddress: toHex, amount }); + + const params = { + txParams: {}, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({}) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'missing wallet base address for consolidation destination validation', + }); + }); + + it('should validate TSS sendMany when prebuild matches one of multiple recipients', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const toHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const toBase58 = Utils.getBase58AddressFromHex(toHex); + const amount = 1000000; + + const rawDataHex = buildTssTransferTxHex({ ownerAddress: ownerHex, toAddress: toHex, amount }); + + const params = { + txParams: { + recipients: [ + { address: toBase58, amount: amount.toString() }, + { address: 'TQFxDSoXy2yXRE5HtKwAwrNRXGxYxkeSGk', amount: '500000' }, + ], + }, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: {}, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail TSS sendMany when prebuild does not match any recipient', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const toHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const amount = 1000000; + // Use different valid base58 addresses that don't match the prebuild destination + const otherAddress1 = Utils.getBase58AddressFromHex('4173a5993cd182ae152adad8203163f780c65a8aa5'); + const otherAddress2 = Utils.getBase58AddressFromHex('41c25420255c2c5a2dd54ef69f92ef261e6bd4216a'); + + const rawDataHex = buildTssTransferTxHex({ ownerAddress: ownerHex, toAddress: toHex, amount }); + + const params = { + txParams: { + recipients: [ + { address: otherAddress1, amount: '500000' }, + { address: otherAddress2, amount: '500000' }, + ], + }, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: {}, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'transaction in txPrebuild does not match any recipient in txParams', + }); + }); }); it('should return true for non-Transfer, non-TriggerSmartContract types in TSS', async function () { @@ -682,7 +1152,7 @@ describe('TRON Verify Transaction:', function () { }); }); - it('should return true when no recipients are provided (consolidation path)', async function () { + it('should return true when no recipients and destination is wallet base address (consolidation path)', async function () { const params = { txParams: { recipients: [], @@ -690,13 +1160,47 @@ describe('TRON Verify Transaction:', function () { txPrebuild: { txHex: trc20RawDataHex, }, - wallet: {}, + wallet: { coinSpecific: () => ({ baseAddress: trc20RecipientBase58 }) }, walletType: 'tss', }; const result = await basecoin.verifyTransaction(params); assert.strictEqual(result, true); }); + + it('should fail consolidation when TRC20 destination does not match wallet base address', async function () { + const params = { + txParams: { + recipients: [], + }, + txPrebuild: { + txHex: trc20RawDataHex, + }, + wallet: { coinSpecific: () => ({ baseAddress: 'TLWh67P93KgtnZNCtGnEHM1H33Nhq2uvvN' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'consolidation destination address does not match wallet base address', + }); + }); + + it('should fail consolidation when wallet base address is missing', async function () { + const params = { + txParams: { + recipients: [], + }, + txPrebuild: { + txHex: trc20RawDataHex, + }, + wallet: { coinSpecific: () => ({}) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'missing wallet base address for consolidation destination validation', + }); + }); }); it('should throw error when txHex is missing for TSS wallet', async function () {