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.
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
Yes you can. Refer these for more info.
https://ethereum.org/en/developers/docs/transactions/
https://goethereumbook.org/transaction-raw-create/