Skip to content

Commit

Permalink
Signer recognition issue (#735)
Browse files Browse the repository at this point in the history
The use of `instanceof` operator leads to unexpected behaviors. The
issue has been solved with the use of built-in native check method.

What's more, implemented unit tests for functions that propagated the
issue described above.
  • Loading branch information
lukasz-zimnoch authored Nov 6, 2023
2 parents 1d21c23 + 703ad0c commit 25d2de2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions typescript/src/lib/ethereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function ethereumNetworkFromSigner(
signer: EthereumSigner
): Promise<EthereumNetwork> {
let chainId: number
if (signer instanceof Signer) {
if (Signer.isSigner(signer)) {
chainId = await signer.getChainId()
} else {
const network = await signer.getNetwork()
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function ethereumNetworkFromSigner(
export async function ethereumAddressFromSigner(
signer: EthereumSigner
): Promise<EthereumAddress | undefined> {
if (signer instanceof Signer) {
if (Signer.isSigner(signer)) {
return EthereumAddress.from(await signer.getAddress())
} else {
return undefined
Expand Down
40 changes: 39 additions & 1 deletion typescript/test/lib/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import {
EthereumAddress,
EthereumBridge,
EthereumTBTCToken,
ethereumAddressFromSigner,
ethereumNetworkFromSigner,
Hex,
} from "../../src"
import {
deployMockContract,
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"
Expand Down Expand Up @@ -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"
)
})
})
})
})

0 comments on commit 25d2de2

Please sign in to comment.