UNPKG

shogun-core

Version:

SHOGUN CORE - Core library for Shogun Ecosystem

89 lines (88 loc) 3.09 kB
/** * Seed Phrase Utilities for Multi-Device Authentication * Provides BIP39-compatible seed phrase generation and validation */ import { generateMnemonic, mnemonicToSeedSync, validateMnemonic, } from '@scure/bip39'; import { wordlist } from '@scure/bip39/wordlists/english'; import { sha256 } from '@noble/hashes/sha256'; import { bytesToHex } from '@noble/hashes/utils'; /** * Generate a new 12-word BIP39 mnemonic seed phrase * @returns {string} 12-word mnemonic seed phrase */ export function generateSeedPhrase() { return generateMnemonic(wordlist, 128); // 128 bits = 12 words } /** * Validate a BIP39 mnemonic seed phrase * @param {string} mnemonic - The seed phrase to validate * @returns {boolean} True if valid, false otherwise */ export function validateSeedPhrase(mnemonic) { try { return validateMnemonic(mnemonic, wordlist); } catch (error) { return false; } } /** * Derive a deterministic seed from mnemonic and username * @param {string} mnemonic - The BIP39 mnemonic seed phrase * @param {string} username - Username to include in derivation * @returns {Uint8Array} 64-byte seed for key derivation */ export function mnemonicToSeed(mnemonic, username) { if (!validateSeedPhrase(mnemonic)) { throw new Error('Invalid mnemonic seed phrase'); } // Use username as additional entropy in the passphrase // This ensures different users with same seed phrase get different keys var passphrase = "shogun-".concat(username); return mnemonicToSeedSync(mnemonic, passphrase); } /** * Convert seed to deterministic password for GunDB * @param {Uint8Array} seed - The seed from mnemonic * @returns {string} Hex-encoded password */ export function seedToPassword(seed) { // Hash the seed to create a deterministic password var hash = sha256(seed); return bytesToHex(hash); } /** * Derive GunDB credentials from mnemonic * @param {string} mnemonic - The BIP39 mnemonic * @param {string} username - Username for derivation * @returns {{password: string; seed: Uint8Array}} Credentials for GunDB */ export function deriveCredentialsFromMnemonic(mnemonic, username) { var seed = mnemonicToSeed(mnemonic, username); var password = seedToPassword(seed); return { password: password, seed: seed, }; } /** * Format seed phrase for display (with word numbers) * @param {string} mnemonic - The seed phrase * @returns {string} Formatted seed phrase with numbers */ export function formatSeedPhrase(mnemonic) { var words = mnemonic.split(' '); return words.map(function (word, index) { return "".concat(index + 1, ". ").concat(word); }).join('\n'); } /** * Normalize and clean user input for seed phrase * @param {string} input - User-provided seed phrase * @returns {string} Normalized seed phrase */ export function normalizeSeedPhrase(input) { return input .toLowerCase() .trim() .replace(/\s+/g, ' ') // Replace multiple spaces with single space .replace(/[^\w\s]/g, ''); // Remove special characters }