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

update @ethereumjs deps #1207

Merged
merged 21 commits into from
Nov 30, 2022
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
2 changes: 1 addition & 1 deletion main/accounts/Account/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import log from 'electron-log'
import { isValidAddress, addHexPrefix } from 'ethereumjs-util'
import { isValidAddress, addHexPrefix } from '@ethereumjs/util'

import { AccessRequest, AccountRequest, Accounts, RequestMode, TransactionRequest } from '..'
import nebulaApi from '../../nebula'
Expand Down
2 changes: 1 addition & 1 deletion main/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventEmitter from 'events'
import log from 'electron-log'
import { Notification } from 'electron'
import { addHexPrefix, intToHex} from 'ethereumjs-util'
import { addHexPrefix, intToHex } from '@ethereumjs/util'
import { v5 as uuidv5 } from 'uuid'

import provider from '../provider'
Expand Down
9 changes: 4 additions & 5 deletions main/chains/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { BN } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Common } from '@ethereumjs/common'

function chainConfig (chain: number, hardfork: string) {
const chainId = new BN(chain)
const chainId = BigInt(chain)

return Common.isSupportedChainId(chainId)
? new Common({ chain: chainId.toNumber(), hardfork })
: Common.custom({ chainId: chainId.toNumber() }, { baseChain: 'mainnet', hardfork })
? new Common({ chain: chainId, hardfork })
: Common.custom({ chainId }, { baseChain: 'mainnet', hardfork })
}

export default chainConfig
2 changes: 1 addition & 1 deletion main/chains/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common from '@ethereumjs/common'
import { Common } from '@ethereumjs/common'
import { chainsType } from '@ethereumjs/common/dist/types'
import { EventEmitter } from 'stream'

Expand Down
2 changes: 1 addition & 1 deletion main/chains/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// status = Network Mismatch, Not Connected, Connected, Standby, Syncing

const EventEmitter = require('events')
const { addHexPrefix } = require('ethereumjs-util')
const { addHexPrefix } = require('@ethereumjs/util')
const { Hardfork } = require('@ethereumjs/common')
const provider = require('eth-provider')
const log = require('electron-log')
Expand Down
2 changes: 1 addition & 1 deletion main/contracts/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionDescription } from '@ethersproject/abi'
import { Contract } from '@ethersproject/contracts'
import { Web3Provider } from '@ethersproject/providers'
import { addHexPrefix } from 'ethereumjs-util'
import { addHexPrefix } from '@ethereumjs/util'
import log from 'electron-log'
import erc20Abi from '../externalData/balances/erc-20-abi'
import provider from '../provider'
Expand Down
2 changes: 1 addition & 1 deletion main/crypt/typedDataUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Translated to JavaScript from https://github.com/dicether/eip712/blob/master/src/eip712.ts
const abi = require('ethereumjs-abi')
const ethUtil = require('ethereumjs-util')
const ethUtil = require('@ethereumjs/util')

const PRIMITIVE_TYPES = [
/^bytes[0-9]|[0-2][0-9]|3[0-2]$/,
Expand Down
2 changes: 1 addition & 1 deletion main/externalData/balances/scan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js'
import { BigNumber as EthersBigNumber } from '@ethersproject/bignumber'
import { Interface } from '@ethersproject/abi'
import { addHexPrefix } from 'ethereumjs-util'
import { addHexPrefix } from '@ethereumjs/util'
import log from 'electron-log'

import multicall, { Call, supportsChain as multicallSupportsChain } from '../../multicall'
Expand Down
2 changes: 1 addition & 1 deletion main/multicall/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Interface } from '@ethersproject/abi'
import { addHexPrefix } from 'ethereumjs-util'
import { addHexPrefix } from '@ethereumjs/util'
import log from 'electron-log'

import type { BytesLike } from '@ethersproject/bytes'
Expand Down
12 changes: 8 additions & 4 deletions main/provider/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
pubToAddress,
ecrecover,
hashPersonalMessage,
} from 'ethereumjs-util'
} from '@ethereumjs/util'
import log from 'electron-log'
import BN from 'bignumber.js'
import { v5 as uuidv5 } from 'uuid'
Expand Down Expand Up @@ -69,6 +69,11 @@ export function feeTotalOverMax (rawTx: TransactionData, maxTotalFee: number) {
return totalFee > maxTotalFee
}

function parseValue(value = '') {
const parsedHex = parseInt(value, 16)
return (!!parsedHex && addHexPrefix(unpadHexString(value)) || '0x0')
}

