ethereumjs / ethereumjs-tx

Project is in active development and has been moved to the EthereumJS VM monorepo.
https://github.com/ethereumjs/ethereumjs-vm/tree/master/packages/tx
Mozilla Public License 2.0
779 stars 235 forks source link

ethereumjs-tx generates wrong signature #99

Closed ChandraNakka closed 6 years ago

ChandraNakka commented 6 years ago

I'm using this below example code to prepare transaction signature

const ethTx = require('ethereumjs-tx');
const privateKey = Buffer.from('74d1b4f06f610da2761953996c8f1a6df2540342bbef575bd91fb03004557cc5', 'hex');

const txParams = {
    "nonce": "0x02",
    "gasPrice": "0x06fc23ac00",
    "gasLimit": "0x5208",
    "to": "0x58f9ce2c1af3b540580561259955d66d5853e77a",
    "value": "0x00",
    "data": "0x",
    "chainId": 3
};

const tx = new ethTx(txParams);
tx.sign(privateKey);
console.log(tx.serialize().toString('hex'));

Above code is generating this below signature (wrong signature)

f864028506fc23ac008252089458f9ce2c1af3b540580561259955d66d5853e77a808029a07453af3e32aa4a67575c9d321ed86c082b158d36e5ec7fca7b90ac8845b5948ca00acc0393bf8881127624d366afdcf33e3f276d98b13ea581a2362a63e5c8ddf5

But, MyEtherWallet generates this signature (working signature)

f864028506fc23ac008252089458f9ce2c1af3b540580561259955d66d5853e77a80802aa0fcca5dc42e49f26bb5545291b33eacb932d8f2ee40a1a97be02f6916c7ae4f3aa0318507b28e5e94380a7af2efd057ab11d2dc8c863f7c9ad66477e19b259e066c

This is my configuration:

ethereumjs-tx: 1.3.4
nodejs: v8.10.0
ubuntu: 18.04 64bit
fanatid commented 6 years ago

@ChandraNakka can you paste example how I can generate signature with MEW? How you identified that signature generated by this lib is wrong and MEW is right and working?

const secp256k1 = require('secp256k1')
const privateKey = Buffer.from('74d1b4f06f610da2761953996c8f1a6df2540342bbef575bd91fb03004557cc5', 'hex')
const publicKey = secp256k1.publicKeyCreate(privateKey, false).slice(1)

// ethjs tx
const tx1 = new EthTx('f864028506fc23ac008252089458f9ce2c1af3b540580561259955d66d5853e77a808029a07453af3e32aa4a67575c9d321ed86c082b158d36e5ec7fca7b90ac8845b5948ca00acc0393bf8881127624d366afdcf33e3f276d98b13ea581a2362a63e5c8ddf5')
console.log(tx1.getSenderPublicKey().equals(publicKey))

// mew
const tx2 = new EthTx('f864028506fc23ac008252089458f9ce2c1af3b540580561259955d66d5853e77a80802aa0fcca5dc42e49f26bb5545291b33eacb932d8f2ee40a1a97be02f6916c7ae4f3aa0318507b28e5e94380a7af2efd057ab11d2dc8c863f7c9ad66477e19b259e066c')
console.log(tx2.getSenderPublicKey().equals(publicKey))

Result:

true false

fanatid commented 6 years ago

@ChandraNakka issue related with https://github.com/ethjs/ethjs-util/issues/8 When ethjs-util will be fixed, signatures will be right (I already tested with old libs and all fine). I tested code with ethjs-util@0.1.6 (contain bad code). Probably https://github.com/ethereumjs/ethereumjs-tx/issues/101 should help avoid such situations.