]> git.codecow.com Git - libnemo.git/commitdiff
Use instanceof instead of constructor name when validating.
authorChris Duncan <chris@zoso.dev>
Wed, 9 Jul 2025 17:39:56 +0000 (10:39 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 9 Jul 2025 17:39:56 +0000 (10:39 -0700)
src/lib/account.ts
src/lib/tools.ts
src/lib/wallets/wallet.ts
src/lib/workers/bip44-ckd.ts
test/test.refresh-accounts.mjs

index c3045351cca57c5cf489ada59f719f037c2065e2..5a641d33cdb36e860c5baa40f08a3d5f1efdecb9 100644 (file)
@@ -48,7 +48,7 @@ export class Account {
        set frontier (v) { this.#frontier = v }\r
        set receivable (v) { this.#receivable = v ? BigInt(v) : undefined }\r
        set representative (v) {\r
-               if (v?.constructor === Account) {\r
+               if (v instanceof Account) {\r
                        this.#representative = v\r
                } else if (typeof v === 'string') {\r
                        this.#representative = Account.fromAddress(v)\r
@@ -195,10 +195,10 @@ export class Account {
        * @param {Rpc|string|URL} rpc - RPC node information required to call `account_info`\r
        */\r
        async refresh (rpc: Rpc | string | URL): Promise<void> {\r
-               if (typeof rpc === 'string' || rpc.constructor === URL) {\r
+               if (typeof rpc === 'string' || rpc instanceof URL) {\r
                        rpc = new Rpc(rpc)\r
                }\r
-               if (rpc.constructor !== Rpc) {\r
+               if (!(rpc instanceof Rpc)) {\r
                        throw new TypeError('RPC must be a valid node')\r
                }\r
                const data = {\r
index dda79890727b151f11e494529541c43033c1931d..c02fe61a6e3ca265d01ec9bb6c806bab55a36994 100644 (file)
@@ -115,10 +115,10 @@ export async function sweep (
        if (rpc == null || wallet == null || recipient == null) {
                throw new ReferenceError('Missing required sweep arguments')
        }
-       if (typeof rpc === 'string' || rpc.constructor === URL) {
+       if (typeof rpc === 'string' || rpc instanceof URL) {
                rpc = new Rpc(rpc)
        }
-       if (rpc.constructor !== Rpc) {
+       if (!(rpc instanceof Rpc)) {
                throw new TypeError('RPC must be a valid node')
        }
        const blockQueue: Promise<void>[] = []
index 9e63cb218cd0e328ab6b5870f8ee7229d20eccef..d81d3a801ed27ab16dd6b02fc2cf18ae79d9569a 100644 (file)
@@ -151,7 +151,7 @@ export abstract class Wallet {
                if (typeof password === 'string') {\r
                        password = utf8.toBytes(password)\r
                }\r
-               if (password == null || password.constructor.name !== 'Uint8Array') {\r
+               if (password == null || !(password instanceof Uint8Array)) {\r
                        throw new Error('Failed to unlock wallet')\r
                }\r
                try {\r
@@ -202,10 +202,10 @@ export abstract class Wallet {
        * @returns {Promise<Account[]>} Accounts with updated balances, frontiers, and representatives\r
        */\r
        async refresh (rpc: Rpc | string | URL, from: number = 0, to: number = from): Promise<AccountList> {\r
-               if (typeof rpc === 'string' || rpc.constructor === URL) {\r
+               if (typeof rpc === 'string' || rpc instanceof URL) {\r
                        rpc = new Rpc(rpc)\r
                }\r
-               if (rpc.constructor !== Rpc) {\r
+               if (!(rpc instanceof Rpc)) {\r
                        throw new TypeError('RPC must be a valid node')\r
                }\r
                const accounts = await this.accounts(from, to)\r
@@ -229,7 +229,7 @@ export abstract class Wallet {
                if (typeof password === 'string') {\r
                        password = utf8.toBytes(password)\r
                }\r
-               if (password == null || password.constructor.name !== 'Uint8Array') {\r
+               if (password == null || !(password instanceof Uint8Array)) {\r
                        throw new Error('Failed to unlock wallet')\r
                }\r
                try {\r
index 47287e0dda190af7b7c34ffac6d2b5fbe9968910..1feff73143099cb44658831218a18e074e921f6f 100644 (file)
@@ -111,7 +111,7 @@ export class Bip44Ckd extends WorkerInterface {
        }
 
        static ser256 (integer: DataView): Uint8Array {
-               if (integer.constructor !== DataView) {
+               if (!(integer instanceof DataView)) {
                        throw new TypeError(`Expected DataView, received ${typeof integer}`)
                }
                if (integer.byteLength > 32) {
index b54b6d57cd55ddced0fb542dc1b244574fff23e5..4118d306fcd1b631d180700eb73ae1883875917f 100644 (file)
@@ -17,19 +17,19 @@ await suite('refreshing account info', { skip: true }, async () => {
                const account = await wallet.account()
                await account.refresh(rpc)
 
-               assert.equals(typeof account.balance, 'bigint')
                assert.exists(account.balance)
                assert.notEqual(account.balance, '')
-               assert.notEqual(account.balance && account.balance < 0, true)
+               assert.equals(typeof account.balance, 'bigint')
+               assert.ok(account.balance >= 0)
 
                assert.exists(account.frontier)
                assert.equals(typeof account.frontier, 'string')
                assert.notEqual(account.frontier, '')
                assert.ok(/^[A-Fa-f0-9]{64}$/.test(account.frontier))
 
-               assert.equals(account.representative && account.representative.constructor, Account)
                assert.exists(account.representative)
                assert.notEqual(account.representative, '')
+               assert.equals(account.representative.constructor, Account)
                assert.exists(account.representative?.address)
                assert.notEqual(account.representative?.address, '')