]> git.codecow.com Git - libnemo.git/commitdiff
Create wallet IDs from full 256-bit entropy. Change name of public method for putting...
authorChris Duncan <chris@zoso.dev>
Sun, 6 Jul 2025 07:23:30 +0000 (00:23 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 6 Jul 2025 07:23:30 +0000 (00:23 -0700)
src/lib/wallets/bip44-wallet.ts
src/lib/wallets/blake2b-wallet.ts
src/lib/wallets/wallet.ts
src/lib/workers/safe.ts

index 0b4865a181bd7528d3d0cba2a012c37e28a285fd..2632535ffe00fc798e3574b14c7d6a730cc9a4fc 100644 (file)
@@ -93,7 +93,7 @@ export class Bip44Wallet extends Wallet {
        static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise<Bip44Wallet> {\r
                let wallet: Bip44Wallet\r
                try {\r
-                       const id = await Entropy.create(16)\r
+                       const id = await Entropy.create()\r
                        const e = await Entropy.import(entropy)\r
                        const m = await Bip39Mnemonic.fromEntropy(e.hex)\r
                        const s = await m.toBip39Seed(salt)\r
@@ -132,7 +132,7 @@ export class Bip44Wallet extends Wallet {
        static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise<Bip44Wallet> {\r
                let wallet: Bip44Wallet\r
                try {\r
-                       const id = await Entropy.create(16)\r
+                       const id = await Entropy.create()\r
                        const m = await Bip39Mnemonic.fromPhrase(mnemonic)\r
                        const s = await m.toBip39Seed(salt)\r
                        Bip44Wallet.#isInternal = true\r
@@ -176,7 +176,7 @@ export class Bip44Wallet extends Wallet {
                if (!/^[0-9a-fA-F]+$/i.test(seed)) {\r
                        throw new Error('Seed contains invalid hexadecimal characters.')\r
                }\r
-               const id = await Entropy.create(16)\r
+               const id = await Entropy.create()\r
                Bip44Wallet.#isInternal = true\r
                const wallet = new this(id, seed)\r
                try {\r
index 880bd32245d1a8c717f16e30dc5cd69b316d0cfc..c2a405d8b60ea65e01b65890123d0d024ecc2c61 100644 (file)
@@ -85,7 +85,7 @@ export class Blake2bWallet extends Wallet {
                if (!/^[0-9a-fA-F]+$/i.test(seed)) {\r
                        throw new Error('Seed contains invalid hexadecimal characters.')\r
                }\r
-               const id = await Entropy.create(16)\r
+               const id = await Entropy.create()\r
                const s = seed\r
                const m = await Bip39Mnemonic.fromEntropy(seed)\r
                Blake2bWallet.#isInternal = true\r
@@ -118,7 +118,7 @@ export class Blake2bWallet extends Wallet {
        static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise<Blake2bWallet> {\r
                let wallet: Blake2bWallet\r
                try {\r
-                       const id = await Entropy.create(16)\r
+                       const id = await Entropy.create()\r
                        const m = await Bip39Mnemonic.fromPhrase(mnemonic)\r
                        const s = await m.toBlake2bSeed()\r
                        Blake2bWallet.#isInternal = true\r
index 4a685177a96ffb499fd480f377a8726c5d249656..b2efda7696592f4cc2ba85051f9713a36d8c5104 100644 (file)
@@ -152,7 +152,8 @@ export abstract class Wallet {
        }\r
 \r
        /**\r
-       * Locks the wallet with a password that will be needed to unlock it later.\r
+       * Locks the wallet and all currently derived accounts with a password that\r
+       * will be needed to unlock it later.\r
        *\r
        * @param {(string|Uint8Array)} password Used to lock the wallet\r
        * @returns True if successfully locked\r
@@ -177,12 +178,12 @@ export abstract class Wallet {
                                data.seed = this.#s\r
                        }\r
                        const response = (await Wallet.#poolSafe.assign({\r
-                               method: 'put',\r
+                               method: 'set',\r
                                name: this.id,\r
                                password,\r
                                data\r
                        }))[0]\r
-                       const success = response.result\r
+                       const success = response?.result\r
                        if (!success) {\r
                                throw null\r
                        }\r
@@ -196,9 +197,9 @@ export abstract class Wallet {
                } finally {\r
                        password.fill(0)\r
                }\r
-               this.#locked = true\r
                this.#m = null\r
                this.#s = null\r
+               this.#locked = true\r
                return true\r
        }\r
 \r
@@ -248,8 +249,8 @@ export abstract class Wallet {
                                name: this.id,\r
                                password\r
                        }))[0]\r
-                       const { id, mnemonic, seed } = response.result\r
-                       if (id !== this.id) {\r
+                       const { id, mnemonic, seed } = response?.result\r
+                       if (id == null || id !== this.id) {\r
                                throw null\r
                        }\r
                        if (mnemonic != null) {\r
@@ -263,12 +264,12 @@ export abstract class Wallet {
                                promises.push(account.unlock(password))\r
                        }\r
                        await Promise.all(promises)\r
-                       this.#locked = false\r
                } catch (err) {\r
                        throw new Error('Failed to unlock wallet')\r
                } finally {\r
                        password.fill(0)\r
                }\r
+               this.#locked = false\r
                return true\r
        }\r
 \r
index d3c21ff10365acc7a3d3a72dc115c0e2f2883c6b..5af2b60c628f7017978eacf692ee99f99c07014b 100644 (file)
@@ -46,13 +46,9 @@ export class Safe extends WorkerInterface {
                        let result
                        try {
                                const passwordBytes = obj.toBytes(password ?? [])
-                               switch (d.method) {
-                                       case 'put': {
-                                               result = await this.put(name, passwordBytes, data)
-                                               break
-                                       }
-                                       case 'overwrite': {
-                                               result = await this.overwrite(name, passwordBytes, data)
+                               switch (method) {
+                                       case 'set': {
+                                               result = await this.set(name, passwordBytes, data)
                                                break
                                        }
                                        case 'get': {
@@ -89,18 +85,11 @@ export class Safe extends WorkerInterface {
        /**
        * Encrypts data with a password byte array and stores it in the Safe.
        */
-       static async put (name: string, password: Uint8Array, data: any): Promise<boolean> {
+       static async set (name: string, password: Uint8Array, data: any): Promise<boolean> {
                if (await this.#exists(name)) {
                        password.fill(0)
                        throw new Error(this.ERR_MSG)
                }
-               return this.overwrite(name, password, data)
-       }
-
-       /**
-       * Encrypts data with a password byte array and stores it in the Safe.
-       */
-       static async overwrite (name: string, password: Uint8Array, data: any): Promise<boolean> {
                let passkey: CryptoKey
                try {
                        passkey = await globalThis.crypto.subtle.importKey('raw', password, 'PBKDF2', false, ['deriveBits', 'deriveKey'])
@@ -132,7 +121,7 @@ export class Safe extends WorkerInterface {
                                encrypted: bytes.toHex(new Uint8Array(encrypted)),
                                iv: iv.hex
                        }
-                       return await this.#put(record, name)
+                       return await this.#add(record, name)
                } catch (err) {
                        throw new Error(this.ERR_MSG)
                }
@@ -244,8 +233,8 @@ export class Safe extends WorkerInterface {
                })
        }
 
-       static async #put (record: SafeRecord, name: string): Promise<boolean> {
-               const result = await this.#transact<typeof name>('readwrite', db => db.put(record, name))
+       static async #add (record: SafeRecord, name: string): Promise<boolean> {
+               const result = await this.#transact<typeof name>('readwrite', db => db.add(record, name))
                return await this.#exists(result)
        }