首先安裝 NodeJS 和 Npm
npm install -g js4eos
使用的是js4eos , 非 eosio 環境下的cleos
js4eos wallet create 創建一個默認錢包, 記住輸出的密碼
你必需得有一個EOS賬戶, 才能創建新的賬戶
沒有賬戶的可以郵箱聯繫我, 嘿嘿嘿! [email protected]
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();
},
};