UNPKG

@iacobus/hd

Version:

Hierarchical Deterministic Symmetric Keys.

113 lines (112 loc) 3.44 kB
"use strict"; /** * @fileoverview Entry point for @iacobus/hd. * Exports the Hdsk class, which exposes functionality for symmetric hierarchical * deterministic key derivation, including master key derivation and schema-enforced * derivation path parsing. * @module * @author Jacob V. B. Haap <iacobus.xyz> * @license MIT */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Hdsk = exports.Key = exports.Schema = void 0; const path_js_1 = require("./path.js"); const key_js_1 = require("./key.js"); /** Schema is an instance of a derivation path schema. */ class Schema { constructor(str) { Object.defineProperty(this, "schema", { enumerable: true, configurable: true, writable: true, value: void 0 }); const schema = (0, path_js_1.newSchema)(str); this.schema = schema; } /** * parse parses a new derivation path from a given hash and string. * @example * const path = Schema.parse(h, "m/42/0/1/0"); */ parse(h, str) { const path = (0, path_js_1.newPath)(h, str, this.schema); return path; } } exports.Schema = Schema; /** Key is an instance of an HD key. */ class Key { constructor(h, key) { Object.defineProperty(this, "key", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "h", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.h = h; this.key = key; } /** * child derives a new child key from a given index. * @example * const child = Key.child(42); */ child(index) { const key = (0, key_js_1.deriveChild)(this.h, this.key, index); return new Key(this.h, key); } /** * node derives a new key at a specific node in a hierarchy in * relation to a master key, from a given derivation path. * @example * const node = Key.node(path); */ node(path) { const key = (0, key_js_1.deriveNode)(this.h, this.key, path); return new Key(this.h, key); } /** * lineage checks if a key is the direct child of a master key, * from a given hash master key. * @example * const related = Key.lineage(master.key); */ lineage(master) { const related = (0, key_js_1.lineage)(this.h, this.key, master); return related; } } exports.Key = Key; /** * Hdsk exposes functionality for hierarchical deterministic symmetric key derivation. * It provides methods to derive master keys and parse derivation path schemas, used to * initialize key hierarchies, and parse schema-enforced derivation paths. */ class Hdsk { /** * schema parses a new derivation path schema from a given string. * @example * const str: string = "m / application: any / purpose: any / context: any / index: num"; * const schema = new Hdsk().schema(str); */ schema(str) { return new Schema(str); } /** * master derives a new master key from a given hash and secret. * @example * const master = new Hdsk().master(h, secret); */ master(h, secret) { const key = (0, key_js_1.deriveMaster)(h, secret); return new Key(h, key); } } exports.Hdsk = Hdsk;