From b968966d15bf1bb1c472c2a6b47bad453ec53f58 Mon Sep 17 00:00:00 2001 From: Marzooqa Kather Date: Mon, 3 Aug 2026 10:23:21 +0000 Subject: [PATCH 1/3] fix(bsc): remove weak verifyTssTransaction override, inherit base class Remove the stub verifyTssTransaction overrides from Bsc and BscToken that unconditionally returned true after only shallow presence checks. Both classes now inherit AbstractEthLikeNewCoins.verifyTssTransaction, which decodes txHex and validates native-BNB and BEP-20 transfers against the declared recipients. The original override was added in commit 3d294365bd (COIN-3222, May 2025) to unblock a txHex-decoding crash in the transaction builder. That issue is no longer present, so the bypass can be removed. Add regression tests confirming: - Native BNB TSS transfer with matching recipient passes - Native BNB TSS transfer with mismatched recipient throws - BEP-20 TSS token transfer with matching calldata recipient passes - BEP-20 TSS token transfer with mismatched calldata recipient throws Ticket: WCI-1169 Session-Id: 4ade7ff7-085e-476d-b280-f4d3dd01c105 Task-Id: 9e3fa483-2a00-47db-aa04-c64b6f003a77 --- modules/sdk-coin-bsc/src/bsc.ts | 38 +------- modules/sdk-coin-bsc/src/bscToken.ts | 35 +------- modules/sdk-coin-bsc/test/unit/bsc.ts | 121 ++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 70 deletions(-) diff --git a/modules/sdk-coin-bsc/src/bsc.ts b/modules/sdk-coin-bsc/src/bsc.ts index 9b3e255500..ebc836bc19 100644 --- a/modules/sdk-coin-bsc/src/bsc.ts +++ b/modules/sdk-coin-bsc/src/bsc.ts @@ -5,14 +5,9 @@ import { MPCAlgorithm, MultisigType, multisigTypes, - NO_RECIPIENT_TX_TYPES, } from '@bitgo/sdk-core'; import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics'; -import { - AbstractEthLikeNewCoins, - recoveryBlockchainExplorerQuery, - VerifyEthTransactionOptions, -} from '@bitgo/abstract-eth'; +import { AbstractEthLikeNewCoins, recoveryBlockchainExplorerQuery } from '@bitgo/abstract-eth'; import { TransactionBuilder } from './lib'; export class Bsc extends AbstractEthLikeNewCoins { @@ -63,35 +58,4 @@ export class Bsc extends AbstractEthLikeNewCoins { return await recoveryBlockchainExplorerQuery(query, explorerUrl as string, apiToken); } - /** - * Verify if a tss transaction is valid - * - * @param {VerifyEthTransactionOptions} params - * @param {TransactionParams} params.txParams - params object passed to send - * @param {TransactionPrebuild} params.txPrebuild - prebuild object returned by server - * @param {Wallet} params.wallet - Wallet object to obtain keys to verify against - * @returns {boolean} - */ - async verifyTssTransaction(params: VerifyEthTransactionOptions): Promise { - const { txParams, txPrebuild, wallet } = params; - if ( - !txParams?.recipients && - !( - txParams.prebuildTx?.consolidateId || - txParams.stakingRequestId || - txParams.prebuildTx?.stakingRequestId || - (txParams.type && NO_RECIPIENT_TX_TYPES.has(txParams.type)) - ) - ) { - throw new Error(`missing txParams`); - } - if (!wallet || !txPrebuild) { - throw new Error(`missing params`); - } - if (txParams.hop && txParams.recipients && txParams.recipients.length > 1) { - throw new Error(`tx cannot be both a batch and hop transaction`); - } - - return true; - } } diff --git a/modules/sdk-coin-bsc/src/bscToken.ts b/modules/sdk-coin-bsc/src/bscToken.ts index b2923cd137..c015368b86 100644 --- a/modules/sdk-coin-bsc/src/bscToken.ts +++ b/modules/sdk-coin-bsc/src/bscToken.ts @@ -3,8 +3,8 @@ */ import { EthLikeTokenConfig, coins } from '@bitgo/statics'; -import { BitGoBase, CoinConstructor, NamedCoinConstructor, MPCAlgorithm, NO_RECIPIENT_TX_TYPES } from '@bitgo/sdk-core'; -import { CoinNames, EthLikeToken, VerifyEthTransactionOptions } from '@bitgo/abstract-eth'; +import { BitGoBase, CoinConstructor, NamedCoinConstructor, MPCAlgorithm } from '@bitgo/sdk-core'; +import { CoinNames, EthLikeToken } from '@bitgo/abstract-eth'; import { TransactionBuilder } from './lib'; export { EthLikeTokenConfig }; @@ -43,35 +43,4 @@ export class BscToken extends EthLikeToken { getFullName(): string { return 'Bsc Token'; } - /** - * Verify if a tss transaction is valid - * - * @param {VerifyEthTransactionOptions} params - * @param {TransactionParams} params.txParams - params object passed to send - * @param {TransactionPrebuild} params.txPrebuild - prebuild object returned by server - * @param {Wallet} params.wallet - Wallet object to obtain keys to verify against - * @returns {boolean} - */ - async verifyTssTransaction(params: VerifyEthTransactionOptions): Promise { - const { txParams, txPrebuild, wallet } = params; - if ( - !txParams?.recipients && - !( - txParams.prebuildTx?.consolidateId || - txParams.stakingRequestId || - txParams.prebuildTx?.stakingRequestId || - (txParams.type && NO_RECIPIENT_TX_TYPES.has(txParams.type)) - ) - ) { - throw new Error(`missing txParams`); - } - if (!wallet || !txPrebuild) { - throw new Error(`missing params`); - } - if (txParams.hop && txParams.recipients && txParams.recipients.length > 1) { - throw new Error(`tx cannot be both a batch and hop transaction`); - } - - return true; - } } diff --git a/modules/sdk-coin-bsc/test/unit/bsc.ts b/modules/sdk-coin-bsc/test/unit/bsc.ts index 6c80d6047b..0269ee91ef 100644 --- a/modules/sdk-coin-bsc/test/unit/bsc.ts +++ b/modules/sdk-coin-bsc/test/unit/bsc.ts @@ -2,8 +2,12 @@ import 'should'; import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; import { BitGoAPI } from '@bitgo/sdk-api'; +import { TransactionType, Wallet } from '@bitgo/sdk-core'; +import EthereumAbi from 'ethereumjs-abi'; import { Bsc, Tbsc } from '../../src/index'; +import { TransactionBuilder } from '../../src/lib'; +import { getBuilder } from './getBuilder'; const bitgo: TestBitGoAPI = TestBitGo.decorate(BitGoAPI, { env: 'test' }); @@ -39,4 +43,121 @@ describe('Native BNB', function () { tbsc.allowsAccountConsolidations().should.equal(true); }); }); + + describe('verifyTssTransaction', function () { + const recipientAddress = '0x174cfd823af8ce27ed0afee3fcf3c3ba259116be'; + const wrongAddress = '0x7e85bdc27c050e3905ebf4b8e634d9ad6edd0de6'; + const tokenContractAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'; + const transferAmount = '1000000000000000000'; + + it('should accept a native BNB transfer where txHex matches declared recipient', async function () { + const coin = bitgo.coin('tbsc') as Tbsc; + + const txBuilder = getBuilder('tbsc') as TransactionBuilder; + txBuilder.type(TransactionType.SingleSigSend); + txBuilder.fee({ fee: '10', gasLimit: '21000' }); + txBuilder.counter(1); + txBuilder.contract(recipientAddress); + txBuilder.value(transferAmount); + const tx = await txBuilder.build(); + const txHex = tx.toBroadcastFormat(); + + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } }); + + const result = await coin.verifyTssTransaction({ + txParams: { + type: 'transfer', + recipients: [{ address: recipientAddress, amount: transferAmount }], + } as any, + txPrebuild: { txHex, coin: 'tbsc', walletId: 'fakeWalletId' } as any, + wallet, + }); + result.should.equal(true); + }); + + it('should reject a native BNB transfer when txHex recipient does not match declared recipient', async function () { + const coin = bitgo.coin('tbsc') as Tbsc; + + const txBuilder = getBuilder('tbsc') as TransactionBuilder; + txBuilder.type(TransactionType.SingleSigSend); + txBuilder.fee({ fee: '10', gasLimit: '21000' }); + txBuilder.counter(1); + txBuilder.contract(wrongAddress); + txBuilder.value(transferAmount); + const tx = await txBuilder.build(); + const txHex = tx.toBroadcastFormat(); + + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } }); + + await coin + .verifyTssTransaction({ + txParams: { + type: 'transfer', + recipients: [{ address: recipientAddress, amount: transferAmount }], + } as any, + txPrebuild: { txHex, coin: 'tbsc', walletId: 'fakeWalletId' } as any, + wallet, + }) + .should.be.rejectedWith('destination address does not match with the recipient address'); + }); + + it('should accept a BEP-20 token transfer where calldata matches declared recipient', async function () { + const coin = bitgo.coin('tbsc') as Tbsc; + + const methodId = EthereumAbi.methodID('transfer', ['address', 'uint256']); + const encodedParams = EthereumAbi.rawEncode(['address', 'uint256'], [recipientAddress, '10000000']); + const erc20TransferData = '0x' + Buffer.concat([methodId, encodedParams]).toString('hex'); + + const txBuilder = getBuilder('tbsc') as TransactionBuilder; + txBuilder.type(TransactionType.ContractCall); + txBuilder.fee({ fee: '10', gasLimit: '60000' }); + txBuilder.counter(1); + txBuilder.contract(tokenContractAddress); + txBuilder.data(erc20TransferData); + const tx = await txBuilder.build(); + const txHex = tx.toBroadcastFormat(); + + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } }); + + const result = await coin.verifyTssTransaction({ + txParams: { + type: 'transfer', + recipients: [{ address: recipientAddress, amount: '10000000' }], + } as any, + txPrebuild: { txHex, coin: 'tbsc', walletId: 'fakeWalletId' } as any, + wallet, + }); + result.should.equal(true); + }); + + it('should reject a BEP-20 token transfer when calldata recipient does not match declared recipient', async function () { + const coin = bitgo.coin('tbsc') as Tbsc; + + const methodId = EthereumAbi.methodID('transfer', ['address', 'uint256']); + const encodedParams = EthereumAbi.rawEncode(['address', 'uint256'], [wrongAddress, '10000000']); + const erc20TransferData = '0x' + Buffer.concat([methodId, encodedParams]).toString('hex'); + + const txBuilder = getBuilder('tbsc') as TransactionBuilder; + txBuilder.type(TransactionType.ContractCall); + txBuilder.fee({ fee: '10', gasLimit: '60000' }); + txBuilder.counter(1); + txBuilder.contract(tokenContractAddress); + txBuilder.data(erc20TransferData); + const tx = await txBuilder.build(); + const txHex = tx.toBroadcastFormat(); + + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } }); + + await coin + .verifyTssTransaction({ + txParams: { + type: 'transfer', + recipients: [{ address: recipientAddress, amount: '10000000' }], + } as any, + txPrebuild: { txHex, coin: 'tbsc', walletId: 'fakeWalletId' } as any, + wallet, + }) + .should.be.rejectedWith('destination address does not match with the recipient address'); + }); + }); }); From 98afd5e2475b86d9a7a0f3adfdb0c9b3edb0951c Mon Sep 17 00:00:00 2001 From: Marzooqa Kather Date: Mon, 3 Aug 2026 11:10:57 +0000 Subject: [PATCH 2/3] fix(bsc): port transfer validation from base class into verifyTssTransaction Replace the unconditional-return-true stub in Bsc and BscToken with the same 'transfer' calldata validation that AbstractEthLikeNewCoins already performs: - native BNB (data === '0x'): verifies destination address and amount match the declared recipient - BEP-20 transfer() (0xa9059cbb): decodes ABI calldata, verifies recipient address and token amount, including the WalletConnect recipients[0].data fallback The original stub was introduced in commit 3d294365bd (COIN-3222, May 2025) to unblock a txHex-decoding crash in the transaction builder. That issue is resolved; this brings BSC to parity with ETH and other AbstractEthLikeNewCoins coins without removing the override. The shallow presence guards (missing txParams, missing params, hop+batch conflict) are retained from the original override, with the addition of the missing txPrebuild.consolidateId check that the base class also performs. Add regression tests confirming: - Native BNB TSS transfer with matching recipient passes - Native BNB TSS transfer with mismatched recipient throws - BEP-20 TSS token transfer with matching calldata recipient passes - BEP-20 TSS token transfer with mismatched calldata recipient throws Ticket: WCI-1169 Session-Id: 4ade7ff7-085e-476d-b280-f4d3dd01c105 Task-Id: 9e3fa483-2a00-47db-aa04-c64b6f003a77 --- modules/sdk-coin-bsc/src/bsc.ts | 111 ++++++++++++++++++++++++- modules/sdk-coin-bsc/src/bscToken.ts | 119 ++++++++++++++++++++++++++- 2 files changed, 227 insertions(+), 3 deletions(-) diff --git a/modules/sdk-coin-bsc/src/bsc.ts b/modules/sdk-coin-bsc/src/bsc.ts index ebc836bc19..d7116b1f44 100644 --- a/modules/sdk-coin-bsc/src/bsc.ts +++ b/modules/sdk-coin-bsc/src/bsc.ts @@ -5,9 +5,19 @@ import { MPCAlgorithm, MultisigType, multisigTypes, + NO_RECIPIENT_TX_TYPES, + Recipient, + TxIntentMismatchRecipientError, } from '@bitgo/sdk-core'; import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics'; -import { AbstractEthLikeNewCoins, recoveryBlockchainExplorerQuery } from '@bitgo/abstract-eth'; +import { + AbstractEthLikeNewCoins, + getBufferedByteCode, + getRawDecoded, + recoveryBlockchainExplorerQuery, + VerifyEthTransactionOptions, +} from '@bitgo/abstract-eth'; +import { addHexPrefix } from 'ethereumjs-util'; import { TransactionBuilder } from './lib'; export class Bsc extends AbstractEthLikeNewCoins { @@ -58,4 +68,103 @@ export class Bsc extends AbstractEthLikeNewCoins { return await recoveryBlockchainExplorerQuery(query, explorerUrl as string, apiToken); } + /** + * Verify if a tss transaction is valid. + * + * Performs the same 'transfer' calldata validation as AbstractEthLikeNewCoins: + * - native BNB (data === '0x'): checks destination address and amount + * - BEP-20 transfer() (0xa9059cbb): decodes calldata, checks destination and + * amount, including the WalletConnect recipients[0].data fallback + * + * @param {VerifyEthTransactionOptions} params + * @returns {Promise} + */ + async verifyTssTransaction(params: VerifyEthTransactionOptions): Promise { + const { txParams, txPrebuild, wallet } = params; + + const throwRecipientMismatch = (message: string, mismatchedRecipients: Recipient[]): never => { + throw new TxIntentMismatchRecipientError(message, undefined, [txParams], txPrebuild?.txHex, mismatchedRecipients); + }; + + if ( + !txParams?.recipients && + !( + txParams.prebuildTx?.consolidateId || + txPrebuild?.consolidateId || + txParams.stakingRequestId || + txParams.prebuildTx?.stakingRequestId || + (txParams.type && NO_RECIPIENT_TX_TYPES.has(txParams.type)) + ) + ) { + throw new Error(`missing txParams`); + } + if (!wallet || !txPrebuild) { + throw new Error(`missing params`); + } + if (txParams.hop && txParams.recipients && txParams.recipients.length > 1) { + throw new Error(`tx cannot be both a batch and hop transaction`); + } + + if (txParams.type && txParams.type === 'transfer') { + if (txParams.recipients && txParams.recipients.length === 1) { + const recipients = txParams.recipients; + const expectedAmount = recipients[0].amount.toString(); + const expectedDestination = recipients[0].address; + + const txBuilder = this.getTransactionBuilder(); + txBuilder.from(txPrebuild.txHex); + const tx = await txBuilder.build(); + const txJson = tx.toJson(); + + if (txJson.data === '0x') { + if (expectedAmount !== txJson.value) { + throwRecipientMismatch('the transaction amount in txPrebuild does not match the value given by client', [ + { address: txJson.to, amount: txJson.value }, + ]); + } + if (expectedDestination.toLowerCase() !== txJson.to.toLowerCase()) { + throwRecipientMismatch('destination address does not match with the recipient address', [ + { address: txJson.to, amount: txJson.value }, + ]); + } + } else if (txJson.data.startsWith('0xa9059cbb')) { + const [recipientAddress, amount] = getRawDecoded( + ['address', 'uint256'], + getBufferedByteCode('0xa9059cbb', txJson.data) + ); + + // Check if recipients[0].data exists (WalletConnect flow) + let expectedRecipientAddress: string; + let expectedTokenAmount: string; + const recipientData = (recipients[0] as any).data; + + if (recipientData && recipientData.startsWith('0xa9059cbb')) { + const [expectedRecipient, expectedAmt] = getRawDecoded( + ['address', 'uint256'], + getBufferedByteCode('0xa9059cbb', recipientData) + ); + expectedRecipientAddress = addHexPrefix(expectedRecipient.toString()).toLowerCase(); + expectedTokenAmount = expectedAmt.toString(); + } else { + expectedRecipientAddress = expectedDestination.toLowerCase(); + expectedTokenAmount = expectedAmount; + } + + if (expectedTokenAmount !== amount.toString()) { + throwRecipientMismatch('the transaction amount in txPrebuild does not match the value given by client', [ + { address: addHexPrefix(recipientAddress.toString()), amount: amount.toString() }, + ]); + } + + if (expectedRecipientAddress !== addHexPrefix(recipientAddress.toString()).toLowerCase()) { + throwRecipientMismatch('destination address does not match with the recipient address', [ + { address: addHexPrefix(recipientAddress.toString()), amount: amount.toString() }, + ]); + } + } + } + } + + return true; + } } diff --git a/modules/sdk-coin-bsc/src/bscToken.ts b/modules/sdk-coin-bsc/src/bscToken.ts index c015368b86..a89dbc38d4 100644 --- a/modules/sdk-coin-bsc/src/bscToken.ts +++ b/modules/sdk-coin-bsc/src/bscToken.ts @@ -3,8 +3,23 @@ */ import { EthLikeTokenConfig, coins } from '@bitgo/statics'; -import { BitGoBase, CoinConstructor, NamedCoinConstructor, MPCAlgorithm } from '@bitgo/sdk-core'; -import { CoinNames, EthLikeToken } from '@bitgo/abstract-eth'; +import { + BitGoBase, + CoinConstructor, + MPCAlgorithm, + NamedCoinConstructor, + NO_RECIPIENT_TX_TYPES, + Recipient, + TxIntentMismatchRecipientError, +} from '@bitgo/sdk-core'; +import { + CoinNames, + EthLikeToken, + getBufferedByteCode, + getRawDecoded, + VerifyEthTransactionOptions, +} from '@bitgo/abstract-eth'; +import { addHexPrefix } from 'ethereumjs-util'; import { TransactionBuilder } from './lib'; export { EthLikeTokenConfig }; @@ -43,4 +58,104 @@ export class BscToken extends EthLikeToken { getFullName(): string { return 'Bsc Token'; } + + /** + * Verify if a tss transaction is valid. + * + * Performs the same 'transfer' calldata validation as AbstractEthLikeNewCoins: + * - native transfer (data === '0x'): checks destination address and amount + * - BEP-20 transfer() (0xa9059cbb): decodes calldata, checks destination and + * amount, including the WalletConnect recipients[0].data fallback + * + * @param {VerifyEthTransactionOptions} params + * @returns {Promise} + */ + async verifyTssTransaction(params: VerifyEthTransactionOptions): Promise { + const { txParams, txPrebuild, wallet } = params; + + const throwRecipientMismatch = (message: string, mismatchedRecipients: Recipient[]): never => { + throw new TxIntentMismatchRecipientError(message, undefined, [txParams], txPrebuild?.txHex, mismatchedRecipients); + }; + + if ( + !txParams?.recipients && + !( + txParams.prebuildTx?.consolidateId || + txPrebuild?.consolidateId || + txParams.stakingRequestId || + txParams.prebuildTx?.stakingRequestId || + (txParams.type && NO_RECIPIENT_TX_TYPES.has(txParams.type)) + ) + ) { + throw new Error(`missing txParams`); + } + if (!wallet || !txPrebuild) { + throw new Error(`missing params`); + } + if (txParams.hop && txParams.recipients && txParams.recipients.length > 1) { + throw new Error(`tx cannot be both a batch and hop transaction`); + } + + if (txParams.type && txParams.type === 'transfer') { + if (txParams.recipients && txParams.recipients.length === 1) { + const recipients = txParams.recipients; + const expectedAmount = recipients[0].amount.toString(); + const expectedDestination = recipients[0].address; + + const txBuilder = this.getTransactionBuilder(); + txBuilder.from(txPrebuild.txHex); + const tx = await txBuilder.build(); + const txJson = tx.toJson(); + + if (txJson.data === '0x') { + if (expectedAmount !== txJson.value) { + throwRecipientMismatch('the transaction amount in txPrebuild does not match the value given by client', [ + { address: txJson.to, amount: txJson.value }, + ]); + } + if (expectedDestination.toLowerCase() !== txJson.to.toLowerCase()) { + throwRecipientMismatch('destination address does not match with the recipient address', [ + { address: txJson.to, amount: txJson.value }, + ]); + } + } else if (txJson.data.startsWith('0xa9059cbb')) { + const [recipientAddress, amount] = getRawDecoded( + ['address', 'uint256'], + getBufferedByteCode('0xa9059cbb', txJson.data) + ); + + // Check if recipients[0].data exists (WalletConnect flow) + let expectedRecipientAddress: string; + let expectedTokenAmount: string; + const recipientData = (recipients[0] as any).data; + + if (recipientData && recipientData.startsWith('0xa9059cbb')) { + const [expectedRecipient, expectedAmt] = getRawDecoded( + ['address', 'uint256'], + getBufferedByteCode('0xa9059cbb', recipientData) + ); + expectedRecipientAddress = addHexPrefix(expectedRecipient.toString()).toLowerCase(); + expectedTokenAmount = expectedAmt.toString(); + } else { + expectedRecipientAddress = expectedDestination.toLowerCase(); + expectedTokenAmount = expectedAmount; + } + + if (expectedTokenAmount !== amount.toString()) { + throwRecipientMismatch('the transaction amount in txPrebuild does not match the value given by client', [ + { address: addHexPrefix(recipientAddress.toString()), amount: amount.toString() }, + ]); + } + + if (expectedRecipientAddress !== addHexPrefix(recipientAddress.toString()).toLowerCase()) { + throwRecipientMismatch('destination address does not match with the recipient address', [ + { address: addHexPrefix(recipientAddress.toString()), amount: amount.toString() }, + ]); + } + } + } + } + + return true; + } } From 6c6f12a604f6cd1940e993a769b530870c17d0c7 Mon Sep 17 00:00:00 2001 From: Marzooqa Kather Date: Mon, 3 Aug 2026 11:13:41 +0000 Subject: [PATCH 3/3] fix(bsc): remove ethereumjs-util import, add WalletConnect test coverage Replace the ethereumjs-util addHexPrefix import (not in sdk-coin-bsc deps) with an inline helper. ethereumjs-util is a transitive dep of abstract-eth, not a direct dep of sdk-coin-bsc, so relying on it was fragile. Add two tests for the WalletConnect recipients[0].data flow in verifyTssTransaction: - BEP-20 with matching recipients[0].data passes - BEP-20 with tampered txHex calldata but correct recipients[0].data throws (tampered-recipient rejection) Ticket: WCI-1169 Session-Id: 4ade7ff7-085e-476d-b280-f4d3dd01c105 Task-Id: 9e3fa483-2a00-47db-aa04-c64b6f003a77 --- modules/sdk-coin-bsc/src/bsc.ts | 3 +- modules/sdk-coin-bsc/src/bscToken.ts | 3 +- modules/sdk-coin-bsc/test/unit/bsc.ts | 66 +++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/modules/sdk-coin-bsc/src/bsc.ts b/modules/sdk-coin-bsc/src/bsc.ts index d7116b1f44..9eac4b6ba2 100644 --- a/modules/sdk-coin-bsc/src/bsc.ts +++ b/modules/sdk-coin-bsc/src/bsc.ts @@ -17,9 +17,10 @@ import { recoveryBlockchainExplorerQuery, VerifyEthTransactionOptions, } from '@bitgo/abstract-eth'; -import { addHexPrefix } from 'ethereumjs-util'; import { TransactionBuilder } from './lib'; +const addHexPrefix = (hex: string): string => (hex.startsWith('0x') ? hex : `0x${hex}`); + export class Bsc extends AbstractEthLikeNewCoins { protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly) { super(bitgo, staticsCoin); diff --git a/modules/sdk-coin-bsc/src/bscToken.ts b/modules/sdk-coin-bsc/src/bscToken.ts index a89dbc38d4..f72b67321d 100644 --- a/modules/sdk-coin-bsc/src/bscToken.ts +++ b/modules/sdk-coin-bsc/src/bscToken.ts @@ -19,9 +19,10 @@ import { getRawDecoded, VerifyEthTransactionOptions, } from '@bitgo/abstract-eth'; -import { addHexPrefix } from 'ethereumjs-util'; import { TransactionBuilder } from './lib'; +const addHexPrefix = (hex: string): string => (hex.startsWith('0x') ? hex : `0x${hex}`); + export { EthLikeTokenConfig }; export class BscToken extends EthLikeToken { diff --git a/modules/sdk-coin-bsc/test/unit/bsc.ts b/modules/sdk-coin-bsc/test/unit/bsc.ts index 0269ee91ef..95f1e16aa9 100644 --- a/modules/sdk-coin-bsc/test/unit/bsc.ts +++ b/modules/sdk-coin-bsc/test/unit/bsc.ts @@ -159,5 +159,71 @@ describe('Native BNB', function () { }) .should.be.rejectedWith('destination address does not match with the recipient address'); }); + + it('should accept a BEP-20 token transfer using WalletConnect recipients[0].data flow', async function () { + const coin = bitgo.coin('tbsc') as Tbsc; + + // txHex sends to recipientAddress; recipients[0].data encodes the same intent + const methodId = EthereumAbi.methodID('transfer', ['address', 'uint256']); + const encodedParams = EthereumAbi.rawEncode(['address', 'uint256'], [recipientAddress, '10000000']); + const erc20TransferData = '0x' + Buffer.concat([methodId, encodedParams]).toString('hex'); + + const txBuilder = getBuilder('tbsc') as TransactionBuilder; + txBuilder.type(TransactionType.ContractCall); + txBuilder.fee({ fee: '10', gasLimit: '60000' }); + txBuilder.counter(1); + txBuilder.contract(tokenContractAddress); + txBuilder.data(erc20TransferData); + const tx = await txBuilder.build(); + const txHex = tx.toBroadcastFormat(); + + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } }); + + const result = await coin.verifyTssTransaction({ + txParams: { + type: 'transfer', + // WalletConnect passes the intended calldata in recipients[0].data + recipients: [{ address: tokenContractAddress, amount: '0', data: erc20TransferData }], + } as any, + txPrebuild: { txHex, coin: 'tbsc', walletId: 'fakeWalletId' } as any, + wallet, + }); + result.should.equal(true); + }); + + it('should reject a BEP-20 token transfer using WalletConnect flow when calldata recipient is tampered', async function () { + const coin = bitgo.coin('tbsc') as Tbsc; + + // txHex sends to wrongAddress (tampered) + const methodId = EthereumAbi.methodID('transfer', ['address', 'uint256']); + const tamperedParams = EthereumAbi.rawEncode(['address', 'uint256'], [wrongAddress, '10000000']); + const tamperedData = '0x' + Buffer.concat([methodId, tamperedParams]).toString('hex'); + + const txBuilder = getBuilder('tbsc') as TransactionBuilder; + txBuilder.type(TransactionType.ContractCall); + txBuilder.fee({ fee: '10', gasLimit: '60000' }); + txBuilder.counter(1); + txBuilder.contract(tokenContractAddress); + txBuilder.data(tamperedData); + const tx = await txBuilder.build(); + const txHex = tx.toBroadcastFormat(); + + // recipients[0].data declares the correct recipient (recipientAddress) + const correctParams = EthereumAbi.rawEncode(['address', 'uint256'], [recipientAddress, '10000000']); + const correctData = '0x' + Buffer.concat([methodId, correctParams]).toString('hex'); + + const wallet = new Wallet(bitgo, coin, { coinSpecific: { baseAddress: recipientAddress } }); + + await coin + .verifyTssTransaction({ + txParams: { + type: 'transfer', + recipients: [{ address: tokenContractAddress, amount: '0', data: correctData }], + } as any, + txPrebuild: { txHex, coin: 'tbsc', walletId: 'fakeWalletId' } as any, + wallet, + }) + .should.be.rejectedWith('destination address does not match with the recipient address'); + }); }); });