Web3.js: how I can get the transaction hash before web3.eth.sendSignedTransaction

Created on 15 Jan 2019  路  6Comments  路  Source: ChainSafe/web3.js

        let tx = new Tx(rawTransaction);
        tx.sign(privateKey);
        let serializedTx = '0x' + tx.serialize().toString('hex');
        // I need to get the transaction hash here
        let sender = web3.eth.sendSignedTransaction(serializedTx).once('transactionHash',  async hash => {
            console.log(hash)              
        })

Most helpful comment

@dk-lan This will get you the txHash of your transaction before sending it.
const txHash = await web3.utils.sha3(serializedTx);

All 6 comments

You might want to try this: https://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#signtransaction

tq for you reply, but I only have privateKey, signtransaction must need password for account.

Managing plaintext private keys is a bad idea, but if you must, then try with https://web3js.readthedocs.io/en/1.0/web3-eth.html#signtransaction.

@dk-lan This will get you the txHash of your transaction before sending it.
const txHash = await web3.utils.sha3(serializedTx);

@dev1644
thanks , the way is correctly

@dev1644, hi I've tried wtih online hashing such as https://emn178.github.io/online-tools/keccak_256.html but the result is different. Maybe web3 has modified few input data or something? Thanks for your help so much bro.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xpepermint picture xpepermint  路  3Comments

connectdotz picture connectdotz  路  3Comments

sundbry picture sundbry  路  3Comments

praveengupta0895 picture praveengupta0895  路  3Comments

mishell-trickster picture mishell-trickster  路  3Comments