Skip to content

Commit

Permalink
Adding util function to generate dynamic address and wallet.
Browse files Browse the repository at this point in the history
Updating test cases
  • Loading branch information
erdimaden committed May 24, 2024
1 parent 2d00a53 commit 4c367b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/coinbase/tests/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { User } from "./../user";
import { Wallet } from "./../wallet";
import {
addressesApiMock,
generateWalletFromSeed,
getAddressFromHDKey,
mockReturnRejectedValue,
mockReturnValue,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -181,7 +177,6 @@ describe("User Class", () => {
let seedDataWithoutSeed: Record<string, any>;
let seedDataWithoutIv: Record<string, any>;
let seedDataWithoutAuthTag: Record<string, any>;
let seed: string = "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe";

beforeAll(() => {
walletId = crypto.randomUUID();
Expand Down Expand Up @@ -212,7 +207,7 @@ describe("User Class", () => {

initialSeedData = {
[walletId]: {
seed,
seed: "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe",
encrypted: false,
iv: "",
authTag: "",
Expand All @@ -229,15 +224,15 @@ describe("User Class", () => {
};
seedDataWithoutIv = {
[walletId]: {
seed,
seed: "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe",
encrypted: true,
iv: "",
auth_tag: "0x111",
},
};
seedDataWithoutAuthTag = {
[walletId]: {
seed,
seed: "86fc9fba421dcc6ad42747f14132c3cd975bd9fb1454df84ce5ea554f2542fbe",
encrypted: true,
iv: "0x111",
auth_tag: "",
Expand All @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/coinbase/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<string, string> = {};
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";
Expand Down

0 comments on commit 4c367b9

Please sign in to comment.