From: Chris Duncan Date: Wed, 10 Sep 2025 06:15:23 +0000 (-0700) Subject: Implement vault timer config in wallet. X-Git-Tag: v0.10.5~22^2~2 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=af5af68d4c030329e0e88aa97a295407cbe8654e;p=libnemo.git Implement vault timer config in wallet. --- diff --git a/src/lib/wallet/config.ts b/src/lib/wallet/config.ts new file mode 100644 index 0000000..2a5c34e --- /dev/null +++ b/src/lib/wallet/config.ts @@ -0,0 +1,26 @@ +//! SPDX-FileCopyrightText: 2025 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later + +import { Vault } from '../vault' +import { Wallet } from '../wallet' + +export async function _config (wallet: Wallet, vault: Vault, settings: { timeout: number }): Promise +export async function _config (wallet: Wallet, vault: Vault, settings: unknown): Promise { + try { + if (settings == null || typeof settings !== 'object') { + throw new TypeError('Invalid configuration settings') + } + const { timeout } = settings as { [key: string]: unknown } + if (typeof timeout !== 'number') { + throw new TypeError('Timeout must be number', { cause: timeout }) + } + if (wallet.type === 'BIP-44' || wallet.type === 'BLAKE2b') { + await vault.request({ + action: 'config', + timeout + }) + } + } catch (err) { + throw new Error('Failed to lock wallet', { cause: err }) + } +} diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index 751e47d..0b6b45b 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -10,6 +10,7 @@ import { Rpc } from '../rpc' import { Vault } from '../vault' import { _accounts } from './accounts' import { _backup } from './backup' +import { _config } from './config' import { _create } from './create' import { _destroy } from './destroy' import { _get } from './get' @@ -245,6 +246,14 @@ export class Wallet { return await _accounts(this.type, this.#accounts, this.#vault, from, to) } + /** + * Configures vault worker settings. + * @param {number} timeout - Measured in seconds of inactivity before wallet automatically locks + */ + async config (settings: { timeout: number }): Promise { + return await _config(this, this.#vault, settings) + } + /** * Removes encrypted secrets in storage, releases variable references to * allow garbage collection, and terminates vault worker. diff --git a/src/types.d.ts b/src/types.d.ts index 4d2d321..8c1d490 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -741,6 +741,13 @@ export declare class Wallet { */ accounts (from?: number, to?: number): Promise> /** + * Configures vault worker settings. + * @param {number} timeout - Measured in seconds of inactivity before wallet automatically locks + */ + config (settings: { + timeout: number + }): Promise + /** * Removes encrypted secrets in storage, releases variable references to * allow garbage collection, and terminates vault worker. */ diff --git a/test/test.lock-unlock.mjs b/test/test.lock-unlock.mjs index 1618ad9..31a05e1 100644 --- a/test/test.lock-unlock.mjs +++ b/test/test.lock-unlock.mjs @@ -182,5 +182,34 @@ await Promise.all([ }, 60000) }) }) + + await test('configure autolock', { skip: false }, async () => { + const wallet = await Wallet.load('BIP-44', NANO_TEST_VECTORS.PASSWORD, NANO_TEST_VECTORS.MNEMONIC, NANO_TEST_VECTORS.PASSWORD) + assert.equal(wallet.isLocked, true) + await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) + assert.equal(wallet.isLocked, false) + await wallet.config({ timeout: 5 }) + + await new Promise(async (resolve) => { + console.log('Waiting 3 seconds...') + setTimeout(async () => { + // should still be unlocked + assert.equal(wallet.isLocked, false) + assert.ok(await wallet.verify(NANO_TEST_VECTORS.MNEMONIC)) + resolve(null) + }, 3000) + }) + + await new Promise(async (resolve) => { + console.log('Timer should not be reset by verify, waiting 3 more seconds...') + setTimeout(async () => { + // should be locked from account() reset and not reset by verify() + assert.equal(wallet.isLocked, true) + await assert.rejects(wallet.verify(NANO_TEST_VECTORS.MNEMONIC)) + await assert.resolves(wallet.destroy()) + resolve(null) + }, 3000) + }) + }) }) ])