Skip to content

Instantly share code, notes, and snippets.

@bitgord
Created December 3, 2016 19:40
Show Gist options
  • Save bitgord/38132054a844d8e52068bedad0d414ca to your computer and use it in GitHub Desktop.
Save bitgord/38132054a844d8e52068bedad0d414ca to your computer and use it in GitHub Desktop.
Sign-Eth-Transactions
//Set Variable for PrivateKey in TestRPC
var pKey1 = ""
//Require EthTx
var EthTx = require("ethereumjs-tx")
//Create Buffer Encoded Class
var pKey1x = new Buffer(pKey1, 'hex')
//Create Raw Transaction Data Structure
var rawTx = {
nonce: web3.toHex(web3.eth.getTransactionCount(acct1)),
to: acct2,
gasPrice: web3.toHex(2000000000000),
gasLimit: web3.toHex(21000),
value: web3.toHex(web3.toWei(25, 'ether')),
data: ""
}
//new Tx Variable and pass in rawTx
var tx = new EthTx(rawTx)
//Sign with Private Key
tx.sign(pKey1x)
//Serialize to String
tx.serialize().toString('hex')
//Send Raw Transaction
web3.eth.sendRawTransaction(`0x${tx.serialize().toString('hex')}`, (error, data) => {
if(!error) { console.log(data) }
})
//Inspect Transaction
web3.eth.getTransaction("[[put transaction string that was returned]]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment