From cfd2e4b7d2dbb4879d4055efde59408ed306b79a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 19 Aug 2025 12:00:06 -0700 Subject: [PATCH] Extract destroy method from wallet into separate module. Clean up imports. --- package.json | 1 + src/lib/block.ts | 4 ++-- src/lib/tools.ts | 13 +++---------- src/lib/wallet/accounts.ts | 4 ++-- src/lib/wallet/backup.ts | 2 +- src/lib/wallet/create.ts | 6 +++--- src/lib/wallet/destroy.ts | 19 +++++++++++++++++++ src/lib/wallet/get.ts | 2 +- src/lib/wallet/index.ts | 29 +++++++++++------------------ src/lib/wallet/ledger.ts | 4 ++-- src/lib/wallet/load.ts | 4 ++-- src/lib/wallet/lock.ts | 2 +- src/lib/wallet/refresh.ts | 6 ++---- src/lib/wallet/restore.ts | 1 - src/lib/wallet/sign.ts | 2 +- src/lib/wallet/unlock.ts | 6 +++--- src/lib/wallet/unopened.ts | 5 +---- src/lib/wallet/verify.ts | 4 ++-- src/main.ts | 9 ++++----- 19 files changed, 61 insertions(+), 62 deletions(-) create mode 100644 src/lib/wallet/destroy.ts diff --git a/package.json b/package.json index 5906ce9..3743a6d 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "imports": { "#crypto": "./src/lib/crypto/index.js", "#types": "./src/types.d.ts", + "#vault": "./src/lib/vault/index.js", "#wallet": "./src/lib/wallet/index.js" }, "dependencies": { diff --git a/src/lib/block.ts b/src/lib/block.ts index 37c7fb7..7381ef1 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -3,12 +3,12 @@ import { NanoPow } from 'nano-pow' import { Blake2b, NanoNaCl } from '#crypto' +import { Wallet } from '#wallet' import { Account } from './account' import { BURN_PUBLIC_KEY, PREAMBLE, DIFFICULTY_RECEIVE, DIFFICULTY_SEND, UNITS } from './constants' import { bytes, dec, hex } from './convert' import { Rpc } from './rpc' import { convert } from './tools' -import { Wallet } from '#wallet' /** * Represents a block as defined by the Nano cryptocurrency protocol. @@ -428,7 +428,7 @@ export class Block { await wallet.sign(param, this) } else if (typeof input === 'number') { const index = input - const { Ledger } = await import('./wallet/ledger') + const { Ledger } = await import('#wallet') const ledger = await Ledger.create() await ledger.connect() if (param && param instanceof Block) { diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 39d5d8d..bf8495c 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -2,21 +2,14 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Blake2b, NanoNaCl } from '#crypto' -import { Key } from '#types' +import { Key, SweepResult } from '#types' import { Wallet } from '#wallet' import { Account } from './account' import { Block } from './block' import { MAX_SUPPLY, UNITS } from './constants' import { bytes, hex } from './convert' -import { Ledger } from './wallet/ledger' import { Rpc } from './rpc' -type SweepResult = { - status: "success" | "error" - address: string - message: string -} - /** * Converts a decimal amount of nano from one unit divider to another. * @@ -124,7 +117,7 @@ export async function sign (key: Key, ...input: string[]): Promise { * them all to a single recipient address. Hardware wallets are unsupported. * * @param {Rpc|string|URL} rpc - RPC node information required to refresh accounts, calculate PoW, and process blocks -* @param {(Wallet|Ledger)} wallet - Wallet from which to sweep funds +* @param {(Wallet)} wallet - Wallet from which to sweep funds * @param {string} recipient - Destination address for all swept funds * @param {number} from - Starting account index to sweep * @param {number} to - Ending account index to sweep @@ -132,7 +125,7 @@ export async function sign (key: Key, ...input: string[]): Promise { */ export async function sweep ( rpc: Rpc | string | URL, - wallet: Wallet | Ledger, + wallet: Wallet, recipient: string, from: number = 0, to: number = from diff --git a/src/lib/wallet/accounts.ts b/src/lib/wallet/accounts.ts index 84ab89b..3aa3f96 100644 --- a/src/lib/wallet/accounts.ts +++ b/src/lib/wallet/accounts.ts @@ -1,9 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Account, AccountList } from '../account' -import { Vault } from '../vault' import { KeyPair } from '#types' +import { Vault } from '#vault' +import { Account, AccountList } from '../account' export async function _accounts (accounts: AccountList, vault: Vault, from: number, to: number): Promise export async function _accounts (accounts: AccountList, vault: Vault, from: unknown, to: unknown): Promise { diff --git a/src/lib/wallet/backup.ts b/src/lib/wallet/backup.ts index 76752eb..c668aa3 100644 --- a/src/lib/wallet/backup.ts +++ b/src/lib/wallet/backup.ts @@ -1,9 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Database } from '../database' import { NamedData } from '#types' import { Wallet } from '#wallet' +import { Database } from '../database' export async function _backup () { try { diff --git a/src/lib/wallet/create.ts b/src/lib/wallet/create.ts index a9c65be..8b83af8 100644 --- a/src/lib/wallet/create.ts +++ b/src/lib/wallet/create.ts @@ -1,12 +1,12 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later +import { NamedData } from '#types' +import { Vault } from '#vault' +import { Wallet } from '#wallet' import { utf8 } from '../convert' import { Database } from '../database' import { _load } from './load' -import { Wallet } from '.' -import { Vault } from '../vault' -import { NamedData } from '#types' export async function _create (wallet: Wallet, vault: Vault, password: string, mnemonicSalt?: string): Promise> export async function _create (wallet: Wallet, vault: Vault, password: unknown, mnemonicSalt?: unknown): Promise> { diff --git a/src/lib/wallet/destroy.ts b/src/lib/wallet/destroy.ts new file mode 100644 index 0000000..133294a --- /dev/null +++ b/src/lib/wallet/destroy.ts @@ -0,0 +1,19 @@ +//! SPDX-FileCopyrightText: 2025 Chris Duncan +//! SPDX-License-Identifier: GPL-3.0-or-later + +import { Vault } from '#vault' +import { Wallet } from '#wallet' +import { Database } from '../database' + +export async function _destroy (wallet: Wallet, vault: Vault) { + try { + vault.terminate() + const isDeleted = await Database.delete(wallet.id, Wallet.DB_NAME) + if (!isDeleted) { + throw new Error('Failed to delete wallet from database') + } + } catch (err) { + console.error(err) + throw new Error('Failed to destroy wallet', { cause: err }) + } +} diff --git a/src/lib/wallet/get.ts b/src/lib/wallet/get.ts index 9a5f1c4..b09e888 100644 --- a/src/lib/wallet/get.ts +++ b/src/lib/wallet/get.ts @@ -1,9 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Database } from '../database' import { NamedData } from '#types' import { Wallet } from '#wallet' +import { Database } from '../database' export async function _get (recordId: string) { try { diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index 6f7d202..3a32e9f 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -1,26 +1,28 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { KeyPair, NamedData, WalletType } from '#types' +import { NamedData, WalletType } from '#types' +import { Vault } from '#vault' import { Account, AccountList } from '../account' -import { _accounts } from './accounts' -import { _backup } from './backup' import { Block } from '../block' import { ADDRESS_GAP } from '../constants' -import { bytes, hex, utf8 } from '../convert' +import { bytes } from '../convert' +import { Rpc } from '../rpc' +import { _accounts } from './accounts' +import { _backup } from './backup' import { _create } from './create' -import { Database } from '../database' +import { _destroy } from './destroy' import { _get } from './get' import { _load } from './load' import { _lock } from './lock' import { _refresh } from './refresh' import { _restore } from './restore' -import { Rpc } from '../rpc' import { _sign } from './sign' import { _unlock } from './unlock' -import { Vault } from '../vault' -import { _verify } from './verify' import { _unopened } from './unopened' +import { _verify } from './verify' + +export { Ledger } from './ledger' /** * Represents a wallet containing numerous Nano accounts derived from a single @@ -233,16 +235,7 @@ export class Wallet { * allow garbage collection, and terminates vault worker. */ async destroy (): Promise { - try { - const isDeleted = await Database.delete(this.#id, Wallet.DB_NAME) - if (!isDeleted) { - throw new Error('Failed to delete wallet from database') - } - this.#vault.terminate() - } catch (err) { - console.error(err) - throw new Error('Failed to destroy wallet', { cause: err }) - } + return await _destroy(this, this.#vault) } /** diff --git a/src/lib/wallet/ledger.ts b/src/lib/wallet/ledger.ts index 54767e9..54a5a03 100644 --- a/src/lib/wallet/ledger.ts +++ b/src/lib/wallet/ledger.ts @@ -5,14 +5,14 @@ import { ledgerUSBVendorId } from '@ledgerhq/devices' import { default as TransportBLE } from '@ledgerhq/hw-transport-web-ble' import { default as TransportUSB } from '@ledgerhq/hw-transport-webusb' import { default as TransportHID } from '@ledgerhq/hw-transport-webhid' +import { DeviceStatus, LedgerAccountResponse, LedgerResponse, LedgerSignResponse, LedgerVersionResponse } from '#types' +import { Wallet } from '#wallet' import { Account, AccountList } from '../account' import { Block } from '../block' import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants' import { bytes, dec, hex } from '../convert' import { Database } from '../database' import { Rpc } from '../rpc' -import { Wallet } from '.' -import { DeviceStatus, LedgerAccountResponse, LedgerResponse, LedgerSignResponse, LedgerVersionResponse } from '#types' /** * Ledger hardware wallet created by communicating with a Ledger device via ADPU diff --git a/src/lib/wallet/load.ts b/src/lib/wallet/load.ts index 684783a..d19558b 100644 --- a/src/lib/wallet/load.ts +++ b/src/lib/wallet/load.ts @@ -3,10 +3,10 @@ import { Bip39 } from '#crypto' import { NamedData } from '#types' +import { Vault } from '#vault' +import { Wallet } from '#wallet' import { hex, utf8 } from '../convert' import { Database } from '../database' -import { Wallet } from '#wallet' -import { Vault } from '../vault' export async function _load (wallet: Wallet, vault: Vault, password: string, secret: string, mnemonicSalt?: string): Promise export async function _load (wallet: Wallet, vault: Vault, password: unknown, secret: unknown, mnemonicSalt?: unknown): Promise { diff --git a/src/lib/wallet/lock.ts b/src/lib/wallet/lock.ts index 16d9b80..75cccbb 100644 --- a/src/lib/wallet/lock.ts +++ b/src/lib/wallet/lock.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Vault } from '../vault' +import { Vault } from '#vault' export async function _lock (vault: Vault): Promise export async function _lock (vault: Vault): Promise { diff --git a/src/lib/wallet/refresh.ts b/src/lib/wallet/refresh.ts index 1c8ac68..148fc14 100644 --- a/src/lib/wallet/refresh.ts +++ b/src/lib/wallet/refresh.ts @@ -1,11 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Account, AccountList } from '../account' -import { Rpc } from '../rpc' -import { Vault } from '../vault' import { Wallet } from '#wallet' -import { KeyPair } from '#types' +import { AccountList } from '../account' +import { Rpc } from '../rpc' export async function _refresh (wallet: Wallet, rpc: Rpc | string | URL, from: number, to: number): Promise export async function _refresh (wallet: Wallet, rpc: unknown, from: unknown, to: unknown): Promise { diff --git a/src/lib/wallet/restore.ts b/src/lib/wallet/restore.ts index 849dfd5..d2e6d3a 100644 --- a/src/lib/wallet/restore.ts +++ b/src/lib/wallet/restore.ts @@ -3,7 +3,6 @@ import { _backup } from './backup' import { _get } from './get' -import { WalletType } from '#types' export async function _restore (id?: unknown) { try { diff --git a/src/lib/wallet/sign.ts b/src/lib/wallet/sign.ts index 8346401..b852aa7 100644 --- a/src/lib/wallet/sign.ts +++ b/src/lib/wallet/sign.ts @@ -1,9 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later +import { Vault } from '#vault' import { Block } from '../block' import { bytes, hex } from '../convert' -import { Vault } from '../vault' export async function _sign (vault: Vault, index: number, block: Block): Promise export async function _sign (vault: Vault, index: unknown, block: unknown): Promise { diff --git a/src/lib/wallet/unlock.ts b/src/lib/wallet/unlock.ts index 0d62925..90209ae 100644 --- a/src/lib/wallet/unlock.ts +++ b/src/lib/wallet/unlock.ts @@ -1,10 +1,10 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { _get } from './get' -import { utf8 } from '../convert' +import { Vault } from '#vault' import { Wallet } from '#wallet' -import { Vault } from '../vault' +import { utf8 } from '../convert' +import { _get } from './get' export async function _unlock (wallet: Wallet, vault: Vault, password: string): Promise export async function _unlock (wallet: Wallet, vault: Vault, password: unknown): Promise { diff --git a/src/lib/wallet/unopened.ts b/src/lib/wallet/unopened.ts index 4b0e17d..1bbcc33 100644 --- a/src/lib/wallet/unopened.ts +++ b/src/lib/wallet/unopened.ts @@ -1,12 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later +import { Wallet } from '#wallet' import { Account } from '../account' -import { hex } from '../convert' import { Rpc } from '../rpc' -import { Vault } from '../vault' -import { Wallet } from '#wallet' -import { NamedData } from '#types' export async function _unopened (wallet: Wallet, rpc: Rpc, batchSize: number, from: number): Promise export async function _unopened (wallet: Wallet, rpc: unknown, batchSize: unknown, from: unknown): Promise { diff --git a/src/lib/wallet/verify.ts b/src/lib/wallet/verify.ts index 01526b7..9fcf267 100644 --- a/src/lib/wallet/verify.ts +++ b/src/lib/wallet/verify.ts @@ -1,9 +1,9 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { hex } from '../convert' -import { Vault } from '../vault' import { NamedData } from '#types' +import { Vault } from '#vault' +import { hex } from '../convert' export async function _verify (vault: Vault, secret: string): Promise export async function _verify (vault: Vault, secret: unknown): Promise { diff --git a/src/main.ts b/src/main.ts index e94f6c9..e3746f2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,21 +2,20 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Blake2b } from '#crypto' -import { Wallet } from '#wallet' +import { Ledger, Wallet } from '#wallet' import { Account } from './lib/account' import { Block } from './lib/block' -import { Ledger } from './lib/wallet/ledger' import { Rolodex } from './lib/rolodex' import { Rpc } from './lib/rpc' import { Tools } from './lib/tools' export { - Blake2b, - Wallet, Account, + Blake2b, Block, Ledger, Rolodex, Rpc, - Tools + Tools, + Wallet } -- 2.47.3