UNPKG

2.41 kBMarkdownView Raw
1# Networks
2
3Bitcore provides support for the main bitcoin network as well as for `testnet3`, the current test blockchain. We encourage the use of `Networks.livenet` and `Networks.testnet` as constants. Note that the library sometimes may check for equality against this object. Please avoid creating a deep copy of this object.
4
5The `Network` namespace has a function, `get(...)` that returns an instance of a `Network` or `undefined`. The only argument to this function is some kind of identifier of the network: either its name, a reference to a Network object, or a number used as a magic constant to identify the network (for example, the value `0` that gives bitcoin addresses the distinctive `'1'` at its beginning on livenet, is a `0x6F` for testnet).
6
7## Regtest
8
9The regtest network is useful for development as it's possible to programmatically and instantly generate blocks for testing. It's currently supported as a variation of testnet. Here is an example of how to use regtest with the Bitcore Library:
10
11```js
12// Standard testnet
13> bitcore.Networks.testnet.networkMagic;
14<Buffer 0b 11 09 07>
15```
16
17```js
18// Enabling testnet to use the regtest port and magicNumber
19> bitcore.Networks.enableRegtest();
20> bitcore.Networks.testnet.networkMagic;
21<Buffer fa bf b5 da>
22```
23
24## Setting the Default Network
25
26Most projects will only need to work with one of the networks. The value of `Networks.defaultNetwork` can be set to `Networks.testnet` if the project will need to only to work on testnet (the default is `Networks.livenet`).
27
28## Network constants
29
30The functionality of testnet and livenet is mostly similar (except for some relaxed block validation rules on testnet). They differ in the constants being used for human representation of base58 encoded strings. These are sometimes referred to as "version" constants.
31
32Take a look at this modified snippet from [networks.js](https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib/lib/networks.js)
33
34```javascript
35var livenet = new Network();
36_.extend(livenet, {
37 name: 'livenet',
38 alias: 'mainnet',
39 pubkeyhash: 0x00,
40 privatekey: 0x80,
41 scripthash: 0x05,
42 xpubkey: 0x0488b21e,
43 xprivkey: 0x0488ade4,
44 port: 8333
45});
46
47var testnet = new Network();
48_.extend(testnet, {
49 name: 'testnet',
50 alias: 'testnet',
51 pubkeyhash: 0x6f,
52 privatekey: 0xef,
53 scripthash: 0xc4,
54 xpubkey: 0x043587cf,
55 xprivkey: 0x04358394,
56 port: 18333
57});
58```