Skip to content

Commit

Permalink
Merge pull request #4249 from BitGo/WIN-1867-modify-encoding-related-…
Browse files Browse the repository at this point in the history
…changes-arb-op

fix(abstract-eth): change tx encoding method for arb and op
  • Loading branch information
gianchandania authored Feb 5, 2024
2 parents fb84e7f + c11b0dd commit 5db72c1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
8 changes: 6 additions & 2 deletions modules/abstract-eth/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseCoin as CoinConfig, EthereumNetwork } from '@bitgo/statics';
import { BaseCoin as CoinConfig, EthereumNetwork, CoinFeature } from '@bitgo/statics';
import EthereumCommon from '@ethereumjs/common';
import EthereumAbi from 'ethereumjs-abi';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -614,7 +614,11 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
throw new BuildTransactionError('Missing transfer information');
}
const chainId = this._common.chainIdBN().toString();
return this._transfer.signAndBuild(chainId);
// This change is made to support new contracts with different encoding type
const coinUsesNonPackedEncodingForTxData = this._coinConfig.features.includes(
CoinFeature.USES_NON_PACKED_ENCODING_FOR_TXDATA
);
return this._transfer.signAndBuild(chainId, coinUsesNonPackedEncodingForTxData);
}

private buildSendTransaction(): TxData {
Expand Down
28 changes: 25 additions & 3 deletions modules/abstract-eth/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BN from 'bn.js';
import { coins, BaseCoin, ContractAddressDefinedToken, EthereumNetwork as EthLikeNetwork } from '@bitgo/statics';
import { BuildTransactionError, InvalidParameterValueError } from '@bitgo/sdk-core';
import { decodeTransferData, sendMultiSigData, sendMultiSigTokenData, isValidEthAddress, isValidAmount } from './utils';
import { defaultAbiCoder, keccak256 } from 'ethers/lib/utils';

/** ETH transfer builder */
export class TransferBuilder {
Expand All @@ -18,6 +19,7 @@ export class TransferBuilder {
private _tokenContractAddress?: string;
private _coin: Readonly<BaseCoin>;
private _chainId?: string;
private _coinUsesNonPackedEncodingForTxData?: boolean;

constructor(serializedData?: string) {
if (serializedData) {
Expand Down Expand Up @@ -93,8 +95,13 @@ export class TransferBuilder {
throw new InvalidParameterValueError('Invalid expiration time');
}

signAndBuild(chainId?: string): string {
signAndBuild(chainId?: string, coinUsesNonPackedEncodingForTxData?: boolean): string {
this._chainId = chainId;

// If the coin uses non-packed encoding for tx data, the operation hash is calculated differently
// This new encoding type is applicable only for native coins and not tokens
this._coinUsesNonPackedEncodingForTxData =
coinUsesNonPackedEncodingForTxData && this._tokenContractAddress === undefined;
if (this.hasMandatoryFields()) {
if (this._tokenContractAddress !== undefined) {
return sendMultiSigTokenData(
Expand Down Expand Up @@ -133,7 +140,19 @@ export class TransferBuilder {
*/
private getOperationHash(): string {
const operationData = this.getOperationData();
return ethUtil.bufferToHex(EthereumAbi.soliditySHA3(...operationData));
let operationHash: string;

if (this._coinUsesNonPackedEncodingForTxData) {
const types: string[] = operationData[0] as string[];
const values: string[] = operationData[1].map((item) =>
item instanceof Buffer ? '0x' + item.toString('hex') : item
);
operationHash = keccak256(defaultAbiCoder.encode(types, values));
} else {
// If the coin uses packed encoding for tx data or it is a token, the operation hash is calculated using the Ethereum ABI
operationHash = ethUtil.bufferToHex(EthereumAbi.soliditySHA3(...operationData));
}
return operationHash;
}

protected getOperationData(): (string | Buffer)[][] {
Expand All @@ -151,11 +170,14 @@ export class TransferBuilder {
],
];
} else {
const toAddress = this._coinUsesNonPackedEncodingForTxData
? this._toAddress
: new BN(ethUtil.stripHexPrefix(this._toAddress), 16);
operationData = [
['string', 'address', 'uint', 'bytes', 'uint', 'uint'],
[
this.getNativeOperationHashPrefix(),
new BN(ethUtil.stripHexPrefix(this._toAddress), 16),
toAddress,
this._amount,
Buffer.from(ethUtil.padToEven(ethUtil.stripHexPrefix(this._data)) || '', 'hex'),
this._expirationTime,
Expand Down
4 changes: 2 additions & 2 deletions modules/sdk-coin-arbeth/test/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export const SEND_FUNDS_NO_KEY_DATA =
'0x391252150000000000000000000000007325a3f7d4f9e86ae62cf742426078c3755730d5000000000000000000000000000000000000000000000000016345785d8a000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec6ab34000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';

export const SEND_TX_BROADCAST_LEGACY =
'0xf901cd02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000410374eff4ca3b3d8c7b37ac7515ea4d212847bdc08a5ede459436c3ebcfd7f9b855d021e7590b6fa36d68ba1c58fc568d52d6794e465f6687da90cc70aa3335231b00000000000000000000000000000000000000000000000000000000000000830cde00a074b64b8ffaf205436aa4bfcbb78e98329f4fed4f11bf3b40f9eef5a9a16bc165a01740883cc0236ab97286398d114d8bebc1a0717558d45533bebb7e5c130eeff6';
'0xf901cd02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041251032b0cdf55de6ae08a0d5bc3bc60e9a82770f225358d48ba43a0f37257e073acfbf47e9ed4919ceb67d3edd38bd2f9f1df43cda63106d831fbe731ca0e2e11c00000000000000000000000000000000000000000000000000000000000000830cde00a08d1a00b933c4ef12181684f70e132fd1400ec14c3d05890dc9de38788bd9ac35a008cca96d02ee98d44b31fade38c2b6e805df1dc2d19570a26d9fba26a6ff778b';

export const SEND_TOKEN_TX_BROADCAST =
'0xf901ad02843b9aca0083b8a1a094df7decb1baa8f529f0c8982cbb4be5035719529980b901440dcd7a6c00000000000000000000000072c2c8e08bf91d755cd7d26b49a2ee3dc99de1b90000000000000000000000000000000000000000000000000000000000000064000000000000000000000000143e1dae4f018ff86051a01d44a1b49b13704056000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000041bec4f4b3c7f2b3701694d615794a5fa9ee2e097e50d2873e6b17b565b44900920b6e2fbc97cdc7fc690a71a509ad08851bd69c99e999660a892ccf67b9c2c8a31c00000000000000000000000000000000000000000000000000000000000000830cddffa0c12178bc3bd603a415f7510a9160a774828176b86105913685309f3e64bbd00fa01d8593a7d051656bb0023219f98f6da708fade85f26e7c974a8eaa0246c2e795';

export const SEND_TX_AMOUNT_ZERO_BROADCAST =
'0xf901cd02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000415bba6d3d5288d585cbb01997410bce555295a19ab7ede922866ae0c9b50698754f494bd624ea91a4b098e9d17080cc3af571cedad6334c4be1e6e49498af68481c00000000000000000000000000000000000000000000000000000000000000830cde00a0e0421465dee286fd38937b8ea67807fc30265ac5fb5e7ea23ae73074ef22a1b8a032ed7887c90ade813e659e63145409d7ac896342eb02674524184e806004945f';
'0xf901cd02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004105c71ac24598ef2ba839d1d50757883ef8afafb94d34d8363add922380a6aefa6378ef9e8f6a80eac19363bfbb03f0097c87610cf7f68ca22868d3b4074484d71b00000000000000000000000000000000000000000000000000000000000000830cddffa0b727dcc1b0cc28c12b033ddcb8308c40ce5d2f33e171275cc6bda9f86b3a5b3fa02f1983da5f96b0c03edb36f4896a3d53cf2766c88230ab140e3b485b06579826';
4 changes: 2 additions & 2 deletions modules/sdk-coin-opeth/test/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export const SEND_FUNDS_NO_KEY_DATA =
'0x391252150000000000000000000000007325a3f7d4f9e86ae62cf742426078c3755730d5000000000000000000000000000000000000000000000000016345785d8a000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec6ab34000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';

export const SEND_TX_BROADCAST_LEGACY =
'0xf901ce02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000419c32d17b9e99ca78a86aa781c5671a1413fcbdbe6e2f0fbc762be19a958bb10e3c9573f5defc6a8d5ddbb088cf630e8fe64587dd9a9f206e8c11cec72d748d421c000000000000000000000000000000000000000000000000000000000000008401546fdca0dd1f9f8094ca33aa097c1d77af1ca0205578511a051a9bcbdaa219b13ce307d7a0614f15ca8bd33877565a9f4979f29d226c8e25000b23b3a24e27c0af9d66c418';
'0xf901ce02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004195e32e1e4e59c93c73d3aaed895698c87f88b5f498927d02120798d74b9b37463d9075d8d334a0ade945bb0396378d2983d42f397bb86fb400f40e141bde0ecf1c000000000000000000000000000000000000000000000000000000000000008401546fdba03e6f23d9c608050ea4fe6af2ed5f912b39d589c3b26daa7b2ebf2afba4bd6af6a03b7af332d57a8578c25e319362fd1f995305337b8fe8432ba66c99fc07909b1f';

export const SEND_TOKEN_TX_BROADCAST =
'0xf901ae02843b9aca0083b8a1a094df7decb1baa8f529f0c8982cbb4be5035719529980b901440dcd7a6c00000000000000000000000072c2c8e08bf91d755cd7d26b49a2ee3dc99de1b90000000000000000000000000000000000000000000000000000000000000064000000000000000000000000e9df68a54bba438c8a6192e95f0f2c53ac93d997000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000041a33c58bf934c5a2c27b0bc2a49a05736581ea04b836a880ddc1fa32b47ca6d314e7fede6ad011a7a4dd25968973dce1b1569ff89211533b35b8505280c9116521c000000000000000000000000000000000000000000000000000000000000008401546fdca0ad044b7711eb3dc7a9c2a15ff269d0a660d0978021b83d00bfe70e9249344eaaa07d47f97ae4f3c413cb5dc2037c0bce5eac9059a29e1a369e3949dbc81f71c424';

export const SEND_TX_AMOUNT_ZERO_BROADCAST =
'0xf901ce02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041b5c940d67207680cdeb8231b285ed6e360b90ecbaad27e7a139e4b0f2d67928a6b9545e6e14ace66cd7190df351fa1344eaa4cbbc37fa2d3ee1000c7d6ed94251b000000000000000000000000000000000000000000000000000000000000008401546fdca0c1065eb926c9eb2ae80579a977ab847bedcd264d6b903fcbb22c14efedae9d1ca02eac77bf7872dfb81cfe74d9b4b807667aebc86edb71816a75849e9fece52bbf';
'0xf901ce02843b9aca0083b8a1a0948f977e912ef500548a0c3be6ddde9899f1199b8180b901643912521500000000000000000000000019645032c7f1533395d44a629462e751084d3e4c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000005ec67e28000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000419e36c81222bb0bcf1c00aad4974efff99609bd898f7c4dba20d79509dc610b6d1be1876f85216a00c14f2e888430d712e4d4ee629dd9f7d8ee97c0641b55d7491c000000000000000000000000000000000000000000000000000000000000008401546fdba0b270e942c8325f6707bc4fe4dbc2a59f52e6536d28dec18d415962fe0518141ba01d7efc6f541e427e9648adebebf61fb665006a237fdde6f3641c353d1cb5e6b3';
5 changes: 5 additions & 0 deletions modules/statics/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ export enum CoinFeature {
* This coin supports bulk staking transaction creation
*/
BULK_STAKING_TRANSACTION = 'bulk-staking-transaction',

/**
* This coin uses non-packed encoding for transaction data
*/
USES_NON_PACKED_ENCODING_FOR_TXDATA = 'uses-non-packed-encoding-for-txdata',
}

/**
Expand Down
14 changes: 12 additions & 2 deletions modules/statics/src/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,18 @@ const GENERIC_TOKEN_FEATURES = [
CoinFeature.GENERIC_TOKEN,
];
const TON_FEATURES = [...AccountCoin.DEFAULT_FEATURES, CoinFeature.TSS, CoinFeature.TSS_COLD, CoinFeature.STAKING];
const ARBETH_FEATURES = [...ETH_FEATURES, CoinFeature.MULTISIG_COLD, CoinFeature.EVM_WALLET];
const OPETH_FEATURES = [...ETH_FEATURES, CoinFeature.MULTISIG_COLD, CoinFeature.EVM_WALLET];
const ARBETH_FEATURES = [
...ETH_FEATURES,
CoinFeature.MULTISIG_COLD,
CoinFeature.EVM_WALLET,
CoinFeature.USES_NON_PACKED_ENCODING_FOR_TXDATA,
];
const OPETH_FEATURES = [
...ETH_FEATURES,
CoinFeature.MULTISIG_COLD,
CoinFeature.EVM_WALLET,
CoinFeature.USES_NON_PACKED_ENCODING_FOR_TXDATA,
];
const ZKETH_FEATURES = [...ETH_FEATURES, CoinFeature.MULTISIG_COLD, CoinFeature.EVM_WALLET];

export const coins = CoinMap.fromCoins([
Expand Down

0 comments on commit 5db72c1

Please sign in to comment.