]> git.codecow.com Git - libnemo.git/commitdiff
Test account lock and unlock.
authorChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 23:26:14 +0000 (16:26 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 23:26:14 +0000 (16:26 -0700)
src/lib/account.ts
test/test.lock-unlock-wallet.mjs

index f74c95059bbf882a7312c173ad9703e05d00283f..f68d6ee6a7e2259959222a14a389230f426cf658 100644 (file)
@@ -141,12 +141,23 @@ export class Account {
                        throw new Error('Failed to unlock wallet')\r
                }\r
                try {\r
+                       const data: { id: string, privateKey: string | null } = {\r
+                               id: this.#pub,\r
+                               privateKey: null\r
+                       }\r
                        if (this.#prv != null) {\r
-                               await Account.#poolSafe.assign({\r
-                                       method: 'put',\r
-                                       name: this.#prv,\r
-                                       password\r
-                               })\r
+                               data.privateKey = this.#prv\r
+                       }\r
+                       console.log(this.#prv)\r
+                       const response = (await Account.#poolSafe.assign({\r
+                               method: 'put',\r
+                               name: this.#pub,\r
+                               password,\r
+                               data\r
+                       }))[0]\r
+                       const success = response.result\r
+                       if (!success) {\r
+                               throw null\r
                        }\r
                } catch (err) {\r
                        console.error(`Failed to lock account ${this.address}`, err)\r
@@ -166,11 +177,19 @@ export class Account {
                        throw new Error('Failed to unlock wallet')\r
                }\r
                try {\r
-                       this.#prv = await Account.#poolSafe.assign({\r
+                       const response = (await Account.#poolSafe.assign({\r
                                method: 'get',\r
                                name: this.#pub,\r
                                password\r
-                       })\r
+                       }))[0]\r
+                       console.log(response)\r
+                       const { id, privateKey } = response.result\r
+                       if (id !== this.#pub) {\r
+                               throw null\r
+                       }\r
+                       if (privateKey != null) {\r
+                               this.#prv = privateKey\r
+                       }\r
                } catch (err) {\r
                        console.error(`Failed to unlock account ${this.address}`, err)\r
                        return false\r
index a22c5d980c27301976761c56a57b4758a9b98bdc..5062183074383377d17bb628a7ab3d0c0f4c2593 100644 (file)
@@ -51,6 +51,25 @@ await suite('Lock and unlock wallets', async () => {
                await wallet.destroy()\r
        })\r
 \r
+       await test('locking and unlocking a Bip44Wallet Account with a password', async () => {\r
+               const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+               const account = await wallet.account()\r
+               const lockResult = await account.lock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               assert.equals(lockResult, true)\r
+               assert.ok('privateKey' in account)\r
+               assert.nullish(account.privateKey)\r
+\r
+               const unlockResult = await account.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               assert.equals(unlockResult, true)\r
+               assert.ok('privateKey' in account)\r
+               assert.equals(account.privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r
+\r
+               await wallet.destroy()\r
+       })\r
+\r
        await test('fail to unlock a Bip44Wallet with different passwords', async () => {\r
                const wallet = await Bip44Wallet.fromMnemonic(NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD)\r
                await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
@@ -185,6 +204,25 @@ await suite('Lock and unlock wallets', async () => {
                await wallet.destroy()\r
        })\r
 \r
+       await test('locking and unlocking a Blake2bWallet Account with a password', async () => {\r
+               const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_0)\r
+               await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+               const account = await wallet.account()\r
+               const lockResult = await account.lock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               assert.equals(lockResult, true)\r
+               assert.ok('privateKey' in account)\r
+               assert.nullish(account.privateKey)\r
+\r
+               const unlockResult = await account.unlock(NANO_TEST_VECTORS.PASSWORD)\r
+\r
+               assert.equals(unlockResult, true)\r
+               assert.ok('privateKey' in account)\r
+               assert.equals(account.privateKey, NANO_TEST_VECTORS.PRIVATE_0)\r
+\r
+               await wallet.destroy()\r
+       })\r
+\r
        await test('fail to unlock a Blake2bWallet with different passwords', async () => {\r
                const wallet = await Blake2bWallet.fromSeed(NANO_TEST_VECTORS.PASSWORD, TREZOR_TEST_VECTORS.ENTROPY_1)\r
 \r