question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ethers signs my request with a different from address

See original GitHub issue

Hi, I am trying to sign a request with ethers using serialize.

This is my code

const { utils } = require('ethers');
const bn = require('bignumber.js');
const { serializeTransaction, parseEther } = utils;
const tx2 = require('ethereumjs-tx');
async function serialize1() {
        const wei = parseEther('0.02');
        console.log(wei);
        const gasPrice = '0x' + (2000000000).toString(16);
        console.log(gasPrice);
        const tx = {
                gasPrice: gasPrice,
                gasLimit: 21000,
                to: '0x3e245df5a4de41e65cecd1f98b96ca06c3d319f0',
                value: wei,
                nonce: 0,
                data: '',
                chainId: 3,
        };
        const serializedTransaction = await serializeTransaction(tx);
        console.log(serializedTransaction);
        const signature = 'c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff53d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f1b';
        console.log('---------------------------------------------');
        const signedSerialized = await serializeTransaction(tx, Buffer.from(signature, 'hex'));
        console.log(signedSerialized);
}

serialize1();

The signature (in hex) is for the address: 0x518171C334AD3adc1cF990A884D67963201B675F but the function returns 0xf86a808477359400825208943e245df5a4de41e65cecd1f98b96ca06c3d319f087470de4df8200008029a0c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff5a03d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f which when decoded is

{
  "nonce": 0,
  "gasPrice": 2000000000,
  "gasLimit": 21000,
  "to": "0x3e245df5a4de41e65cecd1f98b96ca06c3d319f0",
  "value": 20000000000000000,
  "data": "",
  "from": "0x0d67ffdd9b1d8858582d903bf0093e22b9da682e",
  "r": "c4b71f516c36468022d61b0e22016c6bc69bc2c99666fb7e63e3d04841c82ff5",
  "v": "29",
  "s": "3d5c687ebe43b290d03caa6766e8e957192faa6b360f4102b536c54c3318304f"
}

Here the from address shown is 0x0d67ffdd9b1d8858582d903bf0093e22b9da682e What I want to know is that what I am feeding into the serializeTransaction function is of the correct format or not? Does it need to go through any thing else when getting the final signed transaction? Am I using the function correctly?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
codemaster101commented, Jul 24, 2020

Yo ricmoooooooooooo you’re a legend. I was supposed to pass the keccack of the transaction before sending it to the sign_ecdsa.

1reaction
ricmoocommented, Jul 24, 2020

Yeah, my hunch is that you are not signing what you think you are signing. The ECDSA algorithm can only sign a 256-bit number, so usually data is hashed.

In the case of a transaction, the keccak256 of the unsigned, serialized transaction is hashed, depending on whether EIP-155 is in use or not, will dictate whether it is 6 RLP encoded fields or 9, which includes an encoded chain ID and 2 null data sets.

In the case of a message, it is prefixed with a preamble strong, and then the utf-8 bytes are hashed and this is signed.

So, you need to look up sign_ecdsa and see how it processes bytes. If it expects a digest, it may be truncating for example, which is not what you want to happen. If too short it might be padding. Whatever that method does, I think it is not doing what you need. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Signing a request with a signature gives the wrong from address
You don't sign a request with a signature, you sign a transaction. You cannot generate generic signatures to be associated to transactions ...
Read more >
Signers - ethers
A Signer in ethers is an abstraction of an Ethereum Account, which can be used to sign messages and transactions and send signed...
Read more >
Documentation - Ethers.js
A Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign messages...
Read more >
Transactions | ethereum.org
Transactions are cryptographically signed instructions from accounts. An account will initiate a transaction to update the state of the Ethereum network.
Read more >
How to Verify a Message Signature on Ethereum
With Ethers.js, we will use the provided starter files to create a frontend UI that lets you connect to a MetaMask wallet to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found