From a6b637ae978353542e30fe4b04a891064eb23a8e Mon Sep 17 00:00:00 2001 From: harman-bitgo Date: Thu, 12 Nov 2026 12:58:03 +0530 Subject: [PATCH] fix(sdk-coin-trx): fail closed on unvalidated contract types in verifyTransaction Previously, verifyTransaction only validated TransferContract (and TriggerSmartContract on TSS), letting all other contract types pass with `return true`. This meant dangerous operations like AccountPermissionUpdate could be signed without any verification. Changes: - TSS path: validate owner_address for all 9 non-transfer contract types, throw on unknown types instead of returning true - Multisig path: add TriggerSmartContract recipient/amount validation (was completely unverified), validate owner_address for all 9 non-transfer contract types, throw on unknown types - Refactor error messages: context-aware messages distinguish consolidation destination vs contract owner_address validation - Extract shared allowlists (OWNER_VALIDATED_CONTRACT_TYPES and OWNER_VALIDATED_MULTISIG_CONTRACT_TYPES) to keep TSS/multisig in sync - Replace `any` types with minimal typed interfaces - Comprehensive test coverage: 65 verifyTransaction tests covering all contract types on both TSS and multisig paths, including rootAddress fallback Note: only owner_address is validated for non-transfer contracts. Fields like receiver_address (DelegateResource), votes (VoteWitness), or permission keys (AccountPermissionUpdate) are not checked because txParams does not yet carry structured intent for these operations. This is intentional and documented. TICKET: CHALO-2126 Co-Authored-By: Claude Opus 4.6 (1M context) --- modules/sdk-coin-trx/src/trx.ts | 134 ++- modules/sdk-coin-trx/test/unit/trxToken.ts | 2 +- .../test/unit/verifyTransaction.ts | 899 +++++++++++++++++- 3 files changed, 1005 insertions(+), 30 deletions(-) diff --git a/modules/sdk-coin-trx/src/trx.ts b/modules/sdk-coin-trx/src/trx.ts index 35ac0945d9..c38b755a2d 100644 --- a/modules/sdk-coin-trx/src/trx.ts +++ b/modules/sdk-coin-trx/src/trx.ts @@ -49,6 +49,35 @@ interface WalletWithCoinSpecific { import { getBuilder } from './lib/builder'; import { isInteger, isUndefined } from 'lodash'; +/** + * Contract types that are validated by checking owner_address against the wallet's base address. + * These are kept in sync: OWNER_VALIDATED_CONTRACT_TYPES (TSS/numeric enum) and + * OWNER_VALIDATED_MULTISIG_CONTRACT_TYPES (multisig/string) cover the same set. + */ +const OWNER_VALIDATED_CONTRACT_TYPES: Enum.ContractType[] = [ + Enum.ContractType.FreezeBalanceV2, + Enum.ContractType.UnfreezeBalanceV2, + Enum.ContractType.WithdrawExpireUnfreeze, + Enum.ContractType.VoteWitness, + Enum.ContractType.WithdrawBalance, + Enum.ContractType.DelegateResourceContract, + Enum.ContractType.UnDelegateResourceContract, + Enum.ContractType.AccountCreate, + Enum.ContractType.AccountPermissionUpdate, +]; + +const OWNER_VALIDATED_MULTISIG_CONTRACT_TYPES: string[] = [ + 'FreezeBalanceV2Contract', + 'UnfreezeBalanceV2Contract', + 'WithdrawExpireUnfreezeContract', + 'VoteWitnessContract', + 'WithdrawBalanceContract', + 'DelegateResourceContract', + 'UnDelegateResourceContract', + 'AccountCreateContract', + 'AccountPermissionUpdateContract', +]; + export const MINIMUM_TRON_MSIG_TRANSACTION_FEE = 1e6; export const SAFE_TRON_TRANSACTION_FEE = 2.1 * 1e6; // TRON foundation recommends 2.1 TRX as fees for guaranteed transaction export const SAFE_TRON_TOKEN_TRANSACTION_FEE = 100 * 1e6; // TRON foundation recommends 100 TRX as fees for guaranteed transaction @@ -442,7 +471,20 @@ export class Trx extends BaseCoin { return this.validateTriggerSmartContract(decodedTx, txParams, txPrebuild, wallet); } - return true; + // Staking, voting, resource management, account setup, and permission update contracts: + // These do not transfer funds to external recipients, but we must verify the owner_address + // belongs to this wallet to prevent signing transactions for other accounts (CHALO-2126). + // Note: only owner_address is validated here. Fields like receiver_address (DelegateResource), + // votes (VoteWitness), or new permission keys (AccountPermissionUpdate) are not checked because + // txParams does not yet carry structured intent for these operations. This is intentional and + // matches the current SDK contract — a follow-up may add deeper intent validation. + if (decodedTx.contractType !== undefined && OWNER_VALIDATED_CONTRACT_TYPES.includes(decodedTx.contractType)) { + return this.validateDecodedContractOwnerAddress(decodedTx, wallet); + } + + // Fail closed: unknown contract types must not be signed (CHALO-2126). + const typeName = decodedTx.contractType !== undefined ? Enum.ContractType[decodedTx.contractType] : 'undefined'; + throw new Error(`Unsupported contract type for TSS verification: ${typeName} (${decodedTx.contractType})`); } // On-chain multisig path: txHex is a full transaction JSON string (with txID, raw_data, raw_data_hex). @@ -460,9 +502,24 @@ export class Trx extends BaseCoin { if (contract.type === 'TransferContract') { return this.validateTransferContract(contract, txParams, wallet); - } else { - return true; } + + if (contract.type === 'TriggerSmartContract') { + // TRC20 token transfer on multisig path: decode and validate like the TSS path (CHALO-2126). + const rawDataHex = this.extractRawDataHex(rawTx); + const decodedTx = Utils.decodeTransaction(rawDataHex); + return this.validateTriggerSmartContract(decodedTx, txParams, txPrebuild, wallet); + } + + // Staking, voting, resource management, account setup, and permission update contracts: + // Validate owner_address belongs to this wallet (CHALO-2126). + // See OWNER_VALIDATED_CONTRACT_TYPES comment for why only owner_address is checked. + if (OWNER_VALIDATED_MULTISIG_CONTRACT_TYPES.includes(contract.type)) { + return this.validateMultisigContractOwnerAddress(contract, wallet); + } + + // Fail closed: unknown contract types must not be signed (CHALO-2126). + throw new Error(`Unsupported contract type for verification: ${contract.type}`); } /** @@ -550,6 +607,25 @@ export class Trx extends BaseCoin { return true; } + /** + * Validate that a given address matches the wallet's own base address. + * @param context Describes what is being validated (e.g. 'consolidation destination', 'contract owner') + */ + private validateAddressMatchesWallet( + wallet: WalletWithCoinSpecific | undefined, + actualAddress: string, + context: string + ): void { + const coinSpecific = wallet?.coinSpecific?.(); + const baseAddress = coinSpecific?.baseAddress || coinSpecific?.rootAddress; + if (!baseAddress) { + throw new Error(`missing wallet base address for ${context} validation`); + } + if (Utils.getBase58AddressFromHexAddress(baseAddress).toLowerCase() !== actualAddress.toLowerCase()) { + throw new Error(`${context} does not match wallet base address`); + } + } + /** * Validate that a consolidation destination is the wallet's own base address. * Shared by both native TRX and TRC20 validation paths (CHALO-2125). @@ -558,14 +634,54 @@ export class Trx extends BaseCoin { 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'); + this.validateAddressMatchesWallet(wallet, actualDestination, 'consolidation destination'); + } + + /** + * Validate that the owner_address of a decoded (TSS path) contract belongs to this wallet. + * Used for staking, voting, resource management, account setup, and permission update contracts + * that do not transfer funds to external recipients (CHALO-2126). + */ + private validateDecodedContractOwnerAddress(decodedTx: RawData, wallet: WalletWithCoinSpecific | undefined): boolean { + let ownerAddress: string | undefined; + + if (decodedTx.contractType === Enum.ContractType.AccountPermissionUpdate) { + // AccountPermissionUpdate decoded contract is a single object (not array-wrapped) + ownerAddress = (decodedTx.contract as unknown as Interface.AccountPermissionUpdateContract).ownerAddress; + } else { + // All other decoded contracts are arrays with parameter.value.owner_address + const contracts = decodedTx.contract as Array<{ parameter?: { value?: { owner_address?: string } } }>; + ownerAddress = contracts[0]?.parameter?.value?.owner_address; } - if (Utils.getBase58AddressFromHexAddress(baseAddress).toLowerCase() !== actualDestination.toLowerCase()) { - throw new Error('consolidation destination address does not match wallet base address'); + + if (!ownerAddress) { + throw new Error('Missing owner_address in contract'); } + + // Decoded addresses are already in base58 format; canonicalize for comparison. + const ownerBase58 = Utils.getBase58AddressFromHexAddress(ownerAddress); + this.validateAddressMatchesWallet(wallet, ownerBase58, 'contract owner_address'); + return true; + } + + /** + * Validate that the owner_address of a multisig-path (JSON) contract belongs to this wallet. + * Used for staking, voting, resource management, account setup, and permission update contracts + * on the on-chain multisig path (CHALO-2126). + */ + private validateMultisigContractOwnerAddress( + contract: { parameter?: { value?: { owner_address?: string } } }, + wallet: WalletWithCoinSpecific | undefined + ): boolean { + const ownerAddressHex = contract?.parameter?.value?.owner_address; + if (!ownerAddressHex) { + throw new Error('Missing owner_address in contract'); + } + + // Multisig path addresses are in hex format (41-prefixed); convert to base58. + const ownerBase58 = Utils.getBase58AddressFromHex(ownerAddressHex); + this.validateAddressMatchesWallet(wallet, ownerBase58, 'contract owner_address'); + return true; } /** diff --git a/modules/sdk-coin-trx/test/unit/trxToken.ts b/modules/sdk-coin-trx/test/unit/trxToken.ts index 2c940f7c24..e099f74c78 100644 --- a/modules/sdk-coin-trx/test/unit/trxToken.ts +++ b/modules/sdk-coin-trx/test/unit/trxToken.ts @@ -124,7 +124,7 @@ describe('TrxToken verifyTransaction:', function () { wallet: { coinSpecific: () => ({ baseAddress: 'TLWh67P93KgtnZNCtGnEHM1H33Nhq2uvvN' }) }, walletType: 'tss', } as any), - { message: 'consolidation destination address does not match wallet base address' } + { message: 'consolidation destination does not match wallet base address' } ); }); diff --git a/modules/sdk-coin-trx/test/unit/verifyTransaction.ts b/modules/sdk-coin-trx/test/unit/verifyTransaction.ts index 9ccad2c3b0..2e48c4cd45 100644 --- a/modules/sdk-coin-trx/test/unit/verifyTransaction.ts +++ b/modules/sdk-coin-trx/test/unit/verifyTransaction.ts @@ -6,6 +6,121 @@ import { TestBitGoAPI, TestBitGo } from '@bitgo/sdk-test'; import { Trx, Ttrx } from '../../src'; import { Utils } from '../../src/lib'; import { UnsignedBuildTransaction, UnsignedAccountPermissionUpdateContractTx } from '../resources'; +import { protocol } from '../../resources/protobuf/tron'; + +/** + * Helper to build a raw_data_hex (protobuf) for any supported contract type. + * Properly encodes the contract value using the protocol library. + */ +function buildTssContractTxHex(params: { + contractType: string; + contractValue: any; + timestamp?: number; + expiration?: number; +}): string { + const timestamp = params.timestamp || Date.now(); + + // Map contract type string to protobuf type_url and encoder + const encoders: Record Uint8Array }> = { + FreezeBalanceV2Contract: { + typeUrl: 'type.googleapis.com/protocol.FreezeBalanceV2Contract', + encode: (v) => + protocol.FreezeBalanceV2Contract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + frozenBalance: v.frozen_balance, + resource: v.resource === 'ENERGY' ? protocol.ResourceCode.ENERGY : protocol.ResourceCode.BANDWIDTH, + }).finish(), + }, + UnfreezeBalanceV2Contract: { + typeUrl: 'type.googleapis.com/protocol.UnfreezeBalanceV2Contract', + encode: (v) => + protocol.UnfreezeBalanceV2Contract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + unfreezeBalance: v.unfreeze_balance, + resource: v.resource === 'ENERGY' ? protocol.ResourceCode.ENERGY : protocol.ResourceCode.BANDWIDTH, + }).finish(), + }, + VoteWitnessContract: { + typeUrl: 'type.googleapis.com/protocol.VoteWitnessContract', + encode: (v) => + protocol.VoteWitnessContract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + votes: v.votes.map((vote: any) => ({ + voteAddress: Buffer.from(vote.vote_address, 'hex'), + voteCount: vote.vote_count, + })), + }).finish(), + }, + WithdrawBalanceContract: { + typeUrl: 'type.googleapis.com/protocol.WithdrawBalanceContract', + encode: (v) => + protocol.WithdrawBalanceContract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + }).finish(), + }, + WithdrawExpireUnfreezeContract: { + typeUrl: 'type.googleapis.com/protocol.WithdrawExpireUnfreezeContract', + encode: (v) => + protocol.WithdrawBalanceContract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + }).finish(), + }, + DelegateResourceContract: { + typeUrl: 'type.googleapis.com/protocol.DelegateResourceContract', + encode: (v) => + protocol.DelegateResourceContract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + receiverAddress: Buffer.from(v.receiver_address, 'hex'), + balance: v.balance, + resource: v.resource === 'ENERGY' ? protocol.ResourceCode.ENERGY : protocol.ResourceCode.BANDWIDTH, + }).finish(), + }, + UnDelegateResourceContract: { + typeUrl: 'type.googleapis.com/protocol.UnDelegateResourceContract', + encode: (v) => + protocol.UnDelegateResourceContract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + receiverAddress: Buffer.from(v.receiver_address, 'hex'), + balance: v.balance, + resource: v.resource === 'ENERGY' ? protocol.ResourceCode.ENERGY : protocol.ResourceCode.BANDWIDTH, + }).finish(), + }, + AccountCreateContract: { + typeUrl: 'type.googleapis.com/protocol.AccountCreateContract', + encode: (v) => + protocol.AccountCreateContract.encode({ + ownerAddress: Buffer.from(v.owner_address, 'hex'), + accountAddress: Buffer.from(v.account_address, 'hex'), + }).finish(), + }, + }; + + const encoder = encoders[params.contractType]; + if (!encoder) { + throw new Error(`No encoder for contract type: ${params.contractType}`); + } + + const encodedValue = encoder.encode(params.contractValue); + const base64Value = Buffer.from(encodedValue).toString('base64'); + + const contract = { + parameter: { + value: base64Value, + type_url: encoder.typeUrl, + }, + type: params.contractType, + }; + + const transformedRawData = { + contract: [contract] as any, + refBlockBytes: 'c8cf', + refBlockHash: '89177fd84c5d9196', + expiration: params.expiration || timestamp + 3600000, + timestamp: timestamp, + }; + + return Utils.generateRawDataHex(transformedRawData); +} describe('TRON Verify Transaction:', function () { const bitgo: TestBitGoAPI = TestBitGo.decorate(BitGoAPI, { env: 'test' }); @@ -577,7 +692,7 @@ describe('TRON Verify Transaction:', function () { }; await assert.rejects(basecoin.verifyTransaction(params), { - message: 'consolidation destination address does not match wallet base address', + message: 'consolidation destination does not match wallet base address', }); }); @@ -806,6 +921,323 @@ describe('TRON Verify Transaction:', function () { }); }); }); + + describe('Multisig path owner address validation (CHALO-2126)', () => { + it('should validate AccountPermissionUpdateContract when owner matches wallet base address', async function () { + const params = { + txParams: {}, + txPrebuild: { + txHex: JSON.stringify(UnsignedAccountPermissionUpdateContractTx), + }, + wallet: { + coinSpecific: () => ({ + baseAddress: Utils.getBase58AddressFromHex('41ca42a499a2eee394323baa2c2f7d8a764ce0689c'), + }), + }, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail AccountPermissionUpdateContract when owner does not match wallet base address', async function () { + const params = { + txParams: {}, + txPrebuild: { + txHex: JSON.stringify(UnsignedAccountPermissionUpdateContractTx), + }, + wallet: { + coinSpecific: () => ({ baseAddress: 'TLWh67P93KgtnZNCtGnEHM1H33Nhq2uvvN' }), + }, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should validate FreezeBalanceV2Contract on multisig path when owner matches', async function () { + const ownerHex = '41ca42a499a2eee394323baa2c2f7d8a764ce0689c'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + const timestamp = Date.now(); + const expiration = timestamp + 3600000; + + const freezeContract = { + parameter: { + value: { + frozen_balance: 1000000, + owner_address: ownerHex, + resource: 'BANDWIDTH', + }, + type_url: 'type.googleapis.com/protocol.FreezeBalanceV2Contract', + }, + type: 'FreezeBalanceV2Contract', + }; + + // Use same timestamp/expiration for both raw_data and raw_data_hex to avoid builder mismatch + const rawDataHex = buildTssContractTxHex({ + contractType: 'FreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, frozen_balance: 1000000, resource: 'BANDWIDTH' }, + timestamp, + expiration, + }); + + const rawData = { + contract: [freezeContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration, + timestamp, + fee_limit: 150000000, + }; + + 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: ownerBase58 }) }, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail FreezeBalanceV2Contract on multisig path when owner does not match', async function () { + const ownerHex = '41ca42a499a2eee394323baa2c2f7d8a764ce0689c'; + const timestamp = Date.now(); + const expiration = timestamp + 3600000; + + const freezeContract = { + parameter: { + value: { + frozen_balance: 1000000, + owner_address: ownerHex, + resource: 'BANDWIDTH', + }, + type_url: 'type.googleapis.com/protocol.FreezeBalanceV2Contract', + }, + type: 'FreezeBalanceV2Contract', + }; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'FreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, frozen_balance: 1000000, resource: 'BANDWIDTH' }, + timestamp, + expiration, + }); + + const rawData = { + contract: [freezeContract], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration, + timestamp, + fee_limit: 150000000, + }; + + 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: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + describe('Multisig TriggerSmartContract validation (CHALO-2126)', () => { + // TRC20 transfers on multisig path were previously unvalidated (return true). + // Now they decode protobuf and validate recipient + amount. + // The multisig path uses extractRawDataHex to get raw_data_hex from JSON, + // then decodes via protobuf. We build a fresh TRC20 tx with valid timestamps + // to avoid builder expiration errors. + const trc20OwnerHex = '41c51fbeea78910b15b1d3e8a9b62914ca94d1a4ac'; + const trc20ContractHex = '4142a1e39aefa49290f2b3f9ed688d7cecf86cd6e0'; + const trc20RecipientHex = '418483618ca85c35a9b923d98bebca718f5a1db279'; + const trc20RecipientBase58 = Utils.getBase58AddressFromHex(trc20RecipientHex); + const trc20Amount = '100000000'; + + function buildMultisigTrc20TxHex(): string { + const timestamp = Date.now(); + // ABI-encode transfer(address,uint256): function selector + padded address + padded amount + const transferSelector = 'a9059cbb'; + // Strip '41' prefix, pad to 32 bytes + const paddedRecipient = '000000000000000000000000' + trc20RecipientHex.slice(2); + const paddedAmount = BigInt(trc20Amount).toString(16).padStart(64, '0'); + const abiData = transferSelector + paddedRecipient + paddedAmount; + + const triggerContract = protocol.TriggerSmartContract.encode({ + ownerAddress: Buffer.from(trc20OwnerHex, 'hex'), + contractAddress: Buffer.from(trc20ContractHex, 'hex'), + data: Buffer.from(abiData, 'hex'), + }).finish(); + + const contract = { + parameter: { + value: Buffer.from(triggerContract).toString('base64'), + type_url: 'type.googleapis.com/protocol.TriggerSmartContract', + }, + type: 'TriggerSmartContract', + }; + + const rawDataHex = Utils.generateRawDataHex({ + contract: [contract] as any, + refBlockBytes: 'c8cf', + refBlockHash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + feeLimit: 150000000, + }); + + const txID = createHash('sha256').update(Buffer.from(rawDataHex, 'hex')).digest('hex'); + return JSON.stringify({ + txID, + raw_data: { + contract: [ + { + parameter: { + value: { + data: abiData, + owner_address: trc20OwnerHex, + contract_address: trc20ContractHex, + }, + type_url: 'type.googleapis.com/protocol.TriggerSmartContract', + }, + type: 'TriggerSmartContract', + }, + ], + ref_block_bytes: 'c8cf', + ref_block_hash: '89177fd84c5d9196', + expiration: timestamp + 3600000, + timestamp: timestamp, + fee_limit: 150000000, + }, + raw_data_hex: rawDataHex, + }); + } + + it('should validate multisig TRC20 when recipient matches', async function () { + const params = { + txParams: { + recipients: [{ address: trc20RecipientBase58, amount: trc20Amount }], + }, + txPrebuild: { txHex: buildMultisigTrc20TxHex() }, + wallet: {}, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail multisig TRC20 when amount does not match', async function () { + const params = { + txParams: { + recipients: [{ address: trc20RecipientBase58, amount: '999' }], + }, + txPrebuild: { txHex: buildMultisigTrc20TxHex() }, + wallet: {}, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'transaction amount in txPrebuild does not match the value given by client', + }); + }); + + it('should fail multisig TRC20 when destination does not match', async function () { + const params = { + txParams: { + recipients: [{ address: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz', amount: trc20Amount }], + }, + txPrebuild: { txHex: buildMultisigTrc20TxHex() }, + wallet: {}, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'destination address does not match with the recipient address', + }); + }); + + it('should validate multisig TRC20 consolidation when destination matches wallet base address', async function () { + const params = { + txParams: { recipients: [] }, + txPrebuild: { txHex: buildMultisigTrc20TxHex() }, + wallet: { coinSpecific: () => ({ baseAddress: trc20RecipientBase58 }) }, + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail multisig TRC20 consolidation when destination does not match wallet base address', async function () { + const params = { + txParams: { recipients: [] }, + txPrebuild: { txHex: buildMultisigTrc20TxHex() }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'consolidation destination does not match wallet base address', + }); + }); + }); + + it('should fail unknown contract type on multisig path', async function () { + const timestamp = Date.now(); + const unknownContract = { + parameter: { + value: { + owner_address: '4173a5993cd182ae152adad8203163f780c65a8aa5', + }, + type_url: 'type.googleapis.com/protocol.SomeUnknownContract', + }, + type: 'SomeUnknownContract', + }; + + const rawData = { + contract: [unknownContract], + 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: {}, + }; + + // The builder or decode step should reject unknown contract types + await assert.rejects(basecoin.verifyTransaction(params)); + }); + }); }); describe('TSS Wallet Verification', () => { @@ -967,7 +1399,7 @@ describe('TRON Verify Transaction:', function () { }; await assert.rejects(basecoin.verifyTransaction(params), { - message: 'consolidation destination address does not match wallet base address', + message: 'consolidation destination does not match wallet base address', }); }); @@ -1069,24 +1501,451 @@ describe('TRON Verify Transaction:', function () { }); }); - it('should return true for non-Transfer, non-TriggerSmartContract types in TSS', async function () { - // AccountPermissionUpdate and other native TRX contracts (freeze, vote, etc.) pass through - // without recipient validation in the TSS path. - const rawDataHex = UnsignedAccountPermissionUpdateContractTx.raw_data_hex; + describe('Owner address validation for non-transfer contract types (CHALO-2126)', () => { + it('should validate AccountPermissionUpdate when owner matches wallet base address', async function () { + const rawDataHex = UnsignedAccountPermissionUpdateContractTx.raw_data_hex; + // owner_address in the test fixture is 41ca42a499a2eee394323baa2c2f7d8a764ce0689c + const ownerBase58 = Utils.getBase58AddressFromHex('41ca42a499a2eee394323baa2c2f7d8a764ce0689c'); - const params = { - txParams: { - recipients: [], - }, - txPrebuild: { - txHex: rawDataHex, - }, - wallet: {}, - walletType: 'tss', - }; + const params = { + txParams: { + recipients: [], + }, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail AccountPermissionUpdate when owner does not match wallet base address', async function () { + const rawDataHex = UnsignedAccountPermissionUpdateContractTx.raw_data_hex; + + const params = { + txParams: { + recipients: [], + }, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should fail AccountPermissionUpdate when wallet base address is missing', async function () { + const rawDataHex = UnsignedAccountPermissionUpdateContractTx.raw_data_hex; + + const params = { + txParams: { + recipients: [], + }, + txPrebuild: { + txHex: rawDataHex, + }, + wallet: { coinSpecific: () => ({}) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'missing wallet base address for contract owner_address validation', + }); + }); + + it('should validate FreezeBalanceV2 when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + + const rawDataHex = buildTssContractTxHex({ + contractType: 'FreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, frozen_balance: 1000000, resource: 'BANDWIDTH' }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail FreezeBalanceV2 when owner does not match wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'FreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, frozen_balance: 1000000, resource: 'BANDWIDTH' }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; - const result = await basecoin.verifyTransaction(params); - assert.strictEqual(result, true); + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should validate VoteWitness when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + const voteAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'VoteWitnessContract', + contractValue: { + owner_address: ownerHex, + votes: [{ vote_address: voteAddressHex, vote_count: 100 }], + }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail VoteWitness when owner does not match wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const voteAddressHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'VoteWitnessContract', + contractValue: { + owner_address: ownerHex, + votes: [{ vote_address: voteAddressHex, vote_count: 100 }], + }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should validate DelegateResourceContract when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + const receiverHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'DelegateResourceContract', + contractValue: { + owner_address: ownerHex, + receiver_address: receiverHex, + balance: 1000000, + resource: 'BANDWIDTH', + }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail DelegateResourceContract when owner does not match wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const receiverHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'DelegateResourceContract', + contractValue: { + owner_address: ownerHex, + receiver_address: receiverHex, + balance: 1000000, + resource: 'BANDWIDTH', + }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should validate WithdrawBalanceContract when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + + const rawDataHex = buildTssContractTxHex({ + contractType: 'WithdrawBalanceContract', + contractValue: { owner_address: ownerHex }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should validate UnDelegateResourceContract when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + const receiverHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'UnDelegateResourceContract', + contractValue: { + owner_address: ownerHex, + receiver_address: receiverHex, + balance: 1000000, + resource: 'BANDWIDTH', + }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should validate UnfreezeBalanceV2 when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + + const rawDataHex = buildTssContractTxHex({ + contractType: 'UnfreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, unfreeze_balance: 1000000, resource: 'BANDWIDTH' }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should validate WithdrawExpireUnfreeze when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + + const rawDataHex = buildTssContractTxHex({ + contractType: 'WithdrawExpireUnfreezeContract', + contractValue: { owner_address: ownerHex }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should validate AccountCreate when owner matches wallet base address', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + const accountHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + + const rawDataHex = buildTssContractTxHex({ + contractType: 'AccountCreateContract', + contractValue: { owner_address: ownerHex, account_address: accountHex }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail WithdrawBalanceContract when owner does not match', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const rawDataHex = buildTssContractTxHex({ + contractType: 'WithdrawBalanceContract', + contractValue: { owner_address: ownerHex }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should fail UnDelegateResourceContract when owner does not match', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const receiverHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const rawDataHex = buildTssContractTxHex({ + contractType: 'UnDelegateResourceContract', + contractValue: { + owner_address: ownerHex, + receiver_address: receiverHex, + balance: 1000000, + resource: 'BANDWIDTH', + }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should fail UnfreezeBalanceV2 when owner does not match', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const rawDataHex = buildTssContractTxHex({ + contractType: 'UnfreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, unfreeze_balance: 1000000, resource: 'BANDWIDTH' }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should fail WithdrawExpireUnfreeze when owner does not match', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const rawDataHex = buildTssContractTxHex({ + contractType: 'WithdrawExpireUnfreezeContract', + contractValue: { owner_address: ownerHex }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should fail AccountCreate when owner does not match', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const accountHex = '41d6cd6a2c0ff35a319e6abb5b9503ba0278679882'; + const rawDataHex = buildTssContractTxHex({ + contractType: 'AccountCreateContract', + contractValue: { owner_address: ownerHex, account_address: accountHex }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'contract owner_address does not match wallet base address', + }); + }); + + it('should validate owner using rootAddress fallback', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const ownerBase58 = Utils.getBase58AddressFromHex(ownerHex); + const rawDataHex = buildTssContractTxHex({ + contractType: 'FreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, frozen_balance: 1000000, resource: 'BANDWIDTH' }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: { coinSpecific: () => ({ rootAddress: ownerBase58 }) }, + walletType: 'tss', + }; + + const result = await basecoin.verifyTransaction(params); + assert.strictEqual(result, true); + }); + + it('should fail when wallet has no coinSpecific for non-transfer types', async function () { + const ownerHex = '4173a5993cd182ae152adad8203163f780c65a8aa5'; + const rawDataHex = buildTssContractTxHex({ + contractType: 'FreezeBalanceV2Contract', + contractValue: { owner_address: ownerHex, frozen_balance: 1000000, resource: 'BANDWIDTH' }, + }); + + const params = { + txParams: {}, + txPrebuild: { txHex: rawDataHex }, + wallet: {}, + walletType: 'tss', + }; + + await assert.rejects(basecoin.verifyTransaction(params), { + message: 'missing wallet base address for contract owner_address validation', + }); + }); }); describe('TriggerSmartContract (TRC20) routed through native Trx', () => { @@ -1176,12 +2035,12 @@ describe('TRON Verify Transaction:', function () { txPrebuild: { txHex: trc20RawDataHex, }, - wallet: { coinSpecific: () => ({ baseAddress: 'TLWh67P93KgtnZNCtGnEHM1H33Nhq2uvvN' }) }, + wallet: { coinSpecific: () => ({ baseAddress: 'TTsGwnTLQ4eryFJpDvJSfuGQxPXRCjXvZz' }) }, walletType: 'tss', }; await assert.rejects(basecoin.verifyTransaction(params), { - message: 'consolidation destination address does not match wallet base address', + message: 'consolidation destination does not match wallet base address', }); });