diff --git a/src/accounts/avalanche.ts b/src/accounts/avalanche.ts index 3262eee4..ee9c47fe 100644 --- a/src/accounts/avalanche.ts +++ b/src/accounts/avalanche.ts @@ -5,7 +5,7 @@ import { BaseMessage, Chain } from "../messages/message"; import { decrypt as secp256k1_decrypt, encrypt as secp256k1_encrypt } from "eciesjs"; import { KeyPair } from "avalanche/dist/apis/avm"; import { Avalanche, BinTools, Buffer as AvaBuff } from "avalanche"; -import { JsonRPCWallet } from "../providers/JsonRPCWallet"; +import { JsonRPCWallet, RpcChainType } from "../providers/JsonRPCWallet"; import { BaseProviderWallet } from "../providers/BaseProviderWallet"; import { providers } from "ethers"; @@ -141,8 +141,9 @@ export async function ImportAccountFromPrivateKey(privateKey: string): Promise { const avaxProvider = new providers.Web3Provider(provider); const jrw = new JsonRPCWallet(avaxProvider); - await jrw.connect(); + await jrw.changeNetwork(RpcChainType.AVAX); + await jrw.connect(); if (jrw.address) { return new AvalancheAccount(jrw, jrw.address); } diff --git a/src/providers/JsonRPCWallet.ts b/src/providers/JsonRPCWallet.ts index 56ec4c01..79aa8bbf 100644 --- a/src/providers/JsonRPCWallet.ts +++ b/src/providers/JsonRPCWallet.ts @@ -4,6 +4,36 @@ import { BaseProviderWallet } from "./BaseProviderWallet"; const RPC_WARNING = `DEPRECATION WARNING: Encryption/Decryption features may become obsolete, for more information: https://github.com/aleph-im/aleph-sdk-ts/issues/37`; +export enum RpcChainType { + ETH, + AVAX, +} + +const ChainData = { + [RpcChainType.AVAX]: { + chainId: "0xA86A", + rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"], + chainName: "Avalanche Mainnet", + nativeCurrency: { + name: "AVAX", + symbol: "AVAX", + decimals: 18, + }, + blockExplorerUrls: ["https://snowtrace.io"], + }, + [RpcChainType.ETH]: { + chainId: "0x1", + rpcUrls: ["https://mainnet.infura.io/v3/"], + chainName: "Ethereum Mainnet", + nativeCurrency: { + name: "ETH", + symbol: "ETH", + decimals: 18, + }, + blockExplorerUrls: ["https://etherscan.io"], + }, +}; + /** * Wrapper for JSON RPC Providers (ex: Metamask) */ @@ -51,4 +81,10 @@ export class JsonRPCWallet extends BaseProviderWallet { if (!this.signer) throw new Error("Wallet not connected"); return this.signer.signMessage(data); } + + public async changeNetwork(chain: RpcChainType = RpcChainType.ETH): Promise { + if (chain === RpcChainType.ETH) { + await this.provider.send("wallet_switchEthereumChain", [{ chainId: "0x1" }]); + } else await this.provider.send("wallet_addEthereumChain", [ChainData[chain]]); + } } diff --git a/tests/providers/ethereumProvider.ts b/tests/providers/ethereumProvider.ts index ab0b1fc9..bad12aca 100644 --- a/tests/providers/ethereumProvider.ts +++ b/tests/providers/ethereumProvider.ts @@ -92,6 +92,14 @@ export class EthereumProvider implements IMockProvider { return Promise.resolve(decrypted); } + case "wallet_addEthereumChain": { + return Promise.resolve(); + } + + case "wallet_switchEthereumChain": { + return Promise.resolve(); + } + default: this.log(`resquesting missing method ${method}`); // eslint-disable-next-line prefer-promise-reject-errors