5

My program is relaying raw transactions to a geth node from a mobile client.

I noticed that a transaction with the identical content will yield an identical hash. e.g. calling the same function with the same parameters.

If I send this raw transaction more than once in quick succession (before there was time to mine it) I get the known transaction error.

How can I check if my transaction is known? Or put it another way: How can I compute the transaction hash from the raw transaction string?

In the code below, I am looking for a solution to someWayToComputeHashFrom

This is how I create my transaction string with eth-lightwallet. The contract object is a web3 contract.

getCertificateContract().then((contract) => {
            let payloadData = undefined;
            let functionName = undefined;
            functionName = 'registerAuditReport';
            payloadData = contract.registerAuditReport.getData(result.hash, documentId);

            let gasEstimate = w3.toHex(w3.eth.estimateGas({
                    to: contract.address,
                    data: payloadData
                }) + 10000);

            let nonce = w3.toHex(w3.eth.getTransactionCount(profile.address) + 10000);

            var rawTx = {
                nonce: nonce,
                gasPrice: gasPrice,
                gasLimit: gasEstimate,
                to: contract.address,
                from: profile.address,
                value: '0x00',
                data: payloadData
            };

            let rawTxString = txutils.functionTx(contract.abi, functionName, [result.hash, documentId], rawTx);

            let signedTxString = signing.signTx(wallet.keystore, wallet.pwDerivedKey, rawTxString, '0x' + senderAddress);


            let txHash = someWayToComputeHashFrom('0x' + rawTxHexString);
            w3.eth.sendRawTransaction('0x' + rawTxHexString, function (err, hash) {
                console.log('transaction hash is', hash);
            })
        })
5

the answer is web3.sha3(rawTxHexString, {encoding: 'hex'}) will return the transaction hash. The rawTxHexString is without the leading 0x

3

if your problem is the Known Transaction Error originating from the same use of nonce from getTransactionCount, you can use the transaction count with the pending transaction count for your nonce.

This one will give you the transaction count with the pending ones!

web3.eth.getTransactionCount(fromAddress, 'pending')
  • Excellent Thank you! – Russo Jul 8 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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