1 | 'use strict'
|
2 |
|
3 | var expect = require('chai').expect
|
4 | var should = require('chai').should()
|
5 | var bsv = require('..')
|
6 | var networks = bsv.Networks
|
7 |
|
8 | describe('Networks', function () {
|
9 | var customnet
|
10 |
|
11 | it('should contain all Networks', function () {
|
12 | should.exist(networks.livenet)
|
13 | should.exist(networks.testnet)
|
14 | should.exist(networks.stn)
|
15 | should.exist(networks.defaultNetwork)
|
16 | })
|
17 |
|
18 | it('should be able to define a custom Network', function () {
|
19 | var custom = {
|
20 | name: 'customnet',
|
21 | alias: 'mynet',
|
22 | pubkeyhash: 0x10,
|
23 | privatekey: 0x90,
|
24 | scripthash: 0x08,
|
25 | xpubkey: 0x0278b20e,
|
26 | xprivkey: 0x0278ade4,
|
27 | networkMagic: 0xe7beb4d4,
|
28 | port: 20001,
|
29 | dnsSeeds: [
|
30 | 'localhost',
|
31 | 'mynet.localhost'
|
32 | ]
|
33 | }
|
34 | networks.add(custom)
|
35 | customnet = networks.get('customnet')
|
36 | for (var key in custom) {
|
37 | if (key !== 'networkMagic') {
|
38 | customnet[key].should.equal(custom[key])
|
39 | } else {
|
40 | var expected = Buffer.from('e7beb4d4', 'hex')
|
41 | customnet[key].should.deep.equal(expected)
|
42 | }
|
43 | }
|
44 | })
|
45 |
|
46 | it('should have network magic for testnet', function () {
|
47 | var testnet = networks.get('testnet')
|
48 | Buffer.isBuffer(testnet.networkMagic).should.equal(true)
|
49 | })
|
50 |
|
51 | it('should have network magic for stn', function () {
|
52 | var stn = networks.get('stn')
|
53 | Buffer.isBuffer(stn.networkMagic).should.equal(true)
|
54 | })
|
55 |
|
56 | it('can remove a custom network', function () {
|
57 | networks.remove(customnet)
|
58 | var net = networks.get('customnet')
|
59 | should.equal(net, undefined)
|
60 | })
|
61 |
|
62 | it('should not set a network map for an undefined value', function () {
|
63 | var custom = {
|
64 | name: 'somenet',
|
65 | pubkeyhash: 0x13,
|
66 | privatekey: 0x93,
|
67 | scripthash: 0x11,
|
68 | xpubkey: 0x0278b20f,
|
69 | xprivkey: 0x0278ade5,
|
70 | networkMagic: 0xe7beb4d5,
|
71 | port: 20008,
|
72 | dnsSeeds: [
|
73 | 'somenet.localhost'
|
74 | ]
|
75 | }
|
76 | networks.add(custom)
|
77 | var network = networks.get(undefined)
|
78 | should.not.exist(network)
|
79 | var somenet = networks.get('somenet')
|
80 | should.exist(somenet)
|
81 | somenet.name.should.equal('somenet')
|
82 | networks.remove(somenet)
|
83 | })
|
84 |
|
85 | var constants = ['name', 'alias', 'pubkeyhash', 'scripthash', 'xpubkey', 'xprivkey']
|
86 |
|
87 | constants.forEach(function (key) {
|
88 | it('should have constant ' + key + ' for livenet, testnet and stn', function () {
|
89 | networks.testnet.hasOwnProperty(key).should.equal(true)
|
90 | networks.livenet.hasOwnProperty(key).should.equal(true)
|
91 | networks.stn.hasOwnProperty(key).should.equal(true)
|
92 | })
|
93 | })
|
94 |
|
95 | it('tests only for the specified key', function () {
|
96 | expect(networks.get(0x6f, 'pubkeyhash')).to.equal(networks.testnet)
|
97 | expect(networks.get(0x6f, 'privatekey')).to.equal(undefined)
|
98 | })
|
99 |
|
100 | it('can test for multiple keys', function () {
|
101 | expect(networks.get(0x6f, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet)
|
102 | expect(networks.get(0xc4, ['pubkeyhash', 'scripthash'])).to.equal(networks.testnet)
|
103 | expect(networks.get(0x6f, ['privatekey', 'port'])).to.equal(undefined)
|
104 | })
|
105 |
|
106 | it('should have regtest network', function () {
|
107 | expect(networks.get('regtest').name).to.equal('regtest')
|
108 | })
|
109 |
|
110 | it('should have testnet network', function () {
|
111 | expect(networks.get('testnet').name).to.equal('testnet')
|
112 | })
|
113 |
|
114 | it('should have stn network', function () {
|
115 | expect(networks.get('stn').name).to.equal('stn')
|
116 | })
|
117 |
|
118 | it('should have livenet network', function () {
|
119 | expect(networks.get('livenet').name).to.equal('livenet')
|
120 | })
|
121 |
|
122 | it('should have bchtest cashAddrPrefix', function () {
|
123 | expect(networks.get('testnet').cashAddrPrefix).to.equal('bchtest')
|
124 | })
|
125 |
|
126 | it('should have bchreg cashAddrPrefix', function () {
|
127 | expect(networks.get('regtest').cashAddrPrefix).to.equal('bchreg')
|
128 | })
|
129 |
|
130 | it('should have bchreg cashAddrPrefix after enableRegtest is called', function () {
|
131 | var network = networks.get('testnet')
|
132 | networks.enableRegtest()
|
133 | expect(network.cashAddrPrefix).to.equal('bchreg')
|
134 | })
|
135 |
|
136 | it('should have bchtest cashAddrPrefix after disableRegtest is called', function () {
|
137 | var network = networks.get('testnet')
|
138 | networks.disableRegtest()
|
139 | expect(network.cashAddrPrefix).to.equal('bchtest')
|
140 | })
|
141 | it('should have bsvstn cashAddrPrefix after enableStn is called', function () {
|
142 | var network = networks.get('testnet')
|
143 | networks.enableStn()
|
144 | expect(network.cashAddrPrefix).to.equal('bsvstn')
|
145 | })
|
146 |
|
147 | it('should have bchtest cashAddrPrefix after disableStn is called', function () {
|
148 | var network = networks.get('testnet')
|
149 | networks.disableStn()
|
150 | expect(network.cashAddrPrefix).to.equal('bchtest')
|
151 | })
|
152 |
|
153 | it('converts to string using the "name" property', function () {
|
154 | networks.livenet.toString().should.equal('livenet')
|
155 | })
|
156 |
|
157 | it('network object should be immutable', function () {
|
158 | expect(networks.testnet.name).to.equal('testnet')
|
159 | var fn = function () { networks.testnet.name = 'livenet' }
|
160 | expect(fn).to.throw(TypeError)
|
161 | })
|
162 | })
|