首先安装 NodeJS 和 Npm
npm install -g js4eos
使用的是js4eos , 非 eosio 环境下的cleos
js4eos wallet create 创建一个默认钱包, 记住输出的密码
你必需得有一个EOS账户, 才能创建新的账户
var mysql = require('mysql');
var schedule = require('node-schedule');
const { exec } = require('child_process');
module.exports = {
topqueue : {type:2,updateTime:'',list:[]},
pool : {},
wallerPas : 'debada4ba0b6d1072c**************', //js4eos 钱包密码
creator : 'wangkes*****', // 创号账户
creatorPublicKey : '*****************', // 创号账户公钥
connectMysql : function(){
this.pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'root',
port: '3306',
database: 'block'
});
},
newAccount : function(){
var cmd = 'js4eos wallet unlock --password '+this.wallerPas;
exec(cmd, (err1, unlockRes, stderr1) => {
if (unlockRes) {
cmd = 'js4eos create key';
exec(cmd, (err2, secretKey, stderr2) => {
if (secretKey) {
secretKey = JSON.parse(secretKey);
var privateKey = secretKey['privateKey']; // 私钥
var publicKey = secretKey['publicKey']; // 公钥
cmd = 'js4eos wallet import '+privateKey; // 导入钱包
exec(cmd, (err3, importRes, stderr3) => {
var accountName = this.randomWord(true,12,12); //随机产生12的账户名
cmd = 'js4eos system newaccount '+this.creator+' '+accountName+' '+this.creatorPublicKey+' '+publicKey+' --stake-net "0.0001 EOS" --stake-cpu "0.0001 EOS" --buy-ram-kbytes 8 -p '+this.creator; //创建账户, 预计花费0.5个 EOS
exec(cmd, (err4, Result, stderr4) => {
if(Result.indexOf("executed") != -1 ){
// 这里我做了保存数据库操作, 到这一步账户已经创建完毕, 不想保存到数据库的, 打印公私钥, 和账户名称就可以了
this.pool.getConnection(function(err, conn){
conn.query('INSERT INTO account(account,privateKey,publicKey,createTime,) VALUES(?,?,?,?)', [accountName,privateKey,publicKey,new Date().getTime()],(err,result)=>{conn.release();callBack(result)})
});
}
});
});
}
});
}
});
},
randomWord : function(randomFlag, min, max){
var str = "",
range = min,
arr = ['0', '1', '2', '3', '4', '5', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
// 随机产生
if(randomFlag){
range = Math.round(Math.random() * (max-min)) + min;
}
for(var i=0; i<range; i++){
pos = Math.round(Math.random() * (arr.length-1));
str += arr[pos];
}
return str;
},
startNewAccount_Job : function () {
// 可以定时创建账户
/*schedule.scheduleJob('0 0 0 1/1 * ? *', function(){
newAccount();
console.log('execute newAccount:' + new Date());
});*/
this.newAccount();
},
};