-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathnoise.js
More file actions
34 lines (27 loc) · 989 Bytes
/
noise.js
File metadata and controls
34 lines (27 loc) · 989 Bytes
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
/* eslint-disable no-console */
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { tcp } from '@libp2p/tcp'
import { createLibp2p } from 'libp2p'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
const createNode = async () => {
const node = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [tcp()],
streamMuxers: [yamux()],
connectionEncrypters: [noise()]
})
return node
}
const node1 = await createNode()
const node2 = await createNode()
node2.handle('/a-protocol', (stream) => {
stream.addEventListener('message', (evt) => {
console.log(uint8ArrayToString(evt.data.subarray()))
})
})
const stream = await node1.dialProtocol(node2.getMultiaddrs(), '/a-protocol')
stream.send(uint8ArrayFromString('This information is sent out encrypted to the other peer'))