How to do it...

We will now learn how we can send raw transactions:

  1. You need to sign your transaction before broadcasting it to an Ethereum network. If you are using the existing version of web3.js (0.2x.x), this operation is not built in. Install ethereumjs-tx via npm to sign transactions easily:
$ npm install ethereumjs-tx --save
  1. Once it is installed, import it into your project and create the transaction object to sign:
// For web3.js 0.2x.x
var
Tx = require("ethereumjs-tx");

var
rawTx = { nonce: "0x00", gasPrice: "0x09184e72a000", gasLimit: "0x2710", to: "0x0000000000000000000000000000000000000000", value: "0x00", data: "0x3f9263449875000000000000000000000000000000000000000000000000000000000009" }

var tx = new Tx(rawTx);

Use the following parameters to build the transaction object:

  • nonce (optional): The nonce to use while signing the transaction. The default is web3.eth.getTransactionCount().
  • to (optional): The address of the transaction receiver. Can be empty when deploying a contract.
  • data (optional): The call data for this transaction. Can be empty for value transfers.
  • value (optional): The amount to transfer in Wei.
  • gasPrice (optional): The gas price set by this transaction. The default is web3.eth.gasPrice().
  • gas: The gas provided by the transaction.
  1. Sign the transaction object using your private key:
// Import your private key as a buffer
var
privateKey = new Buffer('<privateKey>', 'hex');
// Sign the transaction with the private key
tx.sign(privateKey);
// Serialize the transaction
var serializedTx = tx.serialize();
  1. Finally, broadcast the transaction to the network using sendRawTransaction:
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), 
function(err, hash) { if (!err) console.log(hash); });
  1. This functionality is available out of the box starting from web3.js v1.x.x. Use the signTransaction method to sign a transaction using the private key:
web3.eth.accounts.signTransaction({
  nonce: "0x00",
  gasPrice: "0x09184e72a000", 
  gasLimit: "0x2710",
  to: "0x0000000000000000000000000000000000000000", 
  value: "0x00", 
  data: "0x3f9263449875000000000000000000000000000000000000000000000000000000000009"
}, '<privateKey>')
.then(function(result) {
console.log(result);
}
);

This method returns an object with the following values:

  • messageHash: The hash of the given message
  • r: First 32 bytes of the signature
  • s: Next 32 bytes of the signature
  • v: Recovery value +27
  • rawTransaction: The RLP encoded transaction, which is broadcast to the network
  1. Use the web3.eth.sendSignedTransaction method to send the rawTransaction value:
web3.eth.sendSignedTransaction("<rawTransaction>")
.on('receipt', function(receipt) {
console.log(receipt);
});
  1. To sign data from a specific account without using a private key, use the eth.sign method. Make sure that you have unlocked the account before signing the transaction:
// For web3.js 0.2x.x
web3.eth.sign(address, dataToSign, [, callback])

var result = web3.eth.sign(
"0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
web3.sha3("Hello World!")
)
console.log(result);

// For web3.js 1.x.x
web3.eth.sign(dataToSign, address [, callback])

web3.eth.sign(
"Hello world",
"0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe"
).then(function (result) {
console.log(result);
}
);
  1. web3.js v1.x.x supports signing arbitrary data using your private key. This can be done with the eth.accounts.sign method. This returns an object with the message, messageHashr, sand v values:
web3.eth.accounts.sign("Hello World", "<privateKey>");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.167.183