Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update @ethereumjs/tx #2806

Merged
merged 6 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/service/keyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mockEncryptor from './mock-encryptor';
import contactBook from '@/background/service/contactBook';
import { normalizeAddress } from '@/background/utils';
import { Wallet } from '@ethereumjs/wallet';
import { bytesToHex } from '@ethereumjs/util';
import { utils } from '@ethereumjs/rlp';

const password = 'password123';
const walletOneSeedWords =
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('keyringService', () => {
);

const wallet = Wallet.fromPrivateKey(Buffer.from(privateAppKey, 'hex'));
const recoveredAddress = bytesToHex(wallet.getAddress());
const recoveredAddress = utils.bytesToHex(wallet.getAddress());

expect(recoveredAddress).toBe(appKeyAddress);
expect(privateAppKey).not.toBe(privateKey);
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
"@dnd-kit/utilities": "3.0.2",
"@eth-optimism/contracts": "0.5.32",
"@eth-optimism/contracts-ts": "0.17.2",
"@ethereumjs/common": "2.5.0",
"@ethereumjs/tx": "3.3.2",
"@ethereumjs/util": "9.0.3",
"@ethereumjs/common": "3.1.1",
"@ethereumjs/rlp": "4.0.1",
"@ethereumjs/tx": "4.1.1",
"@ethereumjs/util": "8.0.5",
"@ethereumjs/wallet": "2.0.3",
"@gnosis.pm/safe-core-sdk": "1.1.1",
"@gnosis.pm/safe-core-sdk-types": "0.1.1",
"@imkey/web3-provider": "1.3.10",
"@keystonehq/hw-app-eth": "0.4.4",
"@keystonehq/hw-transport-webusb": "0.3.2",
"@keystonehq/metamask-airgapped-keyring": "0.2.5-alpha.2.1",
"@keystonehq/metamask-airgapped-keyring": "0.14.1",
"@ledgerhq/devices": "8.4.4",
"@ledgerhq/hw-app-eth": "6.39.0",
"@ledgerhq/hw-transport-webhid": "6.29.4",
Expand All @@ -55,9 +56,9 @@
"@rabby-wallet/eth-coinbase-keyring": "0.0.0-beta.8",
"@rabby-wallet/eth-gnosis-keyring": "0.0.1",
"@rabby-wallet/eth-hd-keyring": "4.3.2",
"@rabby-wallet/eth-lattice-keyring": "1.2.2",
"@rabby-wallet/eth-lattice-keyring": "1.2.4",
"@rabby-wallet/eth-simple-keyring": "5.0.1",
"@rabby-wallet/eth-trezor-keyring": "2.6.1",
"@rabby-wallet/eth-trezor-keyring": "2.6.3",
"@rabby-wallet/eth-walletconnect-keyring": "2.1.5",
"@rabby-wallet/eth-watch-keyring": "1.0.0",
"@rabby-wallet/gnosis-sdk": "1.3.10",
Expand Down
49 changes: 17 additions & 32 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { matomoRequestEvent } from '@/utils/matomo-request';
import * as Sentry from '@sentry/browser';
import Common, { Hardfork } from '@ethereumjs/common';
import { Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory } from '@ethereumjs/tx';
import { ethers } from 'ethers';
import {
bufferToHex,
isHexString,
addHexPrefix,
intToHex,
} from 'ethereumjs-util';
bufferToHex,
} from '@ethereumjs/util';
import { stringToHex } from 'web3-utils';
import { ethErrors } from 'eth-rpc-errors';
import {
Expand Down Expand Up @@ -87,6 +86,13 @@ const reportSignText = (params: {
});
};

const convertToHex = (data: Buffer | bigint) => {
if (typeof data === 'bigint') {
return `0x${data.toString(16)}`;
}
return bufferToHex(data);
};

