From bf16a06977778ab6a0a5c144fb9218c57f699f7f Mon Sep 17 00:00:00 2001 From: Ruben Gutierrez <45153592+RubenGutierrezC@users.noreply.github.com> Date: Thu, 20 Oct 2022 09:59:10 -0400 Subject: [PATCH] add metamask support (#65) --- src/constants.ts | 18 +++++++++++++++++- src/interfaces.ts | 4 ++++ src/wallets/key.ts | 10 +++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 49de03f..7e4b83b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,5 @@ import { ChainID, ChainType, Unit } from '@harmony-js/utils' -import { ContractAddresses, HarmonyRpcConfig, HarmonyShards, ITransactionOptions } from './interfaces' +import { ChainId, ContractAddresses, HarmonyRpcConfig, HarmonyShards, ITransactionOptions } from './interfaces' export const AddressZero = '0x0000000000000000000000000000000000000000' export const DEFAULT_GAS_PRICE = '1000000000' @@ -8,6 +8,10 @@ export const HARMONY_RPC_SHARD_0_URL = 'https://api.harmony.one' export const HARMONY_RPC_SHARD_1_URL = 'https://s1.api.harmony.one' export const HARMONY_RPC_SHARD_2_URL = 'https://s2.api.harmony.one' export const HARMONY_RPC_SHARD_3_URL = 'https://s3.api.harmony.one' +export const HARMONY_RPC_SHARD_0_TESTNET_URL = 'https://api.s0.b.hmny.io' +export const HARMONY_RPC_SHARD_1_TESTNET_URL = 'https://api.s1.b.hmny.io' +export const HARMONY_RPC_SHARD_2_TESTNET_URL = 'https://api.s2.b.hmny.io' +export const HARMONY_RPC_SHARD_3_TESTNET_URL = 'https://api.s3.b.hmny.io' export const HARMONY_RPC_SHARD_0_DEVNET_URL = 'https://api.s0.ps.hmny.io' export const HARMONY_RPC_WS = 'wss://ws.s0.t.hmny.io' export const HARMONY_RPC_DEVNET_WS = 'wss://ws.s0.ps.hmny.io' @@ -45,6 +49,18 @@ export const HARMONY_SHARDS = { [HarmonyShards.SHARD_0_DEVNET]: HARMONY_RPC_SHARD_0_DEVNET, } +export const CHAINS_ID: ChainId = { + 1666600000: HARMONY_RPC_SHARD_0_URL, + 1666600001: HARMONY_RPC_SHARD_1_URL, + 1666600002: HARMONY_RPC_SHARD_2_URL, + 1666600003: HARMONY_RPC_SHARD_3_URL, + 1666700000: HARMONY_RPC_SHARD_0_TESTNET_URL, + 1666700001: HARMONY_RPC_SHARD_1_TESTNET_URL, + 1666700002: HARMONY_RPC_SHARD_2_TESTNET_URL, + 1666700003: HARMONY_RPC_SHARD_3_TESTNET_URL, + 1666900000: HARMONY_RPC_SHARD_0_DEVNET_URL, +} + export enum NetworkInfo { MAINNET, DEVNET, diff --git a/src/interfaces.ts b/src/interfaces.ts index 19579c1..1c44e1b 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -80,3 +80,7 @@ export interface BridgeResponse { addr: string receiptId: string } + +export interface ChainId { + [key: number]: string +} diff --git a/src/wallets/key.ts b/src/wallets/key.ts index ac82112..6df2c40 100644 --- a/src/wallets/key.ts +++ b/src/wallets/key.ts @@ -1,7 +1,7 @@ import { Wallet } from '@harmony-js/account' import { Messenger, HttpProvider, WSProvider } from '@harmony-js/network' import { ChainID, ChainType, isWs } from '@harmony-js/utils' -import { HARMONY_SHARDS } from '../constants' +import { HARMONY_SHARDS, CHAINS_ID } from '../constants' import { HarmonyRpcConfig, HarmonyShards, RpcProviderType } from '../interfaces' /** @@ -21,6 +21,14 @@ export class Key extends Wallet { provider = url } else if (typeof url === 'string') { provider = isWs(url) ? new WSProvider(url) : new HttpProvider(url) + } else if (url['isMetaMask']) { + const _chain = Number(url['networkVersion']) + const rpc_url = CHAINS_ID[_chain] + + if (!rpc_url) throw new Error('Invalid metamask harmony chain.') + + provider = new HttpProvider(rpc_url) + chain = _chain } else { throw new Error('Invalid url param.') }