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

Add signer for ledger live app #743

Merged
merged 19 commits into from
Nov 14, 2023
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
1 change: 1 addition & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@bitcoinerlab/secp256k1": "^1.0.5",
"@keep-network/ecdsa": "development",
"@keep-network/tbtc-v2": "development",
"@ledgerhq/wallet-api-client": "^1.2.1",
"bitcoinjs-lib": "^6.1.5",
"bufio": "^1.0.6",
"ecpair": "^2.1.0",
Expand Down
1 change: 1 addition & 0 deletions typescript/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./backoff"
export * from "./hex"
export * from "./ledger"
2 changes: 2 additions & 0 deletions typescript/src/lib/utils/ledger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./ledger"
export * from "./wallet-api"
170 changes: 170 additions & 0 deletions typescript/src/lib/utils/ledger/ledger.ts
lukasz-zimnoch marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { ethers } from "ethers"
import {
Account,
WalletAPIClient,
WindowMessageTransport,
} from "@ledgerhq/wallet-api-client"
import BigNumber from "bignumber.js"
import { Hex } from "../../utils"
import { AddressZero } from "@ethersproject/constants"
import { Deferrable } from "@ethersproject/properties"
import { getWalletAPIClient, getWindowMessageTransport } from "."
import { Signer } from "ethers"

export class LedgerLiveAppEthereumSigner extends Signer {
lukasz-zimnoch marked this conversation as resolved.
Show resolved Hide resolved
private _walletApiClient: WalletAPIClient
private _windowMessageTransport: WindowMessageTransport
private _account: Account | undefined

constructor(
provider: ethers.providers.Provider,
windowMessageTransport?: WindowMessageTransport,
walletApiClient?: WalletAPIClient
) {
super()
ethers.utils.defineReadOnly(this, "provider", provider || null)
this._windowMessageTransport =
windowMessageTransport || getWindowMessageTransport()
this._walletApiClient =
walletApiClient || getWalletAPIClient(this._windowMessageTransport)
}

get account() {
return this._account
}

setAccount(account: Account | undefined): void {
this._account = account
}

async requestAccount(
params: { currencyIds?: string[] | undefined } | undefined
): Promise<Account> {
this._windowMessageTransport.connect()
const account = await this._walletApiClient.account.request(params)
this._windowMessageTransport.disconnect()
this._account = account
return this._account
}

getAccountId(): string {
if (!this._account || !this._account.id) {
throw new Error(
"Account not found. Please use `requestAccount` method first."
)
}
return this._account.id
}

async getAddress(): Promise<string> {
if (!this._account || !this._account.address) {
throw new Error(
"Account not found. Please use `requestAccount` method first."
)
}
return this._account.address
}

async signMessage(message: string): Promise<string> {
if (!this._account || !this._account.address) {
throw new Error(
"Account not found. Please use `requestAccount` method first."
)
}
this._windowMessageTransport.connect()
const buffer = await this._walletApiClient.message.sign(
this._account.id,
Buffer.from(message)
)
this._windowMessageTransport.disconnect()
return buffer.toString()
}

async signTransaction(
transaction: ethers.providers.TransactionRequest
): Promise<string> {
if (!this._account || !this._account.address) {
throw new Error(
"Account not found. Please use `requestAccount` method first."
)
}

const { value, to, nonce, data, gasPrice, gasLimit } = transaction

const ethereumTransaction: any = {
family: "ethereum" as const,
amount: value ? new BigNumber(value.toString()) : new BigNumber(0),
recipient: to ? to : AddressZero,
}

if (nonce) ethereumTransaction.nonce = nonce
if (data)
ethereumTransaction.data = Buffer.from(
Hex.from(data.toString()).toString(),
"hex"
)
if (gasPrice)
ethereumTransaction.gasPrice = new BigNumber(gasPrice.toString())
if (gasLimit)
ethereumTransaction.gasLimit = new BigNumber(gasLimit.toString())

this._windowMessageTransport.connect()
const buffer = await this._walletApiClient.transaction.sign(
this._account.id,
ethereumTransaction
)
this._windowMessageTransport.disconnect()
return buffer.toString()
}

async sendTransaction(
transaction: Deferrable<ethers.providers.TransactionRequest>
): Promise<ethers.providers.TransactionResponse> {
if (!this._account || !this._account.address) {
throw new Error(
"Account not found. Please use `requestAccount` method first."
)
}

const { value, to, nonce, data, gasPrice, gasLimit } = transaction

const ethereumTransaction: any = {
family: "ethereum" as const,
amount: value ? new BigNumber(value.toString()) : new BigNumber(0),
recipient: to ? to : AddressZero,
}

if (nonce) ethereumTransaction.nonce = nonce
if (data)
ethereumTransaction.data = Buffer.from(
Hex.from(data.toString()).toString(),
"hex"
)
if (gasPrice)
ethereumTransaction.gasPrice = new BigNumber(gasPrice.toString())
if (gasLimit)
ethereumTransaction.gasLimit = new BigNumber(gasLimit.toString())

this._windowMessageTransport.connect()
const transactionHash =
await this._walletApiClient.transaction.signAndBroadcast(
this._account.id,
ethereumTransaction
)
this._windowMessageTransport.disconnect()

const transactionResponse = await this.provider?.getTransaction(
transactionHash
)

if (!transactionResponse) {
throw new Error("Transaction response not found!")
}

return transactionResponse
}

connect(provider: ethers.providers.Provider): Signer {
return new LedgerLiveAppEthereumSigner(provider)
}
}
16 changes: 16 additions & 0 deletions typescript/src/lib/utils/ledger/wallet-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
WalletAPIClient,
WindowMessageTransport,
} from "@ledgerhq/wallet-api-client"

