From: Chris Duncan Date: Sun, 21 Sep 2025 22:01:20 +0000 (-0700) Subject: Eliminate self-references. X-Git-Tag: v0.10.5~12^2~26 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=aefc221f677622ebfaea8222b8c4b69aa064a592;p=libnemo.git Eliminate self-references. --- diff --git a/src/lib/account/address.ts b/src/lib/account/address.ts index 26a7fd3..ebe1c2d 100644 --- a/src/lib/account/address.ts +++ b/src/lib/account/address.ts @@ -29,7 +29,7 @@ export class Address { } if (typeof value === 'string') { if (RegExp(`^[A-F0-9]{${ACCOUNT_KEY_HEX_LENGTH}}$`, 'i').test(value)) { - this.#address = Address.fromPublicKey(hex.toBuffer(value)) + this.#address = (this.constructor as typeof Address).fromPublicKey(hex.toBuffer(value)) } else if (RegExp(`^(${PREFIX}|${PREFIX_LEGACY})[13][${ALPHABET}]{59}$`).test(value)) { this.#address = value } else { @@ -37,7 +37,7 @@ export class Address { } } else if (value instanceof ArrayBuffer || value instanceof Uint8Array) { const v = new Uint8Array(value) - this.#address = Address.fromPublicKey(v) + this.#address = (this.constructor as typeof Address).fromPublicKey(v) } else { throw new TypeError('Invalid address input', { cause: value }) } diff --git a/src/lib/rolodex.ts b/src/lib/rolodex.ts index f3de895..a20f4de 100644 --- a/src/lib/rolodex.ts +++ b/src/lib/rolodex.ts @@ -76,7 +76,7 @@ export class Rolodex { id: name, addresses } - const results = await Database.put(data, Rolodex.DB_NAME) + const results = await Database.put(data, this.DB_NAME) if (results.length !== Object.keys(data).length) { throw new Error('Unexpected results from adding address', { cause: results }) } @@ -104,11 +104,11 @@ export class Rolodex { addresses } } - const isUpdated = await Database.put(data, Rolodex.DB_NAME) + const isUpdated = await Database.put(data, this.DB_NAME) if (!isUpdated) { throw new Error('failed to remove address from existing name') } - const isDeleted = await Database.delete(address, Rolodex.DB_NAME) + const isDeleted = await Database.delete(address, this.DB_NAME) return isDeleted } @@ -121,7 +121,7 @@ export class Rolodex { static async deleteName (name: string): Promise { const records = await this.getAddresses(name) records.push(name) - return await Database.delete(records, Rolodex.DB_NAME) + return await Database.delete(records, this.DB_NAME) } /** @@ -132,7 +132,7 @@ export class Rolodex { */ static async getAddresses (name: string): Promise { try { - const records = await Database.get>(name, Rolodex.DB_NAME) + const records = await Database.get>(name, this.DB_NAME) const record = records[name] return record?.addresses ? record.addresses.sort() @@ -150,7 +150,7 @@ export class Rolodex { */ static async getAllNames (): Promise { try { - const records = await Database.getAll(Rolodex.DB_NAME) + const records = await Database.getAll(this.DB_NAME) return Object.keys(records).filter(v => v.slice(0, 5) !== 'nano_') } catch (err) { console.error(err) @@ -166,7 +166,7 @@ export class Rolodex { */ static async getName (address: string): Promise { try { - const records = await Database.get>(address, Rolodex.DB_NAME) + const records = await Database.get>(address, this.DB_NAME) const record = records[address] return record?.name ?? null } catch (err) { diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index e3bf410..4e7a021 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -33,6 +33,12 @@ export type WalletType = 'BIP-44' | 'BLAKE2b' | 'Ledger' * three types of wallets are supported: BIP-44, BLAKE2b, and Ledger. */ export class Wallet { + static #isInternal: boolean = false + + /** + * @returns {boolean} + */ + static get isInternal (): boolean { return this.#isInternal } /** * @returns {'Wallet'} */ @@ -66,8 +72,9 @@ export class Wallet { */ static async create (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicSalt?: string): Promise static async create (type: WalletType, password?: string, mnemonicSalt?: string): Promise { - Wallet.#isInternal = true + this.#isInternal = true const self = new this(type) + this.#isInternal = false { ({ mnemonic: self.#mnemonic, seed: self.#seed } = await _create(self, self.#vault, password, mnemonicSalt)) } return self } @@ -94,8 +101,9 @@ export class Wallet { */ static async load (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicPhrase: string, mnemonicSalt?: string): Promise static async load (type: WalletType, password: string, secret: string, mnemonicSalt?: string): Promise { - Wallet.#isInternal = true + this.#isInternal = true const self = new this(type) + this.#isInternal = false await _load(self, self.#vault, password, secret, mnemonicSalt) return self } @@ -117,21 +125,22 @@ export class Wallet { static async restore (id?: string): Promise { const backups = await _restore(id) const wallets = backups.map(backup => { - Wallet.#isInternal = true - return new this(backup.type, backup.id) + this.#isInternal = true + const self = new this(backup.type, backup.id) + this.#isInternal = false + return self }) return typeof id === 'string' ? wallets[0] : wallets } constructor (type: WalletType, id?: string) constructor (type: unknown, id?: string) { - if (!Wallet.#isInternal) { + if (!(this.constructor as typeof Wallet).isInternal) { throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`) } if (type !== 'BIP-44' && type !== 'BLAKE2b' && type !== 'Ledger') { throw new TypeError('Invalid wallet type', { cause: type }) } - Wallet.#isInternal = false this.#accounts = new Map() this.#id = id ?? crypto.randomUUID() this.#type = type @@ -396,7 +405,6 @@ export class Wallet { return await _verify(this.type, this.#vault, secret) } - static #isInternal: boolean = false #accounts: Map #id: string #mnemonic?: ArrayBuffer