From e6ecd1a27f664ecb7101bc34d50d2b6d05a3dcfb Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 7 Aug 2025 19:44:10 -0700 Subject: [PATCH] Simplify BIP class names. --- src/lib/bip39.ts | 14 +++++++------- src/lib/bip44.ts | 2 +- src/lib/safe.ts | 22 +++++++++++----------- src/lib/wallet.ts | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/lib/bip39.ts b/src/lib/bip39.ts index ce721b1..a4002fd 100644 --- a/src/lib/bip39.ts +++ b/src/lib/bip39.ts @@ -8,7 +8,7 @@ import { Entropy } from './entropy' /** * Represents a mnemonic phrase that identifies a wallet as defined by BIP-39. */ -export class Bip39Mnemonic { +export class Bip39 { static #isInternal: boolean = false /** @@ -40,7 +40,7 @@ export class Bip39Mnemonic { * @param {(string|ArrayBuffer|Uint8Array)} entropy - Cryptographically secure random value * @returns {string} Mnemonic phrase created using the BIP-39 wordlist */ - static async fromEntropy (entropy: string | ArrayBuffer | Uint8Array): Promise { + static async fromEntropy (entropy: string | ArrayBuffer | Uint8Array): Promise { const e = new Entropy(entropy) const phraseLength = 0.75 * e.byteLength const checksum = await this.#checksum(e.bytes) @@ -67,7 +67,7 @@ export class Bip39Mnemonic { * @param {string} phrase - String of 12, 15, 18, 21, or 24 words * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist */ - static async fromPhrase (phrase: string): Promise { + static async fromPhrase (phrase: string): Promise { this.#isInternal = true const self = new this() const isValid = await this.validate(phrase) @@ -141,10 +141,10 @@ export class Bip39Mnemonic { get phrase (): string | undefined { return this.#phrase?.join(' ').normalize('NFKD') } private constructor () { - if (!Bip39Mnemonic.#isInternal) { + if (!Bip39.#isInternal) { throw new Error(`Bip39Mnemonic must be created with async methods 'fromPhrase()' or 'fromEntropy().`) } - Bip39Mnemonic.#isInternal = false + Bip39.#isInternal = false } /** @@ -229,11 +229,11 @@ export class Bip39Mnemonic { if (this.#blake2bSeed == null) { let bits = 0n for (const word of this.#phrase) { - const wordIndex = Bip39Mnemonic.wordlist.indexOf(word) + const wordIndex = Bip39.wordlist.indexOf(word) if (wordIndex === -1) { throw new RangeError('Word not found in BIP-39 list') } - bits = (bits << 11n) | BigInt(Bip39Mnemonic.wordlist.indexOf(word)) + bits = (bits << 11n) | BigInt(Bip39.wordlist.indexOf(word)) } bits >>= 8n this.#blake2bSeed = new Uint8Array(32) diff --git a/src/lib/bip44.ts b/src/lib/bip44.ts index a7c82ec..00fd399 100644 --- a/src/lib/bip44.ts +++ b/src/lib/bip44.ts @@ -8,7 +8,7 @@ type ExtendedKey = { chainCode: ArrayBuffer } -export class Bip44Ckd { +export class Bip44 { /** * Derives a private child key for a coin by following the specified BIP-32 and * BIP-44 derivation path. Purpose is always 44'. Only hardened child keys are diff --git a/src/lib/safe.ts b/src/lib/safe.ts index 74a773b..e1f7c43 100644 --- a/src/lib/safe.ts +++ b/src/lib/safe.ts @@ -4,8 +4,8 @@ 'use strict' import { parentPort } from 'node:worker_threads' -import { Bip39Mnemonic } from './bip39' -import { Bip44Ckd } from './bip44' +import { Bip39 } from './bip39' +import { Bip44 } from './bip44' import { Blake2b } from './blake2b' import { default as Constants, BIP44_COIN_NANO } from './constants' import { default as Convert, bytes, hex, utf8 } from './convert' @@ -20,7 +20,7 @@ export class Safe { static #locked: boolean = true static #type?: 'BIP-44' | 'BLAKE2b' static #seed?: ArrayBuffer - static #mnemonic?: Bip39Mnemonic + static #mnemonic?: Bip39 static #parentPort?: any static { NODE: this.#parentPort = parentPort @@ -112,7 +112,7 @@ export class Safe { static async create (type?: 'BIP-44' | 'BLAKE2b', key?: CryptoKey, keySalt?: ArrayBuffer, mnemonicSalt?: string): Promise> { try { const entropy = new Entropy() - const { phrase: mnemonicPhrase } = await Bip39Mnemonic.fromEntropy(entropy.bytes) + const { phrase: mnemonicPhrase } = await Bip39.fromEntropy(entropy.bytes) const record = await this.import(type, key, keySalt, mnemonicPhrase, mnemonicSalt) if (this.#seed == null || this.#mnemonic?.phrase == null) { throw new Error('Failed to generate seed and mnemonic') @@ -143,7 +143,7 @@ export class Safe { throw new Error('Invalid wallet account index') } const prv = this.#type === 'BIP-44' - ? await Bip44Ckd.ckd(this.#seed, BIP44_COIN_NANO, index) + ? await Bip44.ckd(this.#seed, BIP44_COIN_NANO, index) : await this.#deriveBlake2bPrivateKey(this.#seed, index) const pub = await NanoNaCl.convert(new Uint8Array(prv)) return { index, publicKey: pub.buffer } @@ -190,10 +190,10 @@ export class Safe { if (secret instanceof ArrayBuffer) { this.#seed = secret if (type === 'BLAKE2b') { - this.#mnemonic = await Bip39Mnemonic.fromEntropy(new Uint8Array(secret)) + this.#mnemonic = await Bip39.fromEntropy(new Uint8Array(secret)) } } else { - this.#mnemonic = await Bip39Mnemonic.fromPhrase(secret) + this.#mnemonic = await Bip39.fromPhrase(secret) this.#seed = type === 'BIP-44' ? (await this.#mnemonic.toBip39Seed(mnemonicSalt ?? '')).buffer : (await this.#mnemonic.toBlake2bSeed()).buffer @@ -231,7 +231,7 @@ export class Safe { if (data == null) { throw new Error('Data to sign not found') } - const prv = await Bip44Ckd.ckd(this.#seed, BIP44_COIN_NANO, index) + const prv = await Bip44.ckd(this.#seed, BIP44_COIN_NANO, index) const sig = await NanoNaCl.detached(new Uint8Array(data), new Uint8Array(prv)) return { signature: sig.buffer } } catch (err) { @@ -261,7 +261,7 @@ export class Safe { throw new TypeError('Invalid seed') } this.#seed = seed - if (mnemonic != null) this.#mnemonic = await Bip39Mnemonic.fromPhrase(mnemonic) + if (mnemonic != null) this.#mnemonic = await Bip39.fromPhrase(mnemonic) this.#locked = false return { isUnlocked: !this.#locked } } catch (err) { @@ -530,8 +530,8 @@ export default ` ${importWorkerThreads} ${Convert} ${Constants} - const Bip39Mnemonic = ${Bip39Mnemonic} - const Bip44Ckd = ${Bip44Ckd} + const Bip39 = ${Bip39} + const Bip44 = ${Bip44} const Blake2b = ${Blake2b} const Entropy = ${Entropy} const NanoNaCl = ${NanoNaCl} diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 3c06de1..78648ff 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -2,7 +2,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Account, AccountList } from './account' -import { Bip39Mnemonic } from './bip39' +import { Bip39 } from './bip39' import { Block } from './block' import { ADDRESS_GAP } from './constants' import { bytes, hex, utf8 } from './convert' @@ -113,7 +113,7 @@ export class Wallet { } if (/^(?:[A-F0-9]{64}){1,2}$/i.test(secret)) { data.seed = hex.toBuffer(secret) - } else if (await Bip39Mnemonic.validate(secret)) { + } else if (await Bip39.validate(secret)) { data.mnemonicPhrase = secret.toLowerCase() if (mnemonicSalt != null) data.mnemonicSalt = mnemonicSalt } else { -- 2.47.3