]> git.codecow.com Git - libnemo.git/commitdiff
Eliminate self-references.
authorChris Duncan <chris@zoso.dev>
Sun, 21 Sep 2025 22:01:20 +0000 (15:01 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 21 Sep 2025 22:01:20 +0000 (15:01 -0700)
src/lib/account/address.ts
src/lib/rolodex.ts
src/lib/wallet/index.ts

index 26a7fd37000afed76722583caa5ce0f425757a77..ebe1c2de396efad9295c2535027bf60423675179 100644 (file)
@@ -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 })
                }
index f3de8952608a5dee352e56ace60505e12bed1da6..a20f4de286fe1df832d9b7d81996f3b2435dab5f 100644 (file)
@@ -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<boolean> {
                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<string[]> {
                try {
-                       const records = await Database.get<NamedData<string[]>>(name, Rolodex.DB_NAME)
+                       const records = await Database.get<NamedData<string[]>>(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<string[]> {
                try {
-                       const records = await Database.getAll<NamedData>(Rolodex.DB_NAME)
+                       const records = await Database.getAll<NamedData>(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<string | null> {
                try {
-                       const records = await Database.get<NamedData<string>>(address, Rolodex.DB_NAME)
+                       const records = await Database.get<NamedData<string>>(address, this.DB_NAME)
                        const record = records[address]
                        return record?.name ?? null
                } catch (err) {
index e3bf4104d1d8f9239ff42205cc584344ea8f9b1e..4e7a021db202238f3ec3acd82ba207ccb5104750 100644 (file)
@@ -33,6 +33,12 @@ export type WalletType = 'BIP-44' | 'BLAKE2b' | 'Ledger'
 * three types of wallets are supported: BIP-44, BLAKE2b, and Ledger.\r
 */\r
 export class Wallet {\r
+       static #isInternal: boolean = false\r
+\r
+       /**\r
+       * @returns {boolean}\r
+       */\r
+       static get isInternal (): boolean { return this.#isInternal }\r
        /**\r
        * @returns {'Wallet'}\r
        */\r
@@ -66,8 +72,9 @@ export class Wallet {
        */\r
        static async create (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicSalt?: string): Promise<Wallet>\r
        static async create (type: WalletType, password?: string, mnemonicSalt?: string): Promise<Wallet> {\r
-               Wallet.#isInternal = true\r
+               this.#isInternal = true\r
                const self = new this(type)\r
+               this.#isInternal = false\r
                { ({ mnemonic: self.#mnemonic, seed: self.#seed } = await _create(self, self.#vault, password, mnemonicSalt)) }\r
                return self\r
        }\r
@@ -94,8 +101,9 @@ export class Wallet {
        */\r
        static async load (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicPhrase: string, mnemonicSalt?: string): Promise<Wallet>\r
        static async load (type: WalletType, password: string, secret: string, mnemonicSalt?: string): Promise<Wallet> {\r
-               Wallet.#isInternal = true\r
+               this.#isInternal = true\r
                const self = new this(type)\r
+               this.#isInternal = false\r
                await _load(self, self.#vault, password, secret, mnemonicSalt)\r
                return self\r
        }\r
@@ -117,21 +125,22 @@ export class Wallet {
        static async restore (id?: string): Promise<Wallet | Wallet[]> {\r
                const backups = await _restore(id)\r
                const wallets = backups.map(backup => {\r
-                       Wallet.#isInternal = true\r
-                       return new this(backup.type, backup.id)\r
+                       this.#isInternal = true\r
+                       const self = new this(backup.type, backup.id)\r
+                       this.#isInternal = false\r
+                       return self\r
                })\r
                return typeof id === 'string' ? wallets[0] : wallets\r
        }\r
 \r
        constructor (type: WalletType, id?: string)\r
        constructor (type: unknown, id?: string) {\r
-               if (!Wallet.#isInternal) {\r
+               if (!(this.constructor as typeof Wallet).isInternal) {\r
                        throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`)\r
                }\r
                if (type !== 'BIP-44' && type !== 'BLAKE2b' && type !== 'Ledger') {\r
                        throw new TypeError('Invalid wallet type', { cause: type })\r
                }\r
-               Wallet.#isInternal = false\r
                this.#accounts = new Map<number, Account>()\r
                this.#id = id ?? crypto.randomUUID()\r
                this.#type = type\r
@@ -396,7 +405,6 @@ export class Wallet {
                return await _verify(this.type, this.#vault, secret)\r
        }\r
 \r
-       static #isInternal: boolean = false\r
        #accounts: Map<number, Account>\r
        #id: string\r
        #mnemonic?: ArrayBuffer\r