2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const os = require("os");
|
|
|
|
const resolve = require("path").resolve;
|
|
|
|
|
|
|
|
const AES = require("aes-js");
|
|
|
|
const scrypt = require("scrypt-js");
|
|
|
|
|
2019-08-25 16:20:34 -04:00
|
|
|
const { prompt } = require("../packages/cli");
|
2019-05-14 18:25:46 -04:00
|
|
|
const randomBytes = require("../packages/random").randomBytes;
|
|
|
|
const computeHmac = require("../packages/sha2").computeHmac;
|
|
|
|
|
|
|
|
const colorify = require("./log").colorify;
|
|
|
|
|
|
|
|
function getScrypt(message, password, salt) {
|
|
|
|
let progressBar = prompt.getProgressBar(message);
|
2019-11-23 21:38:13 +09:00
|
|
|
return scrypt.scrypt(Buffer.from(password), Buffer.from(salt), (1 << 17), 8, 1, 64, progressBar);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
function Config(filename) {
|
|
|
|
this.salt = null;
|
|
|
|
this.dkey = null;
|
|
|
|
this.values = { };
|
2020-06-12 19:22:16 -04:00
|
|
|
this.canary = "";
|
2019-09-20 22:38:03 -04:00
|
|
|
this.filename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.prototype.load = async function() {
|
|
|
|
if (this.dkey) { return; }
|
|
|
|
|
|
|
|
let data = null;
|
|
|
|
if (fs.existsSync(this.filename)) {
|
|
|
|
data = JSON.parse(fs.readFileSync(this.filename));
|
|
|
|
} else {
|
|
|
|
data = {
|
|
|
|
salt: Buffer.from(randomBytes(32)).toString("hex")
|
|
|
|
};
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 19:22:16 -04:00
|
|
|
this.canary = data.canary || "";
|
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
this.salt = data.salt;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
const password = await prompt.getPassword(colorify("Password (config-store): ", "bold"));
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
this.dkey = await getScrypt(colorify("Unlocking config", "bold"), password, this.salt);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
if (data.ciphertext) {
|
|
|
|
const ciphertext = Buffer.from(data.ciphertext, "base64");
|
|
|
|
const iv = Buffer.from(data.iv, "base64");
|
|
|
|
const aes = new AES.ModeOfOperation.ctr(this.dkey.slice(0, 32), new AES.Counter(iv));
|
|
|
|
const plaintext = aes.decrypt(ciphertext);
|
|
|
|
const hmac = computeHmac("sha512", this.dkey.slice(32, 64), plaintext);
|
|
|
|
if (hmac !== data.hmac) {
|
|
|
|
throw new Error("wrong password");
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
this.values = JSON.parse(Buffer.from(plaintext).toString());
|
|
|
|
}
|
|
|
|
};
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-02-12 17:18:43 -05:00
|
|
|
Config.prototype.keys = async function() {
|
|
|
|
await this.load();
|
|
|
|
return Object.keys(this.values);
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
Config.prototype.save = function() {
|
|
|
|
this.values._junk = Buffer.from(randomBytes(16 + parseInt(Math.random() * 48))).toString("base64")
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
const plaintext = Buffer.from(JSON.stringify(this.values));
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
const iv = Buffer.from(randomBytes(16));
|
|
|
|
const hmac = computeHmac("sha512", this.dkey.slice(32, 64), plaintext);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
const aes = new AES.ModeOfOperation.ctr(this.dkey.slice(0, 32), new AES.Counter(iv));
|
|
|
|
const ciphertext = Buffer.from(aes.encrypt(plaintext));
|
|
|
|
|
|
|
|
const data = {
|
2019-05-14 18:25:46 -04:00
|
|
|
ciphertext: ciphertext.toString("base64"),
|
|
|
|
iv: iv.toString("base64"),
|
2019-09-20 22:38:03 -04:00
|
|
|
salt: this.salt,
|
2020-06-12 19:22:16 -04:00
|
|
|
hmac: hmac,
|
|
|
|
canary: this.canary
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
fs.writeFileSync(this.filename, JSON.stringify(data, null, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.prototype.get = async function(key) {
|
|
|
|
await this.load();
|
|
|
|
return this.values[key];
|
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.set = async function(key, value) {
|
|
|
|
await this.load();
|
|
|
|
this.values[key] = value;
|
|
|
|
this.save();
|
|
|
|
};
|
|
|
|
|
|
|
|
Config.prototype.lock = function() {
|
|
|
|
this.salt = this.dkey = null;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-09-20 22:38:03 -04:00
|
|
|
const config = new Config(resolve(os.homedir(), ".ethers-dist"));
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
module.exports = {
|
2019-09-20 22:38:03 -04:00
|
|
|
get: function(key) {
|
|
|
|
return config.get(key);
|
|
|
|
},
|
|
|
|
set: function(key, value) {
|
|
|
|
config.set(key, value);
|
|
|
|
},
|
2020-02-12 17:18:43 -05:00
|
|
|
keys: function() {
|
|
|
|
return config.keys();
|
|
|
|
},
|
2019-09-20 22:38:03 -04:00
|
|
|
lock: function() {
|
|
|
|
config.lock();
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|