UNPKG

evm-blockchain-tools

Version:

This is a collection of resuseable tools to support development for EVM-powered blockchains

152 lines 7.36 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CryptoWalletRegistry = void 0; const crypto = __importStar(require("crypto")); const aes_encryption_1 = require("../utils/aes-encryption"); const utils_1 = require("../utils"); const interfaces_1 = require("../common/interfaces"); class CryptoWalletRegistry { constructor(config, storageEngine) { this.config = config; this.storageEngine = storageEngine; } initMasterWallet() { return __awaiter(this, void 0, void 0, function* () { const existingMasterWallet = yield this.getMasterWallet(); if (existingMasterWallet) { throw new Error("master wallet already exists"); } const newWallet = yield this.createBlockchainWallet(); yield this.storageEngine.bulkCreate([ Object.assign(Object.assign({}, newWallet), { name: "master_wallet", type: interfaces_1.WALLET_TYPE.MASTER }), ]); return this.getMasterWallet(); }); } getWalletById(id, adminPin, userPin) { return __awaiter(this, void 0, void 0, function* () { const foundWallet = yield this.storageEngine.getOne({ id, }); if (!foundWallet) { throw new Error(`wallet with id ${id} not found`); } const privateKey = yield this.recoverWalletPrivateKey(foundWallet, adminPin, userPin); return Object.assign(Object.assign({}, this.walletToDTO(foundWallet)), { privateKey }); }); } getWalletsByFilter(filter, adminPin, userPin) { return __awaiter(this, void 0, void 0, function* () { const foundWallets = yield this.storageEngine.getMany(filter); if (!foundWallets.length) { return []; } const recoverredWallets = yield Promise.all(foundWallets.map((wallet) => __awaiter(this, void 0, void 0, function* () { const privateKey = yield this.recoverWalletPrivateKey(wallet, adminPin, userPin); return Object.assign(Object.assign({}, this.walletToDTO(wallet)), { privateKey }); }))); return recoverredWallets; }); } walletToDTO(wallet) { return { address: wallet.address, id: wallet.id, name: wallet.name, type: wallet.type, }; } recoverWalletPrivateKey(wallet, adminPin, userPin) { return __awaiter(this, void 0, void 0, function* () { const { privateKey: secret, defaultUserPin } = this.config; const { serverSecretPart, userSecretPart, nonce } = wallet; const decodedServerSecret = (0, aes_encryption_1.decrypt)(serverSecretPart, `${secret}_${(0, aes_encryption_1.encrypt)(adminPin, secret)}`); const decodedUserSecret = (0, aes_encryption_1.decrypt)(userSecretPart, `${secret}_${(0, aes_encryption_1.encrypt)(userPin || defaultUserPin, secret)}`); const originalPrivateKey = yield (0, utils_1.recoverPrivateKey)({ nonce, serverSecret: decodedServerSecret, userSecret: decodedUserSecret, }); return originalPrivateKey; }); } createWallets(data, type) { return __awaiter(this, void 0, void 0, function* () { const payload = yield Promise.all(data.map((i) => __awaiter(this, void 0, void 0, function* () { const { address, serverSecretPart, userSecretPart, nonce } = yield this.createBlockchainWallet(i.pin); return { name: i.name, nonce, tags: i.tags, address, type, serverSecretPart, userSecretPart, }; }))); const createdWallets = yield this.storageEngine.bulkCreate(payload); return createdWallets.map(this.walletToDTO); }); } createBlockchainWallet(userPin) { return __awaiter(this, void 0, void 0, function* () { const { privateKey: secret, adminPin, defaultUserPin } = this.config; const { privateKey, publicKey } = yield (0, utils_1.generateKeyPair)(); const nonce = crypto.randomBytes(24); const [userSecret, serverSecret] = (0, utils_1.splitPrivateKeyToParts)(privateKey, nonce); const signedUserSecret = (0, aes_encryption_1.encrypt)(userSecret, `${secret}_${(0, aes_encryption_1.encrypt)(userPin || defaultUserPin, secret)}`); const signedServerSecret = (0, aes_encryption_1.encrypt)(serverSecret, `${secret}_${(0, aes_encryption_1.encrypt)(adminPin, secret)}`); return { address: publicKey, nonce: nonce.toString("hex"), serverSecretPart: signedServerSecret, userSecretPart: signedUserSecret, }; }); } getMasterWallet() { return __awaiter(this, void 0, void 0, function* () { const masterWallet = yield this.storageEngine.getOne({ type: interfaces_1.WALLET_TYPE.MASTER, }); return this.walletToDTO(masterWallet); }); } paginateSlaveWallets(filters, limit = 2000, offset = 0) { return this.storageEngine.paginate(Object.assign(Object.assign({}, filters), { type: interfaces_1.WALLET_TYPE.SLAVE }), limit, offset); } } exports.CryptoWalletRegistry = CryptoWalletRegistry; //# sourceMappingURL=crypto-wallet-registry.js.map