6

Is there any way I can tell whether a transaction hash is valid or not?
For eg,

0x2592cf699903e83bfd664aa4e339388fd044fe31643a85037be803a5d162729f is valid transaction Hash whereas 0x62720366ef403c9891e2bfbd5358ee3c8a57b113 is not.

How to test programmatically whether a transaction hash is valid or not using web3?

I wish to have a function like:

isValidTransactionHash(String txHash){
// if transaction hash is valid return true else false
}
Prashant Prabhakar Singh
  • 8,026
  • 5
  • 40
  • 79

3 Answers3

9

This method will only validate if the transaction hash has the right characters in the right length, it does not check the blockchain. Useful for string validation on user inputs.

function validate_txhash(addr)
{
  return /^0x([A-Fa-f0-9]{64})$/.test(addr);
}

This regex was supplied by this answer: https://ethereum.stackexchange.com/a/34286/50301

Jacob Jarick
  • 117
  • 1
  • 5
5

I would try with web3.eth.getTransactionReceipt inside a try-catch block:

try {
  var txR = web3.eth.getTransactionReceipt("0x62720366ef403c9891e2bfbd5358ee3c8a57b11");
  if (txR.blockNumber == undefined)
    throw "transaction receipt not found";
}
catch(e) {
  console.log("invalid tx receipt: " + e);
}
SCBuergel
  • 8,774
  • 7
  • 38
  • 69
  • But getTransactionteceipt is not meant for that. It returns null for any string starting with '0x' followed by any string of length 64. Like 0x1111111111111111111111111111111111111111111111111111111111111111 and 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa all are valid transaction hashes according ti this but actually they are not. – Prashant Prabhakar Singh May 04 '17 at 10:59
  • then simply check for some expected child, such as blockNumber. I added that to the example code. – SCBuergel May 04 '17 at 11:09
  • That's not the way man. See, getTransactionReceipt returns null for both an incorrect transaction Hash as well as the transaction that is not mined yet. So checking this way will declare all transaction Hashes which are valid but not yet mined as invalid.Hope you understand. – Prashant Prabhakar Singh May 04 '17 at 11:31
  • Did you test the code that I updated before posting my previous comment? I did and see it working. Please check again. – SCBuergel May 04 '17 at 11:48
  • I tested that. If you pass a transactionHash that is not yet mined (but transaction hash is valid), the value of txR will be null, and since in next line when you try to access blockNumber of null, this will give an error. and in this case, a valid transaction has is also declared as invalid. Please check the code with a valid but unmined transaction. – Prashant Prabhakar Singh May 04 '17 at 11:56
  • Because hashes are irreversible, there's no way to know if a transaction hash is "valid" with just the hash. This may be the best that one can get. – Matthew Schmidt May 04 '17 at 13:22
  • So you don't want to check if the txHash is valid but if it's known to the node you're connected to but not necessarily mind yet. Very different thing. In that case try .getTransaction and same as above check for some child property. – SCBuergel May 05 '17 at 08:21
  • I guess @MatthewSchmidt is right. We probably can't check if a transaction is valid because hashes are irreversible. So to check if tx hash is valid we could check for the string should start with 0x and followed by 62 characters would be enough. – Prashant Prabhakar Singh May 09 '17 at 19:50
  • This is not the correct answer. The short one is the @Jacob post. And there is also a long one, You can validate with a regex like Jacob suggest, after this you can also check if the transaction is in the blockchain with getTransactionReceipt and you may want to check the mempool for unconfirmed transaction. In this way you have a full picture of the transaction status. – gabrielem Nov 06 '20 at 13:53
1
const search = async hash => {
    const re = /[0-9A-Fa-f]{6}/g;
    // 0x + 64 bytes
    if(hash && hash.length === 66 && re.test(hash)) {
      const item = await contract.methods.getItemByHash(hash).call()

    }
  }
Yilmaz
  • 1,580
  • 10
  • 24