From e0eb1cdbe39e9e4f47e673b49b0d65c0968c5ca5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 2 Sep 2017 21:29:38 -0700 Subject: [PATCH] refactor: remove all duck typing. --- lib/blockchain/chainentry.js | 4 +--- lib/hd/mnemonic.js | 4 +--- lib/hd/private.js | 5 +---- lib/hd/public.js | 5 +---- lib/primitives/block.js | 4 +--- lib/primitives/coin.js | 5 +---- lib/primitives/headers.js | 5 +---- lib/primitives/keyring.js | 4 +--- lib/primitives/memblock.js | 5 +---- lib/primitives/merkleblock.js | 4 +--- lib/primitives/txmeta.js | 5 +---- lib/protocol/network.js | 4 +--- lib/wallet/account.js | 4 +--- lib/wallet/masterkey.js | 4 +--- lib/wallet/wallet.js | 4 +--- lib/wallet/walletkey.js | 5 +---- 16 files changed, 16 insertions(+), 55 deletions(-) diff --git a/lib/blockchain/chainentry.js b/lib/blockchain/chainentry.js index b18d0e3f..43b54986 100644 --- a/lib/blockchain/chainentry.js +++ b/lib/blockchain/chainentry.js @@ -538,9 +538,7 @@ ChainEntry.prototype.inspect = function inspect() { */ ChainEntry.isChainEntry = function isChainEntry(obj) { - return obj - && BN.isBN(obj.chainwork) - && typeof obj.getMedianTime === 'function'; + return obj instanceof ChainEntry; }; /* diff --git a/lib/hd/mnemonic.js b/lib/hd/mnemonic.js index 4b14c952..49101e91 100644 --- a/lib/hd/mnemonic.js +++ b/lib/hd/mnemonic.js @@ -554,9 +554,7 @@ Mnemonic.prototype.inspect = function inspect() { */ Mnemonic.isMnemonic = function isMnemonic(obj) { - return obj - && typeof obj.bits === 'number' - && typeof obj.toSeed === 'function'; + return obj instanceof Mnemonic; }; /** diff --git a/lib/hd/private.js b/lib/hd/private.js index 3fce4a74..3dfbeff2 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -742,10 +742,7 @@ HDPrivateKey.fromJSON = function fromJSON(json, network) { */ HDPrivateKey.isHDPrivateKey = function isHDPrivateKey(obj) { - return obj - && typeof obj.derive === 'function' - && typeof obj.fromMnemonic === 'function' - && Buffer.isBuffer(obj.chainCode); + return obj instanceof HDPrivateKey; }; /* diff --git a/lib/hd/public.js b/lib/hd/public.js index b8f1fc84..b93feebd 100644 --- a/lib/hd/public.js +++ b/lib/hd/public.js @@ -561,10 +561,7 @@ HDPublicKey.fromRaw = function fromRaw(data, network) { */ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { - return obj - && typeof obj.derive === 'function' - && obj.fromMnemonic === undefined - && Buffer.isBuffer(obj.chainCode); + return obj instanceof HDPublicKey; }; /* diff --git a/lib/primitives/block.js b/lib/primitives/block.js index 7e20bcb4..ae9c8918 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -818,9 +818,7 @@ Block.prototype.getWitnessSizes = function getWitnessSizes() { */ Block.isBlock = function isBlock(obj) { - return obj - && typeof obj.merkleRoot === 'string' - && typeof obj.getClaimed === 'function'; + return obj instanceof Block; }; /* diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index f4c5101a..f77bb859 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -424,10 +424,7 @@ Coin.fromTX = function fromTX(tx, index, height) { */ Coin.isCoin = function isCoin(obj) { - return obj - && typeof obj.version === 'number' - && typeof obj.script === 'object' - && typeof obj.getDepth === 'function'; + return obj instanceof Coin; }; /* diff --git a/lib/primitives/headers.js b/lib/primitives/headers.js index ecbda889..a8f3587a 100644 --- a/lib/primitives/headers.js +++ b/lib/primitives/headers.js @@ -266,10 +266,7 @@ Headers.prototype.format = function format(view, height) { */ Headers.isHeaders = function isHeaders(obj) { - return obj - && !obj.txs - && typeof obj.toHead === 'function' - && typeof obj.toBlock !== 'function'; + return obj instanceof Headers; }; /* diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index b7d9830b..7e8141f1 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -950,9 +950,7 @@ KeyRing.fromRaw = function fromRaw(data) { */ KeyRing.isKeyRing = function isKeyRing(obj) { - return obj - && Buffer.isBuffer(obj.publicKey) - && typeof obj.toSecret === 'function'; + return obj instanceof KeyRing; }; /* diff --git a/lib/primitives/memblock.js b/lib/primitives/memblock.js index fb91ec68..075b7610 100644 --- a/lib/primitives/memblock.js +++ b/lib/primitives/memblock.js @@ -211,10 +211,7 @@ MemBlock.prototype.toHeaders = function toHeaders() { */ MemBlock.isMemBlock = function isMemBlock(obj) { - return obj - && typeof obj.toBlock === 'function' - && typeof obj.isMemory === 'function' - && obj.isMemory(); + return obj instanceof MemBlock; }; /* diff --git a/lib/primitives/merkleblock.js b/lib/primitives/merkleblock.js index 5ff6b7d9..73731349 100644 --- a/lib/primitives/merkleblock.js +++ b/lib/primitives/merkleblock.js @@ -637,9 +637,7 @@ MerkleBlock.fromMatches = function fromMatches(block, matches) { */ MerkleBlock.isMerkleBlock = function isMerkleBlock(obj) { - return obj - && Buffer.isBuffer(obj.flags) - && typeof obj.verifyBody === 'function'; + return obj instanceof MerkleBlock; }; /** diff --git a/lib/primitives/txmeta.js b/lib/primitives/txmeta.js index aed439bb..ec6e7f7f 100644 --- a/lib/primitives/txmeta.js +++ b/lib/primitives/txmeta.js @@ -292,10 +292,7 @@ TXMeta.fromRaw = function fromRaw(data, enc) { */ TXMeta.isTXMeta = function isTXMeta(obj) { - return obj - && Array.isArray(obj.inputs) - && typeof obj.locktime === 'number' - && typeof obj.mtime === 'number'; + return obj instanceof TXMeta; }; /* diff --git a/lib/protocol/network.js b/lib/protocol/network.js index 88599b43..33c97e5a 100644 --- a/lib/protocol/network.js +++ b/lib/protocol/network.js @@ -367,9 +367,7 @@ Network.prototype.inspect = function inspect() { */ Network.isNetwork = function isNetwork(obj) { - return obj - && typeof obj.genesisBlock === 'string' - && typeof obj.pow === 'object'; + return obj instanceof Network; }; /* diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 024d14f9..4d7dd7ed 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -994,9 +994,7 @@ Account.fromRaw = function fromRaw(db, data) { */ Account.isAccount = function isAccount(obj) { - return obj - && typeof obj.receiveDepth === 'number' - && obj.deriveKey === 'function'; + return obj instanceof Account; }; /* diff --git a/lib/wallet/masterkey.js b/lib/wallet/masterkey.js index ed780e48..373da1a7 100644 --- a/lib/wallet/masterkey.js +++ b/lib/wallet/masterkey.js @@ -692,9 +692,7 @@ MasterKey.prototype.inspect = function inspect() { */ MasterKey.isMasterKey = function isMasterKey(obj) { - return obj - && typeof obj.encrypted === 'boolean' - && typeof obj.decrypt === 'function'; + return obj instanceof MasterKey; }; /* diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 9f39edd3..4fd165f8 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -2584,9 +2584,7 @@ Wallet.fromRaw = function fromRaw(db, data) { */ Wallet.isWallet = function isWallet(obj) { - return obj - && typeof obj.accountDepth === 'number' - && obj.template === 'function'; + return obj instanceof Wallet; }; /* diff --git a/lib/wallet/walletkey.js b/lib/wallet/walletkey.js index 92152339..73381e54 100644 --- a/lib/wallet/walletkey.js +++ b/lib/wallet/walletkey.js @@ -294,10 +294,7 @@ WalletKey.prototype.toPath = function toPath() { */ WalletKey.isWalletKey = function isWalletKey(obj) { - return obj - && Buffer.isBuffer(obj.publicKey) - && typeof obj.index === 'number' - && typeof obj.toPath === 'function'; + return obj instanceof WalletKey; }; /*