]> git.codecow.com Git - libnemo.git/commitdiff
Fix password parsing.
authorChris Duncan <chris@zoso.dev>
Thu, 31 Jul 2025 23:02:56 +0000 (16:02 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 31 Jul 2025 23:02:56 +0000 (16:02 -0700)
src/lib/safe.ts

index b42baa4adc6450c08b4c7bd18bc19335ae376c26..1defc8e3c4dbed5041779b408f78206fb3eb1dd1 100644 (file)
@@ -229,10 +229,16 @@ export class Safe {
        /**
        * Decrypts the input and sets the seed and, if it is included, the mnemonic.
        */
-       static async unlock (key: CryptoKey, iv: ArrayBuffer, encrypted?: ArrayBuffer): Promise<NamedData<boolean>> {
+       static async unlock (key?: CryptoKey, iv?: ArrayBuffer, encrypted?: ArrayBuffer): Promise<NamedData<boolean>> {
                try {
+                       if (key == null) {
+                               throw new TypeError('Wallet password is required')
+                       }
+                       if (iv == null) {
+                               throw new TypeError('Wallet IV is required')
+                       }
                        if (encrypted == null) {
-                               throw new TypeError('Wallet encrypted secrets required to unlock')
+                               throw new TypeError('Wallet encrypted data is required')
                        }
                        const { seed, mnemonic } = await this.#decryptWallet(key, iv, encrypted)
                        if (!(seed instanceof ArrayBuffer)) {
@@ -362,10 +368,10 @@ export class Safe {
                const action = messageData.action
                debugger
                // Password for lock/unlock key
-               if ('password' in messageData || !(messageData.password instanceof ArrayBuffer)) {
+               if (messageData.password != null && !(messageData.password instanceof ArrayBuffer)) {
                        throw new TypeError('Password must be ArrayBuffer')
                }
-               const password: ArrayBuffer = messageData.password
+               const password = messageData.password
 
                // IV for crypto key, included if unlocking or generated if creating
                if (action === 'unlock' && !(messageData.iv instanceof ArrayBuffer)) {
@@ -384,7 +390,9 @@ export class Safe {
                        : crypto.getRandomValues(new Uint8Array(32)).buffer
 
                // CryptoKey from password, decryption key if unlocking else encryption key
-               const key = await this.#createAesKey(action === 'unlock' ? 'decrypt' : 'encrypt', password, keySalt)
+               const key = password instanceof ArrayBuffer
+                       ? await this.#createAesKey(action === 'unlock' ? 'decrypt' : 'encrypt', password, keySalt)
+                       : undefined
 
                // Type of wallet
                if (messageData.type !== undefined && messageData.type !== 'BIP-44' && messageData.type !== 'BLAKE2b') {