Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function signTransaction(txData) {
const dPath = addressToPathMap[txData.from]
if (!dPath) throw new Error(`address unknown '${txData.from}'`)
const transport = await getTransport()
try {
const eth = new AppEth(transport)
const tx = new EthereumTx(txData)
// Set the EIP155 bits
tx.raw[6] = Buffer.from([networkId]) // v
tx.raw[7] = Buffer.from([]) // r
tx.raw[8] = Buffer.from([]) // s
// Pass hex-rlp to ledger for signing
const result = await eth.signTransaction(dPath, tx.serialize().toString('hex'))
// Store signature in transaction
tx.v = Buffer.from(result.v, 'hex')
tx.r = Buffer.from(result.r, 'hex')
tx.s = Buffer.from(result.s, 'hex')
// EIP155: v should be chain_id * 2 + {35, 36}
async function getAccounts() {
let transport
try {
transport = await getTransport()
} catch (e) {
return Promise.reject(e)
}
try {
const eth = new AppEth(transport)
const addresses = {}
for (let i = accountsOffset; i < accountsOffset + accountsLength; i++) {
const accountPath = `${pathComponents.basePath}/${pathComponents.index + i}`
let address
try {
address = await eth.getAddress(accountPath, askConfirm, false) //eslint-disable-line
} catch (e) {
return Promise.reject(e)
}
addresses[path] = address.address.toLowerCase()
addressToPathMap[address.address.toLowerCase()] = path
}
return addresses
} finally {
transport.close()
}
async function signPersonalMessage(msgData) {
const dPath = addressToPathMap[msgData.from.toLowerCase()]
if (!dPath) throw new Error(`address unknown '${msgData.from}'`)
const transport = await getTransport()
try {
const eth = new AppEth(transport)
let result
try {
result = await eth.signPersonalMessage(dPath, stripHexPrefix(msgData.data))
} catch (e) {
return Promise.reject(e)
}
const v = parseInt(result.v, 10) - 27
let vHex = v.toString(16)
if (vHex.length < 2) {
vHex = `0${v}`
}
return `0x${result.r}${result.s}${vHex}`
} finally {
transport.close()
const getLedgerAccount = async subPath => {
const transport = await TransportU2F.create(10000, 10000)
const eth = new AppEth(transport)
const path = `${subPath}`
const addressPromise = eth.getAddress(path, false, true)
addressPromise.then(() => transport.close())
return addressPromise
}