From c3e3981c0137018910a02c551a47158f8c756431 Mon Sep 17 00:00:00 2001 From: Keith Broughton <101608096+keithbro-imx@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:31:20 +1000 Subject: [PATCH] fix: Change ethers.utils imports (#1915) --- .../Transactions/TransactionList.tsx | 4 ++-- .../internal/bridge/sdk/src/lib/axelarUtils.ts | 9 ++++----- .../internal/bridge/sdk/src/tokenBridge.ts | 18 +++++++++--------- .../dex/sdk/src/lib/transactionUtils/swap.ts | 14 +++++++------- .../EthSendTransactionExamples/NFTApproval.tsx | 4 ++-- .../EthSendTransactionExamples/NFTTransfer.tsx | 4 ++-- .../SpendingCapApproval.tsx | 4 ++-- .../TransferERC20.tsx | 4 ++-- .../EthSendTransactionExamples/TransferImx.tsx | 4 ++-- .../x-client/src/utils/crypto/crypto.test.ts | 18 +++++++++++++++++- packages/x-client/src/utils/crypto/crypto.ts | 4 ++-- 11 files changed, 51 insertions(+), 36 deletions(-) diff --git a/packages/checkout/widgets-lib/src/components/Transactions/TransactionList.tsx b/packages/checkout/widgets-lib/src/components/Transactions/TransactionList.tsx index 628d0d1abf..ad388e7012 100644 --- a/packages/checkout/widgets-lib/src/components/Transactions/TransactionList.tsx +++ b/packages/checkout/widgets-lib/src/components/Transactions/TransactionList.tsx @@ -12,7 +12,7 @@ import { AXELAR_SCAN_URL } from 'lib'; import { Transaction, TransactionStatus } from 'lib/clients'; import { CryptoFiatContext } from 'context/crypto-fiat-context/CryptoFiatContext'; import { calculateCryptoToFiat, getTokenImageByAddress, isNativeToken } from 'lib/utils'; -import { formatUnits } from 'ethers/lib/utils'; +import { utils } from 'ethers'; import { useTranslation } from 'react-i18next'; import { TransactionItem } from './TransactionItem'; import { KnownNetworkMap } from './transactionsType'; @@ -81,7 +81,7 @@ export function TransactionList({ const hash = transaction.blockchain_metadata.transaction_hash; const tokens = knownTokenMap[transaction.details.from_chain]; const token = tokens[transaction.details.from_token_address.toLowerCase()]; - const amount = formatUnits(transaction.details.amount, token.decimals); + const amount = utils.formatUnits(transaction.details.amount, token.decimals); const fiat = calculateCryptoToFiat(amount, token.symbol, cryptoFiatState.conversions); if (transaction.details.current_status.status === TransactionStatus.WITHDRAWAL_PENDING) { diff --git a/packages/internal/bridge/sdk/src/lib/axelarUtils.ts b/packages/internal/bridge/sdk/src/lib/axelarUtils.ts index 903f37412e..952d63636e 100644 --- a/packages/internal/bridge/sdk/src/lib/axelarUtils.ts +++ b/packages/internal/bridge/sdk/src/lib/axelarUtils.ts @@ -3,8 +3,7 @@ import { Address } from 'types'; import { WITHDRAW_SIG, NATIVE } from 'constants/bridges'; import { CHILD_ERC20 } from 'contracts/ABIs/ChildERC20'; import { withBridgeError, BridgeErrorType } from 'errors'; -import { ethers } from 'ethers'; -import { keccak256, defaultAbiCoder } from 'ethers/lib/utils'; +import { ethers, utils } from 'ethers'; import { createContract } from 'contracts/createContract'; import { isWrappedIMX, getRootIMX } from './utils'; @@ -16,8 +15,8 @@ import { isWrappedIMX, getRootIMX } from './utils'; * @returns hash of payload and current time. */ export function genUniqueAxelarCommandId(payload: string) { - return keccak256( - defaultAbiCoder.encode(['bytes', 'uint256'], [payload, new Date().getTime()]), + return utils.keccak256( + utils.defaultAbiCoder.encode(['bytes', 'uint256'], [payload, new Date().getTime()]), ); } @@ -31,7 +30,7 @@ export function genAxelarWithdrawPayload( recipient: string, amount: string, ) { - return defaultAbiCoder.encode( + return utils.defaultAbiCoder.encode( ['bytes32', 'address', 'address', 'address', 'uint256'], [WITHDRAW_SIG, rootToken, sender, recipient, amount], ); diff --git a/packages/internal/bridge/sdk/src/tokenBridge.ts b/packages/internal/bridge/sdk/src/tokenBridge.ts index 0eefd16eb7..d5b50f5df1 100644 --- a/packages/internal/bridge/sdk/src/tokenBridge.ts +++ b/packages/internal/bridge/sdk/src/tokenBridge.ts @@ -1,10 +1,7 @@ /* eslint-disable no-console */ /* eslint-disable class-methods-use-this */ import axios, { AxiosResponse } from 'axios'; -import { ethers } from 'ethers'; -import { - concat, defaultAbiCoder, hexValue, hexlify, keccak256, zeroPad, -} from 'ethers/lib/utils'; +import { ethers, utils } from 'ethers'; import { ROOT_AXELAR_ADAPTOR } from 'contracts/ABIs/RootAxelarBridgeAdaptor'; import { checkReceiver, validateBridgeReqArgs, validateChainConfiguration, validateChainIds, @@ -853,7 +850,7 @@ export class TokenBridge { // tx value for simulation mocked as amount + 1 wei for a native bridge and 1 wei for token bridges // hexValue() is required to remove leading zeros, which tenderly does not support. - const txValue = (token.toUpperCase() !== NATIVE) ? '0x1' : hexValue(amount.add('1').toHexString()); + const txValue = (token.toUpperCase() !== NATIVE) ? '0x1' : utils.hexValue(amount.add('1').toHexString()); simulations.push({ from: sender, @@ -895,15 +892,18 @@ export class TokenBridge { const sourceChain = getChildchain(destinationChainId); const sourceAddress = ethers.utils.getAddress(getChildAdaptor(destinationChainId)).toString(); const destinationAddress = getRootAdaptor(destinationChainId); - const payloadHash = keccak256(payload); + const payloadHash = utils.keccak256(payload); // Calculate slot key for given command ID. - const command = defaultAbiCoder.encode( + const command = utils.defaultAbiCoder.encode( ['bytes32', 'bytes32', 'string', 'string', 'address', 'bytes32'], [SLOT_PREFIX_CONTRACT_CALL_APPROVED, commandId, sourceChain, sourceAddress, destinationAddress, payloadHash], ); - const commandHash = keccak256(command); - const slot = keccak256(concat([commandHash, hexlify(zeroPad(hexlify(SLOT_POS_CONTRACT_CALL_APPROVED), 32))])); + const commandHash = utils.keccak256(command); + const slot = utils.keccak256(utils.concat([ + commandHash, + utils.hexlify(utils.zeroPad(utils.hexlify(SLOT_POS_CONTRACT_CALL_APPROVED), 32)), + ])); // Encode execute data const axelarAdapterContract = await createContract( diff --git a/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts b/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts index 4006b31b57..adf2131336 100644 --- a/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts +++ b/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts @@ -6,9 +6,9 @@ import { isNative, toCurrencyAmount, toPublicAmount } from 'lib/utils'; import { QuoteResult } from 'lib/getQuotesForRoutes'; import { NativeTokenService, canUnwrapToken } from 'lib/nativeTokenService'; import { Coin, CoinAmount, Native } from 'types'; -import { Interface } from 'ethers/lib/utils'; import { IImmutableSwapProxy, ImmutableSwapProxyInterface } from 'contracts/types/ImmutableSwapProxy'; import { ImmutableSwapProxy__factory } from 'contracts/types'; +import { utils } from 'ethers'; import { SecondaryFee, TransactionDetails } from '../../types'; import { calculateGasFee } from './gas'; import { slippageToFraction } from './slippage'; @@ -30,8 +30,8 @@ function buildSinglePoolSwap( route: Route, amountIn: string, amountOut: string, - routerContract: Interface, - paymentsContract: Interface, + routerContract: utils.Interface, + paymentsContract: utils.Interface, ) { const calldatas: string[] = []; @@ -153,8 +153,8 @@ function buildMultiPoolSwap( route: Route, amountIn: string, amountOut: string, - routerContract: Interface, - paymentsContract: Interface, + routerContract: utils.Interface, + paymentsContract: utils.Interface, ) { const path: string = encodeRouteToPath(route, trade.tradeType === Uniswap.TradeType.EXACT_OUTPUT); const calldatas: string[] = []; @@ -280,8 +280,8 @@ function buildSwapParameters( trade: Trade, secondaryFees: SecondaryFee[], swapProxyContract: ImmutableSwapProxyInterface, - routerContract: Interface, - paymentsContract: Interface, + routerContract: utils.Interface, + paymentsContract: utils.Interface, maximumAmountIn: string, minimumAmountOut: string, ) { diff --git a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTApproval.tsx b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTApproval.tsx index a6737c0e2a..968125a757 100644 --- a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTApproval.tsx +++ b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTApproval.tsx @@ -5,8 +5,8 @@ import { Accordion, Form } from 'react-bootstrap'; import { usePassportProvider } from '@/context/PassportProvider'; import WorkflowButton from '@/components/WorkflowButton'; import { RequestExampleProps, EnvironmentNames } from '@/types'; -import { Interface } from 'ethers/lib/utils'; import { useImmutableProvider } from '@/context/ImmutableProvider'; +import { utils } from 'ethers'; enum ApproveType { NFTApprove = 'NFT_APPROVE', @@ -33,7 +33,7 @@ function NFTApproval({ disabled, handleExampleSubmitted }: RequestExampleProps) 'function setApprovalForAll(address to, bool approved)', 'function approve(address to, uint256 tokenId)', ]; - return new Interface(abi); + return new utils.Interface(abi); }, []); const [choosedApproveType, setChoosedApproveType] = useState(ApproveType.NFTApprove); diff --git a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTTransfer.tsx b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTTransfer.tsx index 0829c4c25c..ba1aa87d5f 100644 --- a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTTransfer.tsx +++ b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/NFTTransfer.tsx @@ -4,7 +4,6 @@ import React, { useMemo, useState, } from 'react'; -import { Interface } from 'ethers/lib/utils'; import InputGroup from 'react-bootstrap/InputGroup'; import { Accordion, Form, Image, Table, @@ -14,6 +13,7 @@ import { useImmutableProvider } from '@/context/ImmutableProvider'; import { usePassportProvider } from '@/context/PassportProvider'; import { NFT } from '@imtbl/generated-clients/dist/blockchain-data'; import WorkflowButton from '@/components/WorkflowButton'; +import { utils } from 'ethers'; type GroupedAsset = { contract_address: string; @@ -50,7 +50,7 @@ function NFTTransfer({ disabled, handleExampleSubmitted }: RequestExampleProps) 'function safeTransferFrom(address from, address to, uint256 token_id)', 'function safeTransferFromBatch((address, address[], uint256[]))', ]; - return new Interface(abi); + return new utils.Interface(abi); }, []); useEffect(() => { diff --git a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/SpendingCapApproval.tsx b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/SpendingCapApproval.tsx index 53a7d64311..8ca7559109 100644 --- a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/SpendingCapApproval.tsx +++ b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/SpendingCapApproval.tsx @@ -5,8 +5,8 @@ import { Accordion, Form } from 'react-bootstrap'; import { usePassportProvider } from '@/context/PassportProvider'; import WorkflowButton from '@/components/WorkflowButton'; import { RequestExampleProps, EnvironmentNames } from '@/types'; -import { Interface } from 'ethers/lib/utils'; import { useImmutableProvider } from '@/context/ImmutableProvider'; +import { utils } from 'ethers'; const getErc20DefaultContractAddress = (environment: EnvironmentNames) => { switch (environment) { @@ -27,7 +27,7 @@ function SpendingCapApproval({ disabled, handleExampleSubmitted }: RequestExampl const abi = [ 'function approve(address spender, uint256 amount)', ]; - return new Interface(abi); + return new utils.Interface(abi); }, []); const [fromAddress, setFromAddress] = useState(''); const [erc20ContractAddress, setErc20ContractAddress] = useState(getErc20DefaultContractAddress(environment)); diff --git a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferERC20.tsx b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferERC20.tsx index ad9e05dd68..d291a834c6 100644 --- a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferERC20.tsx +++ b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferERC20.tsx @@ -9,8 +9,8 @@ import InputGroup from 'react-bootstrap/InputGroup'; import { usePassportProvider } from '@/context/PassportProvider'; import WorkflowButton from '@/components/WorkflowButton'; import { RequestExampleProps, EnvironmentNames } from '@/types'; -import { Interface } from 'ethers/lib/utils'; import { useImmutableProvider } from '@/context/ImmutableProvider'; +import { utils } from 'ethers'; const getErc20DefaultContractAddress = (environment: EnvironmentNames) => { switch (environment) { @@ -42,7 +42,7 @@ function TransferERC20({ disabled, handleExampleSubmitted }: RequestExampleProps 'function transfer(address to, uint256 amount)', 'function transferFrom(address from, address to, uint256 amount)', ]; - return new Interface(abi); + return new utils.Interface(abi); }, []); useEffect(() => { diff --git a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferImx.tsx b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferImx.tsx index 3c375a3869..0a444c0600 100644 --- a/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferImx.tsx +++ b/packages/passport/sdk-sample-app/src/components/zkevm/EthSendTransactionExamples/TransferImx.tsx @@ -4,7 +4,7 @@ import InputGroup from 'react-bootstrap/InputGroup'; import { usePassportProvider } from '@/context/PassportProvider'; import WorkflowButton from '@/components/WorkflowButton'; import { RequestExampleProps } from '@/types'; -import { parseUnits } from 'ethers/lib/utils'; +import { utils } from 'ethers'; function TransferImx({ disabled, handleExampleSubmitted }: RequestExampleProps) { const [fromAddress, setFromAddress] = useState(''); @@ -23,7 +23,7 @@ function TransferImx({ disabled, handleExampleSubmitted }: RequestExampleProps) if (Number(rawAmount) < 0) { setAmountConvertError(amountRange); } - const value = parseUnits(rawAmount, imxTokenDecimal).toString(); + const value = utils.parseUnits(rawAmount, imxTokenDecimal).toString(); setParams([{ from: fromAddress, to: toAddress, diff --git a/packages/x-client/src/utils/crypto/crypto.test.ts b/packages/x-client/src/utils/crypto/crypto.test.ts index ea32c4bdd4..c6b9a052a1 100644 --- a/packages/x-client/src/utils/crypto/crypto.test.ts +++ b/packages/x-client/src/utils/crypto/crypto.test.ts @@ -1,6 +1,8 @@ import { getDefaultProvider } from '@ethersproject/providers'; import { Wallet } from '@ethersproject/wallet'; -import { signMessage, signRaw } from './crypto'; +import { signMessage, signRaw, signRegisterEthAddress } from './crypto'; +import { generateLegacyStarkPrivateKey } from '../stark/starkCurve'; +import { createStarkSigner } from '../stark/starkSigner'; describe('signRaw()', () => { test('Correctly signs string', async () => { @@ -36,3 +38,17 @@ describe('signMessage()', () => { }); }); }); + +describe('signRegisterEthAddress()', () => { + test('Correctly signs message', async () => { + const signer = new Wallet('5c7b4b5cad9a3fc7b1ba235a49cd74e615488a18b0d6a531739fd1062935104d'); + const starkKey = await generateLegacyStarkPrivateKey(signer); + const starkSigner = createStarkSigner(starkKey); + const starkPublicKey = await createStarkSigner(starkKey).getAddress(); + const ethSignature = await signRegisterEthAddress(starkSigner, signer.publicKey, starkPublicKey); + expect(ethSignature).toEqual( + // eslint-disable-next-line max-len + '0x03fd522589dd46a74cc7d004eea819c111294dbb1a1af9ccdb4d42565730134c05d33b76e5fe733314051420c2c76aea1f6d677394eb7b92369d2a2bab2674ab02603b092fe208e20cd5187a4ded4645e2f55605988c91579b3822bb63629e21', + ); + }); +}); diff --git a/packages/x-client/src/utils/crypto/crypto.ts b/packages/x-client/src/utils/crypto/crypto.ts index 188967de92..48c925e678 100644 --- a/packages/x-client/src/utils/crypto/crypto.ts +++ b/packages/x-client/src/utils/crypto/crypto.ts @@ -4,7 +4,7 @@ import BN from 'bn.js'; import elliptic from 'elliptic'; import * as encUtils from 'enc-utils'; import { Signer } from '@ethersproject/abstract-signer'; -import { solidityKeccak256 } from 'ethers/lib/utils'; +import { utils } from 'ethers'; import { StarkSigner } from '../../types'; import { starkEcOrder } from '../stark/starkCurve'; @@ -105,7 +105,7 @@ export async function signRegisterEthAddress( ethAddress: string, starkPublicKey: string, ): Promise { - const hash: string = solidityKeccak256( + const hash: string = utils.solidityKeccak256( ['string', 'address', 'uint256'], ['UserRegistration:', ethAddress, starkPublicKey], );