forked from ethereumjs/ethereumjs-devp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.ts
39 lines (32 loc) · 1.07 KB
/
simple.ts
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
import chalk from 'chalk'
import { DPT } from '../src'
const PRIVATE_KEY = 'd772e3d6a001a38064dd23964dd2836239fa0e6cec8b28972a87460a17210fe9'
const BOOTNODES = require('ethereum-common').bootstrapNodes.map((node: any) => {
return {
address: node.ip,
udpPort: node.port,
tcpPort: node.port,
}
})
const dpt = new DPT(Buffer.from(PRIVATE_KEY, 'hex'), {
endpoint: {
address: '0.0.0.0',
udpPort: null,
tcpPort: null,
},
})
dpt.on('error', err => console.error(chalk.red(err.stack || err)))
dpt.on('peer:added', peer => {
const info = `(${peer.id.toString('hex')},${peer.address},${peer.udpPort},${peer.tcpPort})`
console.log(chalk.green(`New peer: ${info} (total: ${dpt.getPeers().length})`))
})
dpt.on('peer:removed', peer => {
console.log(
chalk.yellow(`Remove peer: ${peer.id.toString('hex')} (total: ${dpt.getPeers().length})`),
)
})
// for accept incoming connections uncomment next line
// dpt.bind(30303, '0.0.0.0')
for (let bootnode of BOOTNODES) {
dpt.bootstrap(bootnode).catch(err => console.error(chalk.bold.red(err.stack || err)))
}