UNPKG

mnemonic-keyring

Version:

Mnemonic Keyring for secp256k1 and ed25519

104 lines 4.94 kB
"use strict"; 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()); }); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const encUtils = __importStar(require("enc-utils")); const iso_random_1 = require("@pedrouid/iso-random"); const ed25519_1 = __importDefault(require("bcrypto/lib/ed25519")); const secp256k1_1 = __importDefault(require("bcrypto/lib/secp256k1")); const base16_1 = __importDefault(require("bcrypto/lib/encoding/base16")); const bip39 = __importStar(require("bip39")); const bip32 = __importStar(require("bip32")); const constants_1 = require("./constants"); class MnemonicKeyring { constructor(mnemonic, masterKey) { this.mnemonic = mnemonic; this.masterKey = masterKey; this.mnemonic = mnemonic; this.masterKey = masterKey; } static generateMnemonic(length = constants_1.DEFAULT_ENTROPY_LENGTH) { return bip39.entropyToMnemonic(encUtils.arrayToBuffer(iso_random_1.randomBytes(length))); } static deriveMasterKey(mnemonic) { return __awaiter(this, void 0, void 0, function* () { if (!bip39.validateMnemonic(mnemonic)) { throw new Error('Invalid mnemonic provided!'); } const seed = yield bip39.mnemonicToSeed(mnemonic); return bip32.fromSeed(seed); }); } static init(opts) { return __awaiter(this, void 0, void 0, function* () { const storageKey = opts.storageKey || constants_1.DEFAULT_STORAGE_KEY; const entropyLength = opts.entropyLength || constants_1.DEFAULT_ENTROPY_LENGTH; let mnemonic; if (typeof opts.mnemonic !== 'undefined') { mnemonic = opts.mnemonic; } else { mnemonic = opts.mnemonic || (typeof opts.storage !== 'undefined' ? yield opts.storage.getItem(storageKey) : undefined) || this.generateMnemonic(entropyLength); } if (typeof opts.storage !== 'undefined') { yield opts.storage.setItem(storageKey, mnemonic); } const masterKey = yield this.deriveMasterKey(mnemonic); return new MnemonicKeyring(mnemonic, masterKey); }); } getPrivateKey(derivationPath) { return this.derivePrivateKey(this.masterKey, derivationPath); } getPublicKey(derivationPath, ellipticCurve = constants_1.DEFAULT_ELLIPTIC_CURVE) { const privateKey = this.derivePrivateKey(this.masterKey, derivationPath); return this.derivePublicKey(privateKey, ellipticCurve); } getKeyPair(derivationPath, ellipticCurve = constants_1.DEFAULT_ELLIPTIC_CURVE) { const privateKey = this.derivePrivateKey(this.masterKey, derivationPath); const publicKey = this.derivePublicKey(privateKey, ellipticCurve); return { privateKey, publicKey }; } derivePrivateKey(masterKey, derivationPath) { const hdnode = masterKey.derivePath(derivationPath); return base16_1.default.encode(hdnode.privateKey || Buffer.from([])); } derivePublicKey(privateKey, ellipticCurve = constants_1.DEFAULT_ELLIPTIC_CURVE) { let publicKey; switch (ellipticCurve) { case 'ed25519': publicKey = ed25519_1.default.publicKeyCreate(base16_1.default.decode(privateKey), true); break; case 'secp256k1': publicKey = secp256k1_1.default.publicKeyCreate(base16_1.default.decode(privateKey), true); break; default: throw new Error(`Elliptic curve not supported: ${ellipticCurve}`); } return base16_1.default.encode(publicKey); } } exports.MnemonicKeyring = MnemonicKeyring; //# sourceMappingURL=keyring.js.map