-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path6.2-template-code.js
113 lines (93 loc) · 5.37 KB
/
6.2-template-code.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
const PeerInfo = require('peer-info')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WStar = require('libp2p-webrtc-star')
const Wrtc = require('wrtc')
const multiaddr = require('multiaddr')
const Mplex = require('libp2p-mplex')
const Secio = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
const MDNS = require('libp2p-mdns')
const KadDHT = require('libp2p-kad-dht')
// TODO: Include libp2p-gossipsub module
// TODO: change this include pubsub chat class as Pubsubchat
const Chat = require('./5.0-finished-code.js')
const transportKey = WStar.prototype[Symbol.toStringTag]
const isBootstrap = process.argv[2] == '--bootstrap'
const info = {
"id": "QmWjz6xb8v9K4KnYEwP5Yk75k5mMBCehzWFLCvvQpYxF3d",
"privKey": "CAASpwkwggSjAgEAAoIBAQDKNKwPX4DJhYdGreAVaJy+efhIfbyczR0Mfyi/JfzszY9INH83Veo2s/yOKv+YOP4y7OWpkXL5G6K8fLgxwq5gtTc78W07uz5ZUrxfOT0R4QJuiiQHjQSxYKw08yLIP9JaR2ztL46DOO/Nvzl9gCWHGsAb+w+RLWa0R0SRyvaDiw8aZW9G70yYTGF/SPkEoYN26sioVDwppmKxZ9mTuKsujG0AGAMVPnmjhDI5WmBD3gnOiqCECqlgxl29Qlc1fCIbojcUVE9eWFWassFLicGdo/iMacsVvoTav9JvHZsMvg1HXeK0khQWluCUfdcR6coijDMDWBa77dTI6+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yYCF47U+csbRzGb/lszRwg1QvGtTvzQWuNAcAKclCuN7xplJJ+DUyvVA00WCz/z8MMa/PK8nB0KoUDfuFvo8bbNEAPcGK0l/He7+hF4wdm4S8fX22up5GgJUdV/dv8KZdE2U7yIU/i8BKw6Z3vJB7RB900yfjt56VlgsKspAB0=",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDKNKwPX4DJhYdGreAVaJy+efhIfbyczR0Mfyi/JfzszY9INH83Veo2s/yOKv+YOP4y7OWpkXL5G6K8fLgxwq5gtTc78W07uz5ZUrxfOT0R4QJuiiQHjQSxYKw08yLIP9JaR2ztL46DOO/Nvzl9gCWHGsAb+w+RLWa0R0SRyvaDiw8aZW9G70yYTGF/SPkEoYN26sioVDwppmKxZ9mTuKsujG0AGAMVPnmjhDI5WmBD3gnOiqCECqlgxl29Qlc1fCIbojcUVE9eWFWassFLicGdo/iMacsVvoTav9JvHZsMvg1HXeK0khQWluCUfdcR6coijDMDWBa77dTI6+b2ybZXAgMBAAE="
}
let options = {
modules: {
transport: [ TCP, WS, WStar ],
connEncryption: [ Secio ],
streamMuxer: [ Mplex ],
peerDiscovery: [ Bootstrap, MDNS ],
dht: KadDHT
// TODO: modify the config to add gossipsub implementation for pubsub
},
config: {
transport: {
[transportKey]: {
Wrtc
}
},
peerDiscovery: {
bootstrap: {
list: [
'/ip4/127.0.0.1/tcp/63785/ipfs/QmWjz6xb8v9K4KnYEwP5Yk75k5mMBCehzWFLCvvQpYxF3d'
]
},
dht: {
enabled: true,
randomwalk: {
enabled: true
}
}
}
}
}
// TODO: remove this function as the publish automatically sends message to all nodes subsrcibed to it
async function sendMessageToAll(message, libp2p) {
message = message.slice(0, -1)
let peers = libp2p.peerBook.getAllArray()
peers.forEach(async (peerInfo) => {
if(!peerInfo.isConnected() || !peerInfo.protocols.has(Chat.PROTOCOL)) return
libp2p.dialProtocol(peerInfo, Chat.PROTOCOL, (err, stream) => {
if (err) return console.error('Could not negotiate chat protocol stream with peer', err)
Chat.send(message, stream)
})
})
}
async function main() {
let peerInfo = await PeerInfo.create(info);
if(isBootstrap) options.peerInfo = peerInfo;
// Create a libp2p instance
let libp2p = await Libp2p.create(options)
libp2p.on('peer:connect', (peerInfo) => {
console.info(`Connected to ${peerInfo.id.toB58String()}!`)
})
// TODO: create a pubsub chat object and pass libp2p and default topic
if(isBootstrap) {
libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/63785')
libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/63786/ws')
} else {
libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
libp2p.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0/ws')
}
// Handle Message Recieved
// TODO: remove the handler as we have handler in the pubsub class
libp2p.handle(Chat.PROTOCOL, Chat.handler)
// Send Message on User Input
// TODO: modify below to call pubsubChat.sendMessage(message) and bind the object
process.stdin.on('data', message => sendMessageToAll(String(message), libp2p))
// TODO: replace with start method of pubsubchar object as it starts a libp2p node internally
await libp2p.start()
console.info(`Libp2p Started`)
libp2p.peerInfo.multiaddrs.forEach(ma => console.log(ma.toString()))
}
main()