Skip to content

add: v,s,a node register automation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export default config;
/* TASKS */
import "./tasks/deployTasks"
import "./tasks/keyTasks"
import "./tasks/registerTasks"
import "./tasks/registerTasks"
import "./tasks/batchRegisterTasks"
36 changes: 36 additions & 0 deletions src/utilz/FileUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {promises as fs} from "fs";
import path from "path";

// thin wrapper, easier to remember node11+ async api
// also double checks for success calls
// only async apis (!)
export class FileUtil {

public static async readFileUtf8(path: string): Promise<string> {
return await fs.readFile(path, { encoding: 'utf8', flag: 'r' }); // note: without 'r' the file might get created!
}

public static async writeFileUtf8(path: string, content: string): Promise<void> {
return await fs.writeFile(path, content);
}

public static async existsDir(path:string):Promise<boolean> {
return await fs.stat(path).then(stat => stat.isDirectory()).catch((e) => false);
}

// this is the best recommended way to check file status in node 12+
// only fs.stat() or fs.access() are not deprecated and both async
public static async existsFile(path: string) {
return await fs.stat(path).then(stat => stat.isFile()).catch((e) => false);
}

public static async mkDir(path: string): Promise<boolean> {
await fs.mkdir(path, {recursive: true});
return this.existsDir(path);
}

public static resolvePath(pathStr: string) : string {
return path.resolve(pathStr);
}

}
29 changes: 29 additions & 0 deletions src/utilz/KeyUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import crypto from "crypto";
import {Wallet} from "ethers";
import {FileUtil} from "./FileUtil";

export class KeyUtil {

static createEthPrivateKey() {
return "0x" + crypto.randomBytes(32).toString("hex");
}

static async createEthPrivateKeyAsJson(pass: string): Promise<{ addr: string, pubKey: string, privKey: string, jsonContent: string }> {
const privKey = KeyUtil.createEthPrivateKey();
const wallet1 = new Wallet(privKey);
let jsonContent = await wallet1.encrypt(pass);
return {addr: wallet1.address, pubKey: wallet1.publicKey, privKey: wallet1.privateKey, jsonContent};
}

static async createEthPrivateKeyAsFile(pass: string, filePath: string): Promise<{ addr: string }> {
let key = await this.createEthPrivateKeyAsJson(pass);
await FileUtil.writeFileUtf8(filePath, key.jsonContent);
return {addr: key.addr};
}

static async readEthPrivateKeyAddress(jsonFilePath: string): Promise<string> {
const jsonData = JSON.parse(await FileUtil.readFileUtf8(jsonFilePath));
return jsonData.address;
}

}
16 changes: 12 additions & 4 deletions src/utilz/envLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ import {NumUtil} from "./numUtil";

export class EnvLoader {

public static loadEnvOrFail() {
public static loadEnv() {
EnvLoader.loadEnvOrFail(false);
}

public static loadEnvOrFail(fail: boolean = false) {
// loads all .env variables into process.env.* variables
// Optional support for CONFIG_DIR variable
console.log(`config dir is ${process.env.CONFIG_DIR}`)
let options = {}
if (process.env.CONFIG_DIR) {
options = { path: `${process.env.CONFIG_DIR}/.env` }
options = {path: `${process.env.CONFIG_DIR}/.env`}
}
const envFound = dotenv.config(options)
const envFound = dotenv.config(options);
if (envFound.error) {
throw new Error("⚠️ Couldn't find .env file ⚠️")
if (fail) {
throw new Error("⚠️ Couldn't find .env file ⚠️")
} else {
console.log("[WARN] no .env file; if you wanted to load a specific .env please specify CONFIG_DIR env variable ");
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions src/utilz/strUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export class StrUtil {

public static isEmpty(s: string): boolean {
if (s == null) {
return true;
}
if (typeof s !== 'string') {
return false;
}
return s.length === 0
}

public static hasSize(s: string, minSize: number | null, maxSize: number | null): boolean {
if (s == null || typeof s !== 'string') {
return false;
}
const length = s.length;
if (minSize !== null && length < minSize) {
return false;
}
if (maxSize !== null && length > maxSize) {
return false;
}
return true;
}

public static isHex(s: string): boolean {
if (StrUtil.isEmpty(s)) {
return false;
}
let pattern = /^[A-F0-9]+$/i;
let result = pattern.test(s);
return result;
}

/**
* Return s if this is not empty, defaultValue otherwise
* @param s
* @param defaultValue
*/
public static getOrDefault(s: string, defaultValue: string) {
return StrUtil.isEmpty(s) ? defaultValue : s;
}

public static toStringDeep(obj: any): string {
return JSON.stringify(obj, null, 4);
}

// https://ethereum.stackexchange.com/questions/2045/is-ethereum-wallet-address-case-sensitive
public static normalizeEthAddress(addr: string): string {
return addr;
}
}
Loading