]> git.codecow.com Git - libnemo.git/commitdiff
Simplify BIP class names.
authorChris Duncan <chris@zoso.dev>
Fri, 8 Aug 2025 02:44:10 +0000 (19:44 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 8 Aug 2025 02:44:10 +0000 (19:44 -0700)
src/lib/bip39.ts
src/lib/bip44.ts
src/lib/safe.ts
src/lib/wallet.ts

index ce721b1304e16ae4aeeb7a81b21696acdb498518..a4002fd9ab468eb08897d1e85172d93713ba11b7 100644 (file)
@@ -8,7 +8,7 @@ import { Entropy } from './entropy'
 /**\r
 * Represents a mnemonic phrase that identifies a wallet as defined by BIP-39.\r
 */\r
-export class Bip39Mnemonic {\r
+export class Bip39 {\r
        static #isInternal: boolean = false\r
 \r
        /**\r
@@ -40,7 +40,7 @@ export class Bip39Mnemonic {
        * @param {(string|ArrayBuffer|Uint8Array<ArrayBuffer>)} entropy - Cryptographically secure random value\r
        * @returns {string} Mnemonic phrase created using the BIP-39 wordlist\r
        */\r
-       static async fromEntropy (entropy: string | ArrayBuffer | Uint8Array<ArrayBuffer>): Promise<Bip39Mnemonic> {\r
+       static async fromEntropy (entropy: string | ArrayBuffer | Uint8Array<ArrayBuffer>): Promise<Bip39> {\r
                const e = new Entropy(entropy)\r
                const phraseLength = 0.75 * e.byteLength\r
                const checksum = await this.#checksum(e.bytes)\r
@@ -67,7 +67,7 @@ export class Bip39Mnemonic {
        * @param {string} phrase - String of 12, 15, 18, 21, or 24 words\r
        * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist\r
        */\r
-       static async fromPhrase (phrase: string): Promise<Bip39Mnemonic> {\r
+       static async fromPhrase (phrase: string): Promise<Bip39> {\r
                this.#isInternal = true\r
                const self = new this()\r
                const isValid = await this.validate(phrase)\r
@@ -141,10 +141,10 @@ export class Bip39Mnemonic {
        get phrase (): string | undefined { return this.#phrase?.join(' ').normalize('NFKD') }\r
 \r
        private constructor () {\r
-               if (!Bip39Mnemonic.#isInternal) {\r
+               if (!Bip39.#isInternal) {\r
                        throw new Error(`Bip39Mnemonic must be created with async methods 'fromPhrase()' or 'fromEntropy().`)\r
                }\r
-               Bip39Mnemonic.#isInternal = false\r
+               Bip39.#isInternal = false\r
        }\r
 \r
        /**\r
@@ -229,11 +229,11 @@ export class Bip39Mnemonic {
                if (this.#blake2bSeed == null) {\r
                        let bits = 0n\r
                        for (const word of this.#phrase) {\r
-                               const wordIndex = Bip39Mnemonic.wordlist.indexOf(word)\r
+                               const wordIndex = Bip39.wordlist.indexOf(word)\r
                                if (wordIndex === -1) {\r
                                        throw new RangeError('Word not found in BIP-39 list')\r
                                }\r
-                               bits = (bits << 11n) | BigInt(Bip39Mnemonic.wordlist.indexOf(word))\r
+                               bits = (bits << 11n) | BigInt(Bip39.wordlist.indexOf(word))\r
                        }\r
                        bits >>= 8n\r
                        this.#blake2bSeed = new Uint8Array(32)\r
index a7c82ec82e2dae5ff7dcca1284c3a39e2eebc68c..00fd3997a3cffc1704a8e34180ec854d0d5f2f3c 100644 (file)
@@ -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
index 74a773b443408a8298b2eaf1b28148ae1b76109a..e1f7c437f3e0572503d82def1dab8b43ef5a901d 100644 (file)
@@ -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<NamedData<ArrayBuffer>> {
                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}
index 3c06de1d174c8414ce1a5761c349316418819e61..78648ffbc2e9837b6b65880b7d6189b5a93aa3a2 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
 import { Account, AccountList } from './account'\r
-import { Bip39Mnemonic } from './bip39'\r
+import { Bip39 } from './bip39'\r
 import { Block } from './block'\r
 import { ADDRESS_GAP } from './constants'\r
 import { bytes, hex, utf8 } from './convert'\r
@@ -113,7 +113,7 @@ export class Wallet {
                        }\r
                        if (/^(?:[A-F0-9]{64}){1,2}$/i.test(secret)) {\r
                                data.seed = hex.toBuffer(secret)\r
-                       } else if (await Bip39Mnemonic.validate(secret)) {\r
+                       } else if (await Bip39.validate(secret)) {\r
                                data.mnemonicPhrase = secret.toLowerCase()\r
                                if (mnemonicSalt != null) data.mnemonicSalt = mnemonicSalt\r
                        } else {\r