Skip to content

Commit 721151b

Browse files
vasco-santosjacobheun
authored andcommitted
refactor: examples/pnet (#523)
* refactor: examples/pnet * chore: rename pnet-ipfs to pnet * chore: address review
1 parent efc96c2 commit 721151b

File tree

8 files changed

+92
-212
lines changed

8 files changed

+92
-212
lines changed

examples/pnet-ipfs/index.js

Lines changed: 0 additions & 145 deletions
This file was deleted.

examples/pnet-ipfs/libp2p-bundle.js

Lines changed: 0 additions & 60 deletions
This file was deleted.
File renamed without changes.

examples/pnet-ipfs/README.md renamed to examples/pnet/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Private Networking with IPFS
2-
This example shows how to set up a private network of IPFS nodes.
1+
# Private Networking
2+
This example shows how to set up a private network of libp2p nodes.
33

44
## Setup
55
Install dependencies:

examples/pnet/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* eslint no-console: ["off"] */
2+
'use strict'
3+
4+
const { generate } = require('libp2p/src/pnet')
5+
const privateLibp2pNode = require('./libp2p-node')
6+
7+
const pipe = require('it-pipe')
8+
9+
// Create a buffer and write the swarm key to it
10+
const swarmKey = Buffer.alloc(95)
11+
generate(swarmKey)
12+
13+
// This key is for testing a different key not working
14+
const otherSwarmKey = Buffer.alloc(95)
15+
generate(otherSwarmKey)
16+
17+
;(async () => {
18+
const node1 = await privateLibp2pNode(swarmKey)
19+
20+
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
21+
const node2 = await privateLibp2pNode(swarmKey)
22+
// const node2 = await privateLibp2pNode(otherSwarmKey)
23+
24+
await Promise.all([
25+
node1.start(),
26+
node2.start()
27+
])
28+
29+
console.log('nodes started...')
30+
31+
await node1.dial(node2.peerInfo)
32+
33+
node2.handle('/private', ({ stream }) => {
34+
pipe(
35+
stream,
36+
async function (source) {
37+
for await (const msg of source) {
38+
console.log(msg.toString())
39+
}
40+
}
41+
)
42+
})
43+
44+
const { stream } = await node1.dialProtocol(node2.peerInfo, '/private')
45+
46+
await pipe(
47+
['This message is sent on a private network'],
48+
stream
49+
)
50+
})();

examples/pnet/libp2p-node.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const Libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const MPLEX = require('libp2p-mplex')
6+
const SECIO = require('libp2p-secio')
7+
const Protector = require('libp2p/src/pnet')
8+
9+
/**
10+
* privateLibp2pNode returns a libp2p node function that will use the swarm
11+
* key at the given `swarmKeyPath` to create the Protector
12+
*
13+
* @param {Buffer} swarmKey
14+
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
15+
*/
16+
const privateLibp2pNode = async (swarmKeyPath) => {
17+
const node = await Libp2p.create({
18+
modules: {
19+
transport: [TCP], // We're only using the TCP transport for this example
20+
streamMuxer: [MPLEX], // We're only using mplex muxing
21+
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
22+
// care about node identity, and only the presence of private keys
23+
connEncryption: [SECIO],
24+
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
25+
// being left in for explicit readability.
26+
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
27+
peerDiscovery: [],
28+
connProtector: new Protector(swarmKeyPath)
29+
}
30+
})
31+
32+
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
33+
return node
34+
}
35+
36+
module.exports = privateLibp2pNode

examples/pnet-ipfs/package.json renamed to examples/pnet/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14-
"ipfs": "^0.38.0",
15-
"libp2p": "^0.26.2",
16-
"libp2p-mplex": "^0.8.5",
17-
"libp2p-secio": "^0.11.1",
18-
"libp2p-tcp": "^0.13.2"
14+
"libp2p": "../..",
15+
"libp2p-mplex": "^0.9.3",
16+
"libp2p-secio": "^0.12.1",
17+
"libp2p-tcp": "^0.14.2"
1918
}
2019
}
File renamed without changes.

0 commit comments

Comments
 (0)