@@ -2,8 +2,12 @@ import 'should';
22
33import { TestBitGo , TestBitGoAPI } from '@bitgo/sdk-test' ;
44import { BitGoAPI } from '@bitgo/sdk-api' ;
5+ import { TransactionType , Wallet } from '@bitgo/sdk-core' ;
6+ import EthereumAbi from 'ethereumjs-abi' ;
57
68import { Bsc , Tbsc } from '../../src/index' ;
9+ import { TransactionBuilder } from '../../src/lib' ;
10+ import { getBuilder } from './getBuilder' ;
711
812const bitgo : TestBitGoAPI = TestBitGo . decorate ( BitGoAPI , { env : 'test' } ) ;
913
@@ -39,4 +43,121 @@ describe('Native BNB', function () {
3943 tbsc . allowsAccountConsolidations ( ) . should . equal ( true ) ;
4044 } ) ;
4145 } ) ;
46+
47+ describe ( 'verifyTssTransaction' , function ( ) {
48+ const recipientAddress = '0x174cfd823af8ce27ed0afee3fcf3c3ba259116be' ;
49+ const wrongAddress = '0x7e85bdc27c050e3905ebf4b8e634d9ad6edd0de6' ;
50+ const tokenContractAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' ;
51+ const transferAmount = '1000000000000000000' ;
52+
53+ it ( 'should accept a native BNB transfer where txHex matches declared recipient' , async function ( ) {
54+ const coin = bitgo . coin ( 'tbsc' ) as Tbsc ;
55+
56+ const txBuilder = getBuilder ( 'tbsc' ) as TransactionBuilder ;
57+ txBuilder . type ( TransactionType . SingleSigSend ) ;
58+ txBuilder . fee ( { fee : '10' , gasLimit : '21000' } ) ;
59+ txBuilder . counter ( 1 ) ;
60+ txBuilder . contract ( recipientAddress ) ;
61+ txBuilder . value ( transferAmount ) ;
62+ const tx = await txBuilder . build ( ) ;
63+ const txHex = tx . toBroadcastFormat ( ) ;
64+
65+ const wallet = new Wallet ( bitgo , coin , { coinSpecific : { baseAddress : recipientAddress } } ) ;
66+
67+ const result = await coin . verifyTssTransaction ( {
68+ txParams : {
69+ type : 'transfer' ,
70+ recipients : [ { address : recipientAddress , amount : transferAmount } ] ,
71+ } as any ,
72+ txPrebuild : { txHex, coin : 'tbsc' , walletId : 'fakeWalletId' } as any ,
73+ wallet,
74+ } ) ;
75+ result . should . equal ( true ) ;
76+ } ) ;
77+
78+ it ( 'should reject a native BNB transfer when txHex recipient does not match declared recipient' , async function ( ) {
79+ const coin = bitgo . coin ( 'tbsc' ) as Tbsc ;
80+
81+ const txBuilder = getBuilder ( 'tbsc' ) as TransactionBuilder ;
82+ txBuilder . type ( TransactionType . SingleSigSend ) ;
83+ txBuilder . fee ( { fee : '10' , gasLimit : '21000' } ) ;
84+ txBuilder . counter ( 1 ) ;
85+ txBuilder . contract ( wrongAddress ) ;
86+ txBuilder . value ( transferAmount ) ;
87+ const tx = await txBuilder . build ( ) ;
88+ const txHex = tx . toBroadcastFormat ( ) ;
89+
90+ const wallet = new Wallet ( bitgo , coin , { coinSpecific : { baseAddress : recipientAddress } } ) ;
91+
92+ await coin
93+ . verifyTssTransaction ( {
94+ txParams : {
95+ type : 'transfer' ,
96+ recipients : [ { address : recipientAddress , amount : transferAmount } ] ,
97+ } as any ,
98+ txPrebuild : { txHex, coin : 'tbsc' , walletId : 'fakeWalletId' } as any ,
99+ wallet,
100+ } )
101+ . should . be . rejectedWith ( 'destination address does not match with the recipient address' ) ;
102+ } ) ;
103+
104+ it ( 'should accept a BEP-20 token transfer where calldata matches declared recipient' , async function ( ) {
105+ const coin = bitgo . coin ( 'tbsc' ) as Tbsc ;
106+
107+ const methodId = EthereumAbi . methodID ( 'transfer' , [ 'address' , 'uint256' ] ) ;
108+ const encodedParams = EthereumAbi . rawEncode ( [ 'address' , 'uint256' ] , [ recipientAddress , '10000000' ] ) ;
109+ const erc20TransferData = '0x' + Buffer . concat ( [ methodId , encodedParams ] ) . toString ( 'hex' ) ;
110+
111+ const txBuilder = getBuilder ( 'tbsc' ) as TransactionBuilder ;
112+ txBuilder . type ( TransactionType . ContractCall ) ;
113+ txBuilder . fee ( { fee : '10' , gasLimit : '60000' } ) ;
114+ txBuilder . counter ( 1 ) ;
115+ txBuilder . contract ( tokenContractAddress ) ;
116+ txBuilder . data ( erc20TransferData ) ;
117+ const tx = await txBuilder . build ( ) ;
118+ const txHex = tx . toBroadcastFormat ( ) ;
119+
120+ const wallet = new Wallet ( bitgo , coin , { coinSpecific : { baseAddress : recipientAddress } } ) ;
121+
122+ const result = await coin . verifyTssTransaction ( {
123+ txParams : {
124+ type : 'transfer' ,
125+ recipients : [ { address : recipientAddress , amount : '10000000' } ] ,
126+ } as any ,
127+ txPrebuild : { txHex, coin : 'tbsc' , walletId : 'fakeWalletId' } as any ,
128+ wallet,
129+ } ) ;
130+ result . should . equal ( true ) ;
131+ } ) ;
132+
133+ it ( 'should reject a BEP-20 token transfer when calldata recipient does not match declared recipient' , async function ( ) {
134+ const coin = bitgo . coin ( 'tbsc' ) as Tbsc ;
135+
136+ const methodId = EthereumAbi . methodID ( 'transfer' , [ 'address' , 'uint256' ] ) ;
137+ const encodedParams = EthereumAbi . rawEncode ( [ 'address' , 'uint256' ] , [ wrongAddress , '10000000' ] ) ;
138+ const erc20TransferData = '0x' + Buffer . concat ( [ methodId , encodedParams ] ) . toString ( 'hex' ) ;
139+
140+ const txBuilder = getBuilder ( 'tbsc' ) as TransactionBuilder ;
141+ txBuilder . type ( TransactionType . ContractCall ) ;
142+ txBuilder . fee ( { fee : '10' , gasLimit : '60000' } ) ;
143+ txBuilder . counter ( 1 ) ;
144+ txBuilder . contract ( tokenContractAddress ) ;
145+ txBuilder . data ( erc20TransferData ) ;
146+ const tx = await txBuilder . build ( ) ;
147+ const txHex = tx . toBroadcastFormat ( ) ;
148+
149+ const wallet = new Wallet ( bitgo , coin , { coinSpecific : { baseAddress : recipientAddress } } ) ;
150+
151+ await coin
152+ . verifyTssTransaction ( {
153+ txParams : {
154+ type : 'transfer' ,
155+ recipients : [ { address : recipientAddress , amount : '10000000' } ] ,
156+ } as any ,
157+ txPrebuild : { txHex, coin : 'tbsc' , walletId : 'fakeWalletId' } as any ,
158+ wallet,
159+ } )
160+ . should . be . rejectedWith ( 'destination address does not match with the recipient address' ) ;
161+ } ) ;
162+ } ) ;
42163} ) ;
0 commit comments