"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bs58_1 = __importDefault(require("bs58")); const node_forge_1 = require("node-forge"); function byteBufferToNumericArray(digested) { const count = digested.length(); const arr = []; for (let i = 0; i < count; i++) { arr.push(digested.getByte()); } return arr; } class CryptoForge { generateRsaKeyPair(bits) { return new Promise((resolve, reject) => { node_forge_1.pki.rsa.generateKeyPair({ bits, workers: 2 }, (err, { privateKey, publicKey }) => { if (err) { reject(err); return; } this.export(privateKey, publicKey).then(generated => { // tslint:disable-next-line:no-console console.log('generated keypair', generated); }); resolve({ export: () => this.export(privateKey, publicKey), getPublicHash: () => this.getPublicHash(publicKey), getPublicKey: () => this.getPublicKey(publicKey), sign: (data) => this.sign(privateKey, data), }); }); }); } sign(privateKeyObj, dataHexBytes) { return __awaiter(this, void 0, void 0, function* () { const sha256 = node_forge_1.md.sha256.create(); const buf = node_forge_1.util.hexToBytes(dataHexBytes); sha256.update(buf); const signature = privateKeyObj.sign(sha256); const hexed = node_forge_1.util.bytesToHex(signature); return hexed; }); } importRsaKeyPair(serialized) { return __awaiter(this, void 0, void 0, function* () { const { privateKey, publicKey } = JSON.parse(bs58_1.default.decode(serialized).toString('utf-8')); const privateKeyObj = node_forge_1.pki.privateKeyFromPem(privateKey); const publicKeyObj = node_forge_1.pki.publicKeyFromPem(publicKey); return { export: () => this.export(privateKeyObj, publicKeyObj), getPublicHash: () => this.getPublicHash(publicKeyObj), getPublicKey: () => this.getPublicKey(publicKeyObj), sign: (data) => this.sign(privateKeyObj, data), }; }); } getPublicHash(publicKeyObj) { const publicKeyHexString = this.getPublicKey(publicKeyObj); const sha256 = node_forge_1.md.sha256.create(); const buf = node_forge_1.util.hexToBytes(publicKeyHexString); sha256.update(buf); const digested = sha256.digest(); const arr = byteBufferToNumericArray(digested); const pubHash = bs58_1.default.encode(arr); return pubHash; } getPublicKey(publicKeyObj) { const pk = node_forge_1.asn1.toDer(node_forge_1.pki.publicKeyToAsn1(publicKeyObj)).toHex(); return pk; } export(privateKeyObj, publicKeyObj) { return __awaiter(this, void 0, void 0, function* () { const privateKey = node_forge_1.pki.privateKeyToPem(privateKeyObj); const publicKey = node_forge_1.pki.publicKeyToPem(publicKeyObj); const jsonString = JSON.stringify({ privateKey, publicKey }); const uint8Array = node_forge_1.util.text.utf8.encode(jsonString); return bs58_1.default.encode(uint8Array); }); } } exports.default = CryptoForge; //# sourceMappingURL=crypto-forge.js.map