1

I only have the source address, the destination address and the amount of money to send.

Can I create offline-signed raw transaction data from this information?

Also, I want to do something like that with Nodejs, JavaScript.

2 Answers 2

1

yes, in web3.js, you can do it using these packages:

Here is an example using web3.js:

const Web3 = require("web3");
const Tx = require('ethereumjs-tx').Transaction;
const Common = require('ethereumjs-common').default;

const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function sendEth(value) {

    // Define the sender and receiver addresses, and the private key
    const sender = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
    const receiver = '0xe9ba4b4d84d7a3c80245514213b88d50ed937114';
    const privateKey = Buffer.from('1111111111111111111111111111111111111111111111111111111111111111', 'hex');

    // Define the gas limit
    const gasLimit = await web3.eth.estimateGas({
        from: sender,
        to: receiver,
    });

    // Get the transaction count for the sender address
    const nonce = await web3.eth.getTransactionCount(sender);

    // Define the transaction object
    const transactionObject = {
        to: receiver,
        gasPrice: web3.utils.toHex(web3.utils.toWei('50', 'gwei')),
        gasLimit: web3.utils.toHex(gasLimit),
        nonce: web3.utils.toHex(nonce),
        value: web3.utils.toHex(web3.utils.toWei(value, 'ether')),
    };

    // Define the chain configuration
    const common = Common.forCustomChain(
      'mainnet', {
          name: 'sepolia',
          networkId: 11155111,
          chainId: 11155111,
      },
      'petersburg',
  );

    // Create a new transaction object to sign 
    const tx = new Tx(transactionObject, {
        common
    });

    // Sign the transaction using the private key  
    const signedTx = tx.sign(privateKey);

    // Serialize the signed transaction and send it to the blockchain 
    const serializedTx = tx.serialize();
    const rawTransaction = '0x' + serializedTx.toString('hex');
    console.log(`Raw transaction: ${rawTransaction}\n`)
}

sendEth("2");

You will need to edit CHAINSTACK_NODE_URL with your own RPC endpoint and the addresses and private key.

When you run it, it will log the serialized signed transaction. You only need to send it using sendSignedTransaction.

Raw transaction: 0xf86f20850ba43b740082520894f25dadf841518a2cb516307876ce44f416661085881bc16d674ec800008083027125a0d912cc7da45d4b1244bdf93fb7ed48d997ff279809e2edcadbf8404ae92b9687a01e1f18a934e1fabed7fc010f9a56888fa1f5d0e9b9f0a6330348f34ff40d8c19

You can find the complete example and explanation in the Chainstack docs | eth_sendRawTransaction

2
  • Thank you for explaining so politely! I have general knowledge of Nodejs, but absolutely no knowledge of BlockChain or ETH. How do I get Web3js to work? I searched, but Geth and others are too difficult for me to understand.
    – scooby
    Commented Apr 3, 2023 at 23:43
  • No problem! Yes, you need Nodejs and the web3.js library. Then you just create a new js file and paste the code in it. Here you can find the steps needed: docs.chainstack.com/reference/web3-libraries You also need an RPC endpoint to place in const NODE_URL, you can get one for free at Chainstack if you need one :) docs.chainstack.com/reference/ethereum-getting-started Commented Apr 4, 2023 at 11:09
0

Yes you can. Refer these for more info.
https://ethereum.org/en/developers/docs/transactions/
https://goethereumbook.org/transaction-raw-create/

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.