Add ability to create signed transactions
Another way to push TX is to sign it by users's private key (this is opposite to push TX with previously personal.unlockAccount()
).
Sample NodeJs with ethjs-signer code is:
var Web3 = require('web3');
const web3 = new Web3('https://goerli.prylabs.net');
const abi = '[...]';
const address = '0x0'; // user account
const addr = "0x0" // smart contract addr;
const keythereum = require("keythereum");
var datadir = "./";
var keyObject = keythereum.importFromFile(address, datadir);
const privateKey = "0x" + keythereum.recover("password", keyObject).toString('hex');
console.log(privateKey);
//const privateKey = '0x83be0f76d7cd8b24b055cd126d59d58b3d69c333d26a778e17a502d2ce2b6be2';
const contract = new web3.eth.Contract(JSON.parse(abi), addr);
//const methdata = contract.methods.newTable('tab01', '#1;2;3', false).encodeABI();
const methdata = contract.methods.init().encodeABI();
const sign = require('ethjs-signer').sign;
const BN = require('bn.js');
web3.eth.getBalance(address, function(err, result) {
if (err) {
console.log(err);
}
else {
console.log(web3.utils.fromWei(result, "ether") + " ETH")
}
});
web3.eth.getTransactionCount(address, function(err, result) {
if (err) {
console.log(err)
}
else {
console.log("TX count: " + result)
}
});
web3.eth.getTransactionCount(address).then((nonce) => {
web3.eth.sendSignedTransaction(
sign({
from: address,
to: addr,
gas: web3.utils.toHex('6000000'),
gasLimit: web3.utils.toHex('6000000'),
gasPrice: web3.utils.toHex(web3.utils.toWei('1.1', 'gwei')),
//gasPrice: web3.utils.toHex('1100000000'),
value: web3.utils.toHex('0'),
data: methdata,
nonce: nonce,
}, privateKey)
).then((txHash) => {
console.log('Transaction Hash', txHash);
});
});
Actually to implement this feature we need to add to Net::Ethereum:
Edited by Konstantin Narkhov