0

I am trying to let an user sign a transaction and broadcast that at a later moment with the client-side. I am aware it is unsafe but it is for internal use only.

I currently have this code and get the following errors

1: I use signature to broadcast the transaction and receive error

RPC Error: transaction could not be decoded: could not decode RLP components: extra data at end

2: I use serializedTx to broadcast the transaction and receive error

RPC Error: transaction could not be decoded: unsigned transactions not supported

    web3.eth.getTransactionCount(fromAddress).then(async txCount => {
        const txData = {
            nonce: web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(100000),
            gasPrice: '0x09184e72a000',
            to: destinationAddres,
            value: web3.utils.toHex(
                web3.utils.toWei("0.01", "ether") // amount you want to send
            )
        }

        const transaction = new ethereumjs.Tx(txData, { chain: "mainnet" });
        console.log(transaction)

        const serializedTx = transaction.serialize().toString("hex");
        console.log(serializedTx);

        const signature = await ethereum.request({
            method: 'eth_sign',
            params: ['fromAddress', web3.utils.sha3(serializedTx)]
        });
        console.log(signature);

        web3.eth
            .sendSignedTransaction('0x' + serializedTx)
            .on("transactionHash", function(txHash) {
                // show tx hash ?
            })
            .on("receipt", function(receipt) {
                console.log("receipt:" + receipt);
            })
            .on("confirmation", function(confirmationNumber, receipt) {
                if (confirmationNumber >= 1) {
                    // message that tx went ok
                }
            })
            .on("error", function(error) {
                console.log("error sending ETH", error);
            });
    });

0

Your Answer

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

Browse other questions tagged or ask your own question.