interface ApprovalRes extends Tx {
type?: string;
address?: string;
Expand Down Expand Up @@ -488,6 +494,7 @@ class ProviderController extends BaseController {
opts
);
} catch (e) {
console.error(e);
const errObj =
typeof e === 'object'
? { message: e.message }
Expand Down Expand Up @@ -540,9 +547,9 @@ class ProviderController extends BaseController {
const _rawTx = {
...rawTx,
...approvalRes,
r: bufferToHex(signedTx.r),
s: bufferToHex(signedTx.s),
v: bufferToHex(signedTx.v),
r: convertToHex(signedTx.r),
s: convertToHex(signedTx.s),
v: convertToHex(signedTx.v),
};
if (is1559) {
delete _rawTx.gasPrice;
Expand Down Expand Up @@ -659,28 +666,6 @@ class ProviderController extends BaseController {
return signedTx;
}

const buildTx = TransactionFactory.fromTxData({
...approvalRes,
r: addHexPrefix(signedTx.r),
s: addHexPrefix(signedTx.s),
v: addHexPrefix(signedTx.v),
type: is1559 ? '0x2' : '0x0',
});

// Report address type(not sensitive information) to sentry when tx signature is invalid
if (!buildTx.verifySignature()) {
if (!buildTx.v) {
Sentry.captureException(new Error(`v missed, ${keyring.type}`));
} else if (!buildTx.s) {
Sentry.captureException(new Error(`s missed, ${keyring.type}`));
} else if (!buildTx.r) {
Sentry.captureException(new Error(`r missed, ${keyring.type}`));
} else {
Sentry.captureException(
new Error(`invalid signature, ${keyring.type}`)
);
}
}
signedTransactionSuccess = true;
statsData.signed = true;
statsData.signedSuccess = true;
Expand Down Expand Up @@ -728,9 +713,9 @@ class ProviderController extends BaseController {
const res = await openapiService.submitTx({
tx: {
...approvalRes,
r: bufferToHex(signedTx.r),
s: bufferToHex(signedTx.s),
v: bufferToHex(signedTx.v),
r: convertToHex(signedTx.r),
s: convertToHex(signedTx.s),
v: convertToHex(signedTx.v),
value: approvalRes.value || '0x0',
},
push_type: pushType,
Expand Down
2 changes: 1 addition & 1 deletion src/background/controller/provider/rpcFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { resemblesETHAddress } from '@/utils';
import { ProviderRequest } from './type';
import * as Sentry from '@sentry/browser';
import stats from '@/stats';
import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
import { addHexPrefix, stripHexPrefix } from '@ethereumjs/util';
import { findChain } from '@/utils/chain';
import { waitSignComponentAmounted } from '@/utils/signEvent';
import { gnosisController } from './gnosisController';
Expand Down
32 changes: 15 additions & 17 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import * as ethUtil from 'ethereumjs-util';
import {
stripHexPrefix,
isValidPrivate,
addHexPrefix,
toChecksumAddress,
} from '@ethereumjs/util';
import { ethErrors } from 'eth-rpc-errors';
import { ethers, Contract } from 'ethers';
import { groupBy, isEqual, sortBy, truncate, uniq } from 'lodash';
Expand Down Expand Up @@ -80,11 +85,6 @@ import stats from '@/stats';
import { generateAliasName } from '@/utils/account';
import BigNumber from 'bignumber.js';
import * as Sentry from '@sentry/browser';
import {
addHexPrefix,
unpadHexString,
toChecksumAddress,
} from 'ethereumjs-util';
import PQueue from 'p-queue';
import { ProviderRequest } from './provider/type';
import { QuoteResult } from '@rabby-wallet/rabby-swap/dist/quote';
Expand Down Expand Up @@ -305,11 +305,9 @@ export class WalletController extends BaseController {
params.to = to;
delete params.data;
params.value = addHexPrefix(
unpadHexString(
((abiCoder as unknown) as AbiCoder).encodeParameter(
'uint256',
rawAmount
)
((abiCoder as unknown) as AbiCoder).encodeParameter(
'uint256',
rawAmount
)
);
}
Expand Down Expand Up @@ -2984,12 +2982,12 @@ export class WalletController extends BaseController {
};

validatePrivateKey = async (data: string) => {
const privateKey = ethUtil.stripHexPrefix(data);
const privateKey = stripHexPrefix(data);
const buffer = Buffer.from(privateKey, 'hex');

const error = new Error(t('background.error.invalidPrivateKey'));
try {
if (!ethUtil.isValidPrivate(buffer)) {
if (!isValidPrivate(buffer)) {
throw error;
}
} catch {
Expand All @@ -2998,12 +2996,12 @@ export class WalletController extends BaseController {
};

importPrivateKey = async (data) => {
const privateKey = ethUtil.stripHexPrefix(data);
const privateKey = stripHexPrefix(data);
const buffer = Buffer.from(privateKey, 'hex');

const error = new Error(t('background.error.invalidPrivateKey'));
try {
if (!ethUtil.isValidPrivate(buffer)) {
if (!isValidPrivate(buffer)) {
throw error;
}
} catch {
Expand Down Expand Up @@ -3033,7 +3031,7 @@ export class WalletController extends BaseController {

const privateKey = wallet.getPrivateKeyString();
const keyring = await keyringService.importPrivateKey(
ethUtil.stripHexPrefix(privateKey)
stripHexPrefix(privateKey)
);
return this._setCurrentAccountFromKeyring(keyring);
};
Expand Down Expand Up @@ -3698,7 +3696,7 @@ export class WalletController extends BaseController {
options?: any;
}) => {
if (data.startsWith('0x')) {
const stripped = ethUtil.stripHexPrefix(data);
const stripped = stripHexPrefix(data);
const buff = Buffer.from(stripped, 'hex');
data = JSON.parse(buff.toString('utf8'));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/background/service/customTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CHAINS_ENUM } from '@debank/common';
import { GasLevel, Tx } from 'background/service/openapi';
import { createPersistStore, withTimeout } from 'background/utils';
import { BigNumber } from 'bignumber.js';
import { intToHex } from 'ethereumjs-util';
import { intToHex } from '@ethereumjs/util';
import { omitBy, sortBy } from 'lodash';
import { createClient, defineChain, erc20Abi, http, isAddress } from 'viem';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'regenerator-runtime/runtime';
import EventEmitter from 'events';
import * as ethUtil from 'ethereumjs-util';
import { toChecksumAddress } from '@ethereumjs/util';
import * as sigUtil from '@metamask/eth-sig-util';
import {
TypedTransaction,
FeeMarketEIP1559Transaction,
Transaction,
JsonTx,
TransactionFactory,
AccessListEIP2930Transaction,
Transaction,
} from '@ethereumjs/tx';
import { EVENTS } from '@/constant';
import { BitBox02BridgeInterface } from './bitbox02-bridge-interface';
import { bufferToHex } from '@ethereumjs/util';

const hdPathString = "m/44'/60'/0'/0";
const keyringType = 'BitBox02 Hardware';
Expand Down Expand Up @@ -127,7 +128,7 @@ class BitBox02Keyring extends EventEmitter {
balance: null,
index: i + 1,
});
this.paths[ethUtil.toChecksumAddress(address)] = i;
this.paths[toChecksumAddress(address)] = i;
}
return accounts;
}
Expand All @@ -148,7 +149,7 @@ class BitBox02Keyring extends EventEmitter {
balance: null,
index: i + 1,
});
this.paths[ethUtil.toChecksumAddress(address)] = i;
this.paths[toChecksumAddress(address)] = i;
}
return accounts;
}
Expand All @@ -175,10 +176,10 @@ class BitBox02Keyring extends EventEmitter {
let result;
const txData: JsonTx = {
to: tx.to!.toString(),
value: `0x${tx.value.toString('hex')}`,
value: `0x${tx.value.toString(16)}`,
data: this._normalize(tx.data),
nonce: `0x${tx.nonce.toString('hex')}`,
gasLimit: `0x${tx.gasLimit.toString('hex')}`,
nonce: `0x${tx.nonce.toString(16)}`,
gasLimit: `0x${tx.gasLimit.toString(16)}`,
};

if (tx instanceof FeeMarketEIP1559Transaction) {
Expand All @@ -187,30 +188,28 @@ class BitBox02Keyring extends EventEmitter {
tx.toJSON()
);
txData.type = '0x02';
txData.maxPriorityFeePerGas = `0x${tx.maxPriorityFeePerGas.toString(
'hex'
)}`;
txData.maxFeePerGas = `0x${tx.maxFeePerGas.toString('hex')}`;
txData.maxPriorityFeePerGas = `0x${tx.maxPriorityFeePerGas.toString(16)}`;
txData.maxFeePerGas = `0x${tx.maxFeePerGas.toString(16)}`;
} else if (
tx instanceof Transaction ||
tx instanceof AccessListEIP2930Transaction
) {
result = await this.bridge.ethSignTransaction(
tx.common.chainIdBN().toNumber(),
Number(tx.common.chainId()),
this._pathFromAddress(address),
tx.toJSON()
);
txData.gasPrice = `0x${tx.gasPrice.toString('hex')}`;
txData.gasPrice = `0x${tx.gasPrice.toString(16)}`;
}
txData.chainId = `0x${tx.common.chainIdBN().toString('hex')}`;
txData.r = result.r;
txData.s = result.s;
txData.v = result.v;
txData.chainId = `0x${tx.common.chainId().toString(16)}`;
txData.r = bufferToHex(result.r);
txData.s = bufferToHex(result.s);
txData.v = bufferToHex(result.v);
const signedTx = TransactionFactory.fromTxData(txData);
const addressSignedWith = ethUtil.toChecksumAddress(
const addressSignedWith = toChecksumAddress(
signedTx.getSenderAddress().toString()
);
const correctAddress = ethUtil.toChecksumAddress(address);
const correctAddress = toChecksumAddress(address);
if (addressSignedWith !== correctAddress) {
throw new Error('signature doesnt match the right address');
}
Expand Down Expand Up @@ -263,8 +262,7 @@ class BitBox02Keyring extends EventEmitter {
version: options.version,
});
if (
ethUtil.toChecksumAddress(addressSignedWith) !==
ethUtil.toChecksumAddress(withAccount)
toChecksumAddress(addressSignedWith) !== toChecksumAddress(withAccount)
) {
throw new Error('The signature doesnt match the right address');
}
Expand All @@ -285,7 +283,7 @@ class BitBox02Keyring extends EventEmitter {
/* PRIVATE METHODS */

_normalize(buf: Buffer): string {
return ethUtil.bufferToHex(buf).toString();
return bufferToHex(buf);
}

// eslint-disable-next-line no-shadow
Expand All @@ -294,11 +292,11 @@ class BitBox02Keyring extends EventEmitter {
const address = ethUtil
.publicToAddress(dkey.publicKey, true)
.toString('hex');
return ethUtil.toChecksumAddress(`0x${address}`);
return toChecksumAddress(`0x${address}`);
}

_pathFromAddress(address: string): string {
const checksummedAddress = ethUtil.toChecksumAddress(address);
const checksummedAddress = toChecksumAddress(address);
let index = this.paths[checksummedAddress];
if (typeof index === 'undefined') {
for (let i = 0; i < MAX_INDEX; i++) {
Expand All @@ -320,7 +318,7 @@ class BitBox02Keyring extends EventEmitter {
const addrs = await this.getAccounts();

return addrs.map((address) => {
const checksummedAddress = ethUtil.toChecksumAddress(address);
const checksummedAddress = toChecksumAddress(address);
return {
address,
index: this.paths[checksummedAddress] + 1,
Expand Down
3 changes: 1 addition & 2 deletions src/background/service/keyring/eth-cobo-argus-keyring.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { addHexPrefix } from 'ethereumjs-util';
import { addHexPrefix } from '@ethereumjs/util';
import EventEmitter from 'events';
import { t } from 'i18next';
import { isAddress } from 'web3-utils';

export const keyringType = 'CoboArgus';
Expand Down
Loading
Loading