Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: move pnet into separate package #2165

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/migrations/v0.46-v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `

- [AutoNAT](#autonat)
- [KeyChain](#keychain)
- [Pnet](#pnet)
- [Metrics](#metrics)

## AutoNAT
Expand Down Expand Up @@ -54,6 +55,22 @@ const libp2p = await createLibp2p({
const keychain: Keychain = libp2p.services.keychain
```

## Pnet

The pnet module is now published in its own package.

**Before**

```ts
import { preSharedKey, generateKey } from 'libp2p/pnet'
```

**After**

```ts
import { preSharedKey, generateKey } from '@libp2p/pnet'
```

## Metrics

The following metrics were renamed:
Expand Down
8 changes: 1 addition & 7 deletions packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@
"types": "./dist/src/ping/index.d.ts",
"import": "./dist/src/ping/index.js"
},
"./pnet": {
"types": "./dist/src/pnet/index.d.ts",
"import": "./dist/src/pnet/index.js"
},
"./upnp-nat": {
"types": "./dist/src/upnp-nat/index.d.ts",
"import": "./dist/src/upnp-nat/index.js"
Expand Down Expand Up @@ -157,8 +153,7 @@
"rate-limiter-flexible": "^3.0.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^4.0.6",
"wherearewe": "^2.0.1",
"xsalsa20": "^1.1.0"
"wherearewe": "^2.0.1"
},
"devDependencies": {
"@chainsafe/libp2p-gossipsub": "^10.0.0",
Expand All @@ -175,7 +170,6 @@
"@libp2p/mplex": "^9.0.9",
"@libp2p/tcp": "^8.0.10",
"@libp2p/websockets": "^7.0.10",
"@types/xsalsa20": "^1.1.0",
"aegir": "^41.0.2",
"execa": "^8.0.1",
"go-libp2p": "^1.1.1",
Expand Down
68 changes: 0 additions & 68 deletions packages/libp2p/src/pnet/README.md

This file was deleted.

13 changes: 5 additions & 8 deletions packages/libp2p/test/connection-manager/direct.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
import { codes as ErrorCodes } from '../../src/errors.js'
import { plaintext } from '../../src/insecure/index.js'
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
import { preSharedKey } from '../../src/pnet/index.js'
import { DefaultTransportManager } from '../../src/transport-manager.js'
import swarmKey from '../fixtures/swarm.key.js'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
import type { Multiaddr } from '@multiformats/multiaddr'

const swarmKeyBuffer = uint8ArrayFromString(swarmKey)
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')

Expand Down Expand Up @@ -496,9 +493,11 @@ describe('libp2p.dialer (direct, TCP)', () => {
})

it('should use the protectors when provided for connecting', async () => {
const protector: ConnectionProtector = preSharedKey({
psk: swarmKeyBuffer
})()
const protector: ConnectionProtector = {
async protect (connection) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is just a fake, now, this is a more isolated test to connection-manager's use of protectors, which is good.

do we need to test that the arguments passed to connection protector are correct?

does the protector have tests to confirm it is doing the right thing since we're no longer confirming that as a side-effect of this test?

return connection
}
}

libp2p = await createLibp2pNode({
peerId,
Expand All @@ -517,8 +516,6 @@ describe('libp2p.dialer (direct, TCP)', () => {

const protectorProtectSpy = sinon.spy(protector, 'protect')

remoteLibp2p.components.connectionProtector = preSharedKey({ psk: swarmKeyBuffer })()

await libp2p.start()

const connection = await libp2p.dial(remoteAddr)
Expand Down
3 changes: 0 additions & 3 deletions packages/libp2p/test/fixtures/swarm.key.ts

This file was deleted.

21 changes: 13 additions & 8 deletions packages/libp2p/test/upgrading/upgrader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import { codes } from '../../src/errors.js'
import { createLibp2p } from '../../src/index.js'
import { plaintext } from '../../src/insecure/index.js'
import { preSharedKey } from '../../src/pnet/index.js'
import { DEFAULT_MAX_OUTBOUND_STREAMS } from '../../src/registrar.js'
import { DefaultUpgrader } from '../../src/upgrader.js'
import swarmKey from '../fixtures/swarm.key.js'
import type { Libp2p } from '@libp2p/interface'
import type { Connection, ConnectionProtector, Stream } from '@libp2p/interface/connection'
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter'
Expand Down Expand Up @@ -206,9 +204,12 @@
it('should use a private connection protector when provided', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })

const protector = preSharedKey({
psk: uint8ArrayFromString(swarmKey)
})()
const protector: ConnectionProtector = {
async protect (connection) {
return connection
}
}

const protectorProtectSpy = sinon.spy(protector, 'protect')

localComponents.connectionProtector = protector
Expand Down Expand Up @@ -615,6 +616,12 @@
it('should create an Upgrader', async () => {
const deferred = pDefer<Components>()

const protector: ConnectionProtector = {
async protect (connection) {
return connection
}

Check warning on line 622 in packages/libp2p/test/upgrading/upgrader.spec.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/test/upgrading/upgrader.spec.ts#L621-L622

Added lines #L621 - L622 were not covered by tests
}

libp2p = await createLibp2p({
peerId: peers[0],
transports: [
Expand All @@ -627,9 +634,7 @@
connectionEncryption: [
plaintext()
],
connectionProtector: preSharedKey({
psk: uint8ArrayFromString(swarmKey)
}),
connectionProtector: () => protector,
services: {
test: (components: any) => {
deferred.resolve(components)
Expand Down
1 change: 0 additions & 1 deletion packages/libp2p/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"./src/identify/index.ts",
"./src/insecure/index.ts",
"./src/ping/index.ts",
"./src/pnet/index.ts",
"./src/upnp-nat/index.ts"
]
}
4 changes: 4 additions & 0 deletions packages/pnet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/pnet/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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

http://www.apache.org/licenses/LICENSE-2.0

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.
19 changes: 19 additions & 0 deletions packages/pnet/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
89 changes: 89 additions & 0 deletions packages/pnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
> Connection protection management for libp2p leveraging PSK encryption via XSalsa20.

[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)

> Implementation of Connection protection management via a shared secret

# About

Connection protection management for libp2p leveraging PSK encryption via XSalsa20.

## Example

```typescript
import { createLibp2p } from 'libp2p'
import { preSharedKey, generateKey } from '@libp2p/pnet'

// Create a Uint8Array and write the swarm key to it
const swarmKey = new Uint8Array(95)
generateKey(swarmKey)

const node = await createLibp2p({
// ...other options
connectionProtector: preSharedKey({
psk: swarmKey
})
})
```

## Private Shared Keys

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

```
/key/swarm/psk/1.0.0/
/base16/
dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
```

## PSK Generation

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

### From a module using libp2p

If you have a module locally that depends on libp2p, you can run the following from that project, assuming the node\_modules are installed.

```console
node -e "import('@libp2p/pnet').then(({ generateKey }) => generateKey(process.stdout))" > swarm.key
```

### Programmatically

```js
import fs from 'fs'
import { generateKey } from '@libp2p/pnet'

const swarmKey = new Uint8Array(95)
generateKey(swarmKey)

fs.writeFileSync('swarm.key', swarmKey)
```

# Install

```console
$ npm i @libp2p/pnet
```

## Browser `<script>` tag

Loading this module through a script tag will make it's exports available as `Libp2pPnet` in the global namespace.

```html
<script src="https://unpkg.com/@libp2p/pnet/dist/index.min.js"></script>
```

# License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

# Contribution

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.
Loading
Loading