]> git.codecow.com Git - libnemo.git/commitdiff
Add update password method to Safe and fix seed type checking.
authorChris Duncan <chris@zoso.dev>
Fri, 1 Aug 2025 21:44:05 +0000 (14:44 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 1 Aug 2025 21:44:05 +0000 (14:44 -0700)
src/lib/safe.ts

index 16795544a5dda68c5f395455403df21a15c3c114..cff835a56f8e647369bb51a9f1f6d155e511df04 100644 (file)
@@ -74,6 +74,10 @@ export class Safe {
                                                result = await this.unlock(key, iv, encrypted)
                                                break
                                        }
+                                       case 'update': {
+                                               result = await this.update(key, keySalt)
+                                               break
+                                       }
                                        case 'verify': {
                                                result = await this.verify(seed, mnemonicPhrase)
                                                break
@@ -264,6 +268,28 @@ export class Safe {
                }
        }
 
+       /**
+       * Decrypts the input and sets the seed and, if it is included, the mnemonic.
+       */
+       static async update (key?: CryptoKey, keySalt?: ArrayBuffer): Promise<NamedData<ArrayBuffer>> {
+               try {
+                       if (this.#locked) {
+                               throw new Error('Wallet is locked')
+                       }
+                       if (this.#seed == null) {
+                               throw new Error('Wallet seed not found')
+                       }
+                       if (key == null || keySalt == null) {
+                               throw new TypeError('Wallet password is required')
+                       }
+                       const { iv, encrypted } = await this.#encryptWallet(key)
+                       return { iv, salt: keySalt, encrypted }
+               } catch (err) {
+                       console.error(err)
+                       throw new Error('Failed to update wallet password', { cause: err })
+               }
+       }
+
        /**
        * Checks the seed and, if it exists, the mnemonic against input. The wallet
        * must be unlocked prior to verification.
@@ -414,7 +440,7 @@ export class Safe {
                }
 
                // Seed to import
-               if (action === 'import' && !(messageData.seed instanceof ArrayBuffer)) {
+               if (action === 'import' && 'seed' in message && !(messageData.seed instanceof ArrayBuffer)) {
                        throw new TypeError('Seed required to import wallet')
                }
                const seed = messageData.seed instanceof ArrayBuffer