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

Commit

Permalink
src/networks.js: define coin network names
Browse files Browse the repository at this point in the history
We need to describe some more relationships between networks. For
instance, it is very useful to define `getMainnet(network)` and
`getTestnet(network)`.

These methods would fit well in `coins.js` and can be implemented
using object equality on the constants defined in `network.js`.
However, importing `network.js` from `coins.js` would lead to a
circular import.

This change allows using `coin.js` to contain utility methods for
`network.js` without having circular import problems.
  • Loading branch information
OttoAllmendinger committed Jan 8, 2020
1 parent d0cc345 commit 06f0b92
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 32 deletions.
100 changes: 69 additions & 31 deletions src/coins.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,89 @@
// Coins supported by bitgo-bitcoinjs-lib
const typeforce = require('typeforce')

const coins = {
BCH: 'bch',
BSV: 'bsv',
BTC: 'btc',
BTG: 'btg',
LTC: 'ltc',
ZEC: 'zec',
DASH: 'dash'
}
const networks = require('./networks')

coins.isBitcoin = function (network) {
return typeforce.value(coins.BTC)(network.coin)
function isBitcoin (network) {
return typeforce.value(networks.bitcoin.coin)(network.coin)
}

coins.isBitcoinCash = function (network) {
return typeforce.value(coins.BCH)(network.coin)
/**
* @param network
* @returns {boolean} true iff network is bitcoincash or bitcoincashTestnet
*/
function isBitcoinCash (network) {
return typeforce.value(networks.bitcoincash.coin)(network.coin)
}

coins.isBitcoinSV = function (network) {
return typeforce.value(coins.BSV)(network.coin)
/**
* @param network
* @returns {boolean} true iff network is bitcoingold
*/
function isBitcoinGold (network) {
return typeforce.value(networks.bitcoingold.coin)(network.coin)
}

coins.isBitcoinGold = function (network) {
return typeforce.value(coins.BTG)(network.coin)
/**
* @param network
* @returns {boolean} true iff network is bitcoinsv or bitcoinsvTestnet
*/
function isBitcoinSV (network) {
return typeforce.value(networks.bitcoinsv.coin)(network.coin)
}

coins.isDash = function (network) {
return typeforce.value(coins.DASH)(network.coin)
/**
* @param network
* @returns {boolean} true iff network is dash or dashTest
*/
function isDash (network) {
return typeforce.value(networks.dash.coin)(network.coin)
}

coins.isLitecoin = function (network) {
return typeforce.value(coins.LTC)(network.coin)
/**
* @param network
* @returns {boolean} true iff network is litecoin or litecoinTest
*/
function isLitecoin (network) {
return typeforce.value(networks.litecoin.coin)(network.coin)
}

coins.isZcash = function (network) {
return typeforce.value(coins.ZEC)(network.coin)
/**
* @param network
* @returns {boolean} true iff network is zcash or zcashTest
*/
function isZcash (network) {
return typeforce.value(networks.zcash.coin)(network.coin)
}

coins.isValidCoin = typeforce.oneOf(
coins.isBitcoin,
coins.isBitcoinCash,
coins.isBitcoinSV,
coins.isBitcoinGold,
coins.isLitecoin,
coins.isZcash
isValidCoin = typeforce.oneOf(
isBitcoin,
isBitcoinCash,
isBitcoinSV,
isBitcoinGold,
isLitecoin,
isZcash
)

module.exports = coins
module.exports = {
BTC: networks.bitcoin.coin,
BCH: networks.bitcoincash.coin,
BSV: networks.bitcoinsv.coin,
BTG: networks.bitcoingold.coin,
DASH: networks.dash.coin,
LTC: networks.litecoin.coin,
ZEC: networks.zcash.coin,

getMainnet,
isMainnet,
getTestnet,
isTestnet,

isBitcoin,
isBitcoinCash,
isBitcoinGold,
isBitcoinSV,
isDash,
isLitecoin,
isZcash,
isValidCoin
}
11 changes: 10 additions & 1 deletion src/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ wif: src/chainparams.cpp base58Prefixes[SECRET_KEY]
*/

var coins = require('./coins')
const coins = {
BCH: 'bch',
BSV: 'bsv',
BTC: 'btc',
BTG: 'btg',
LTC: 'ltc',
ZEC: 'zec',
DASH: 'dash'
}


module.exports = {

Expand Down

0 comments on commit 06f0b92

Please sign in to comment.