From bb447b29e7fee8507c5473cd422d564c074f0d98 Mon Sep 17 00:00:00 2001 From: Rafal Czajkowski Date: Thu, 31 Oct 2024 16:34:59 +0100 Subject: [PATCH] Update the `connect` function We should return the address at `0` index so we need to call the `getAdress` function from the bitcoin module. The address from the account object is always a fresh address and may be different from address at `0` index. --- .../orangekit/ledger-live/bitcoin-provider.ts | 2 +- .../tests/bitcoin-provider.test.ts | 59 ++++++++++++++++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/dapp/src/utils/orangekit/ledger-live/bitcoin-provider.ts b/dapp/src/utils/orangekit/ledger-live/bitcoin-provider.ts index a7f40c2df..5fed1e76f 100644 --- a/dapp/src/utils/orangekit/ledger-live/bitcoin-provider.ts +++ b/dapp/src/utils/orangekit/ledger-live/bitcoin-provider.ts @@ -153,7 +153,7 @@ export default class AcreLedgerLiveBitcoinProvider } this.#hasConnectFunctionBeenCalled = true - return this.#account.address + return this.#getAddress(this.#account.id) } /** diff --git a/dapp/src/utils/orangekit/ledger-live/tests/bitcoin-provider.test.ts b/dapp/src/utils/orangekit/ledger-live/tests/bitcoin-provider.test.ts index 7f8b861b3..45d4f0e35 100644 --- a/dapp/src/utils/orangekit/ledger-live/tests/bitcoin-provider.test.ts +++ b/dapp/src/utils/orangekit/ledger-live/tests/bitcoin-provider.test.ts @@ -377,6 +377,9 @@ describe("AcreLedgerLiveBitcoinProvider", () => { mockedWalletApiClient.account.list.mockReturnValueOnce([ mockedAccount, ]) + mockedWalletApiClient.bitcoin.getAddress.mockResolvedValueOnce( + mockedAccount.address, + ) provider = new AcreLedgerLiveBitcoinProvider( BitcoinNetwork.Testnet, @@ -393,6 +396,12 @@ describe("AcreLedgerLiveBitcoinProvider", () => { }) }) + it("should get the bitcoin address from bitcoin module", () => { + expect( + mockedWalletApiClient.bitcoin.getAddress, + ).toHaveBeenCalledWith(mockedAccount.id, "0/0") + }) + it("should return the first account", () => { expect(result).toBe(mockedAccount.address) }) @@ -402,6 +411,9 @@ describe("AcreLedgerLiveBitcoinProvider", () => { beforeAll(async () => { mockedWalletApiClient.account.list.mockReturnValueOnce(accounts) mockedWalletApiClient.account.request.mockReturnValueOnce(account2) + mockedWalletApiClient.bitcoin.getAddress.mockResolvedValueOnce( + account2.address, + ) provider = new AcreLedgerLiveBitcoinProvider( BitcoinNetwork.Testnet, @@ -418,6 +430,12 @@ describe("AcreLedgerLiveBitcoinProvider", () => { }) }) + it("should get the bitcoin address from bitcoin module", () => { + expect( + mockedWalletApiClient.bitcoin.getAddress, + ).toHaveBeenCalledWith(mockedAccount.id, "0/0") + }) + it("should return account selected by the user", () => { expect(result).toBe(account2.address) }) @@ -435,6 +453,7 @@ describe("AcreLedgerLiveBitcoinProvider", () => { .mockReturnValueOnce(mockedAccount.address) .mockReturnValueOnce(account2.address) .mockReturnValueOnce(account3.address) + .mockResolvedValueOnce(tryConnectToAddress) provider = new AcreLedgerLiveBitcoinProvider( BitcoinNetwork.Testnet, @@ -453,9 +472,6 @@ describe("AcreLedgerLiveBitcoinProvider", () => { }) it("should get zero address for all accounts", () => { - expect( - mockedWalletApiClient.bitcoin.getAddress, - ).toHaveBeenCalledTimes(accounts.length) expect( mockedWalletApiClient.bitcoin.getAddress, ).toHaveBeenNthCalledWith(1, mockedAccount.id, "0/0") @@ -467,6 +483,13 @@ describe("AcreLedgerLiveBitcoinProvider", () => { ).toHaveBeenNthCalledWith(3, account3.id, "0/0") }) + it("should get the bitcoin address from bitcoin module", () => { + expect(mockedWalletApiClient.bitcoin.getAddress).toHaveBeenCalledWith( + account3.id, + "0/0", + ) + }) + it("should return an account with the same address as `tryConnectToAddress` param", () => { expect(result).toBe(tryConnectToAddress) }) @@ -474,9 +497,17 @@ describe("AcreLedgerLiveBitcoinProvider", () => { }) describe("when the provider is already connected", () => { + const selectedAccount = account2 + let secondConnectionResult: string + beforeAll(async () => { mockedWalletApiClient.account.list.mockReturnValueOnce([mockedAccount]) - mockedWalletApiClient.account.request.mockReturnValueOnce(account2) + mockedWalletApiClient.account.request.mockReturnValueOnce( + selectedAccount, + ) + mockedWalletApiClient.bitcoin.getAddress + .mockReturnValueOnce(mockedAccount.address) + .mockResolvedValueOnce(selectedAccount.address) provider = new AcreLedgerLiveBitcoinProvider( BitcoinNetwork.Testnet, @@ -485,15 +516,27 @@ describe("AcreLedgerLiveBitcoinProvider", () => { ) await provider.connect() - }) - it("should ask the user to select an account", async () => { - const secondConnectionResult = await provider.connect() + secondConnectionResult = await provider.connect() + }) + it("should ask the user to select an account", () => { expect(mockedWalletApiClient.account.request).toHaveBeenCalledWith({ currencyIds: ["bitcoin_testnet"], }) - expect(secondConnectionResult).toBe(account2.address) + }) + + it("should get the bitcoin address from bitcoin module", () => { + expect( + mockedWalletApiClient.bitcoin.getAddress, + // The first connection was when the user connected for the first + // time. Here we want to check the second call, after the user + // selected the account for the second time. + ).toHaveBeenNthCalledWith(2, selectedAccount.id, "0/0") + }) + + it("should return an account selected by the user", () => { + expect(secondConnectionResult).toBe(selectedAccount.address) }) }) })