使用bip39通过助记词生成钱包
/**
* bip39 and wallet
*/
const bip39 = require('bip39');
const HDKey = require('ethereumjs-wallet/hdkey');
/**
* 派生子账户
* @param id
*/
const getChildKey = (id) => {
const seed = bip39.mnemonicToSeed(mainMne);
const masterKey = HDKey.fromMasterSeed(seed);
return masterKey.derivePath("m/44'/60'/0'/0/" + id);
};
/**
* 获取eth地址
* @param id
* @returns {*}
*/
const getAddress = (id) => {
const childKey = getChildKey(id);
return childKey.getWallet().getAddressString();
};
/**
* 获取eth私钥
* @param id
* @returns {*}
*/
const getPrivateKey = (id) => {
const childKey = getChildKey(id);
return childKey.getWallet().getPrivateKeyString();
};
使用web3.js进行离线签名转账
/**
* service层
*/
const userService = require('../service/users');
/**
* web3 and contract
*/
const web3 = require('./web3Util');
const contract = require('../contracts/contract');
//token合约地址
const contractAddress = require('../contracts/address/index');
const EthereumTx = require('ethereumjs-tx');
/**
* 进行离线签名交易
* @param uid
* @param desAddress
* @param value eth数量
* @param tokenValue token数量
* @returns {Promise<void>}
*/
const sendEthTransaction = async (uid, desAddress, value, tokenValue) => {
//获取数据库对象
const findUser = await userService.findById(uid);
//获取私钥
const privateKeyString = decodePrivateKey(findUser.ethPrivateKey).substring(2);
const privateKey = Buffer.from(privateKeyString, 'hex');
//iValue 转账金额
let dataAbi = "";
let iValue;
let toAddress;
let gasLimit;
if(tokenValue !== 0 && tokenValue !=="0") {
//获取转账数据abi
dataAbi = contract.methods.transfer(desAddress, tokenValue.toString()).encodeABI();
logger.debug("data:" + dataAbi);
iValue = 0;
toAddress = contractAddress;
gasLimit = await contract.methods.transfer(desAddress, tokenValue.toString()).estimateGas({from: findUser.ethAddress});
} else {
iValue = parseInt(value);
toAddress = desAddress;
gasLimit = 21000;
}
//gas
const gasPrice = await web3.eth.getGasPrice();
logger.debug("gasLimit: " + gasLimit);
//nonce
let nonce = await web3.eth.getTransactionCount(findUser.ethAddress, "pending");
logger.debug("nonce: " + nonce);
const txParams = {
nonce: toHex(nonce),
gasPrice: toHex(gasPrice * gasPriceFactor),
gasLimit: toHex(gasLimit),
to: toAddress,
value: toHex(iValue),
data: dataAbi,
chainId: chainId
};
const tx = new EthereumTx(txParams);
tx.sign(privateKey);
const serializedTx = tx.serialize();
const block = await web3.eth.sendSignedTransaction(toHex(serializedTx));
logger.debug(block.transactionHash);
return block;
};
tips: sendEthTransaction方法将eth转账和token转账合并,转账eth时将tokenValue赋值为0,转账token时将value赋值为0即可