Skip to content

Commit bab8b69

Browse files
authored
feat: add pnet example (#175)
Restores old pnet example
1 parent d8439b3 commit bab8b69

File tree

11 files changed

+240
-0
lines changed

11 files changed

+240
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
- js-libp2p-example-delegated-routing
2929
- js-libp2p-example-discovery-mechanisms
3030
- js-libp2p-example-peer-and-content-routing
31+
- js-libp2p-example-pnet
3132
- js-libp2p-example-webrtc-private-to-private
3233
defaults:
3334
run:
@@ -84,6 +85,7 @@ jobs:
8485
- js-libp2p-example-delegated-routing
8586
- js-libp2p-example-discovery-mechanisms
8687
- js-libp2p-example-peer-and-content-routing
88+
- js-libp2p-example-pnet
8789
- js-libp2p-example-webrtc-private-to-private
8890
steps:
8991
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ⚠️ IMPORTANT ⚠️
2+
3+
# Please do not create a Pull Request for this repository
4+
5+
The contents of this repository are automatically synced from the parent [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) so any changes made to the standalone repository will be lost after the next sync.
6+
7+
Please open a PR against [js-libp2p Examples](https://github.com/libp2p/js-libp2p-examples) instead.
8+
9+
## Contributing
10+
11+
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.
12+
13+
1. Fork the [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples)
14+
2. Create your Feature Branch (`git checkout -b feature/amazing-example`)
15+
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`)
16+
4. Push to the Branch (`git push origin feature/amazing-example`)
17+
5. Open a Pull Request
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: pull
2+
3+
on:
4+
workflow_dispatch
5+
6+
jobs:
7+
sync:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Pull from another repository
12+
uses: ipfs-examples/actions-pull-directory-from-repo@main
13+
with:
14+
source-repo: libp2p/js-libp2p-examples
15+
source-folder-path: examples/${{ github.event.repository.name }}
16+
source-branch: main
17+
target-branch: main
18+
git-username: github-actions
19+
git-email: [email protected]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# @libp2p/example-pnet
2+
3+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
5+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-examples.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-examples)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-examples/ci.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p-examples/actions/workflows/ci.yml?query=branch%3Amain)
7+
8+
> How to configure a libp2p private network
9+
10+
libp2p networks allow any peer to connect to any other peer and to communicate
11+
with them via protocol streams.
12+
13+
What if you only want to allow a certain subset of peers to connect to you? It's
14+
possible to use a pre-shared key to create a private network on top of the
15+
public libp2p network using the `@libp2p/pnet` module.
16+
17+
## Setup
18+
19+
1. Install the modules in the libp2p root directory, `npm install` and`npm run build`.
20+
21+
## Run
22+
23+
Running the example will cause two nodes with the same swarm key to be started
24+
and exchange basic information.
25+
26+
```
27+
node index.js
28+
```
29+
30+
### Using different keys
31+
32+
This example includes `TASK` comments that can be used to try the example with
33+
different swarm keys. This will allow you to see how nodes will fail to connect
34+
if they are on different private networks and try to connect to one another.
35+
36+
To change the swarm key of one of the nodes, look through `index.js` for
37+
comments starting with `TASK` to indicate where lines are that pertain to
38+
changing the swarm key of node 2.
39+
40+
### Exploring the repos
41+
42+
Once you've run the example you can take a look at the repos in the `./tmp`
43+
directory to see how they differ, including the swarm keys. You should see a
44+
`swarm.key` file in each of the repos and when the nodes are on the same private
45+
network this contents of the `swarm.key` files should be the same.
46+
47+
## License
48+
49+
Licensed under either of
50+
51+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
52+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
53+
54+
## Contribution
55+
56+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint no-console: ["off"] */
2+
3+
import { generateKey } from '@libp2p/pnet'
4+
import { pipe } from 'it-pipe'
5+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7+
import { privateLibp2pNode } from './libp2p-node.js'
8+
9+
// Create a Uint8Array and write the swarm key to it
10+
const swarmKey = new Uint8Array(95)
11+
generateKey(swarmKey)
12+
13+
// This key is for testing a different key not working
14+
const otherSwarmKey = new Uint8Array(95)
15+
generateKey(otherSwarmKey)
16+
17+
const node1 = await privateLibp2pNode(swarmKey)
18+
19+
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
20+
const node2 = await privateLibp2pNode(swarmKey)
21+
// const node2 = await privateLibp2pNode(otherSwarmKey)
22+
23+
console.log('nodes started...')
24+
25+
// connect node1 to node2
26+
await node1.dial(node2.getMultiaddrs())
27+
28+
node2.handle('/private', ({ stream }) => {
29+
pipe(
30+
stream,
31+
async function (source) {
32+
for await (const msg of source) {
33+
console.log(uint8ArrayToString(msg.subarray()))
34+
}
35+
}
36+
)
37+
})
38+
39+
const stream = await node1.dialProtocol(node2.peerId, '/private')
40+
41+
await pipe(
42+
[uint8ArrayFromString('This message is sent on a private network')],
43+
stream
44+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { noise } from '@chainsafe/libp2p-noise'
2+
import { yamux } from '@chainsafe/libp2p-yamux'
3+
import { preSharedKey } from '@libp2p/pnet'
4+
import { tcp } from '@libp2p/tcp'
5+
import { createLibp2p } from 'libp2p'
6+
7+
/**
8+
* privateLibp2pNode returns a libp2p node function that will use the swarm
9+
* key with the given `swarmKey` to create the Protector
10+
*
11+
* @param {any} swarmKey
12+
*/
13+
export async function privateLibp2pNode (swarmKey) {
14+
const node = await createLibp2p({
15+
addresses: {
16+
listen: ['/ip4/0.0.0.0/tcp/0']
17+
},
18+
transports: [tcp()], // We're only using the TCP transport for this example
19+
streamMuxers: [yamux()], // We're only using yamux muxing
20+
// Let's make sure to use identifying crypto in our pnet since the protector
21+
// doesn't care about node identity, and only the presence of private keys
22+
connectionEncrypters: [noise()],
23+
// Leave peer discovery empty, we don't want to find peers. We could omit
24+
// the property, but it's being left in for explicit readability.
25+
// We should explicitly dial pnet peers, or use a custom discovery service
26+
// for finding nodes in our pnet
27+
peerDiscovery: [],
28+
connectionProtector: preSharedKey({
29+
psk: swarmKey
30+
})
31+
})
32+
33+
return node
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@libp2p/example-pnet",
3+
"version": "0.0.0",
4+
"description": "How to configure a libp2p private network",
5+
"license": "Apache-2.0 OR MIT",
6+
"homepage": "https://github.com/libp2p/js-libp2p-examples/tree/master/examples/js-libp2p-example-pnet#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/libp2p/js-libp2p-examples.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/libp2p/js-libp2p-examples/issues"
13+
},
14+
"type": "module",
15+
"scripts": {
16+
"test": "test-node-example test/*"
17+
},
18+
"dependencies": {
19+
"@chainsafe/libp2p-noise": "^16.0.0",
20+
"@chainsafe/libp2p-yamux": "^7.0.0",
21+
"@libp2p/pnet": "^2.0.1",
22+
"@libp2p/tcp": "^10.0.0",
23+
"it-pipe": "^3.0.1",
24+
"libp2p": "^2.0.0",
25+
"uint8arrays": "^5.1.0"
26+
},
27+
"devDependencies": {
28+
"test-ipfs-example": "^1.1.0"
29+
},
30+
"private": true
31+
}

0 commit comments

Comments
 (0)