Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat(src/coins): add isSameCoin(Network, Network)
Browse files Browse the repository at this point in the history
This methods makes it easier to check whether two Networks are for the
same coin (have the same mainnet)

Issue: BG-16466
  • Loading branch information
OttoAllmendinger committed Jan 9, 2020
1 parent 69d0244 commit e1dd2cb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
44 changes: 30 additions & 14 deletions src/coins.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const typeforce = require('typeforce')
const networks = require('./networks')

/**
* @param network
* @param {Network} network
* @returns {string} the name of the network. Returns undefined if network is not a value
* of `networks`
*/
Expand All @@ -13,7 +13,7 @@ function getNetworkName (network) {
}

/**
* @param network
* @param {Network} network
* @returns {Object} the mainnet corresponding to a testnet
*/
function getMainnet (network) {
Expand Down Expand Up @@ -50,31 +50,46 @@ function getMainnet (network) {
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is a mainnet
*/
function isMainnet (network) {
return getMainnet(network) === network
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is a testnet
*/
function isTestnet (network) {
return getMainnet(network) !== network
}

/**
*
* @param {Network} network
* @param {Network} otherNetwork
* @returns {boolean} true iff both networks are for the same coin
*/
function isSameCoin (network, otherNetwork) {
return getMainnet(network) === getMainnet(otherNetwork)
}

const networksArray = Object.keys(networks).map(name => networks[name])
const mainnets = networksArray.filter(isMainnet)
const testnets = networksArray.filter(isTestnet)

/**
* Map where keys are mainnet networks and values are testnet networks
* @type {Map<Network, Network[]>}
*/
const mainnetTestnetPairs = new Map(
mainnets.map(m => [m, testnets.filter(t => getMainnet(t) === m)])
)

/**
* @param network
* @returns {Object|undefined} - The testnet corresponding to a mainnet.
* @param {Network} network
* @returns {Network|undefined} - The testnet corresponding to a mainnet.
* Returns undefined if a network has no testnet.
*/
function getTestnet (network) {
Expand All @@ -95,63 +110,63 @@ function getTestnet (network) {
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network bitcoin or testnet
*/
function isBitcoin (network) {
return getMainnet(network) === networks.bitcoin
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is bitcoincash or bitcoincashTestnet
*/
function isBitcoinCash (network) {
return getMainnet(network) === networks.bitcoincash
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is bitcoingold
*/
function isBitcoinGold (network) {
return getMainnet(network) === networks.bitcoingold
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is bitcoinsv or bitcoinsvTestnet
*/
function isBitcoinSV (network) {
return getMainnet(network) === networks.bitcoinsv
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is dash or dashTest
*/
function isDash (network) {
return getMainnet(network) === networks.dash
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is litecoin or litecoinTest
*/
function isLitecoin (network) {
return getMainnet(network) === networks.litecoin
}

/**
* @param network
* @param {Network} network
* @returns {boolean} true iff network is zcash or zcashTest
*/
function isZcash (network) {
return getMainnet(network) === networks.zcash
}

/**
* @param network
* @param {Network} network
* @returns {boolean} returns true iff network is any of the network stated in the argument
*/
const isValidNetwork = typeforce.oneOf(
Expand Down Expand Up @@ -179,6 +194,7 @@ module.exports = {
isMainnet,
getTestnet,
isTestnet,
isSameCoin,

isBitcoin,
isBitcoinCash,
Expand Down
17 changes: 16 additions & 1 deletion test/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,22 @@ describe('networks', function () {
})

for (const otherName in networks) {
if (name === otherName) {
const otherNetwork = networks[otherName]

it('isSameCoin() returns true testnet/mainnet variants', function () {
assert.strictEqual(
coins.isSameCoin(network, otherNetwork),
otherNetwork === coins.getMainnet(network) ||
otherNetwork === coins.getTestnet(network)
)

assert.strictEqual(
name === otherName,
network === otherNetwork
)
})

if (network === otherNetwork) {
continue
}

Expand Down

0 comments on commit e1dd2cb

Please sign in to comment.