Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Invalid signature with web3.eth.accounts.signTransaction on testrpc #1169

Closed
tensor5 opened this issue Nov 13, 2017 · 3 comments
Closed

Invalid signature with web3.eth.accounts.signTransaction on testrpc #1169

tensor5 opened this issue Nov 13, 2017 · 3 comments
Assignees
Labels
Bug Addressing a bug In Progress Currently being worked on

Comments

@tensor5
Copy link

tensor5 commented Nov 13, 2017

I'm using [email protected] to replicate the example http://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#id6 on testrpc:

web3.eth.accounts.signTransaction({to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    value: '1000000000',
    gas: 2000000
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318').then(console.log);

the output I get is:

{ messageHash: '0x9ce21594b026a4c3fc09dff6e152f69b9beb44bd31549b49c8124b3db831fc27',
  v: '0x.82bf6b784',
  r: 
   [ '0x0aaf3477fff968b4aa173d94f4e0d74c6a9ffdc1985febea5ad447f52c45c3196a03d1bcc7efbc9902c5' ],
  s: '0x3d',
  rawTransaction: '0xf96f.8808504a817c800831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008085.82bf6b784e7aa0aaf3477fff968b4aa173d94f4e0d74c6a9ffdc1985febea5ad447f52c45c3196a03d1bcc7efbc9902c53d4406c6b69c2c6d23b5ac4c29a68ea63a342433bde32a7' }

which is not a valid signature. If I manually specify the mainnet chainId then I don't have this problem:

web3.eth.accounts.signTransaction({
    to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    value: '1000000000',
    gas: 2000000,
    chainId: 0
}, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318').then(console.log);
{ messageHash: '0x6ef4e89eab7867ddf69fae20dd218dbfa337fa8e4d68ce6dc3fcad63e17fdd72',
  v: '0x24',
  r: '0x1b428275439f5d12a85dd70e919b17994abe4d3b2ba3cfd1e370c1a1c86d2a24',
  s: '0x4eebdddb5681d836fb938179d0002b12359c9b2287a8d6ffb156c15de9146251',
  rawTransaction: '0xf869808504a817c800831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008024a01b428275439f5d12a85dd70e919b17994abe4d3b2ba3cfd1e370c1a1c86d2a24a04eebdddb5681d836fb938179d0002b12359c9b2287a8d6ffb156c15de9146251' }
@mirek
Copy link

mirek commented Jan 29, 2018

It looks like rlp is broken for signing transaction when chain_id * 2 + 36 > 255 (overflows 1 byte), the workaround for now seems to be using chain_id in 0-109 range. For details you can have a look at #1334

@CryptoKiddies
Copy link

I wonder if this is the same issue here #1674 that's affecting sendTransaction under the hood when I use a local wallet

@nivida nivida self-assigned this Aug 9, 2018
@nivida nivida added the Bug Addressing a bug label Aug 9, 2018
@nivida nivida added the In Progress Currently being worked on label Aug 20, 2018
@captPython
Copy link

HI everyone,

I am trying to sign transaction using web3.eth.accounts.signTransaction .. tried using Gananche, Local Geth in Light sync mode and Infura ..

when I used Infura and geth with ChainId = 3 that time value of v in signature was 41 or 42 but when used ganache which was runing on chain id 4447 then the value of v was very high like 1228 or above ..

does the bug still exist ..

// Sample Code written
const Web3 = require('web3')
// Or When tried using Ganache
const web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:7545'));

// Or When tried using GETH
const web3 = new Web3(new Web3.providers.HttpProvider('localhost:8545'));

// Or using infura
const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/cxxxxxxxxxx72'));

async function startApp() {
try {
let accounts = await web3.eth.getAccounts();
const sender = accounts[0];
const escrow = accounts[2];

   // Added 0x to private key 
    const privateKey = '0x''201E7B612xxxxxxxxxxx05876E9609D17818';

    // const chainId = await web3.eth.net.getId()

    let amount = '10000000';
    let tx = {
        nonce: web3.utils.toHex(web3.eth.getTransactionCount(sender)),
        chainId: "0x3",
        gas: "0x1e8480",
        gasPrice: web3.utils.toHex(web3.eth.getGasPrice()),
        to: escrow,
        value: "0x989680"
    }

    const signature = await web3.eth.accounts.signTransaction(tx, privateKey);
    console.log(signature);

    // Verify if the sender addres is recovered correctly
    let recover = web3.eth.accounts.recoverTransaction(signature.rawTransaction);

    const v = web3.utils.toDecimal(signature.v);           /// Vlaue is coming 41
    const r = Buffer.from(signature.r);
    const s = Buffer.from(signature.s);
    const mHash = Buffer.from(signature.messageHash);

    const rsv = await ethUtil.ecrecover(mHash, v, r, s);

    console.log(rsv);

} catch (error) {
    console.error(error);
}

}

startApp();

Any leads how should I proceed to get correct value? is this problem exist in other function calls as well like
### web3.eth.sign
web3.eth.signTransaction

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Addressing a bug In Progress Currently being worked on
Projects
None yet
Development

No branches or pull requests

5 participants