From 219377d0745b0d9fd44a751d5964cc05e935a789 Mon Sep 17 00:00:00 2001 From: Rafal Czajkowski Date: Wed, 24 Apr 2024 17:11:47 +0200 Subject: [PATCH] Initialize the Acre SDK w/o Ethereum address --- .../acre-react/contexts/AcreSdkContext.tsx | 8 ++--- dapp/src/hooks/sdk/useInitializeAcreSdk.ts | 15 +++++---- dapp/src/web3/ledger-live-signer.ts | 33 ++++++++++++------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/dapp/src/acre-react/contexts/AcreSdkContext.tsx b/dapp/src/acre-react/contexts/AcreSdkContext.tsx index 6949d8a8e..a05a9de9f 100644 --- a/dapp/src/acre-react/contexts/AcreSdkContext.tsx +++ b/dapp/src/acre-react/contexts/AcreSdkContext.tsx @@ -6,7 +6,7 @@ const TBTC_API_ENDPOINT = import.meta.env.VITE_TBTC_API_ENDPOINT type AcreSdkContextValue = { acre?: Acre - init: (ethereumAddress: string, network: EthereumNetwork) => Promise + init: (bitcoinAddress: string, network: EthereumNetwork) => Promise isInitialized: boolean } @@ -22,11 +22,11 @@ export function AcreSdkProvider({ children }: { children: React.ReactNode }) { // TODO: initialize Acre SDK w/o Ethereum address. const init = useCallback( - async (ethereumAddress: string, network: EthereumNetwork) => { - if (!ethereumAddress) throw new Error("Ethereum address not defined") + async (bitcoinAddress: string, network: EthereumNetwork) => { + if (!bitcoinAddress) throw new Error("Bitcoin address not defined") const sdk = await Acre.initializeEthereum( - await LedgerLiveEthereumSigner.fromAddress(ethereumAddress), + await LedgerLiveEthereumSigner.fromAddress(bitcoinAddress), network, TBTC_API_ENDPOINT, ) diff --git a/dapp/src/hooks/sdk/useInitializeAcreSdk.ts b/dapp/src/hooks/sdk/useInitializeAcreSdk.ts index 79c04a4c7..3e97ebf4d 100644 --- a/dapp/src/hooks/sdk/useInitializeAcreSdk.ts +++ b/dapp/src/hooks/sdk/useInitializeAcreSdk.ts @@ -5,16 +5,17 @@ import { useAcreContext } from "#/acre-react/hooks" import { useWalletContext } from "../useWalletContext" export function useInitializeAcreSdk() { - const { ethAccount } = useWalletContext() + const { btcAccount } = useWalletContext() const { init } = useAcreContext() + const bitcoinAddress = btcAccount?.address useEffect(() => { - // TODO: Init Acre SDK w/o Ethereum account. - if (!ethAccount) return + if (!bitcoinAddress) return - const initSDK = async (ethAddress: string) => { - await init(ethAddress, ETHEREUM_NETWORK) + const initSDK = async (_bitcoinAddress: string) => { + await init(_bitcoinAddress, ETHEREUM_NETWORK) } - logPromiseFailure(initSDK(ethAccount)) - }, [ethAccount, init]) + + logPromiseFailure(initSDK(bitcoinAddress)) + }, [bitcoinAddress, init]) } diff --git a/dapp/src/web3/ledger-live-signer.ts b/dapp/src/web3/ledger-live-signer.ts index 7bdd658c6..95812617e 100644 --- a/dapp/src/web3/ledger-live-signer.ts +++ b/dapp/src/web3/ledger-live-signer.ts @@ -12,7 +12,7 @@ import { TypedDataField, TransactionResponse, } from "ethers" -import { CURRENCY_ID_ETHEREUM } from "#/constants" +import { CURRENCY_ID_BITCOIN } from "#/constants" import { EthereumSignerCompatibleWithEthersV5 } from "@acre-btc/sdk" import { getLedgerWalletAPITransport as getDappLedgerWalletAPITransport, @@ -28,10 +28,10 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { readonly #client: WalletAPIClient - readonly #account: Account + readonly #bitcoinAccount: Account static async fromAddress( - address: string, + bitcoinAddress: string, getLedgerWalletAPITransport: () => WindowMessageTransport = getDappLedgerWalletAPITransport, ) { const dappTransport = getLedgerWalletAPITransport() @@ -41,14 +41,14 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { const client = new WalletAPIClient(dappTransport) const accountsList = await client.account.list({ - currencyIds: [CURRENCY_ID_ETHEREUM], + currencyIds: [CURRENCY_ID_BITCOIN], }) dappTransport.disconnect() - const account = accountsList.find((acc) => acc.address === address) + const account = accountsList.find((acc) => acc.address === bitcoinAddress) - if (!account) throw new Error("Account not found") + if (!account) throw new Error("Bitcoin Account not found") return new LedgerLiveEthereumSigner( dappTransport, @@ -65,13 +65,19 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { provider: Provider | null, ) { super(provider) - this.#account = account + this.#bitcoinAccount = account this.#transport = transport this.#client = walletApiClient } + // eslint-disable-next-line class-methods-use-this getAddress(): Promise { - return Promise.resolve(this.#account.address) + // TODO: We should return the Ethereum address created based on the Bitcoin + // address. We probably will use the Signer from OrangeKit once it is + // implemented. For now, the `Signer.getAddress` is not used anywhere in the + // Acre SDK, only during the tBTC-v2.ts SDK initialization, so we can set + // random ethereum account. + return Promise.resolve("0x7b570B83D53e0671271DCa2CDf3429E9C4CAb12E") } async #clientRequest(callback: () => Promise) { @@ -87,7 +93,7 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { return new LedgerLiveEthereumSigner( this.#transport, this.#client, - this.#account, + this.#bitcoinAccount, provider, ) } @@ -97,7 +103,10 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { serializeLedgerWalletApiEthereumTransaction(transaction) const buffer = await this.#clientRequest(() => - this.#client.transaction.sign(this.#account.id, ethereumTransaction), + this.#client.transaction.sign( + this.#bitcoinAccount.id, + ethereumTransaction, + ), ) return buffer.toString() @@ -111,7 +120,7 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { const transactionHash = await this.#clientRequest(() => this.#client.transaction.signAndBroadcast( - this.#account.id, + this.#bitcoinAccount.id, ethereumTransaction, ), ) @@ -129,7 +138,7 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { async signMessage(message: string | Uint8Array): Promise { const buffer = await this.#clientRequest(() => this.#client.message.sign( - this.#account.id, + this.#bitcoinAccount.id, Buffer.from(message.toString()), ), )