Skip to content

Commit

Permalink
Update the connect function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
r-czajkowski committed Oct 31, 2024
1 parent 6cde85b commit bb447b2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dapp/src/utils/orangekit/ledger-live/bitcoin-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class AcreLedgerLiveBitcoinProvider
}

this.#hasConnectFunctionBeenCalled = true
return this.#account.address
return this.#getAddress(this.#account.id)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ describe("AcreLedgerLiveBitcoinProvider", () => {
mockedWalletApiClient.account.list.mockReturnValueOnce([
mockedAccount,
])
mockedWalletApiClient.bitcoin.getAddress.mockResolvedValueOnce(
mockedAccount.address,
)

provider = new AcreLedgerLiveBitcoinProvider(
BitcoinNetwork.Testnet,
Expand All @@ -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)
})
Expand All @@ -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,
Expand All @@ -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)
})
Expand All @@ -435,6 +453,7 @@ describe("AcreLedgerLiveBitcoinProvider", () => {
.mockReturnValueOnce(mockedAccount.address)
.mockReturnValueOnce(account2.address)
.mockReturnValueOnce(account3.address)
.mockResolvedValueOnce(tryConnectToAddress)

provider = new AcreLedgerLiveBitcoinProvider(
BitcoinNetwork.Testnet,
Expand All @@ -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")
Expand All @@ -467,16 +483,31 @@ 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)
})
})
})

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,
Expand All @@ -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)
})
})
})
Expand Down

0 comments on commit bb447b2

Please sign in to comment.