export function getRawTx (newTx: RPC.SendTransaction.TxParams, accountId: string | undefined): TransactionData {
const { gas, gasLimit, gasPrice, data, value, type, to, ...rawTx } = newTx
const getNonce = () => {
Expand All @@ -84,13 +89,12 @@ export function getRawTx (newTx: RPC.SendTransaction.TxParams, accountId: string
}
return addHexPrefix(nonceBN.toString(16))
}
const parsedValue = !value || parseInt(value, 16) === 0 ? '0x0' : addHexPrefix(unpadHexString(value) || '0')

const tx: TransactionData = {
...rawTx,
from: rawTx.from || accountId,
type: '0x0',
value: parsedValue,
value: parseValue(value),
data: addHexPrefix(padToEven(stripHexPrefix(data || '0x'))),
gasLimit: gasLimit || gas,
chainId: rawTx.chainId,
Expand Down Expand Up @@ -131,7 +135,7 @@ export function getSignedAddress (signed: string, message: string, cb: Callback<
const r = toBuffer(signature.slice(0, 32))
const s = toBuffer(signature.slice(32, 64))
const hash = hashPersonalMessage(toBuffer(message))
const verifiedAddress = '0x' + pubToAddress(ecrecover(hash, v, r, s)).toString('hex')
const verifiedAddress = '0x' + pubToAddress(ecrecover(hash, BigInt(v), r, s)).toString('hex')
cb(null, verifiedAddress)
}

Expand Down
2 changes: 1 addition & 1 deletion main/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isHexString,
isHexPrefixed,
fromUtf8,
} from 'ethereumjs-util'
} from '@ethereumjs/util'

import store from '../store'
import packageFile from '../../package.json'
Expand Down
2 changes: 1 addition & 1 deletion main/reveal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import log from 'electron-log'
import EthereumProvider from 'ethereum-provider'
import { addHexPrefix } from 'ethereumjs-util'
import { addHexPrefix } from '@ethereumjs/util'
import BigNumber from 'bignumber.js'

import proxyConnection from '../provider/proxy'
Expand Down
2 changes: 1 addition & 1 deletion main/signers/Signer/derive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import HDKey from 'hdkey'

import { publicToAddress, toChecksumAddress } from 'ethereumjs-util'
import { publicToAddress, toChecksumAddress } from '@ethereumjs/util'

export enum Derivation {
live = 'live', legacy = 'legacy', standard = 'standard', testnet = 'testnet'
Expand Down
2 changes: 1 addition & 1 deletion main/signers/Signer/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import log from 'electron-log'
import EventEmitter from 'stream'
import { addHexPrefix } from 'ethereumjs-util'
import { addHexPrefix } from '@ethereumjs/util'

import { deriveHDAccounts } from './derive'
import crypt from '../../crypt'
Expand Down
18 changes: 8 additions & 10 deletions main/signers/hot/HotSigner/worker.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
const crypto = require('crypto')
const { signTypedData } = require('@metamask/eth-sig-util')
const { TransactionFactory } = require('@ethereumjs/tx')
const Common = require('@ethereumjs/common').default

const { Common } = require('@ethereumjs/common')
const {
BN,
hashPersonalMessage,
toBuffer,
ecsign,
addHexPrefix,
pubToAddress,
ecrecover
} = require('ethereumjs-util')
} = require('@ethereumjs/util')

function chainConfig (chain, hardfork) {
const chainId = new BN(chain)
const chainId = BigInt(chain)

return Common.isSupportedChainId(chainId)
? new Common({ chain: chainId.toNumber(), hardfork })
: Common.custom({ chainId: chainId.toNumber() }, { baseChain: 'mainnet', hardfork })
? new Common({ chain: chainId, hardfork })
: Common.custom({ chainId: chainId }, { baseChain: 'mainnet', hardfork })
}

class HotSignerWorker {
Expand Down Expand Up @@ -51,7 +49,7 @@ class HotSignerWorker {
const signed = ecsign(hash, key)

// Return serialized signed message
const hex = Buffer.concat([Buffer.from(signed.r), Buffer.from(signed.s), Buffer.from([signed.v])]).toString('hex')
const hex = Buffer.concat([signed.r, signed.s, Buffer.from([Number(signed.v)])]).toString('hex')

pseudoCallback(null, addHexPrefix(hex))
}
Expand All @@ -72,7 +70,7 @@ class HotSignerWorker {
return pseudoCallback('could not determine chain id for transaction')
}

const chainId = parseInt(rawTx.chainId)
const chainId = parseInt(rawTx.chainId, 16)
const hardfork = parseInt(rawTx.type) === 2 ? 'london' : 'berlin'
const common = chainConfig(chainId, hardfork)

Expand All @@ -94,7 +92,7 @@ class HotSignerWorker {
if (signature.length !== 65) return pseudoCallback(new Error('Frame verifyAddress signature has incorrect length'))
// Verify address
let v = signature[64]
v = v === 0 || v === 1 ? v + 27 : v
v = BigInt(v === 0 || v === 1 ? v + 27 : v)
const r = toBuffer(signature.slice(0, 32))
const s = toBuffer(signature.slice(32, 64))
const hash = hashPersonalMessage(toBuffer(message))
Expand Down
2 changes: 1 addition & 1 deletion main/signers/hot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const crypt = require('../../crypt')

const SeedSigner = require('./SeedSigner')
const RingSigner = require('./RingSigner')
const { stripHexPrefix } = require('ethereumjs-util')
const { stripHexPrefix } = require('@ethereumjs/util')

const USER_DATA = app ? app.getPath('userData') : path.resolve(path.dirname(require.main.filename), '../.userData');
const SIGNERS_PATH = path.resolve(USER_DATA, 'signers')
Expand Down
11 changes: 6 additions & 5 deletions main/signers/lattice/Lattice/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Client, Utils, Constants } from 'gridplus-sdk'
import { encode } from 'rlp'
import { padToEven, addHexPrefix } from 'ethereumjs-util'
import { hexToInt } from '../../../../resources/utils'
import log from 'electron-log'
import { encode } from 'rlp'
import { Client, Utils, Constants } from 'gridplus-sdk'
import { padToEven, addHexPrefix } from '@ethereumjs/util'
import { TypedTransaction } from '@ethereumjs/tx'
import { SignTypedDataVersion } from '@metamask/eth-sig-util'

import Signer from '../../Signer'
import { sign, signerCompatibility, londonToLegacy } from '../../../transaction'
import { TransactionData } from '../../../../resources/domain/transaction'
import { Derivation, getDerivationPath } from '../../Signer/derive'
import { hexToInt } from '../../../../resources/utils'

import type { TypedData, TypedMessage } from '../../../accounts/types'
import type { TransactionData } from '../../../../resources/domain/transaction'

const ADDRESS_LIMIT = 10
const HARDENED_OFFSET = 0x80000000
Expand Down
13 changes: 7 additions & 6 deletions main/signers/ledger/Ledger/eth.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { rlp, addHexPrefix, stripHexPrefix, padToEven } from 'ethereumjs-util'
import { SignTypedDataVersion, TypedDataUtils } from '@metamask/eth-sig-util'
import log from 'electron-log'

import { encode } from 'rlp'
import { addHexPrefix, stripHexPrefix, padToEven } from '@ethereumjs/util'
import { SignTypedDataVersion, TypedDataUtils } from '@metamask/eth-sig-util'
import Transport from '@ledgerhq/hw-transport'
import Eth from '@ledgerhq/hw-app-eth'

import { Derivation, getDerivationPath, deriveHDAccounts } from '../../Signer/derive'
import { TransactionData } from '../../../../resources/domain/transaction'
import { sign } from '../../../transaction'
import { DeviceError } from '.'

import type { TypedData } from '../../../accounts/types'
import type { TransactionData } from '../../../../resources/domain/transaction'

export default class LedgerEthereumApp {
private eth: Eth;
private eth: Eth

constructor (transport: Transport) {
this.eth = new Eth(transport)
Expand Down Expand Up @@ -73,7 +74,7 @@ export default class LedgerEthereumApp {
// legacy transactions aren't RLP encoded before they're returned
const message = tx.getMessageToSign(false)
const legacyMessage = message[0] !== tx.type
const rawTxHex = legacyMessage ? rlp.encode(message).toString('hex') : message.toString('hex')
const rawTxHex = legacyMessage ? Buffer.from(encode(message)).toString('hex') : message.toString('hex')

return this.eth.signTransaction(path, rawTxHex, null)
})
Expand Down
2 changes: 1 addition & 1 deletion main/signers/trezor/Trezor/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import log from 'electron-log'
import { hexToInt } from '../../../../resources/utils'
import { padToEven, stripHexPrefix, addHexPrefix } from 'ethereumjs-util'
import { padToEven, stripHexPrefix, addHexPrefix } from '@ethereumjs/util'
import { SignTypedDataVersion, TypedDataUtils } from '@metamask/eth-sig-util'
import type { Device as TrezorDevice } from 'trezor-connect'
import { TypedTransaction } from '@ethereumjs/tx'
Expand Down
2 changes: 1 addition & 1 deletion main/transaction/gasCalculator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intToHex } from 'ethereumjs-util'
import { intToHex } from '@ethereumjs/util'

interface FeeHistoryResponse {
baseFeePerGas: string[]
Expand Down
43 changes: 25 additions & 18 deletions main/transaction/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BN, addHexPrefix, stripHexPrefix, bnToHex, intToHex } from 'ethereumjs-util'
import BigNumber from 'bignumber.js'
import { addHexPrefix, intToHex } from '@ethereumjs/util'
import { TransactionFactory, TypedTransaction } from '@ethereumjs/tx'
import Common from '@ethereumjs/common'
import { Common } from '@ethereumjs/common'

import chainConfig from '../chains/config'
import { AppVersion, SignerSummary } from '../signers/Signer'
import { GasFeesSource, TransactionData, typeSupportsBaseFee } from '../../resources/domain/transaction'
import {
GasFeesSource,
TransactionData,
typeSupportsBaseFee,
} from '../../resources/domain/transaction'
import chainConfig from '../chains/config'

const londonHardforkSigners: SignerCompatibilityByVersion = {
seed: () => true,
Expand Down Expand Up @@ -44,11 +49,10 @@ export interface SignerCompatibility {
compatible: boolean
}

function toBN (hexStr: string) {
return new BN(stripHexPrefix(hexStr), 'hex')
}

function signerCompatibility (txData: TransactionData, signer: SignerSummary): SignerCompatibility {
function signerCompatibility(
txData: TransactionData,
signer: SignerSummary
): SignerCompatibility {
if (typeSupportsBaseFee(txData.type)) {
const compatible = (signer.type in londonHardforkSigners) && londonHardforkSigners[signer.type](signer.appVersion, signer.model)
return { signer: signer.type, tx: 'london', compatible }
Expand Down Expand Up @@ -87,7 +91,8 @@ function maxFee (rawTx: TransactionData) {
}

function calculateMaxFeePerGas(maxBaseFee: string, maxPriorityFee: string) {
return bnToHex(toBN(maxPriorityFee).add(toBN(maxBaseFee)))
const maxFeePerGas = BigNumber(maxPriorityFee).plus(maxBaseFee).toString(16)
return addHexPrefix(maxFeePerGas)
}

function populate (rawTx: TransactionData, chainConfig: Common, gas: GasData): TransactionData {
Expand All @@ -100,10 +105,10 @@ function populate (rawTx: TransactionData, chainConfig: Common, gas: GasData): T
const useFrameGasPrice = !rawTx.gasPrice || isNaN(parseInt(rawTx.gasPrice, 16))
if (useFrameGasPrice) {
// no valid dapp-supplied value for gasPrice so we use the Frame-supplied value
const gasPrice = toBN(gas.price.levels.fast as string)
txData.gasPrice = bnToHex(gasPrice)
const gasPrice = BigNumber(gas.price.levels.fast as string).toString(16)
txData.gasPrice = addHexPrefix(gasPrice)
txData.gasFeesSource = GasFeesSource.Frame
}
}

return txData
}
Expand All @@ -130,8 +135,10 @@ function populate (rawTx: TransactionData, chainConfig: Common, gas: GasData): T
txData.maxFeePerGas = useFrameMaxFeePerGas ? calculateMaxFeePerGas(gas.price.fees.maxBaseFeePerGas, maxPriorityFee) : txData.maxFeePerGas

// if no valid dapp-supplied value for maxPriorityFeePerGas we use the Frame-supplied value
txData.maxPriorityFeePerGas = useFrameMaxPriorityFeePerGas ? bnToHex(toBN(maxPriorityFee)) : txData.maxPriorityFeePerGas

txData.maxPriorityFeePerGas = useFrameMaxPriorityFeePerGas
? addHexPrefix(BigNumber(maxPriorityFee).toString(16))
: txData.maxPriorityFeePerGas

return txData
}

Expand All @@ -144,7 +151,7 @@ function hexifySignature ({ v, r, s }: Signature) {
}

async function sign (rawTx: TransactionData, signingFn: (tx: TypedTransaction) => Promise<Signature>) {
const common = chainConfig(parseInt(rawTx.chainId), parseInt(rawTx.type) === 2 ? 'london' : 'berlin')
const common = chainConfig(parseInt(rawTx.chainId, 16), parseInt(rawTx.type, 16) === 2 ? 'london' : 'berlin')

const tx = TransactionFactory.fromTxData(rawTx, { common })

Expand All @@ -153,8 +160,8 @@ async function sign (rawTx: TransactionData, signingFn: (tx: TypedTransaction) =

return TransactionFactory.fromTxData(
{
...rawTx,
...signature
...rawTx,
...signature
},
{ common }
)
Expand Down
Loading