Signing Transactions with your Ledger Nano S

Posted By : Amit

Aug 27, 2020

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.

 

1)Verify Address

 

const verifyLedgerAddress = async (chosenPath) => {
    let ledgerInitError = false
    try {
        if (!appEth) {
            ledgerInitError = true
            throw new Error()
        }
        await appEth.getAddress(
            chosenPath,
            true,
            true
        )
    } catch (error) {
        let verifyLedgerError = new Error()
        let message = ''
        if (ledgerInitError) {
            message = 'Unable to connect with the ledger application, retry logging in with your device'
        } else {
            message = 'Please verify the address on your ledger device and login again'
        }
        verifyLedgerError.message = message
        verifyLedgerError.name = 'LedgerAddressVerificationError'
        console.error('Error verifying ledger device', error)
        throw verifyLedgerError
    }
}

 

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.

 

2) Sign Transactions

 

const signTransaction = async function (txParams) {
    const path = localStorage.get('hdPath')
    let signature
    const rawTx = new Transaction(txParams)
    rawTx.v = Buffer.from([networkId])
    const serializedRawTx = rawTx.serialize().toString('hex')
    signature = await Vue.prototype.appEth.signTransaction(
        path,
        serializedRawTx
    )
    return signature
}

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
 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

April 5, 2024 at 05:21 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text