//! 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
* @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,
* @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