0

I have a function here that is supposed to sign a transaction and broadcast it after. With ethereumjs-tx you store the private key client-side injecting it into a buffer.

Is it possible to let a client side user (i.e Metamask user) sign this client side and then broadcast the tx?

    web3.eth.getTransactionCount(fromAddress).then(async txCount => {
        const txData = {
            nonce: web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(100000),
            gasPrice: web3.utils.toHex(45),
            to: destinationAddres,
            from: "fromAddress",
            value: web3.utils.toHex(
                web3.utils.toWei("0.01", "ether")
            )
        };

        const transaction = new ethereumjs.Tx(txData, { chain: "mainnet" });
        const serializedTx = "0x" + transaction.serialize().toString("hex");
        const signature = await web3.eth.sign(web3.utils.sha3(serializedTx), fromAddress)
        var self = this;

        web3.eth
            .sendSignedTransaction(serializedTx)
            .on("transactionHash", function(txHash) {
            })
            .on("receipt", function(receipt) {
                console.log("receipt:" + receipt);
            })
            .on("confirmation", function(confirmationNumber, receipt) {
                if (confirmationNumber >= 1) {
                }
            })
            .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.