Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to account with direct navigation parameters #902

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"account.list",
"bitcoin.getAddress",
"bitcoin.getPublicKey",
"bitcoin.getXPub",
"transaction.signAndBroadcast",
"custom.acre.messageSign",
"custom.acre.transactionSignAndBroadcast"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"account.list",
"bitcoin.getAddress",
"bitcoin.getPublicKey",
"bitcoin.getXPub",
"transaction.signAndBroadcast",
"custom.acre.messageSign",
"custom.acre.transactionSignAndBroadcast"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"account.list",
"bitcoin.getAddress",
"bitcoin.getPublicKey",
"bitcoin.getXPub",
"transaction.signAndBroadcast",
"custom.acre.messageSign",
"custom.acre.transactionSignAndBroadcast"
Expand Down
1 change: 1 addition & 0 deletions dapp/manifests/ledger-live/ledger-manifest-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"account.list",
"bitcoin.getAddress",
"bitcoin.getPublicKey",
"bitcoin.getXPub",
"transaction.signAndBroadcast",
"custom.acre.messageSign",
"custom.acre.transactionSignAndBroadcast"
Expand Down
28 changes: 28 additions & 0 deletions dapp/src/utils/orangekit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ async function verifySignInWithWalletMessage(
return result.data
}

/**
* Finds the extended public key (xpub) of the user's account from URL. Users
* can be redirected to the exact app in the Ledger Live application. One of the
* parameters passed via URL is `accountId` - the ID of the user's account in
* Ledger Live.
* @see https://developers.ledger.com/docs/ledger-live/exchange/earn/liveapp#url-parameters-for-direct-navigation
*
* @param {string} url Request url
* @returns The extended public key (xpub) of the user's account if the search
* parameter `accountId` exists in the URL. Otherwise `undefined`.
*/
function findXpubFromUrl(url: string): string | undefined {
const parsedUrl = new URL(url)

const accountId = parsedUrl.searchParams.get("accountId")

if (!accountId) return undefined

// The fourth value separated by `:` is extended public key. See the
// account ID template: `js:2:bitcoin_testnet:<xpub>:<address_type>`.
const xpubFromAccountId = accountId.split(":")[3]

if (!xpubFromAccountId) return undefined

return xpubFromAccountId
}

export default {
getWalletInfo,
isWalletInstalled,
Expand All @@ -119,4 +146,5 @@ export default {
isWalletConnectionRejectedError,
verifySignInWithWalletMessage,
getOrangeKitLedgerLiveConnector,
findXpubFromUrl,
}
27 changes: 22 additions & 5 deletions dapp/src/utils/orangekit/ledger-live/bitcoin-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ function numberToValidHexString(value: number): string {
return `0x${hex}`
}

export type AcreLedgerLiveBitcoinProviderOptions = {
tryConnectToAddress: string | undefined
}
export type AcreLedgerLiveBitcoinProviderOptions =
| {
tryConnectToAddress?: string
tryConnectToAccountByXpub?: never
}
| { tryConnectToAddress?: never; tryConnectToAccountByXpub?: string }

/**
* Ledger Live Wallet API Bitcoin Provider.
Expand Down Expand Up @@ -90,6 +93,7 @@ export default class AcreLedgerLiveBitcoinProvider
network: BitcoinNetwork,
options: AcreLedgerLiveBitcoinProviderOptions = {
tryConnectToAddress: undefined,
tryConnectToAccountByXpub: undefined,
},
) {
const windowMessageTransport = new WindowMessageTransport()
Expand All @@ -115,6 +119,7 @@ export default class AcreLedgerLiveBitcoinProvider
walletApiClient: WalletAPIClient,
options: AcreLedgerLiveBitcoinProviderOptions = {
tryConnectToAddress: undefined,
tryConnectToAccountByXpub: undefined,
},
) {
this.#network = network
Expand All @@ -140,12 +145,24 @@ export default class AcreLedgerLiveBitcoinProvider
currencyIds,
})

if (this.#options.tryConnectToAddress) {
if (
this.#options.tryConnectToAddress ||
this.#options.tryConnectToAccountByXpub
) {
for (let i = 0; i < accounts.length; i += 1) {
const acc = accounts[i]
if (
this.#options.tryConnectToAccountByXpub &&
// eslint-disable-next-line no-await-in-loop
(await this.#walletApiClient.bitcoin.getXPub(acc.id)) ===
this.#options.tryConnectToAccountByXpub
) {
this.#account = acc
break
}

// eslint-disable-next-line no-await-in-loop
const address = await this.#getAddress(acc.id)

if (address === this.#options.tryConnectToAddress) {
this.#account = acc
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ describe("AcreLedgerLiveBitcoinProvider", () => {
})
})

it("should get")
nkuba marked this conversation as resolved.
Show resolved Hide resolved

it("should get zero address for all accounts", () => {
expect(
mockedWalletApiClient.bitcoin.getAddress,
Expand Down
14 changes: 11 additions & 3 deletions dapp/src/wagmiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { env } from "./constants"
import { getLastUsedBtcAddress } from "./hooks/useLastUsedBtcAddress"
import referralProgram, { EmbedApp } from "./utils/referralProgram"
import { orangeKit } from "./utils"
import { AcreLedgerLiveBitcoinProviderOptions } from "./utils/orangekit/ledger-live/bitcoin-provider"

const isTestnet = env.USE_TESTNET
const CHAIN_ID = isTestnet ? sepolia.id : mainnet.id
Expand Down Expand Up @@ -34,12 +35,19 @@ async function getWagmiConfig() {
let createEmbedConnectorFn
const embeddedApp = referralProgram.getEmbeddedApp()
if (referralProgram.isEmbedApp(embeddedApp)) {
const lastUsedBtcAddress = getLastUsedBtcAddress()
const xpub = orangeKit.findXpubFromUrl(window.location.href)
const ledgerLiveConnectorOptions: AcreLedgerLiveBitcoinProviderOptions =
xpub
? { tryConnectToAccountByXpub: xpub }
: {
tryConnectToAddress: lastUsedBtcAddress,
}

const orangeKitLedgerLiveConnector =
orangeKit.getOrangeKitLedgerLiveConnector({
...connectorConfig,
options: {
tryConnectToAddress: getLastUsedBtcAddress(),
},
options: ledgerLiveConnectorOptions,
})

const embedConnectorsMap: Record<
Expand Down
Loading