Skip to content

Commit

Permalink
Initialize the Acre SDK w/o Ethereum address
Browse files Browse the repository at this point in the history
  • Loading branch information
r-czajkowski committed May 8, 2024
1 parent c42d607 commit 219377d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
8 changes: 4 additions & 4 deletions dapp/src/acre-react/contexts/AcreSdkContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
init: (bitcoinAddress: string, network: EthereumNetwork) => Promise<void>
isInitialized: boolean
}

Expand All @@ -22,11 +22,11 @@ export function AcreSdkProvider({ children }: { children: React.ReactNode }) {

// TODO: initialize Acre SDK w/o Ethereum address.
const init = useCallback<AcreSdkContextValue["init"]>(
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,
)
Expand Down
15 changes: 8 additions & 7 deletions dapp/src/hooks/sdk/useInitializeAcreSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
33 changes: 21 additions & 12 deletions dapp/src/web3/ledger-live-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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<string> {
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<T>(callback: () => Promise<T>) {
Expand All @@ -87,7 +93,7 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 {
return new LedgerLiveEthereumSigner(
this.#transport,
this.#client,
this.#account,
this.#bitcoinAccount,
provider,
)
}
Expand All @@ -97,7 +103,10 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 {
serializeLedgerWalletApiEthereumTransaction(transaction)

const buffer = await this.#clientRequest<Buffer>(() =>
this.#client.transaction.sign(this.#account.id, ethereumTransaction),
this.#client.transaction.sign(
this.#bitcoinAccount.id,
ethereumTransaction,
),
)

return buffer.toString()
Expand All @@ -111,7 +120,7 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 {

const transactionHash = await this.#clientRequest<string>(() =>
this.#client.transaction.signAndBroadcast(
this.#account.id,
this.#bitcoinAccount.id,
ethereumTransaction,
),
)
Expand All @@ -129,7 +138,7 @@ class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 {
async signMessage(message: string | Uint8Array): Promise<string> {
const buffer = await this.#clientRequest<Buffer>(() =>
this.#client.message.sign(
this.#account.id,
this.#bitcoinAccount.id,
Buffer.from(message.toString()),
),
)
Expand Down

0 comments on commit 219377d

Please sign in to comment.