Last active
September 7, 2023 07:31
-
-
Save miguelmota/62559d02a1b99cb291635de4b224349c to your computer and use it in GitHub Desktop.
Node.js Ledger Nano S Ethereum sign transaction and broadcast
This file contains hidden or 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
const Transport = require('@ledgerhq/hw-transport-node-hid').default | |
const AppEth = require('@ledgerhq/hw-app-eth').default | |
const Tx = require('ethereumjs-tx') | |
const Web3 = require('web3') | |
const web3 = new Web3('https://mainnet.infura.io:443') | |
async function main() { | |
const index = 1 | |
const devices = await Transport.list() | |
if (devices.length === 0) throw 'no device' | |
const transport = await Transport.create() | |
const eth = new AppEth(transport) | |
const txData = { | |
nonce: web3.utils.toHex(0), | |
gasLimit: web3.utils.toHex(21000), | |
gasPrice: web3.utils.toHex(20e9), // 20 Gwei | |
to: '0x541Ef339F71d15C9401F50e0e2b1632A276e67Ee', | |
from: '0x08437Bc9E3C317f9c00b584fF8D29a8EF0A2783E', | |
value: web3.utils.toHex(web3.utils.toWei(web3.utils.toBN(1), 'ether')), // 1 eth | |
chainId: 1, | |
// these are required! | |
v: '0x01', // chain id | |
r: '0x00', | |
s: '0x00' | |
} | |
const tx = new Tx(txData) | |
const serializedTx = tx.serialize().toString('hex') | |
const sig = await eth.signTransaction(`44'/60'/0'/${index}`, serializedTx) | |
txData.v = '0x' + sig.v | |
txData.r = '0x' + sig.r | |
txData.s = '0x' + sig.s | |
const signedTx = new Tx(txData) | |
const signedSerializedTx = signedTx.serialize().toString('hex') | |
const txHash = await web3.eth.sendSignedTransaction('0x' + signedSerializedTx) | |
console.log(txHash) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment