Skip to content

Commit

Permalink
[hotfix] Derive Address - Address Map Check Error
Browse files Browse the repository at this point in the history
  • Loading branch information
John-peterson-coinbase committed May 24, 2024
1 parent b603eee commit d2f88f0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
30 changes: 7 additions & 23 deletions src/coinbase/tests/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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, "");
});
});
});
17 changes: 3 additions & 14 deletions src/coinbase/tests/wallet_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});
Expand Down
1 change: 0 additions & 1 deletion src/coinbase/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export class User {
public async importWallet(data: WalletData): Promise<Wallet> {
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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/coinbase/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class Wallet {
): Promise<void> {
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");
}

Expand Down

0 comments on commit d2f88f0

Please sign in to comment.