export const getWindowMessageTransport = () => {
return new WindowMessageTransport()
}

export const getWalletAPIClient = (
windowMessageTransport: WindowMessageTransport
) => {
const walletApiClient = new WalletAPIClient(windowMessageTransport)

return walletApiClient
}
63 changes: 63 additions & 0 deletions typescript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1633,11 +1633,26 @@
rxjs "6"
semver "^7.3.5"

"@ledgerhq/devices@^8.0.7":
version "8.0.7"
resolved "https://registry.yarnpkg.com/@ledgerhq/devices/-/devices-8.0.7.tgz#206434dbd8a097529bbfc95f5eef94c2923c7578"
integrity sha512-BbPyET52lXnVs7CxJWrGYqmtGdbGzj+XnfCqLsDnA7QYr1CZREysxmie+Rr6BKpNDBRVesAovXjtaVaZOn+upw==
dependencies:
"@ledgerhq/errors" "^6.14.0"
"@ledgerhq/logs" "^6.10.1"
rxjs "6"
semver "^7.3.5"

"@ledgerhq/errors@^5.50.0":
version "5.50.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/errors/-/errors-5.50.0.tgz#e3a6834cb8c19346efca214c1af84ed28e69dad9"
integrity sha512-gu6aJ/BHuRlpU7kgVpy2vcYk6atjB4iauP2ymF7Gk0ez0Y/6VSMVSJvubeEQN+IV60+OBK0JgeIZG7OiHaw8ow==

"@ledgerhq/errors@^6.14.0":
version "6.14.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/errors/-/errors-6.14.0.tgz#0bf253983773ef12eebce2091f463bc719223b37"
integrity sha512-ZWJw2Ti6Dq1Ott/+qYqJdDWeZm16qI3VNG5rFlb0TQ3UcAyLIQZbnnzzdcVVwVeZiEp66WIpINd/pBdqsHVyOA==

"@ledgerhq/hw-app-eth@^5.11.0":
version "5.53.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-eth/-/hw-app-eth-5.53.0.tgz#5df2d7427db9f387099d0cc437e9730101d7c404"
Expand All @@ -1659,11 +1674,44 @@
"@ledgerhq/errors" "^5.50.0"
events "^3.3.0"

"@ledgerhq/hw-transport@^6.28.8":
version "6.28.8"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-6.28.8.tgz#f99a5c71c5c09591e9bfb1b970c42aafbe81351f"
integrity sha512-XxQVl4htd018u/M66r0iu5nlHi+J6QfdPsORzDF6N39jaz+tMqItb7tUlXM/isggcuS5lc7GJo7NOuJ8rvHZaQ==
dependencies:
"@ledgerhq/devices" "^8.0.7"
"@ledgerhq/errors" "^6.14.0"
events "^3.3.0"

"@ledgerhq/logs@^5.50.0":
version "5.50.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/logs/-/logs-5.50.0.tgz#29c6419e8379d496ab6d0426eadf3c4d100cd186"
integrity sha512-swKHYCOZUGyVt4ge0u8a7AwNcA//h4nx5wIi0sruGye1IJ5Cva0GyK9L2/WdX+kWVTKp92ZiEo1df31lrWGPgA==

"@ledgerhq/logs@^6.10.1":
version "6.10.1"
resolved "https://registry.yarnpkg.com/@ledgerhq/logs/-/logs-6.10.1.tgz#5bd16082261d7364eabb511c788f00937dac588d"
integrity sha512-z+ILK8Q3y+nfUl43ctCPuR4Y2bIxk/ooCQFwZxhtci1EhAtMDzMAx2W25qx8G1PPL9UUOdnUax19+F0OjXoj4w==

"@ledgerhq/wallet-api-client@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@ledgerhq/wallet-api-client/-/wallet-api-client-1.2.1.tgz#b47fe5b4f431282f50ddb64c8abb911545593eba"
integrity sha512-uTBTZCpbLTM5y5Cd7ioQB0lcq0b3cbrU2bGzCiKuY1IEd0NUyFhr2dKliRrcLoMPDRtQRmRnSxeX0BFKinoo8Q==
dependencies:
"@ledgerhq/hw-transport" "^6.28.8"
"@ledgerhq/wallet-api-core" "1.3.1"
bignumber.js "^9.1.2"

"@ledgerhq/[email protected]":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@ledgerhq/wallet-api-core/-/wallet-api-core-1.3.1.tgz#092d027a6d9ce7139a2d4c157480e2feb7c88489"
integrity sha512-yOeb1tfdwF6NdxVEIVr8SVz5iOyh6asWa0bbuCyMpiLrfuVS/Wkr6OeDMBYSxWxXxRFmQDJ9XQxdtSS+MGNk1Q==
dependencies:
"@ledgerhq/errors" "^6.14.0"
bignumber.js "^9.1.2"
uuid "^9.0.0"
zod "^3.22.2"

"@noble/hashes@^1.1.5", "@noble/hashes@^1.2.0":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
Expand Down Expand Up @@ -2574,6 +2622,11 @@ bignumber.js@^7.2.0:
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-7.2.1.tgz#80c048759d826800807c4bfd521e50edbba57a5f"
integrity sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ==

bignumber.js@^9.1.2:
version "9.1.2"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
Expand Down Expand Up @@ -7887,6 +7940,11 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^9.0.0:
version "9.0.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
Expand Down Expand Up @@ -9033,3 +9091,8 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zod@^3.22.2:
version "3.22.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==
Loading