区块链-以太坊开发教程02-nodejs开发区块链钱包(带助记词)
⼀、创建⼀个新的项⽬
新建⼀个Express 框架的项⽬
⼆、安装web3
在此附上,web3 在github的地址:
github/ethereum/web3.js/tree/2.x
执⾏安装web3
[root@192 node]# cd jiaocheng #node.js项⽬根⽬录
[root@192 jiaocheng]# npm install web3
三、引⼊现有的库
[root@192 jiaocheng]# npm install bip39 ethereumjs-wallet ethereumjs-util ethers ethereumjs-tx --save
var Web3 = require('web3');
var bip39 = require('bip39');
var hdkey = require('ethereumjs-wallet/hdkey');
var util = require('ethereumjs-util');
var ethers = require('ethers');
var EthereumTx = require('ethereumjs-tx').Transaction;
四、创建带助记词的钱包
if(req.query.fn === "walletNew"){ //创建钱包
//1、⽣成助记词
let mnemonic = ateMnemonic();
//2、将助记词转成seed
let seed = icToSeedSync(mnemonic);
//3、通过hdkey 将seed⽣成HD Wallet
let hdWallet = hdkey.fromMasterSeed(seed);
/
/4、⽣成钱包中在m/44'/60'/0'/0/0路径的keypair
let key = hdWallet.derivePath("m/44'/60'/0'/0/0");
//5、从keypair中获取私钥
let privateKey = util.bufferToHex(key._hdkey._privateKey);
//6、从keypair中获取公钥
let publicKey = util.bufferToHex(key._hdkey._publicKey);
//7、使⽤keypair中的公钥⽣成地址
let address = util.pubToAddress(publicKey,true);
address = String('hex'));
//8、⽣成keystore
let keystore = pt(privateKey,req.query.password);
res.json({
code: 1,
message: 'OK',
data:{
mnemonic:mnemonic,
privateKey:privateKey,
publicKey:publicKey,
address:address,
keystore:keystore,
user_id:req.query.user_id
}});
五、助记词⽅式导⼊钱包
if(pe === "mnemonic") { //助记词⽅式导⼊
//2、将助记词转成seed
let seed = icToSeedSync(ic);
//3、通过hdkey 将seed⽣成HD Wallet
let hdWallet = hdkey.fromMasterSeed(seed);
//4、⽣成钱包中在m/44'/60'/0'/0/0路径的keypair
let key = hdWallet.derivePath("m/44'/60'/0'/0/0");
//5、从keypair中获取私钥
let privateKey = util.bufferToHex(key._hdkey._privateKey);
//6、从keypair中获取公钥
let publicKey = util.bufferToHex(key._hdkey._publicKey);
//7、使⽤keypair中的公钥⽣成地址
let address = util.pubToAddress(publicKey, true);
address = String('hex'));
//8、⽣成keystore
let keystore = pt(privateKey, req.query.password); res.json({
code: 1,
message: 'OK',
data: {
mnemonic: ic,
privateKey: privateKey,
publicKey: publicKey,
address: address,
keystore: keystore,
user_id: req.query.user_id
}
});
六、私钥⽅式导⼊钱包
else if(pe === "privateKey"){ //私钥⽅式导⼊
//获取钱包对象
wallet = new ethers.Wallet(req.query.privateKey);
//⽣成keystore
let keystore = pt(req.query.privateKey, req.query.password); res.json({
code: 1,
message: 'OK',
data: {
privateKey: wallet.signingKey.keyPair.privateKey,
publicKey: wallet.signingKey.keyPairpressedPublicKey,
address: wallet.signingKey.address,
keystore: keystore,
user_id: req.query.user_id
}
});
七、store对象⽅式导⼊钱包
else if(pe === "keystore"){ //store对象⽅式导⼊
wallet = h.accounts.decrypt(req.query.keystore,req.query.password);
wallet = new ethers.Wallet(wallet.privateKey);
res.json({
code: 1,
message: 'OK',
data: {
privateKey: wallet.signingKey.keyPair.privateKey,
publicKey: wallet.signingKey.keyPairpressedPublicKey,
address: wallet.signingKey.address,
keystore: req.query.keystore,
user_id: req.query.user_id
}
});
⼋、获取指定类型的余额
else if(req.query.fn === "getBalance"){ //获取指定类型的余额
if(pe === "ETH"){
res.json({
code: 1,
message: 'OK',
data: {balance: web3.utils.fromWei(balance,'ether')}
});
});
}else if(pe === "SEA"){
let abi = JSON.adFileSync('./routes/seaToken.abi','utf8'));
con = h.Contract(abi,conaddress);
res.json({
code: 1,
message: 'OK',
data: {balance: web3.utils.fromWei(balance,'mwei')}
});
});
九、ETH链上转账操作
if(pe === "ETH"){
//先做交易签名
//先获取当前付款账号的交易数量(nonce)
let privateKey = Buffer.from(req.query.privateKey.substr(2),'hex');
//设置交易数据
let txData = {
// nonce每次++,以免覆盖之前pending中的交易
nonce: Hex(nonce++),
// 设置gasLimit和gasPrice
gasLimit: Hex(req.query.gas),
gasPrice: Hex(Wei(req.query.gasprice,'ether')),
// 要转账的哪个账号
to: req.query.address_to,
// 从哪个账号转
from: req.query.address_from,
// 以太币转账⾦额
value: Hex(Wei(,'ether')),
data: ''
}
const tx = new EthereumTx(txData);
//⽤私钥签署交易
tx.sign(privateKey);
//序列化
const serializedTx = tx.serialize().toString('hex');
//发送已签名交易
if(!error){
res.json({
code: 1,
message: 'OK',
data: {hash: hash}
});
} else{
res.json({
code: -2,
message: error,
data: {}
});
}
});
});
⼗、链上发送交易操作
else if(pe === "SEA"){
//h⽅法
// 补齐64位,不够前⾯⽤0补齐
function addPreZero(num){
var t = (num+'').length,
s = '';
for(var i=0; i<64-t; i++){
s += '0';
}
return s+num;
}
//将私钥,转换为16进制
//let privateKey = Buffer.from(req.query.privateKey.substr(2),'hex'); //扣除⽤户eth
let privateKey = Buffer.from("调⽤者私钥",'hex'); //扣除平台eth
//设置交易数据
let txData = {
// nonce每次++,以免覆盖之前pending中的交易
nonce: Hex(nonce++),
// 设置gasLimit和gasPrice
gasLimit: Hex(req.query.gas),
gasPrice: Hex(Wei(req.query.gasprice,'ether')),
// 这⾥是合约地址
to: conaddress,
// 从哪个账号转
from: req.query.address_from,
// 以太币转账⾦额
value: '0x00',
// data的组成,由:0x + 要调⽤的合约⽅法的function signature + 要传递的⽅法参数,每个参数都为64位(对transfer来说,第⼀个是接收⼈的地址去掉0x,第⼆个是数量的16进制表⽰,去掉前⾯0x,然后补齐为64位)
data: '0xa5cfc11f' + addPreZero(req.query.address_from.substr(2)) +addPreZero(req.query.address_
to.substr(2))+ addPreZero( Hex(Wei(,'mwei')).substr(2))
}
const tx = new EthereumTx(txData);
//⽤私钥签署交易
tx.sign(privateKey);
//序列化
const serializedTx = tx.serialize().toString('hex');
//发送已签名交易
if(!error){
res.json({
code: 1,
message: 'OK',
data: {hash: hash}
});
}else{
res.json({
code: -2,
message: String(),
data: {}
});
}
});
});
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论