mirror of
https://github.com/zhigang1992/session-key-aspect.git
synced 2026-01-12 22:52:16 +08:00
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
|
|
"use strict"
|
|
|
|
// import required libs
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const Web3 = require("@artela/web3");
|
|
var argv = require('yargs')
|
|
.string('pkfile')
|
|
.argv;
|
|
|
|
|
|
async function create() {
|
|
|
|
const configJson = JSON.parse(fs.readFileSync('./project.config.json', "utf-8").toString());
|
|
// init connection to Artela node
|
|
let node = (argv.node)?String(argv.node):configJson.node;
|
|
if(!node){
|
|
console.log("'node' cannot be empty, please set by the parameter or artela.config.json")
|
|
process.exit(0)
|
|
}
|
|
const web3 = new Web3(node);
|
|
|
|
let privateFile = 'privateKey.txt'; // <-- your private key here, if not exist, create new one
|
|
if(argv.pkfile){
|
|
privateFile=argv.pkfile;
|
|
}
|
|
let account;
|
|
if (fs.existsSync(privateFile)) {
|
|
let pk = fs.readFileSync(privateFile, 'utf-8');
|
|
account = web3.atl.accounts.privateKeyToAccount(pk.trim());
|
|
} else {
|
|
account = web3.atl.accounts.create();
|
|
const dirPath = path.dirname(privateFile);
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
fs.writeFileSync(privateFile, account.privateKey);
|
|
}
|
|
|
|
// add account to wallet
|
|
web3.atl.accounts.wallet.add(account.privateKey);
|
|
console.log("address: ", account.address);
|
|
}
|
|
|
|
create().then();
|