Continuing with the last blog details at Integrating Ledger Nano S, we were able to interact with the Ledger Nano S and generate a list of addresses from which users can choose one address and continue further. We use the same global variables declared in that blog for connecting with the Ledger Nano S.
In this blog, we work on the following
a) Verify Address step
b) Sign Transaction with Ledger and then broadcast the transaction.
a) Previously, we were able to initiate the appEth variable that will be used for any function related to Ledger Nano, be it transaction signing, fetching the chainCode, and publicKey to derive wallets, or verifying addresses.
b) After the address generation step, from the previous blog, we map the addresses generated to the correct hdPath used for their derivation, show the address list to the user, and when the user clicks on one of the addresses (to verify), we pass the hdPath to our verify address function.
c) VerifyLedgerAddress function takes the mapped hdPath as a parameter so that the address shown on the ledger device is the same as the one chosen by the user on the UI.
d) getAddress function uses that hdPath param (chosenPath), fetches the address based on that param, but in the process prompts the Verify Address screen on the ledger, the user can then match the address, and choose to verify or reject on the device.
e) On reject operation from the user, we throw an error with message = 'Please verify the address on your ledger device and log in again' to show the error to the user, and then he can try again.
a) For signing a transaction using the ledger Nano S, we need the hdPath along with the txParams object, which contains data like to address, gas, gas price, nonce value, etc.
b) We then create a Transaction object with those parameters, set up the network id, then use the signTransaction method on the appEth object available to us.
c) Once the signature is created, we can then broadcast the signature using sendSignedTransaction method available from web3js.