Skip to content

Commit 1b30f81

Browse files
authored
docs: update fetch and pnet docs (#1516)
The docs in these modules are out of date
1 parent 7bbf6fd commit 1b30f81

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

src/fetch/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
libp2p-fetch JavaScript Implementation
1+
libp2p-fetch JavaScript Implementation <!-- omit in toc -->
22
=====================================
33

44
> Libp2p fetch protocol JavaScript implementation
55
6+
## Table of contents <!-- omit in toc -->
7+
8+
- [Overview](#overview)
9+
- [Usage](#usage)
10+
611
## Overview
712

813
An implementation of the Fetch protocol as described here: https://github.com/libp2p/specs/tree/master/fetch
@@ -27,7 +32,7 @@ async function my_subsystem_key_lookup(key) {
2732
}
2833

2934
// Enable this peer to respond to fetch requests for keys that begin with '/my_subsystem_key_prefix/'
30-
const libp2p = Libp2p.create(...)
35+
const libp2p = createLibp2p()
3136
libp2p.fetchService.registerLookupFunction('/my_subsystem_key_prefix/', my_subsystem_key_lookup)
3237

3338
const key = '/my_subsystem_key_prefix/{...}'

src/pnet/README.md

+31-29
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
js-libp2p-pnet
1+
js-libp2p-pnet <!-- omit in toc -->
22
==================
33

44
> Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
55
66
**Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-pnet.
77

8-
## Table of Contents
8+
## Table of Contents <!-- omit in toc -->
99

10-
- [js-libp2p-pnet](#js-libp2p-pnet)
11-
- [Table of Contents](#table-of-contents)
12-
- [Usage](#usage)
13-
- [Examples](#examples)
14-
- [Private Shared Keys](#private-shared-keys)
15-
- [PSK Generation](#psk-generation)
16-
- [From libp2p-pnet](#from-libp2p-pnet)
17-
- [From a module using libp2p](#from-a-module-using-libp2p)
18-
- [Programmatically](#programmatically)
10+
- [Usage](#usage)
11+
- [Examples](#examples)
12+
- [Private Shared Keys](#private-shared-keys)
13+
- [PSK Generation](#psk-generation)
14+
- [From a module using libp2p](#from-a-module-using-libp2p)
15+
- [Programmatically](#programmatically)
1916

2017
## Usage
2118

2219
```js
23-
const Protector from 'libp2p-pnet')
24-
const protector = new Protector(swarmKeyBuffer)
25-
const privateConnection = protector.protect(myPublicConnection, (err) => { })
20+
import { createLibp2p } from 'libp2p'
21+
import { preSharedKey, generateKey } from 'libp2p/pnet'
22+
23+
// Create a Uint8Array and write the swarm key to it
24+
const swarmKey = new Uint8Array(95)
25+
generateKey(swarmKey)
26+
27+
const node = await createLibp2p({
28+
// ...other options
29+
connectionProtector: preSharedKey({
30+
psk: swarmKey
31+
})
32+
})
2633
```
2734

28-
### Examples
35+
## Examples
2936
[Private Networks with IPFS](../../examples/pnet-ipfs)
3037

31-
### Private Shared Keys
38+
## Private Shared Keys
3239

3340
Private Shared Keys are expected to be in the following format:
3441

@@ -38,33 +45,28 @@ Private Shared Keys are expected to be in the following format:
3845
dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
3946
```
4047

41-
### PSK Generation
48+
## PSK Generation
4249

4350
A utility method has been created to generate a key for your private network. You can
4451
use one of the methods below to generate your key.
4552

46-
#### From libp2p-pnet
47-
48-
If you have libp2p locally, you can run the following from the projects root.
49-
50-
```sh
51-
node ./src/pnet/key-generator.js > swarm.key
52-
```
53-
5453
#### From a module using libp2p
5554

5655
If you have a module locally that depends on libp2p, you can run the following from
5756
that project, assuming the node_modules are installed.
5857

59-
```sh
60-
node -e "require('libp2p/src/pnet').generate(process.stdout)" > swarm.key
58+
```console
59+
node -e "import('libp2p/pnet').then(({ generateKey }) => generateKey(process.stdout))" > swarm.key
6160
```
6261

6362
#### Programmatically
6463

6564
```js
66-
const writeKey from 'libp2p/src/pnet').generate
65+
import fs from 'fs'
66+
import { generateKey } from 'libp2p/pnet'
67+
6768
const swarmKey = new Uint8Array(95)
68-
writeKey(swarmKey)
69+
generateKey(swarmKey)
70+
6971
fs.writeFileSync('swarm.key', swarmKey)
7072
```

src/pnet/key-generator.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
55
/**
66
* Generates a PSK that can be used in a libp2p-pnet private network
77
*
8-
* @param {Uint8Array} bytes - An object to write the psk into
8+
* @param {Uint8Array | NodeJS.WriteStream} bytes - An object to write the psk into
99
* @returns {void}
1010
*/
11-
export function generateKey (bytes: Uint8Array) {
11+
export function generateKey (bytes: Uint8Array | NodeJS.WriteStream) {
1212
const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16')
1313
const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
1414

15-
bytes.set(key)
15+
if (bytes instanceof Uint8Array) {
16+
bytes.set(key)
17+
} else {
18+
bytes.write(key)
19+
}
1620
}
1721

1822
export const NONCE_LENGTH = 24
1923
export const KEY_LENGTH = 32
20-
21-
try {
22-
if (require.main === module) {
23-
// @ts-expect-error
24-
generate(process.stdout)
25-
}
26-
} catch (error: any) {
27-
28-
}

0 commit comments

Comments
 (0)