UNPKG

smartledger-sdk

Version:

A comprehensive blockchain and cryptographic operations SDK for JavaScript

141 lines (139 loc) 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.BSVKeys = void 0; var _bsv = _interopRequireDefault(require("bsv")); var _index = _interopRequireDefault(require("bsv/mnemonic/index.js")); var _uuid = require("uuid"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class BSVKeys { constructor() { this.bsv = _bsv.default; this.paths = { identity: "m/44'/236'/0'/0/0", financial: "m/44'/0'/0'/0/0", contractual: "m/44'/236'/1'/0/0", property: "m/44'/236'/2'/0/0", document: "m/44'/236'/3'/0/0", privacy: "m/44'/236'/4'/0/0" }; this.uuid = (0, _uuid.v4)(); this.mnemonic = null; this.privateKey = null; this.publicKey = null; this.hdPrivateKey = null; this.path = null; } static generateMnemonic() { // Generate 24-word mnemonic using 256 bits of entropy const entropy = Buffer.from(Array(32).fill(0).map(() => Math.floor(Math.random() * 256))); const mnemonic = new _index.default(entropy); // Verify we have 24 words const words = mnemonic.toString().split(" "); if (words.length !== 24) { // If not 24 words, try again return BSVKeys.generateMnemonic(); } return mnemonic; } static fromMnemonic(mnemonic, purpose = "financial") { if (typeof mnemonic === "string") { mnemonic = new _index.default(mnemonic); } const instance = new BSVKeys(); const path = instance.paths[purpose] || instance.paths.financial; const hdPrivateKey = mnemonic.toHDPrivateKey(); const derivedKey = hdPrivateKey.deriveChild(path); instance.privateKey = derivedKey.privateKey; instance.publicKey = instance.privateKey.publicKey; instance.hdPrivateKey = hdPrivateKey; // Store HD private key for derivation instance.path = path; return instance; } static generateKeyPair() { const instance = new BSVKeys(); instance.mnemonic = BSVKeys.generateMnemonic(); instance.privateKey = instance.mnemonic.toHDPrivateKey().deriveChild(instance.paths.identity).privateKey; instance.publicKey = instance.privateKey.publicKey; return instance; } static fromWIF(wif) { const privateKey = new _bsv.default.PrivateKey.fromWIF(wif); const instance = new BSVKeys(); instance.privateKey = privateKey; instance.publicKey = privateKey.publicKey; return instance; } static fromPrivateKey(privateKey) { const instance = new BSVKeys(); instance.privateKey = privateKey; instance.publicKey = privateKey.publicKey; return instance; } toWIF() { if (!this.privateKey) { throw new Error("Private key is required for WIF format"); } return this.privateKey.toWIF(); } toPublicKeyString() { return this.publicKey.toString(); } toAddress() { return this.publicKey.toAddress().toString(); } deriveChild(path) { if (!this.hdPrivateKey) { throw new Error("HD Private key is required for derivation. Use fromMnemonic to create a derivable key."); } const derivedKey = this.hdPrivateKey.deriveChild(path); const instance = new BSVKeys(); instance.privateKey = derivedKey.privateKey; instance.publicKey = instance.privateKey.publicKey; instance.hdPrivateKey = derivedKey; instance.path = path; return instance; } deriveForPurpose(purpose) { if (!this.hdPrivateKey) { throw new Error("HD Private Key not available. Keys must be created from mnemonic to derive for different purposes."); } const path = this.paths[purpose] || this.paths.financial; const derivedKey = this.hdPrivateKey.deriveChild(path); const instance = new BSVKeys(); instance.privateKey = derivedKey.privateKey; instance.publicKey = instance.privateKey.publicKey; instance.hdPrivateKey = this.hdPrivateKey; instance.path = path; return instance; } static generateAllKeys(mnemonic) { const instance = new BSVKeys(); const keys = { mnemonic: mnemonic.toString(), keys: [], uuid: instance.uuid }; for (const [type, path] of Object.entries(instance.paths)) { const derivedKey = this.fromMnemonic(mnemonic.toString(), type); keys.keys.push({ type, path, wif: derivedKey.toWIF(), publicKey: derivedKey.publicKey.toString(), address: derivedKey.toAddress() }); } return keys; } } // Example usage exports.BSVKeys = BSVKeys; async function getAllKeys() { const mnemonic = BSVKeys.generateMnemonic(); const keys = BSVKeys.generateAllKeys(mnemonic); console.log(keys); } getAllKeys(); var _default = exports.default = BSVKeys;