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 5de537c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 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
78 changes: 62 additions & 16 deletions dapp/src/utils/orangekit/ledger-live/tests/bitcoin-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("AcreLedgerLiveBitcoinProvider", () => {
const mockedAccount = {
id: "test",
address: bitcoinAddress,
zeroAddress,
balance: 100,
spendableBalance: 80,
}
Expand Down Expand Up @@ -358,13 +359,15 @@ describe("AcreLedgerLiveBitcoinProvider", () => {
const account2 = {
...mockedAccount,
id: "2",
address: "bc1qxg2cc7p5yur7tr3rr2cl5jw47lluuvy7cr2usu",
address: "tb1q3x5lk9l83aek9c0vu38tx69u2lyxsdy6tww2rp",
zeroAddress: "tb1qj502nrx8lv4azu4afuxhffma248gq8c00zl8uv",
}

const account3 = {
...mockedAccount,
id: "3",
address: "tb1qhp6ug4hp3cc84spx3l8sddh8w5ffjujvjycqv5",
address: "mrXtbjt4XXcX2aruY1Lx58YF2F8LXYzi1U",
zeroAddress: "mhKLhBdi7JmtYNxRVTn3u5fSsXtiwDbj4K",
}
const accounts = [mockedAccount, account2, account3]

Expand All @@ -377,6 +380,9 @@ describe("AcreLedgerLiveBitcoinProvider", () => {
mockedWalletApiClient.account.list.mockReturnValueOnce([
mockedAccount,
])
mockedWalletApiClient.bitcoin.getAddress.mockResolvedValueOnce(
mockedAccount.zeroAddress,
)

provider = new AcreLedgerLiveBitcoinProvider(
BitcoinNetwork.Testnet,
Expand All @@ -393,15 +399,24 @@ 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)
expect(result).toBe(mockedAccount.zeroAddress)
})
})

describe("when there are more than one account to select", () => {
beforeAll(async () => {
mockedWalletApiClient.account.list.mockReturnValueOnce(accounts)
mockedWalletApiClient.account.request.mockReturnValueOnce(account2)
mockedWalletApiClient.bitcoin.getAddress.mockResolvedValueOnce(
account2.zeroAddress,
)

provider = new AcreLedgerLiveBitcoinProvider(
BitcoinNetwork.Testnet,
Expand All @@ -418,23 +433,30 @@ 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)
expect(result).toBe(account2.zeroAddress)
})
})
})

describe("when `tryConnectToAddress` is set", () => {
const tryConnectToAddress = account3.address
const tryConnectToAddress = account3.zeroAddress

beforeAll(async () => {
mockedWalletApiClient.bitcoin.getAddress.mockClear()

mockedWalletApiClient.account.list.mockReturnValueOnce(accounts)
mockedWalletApiClient.bitcoin.getAddress
.mockReturnValueOnce(mockedAccount.address)
.mockReturnValueOnce(account2.address)
.mockReturnValueOnce(account3.address)
.mockReturnValueOnce(mockedAccount.zeroAddress)
.mockReturnValueOnce(account2.zeroAddress)
.mockReturnValueOnce(account3.zeroAddress)
.mockResolvedValueOnce(tryConnectToAddress)

provider = new AcreLedgerLiveBitcoinProvider(
BitcoinNetwork.Testnet,
Expand All @@ -453,9 +475,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 +486,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.zeroAddress)
.mockResolvedValueOnce(selectedAccount.zeroAddress)

provider = new AcreLedgerLiveBitcoinProvider(
BitcoinNetwork.Testnet,
Expand All @@ -485,15 +519,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.zeroAddress)
})
})
})
Expand Down

0 comments on commit 5de537c

Please sign in to comment.