From 5f5162565965bdabf77b155d2303ae3e5099f34d Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 10 Aug 2025 15:01:28 -0700 Subject: [PATCH] Move wallet create validation into separate module. --- src/lib/wallet/create.ts | 15 +++++++++++++-- src/lib/wallet/index.ts | 22 ++-------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/lib/wallet/create.ts b/src/lib/wallet/create.ts index 7bb7b8c..3e0ef2f 100644 --- a/src/lib/wallet/create.ts +++ b/src/lib/wallet/create.ts @@ -1,9 +1,11 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! 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> +export async function _create (wallet: Wallet, password: unknown, mnemonicSalt?: unknown): Promise> { + 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({ action: 'create', type: wallet.type, - password, + password: utf8.toBuffer(password), mnemonicSalt: mnemonicSalt ?? '' }) + password = undefined + mnemonicSalt = undefined const record = { id: wallet.id, type: wallet.type, diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index 8ff2325..1762f75 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -42,28 +42,10 @@ export class Wallet { * @param {string} [salt=''] - Used when generating the final seed * @returns {Wallet} A newly instantiated Wallet */ - static async create (type: WalletType, password: string, mnemonicSalt?: string): Promise - static async create (type: unknown, password: unknown, mnemonicSalt?: unknown): Promise { - if (type === 'Ledger') { - Wallet.#isInternal = true - return new this(type) - } - if (type !== 'BIP-44' && type !== 'BLAKE2b') { - throw new TypeError('Invalid wallet type') - } - if (typeof password !== 'string') { - throw new TypeError('Invalid password', { cause: typeof password }) - } - const passwordBuffer = utf8.toBuffer(password) - password = undefined - if (mnemonicSalt !== undefined && typeof mnemonicSalt !== 'string') { - throw new TypeError('Mnemonic salt must be a string') - } + static async create (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicSalt?: string): Promise { Wallet.#isInternal = true const self = new this(type) - const { mnemonic, seed } = await _create(self, passwordBuffer, mnemonicSalt) - self.#mnemonic = mnemonic - self.#seed = seed + { ({ mnemonic: self.#mnemonic, seed: self.#seed } = await _create(self, password, mnemonicSalt)) } return self } -- 2.47.3