Skip to content

Instantly share code, notes, and snippets.

@jamesyoung
Last active December 1, 2017 09:35
Show Gist options
  • Save jamesyoung/5f7b1435093a22e9ba791d247bad1ce1 to your computer and use it in GitHub Desktop.
Save jamesyoung/5f7b1435093a22e9ba791d247bad1ce1 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const Tx = require('ethereumjs-tx')
const ledger = require('ledgerco')
const Web3 = require('web3')
const rlp = require('rlp');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8585"))
const metacoinJSON = require('./build/contracts/MetaCoin.json')
const bytecode = metacoinJSON.unlinked_binary
const ownerAddress = fs.readFileSync('./owner.addr', 'utf8').toString()
const sign = async (nonce) => {
const txnObj = {
nonce: '00',
value: '00',
gasPrice: '4E3B29200',
gasLimit: '3D0900',
data: bytecode.split('0x')[1],
from: ownerAddress.split('0x')[1]
}
const raw = [];
raw.push(new Buffer(txnObj.nonce, 'hex'));
raw.push(new Buffer(txnObj.gasPrice, 'hex'));
raw.push(new Buffer(txnObj.gasLimit, 'hex'));
raw.push(new Buffer(txnObj.data, 'hex'));
const encoded = rlp.encode(raw)
try {
const comm = await ledger.comm_node.create_async()
const eth = new ledger.eth(comm)
const result = await eth.signTransaction_async("44'/60'/0'/0'/0", encoded)
.then(function(result) {
console.log(result)
})
.fail(function(err) {console.log('err',err);}); // err Invalid status 6a80
} catch (err) {
console.log(err)
}
/* THIS WORKS */
// const privateKey = fs.readFileSync('./privateKey.addr', 'utf8').toString()
// const contract = web3.eth.contract(metacoinJSON.abi)
// const bytecode = metacoinJSON.unlinked_binary
// const contractData = contract.new.getData({ data: bytecode })
// const gasPrice = web3.eth.gasPrice
// const gasPriceHex = web3.toHex(gasPrice)
// const gasLimitHex = web3.toHex(6000000)
// const nonceHex = web3.toHex(nonce)
// const txObj = {
// nonce: nonceHex,
// gasPrice: gasPriceHex,
// gasLimit: gasLimitHex,
// data: contractData,
// from: ownerAddress
// }
// const Tx = require('ethereumjs-tx')
// const tx = new Tx(txObj)
// tx.sign(privateKey)
// const serializedTx = tx.serialize()
// web3.eth.sendRawTransaction('0x'+serializedTx.toString('hex'), (err, hash) => {
// if (err) { throw err }
// console.log(`https://rinkeby.etherscan.io/tx/${hash}`)
// })
}
sign(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment