2

I noticed that it's possible to sign a transaction with a wrong private key. I took the following example found at https://github.com/ethereumjs/ethereumjs-tx

const EthereumTx = require('ethereumjs-tx').Transaction
const privateKey = Buffer.from(
  'e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
  'hex',
)

const txParams = {
  nonce: '0x00',
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: '0x0000000000000000000000000000000000000000',
  value: '0x00',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
}

// The second parameter is not necessary if these values are used
const tx = new EthereumTx(txParams, { chain: 'mainnet', hardfork: 'petersburg' })
tx.sign(privateKey)
const serializedTx = tx.serialize()

This code does sign the transaction, but how do I verify that the transaction has been correctly signed?

How to use or decode serializedTx and verify that the signature is correct?

4
  • Related: ethereum.stackexchange.com/questions/4196/…
    – eth
    Commented Mar 12, 2020 at 6:32
  • @eth: I think you kinda deprecated my answer when you edited the question and changed the How to decode a transaction with wrong private key title. Commented Mar 12, 2020 at 6:40
  • @goodvibration Sometimes people may not ask their question clearly. I think asking this way can help the OP get his answer to how to verify that a signature is correct: a transaction is just bytes and being able to decode it, means getting the signature which can be compared to the signature that the OP gets. I am surprised that so far there doesn't seem to be a way to decode a transaction easily? (Answering a question and editing it to improve the question is a tip I received from a long time user in Stack Exchange sites.)
    – eth
    Commented Mar 17, 2020 at 9:36
  • Not sure if i get the question correctly, you mean you wanna check if the final serializedTx is signed by a private key? Say, check(serializedTx, privKey) -> True/False ?
    – minhhn2910
    Commented Dec 23, 2022 at 7:11

2 Answers 2

0

This code does sign the transaction, but how do I verify that the transaction has been correctly signed?

How is it possible to verify that the signature is correct?

web3 has an estimateGas method you can use to estimate the gas required for the transaction and If signature is incorrect, that means the transaction is going to fail then it will simply just throw an error.

Also is it possible to programmatically get the private key associated to the address that's logged in (with Metamask)?

Because of security considerations, privateKey is obviously not available. It would be a serious security flaw if you could access metamask accounts that way.

1
  • Thank you for your answer. According to the documentation estimateGas doesn't take a signed transaction as argument but a simple transaction.
    – Julien
    Commented Mar 12, 2020 at 22:45
0

Of course you can sign a transaction with any private key that you want.

But when you finally execute it, the gas required for this transaction (as well as the amount of ether that you have set in the value field) will be taken from the account corresponding with that key.

And if you just choose a random key, then this account will have 0 Ether with a probability of almost 100%, which means that the transaction will subsequently fail for insufficient balance.

1
  • Thank you for your answer. It makes sense, but is there a function that can verify that the signature is correct ?
    – Julien
    Commented Mar 12, 2020 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.