Skip to content

Instantly share code, notes, and snippets.

@cdetrio
Created May 4, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdetrio/8a4caa3511558b9daa20340833bcce75 to your computer and use it in GitHub Desktop.
Save cdetrio/8a4caa3511558b9daa20340833bcce75 to your computer and use it in GitHub Desktop.
// This file reproduces the EIP 155 examples
// values compared to EIP 155 issue text as of 2017-05-03 https://github.com/ethereum/eips/issues/155#issue-183002027
const ethUtil = require('ethereumjs-util') // ethereumjs-util version 5.1.1
const fields = [{
name: 'nonce',
length: 32,
allowLess: true,
default: new Buffer([])
}, {
name: 'gasPrice',
length: 32,
allowLess: true,
default: new Buffer([])
}, {
name: 'gasLimit',
alias: 'gas',
length: 32,
allowLess: true,
default: new Buffer([])
}, {
name: 'to',
allowZero: true,
length: 20,
default: new Buffer([])
}, {
name: 'value',
length: 32,
allowLess: true,
default: new Buffer([])
}, {
name: 'data',
alias: 'input',
allowZero: true,
default: new Buffer([])
}, {
name: 'v',
default: new Buffer([])
}, {
name: 'r',
length: 32,
allowLess: true,
default: new Buffer([])
}, {
name: 's',
length: 32,
allowLess: true,
default: new Buffer([])
}]
const privateKey = Buffer.from('4646464646464646464646464646464646464646464646464646464646464646', 'hex')
/*** reproduce correct example */
const txParams = {
nonce: '0x09',
gasPrice: '0x04A817C800', // 20000000000
gasLimit: '0x5208', // 21000
to: '0x3535353535353535353535353535353535353535',
value: '0xDE0B6B3A7640000', // 10**18
data: '0x',
v: '0x1' // chainId: 1 // EIP 155 chainId
}
var tx = {}
ethUtil.defineProperties(tx, fields, txParams)
console.log('tx fields:', tx.raw)
const signingData = ethUtil.rlp.encode(tx.raw).toString('hex')
console.log('signing data:', signingData)
// ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080
// ^ same as in EIP 155 issue text
const signingHash = ethUtil.sha3('0x' + signingData)
console.log('signing hash:', signingHash.toString('hex'))
// daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53
// ^ correct signing hash
const txSig = ethUtil.ecsign(signingHash, privateKey)
console.log('txSig:', txSig)
/* txSig: { r: <Buffer 28 ef 61 34 0b d9 39 bc 21 95 fe 53 75 67 86 60 03 e1 a1 5d 3c 71 ff 63 e1 59 06 20 aa 63 62 76>,
s: <Buffer 67 cb e9 d8 99 7f 76 1a ec b7 03 30 4b 38 00 cc f5 55 c9 f3 dc 64 21 4b 29 7f b1 96 6a 3b 6d 83>,
v: 27 } */
// txSig.v returns 27 arbitrarily https://github.com/ethereumjs/ethereumjs-util/blob/d03528e7da885539cad141c99ea5b88829f73e72/index.js#L333
// for background see https://bitcoin.stackexchange.com/a/38909
var sigRval = new ethUtil.BN(txSig.r)
console.log('r val:', sigRval.toString(10))
// r val: 18515461264373351373200002665853028612451056578545711640558177340181847433846
// ^ correct R value
var sigSval = new ethUtil.BN(txSig.s)
console.log('s val:', sigSval.toString(10))
// s val: 46948507304638947509940763649030358759909902576025900602547168820602576006531
// ^ correct S value
tx.v = '0x25' // 37 in decimal
tx.r = txSig.r
tx.s = txSig.s
console.log('tx.raw:', tx.raw)
var txSerialized = ethUtil.rlp.encode(tx.raw)
console.log('txSerialized:', txSerialized.toString('hex'))
// txSerialized: f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83
// ^ same as in EIP 155 issue text
/*** reproduce incorrect example */
const txParamsWrong = {
nonce: '0x09',
gasPrice: '0x04A817C800', // 20000000000
gasLimit: '0x5208', // 21000
to: '0x3535353535353535353535353535353535353535',
value: '0xDE0B6B3A7640000', // 10**18
data: '0x',
v: '0x31' // wrong chain id
}
var txWrong = {}
ethUtil.defineProperties(txWrong, fields, txParamsWrong)
const wrongSigningData = ethUtil.rlp.encode(txWrong.raw).toString('hex')
console.log('wrong signing data:', wrongSigningData)
// ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080318080
const wrongHash = ethUtil.sha3('0x' + wrongSigningData)
console.log('wrong signing hash:', wrongHash.toString('hex'))
// 2691916f9e6e5b304f135496c08f632040f02d78e36ae5bbbb38f919730c8fa0
// ^ same as in EIP 155 issue text
const wrongSig = ethUtil.ecsign(wrongHash, privateKey)
console.log('wrongSig:', wrongSig)
/* wrongSig: { r: <Buffer 18 fa 89 79 9b c0 3e d6 ea f9 2d d8 50 2a 69 84 6c a2 72 fb 62 f5 30 6c 3f 01 01 a9 2b ef 38 37>,
s: <Buffer 39 bb c0 91 c7 07 37 4b 76 e6 58 4e 13 88 bc 9c 88 86 bd bf b0 7b 34 53 57 72 f7 af 28 d1 4c 5e>,
v: 27 } */
var wrongRval = new ethUtil.BN(wrongSig.r)
console.log('wrong r val:', wrongRval.toString(10))
// wrong r val: 11298168949998536842419725113857172427648002808790045841403298480749678639159
// ^ same as in EIP 155 issue text
var wrongSval = new ethUtil.BN(wrongSig.s)
console.log('wrong s val:', wrongSval.toString(10))
// wrong s val: 26113561835810707062310182368620287328545641189938585203131842552044123671646
// ^ same as in EIP 155 issue text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment