-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add signer for ledger live app (#743)
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
Showing
52 changed files
with
1,317 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.