@fabric-es/operator
Version:
Network operator
100 lines • 3.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readKey = exports.prepareOrgKeys = void 0;
const crypto_1 = require("crypto");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const util_1 = __importDefault(require("util"));
const elliptic_1 = __importDefault(require("elliptic"));
const types_1 = require("../types");
const utils_1 = require("../utils");
const prepareOrgKeys = async ({ keyPath, curve = 'secp256k1', }) => {
const logger = utils_1.getLogger({ name: '[operator] prepareOrgKeys.js' });
const filePrv = path_1.default.join(keyPath, 'org-prv.key');
const filePub = path_1.default.join(keyPath, 'org.key');
let dataPrv;
try {
dataPrv = await fs_1.promises.readFile(filePrv);
}
catch (e) {
if (e.code !== 'ENOENT') {
throw new Error(util_1.default.format('fail to access key files %s, %j', filePrv, e));
}
}
let dataPub;
try {
dataPub = await fs_1.promises.readFile(filePub);
}
catch (e) {
if (e.code !== 'ENOENT') {
throw new Error(util_1.default.format('fail to access key files %s, %j', filePub, e));
}
}
const ec = new elliptic_1.default.ec(curve);
if (dataPrv && dataPub) {
const strPrv = Buffer.from(dataPrv).toString();
const strPub = Buffer.from(dataPub).toString();
if (verify(ec, strPrv, strPub)) {
return {
status: types_1.SUCCESS,
message: `Using existing org-keys in ${keyPath}`,
};
}
else {
throw new Error(`mismatched org-keys found in ${keyPath}`);
}
}
logger.info(`creating new org-keys...`);
const key = ec.genKeyPair();
const pub = key.getPublic('hex');
const prv = key.getPrivate('hex');
try {
await fs_1.promises.mkdir(keyPath, { recursive: true });
}
catch (e) {
throw new Error(util_1.default.format('fail to create key files in %s, %j', keyPath, e));
}
try {
await fs_1.promises.writeFile(filePrv, prv);
}
catch (e) {
throw new Error(util_1.default.format('fail to write to key files %s, %j', filePrv, e));
}
try {
await fs_1.promises.writeFile(filePub, pub);
}
catch (e) {
throw new Error(util_1.default.format('fail to write to key files %s, %j', filePub, e));
}
return {
status: types_1.SUCCESS,
message: `Created new org-key in ${keyPath}`,
};
};
exports.prepareOrgKeys = prepareOrgKeys;
const verify = (ec, prv, pub) => {
const ahash = crypto_1.createHash('sha256').update('some worthless text...').digest('hex');
const signature = ec.keyFromPrivate(prv, 'hex').sign(ahash).toDER('hex');
return ec.keyFromPublic(pub, 'hex').verify(ahash, signature);
};
const readKey = async (keyPath, isPrivate = false) => {
const file = isPrivate ? path_1.default.join(keyPath, 'org-prv.key') : path_1.default.join(keyPath, 'org.key');
let data;
try {
data = await fs_1.promises.readFile(file);
return Buffer.from(data).toString();
}
catch (e) {
if (e.code === 'ENOENT') {
throw new Error('Key file missing');
}
else {
throw new Error(util_1.default.format('fail to access key files %s, %j', file, e));
}
}
};
exports.readKey = readKey;
//# sourceMappingURL=orgKeys.js.map