Skip to content

Commit

Permalink
Feat: We want to make sure the user is using the Avax network
Browse files Browse the repository at this point in the history
Solution: Call the RPC provider to switch to the specified network
  • Loading branch information
Rgascoin committed Oct 24, 2022
1 parent b79ca11 commit e42a883
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/accounts/avalanche.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -141,8 +141,9 @@ export async function ImportAccountFromPrivateKey(privateKey: string): Promise<A
export async function GetAccountFromProvider(provider: providers.ExternalProvider): Promise<AvalancheAccount> {
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);
}
Expand Down
36 changes: 36 additions & 0 deletions src/providers/JsonRPCWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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<void> {
if (chain === RpcChainType.ETH) {
await this.provider.send("wallet_switchEthereumChain", [{ chainId: "0x1" }]);
} else await this.provider.send("wallet_addEthereumChain", [ChainData[chain]]);
}
}
8 changes: 8 additions & 0 deletions tests/providers/ethereumProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e42a883

Please sign in to comment.