-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-discover-nodes.js
33 lines (26 loc) · 1.11 KB
/
1-discover-nodes.js
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
const dns = require('dns/promises')
const net = require('net')
const client = new net.Socket()
const currentNetwork = 'testnet'
const dnsSeeds = {
mainnet: ['seed.bitcoin.sipa.be', 'dnsseed.bluematt.me', 'dnsseed.bitcoin.dashjr.org', 'seed.bitcoinstats.com', 'seed.bitnodes.io', 'bitseed.xf2.org'],
testnet: ['testnet-seed.bitcoin.jonasschnelli.ch', 'seed.tbtc.petertodd.org', 'testnet-seed.bluematt.me', 'testnet-seed.bitcoin.schildbach.de']
}
const networks = {
regtest: { port: 18444 },
testnet: { port: 18333, dnsSeeds: dnsSeeds.testnet },
mainnet: { port: 8333, dnsSeeds: dnsSeeds.mainnet }
}
; (async () => {
// resolve nodes from seeds
const dnsServers = dnsSeeds[currentNetwork]
const dnsServer = dnsServers[~~(Math.random() * dnsServers.length)]
const addresses = await dns.resolve(dnsServer).catch(() => null)
console.log(`Discovered ${addresses.length} nodes`)
// connect to peer
const address = addresses[~~(Math.random() * addresses.length)]
const peer = { ip: address, port: networks[currentNetwork].port }
client.connect(peer.port, peer.ip, () => {
console.log(`Connected to ${address}`)
})
})()