diff --git a/typescript/src/lib/ethereum/index.ts b/typescript/src/lib/ethereum/index.ts index 84f9b0f75..e39fcc5c9 100644 --- a/typescript/src/lib/ethereum/index.ts +++ b/typescript/src/lib/ethereum/index.ts @@ -33,7 +33,7 @@ export async function ethereumNetworkFromSigner( signer: EthereumSigner ): Promise { let chainId: number - if (signer instanceof Signer) { + if (Signer.isSigner(signer)) { chainId = await signer.getChainId() } else { const network = await signer.getNetwork() @@ -61,7 +61,7 @@ export async function ethereumNetworkFromSigner( export async function ethereumAddressFromSigner( signer: EthereumSigner ): Promise { - if (signer instanceof Signer) { + if (Signer.isSigner(signer)) { return EthereumAddress.from(await signer.getAddress()) } else { return undefined diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index 801e77fe8..0cda5f971 100644 --- a/typescript/test/lib/ethereum.test.ts +++ b/typescript/test/lib/ethereum.test.ts @@ -4,6 +4,8 @@ import { EthereumAddress, EthereumBridge, EthereumTBTCToken, + ethereumAddressFromSigner, + ethereumNetworkFromSigner, Hex, } from "../../src" import { @@ -11,7 +13,7 @@ import { MockContract, } from "@ethereum-waffle/mock-contract" import chai, { assert, expect } from "chai" -import { BigNumber, Wallet, constants, utils } from "ethers" +import { BigNumber, Wallet, constants, getDefaultProvider, utils } from "ethers" import { abi as BridgeABI } from "@keep-network/tbtc-v2/artifacts/Bridge.json" import { abi as TBTCTokenABI } from "@keep-network/tbtc-v2/artifacts/TBTC.json" import { abi as WalletRegistryABI } from "@keep-network/ecdsa/artifacts/WalletRegistry.json" @@ -591,4 +593,40 @@ describe("Ethereum", () => { }) }) }) + + describe("ethereumAddressFromSigner", () => { + context("when the signer is a wallet", () => { + const [mockSigner] = new MockProvider().getWallets() + it("should return the signer's address", async () => { + expect(await ethereumAddressFromSigner(mockSigner)).to.be.eql( + EthereumAddress.from(mockSigner.address) + ) + }) + }) + + context("when the signer is a provider", () => { + const mockProvider = getDefaultProvider() + it("should return undefined", async () => { + expect(await ethereumAddressFromSigner(mockProvider)).to.be.undefined + }) + }) + }) + + describe("ethereumNetworkFromSigner", () => { + context("when the signer is a wallet", () => { + const [mockSigner] = new MockProvider().getWallets() + it("should return the signer's network", async () => { + expect(await ethereumNetworkFromSigner(mockSigner)).to.be.eql("local") + }) + }) + + context("when the signer is a provider", () => { + const mockProvider = getDefaultProvider() + it("should return the signer's network", async () => { + expect(await ethereumNetworkFromSigner(mockProvider)).to.be.eql( + "mainnet" + ) + }) + }) + }) })