all files / src/ network.js

100% Statements 36/36
100% Branches 10/10
100% Functions 12/12
100% Lines 11/11
1 statement, 4 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                1×         1×                             1× 9×             1×             8×               17×               12×               6×               4×      
import {hash} from "./hashing";
 
/**
 * Contains passphrases for common networks:
 * * `Networks.PUBLIC`: `Live DigitalBits Network ; March 2018`
 * * `Networks.TESTNET`: `Test DigitalBits Network ; December 2017`
 * @type {{PUBLIC: string, TESTNET: string}}
 */
export const Networks = {
	PUBLIC: "Live DigitalBits Network ; March 2018",
	TESTNET: "Test DigitalBits Network ; December 2017"
};
 
var current = null;
 
/**
 * The Network class provides helper methods to get the passphrase or id for different
 * digitalbits networks.  It also provides the {@link Network.current} class method that returns the network
 * that will be used by this process for the purposes of generating signatures.
 *
 * You should select network your app will use before adding the first signature. You can use the `use`,
 * `usePublicNetwork` and `useTestNetwork` helper methods.
 *
 * Creates a new `Network` object.
 * @constructor
 * @param {string} networkPassphrase Network passphrase
 */
export class Network {
	constructor(networkPassphrase) {
		this._networkPassphrase = networkPassphrase;
	}
 
	/**
	 * Use DigitalBits Public Network
	 */
	static usePublicNetwork() {
		this.use(new Network(Networks.PUBLIC));
	}
 
	/**
	 * Use test network.
	 */
	static useTestNetwork() {
		this.use(new Network(Networks.TESTNET));
	}
 
	/**
	 * Use network defined by Network object.
	 * @param {Network} network Network to use
	 */
	static use(network) {
		current = network;
	}
 
	/**
	 * Returns currently selected network.
	 * @returns {Network}
	 */
	static current() {
		return current;
	}
 
	/**
	 * Returns network passphrase.
	 * @returns {string}
	 */
	networkPassphrase() {
		return this._networkPassphrase;
	}
 
	/**
	 * Returns Network ID. Network ID is SHA-256 hash of network passphrase.
	 * @returns {string}
	 */
	networkId() {
		return hash(this.networkPassphrase());
	}
}