-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalice.js
88 lines (81 loc) · 2.89 KB
/
alice.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {createLibp2p} from "libp2p"
import {createFromJSON} from "@libp2p/peer-id-factory"
import {webSockets} from "@libp2p/websockets"
import {noise} from "@chainsafe/libp2p-noise"
import {gossipsub} from "@chainsafe/libp2p-gossipsub"
import {mplex} from "@libp2p/mplex"
import {tcp} from "@libp2p/tcp"
import {pubsubPeerDiscovery} from "@libp2p/pubsub-peer-discovery"
async function start() {
const peerId = {
id: "12D3KooWLV3w42LqUb9MWE7oTzG7vwaFjPw9GvDqmsuDif5chTn9",
privKey:
"CAESYI44p8HiCHtCBhuUcetU9XdIEtWvon15a5ZLsfyssSj9nn3mt4oZI0t6wXTHOvIA0GSFWrYkdKp1338oFIambdKefea3ihkjS3rBdMc68gDQZIVatiR0qnXffygUhqZt0g==",
pubKey: "CAESIJ595reKGSNLesF0xzryANBkhVq2JHSqdd9/KBSGpm3S",
}
try {
const libp2p = await createLibp2p({
peerId: await createFromJSON(peerId),
addresses: {
listen: [
"/ip4/0.0.0.0/tcp/5001/p2p/12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p/p2p-circuit",
"/ip4/0.0.0.0/tcp/5002/ws/p2p/12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p/p2p-circuit",
],
},
pubsub: gossipsub({
allowPublishToZeroPeers: true,
}),
transports: [tcp()],
// transports: [tcp({
// outboundSocketInactivityTimeout: 0,
// inboundSocketInactivityTimeout: 0
// })],
//transports: [webSockets()],
connectionEncryption: [noise()],
streamMuxers: [mplex()],
peerDiscovery: [
// @ts-ignore package has broken typings
pubsubPeerDiscovery({
interval: 1000,
}),
],
relay: {
// Circuit Relay options (this config is part of libp2p core configurations)
enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay.
autoRelay: {
enabled: true,
maxListeners: 2,
},
}
})
let connected = false
// Listen for new connections to peers
libp2p.connectionManager.addEventListener("peer:connect", async (evt) => {
const connection = evt.detail
connected = true
console.log(`Connected to ${connection.remotePeer.toString()}`)
console.time("disconnected after")
console.timeEnd("reconnected after")
})
// Listen for peers disconnecting
libp2p.connectionManager.addEventListener("peer:disconnect", (evt) => {
const connection = evt.detail
console.log(`Disconnected from ${connection.remotePeer.toString()}`)
console.timeEnd("disconnected after")
console.time("reconnected after")
connected = false
})
await libp2p.start()
console.log("----------------------------------------------")
console.log("PeerId:", libp2p.peerId.toString())
console.log(
"Listening on:",
libp2p.getMultiaddrs().map((it) => it.toString()),
)
console.log("----------------------------------------------")
return libp2p
} catch (err) {
console.error(err)
}
}
start()