]> git.codecow.com Git - libnemo.git/commitdiff
Reorder wallet properties and add documentation.
authorChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 16:24:25 +0000 (09:24 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 18 Aug 2025 16:24:25 +0000 (09:24 -0700)
src/lib/wallet/index.ts

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