Skip to content

Commit

Permalink
Fix: message.chain=ETH for all EVM chains messages (else rejected)
Browse files Browse the repository at this point in the history
  • Loading branch information
philogicae committed Sep 5, 2024
1 parent d0566be commit 29a2481
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
16 changes: 9 additions & 7 deletions packages/avalanche/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand Down Expand Up @@ -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<AvalancheAccount> {
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)
}
Expand Down
13 changes: 8 additions & 5 deletions packages/base/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<BaseAccount> {
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)
Expand Down
14 changes: 8 additions & 6 deletions packages/ethereum/src/account.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<ETHAccount> {
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)
Expand Down

0 comments on commit 29a2481

Please sign in to comment.