diff --git a/src/coinbase/tests/user_test.ts b/src/coinbase/tests/user_test.ts index 49b0c79f..e7802d3c 100644 --- a/src/coinbase/tests/user_test.ts +++ b/src/coinbase/tests/user_test.ts @@ -14,6 +14,7 @@ import { User } from "./../user"; import { Wallet } from "./../wallet"; import { addressesApiMock, + generateWalletFromSeed, getAddressFromHDKey, mockReturnRejectedValue, mockReturnValue, @@ -56,9 +57,7 @@ describe("User Class", () => { beforeAll(async () => { walletId = crypto.randomUUID(); walletData = { walletId: walletId, seed: bip39.generateMnemonic() }; - const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(walletData.seed)); - const wallet1 = baseWallet.derive("m/44'/60'/0'/0/0"); - const address1 = getAddressFromHDKey(wallet1); + const { address1 } = generateWalletFromSeed(walletData.seed); mockAddressModel = newAddressModel(walletId, address1); mockAddressList = { data: [mockAddressModel], @@ -106,14 +105,11 @@ describe("User Class", () => { beforeAll(async () => { walletId = crypto.randomUUID(); seed = "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe"; - const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(seed)); - const wallet1 = baseWallet.derive("m/44'/60'/0'/0/0"); - const address1 = getAddressFromHDKey(wallet1); - + const { address1, wallet1PrivateKey } = generateWalletFromSeed(seed); mockAddressModel = { address_id: address1, wallet_id: walletId, - public_key: convertStringToHex(wallet1.privateKey!), + public_key: wallet1PrivateKey, network_id: Coinbase.networkList.BaseSepolia, }; mockWalletModel = { @@ -181,7 +177,6 @@ describe("User Class", () => { let seedDataWithoutSeed: Record; let seedDataWithoutIv: Record; let seedDataWithoutAuthTag: Record; - let seed: string = "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe"; beforeAll(() => { walletId = crypto.randomUUID(); @@ -212,7 +207,7 @@ describe("User Class", () => { initialSeedData = { [walletId]: { - seed, + seed: "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe", encrypted: false, iv: "", authTag: "", @@ -229,7 +224,7 @@ describe("User Class", () => { }; seedDataWithoutIv = { [walletId]: { - seed, + seed: "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe", encrypted: true, iv: "", auth_tag: "0x111", @@ -237,7 +232,7 @@ describe("User Class", () => { }; seedDataWithoutAuthTag = { [walletId]: { - seed, + seed: "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe", encrypted: true, iv: "0x111", auth_tag: "", @@ -257,13 +252,7 @@ describe("User Class", () => { }); it("loads the Wallet from backup", async () => { - const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(seed)); - const wallet1 = baseWallet.derive("m/44'/60'/0'/0/0"); - const address1 = getAddressFromHDKey(wallet1); - - const wallet2 = baseWallet.derive("m/44'/60'/0'/0/1"); - const address2 = getAddressFromHDKey(wallet2); - + const { address1, address2 } = generateWalletFromSeed(seed); const addressModel1: AddressModel = newAddressModel(walletId, address1); const addressModel2: AddressModel = newAddressModel(walletId, address2); walletModelWithDefaultAddress = { @@ -326,9 +315,7 @@ describe("User Class", () => { jest.clearAllMocks(); walletId = crypto.randomUUID(); const seed = bip39.generateMnemonic(); - const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(seed)); - const wallet1 = baseWallet.derive("m/44'/60'/0'/0/0"); - const address1 = getAddressFromHDKey(wallet1); + const { address1 } = generateWalletFromSeed(seed); mockAddressModel = newAddressModel(walletId, address1); const addressModel1: AddressModel = newAddressModel(walletId); @@ -388,9 +375,7 @@ describe("User Class", () => { it("should return the list of Wallets", async () => { const seed = bip39.generateMnemonic(); - const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(seed)); - const wallet1 = baseWallet.derive("m/44'/60'/0'/0/0"); - const address1 = getAddressFromHDKey(wallet1); + const { address1 } = generateWalletFromSeed(seed); mockAddressModel = newAddressModel(walletId, address1); Coinbase.apiClients.wallet!.listWallets = mockReturnValue({ @@ -401,7 +386,6 @@ describe("User Class", () => { }); Coinbase.apiClients.address!.listAddresses = mockReturnValue(addressListModel); const wallets = await user.getWallets(); - // instance of UnhydratedWallet expect(wallets[0]).toBeInstanceOf(UnhydratedWallet); expect(wallets.length).toBe(1); expect(wallets[0].getId()).toBe(walletId); diff --git a/src/coinbase/tests/utils.ts b/src/coinbase/tests/utils.ts index e397d8bf..64722039 100644 --- a/src/coinbase/tests/utils.ts +++ b/src/coinbase/tests/utils.ts @@ -3,6 +3,7 @@ import axios, { AxiosInstance } from "axios"; import { Decimal } from "decimal.js"; import { ethers } from "ethers"; import { randomUUID } from "crypto"; +import * as bip39 from "bip39"; import { Configuration, Wallet as WalletModel, @@ -28,6 +29,18 @@ export const getAddressFromHDKey = (hdKey: HDKey): string => { export const walletId = randomUUID(); export const transferId = randomUUID(); +export const generateWalletFromSeed = (seed: string, count = 2) => { + const baseWallet = HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(seed)); + const data: Record = {}; + for (let i = 0; i < count; i++) { + const wallet = baseWallet.derive(`m/44'/60'/0'/0/${i}`); + data[`wallet${i + 1}`] = getAddressFromHDKey(wallet); + data[`wallet${i + 1}PrivateKey`] = convertStringToHex(wallet.privateKey!); + data[`address${i + 1}`] = getAddressFromHDKey(wallet); + } + return data; +}; + export const generateRandomHash = (length = 8) => { const characters = "abcdef0123456789"; let hash = "0x";