1 | 'use strict';
|
2 |
|
3 | var expect = require('chai').expect;
|
4 | var should = require('chai').should();
|
5 | var bitcore = require('..');
|
6 | var networks = bitcore.Networks;
|
7 |
|
8 | describe('Networks', function() {
|
9 |
|
10 | var customnet;
|
11 |
|
12 | it('should contain all Networks', function() {
|
13 | should.exist(networks.livenet);
|
14 | should.exist(networks.testnet);
|
15 | should.exist(networks.defaultNetwork);
|
16 | });
|
17 |
|
18 | it('#DEPRECATED will enable/disable regtest Network', function() {
|
19 | const beforeEnable = networks.testnet;
|
20 | networks.enableRegtest();
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | networks.testnet.should.deep.equal(beforeEnable);
|
28 |
|
29 | networks.disableRegtest();
|
30 | networks.testnet.should.deep.equal(beforeEnable);
|
31 | });
|
32 |
|
33 | it('will get network based on string "regtest" value', function() {
|
34 | var network = networks.get('regtest');
|
35 | network.should.equal(networks.regtest);
|
36 | });
|
37 |
|
38 | it('should be able to define a custom Network', function() {
|
39 | var custom = {
|
40 | name: 'customnet',
|
41 | alias: 'mynet',
|
42 | pubkeyhash: 0x10,
|
43 | privatekey: 0x90,
|
44 | scripthash: 0x08,
|
45 | xpubkey: 0x0278b20e,
|
46 | xprivkey: 0x0278ade4,
|
47 | networkMagic: 0xe7beb4d4,
|
48 | port: 20001,
|
49 | dnsSeeds: [
|
50 | 'localhost',
|
51 | 'mynet.localhost'
|
52 | ]
|
53 | };
|
54 | networks.add(custom);
|
55 | customnet = networks.get('customnet');
|
56 | for (var key in custom) {
|
57 | if (key !== 'networkMagic') {
|
58 | customnet[key].should.equal(custom[key]);
|
59 | } else {
|
60 | var expected = Buffer.from('e7beb4d4', 'hex');
|
61 | customnet[key].should.deep.equal(expected);
|
62 | }
|
63 | }
|
64 | });
|
65 |
|
66 | it('can remove a custom network', function() {
|
67 | networks.remove(customnet);
|
68 | var net = networks.get('customnet');
|
69 | should.equal(net, undefined);
|
70 | });
|
71 |
|
72 | it('should not set a network map for an undefined value', function() {
|
73 | var custom = {
|
74 | name: 'somenet',
|
75 | pubkeyhash: 0x13,
|
76 | privatekey: 0x93,
|
77 | scripthash: 0x11,
|
78 | xpubkey: 0x0278b20f,
|
79 | xprivkey: 0x0278ade5,
|
80 | networkMagic: 0xe7beb4d5,
|
81 | port: 20008,
|
82 | dnsSeeds: [
|
83 | 'somenet.localhost'
|
84 | ]
|
85 | };
|
86 | networks.add(custom);
|
87 | var network = networks.get(undefined);
|
88 | should.not.exist(network);
|
89 | var somenet = networks.get('somenet');
|
90 | should.exist(somenet);
|
91 | somenet.name.should.equal('somenet');
|
92 | networks.remove(somenet);
|
93 | });
|
94 |
|
95 | var constants = ['name', 'alias', 'pubkeyhash', 'scripthash', 'xpubkey', 'xprivkey'];
|
96 |
|
97 | constants.forEach(function(key){
|
98 | it('should have constant '+key+' for livenet and testnet', function(){
|
99 | networks.testnet.hasOwnProperty(key).should.equal(true);
|
100 | networks.livenet.hasOwnProperty(key).should.equal(true);
|
101 | });
|
102 | });
|
103 |
|
104 | it('tests only for the specified key', function() {
|
105 | expect(networks.get(0x6f, 'pubkeyhash')).to.equal(networks.testnet);
|
106 | expect(networks.get(0x6f, 'privatekey')).to.equal(undefined);
|
107 | });
|
108 |
|
109 | it('can test for multiple keys', function() {
|
110 | expect(networks.get(0x6f, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet);
|
111 | expect(networks.get(0xc4, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet);
|
112 | expect(networks.get(0x6f, ['privatekey', 'port'])).to.equal(undefined);
|
113 | });
|
114 |
|
115 | it('converts to string using the "name" property', function() {
|
116 | networks.livenet.toString().should.equal('livenet');
|
117 | });
|
118 |
|
119 | it('network object should be immutable', function() {
|
120 | expect(networks.testnet.name).to.equal('testnet')
|
121 | var fn = function() { networks.testnet.name = 'livenet' }
|
122 | expect(fn).to.throw(TypeError)
|
123 | });
|
124 |
|
125 | });
|