]> git.codecow.com Git - libnemo.git/commitdiff
Move wallet create validation into separate module.
authorChris Duncan <chris@zoso.dev>
Sun, 10 Aug 2025 22:01:28 +0000 (15:01 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 10 Aug 2025 22:01:28 +0000 (15:01 -0700)
src/lib/wallet/create.ts
src/lib/wallet/index.ts

index 7bb7b8c6d5a830b3e68e609a8f854d4f06fae118..3e0ef2fa96ea0bced60a142029dfecb399a385d2 100644 (file)
@@ -1,9 +1,11 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
+import { utf8 } from '../convert'
 import { Database } from '../database'
 import { _load } from './load'
 import { Wallet } from '.'
+import { NamedData } from '#types'
 
 /**
 * Creates a new HD wallet by using an entropy value generated using a
@@ -13,14 +15,23 @@ import { Wallet } from '.'
 * @param {string} [salt=''] - Used when generating the final seed
 * @returns {Wallet} A newly instantiated Wallet
 */
-export async function _create (wallet: Wallet, password: ArrayBuffer, mnemonicSalt?: string) {
+export async function _create (wallet: Wallet, password: string, mnemonicSalt?: string): Promise<NamedData<ArrayBuffer>>
+export async function _create (wallet: Wallet, password: unknown, mnemonicSalt?: unknown): Promise<NamedData<ArrayBuffer>> {
+       if (typeof password !== 'string') {
+               throw new TypeError('Invalid password', { cause: typeof password })
+       }
+       if (mnemonicSalt !== undefined && typeof mnemonicSalt !== 'string') {
+               throw new TypeError('Mnemonic salt must be a string')
+       }
        try {
                const { iv, salt, encrypted, seed, mnemonic } = await wallet.vault.request<ArrayBuffer>({
                        action: 'create',
                        type: wallet.type,
-                       password,
+                       password: utf8.toBuffer(password),
                        mnemonicSalt: mnemonicSalt ?? ''
                })
+               password = undefined
+               mnemonicSalt = undefined
                const record = {
                        id: wallet.id,
                        type: wallet.type,
index 8ff2325c0466cd2420ff3202cab7867355efdf4b..1762f75637c0d0e1a2c2898a64cee6bd8317c4ea 100644 (file)
@@ -42,28 +42,10 @@ export class Wallet {
        * @param {string} [salt=''] - Used when generating the final seed\r
        * @returns {Wallet} A newly instantiated Wallet\r
        */\r
-       static async create (type: WalletType, password: string, mnemonicSalt?: string): Promise<Wallet>\r
-       static async create (type: unknown, password: unknown, mnemonicSalt?: unknown): Promise<Wallet> {\r
-               if (type === 'Ledger') {\r
-                       Wallet.#isInternal = true\r
-                       return new this(type)\r
-               }\r
-               if (type !== 'BIP-44' && type !== 'BLAKE2b') {\r
-                       throw new TypeError('Invalid wallet type')\r
-               }\r
-               if (typeof password !== 'string') {\r
-                       throw new TypeError('Invalid password', { cause: typeof password })\r
-               }\r
-               const passwordBuffer = utf8.toBuffer(password)\r
-               password = undefined\r
-               if (mnemonicSalt !== undefined && typeof mnemonicSalt !== 'string') {\r
-                       throw new TypeError('Mnemonic salt must be a string')\r
-               }\r
+       static async create (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicSalt?: string): Promise<Wallet> {\r
                Wallet.#isInternal = true\r
                const self = new this(type)\r
-               const { mnemonic, seed } = await _create(self, passwordBuffer, mnemonicSalt)\r
-               self.#mnemonic = mnemonic\r
-               self.#seed = seed\r
+               { ({ mnemonic: self.#mnemonic, seed: self.#seed } = await _create(self, password, mnemonicSalt)) }\r
                return self\r
        }\r
 \r