Skip to content

Instantly share code, notes, and snippets.

@changwu-tw
Created February 5, 2017 16:43
Show Gist options
  • Save changwu-tw/12b62225d4e0da3dfa6141cde46dfc8b to your computer and use it in GitHub Desktop.
Save changwu-tw/12b62225d4e0da3dfa6141cde46dfc8b to your computer and use it in GitHub Desktop.
Create raw transaction & sign transaction
// 地址:0x7754cea59bffd5b3638d617121fac3e224e507b3
// 私鑰:ec359d9f260fa5281b4f41c0001be1df1b0e81a742b19f69cd51fe8ed9d20dc7
> var privKey1 = '7f1a49ce75b524b81dbb0964db5da64d5b588abc4fd3f60535a97a3f8115ba43'
undefined
> var EthTx = require("ethereumjs-tx")
undefined
> var privKey1x = new Buffer(privKey1, 'hex')
undefined
> privKey1x
<Buffer 7f 1a 49 ce 75 b5 24 b8 1d bb 09 64 db 5d a6 4d 5b 58 8a bc 4f d3 f6 05 35 a9 7a 3f 81 15 ba 43>
> var rawTx = {
… nonce: web3.toHex(web3.eth.getTransactionCount(acct1)),
… to: acct2,
… gasPrice: web3.toHex(2000000000),
… gasLimit: web3.toHex(21000),
… value: web3.toHex(web3.toWei(25, 'ether')),
… data: ''
… }
undefined
> rawTx
{ nonce: '0x2',
to: '0x1b8d0c462a116a2496bf607aebf1b3d9ee49f5f1',
gasPrice: '0x77359400',
gasLimit: '0x5208',
value: '0x15af1d78b58c40000',
data: '' }
> var tx = new EthTx(rawTx)
undefined
> tx.sign(privKey1x)
undefined
> tx.serialize().toString('hex')
'f86c028477359400825208941b8d0c462a116a2496bf607aebf1b3d9ee49f5f189015af1d78b58c40000801ca0fefa0a24e6ddd058243a1f05280481a8f0729bc951b5e2a8b171b1eb10cef737a03bd521d07f1332767d098ba4834712f77d4d63e809f6d928206edc5f1392ec50'
> web3.eth.sendRawTransaction(`0x${tx.serialize().toString('hex')}`)
'0x1b524be73803bdafd16dd5021b0c396601af32e8b606185c14e02a74732f9d1e'
> web3.eth.sendRawTransaction(`0x${tx.serialize().toString('hex')}`, (error, data) => {
… if (!error) { console.log(data) }
… })
undefined
> 0x74e76429588284eca9565f1dd6c4f2e63cb7152ecb63f7db851598e6bb654ed5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment