From 0be1617630e1ad4e973856af0044f6a23a84d0e7 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 18 Aug 2025 09:24:25 -0700 Subject: [PATCH] Reorder wallet properties and add documentation. --- src/lib/wallet/index.ts | 90 ++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index 71ff48d..2d5e4a9 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -22,9 +22,17 @@ import { WorkerQueue } from '../vault/worker-queue' * three types of wallets are supported: BIP-44, BLAKE2b, and Ledger. */ export class Wallet { - static #isInternal: boolean = false static DB_NAME = 'Wallet' + /** + * Retrieves all encrypted wallets from the database. + * + * @returns Array of wallets with encrypted secrets and unencrypted metadata + */ + static async backup (): Promise { + return _backup() + } + /** * Creates a new HD wallet by using an entropy value generated using a * cryptographically strong pseudorandom number generator. @@ -40,15 +48,6 @@ export class Wallet { return self } - /** - * Retrieves all encrypted wallets from the database. - * - * @returns Array of wallets with encrypted secrets and unencrypted metadata - */ - static async backup (): Promise { - return _backup() - } - /** * Imports an existing HD wallet by using an entropy value generated using a * cryptographically strong pseudorandom number generator.NamedD @@ -100,19 +99,36 @@ export class Wallet { return typeof id === 'string' ? wallets[0] : wallets } - #accounts: AccountList - #lockTimer?: any - #id: string - #mnemonic?: ArrayBuffer - #vault: WorkerQueue - #seed?: ArrayBuffer - #type: WalletType + constructor (type: WalletType, id?: string) + constructor (type: unknown, id?: string) { + if (!Wallet.#isInternal && type !== 'Ledger') { + throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`) + } + if (type !== 'BIP-44' && type !== 'BLAKE2b' && type !== 'Ledger') { + throw new TypeError('Invalid wallet type', { cause: type }) + } + Wallet.#isInternal = false + this.#accounts = new AccountList() + this.#id = id ?? crypto.randomUUID() + this.#vault = new WorkerQueue(VaultWorker) + this.#type = type + } + + /** + * @returns UUID of the wallet. + */ + get id (): string { return this.#id } - get id () { return this.#id } - get type () { return this.#type } + /** + * Method used to create wallet and derive accounts. + */ + get type (): WalletType { return this.#type } - /** Set when calling `create()` and self-destructs after the first read. */ - get mnemonic () { + /** + * Set when calling `create()`. Self-destructs after the first read. + * @returns Mnemonic phrase used to derive the wallet seed. + */ + get mnemonic (): string | undefined { if (this.#mnemonic == null) return undefined try { const b = new Uint8Array(this.#mnemonic) @@ -125,8 +141,12 @@ export class Wallet { return undefined } } - /** Set when calling `create()` and self-destructs after the first read. */ - get seed () { + + /** + * Set when calling `create()`. Self-destructs after the first read. + * @returns Seed used to derive accounts. + */ + get seed (): string | undefined { if (this.#seed == null) return undefined try { const b = new Uint8Array(this.#seed) @@ -140,21 +160,6 @@ export class Wallet { } } - constructor (type: WalletType, id?: string) - constructor (type: unknown, id?: string) { - if (!Wallet.#isInternal && type !== 'Ledger') { - throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`) - } - if (type !== 'BIP-44' && type !== 'BLAKE2b' && type !== 'Ledger') { - throw new TypeError('Invalid wallet type', { cause: type }) - } - Wallet.#isInternal = false - this.#accounts = new AccountList() - this.#id = id ?? crypto.randomUUID() - this.#vault = new WorkerQueue(VaultWorker) - this.#type = type - } - /** * Retrieves an account from a wallet using its child key derivation function. * Defaults to the first account at index 0. @@ -444,4 +449,13 @@ export class Wallet { this.#lockTimer = setTimeout(() => this.lock(), 300000) } } + + static #isInternal: boolean = false + #accounts: AccountList + #lockTimer?: any + #id: string + #mnemonic?: ArrayBuffer + #vault: WorkerQueue + #seed?: ArrayBuffer + #type: WalletType } -- 2.47.3