From 36f80bf2febc9110404c7f8a89f6d73f7bc12966 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Sun, 5 Nov 2023 22:02:31 +0100 Subject: [PATCH 1/4] Update signer check The use of `instanceof` operator leads to unexpected behaviors. The issue has been solved with the use of built-in native check method. --- typescript/package.json | 2 +- typescript/src/lib/ethereum/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/typescript/package.json b/typescript/package.json index 15ca45520..f9941733d 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -1,5 +1,5 @@ { - "name": "@keep-network/tbtc-v2.ts", + "name": "tbtc-sdk-v2", "version": "2.2.0-dev", "license": "MIT", "main": "dist/src/index.js", 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 From 7c75912241ffaa23491d85d36a3953a9a84a24a2 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Mon, 6 Nov 2023 11:50:06 +0100 Subject: [PATCH 2/4] Add unit tests for signer-related functions Added unit tests for `ethereumNetworkFromSigner` and `ethereumAddressFromSigner` functions. --- typescript/test/lib/ethereum.test.ts | 38 +++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index 801e77fe8..5d9523217 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,38 @@ 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") + }) + }) + }) }) From 45e54a9beae3bcb12b88c5c57a5f1340dc3da775 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Mon, 6 Nov 2023 11:54:32 +0100 Subject: [PATCH 3/4] Revert module name change For development purpose I have the library aliased locally. This commit reverts changes to default. --- typescript/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript/package.json b/typescript/package.json index f9941733d..15ca45520 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -1,5 +1,5 @@ { - "name": "tbtc-sdk-v2", + "name": "@keep-network/tbtc-v2.ts", "version": "2.2.0-dev", "license": "MIT", "main": "dist/src/index.js", From 48bcba89b31305a5eb6b8a2f56a0051a82192b01 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Mon, 6 Nov 2023 12:07:15 +0100 Subject: [PATCH 4/4] Resolve code formatter issues --- typescript/test/lib/ethereum.test.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/typescript/test/lib/ethereum.test.ts b/typescript/test/lib/ethereum.test.ts index 5d9523217..0cda5f971 100644 --- a/typescript/test/lib/ethereum.test.ts +++ b/typescript/test/lib/ethereum.test.ts @@ -4,7 +4,7 @@ import { EthereumAddress, EthereumBridge, EthereumTBTCToken, - ethereumAddressFromSigner, + ethereumAddressFromSigner, ethereumNetworkFromSigner, Hex, } from "../../src" @@ -600,11 +600,11 @@ describe("Ethereum", () => { 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", () => { + }) + + context("when the signer is a provider", () => { const mockProvider = getDefaultProvider() it("should return undefined", async () => { expect(await ethereumAddressFromSigner(mockProvider)).to.be.undefined @@ -619,11 +619,13 @@ describe("Ethereum", () => { 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") + expect(await ethereumNetworkFromSigner(mockProvider)).to.be.eql( + "mainnet" + ) }) }) })