forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.ts
44 lines (36 loc) · 1.22 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
40
41
42
43
44
import chalk from 'chalk'
import Common from '@ethereumjs/common'
import { DPT } from '../src/index'
const PRIVATE_KEY = 'd772e3d6a001a38064dd23964dd2836239fa0e6cec8b28972a87460a17210fe9'
const config = new Common({ chain: 'mainnet' })
const bootstrapNodes = config.bootstrapNodes()
const BOOTNODES = 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,
},
})
/* eslint-disable no-console */
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 (const bootnode of BOOTNODES) {
dpt.bootstrap(bootnode).catch((err) => console.error(chalk.bold.red(err.stack || err)))
}