@libp2p/bootstrap 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +62 -0
- package/dist/src/index.d.ts +37 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +75 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +147 -0
- package/src/index.ts +103 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
js-libp2p-bootstrap
|
|
2
|
+
=================
|
|
3
|
+
|
|
4
|
+
[](http://protocol.ai)
|
|
5
|
+
[](http://libp2p.io/)
|
|
6
|
+
[](http://webchat.freenode.net/?channels=%23libp2p)
|
|
7
|
+
[](https://discuss.libp2p.io)
|
|
8
|
+
[](https://codecov.io/gh/libp2p/js-libp2p-bootstrap)
|
|
9
|
+
[](https://travis-ci.com/libp2p/js-libp2p-bootstrap)
|
|
10
|
+
[](https://github.com/feross/standard)
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
> JavaScript libp2p Implementation of the railing process of a Node through a bootstrap peer list
|
|
14
|
+
|
|
15
|
+
## Lead Maintainer
|
|
16
|
+
|
|
17
|
+
[Vasco Santos](https://github.com/vasco-santos).
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```JavaScript
|
|
22
|
+
const Libp2p = require('libp2p')
|
|
23
|
+
const Bootstrap = require('libp2p-bootstrap')
|
|
24
|
+
const TCP = require('libp2p-tcp')
|
|
25
|
+
const { NOISE } = require('libp2p-noise')
|
|
26
|
+
const MPLEX = require('libp2p-mplex')
|
|
27
|
+
|
|
28
|
+
let options = {
|
|
29
|
+
modules: {
|
|
30
|
+
transport: [ TCP ],
|
|
31
|
+
peerDiscovery: [ Bootstrap ],
|
|
32
|
+
streamMuxer: [ MPLEX ],
|
|
33
|
+
encryption: [ NOISE ]
|
|
34
|
+
},
|
|
35
|
+
config: {
|
|
36
|
+
peerDiscovery: {
|
|
37
|
+
[Bootstrap.tag]: {
|
|
38
|
+
list: [ // a list of bootstrap peer multiaddrs to connect to on node startup
|
|
39
|
+
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
|
40
|
+
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
|
41
|
+
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa"
|
|
42
|
+
],
|
|
43
|
+
interval: 5000 // default is 10 ms,
|
|
44
|
+
enabled: true
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function start () {
|
|
51
|
+
let libp2p = await Libp2p.create(options)
|
|
52
|
+
|
|
53
|
+
libp2p.on('peer:discovery', function (peerId) {
|
|
54
|
+
console.log('found peer: ', peerId.toB58String())
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
await libp2p.start()
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
start()
|
|
62
|
+
```
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import type PeerDiscovery from '@libp2p/interfaces/peer-discovery';
|
|
4
|
+
export interface BootstrapOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The list of peer addresses in multi-address format
|
|
7
|
+
*/
|
|
8
|
+
list: string[];
|
|
9
|
+
/**
|
|
10
|
+
* The interval between emitting addresses in milliseconds
|
|
11
|
+
*/
|
|
12
|
+
interval?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Emits 'peer' events on a regular interval for each peer in the provided list.
|
|
16
|
+
*/
|
|
17
|
+
export declare class Bootstrap extends EventEmitter implements PeerDiscovery {
|
|
18
|
+
static tag: string;
|
|
19
|
+
private timer;
|
|
20
|
+
private readonly list;
|
|
21
|
+
private readonly interval;
|
|
22
|
+
constructor(options?: BootstrapOptions);
|
|
23
|
+
isStarted(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Start emitting events
|
|
26
|
+
*/
|
|
27
|
+
start(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Emit each address in the list as a PeerInfo
|
|
30
|
+
*/
|
|
31
|
+
_discoverBootstrapPeers(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Stop emitting events
|
|
34
|
+
*/
|
|
35
|
+
stop(): void;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAErC,OAAO,KAAK,aAAa,MAAM,mCAAmC,CAAA;AAMlE,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,IAAI,EAAE,MAAM,EAAE,CAAA;IAEd;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,qBAAa,SAAU,SAAQ,YAAa,YAAW,aAAa;IAClE,MAAM,CAAC,GAAG,SAAc;IAExB,OAAO,CAAC,KAAK,CAAqB;IAClC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAU;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;gBAEpB,OAAO,GAAE,gBAA+B;IAWrD,SAAS;IAIT;;OAEG;IACH,KAAK;IAUL;;OAEG;IACH,uBAAuB;IA+BvB;;OAEG;IACH,IAAI;CAIL"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { PeerId } from '@libp2p/peer-id';
|
|
2
|
+
import { Multiaddr } from '@multiformats/multiaddr';
|
|
3
|
+
import { P2P } from '@multiformats/mafmt';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import debug from 'debug';
|
|
6
|
+
const log = Object.assign(debug('libp2p:bootstrap'), {
|
|
7
|
+
error: debug('libp2p:bootstrap:error')
|
|
8
|
+
});
|
|
9
|
+
/**
|
|
10
|
+
* Emits 'peer' events on a regular interval for each peer in the provided list.
|
|
11
|
+
*/
|
|
12
|
+
export class Bootstrap extends EventEmitter {
|
|
13
|
+
constructor(options = { list: [] }) {
|
|
14
|
+
if (options.list == null || options.list.length === 0) {
|
|
15
|
+
throw new Error('Bootstrap requires a list of peer addresses');
|
|
16
|
+
}
|
|
17
|
+
super();
|
|
18
|
+
this.list = options.list;
|
|
19
|
+
this.interval = options.interval ?? 10000;
|
|
20
|
+
this.timer = null;
|
|
21
|
+
}
|
|
22
|
+
isStarted() {
|
|
23
|
+
return Boolean(this.timer);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Start emitting events
|
|
27
|
+
*/
|
|
28
|
+
start() {
|
|
29
|
+
if (this.timer != null) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.timer = setInterval(() => this._discoverBootstrapPeers(), this.interval);
|
|
33
|
+
log('Starting bootstrap node discovery');
|
|
34
|
+
this._discoverBootstrapPeers();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Emit each address in the list as a PeerInfo
|
|
38
|
+
*/
|
|
39
|
+
_discoverBootstrapPeers() {
|
|
40
|
+
if (this.timer == null) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.list.forEach((candidate) => {
|
|
44
|
+
if (!P2P.matches(candidate)) {
|
|
45
|
+
return log.error('Invalid multiaddr');
|
|
46
|
+
}
|
|
47
|
+
const ma = new Multiaddr(candidate);
|
|
48
|
+
const peerIdStr = ma.getPeerId();
|
|
49
|
+
if (peerIdStr == null) {
|
|
50
|
+
log.error('Invalid bootstrap multiaddr without peer id');
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const peerId = PeerId.fromString(peerIdStr);
|
|
54
|
+
try {
|
|
55
|
+
this.emit('peer', {
|
|
56
|
+
id: peerId,
|
|
57
|
+
multiaddrs: [ma]
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
log.error('Invalid bootstrap peer id', err);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Stop emitting events
|
|
67
|
+
*/
|
|
68
|
+
stop() {
|
|
69
|
+
if (this.timer != null)
|
|
70
|
+
clearInterval(this.timer);
|
|
71
|
+
this.timer = null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
Bootstrap.tag = 'bootstrap';
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AACrC,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,kBAAkB,CAAC,EAAE;IACnD,KAAK,EAAE,KAAK,CAAC,wBAAwB,CAAC;CACvC,CAAC,CAAA;AAcF;;GAEG;AACH,MAAM,OAAO,SAAU,SAAQ,YAAY;IAOzC,YAAa,UAA4B,EAAE,IAAI,EAAE,EAAE,EAAE;QACnD,IAAI,OAAO,CAAC,IAAI,IAAI,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;YACrD,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;SAC/D;QACD,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAA;QACzC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACnB,CAAC;IAED,SAAS;QACP,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE;YACtB,OAAM;SACP;QAED,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,uBAAuB,EAAE,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;QAC7E,GAAG,CAAC,mCAAmC,CAAC,CAAA;QACxC,IAAI,CAAC,uBAAuB,EAAE,CAAA;IAChC,CAAC;IAED;;OAEG;IACH,uBAAuB;QACrB,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE;YACtB,OAAM;SACP;QAED,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;YAC9B,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;gBAC3B,OAAO,GAAG,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAA;aACtC;YAED,MAAM,EAAE,GAAG,IAAI,SAAS,CAAC,SAAS,CAAC,CAAA;YACnC,MAAM,SAAS,GAAG,EAAE,CAAC,SAAS,EAAE,CAAA;YAEhC,IAAI,SAAS,IAAI,IAAI,EAAE;gBACrB,GAAG,CAAC,KAAK,CAAC,6CAA6C,CAAC,CAAA;gBACxD,OAAM;aACP;YAED,MAAM,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAA;YAE3C,IAAI;gBACF,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;oBAChB,EAAE,EAAE,MAAM;oBACV,UAAU,EAAE,CAAC,EAAE,CAAC;iBACjB,CAAC,CAAA;aACH;YAAC,OAAO,GAAG,EAAE;gBACZ,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAA;aAC5C;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;IAED;;OAEG;IACH,IAAI;QACF,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI;YAAE,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACjD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACnB,CAAC;;AA1EM,aAAG,GAAG,WAAW,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@libp2p/bootstrap",
|
|
3
|
+
"version": "0.14.0",
|
|
4
|
+
"description": "Node.js IPFS Implementation of the railing process of a Node through a bootstrap peer list",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p-bootstrap#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p-bootstrap.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p-bootstrap/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"IPFS"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=16.0.0",
|
|
19
|
+
"npm": ">=7.0.0"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"types": "./dist/src/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"dist/src",
|
|
26
|
+
"!dist/test",
|
|
27
|
+
"!**/*.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/src/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"eslintConfig": {
|
|
35
|
+
"extends": "ipfs",
|
|
36
|
+
"parserOptions": {
|
|
37
|
+
"sourceType": "module"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"release": {
|
|
41
|
+
"branches": [
|
|
42
|
+
"master"
|
|
43
|
+
],
|
|
44
|
+
"plugins": [
|
|
45
|
+
[
|
|
46
|
+
"@semantic-release/commit-analyzer",
|
|
47
|
+
{
|
|
48
|
+
"preset": "conventionalcommits",
|
|
49
|
+
"releaseRules": [
|
|
50
|
+
{
|
|
51
|
+
"breaking": true,
|
|
52
|
+
"release": "major"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"revert": true,
|
|
56
|
+
"release": "patch"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"type": "feat",
|
|
60
|
+
"release": "minor"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"type": "fix",
|
|
64
|
+
"release": "patch"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"type": "chore",
|
|
68
|
+
"release": "patch"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"type": "docs",
|
|
72
|
+
"release": "patch"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"type": "test",
|
|
76
|
+
"release": "patch"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"scope": "no-release",
|
|
80
|
+
"release": false
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
"@semantic-release/release-notes-generator",
|
|
87
|
+
{
|
|
88
|
+
"preset": "conventionalcommits",
|
|
89
|
+
"presetConfig": {
|
|
90
|
+
"types": [
|
|
91
|
+
{
|
|
92
|
+
"type": "feat",
|
|
93
|
+
"section": "Features"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"type": "fix",
|
|
97
|
+
"section": "Bug Fixes"
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
"type": "chore",
|
|
101
|
+
"section": "Trivial Changes"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"type": "docs",
|
|
105
|
+
"section": "Trivial Changes"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"type": "test",
|
|
109
|
+
"section": "Tests"
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
],
|
|
115
|
+
"@semantic-release/changelog",
|
|
116
|
+
"@semantic-release/npm",
|
|
117
|
+
"@semantic-release/github",
|
|
118
|
+
"@semantic-release/git"
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
"scripts": {
|
|
122
|
+
"lint": "aegir lint",
|
|
123
|
+
"dep-check": "aegir dep-check",
|
|
124
|
+
"build": "tsc",
|
|
125
|
+
"pretest": "npm run build",
|
|
126
|
+
"test": "aegir test -f ./dist/test",
|
|
127
|
+
"test:chrome": "npm run test -- -t browser --cov",
|
|
128
|
+
"test:chrome-webworker": "npm run test -- -t webworker",
|
|
129
|
+
"test:firefox": "npm run test -- -t browser -- --browser firefox",
|
|
130
|
+
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
|
|
131
|
+
"test:node": "npm run test -- -t node --cov",
|
|
132
|
+
"test:electron-main": "npm run test -- -t electron-main",
|
|
133
|
+
"release": "semantic-release"
|
|
134
|
+
},
|
|
135
|
+
"dependencies": {
|
|
136
|
+
"@libp2p/peer-id": "^1.0.4",
|
|
137
|
+
"@multiformats/mafmt": "^11.0.2",
|
|
138
|
+
"@multiformats/multiaddr": "^10.1.2",
|
|
139
|
+
"debug": "^4.3.1"
|
|
140
|
+
},
|
|
141
|
+
"devDependencies": {
|
|
142
|
+
"@libp2p/interface-compliance-tests": "^1.0.8",
|
|
143
|
+
"@libp2p/interfaces": "^1.1.1",
|
|
144
|
+
"@types/debug": "^4.1.5",
|
|
145
|
+
"aegir": "^36.1.3"
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { PeerId } from '@libp2p/peer-id'
|
|
2
|
+
import { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
+
import { P2P } from '@multiformats/mafmt'
|
|
4
|
+
import { EventEmitter } from 'events'
|
|
5
|
+
import debug from 'debug'
|
|
6
|
+
import type PeerDiscovery from '@libp2p/interfaces/peer-discovery'
|
|
7
|
+
|
|
8
|
+
const log = Object.assign(debug('libp2p:bootstrap'), {
|
|
9
|
+
error: debug('libp2p:bootstrap:error')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export interface BootstrapOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The list of peer addresses in multi-address format
|
|
15
|
+
*/
|
|
16
|
+
list: string[]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The interval between emitting addresses in milliseconds
|
|
20
|
+
*/
|
|
21
|
+
interval?: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Emits 'peer' events on a regular interval for each peer in the provided list.
|
|
26
|
+
*/
|
|
27
|
+
export class Bootstrap extends EventEmitter implements PeerDiscovery {
|
|
28
|
+
static tag = 'bootstrap'
|
|
29
|
+
|
|
30
|
+
private timer: NodeJS.Timer | null
|
|
31
|
+
private readonly list: string[]
|
|
32
|
+
private readonly interval: number
|
|
33
|
+
|
|
34
|
+
constructor (options: BootstrapOptions = { list: [] }) {
|
|
35
|
+
if (options.list == null || options.list.length === 0) {
|
|
36
|
+
throw new Error('Bootstrap requires a list of peer addresses')
|
|
37
|
+
}
|
|
38
|
+
super()
|
|
39
|
+
|
|
40
|
+
this.list = options.list
|
|
41
|
+
this.interval = options.interval ?? 10000
|
|
42
|
+
this.timer = null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
isStarted () {
|
|
46
|
+
return Boolean(this.timer)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Start emitting events
|
|
51
|
+
*/
|
|
52
|
+
start () {
|
|
53
|
+
if (this.timer != null) {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.timer = setInterval(() => this._discoverBootstrapPeers(), this.interval)
|
|
58
|
+
log('Starting bootstrap node discovery')
|
|
59
|
+
this._discoverBootstrapPeers()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Emit each address in the list as a PeerInfo
|
|
64
|
+
*/
|
|
65
|
+
_discoverBootstrapPeers () {
|
|
66
|
+
if (this.timer == null) {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.list.forEach((candidate) => {
|
|
71
|
+
if (!P2P.matches(candidate)) {
|
|
72
|
+
return log.error('Invalid multiaddr')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const ma = new Multiaddr(candidate)
|
|
76
|
+
const peerIdStr = ma.getPeerId()
|
|
77
|
+
|
|
78
|
+
if (peerIdStr == null) {
|
|
79
|
+
log.error('Invalid bootstrap multiaddr without peer id')
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const peerId = PeerId.fromString(peerIdStr)
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
this.emit('peer', {
|
|
87
|
+
id: peerId,
|
|
88
|
+
multiaddrs: [ma]
|
|
89
|
+
})
|
|
90
|
+
} catch (err) {
|
|
91
|
+
log.error('Invalid bootstrap peer id', err)
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Stop emitting events
|
|
98
|
+
*/
|
|
99
|
+
stop () {
|
|
100
|
+
if (this.timer != null) clearInterval(this.timer)
|
|
101
|
+
this.timer = null
|
|
102
|
+
}
|
|
103
|
+
}
|