"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 crypto_1 = require("crypto"); const bs58_1 = __importDefault(require("bs58")); class CryptoNode { generateRsaKeyPair(bits) { return new Promise((resolve, reject) => { crypto_1.generateKeyPair('rsa', { modulusLength: bits, privateKeyEncoding: { format: 'pem', type: 'pkcs1', }, publicKeyEncoding: { format: 'pem', type: 'spki', }, }, (err, publicKey, privateKey) => { if (err) { reject(err); return; } resolve({ export: () => this.export(privateKey, publicKey), getPublicHash: () => this.getPublicHash(publicKey), getPublicKey: () => this.getPublicKey(publicKey), sign: (data) => this.sign(privateKey, data), }); }); }); } sign(privateKeyText, dataHexBytes) { return __awaiter(this, void 0, void 0, function* () { const privateKey = crypto_1.createPrivateKey(privateKeyText); const sign = crypto_1.createSign('SHA256'); sign.write(Buffer.from(dataHexBytes, 'hex')); sign.end(); const signature = sign.sign(privateKey, 'hex'); return signature; }); } importRsaKeyPair(serialized) { return __awaiter(this, void 0, void 0, function* () { const decoded = bs58_1.default.decode(serialized); const { privateKey, publicKey } = JSON.parse(decoded.toString('utf-8')); return { export: () => this.export(privateKey, publicKey), getPublicHash: () => this.getPublicHash(publicKey), getPublicKey: () => this.getPublicKey(publicKey), sign: (data) => this.sign(privateKey, data), }; }); } getPublicHash(publicKeyText) { const publicKey = crypto_1.createPublicKey(publicKeyText); const derBytes = publicKey.export({ format: 'der', type: 'spki', }); const sha256 = crypto_1.createHash('sha256'); sha256.update(derBytes); const pubHash = bs58_1.default.encode(sha256.digest()); return pubHash; } getPublicKey(publicKeyText) { const publicKey = crypto_1.createPublicKey(publicKeyText); const derBytes = publicKey.export({ format: 'der', type: 'spki', }); return derBytes.toString('hex'); } export(privateKey, publicKey) { return __awaiter(this, void 0, void 0, function* () { const jsonString = JSON.stringify({ privateKey, publicKey }); return bs58_1.default.encode(Buffer.from(jsonString, 'utf-8')); }); } } exports.default = CryptoNode; //# sourceMappingURL=crypto-node.js.map