Transaction signature is the only way to prove your identity to blockchain, which is also a basic concept to be clarified when using Ethereum's Web3 development library. In this tutorial, we will learn how to use Web3.js to complete the signing and submission of Ethereum smart contract call transactions, which is applicable to all Ethereum contracts including ERC20 token contracts.
Learn the DApp development of Ethereum in a familiar language: Java | Php | Python | .Net / C# | Golang | Node.JS | Flutter / Dart
1. Ethereum transaction signature overview
There are two ways to sign a transaction: using an unlocked account or using a private key.
If you are developing with testRPC or Ganache, you should already know the test account it creates. These accounts are unlocked by default, so you can sign transactions directly with them. You can also use a special Web3 provider such as the truffle hdwallet provider to generate an unlocked account.
More commonly, the transaction is signed with the private key corresponding to the address where the transaction was initiated. With security in mind, you need to be very careful when using private keys.
2. Create Ethereum smart contract call transaction
First you need to build a trading partner that calls the contract method:
// 12 word mnemonic for HD Wallet Provider // You can use any provider such as the HttpProvider if you are // signing with private key const mnemonic = "opinion destroy betray ..."; const provider = new HDWalletProvider(mnemonic,"http://localhost:8545"); const web3 = new Web3(provider); const myContract = new web3.eth.Contract(contractAbi,contractAddress); const tx = { // this could be provider.addresses[0] if it exists from: fromAddress, // target address, this could be a smart contract address to: toAddress, // optional if you want to specify the gas limit gas: gasLimit, // optional if you are invoking say a payable function value: value, // this encodes the ABI of the method and the arguements data: myContract.methods.myMethod(arg, arg2).encodeABI() };
How to sign next depends on whether you use the unlock account or the private key.
3. Sign the Ethereum transaction with the unlocked account
If you use the unlocked account, use the following code to sign:
const signPromise = web3.eth.signTransaction(tx, tx.from);
Note that if you use the unlock account, call myContract.methods.myMethod(arg, arg2) can automatically complete the signature. The above code is just to demonstrate the signing process.
4. Signing Ethereum transaction with private key
However, if this is a locked account or not a node managed account at all, you can use the private key to sign the transaction:
const signPromise = web3.eth.accounts.signTransaction(tx, privateKey);
In both cases, a Promise object is returned, and its resolution result is the naked transaction string after signature.
5. Broadcast signed naked transaction string
Since all information about contract call has been encoded in the signed naked transaction string, including method name, call parameter, gas price, etc., it can be directly submitted to the Ethereum node for broadcast:
signPromise.then((signedTx) => { // raw transaction string may be available in .raw or // .rawTransaction depending on which signTransaction // function was called const sentTx = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction); sentTx.on("receipt", receipt => { // do something when receipt comes back }); sentTx.on("error", err => { // do something on transaction error }); }).catch((err) => { // do something when promise fails });
Again, the private key is used in the above operations, so you must be very careful about security issues.
Original link: Ethereum contract call naked transaction signing tutorial - huizhi.com