我试着写保存私钥和体征交易服务器。我用ethereumjs钱包/ hdkey生成账户和私有密钥,ethereumjs-TX签署交易,并用Httprovider发送交易web3js。
不幸的是,当我尝试发送交易,我总是得到错误信息“超出块限气”(即使我我gasLimit设置为21000,远低于我的伽纳彻-CLI实例的块气体限制)。
我怀疑原始编码的交易被错误地形成。
任何想法实际的问题是什么,如何解决呢?
干杯
const hdkey = require('ethereumjs-wallet/hdkey');
const Transaction = require('ethereumjs-tx');
const walletHdpath = "m/44'/60'/0'/0/";
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(process.env.KEYSTORE_SEED));
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
async function generateAccount() {
const wallet = hdwallet.derivePath(walletHdpath + nextAccountIndex).getWallet();
nextAccountIndex += 1;
const addr = '0x' + wallet.getAddress().toString('hex');
accounts[addr] = wallet;
await fundAccount(addr);
return addr;
}
async function fundAccount(address) {
const txParams = {
gasPrice: '20000000000',
gasLimit: '21000',
from: process.env.KEYSTORE_ADDRESS_0,
to: address,
value: web3.utils.toWei('0.1', 'ether'),
data: ''
}
const signed = signTransaction(txParams);
// this line throws exception: "exceeds block gas limit"
await web3.eth.sendSignedTransaction(signed.signed_transaction);
}
function signTransaction(txParams) {
const from = txParams.from.toLowerCase();
const wallet = accounts[from];
if (wallet === undefined) {
return {sucess: false, message: "unknown from account" }
}
const tx = new Transaction(txParams);
const pkey = wallet.getPrivateKey();
tx.sign(pkey);
const rawTx = '0x' + tx.serialize().toString('hex');
return { success: true, signed_transaction: rawTx }
}
问题是,在txParams的值需要十六进制编码,并以0x前缀