From 29a2481c8eb4a8f70e2371e9f7af158c48474e8c Mon Sep 17 00:00:00 2001 From: philogicae Date: Thu, 5 Sep 2024 13:54:09 +0300 Subject: [PATCH] Fix: message.chain=ETH for all EVM chains messages (else rejected) --- packages/avalanche/src/account.ts | 16 +++++++++------- packages/base/src/account.ts | 13 ++++++++----- packages/ethereum/src/account.ts | 14 ++++++++------ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/avalanche/src/account.ts b/packages/avalanche/src/account.ts index d9c33b4a..f3f21d18 100644 --- a/packages/avalanche/src/account.ts +++ b/packages/avalanche/src/account.ts @@ -3,10 +3,9 @@ import { KeyPair, KeyChain } from 'avalanche/dist/apis/avm' import { KeyPair as EVMKeyPair } from 'avalanche/dist/apis/evm' import { ethers, providers } from 'ethers' import { privateToAddress } from 'ethereumjs-util' - import { Blockchain } from '@aleph-sdk/core' import { SignableMessage, BaseProviderWallet } from '@aleph-sdk/account' -import { ChangeRpcParam, RpcId, EVMAccount, JsonRPCWallet } from '@aleph-sdk/evm' +import { ChangeRpcParam, RpcId, EVMAccount, JsonRPCWallet, ChainData } from '@aleph-sdk/evm' import { digestMessage, verifyAvalanche } from './verify' /** @@ -152,18 +151,21 @@ export async function importAccountFromMnemonic( /** * Get an account from a Web3 provider (ex: Metamask) * - * @param {providers.ExternalProvider} provider from metamask + * @param {providers.ExternalProvider | ethers.providers.Web3Provider} provider * @param requestedRpc Use this params to change the RPC endpoint; */ export async function getAccountFromProvider( - provider: providers.ExternalProvider, + provider: ethers.providers.ExternalProvider | ethers.providers.Web3Provider, requestedRpc: ChangeRpcParam = RpcId.AVAX, ): Promise { - const avaxProvider = new providers.Web3Provider(provider) - const jrw = new JsonRPCWallet(avaxProvider) - await jrw.changeNetwork(requestedRpc) + const ETHprovider = + provider instanceof ethers.providers.Web3Provider ? provider : new providers.Web3Provider(provider) + const jrw = new JsonRPCWallet(ETHprovider) + const chainId = Number((typeof requestedRpc === 'number' ? ChainData[requestedRpc] : requestedRpc).chainId) + if (chainId !== (await jrw.provider.getNetwork()).chainId) await jrw.changeNetwork(requestedRpc) await jrw.connect() + if (jrw.address) { return new AvalancheAccount(jrw, jrw.address) } diff --git a/packages/base/src/account.ts b/packages/base/src/account.ts index 185f36e1..f11df3cb 100644 --- a/packages/base/src/account.ts +++ b/packages/base/src/account.ts @@ -2,7 +2,7 @@ import * as bip39 from 'bip39' import { Blockchain } from '@aleph-sdk/core' import { ETHAccount } from '@aleph-sdk/ethereum' import { ethers } from 'ethers' -import { ChangeRpcParam, JsonRPCWallet, RpcId } from '@aleph-sdk/evm' +import { ChainData, ChangeRpcParam, JsonRPCWallet, RpcId } from '@aleph-sdk/evm' /** * BaseAccount implements the Account class for the Base protocol. @@ -55,16 +55,19 @@ export function newAccount(derivationPath = "m/44'/60'/0'/0/0"): { account: Base /** * Get an account from a Web3 provider (ex: Metamask) * - * @param {ethers.providers.ExternalProvider} provider + * @param {ethers.providers.ExternalProvider | ethers.providers.Web3Provider} provider * @param requestedRpc Use this params to change the RPC endpoint; */ export async function getAccountFromProvider( - provider: ethers.providers.ExternalProvider, + provider: ethers.providers.ExternalProvider | ethers.providers.Web3Provider, requestedRpc: ChangeRpcParam = RpcId.BASE, ): Promise { - const ETHprovider = new ethers.providers.Web3Provider(provider) + const ETHprovider = + provider instanceof ethers.providers.Web3Provider ? provider : new ethers.providers.Web3Provider(provider) const jrw = new JsonRPCWallet(ETHprovider) - await jrw.changeNetwork(requestedRpc) + + const chainId = Number((typeof requestedRpc === 'number' ? ChainData[requestedRpc] : requestedRpc).chainId) + if (chainId !== (await jrw.provider.getNetwork()).chainId) await jrw.changeNetwork(requestedRpc) await jrw.connect() if (jrw.address) return new BaseAccount(jrw, jrw.address) diff --git a/packages/ethereum/src/account.ts b/packages/ethereum/src/account.ts index ed11e648..cdecc566 100644 --- a/packages/ethereum/src/account.ts +++ b/packages/ethereum/src/account.ts @@ -1,9 +1,8 @@ import * as bip39 from 'bip39' import { ethers } from 'ethers' - import { Blockchain } from '@aleph-sdk/core' import { SignableMessage, BaseProviderWallet } from '@aleph-sdk/account' -import { ChangeRpcParam, JsonRPCWallet, RpcId, EVMAccount } from '@aleph-sdk/evm' +import { ChangeRpcParam, JsonRPCWallet, RpcId, EVMAccount, ChainData } from '@aleph-sdk/evm' /** * ETHAccount implements the Account class for the Ethereum protocol. @@ -96,16 +95,19 @@ export function newAccount(derivationPath = "m/44'/60'/0'/0/0"): { account: ETHA /** * Get an account from a Web3 provider (ex: Metamask) * - * @param {ethers.providers.ExternalProvider} provider + * @param {ethers.providers.ExternalProvider | ethers.providers.Web3Provider} provider * @param requestedRpc Use this params to change the RPC endpoint; */ export async function getAccountFromProvider( - provider: ethers.providers.ExternalProvider, + provider: ethers.providers.ExternalProvider | ethers.providers.Web3Provider, requestedRpc: ChangeRpcParam = RpcId.ETH, ): Promise { - const ETHprovider = new ethers.providers.Web3Provider(provider) + const ETHprovider = + provider instanceof ethers.providers.Web3Provider ? provider : new ethers.providers.Web3Provider(provider) const jrw = new JsonRPCWallet(ETHprovider) - await jrw.changeNetwork(requestedRpc) + + const chainId = Number((typeof requestedRpc === 'number' ? ChainData[requestedRpc] : requestedRpc).chainId) + if (chainId !== (await jrw.provider.getNetwork()).chainId) await jrw.changeNetwork(requestedRpc) await jrw.connect() if (jrw.address) return new ETHAccount(jrw, jrw.address)