|
| 1 | +import BigNumber from 'bignumber.js'; |
1 | 2 | import { BitGoAPI } from '@bitgo/sdk-api'; |
2 | 3 | import { common, ECDSAMethodTypes, FullySignedTransaction, Recipient, TransactionType, Wallet } from '@bitgo/sdk-core'; |
3 | 4 | import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; |
@@ -221,6 +222,94 @@ describe('Polygon', function () { |
221 | 222 | halfSignedRawTx.halfSigned.eip1559.maxFeePerGas.should.equal('7593123'); |
222 | 223 | halfSignedRawTx.halfSigned.eip1559.maxPriorityFeePerGas.should.equal('150'); |
223 | 224 | }); |
| 225 | + |
| 226 | + it('should include isBatch field in halfSigned when signing a native batch transaction', async function () { |
| 227 | + const batcherContractAddress = '0xb1b7e7cc1ecafbfd0771a5eb5454ab5b0356980d'; |
| 228 | + const recipients: Recipient[] = [ |
| 229 | + { |
| 230 | + address: account_2.address, |
| 231 | + amount: '500000000000', |
| 232 | + }, |
| 233 | + { |
| 234 | + address: '0x7e85bdc27c050e3905ebf4b8e634d9ad6edd0de6', |
| 235 | + amount: '500000000000', |
| 236 | + }, |
| 237 | + ]; |
| 238 | + const totalAmount = recipients.reduce( |
| 239 | + (sum, recipient) => new BigNumber(sum).plus(recipient.amount).toString(), |
| 240 | + '0' |
| 241 | + ); |
| 242 | + const BATCH_METHOD_NAME = 'batch'; |
| 243 | + const BATCH_METHOD_TYPES = ['address[]', 'uint256[]']; |
| 244 | + |
| 245 | + const addresses = recipients.map((r) => r.address); |
| 246 | + const amounts = recipients.map((r) => r.amount); |
| 247 | + const batchExecutionInfo = { |
| 248 | + values: [addresses, amounts], |
| 249 | + totalAmount: totalAmount, |
| 250 | + }; |
| 251 | + |
| 252 | + const ethAbi = require('ethereumjs-abi'); |
| 253 | + const ethUtil = require('ethereumjs-util'); |
| 254 | + |
| 255 | + const getMethodCallData = (functionName, types, values) => { |
| 256 | + return Buffer.concat([ |
| 257 | + // function signature |
| 258 | + ethAbi.methodID(functionName, types), |
| 259 | + // function arguments |
| 260 | + ethAbi.rawEncode(types, values), |
| 261 | + ]); |
| 262 | + }; |
| 263 | + |
| 264 | + const batchData = ethUtil.addHexPrefix( |
| 265 | + getMethodCallData(BATCH_METHOD_NAME, BATCH_METHOD_TYPES, batchExecutionInfo.values).toString('hex') |
| 266 | + ); |
| 267 | + |
| 268 | + const txBuilder = getBuilder('tpolygon') as TransactionBuilder; |
| 269 | + txBuilder.fee({ |
| 270 | + fee: '280000000000', |
| 271 | + gasLimit: '7000000', |
| 272 | + }); |
| 273 | + txBuilder.counter(1); |
| 274 | + txBuilder.type(TransactionType.Send); |
| 275 | + txBuilder.contract(account_1.address); |
| 276 | + txBuilder |
| 277 | + .transfer() |
| 278 | + .amount(totalAmount) |
| 279 | + .to(batcherContractAddress) |
| 280 | + .data(batchData) |
| 281 | + .expirationTime(10000) |
| 282 | + .contractSequenceId(1); |
| 283 | + |
| 284 | + const unsignedTx = await txBuilder.build(); |
| 285 | + const unsignedTxHex = unsignedTx.toBroadcastFormat(); |
| 286 | + |
| 287 | + const txPrebuild = { |
| 288 | + txHex: unsignedTxHex, |
| 289 | + isBatch: true, |
| 290 | + recipients: [ |
| 291 | + { |
| 292 | + amount: totalAmount, |
| 293 | + address: batcherContractAddress, |
| 294 | + }, |
| 295 | + ], |
| 296 | + nextContractSequenceId: 1, |
| 297 | + gasPrice: 280000000000, |
| 298 | + gasLimit: 7000000, |
| 299 | + coin: 'tpolygon', |
| 300 | + walletId: 'fakeWalletId', |
| 301 | + walletContractAddress: account_1.address, |
| 302 | + }; |
| 303 | + |
| 304 | + const signedTx = await basecoin.signTransaction({ |
| 305 | + txPrebuild, |
| 306 | + prv: account_1.owner_2, |
| 307 | + }); |
| 308 | + |
| 309 | + should.exist(signedTx); |
| 310 | + should.exist(signedTx.halfSigned); |
| 311 | + signedTx.halfSigned.should.have.property('isBatch', true); |
| 312 | + }); |
224 | 313 | }); |
225 | 314 |
|
226 | 315 | describe('Transaction Verification', function () { |
|
0 commit comments