Skip to content

Commit

Permalink
Add signer for ledger live app (#743)
Browse files Browse the repository at this point in the history
Refs: threshold-network/token-dashboard#649

In the T dashboard repo we've created an ethereum signer for Ledger Live
App (ref threshold-network/token-dashboard#655).
We've decided to move it here.

The LedgerLiveAppEthereumSigner extends the `Signer` class from
`ethers`. It implements all the needed methods, like `getAddress`,
`signMessage`, `signTransaction` and `connect`. Additionaly I've added
`sendTransaction` method that will be mainly used to communicate with
contracts. All the methods use `ledgergq/wallet-api-client` under the
hood.

### Constructor

The `Signer` class has `provider` property tha we have to define in the
constructor - that's why this is the argument needed to create an
instance of our ledger live app ehtereum signer. Besides that we will
also have `_walletApiClient`, `_windowMessageTransport` and `_account`
properties in our class. The first two can be also passed through a
constructor, but if they not they will be initialized automatically
using our methods in `src/lib/utils/ledger/wallet-api.ts`. ~~This is
also the reason why I've decided to create a separate `ledger` folder in
`utils`.~~ (EDIT: I've actually moved everything to one `ledger.ts`
file. See
#743 (comment)).

### Account

As i mentioned earlier, we also have `_account` property which will
store an `Account` object (from `@ledgerhw/wallet-api-client`). This
will be helpful whenn doing a transaction, because for that we will have
to use account id, so storing only the address would not work.

The `_account` is managed trough getter and setter, so the user can
request an account with either `wallet-api-client` or
`wallet-api-client-react` and use `setAccount` setter to store it in our
signer. The account MUST be set before doing transaction or signing a
message. If it's not set, then the proper error will be thrown.

We can also get only the address through the `getAddress` method
(required from `Signer` abstract class) and `getAccountId`, which is not
really used anywhere, but might be helpful in some cases.

I've also created a `requestAccount` method if we would want to trigger
the request account, but it will also not be used in our dApp, since we
will be requesting an account through a hook from
`wallet-api-client-react` and just setting the account with
`setAccount`. I've decided to keep it there anyway, as it might be
helpful for someone, who decides to use it in his app.

### Sending transaction

Sending transaction uses wallet-api under the hood. An example ethereum
transaction object, that we will be using with that library, can look
like this:

```
const ethereumTransaction = {
  family: "ethereum",
  amount: new BigNumber(100000000000000),
  recipient: "0xRecipientAddress",
  nonce: 2,
  data: Buffer.from("SomeDataInHex", "hex"),
  gasPrice: new BigNumber(20000000000),
  gasLimit: new BigNumber(21000),
};
```

It is worth noticing that calling a contract method requires to:
1) Pass `0` as the amount,
2) Pass contract address as the `recipient`
3) Pass the hex data related to calling that method
4) Set the `family` to `ethereum`

The rest of the things are optional. It is also worht noticing that the
lib uses `BigNumber` from `bignumber.js` library.

### Connecting and disconnecting the transport

When doing any operation (like sending transaction or requesting an
account) with `wallet-api` we have to connect the
`_windowMessageTransport` just before doing that, and disconnect it just
after to avoid some unexpected issues. It is as simple as calling
`this._walletMessageTransport.connect()` and
`this._walletMessageTransport.disconnect()` methods.
  • Loading branch information
lukasz-zimnoch authored Nov 14, 2023
2 parents 956fa07 + 3c874c4 commit 8822b31
Show file tree
Hide file tree
Showing 52 changed files with 1,317 additions and 351 deletions.
83 changes: 42 additions & 41 deletions typescript/api-reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [EthereumTBTCVault](classes/EthereumTBTCVault.md)
- [EthereumWalletRegistry](classes/EthereumWalletRegistry.md)
- [Hex](classes/Hex.md)
- [LedgerLiveEthereumSigner](classes/LedgerLiveEthereumSigner.md)
- [MaintenanceService](classes/MaintenanceService.md)
- [OptimisticMinting](classes/OptimisticMinting.md)
- [RedemptionsService](classes/RedemptionsService.md)
Expand Down Expand Up @@ -119,7 +120,7 @@ Data about a Bitcoin transaction input.

#### Defined in

[lib/bitcoin/tx.ts:63](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L63)
[src/lib/bitcoin/tx.ts:63](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L63)

___

Expand All @@ -131,7 +132,7 @@ Data about a Bitcoin unspent transaction output.

#### Defined in

[lib/bitcoin/tx.ts:93](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L93)
[src/lib/bitcoin/tx.ts:93](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L93)

___

Expand All @@ -143,7 +144,7 @@ Represents an event emitted on deposit reveal to the on-chain bridge.

#### Defined in

[lib/contracts/bridge.ts:283](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L283)
[src/lib/contracts/bridge.ts:283](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L283)

___

Expand All @@ -156,7 +157,7 @@ wallet registry.

