Signing Transactions with your Ledger Nano S

Signing Transactions with your Ledger Nano S

Posted By : Amit Kumar | 27-Aug-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
 

About Author

loading
Amit Kumar

Amit is a Backend Developer and previously created web apps using javascript, HTML, CSS, Jquery . His hobbies are playing football and listening music.

Leave a Comment

Name is required

Comment is required

Recaptcha is required.

No Comments Yet.

    Request For Proposal

    Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.

    We would love to hear from you!

    Oodles | Blockchain Development Company

    Please enter a valid Phone Number

    Please remove URL from text

    Recaptcha is required.