From: Chris Duncan Date: Wed, 9 Jul 2025 17:39:56 +0000 (-0700) Subject: Use instanceof instead of constructor name when validating. X-Git-Tag: v0.10.5~81 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=f1b9a9855181fdfe372631ca44a3b7794080e59d;p=libnemo.git Use instanceof instead of constructor name when validating. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index c304535..5a641d3 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -48,7 +48,7 @@ export class Account { set frontier (v) { this.#frontier = v } set receivable (v) { this.#receivable = v ? BigInt(v) : undefined } set representative (v) { - if (v?.constructor === Account) { + if (v instanceof Account) { this.#representative = v } else if (typeof v === 'string') { this.#representative = Account.fromAddress(v) @@ -195,10 +195,10 @@ export class Account { * @param {Rpc|string|URL} rpc - RPC node information required to call `account_info` */ async refresh (rpc: Rpc | string | URL): Promise { - 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 data = { diff --git a/src/lib/tools.ts b/src/lib/tools.ts index dda7989..c02fe61 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -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[] = [] diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index 9e63cb2..d81d3a8 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -151,7 +151,7 @@ export abstract class Wallet { if (typeof password === 'string') { password = utf8.toBytes(password) } - if (password == null || password.constructor.name !== 'Uint8Array') { + if (password == null || !(password instanceof Uint8Array)) { throw new Error('Failed to unlock wallet') } try { @@ -202,10 +202,10 @@ export abstract class Wallet { * @returns {Promise} Accounts with updated balances, frontiers, and representatives */ async refresh (rpc: Rpc | string | URL, from: number = 0, to: number = from): Promise { - 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 accounts = await this.accounts(from, to) @@ -229,7 +229,7 @@ export abstract class Wallet { if (typeof password === 'string') { password = utf8.toBytes(password) } - if (password == null || password.constructor.name !== 'Uint8Array') { + if (password == null || !(password instanceof Uint8Array)) { throw new Error('Failed to unlock wallet') } try { diff --git a/src/lib/workers/bip44-ckd.ts b/src/lib/workers/bip44-ckd.ts index 47287e0..1feff73 100644 --- a/src/lib/workers/bip44-ckd.ts +++ b/src/lib/workers/bip44-ckd.ts @@ -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) { diff --git a/test/test.refresh-accounts.mjs b/test/test.refresh-accounts.mjs index b54b6d5..4118d30 100644 --- a/test/test.refresh-accounts.mjs +++ b/test/test.refresh-accounts.mjs @@ -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, '')