From aea1d9f92a4e0dfba3eb4e2976155dda61a23377 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 25 Jul 2025 06:01:54 -0700 Subject: [PATCH] Add wallet types so they can be restored properly later. --- src/lib/wallets/bip44-wallet.ts | 2 +- src/lib/wallets/blake2b-wallet.ts | 2 +- src/lib/wallets/ledger-wallet.ts | 2 +- src/lib/wallets/wallet.ts | 7 +++++-- src/types.d.ts | 3 +++ 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/wallets/bip44-wallet.ts b/src/lib/wallets/bip44-wallet.ts index 1b1614e..a3dff4f 100644 --- a/src/lib/wallets/bip44-wallet.ts +++ b/src/lib/wallets/bip44-wallet.ts @@ -39,7 +39,7 @@ export class Bip44Wallet extends Wallet { throw new Error(`Bip44Wallet cannot be instantiated directly. Use 'await Bip44Wallet.create()' instead.`) } Bip44Wallet.#isInternal = false - super(id, seed, mnemonic) + super(id, 'BIP-44', seed, mnemonic) } /** diff --git a/src/lib/wallets/blake2b-wallet.ts b/src/lib/wallets/blake2b-wallet.ts index 4132cc0..9bd6f74 100644 --- a/src/lib/wallets/blake2b-wallet.ts +++ b/src/lib/wallets/blake2b-wallet.ts @@ -33,7 +33,7 @@ export class Blake2bWallet extends Wallet { throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`) } Blake2bWallet.#isInternal = false - super(id, seed, mnemonic) + super(id, 'BLAKE2b', seed, mnemonic) } /** diff --git a/src/lib/wallets/ledger-wallet.ts b/src/lib/wallets/ledger-wallet.ts index e10356b..87e25e6 100644 --- a/src/lib/wallets/ledger-wallet.ts +++ b/src/lib/wallets/ledger-wallet.ts @@ -40,7 +40,7 @@ export class LedgerWallet extends Wallet { throw new Error(`LedgerWallet cannot be instantiated directly. Use 'await LedgerWallet.create()' instead.`) } LedgerWallet.#isInternal = false - super(id) + super(id, 'Ledger') } /** diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 2a45708..010d51d 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -8,7 +8,7 @@ import { ADDRESS_GAP } from '#src/lib/constants.js' import { bytes, hex, utf8 } from '#src/lib/convert.js' import { Entropy } from '#src/lib/entropy.js' import { Rpc } from '#src/lib/rpc.js' -import { Key, KeyPair } from '#types' +import { Key, KeyPair, WalletType } from '#types' import { SafeWorker } from '#workers' /** @@ -44,6 +44,7 @@ export abstract class Wallet { #locked: boolean #m?: Bip39Mnemonic #s?: Uint8Array + #type: WalletType get id () { return this.#id.hex } get isLocked () { return this.#locked } @@ -56,8 +57,9 @@ export abstract class Wallet { if (this.#locked || this.#s == null) throw new Error('failed to get seed', { cause: 'wallet locked' }) return bytes.toHex(this.#s) } + get type () { return this.#type } - constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { + constructor (id: Entropy, type: WalletType, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) { if (this.constructor === Wallet) { throw new Error('Wallet is an abstract class and cannot be instantiated directly.') } @@ -66,6 +68,7 @@ export abstract class Wallet { this.#locked = false this.#m = mnemonic this.#s = seed + this.#type = type } /** diff --git a/src/types.d.ts b/src/types.d.ts index 6f0efeb..636e1b2 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -583,6 +583,8 @@ export declare const Tools: { verify: typeof verify } +export type WalletType = 'BIP-44' | 'BLAKE2b' | 'Ledger' + /** * Represents a wallet containing numerous Nano accounts derived from a single * source, the form of which can vary based on the type of wallet. The Wallet @@ -604,6 +606,7 @@ export declare abstract class Wallet { get isUnlocked (): boolean get mnemonic (): string get seed (): string + get type (): WalletType constructor (id: Entropy, seed?: Uint8Array, mnemonic?: Bip39Mnemonic) /** * Retrieves an account from a wallet using its child key derivation function. -- 2.47.3