From 3cdb91ed239537294a492d9b01a9c1c360b9a928 Mon Sep 17 00:00:00 2001 From: Rafal Czajkowski Date: Wed, 15 May 2024 10:15:03 +0200 Subject: [PATCH] Remove unused ethers signer for Ledger Live Now the Acre SDK requires the bitcoin provider not ethereum singer. --- dapp/src/web3/index.ts | 1 - dapp/src/web3/ledger-live-signer.ts | 165 ---------------------------- dapp/src/web3/utils/index.ts | 1 - dapp/src/web3/utils/ledger-live.ts | 56 ---------- 4 files changed, 223 deletions(-) delete mode 100644 dapp/src/web3/index.ts delete mode 100644 dapp/src/web3/ledger-live-signer.ts delete mode 100644 dapp/src/web3/utils/index.ts delete mode 100644 dapp/src/web3/utils/ledger-live.ts diff --git a/dapp/src/web3/index.ts b/dapp/src/web3/index.ts deleted file mode 100644 index f38e7e840..000000000 --- a/dapp/src/web3/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./ledger-live-signer" diff --git a/dapp/src/web3/ledger-live-signer.ts b/dapp/src/web3/ledger-live-signer.ts deleted file mode 100644 index 95812617e..000000000 --- a/dapp/src/web3/ledger-live-signer.ts +++ /dev/null @@ -1,165 +0,0 @@ -import { - Account, - WalletAPIClient, - WindowMessageTransport, -} from "@ledgerhq/wallet-api-client" -import { - TypedDataEncoder, - Provider, - Signer, - TransactionRequest, - TypedDataDomain, - TypedDataField, - TransactionResponse, -} from "ethers" -import { CURRENCY_ID_BITCOIN } from "#/constants" -import { EthereumSignerCompatibleWithEthersV5 } from "@acre-btc/sdk" -import { - getLedgerWalletAPITransport as getDappLedgerWalletAPITransport, - getLedgerLiveProvider, - serializeLedgerWalletApiEthereumTransaction, -} from "./utils" - -// Created based on the -// https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/ledger.ts -// but with support for ethers v6. -class LedgerLiveEthereumSigner extends EthereumSignerCompatibleWithEthersV5 { - readonly #transport: WindowMessageTransport - - readonly #client: WalletAPIClient - - readonly #bitcoinAccount: Account - - static async fromAddress( - bitcoinAddress: string, - getLedgerWalletAPITransport: () => WindowMessageTransport = getDappLedgerWalletAPITransport, - ) { - const dappTransport = getLedgerWalletAPITransport() - - dappTransport.connect() - - const client = new WalletAPIClient(dappTransport) - - const accountsList = await client.account.list({ - currencyIds: [CURRENCY_ID_BITCOIN], - }) - - dappTransport.disconnect() - - const account = accountsList.find((acc) => acc.address === bitcoinAddress) - - if (!account) throw new Error("Bitcoin Account not found") - - return new LedgerLiveEthereumSigner( - dappTransport, - client, - account, - getLedgerLiveProvider(), - ) - } - - private constructor( - transport: WindowMessageTransport, - walletApiClient: WalletAPIClient, - account: Account, - provider: Provider | null, - ) { - super(provider) - this.#bitcoinAccount = account - this.#transport = transport - this.#client = walletApiClient - } - - // eslint-disable-next-line class-methods-use-this - getAddress(): Promise { - // 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) { - try { - this.#transport.connect() - return await callback() - } finally { - this.#transport.disconnect() - } - } - - connect(provider: Provider | null): Signer { - return new LedgerLiveEthereumSigner( - this.#transport, - this.#client, - this.#bitcoinAccount, - provider, - ) - } - - async signTransaction(transaction: TransactionRequest): Promise { - const ethereumTransaction = - serializeLedgerWalletApiEthereumTransaction(transaction) - - const buffer = await this.#clientRequest(() => - this.#client.transaction.sign( - this.#bitcoinAccount.id, - ethereumTransaction, - ), - ) - - return buffer.toString() - } - - async sendTransaction(tx: TransactionRequest): Promise { - const populatedTransaction = await this.populateTransaction(tx) - - const ethereumTransaction = - serializeLedgerWalletApiEthereumTransaction(populatedTransaction) - - const transactionHash = await this.#clientRequest(() => - this.#client.transaction.signAndBroadcast( - this.#bitcoinAccount.id, - ethereumTransaction, - ), - ) - - const transactionResponse = - await this.provider?.getTransaction(transactionHash) - - if (!transactionResponse) { - throw new Error("Transaction response not found!") - } - - return transactionResponse - } - - async signMessage(message: string | Uint8Array): Promise { - const buffer = await this.#clientRequest(() => - this.#client.message.sign( - this.#bitcoinAccount.id, - Buffer.from(message.toString()), - ), - ) - - return buffer.toString("hex") - } - - async signTypedData( - domain: TypedDataDomain, - types: Record, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - value: Record, - ): Promise { - const payload = TypedDataEncoder.getPayload( - domain, - types, - value, - ) as unknown as object - - return this.signMessage(JSON.stringify(payload)) - } -} - -export { LedgerLiveEthereumSigner } diff --git a/dapp/src/web3/utils/index.ts b/dapp/src/web3/utils/index.ts deleted file mode 100644 index 222c65f9b..000000000 --- a/dapp/src/web3/utils/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./ledger-live" diff --git a/dapp/src/web3/utils/ledger-live.ts b/dapp/src/web3/utils/ledger-live.ts deleted file mode 100644 index 5ee8122f7..000000000 --- a/dapp/src/web3/utils/ledger-live.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { - EthereumTransaction, - WindowMessageTransport, -} from "@ledgerhq/wallet-api-client" -import { TransactionRequest, ZeroAddress, JsonRpcProvider } from "ethers" -import { Hex } from "@acre-btc/sdk" - -export const getLedgerWalletAPITransport = () => new WindowMessageTransport() - -export const getLedgerLiveProvider = () => - new JsonRpcProvider(import.meta.env.VITE_ETH_HOSTNAME_HTTP) - -// Created based on the -// https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/ledger.ts. -export function serializeLedgerWalletApiEthereumTransaction( - transaction: TransactionRequest, -): EthereumTransaction { - const { - value, - to, - nonce, - data, - gasPrice, - gasLimit, - maxFeePerGas, - maxPriorityFeePerGas, - } = transaction - - const ethereumTransaction: EthereumTransaction = { - family: "ethereum" as const, - // @ts-expect-error We do not want to install external bignumber.js lib so - // here we use bigint. The Ledger Wallet Api just converts the bignumber.js - // object to string so we can pass bigint. See: - // https://github.com/LedgerHQ/wallet-api/blob/main/packages/core/src/families/ethereum/serializer.ts#L4 - amount: value ?? 0, - recipient: to?.toString() || ZeroAddress, - nonce: nonce ?? undefined, - // @ts-expect-error See comment above. - gasPrice: gasPrice ?? undefined, - // @ts-expect-error See comment above. - gasLimit: gasLimit ?? undefined, - // @ts-expect-error See comment above. - maxFeePerGas: maxFeePerGas ?? undefined, - // @ts-expect-error See comment above. - maxPriorityFeePerGas: maxPriorityFeePerGas ?? undefined, - } - - if (nonce) ethereumTransaction.nonce = Number(nonce) - if (data) - ethereumTransaction.data = Buffer.from( - Hex.from(data.toString()).toString(), - "hex", - ) - - return ethereumTransaction -}