#### Defined in

[lib/contracts/wallet-registry.ts:64](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/wallet-registry.ts#L64)
[src/lib/contracts/wallet-registry.ts:64](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/wallet-registry.ts#L64)

___

Expand All @@ -169,7 +170,7 @@ wallet registry.

#### Defined in

[lib/contracts/wallet-registry.ts:79](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/wallet-registry.ts#L79)
[src/lib/contracts/wallet-registry.ts:79](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/wallet-registry.ts#L79)

___

Expand All @@ -182,7 +183,7 @@ wallet registry.

#### Defined in

[lib/contracts/wallet-registry.ts:45](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/wallet-registry.ts#L45)
[src/lib/contracts/wallet-registry.ts:45](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/wallet-registry.ts#L45)

___

Expand All @@ -194,7 +195,7 @@ Additional options used by the Electrum server.

#### Defined in

[lib/electrum/client.ts:48](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L48)
[src/lib/electrum/client.ts:48](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L48)

___

Expand All @@ -220,7 +221,7 @@ True if the error matches, false otherwise.

#### Defined in

[lib/utils/backoff.ts:42](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L42)
[src/lib/utils/backoff.ts:42](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L42)

___

Expand All @@ -232,7 +233,7 @@ Supported Ethereum networks.

#### Defined in

[lib/ethereum/index.ts:74](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L74)
[src/lib/ethereum/index.ts:74](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L74)

___

Expand All @@ -246,7 +247,7 @@ or a Provider that works only in the read-only mode.

#### Defined in

[lib/ethereum/index.ts:25](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L25)
[src/lib/ethereum/index.ts:25](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L25)

___

Expand All @@ -272,7 +273,7 @@ A function that is called with execution status messages.

#### Defined in

[lib/utils/backoff.ts:56](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L56)
[src/lib/utils/backoff.ts:56](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L56)

___

Expand All @@ -284,7 +285,7 @@ Represents an event emitted when new wallet is registered on the on-chain bridge

#### Defined in

[lib/contracts/bridge.ts:445](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L445)
[src/lib/contracts/bridge.ts:445](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L445)

___

Expand All @@ -297,7 +298,7 @@ is cancelled on chain.

#### Defined in

[lib/contracts/tbtc-vault.ts:170](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L170)
[src/lib/contracts/tbtc-vault.ts:170](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L170)

___

Expand All @@ -310,7 +311,7 @@ is finalized on chain.

#### Defined in

[lib/contracts/tbtc-vault.ts:186](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L186)
[src/lib/contracts/tbtc-vault.ts:186](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L186)

___

Expand All @@ -330,7 +331,7 @@ Bridge.

#### Defined in

[lib/contracts/tbtc-vault.ts:120](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L120)
[src/lib/contracts/tbtc-vault.ts:120](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L120)

___

Expand All @@ -343,7 +344,7 @@ on chain.

#### Defined in

[lib/contracts/tbtc-vault.ts:136](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L136)
[src/lib/contracts/tbtc-vault.ts:136](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/tbtc-vault.ts#L136)

___

Expand All @@ -355,7 +356,7 @@ Represents an event emitted on redemption request.

#### Defined in

[lib/contracts/bridge.ts:334](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L334)
[src/lib/contracts/bridge.ts:334](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L334)

___

Expand Down Expand Up @@ -385,7 +386,7 @@ ___

#### Defined in

[lib/utils/backoff.ts:51](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L51)
[src/lib/utils/backoff.ts:51](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L51)

___

Expand All @@ -406,7 +407,7 @@ Convenience type aggregating all TBTC contracts handles.

#### Defined in

[lib/contracts/index.ts:16](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/index.ts#L16)
[src/lib/contracts/index.ts:16](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/index.ts#L16)

## Variables

Expand All @@ -428,7 +429,7 @@ Utility functions allowing to perform Bitcoin address conversions.

#### Defined in

[lib/bitcoin/address.ts:112](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/address.ts#L112)
[src/lib/bitcoin/address.ts:112](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/address.ts#L112)

___

Expand All @@ -446,7 +447,7 @@ Utility functions allowing to deal with Bitcoin compact size uints.

#### Defined in

[lib/bitcoin/csuint.ts:50](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/csuint.ts#L50)
[src/lib/bitcoin/csuint.ts:50](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/csuint.ts#L50)

___

Expand All @@ -467,7 +468,7 @@ Utility functions allowing to deal with Bitcoin hashes.

#### Defined in

[lib/bitcoin/hash.ts:52](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/hash.ts#L52)
[src/lib/bitcoin/hash.ts:52](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/hash.ts#L52)

___

Expand All @@ -487,7 +488,7 @@ Utility functions allowing to serialize and deserialize Bitcoin block headers.

#### Defined in

[lib/bitcoin/header.ts:109](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/header.ts#L109)
[src/lib/bitcoin/header.ts:109](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/header.ts#L109)

___

Expand All @@ -506,7 +507,7 @@ Utility functions allowing to deal with Bitcoin locktime.

#### Defined in

[lib/bitcoin/tx.ts:234](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L234)
[src/lib/bitcoin/tx.ts:234](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L234)

___

Expand All @@ -524,7 +525,7 @@ Utility functions allowing to perform operations on Bitcoin ECDSA private keys.

#### Defined in

[lib/bitcoin/ecdsa-key.ts:77](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/ecdsa-key.ts#L77)
[src/lib/bitcoin/ecdsa-key.ts:77](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/ecdsa-key.ts#L77)

___

Expand All @@ -543,7 +544,7 @@ Utility functions allowing to perform operations on Bitcoin ECDSA public keys.

#### Defined in

[lib/bitcoin/ecdsa-key.ts:51](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/ecdsa-key.ts#L51)
[src/lib/bitcoin/ecdsa-key.ts:51](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/ecdsa-key.ts#L51)

___

Expand All @@ -564,7 +565,7 @@ Utility functions allowing to deal with Bitcoin scripts.

#### Defined in

[lib/bitcoin/script.ts:63](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/script.ts#L63)
[src/lib/bitcoin/script.ts:63](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/script.ts#L63)

___

Expand All @@ -583,7 +584,7 @@ Utility functions allowing to perform Bitcoin target conversions.

#### Defined in

[lib/bitcoin/header.ts:268](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/header.ts#L268)
[src/lib/bitcoin/header.ts:268](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/header.ts#L268)

## Functions

Expand All @@ -610,7 +611,7 @@ Bitcoin transaction along with the inclusion proof.

#### Defined in

[lib/bitcoin/spv.ts:64](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L64)
[src/lib/bitcoin/spv.ts:64](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L64)

___

Expand Down Expand Up @@ -660,7 +661,7 @@ A function that can retry any function.

#### Defined in

[lib/utils/backoff.ts:89](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L89)
[src/lib/utils/backoff.ts:89](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L89)

___

Expand All @@ -685,7 +686,7 @@ Electrum script hash as a hex string.

#### Defined in

[lib/electrum/client.ts:649](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L649)
[src/lib/electrum/client.ts:649](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/electrum/client.ts#L649)

___

Expand Down Expand Up @@ -715,7 +716,7 @@ Throws an error if the address of the signer is not a proper

#### Defined in

[lib/ethereum/index.ts:61](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L61)
[src/lib/ethereum/index.ts:61](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L61)

___

Expand All @@ -739,7 +740,7 @@ Ethereum network.

#### Defined in

[lib/ethereum/index.ts:32](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L32)
[src/lib/ethereum/index.ts:32](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L32)

___

Expand All @@ -764,7 +765,7 @@ Transaction data with fields represented as un-prefixed hex strings.

#### Defined in

[lib/bitcoin/tx.ts:133](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L133)
[src/lib/bitcoin/tx.ts:133](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/tx.ts#L133)

___

Expand Down Expand Up @@ -795,7 +796,7 @@ Throws an error if the signer's Ethereum network is other than

#### Defined in

[lib/ethereum/index.ts:85](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L85)
[src/lib/ethereum/index.ts:85](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/ethereum/index.ts#L85)

___

Expand All @@ -820,7 +821,7 @@ Always returns true.

#### Defined in

[lib/utils/backoff.ts:9](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L9)
[src/lib/utils/backoff.ts:9](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L9)

___

Expand All @@ -846,7 +847,7 @@ Matcher function that returns false if error matches one of the patterns.

#### Defined in

[lib/utils/backoff.ts:20](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L20)
[src/lib/utils/backoff.ts:20](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/utils/backoff.ts#L20)

___

Expand Down Expand Up @@ -875,7 +876,7 @@ An error if the network is not supported by `bitcoinjs-lib`.

#### Defined in

[lib/bitcoin/network.ts:55](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/network.ts#L55)
[src/lib/bitcoin/network.ts:55](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/network.ts#L55)

___

Expand Down Expand Up @@ -916,7 +917,7 @@ If any of the block headers are invalid, or if the block

#### Defined in

[lib/bitcoin/header.ts:132](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/header.ts#L132)
[src/lib/bitcoin/header.ts:132](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/header.ts#L132)

___

Expand Down Expand Up @@ -956,7 +957,7 @@ The function should be used within a try-catch block.

#### Defined in

[lib/bitcoin/spv.ts:145](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L145)
[src/lib/bitcoin/spv.ts:145](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/bitcoin/spv.ts#L145)

___

Expand All @@ -983,4 +984,4 @@ This function does not validate the depositor's identifier as its

#### Defined in

[lib/contracts/bridge.ts:228](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L228)
[src/lib/contracts/bridge.ts:228](https://github.com/keep-network/tbtc-v2/blob/main/typescript/src/lib/contracts/bridge.ts#L228)
Loading

0 comments on commit 8822b31

Please sign in to comment.