0

For educational purposes, I would like to manually create and publish a transaction. I want to see what's needed for the transaction and how everything is done.

With my current code, using Etherscan's transaction broadcaster works, it gets accepted at first, but the transaction always gets thrown out of the pending transactions.

I'm using ethereumjs/tx and web3 in the following way, mostly copied from ethereumjs/tx with a fresh account with some Ether on it (which is why the nonce is 0):

const address = '0x078bB609037E36D43800E1C7531Dbd4f8f1fDd12'

const txParams = {
    nonce: web3.utils.toHex(0),
    gasPrice: web3.utils.toHex(2),
    gasLimit: web3.utils.toHex(500000),
    to: '0x0000000000000000000000000000000000000000',
    value: '0x00',
    data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
}

const common = new Common({ chain: 'rinkeby' })
const tx = Transaction.fromTxData(txParams, { common })

const signedTx = tx.sign(privateKey)

const serializedTx = signedTx.serialize()

console.log(serializedTx.toString("hex"))

After which I manually publish the serialized transaction using Etherscan.

Can someone point out where I'm going wrong?

2 Answers 2

0

There is a "replay attack" paramaters that you need to add. You need to add "chainId" because some node reject transactions if you don't specify what chain you want to spend your ETH.

https://chainid.network/

2
  • I just added chainId: web3.utils.toHex(4) to txParams but the signed serialized transaction did not even change. I think the line const common = new Common({ chain: 'rinkeby' }) already takes care of that.
    – lreinink
    Commented Jul 9, 2021 at 21:39
  • Be aware that some libraries didn't implant replay attack so it will be ignored when you sign your transaction. And this transaction can be reject by some block explorer.
    – GrindCode
    Commented Jul 11, 2021 at 18:35
0

I managed to solve it by using Web3.js, with the following code:

web3.eth.accounts.signTransaction({
        to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
        value: '1',
        gas: 2000000,
        gasPrice: 1000000000,
        nonce: 3,
        chainId: 4
    }, privateKey)
        .then(console.log);

Where privateKey is just a plain old hexadecimal private key (prefixed by 0x.

Take care to use the right chainId and nonce when creating the transaction this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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