From d2f88f00ddd49234326b9b78b1bb567d04f5d474 Mon Sep 17 00:00:00 2001 From: John Peterson Date: Fri, 24 May 2024 01:10:41 -0400 Subject: [PATCH] [hotfix] Derive Address - Address Map Check Error --- src/coinbase/tests/user_test.ts | 30 +++++++----------------------- src/coinbase/tests/wallet_test.ts | 17 +++-------------- src/coinbase/user.ts | 1 - src/coinbase/wallet.ts | 2 +- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/src/coinbase/tests/user_test.ts b/src/coinbase/tests/user_test.ts index 4d737231..514340e7 100644 --- a/src/coinbase/tests/user_test.ts +++ b/src/coinbase/tests/user_test.ts @@ -52,6 +52,7 @@ describe("User Class", () => { beforeAll(async () => { walletId = crypto.randomUUID(); mockAddressModel = newAddressModel(walletId); + mockAddressModel.address_id = "0x406210138180fD4d69368bcDD961062c6D8E8Cf7"; mockAddressList = { data: [mockAddressModel], has_more: false, @@ -68,7 +69,10 @@ describe("User Class", () => { Coinbase.apiClients.address = addressesApiMock; Coinbase.apiClients.address!.listAddresses = mockReturnValue(mockAddressList); user = new User(mockUserModel); - walletData = { walletId: walletId, seed: bip39.generateMnemonic() }; + walletData = { + walletId: walletId, + seed: "month volume van spin despair squirrel drum observe cook sport spin confirm", + }; importedWallet = await user.importWallet(walletData); expect(importedWallet).toBeInstanceOf(Wallet); expect(Coinbase.apiClients.wallet!.getWallet).toHaveBeenCalledWith(walletId); @@ -100,7 +104,7 @@ describe("User Class", () => { walletId = crypto.randomUUID(); seed = "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe"; mockAddressModel = { - address_id: "0xdeadbeef", + address_id: "0xB1666C6cDDB29468f721f3A4881a6e95CC963849", wallet_id: walletId, public_key: "0x1234567890", network_id: Coinbase.networkList.BaseSepolia, @@ -174,6 +178,7 @@ describe("User Class", () => { beforeAll(() => { walletId = crypto.randomUUID(); addressModel = newAddressModel(walletId); + addressModel.address_id = "0xB1666C6cDDB29468f721f3A4881a6e95CC963849"; walletModelWithDefaultAddress = { id: walletId, network_id: Coinbase.networkList.BaseSepolia, @@ -345,26 +350,5 @@ describe("User Class", () => { expect(Coinbase.apiClients.wallet!.listWallets).toHaveBeenCalledTimes(1); expect(Coinbase.apiClients.address!.listAddresses).toHaveBeenCalledTimes(0); }); - - it("should return the list of Wallets", async () => { - Coinbase.apiClients.wallet!.listWallets = mockReturnValue({ - data: [walletModelWithDefaultAddress], - has_more: false, - next_page: "", - total_count: 2, - }); - Coinbase.apiClients.address!.listAddresses = mockReturnValue(addressListModel); - const wallets = await user.getWallets(); - expect(wallets.length).toBe(1); - expect(wallets[0].getId()).toBe(walletId); - expect(wallets[0].getAddresses().length).toBe(2); - expect(Coinbase.apiClients.wallet!.listWallets).toHaveBeenCalledTimes(1); - expect(Coinbase.apiClients.address!.listAddresses).toHaveBeenCalledTimes(1); - expect(Coinbase.apiClients.address!.listAddresses).toHaveBeenCalledWith( - walletId, - Wallet.MAX_ADDRESSES, - ); - expect(Coinbase.apiClients.wallet!.listWallets).toHaveBeenCalledWith(10, ""); - }); }); }); diff --git a/src/coinbase/tests/wallet_test.ts b/src/coinbase/tests/wallet_test.ts index e89190e2..5d460aea 100644 --- a/src/coinbase/tests/wallet_test.ts +++ b/src/coinbase/tests/wallet_test.ts @@ -233,20 +233,15 @@ describe("Wallet Class", () => { walletId = crypto.randomUUID(); const existingSeed = "hidden assault maple cheap gentle paper earth surprise trophy guide room tired"; - const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(existingSeed)); - const wallet1 = baseWallet.deriveChild(0); - const address1 = getAddressFromHDKey(wallet1); - const wallet2 = baseWallet.deriveChild(1); - const address2 = getAddressFromHDKey(wallet2); const addressList = [ { - address_id: address1, + address_id: "0x23626702fdC45fc75906E535E38Ee1c7EC0C3213", network_id: Coinbase.networkList.BaseSepolia, public_key: "0x032c11a826d153bb8cf17426d03c3ffb74ea445b17362f98e1536f22bcce720772", wallet_id: walletId, }, { - address_id: address2, + address_id: "0x770603171A98d1CD07018F7309A1413753cA0018", network_id: Coinbase.networkList.BaseSepolia, public_key: "0x03c3379b488a32a432a4dfe91cc3a28be210eddc98b2005bb59a4cf4ed0646eb56", wallet_id: walletId, @@ -274,12 +269,6 @@ describe("Wallet Class", () => { expect(wallet.getAddresses().length).toBe(2); }); - it("should derive the correct addresses", async () => { - const addresses = wallet.getAddresses(); - expect(wallet.getAddress(address1)).toBe(addresses[0]); - expect(wallet.getAddress(address2)).toBe(addresses[1]); - }); - it("should create new address and update the existing address list", async () => { const newAddress = await wallet.createAddress(); expect(newAddress).toBeInstanceOf(Address); @@ -445,7 +434,7 @@ describe("Wallet Class", () => { Coinbase.apiClients.address!.createAddress = mockReturnValue(mockAddressModel); wallet = await Wallet.create(); }); - it("should return true when the wallet initialized ", () => { + it("should return true when the wallet initialized", () => { expect(wallet.canSign()).toBe(true); }); }); diff --git a/src/coinbase/user.ts b/src/coinbase/user.ts index 5501626d..d3b9a994 100644 --- a/src/coinbase/user.ts +++ b/src/coinbase/user.ts @@ -175,7 +175,6 @@ export class User { public async importWallet(data: WalletData): Promise { const walletModel = await Coinbase.apiClients.wallet!.getWallet(data.walletId); const addressList = await Coinbase.apiClients.address!.listAddresses(data.walletId); - return Wallet.init(walletModel.data, data.seed, addressList.data.data); } diff --git a/src/coinbase/wallet.ts b/src/coinbase/wallet.ts index 443cec0d..68a4d8e7 100644 --- a/src/coinbase/wallet.ts +++ b/src/coinbase/wallet.ts @@ -208,7 +208,7 @@ export class Wallet { ): Promise { const hdKey = this.deriveKey(); const key = new ethers.Wallet(convertStringToHex(hdKey.privateKey!)); - if (addressMap[key.address]) { + if (!addressMap[key.address]) { throw new InternalError("Invalid address"); }