Skip to content

feat!: streams as EventTargets #3218

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

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions .github/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ additionals
SECG
Certicom
RSAES
dialback
chacha
peerStore
xxhandshake
zerolen
connmanager
reprovide
reprovider
reproviding
Expand Down
7 changes: 7 additions & 0 deletions packages/connection-encrypter-noise/.aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

/** @type {import('aegir').PartialOptions} */
export default {
docs: {
entryPoint: "src/index.ts"
}
}
95 changes: 95 additions & 0 deletions packages/connection-encrypter-noise/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# @chainsafe/libp2p-noise

![npm](https://img.shields.io/npm/v/@chainsafe/libp2p-noise)
[![](https://img.shields.io/github/actions/workflow/status/ChainSafe/js-libp2p-noise/js-test-and-release.yml?branch=master)](https://github.com/ChainSafe/js-libp2p-noise/actions)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](https://libp2p.io/)
![](https://img.shields.io/github/issues-raw/ChainSafe/js-libp2p-noise)
[![License Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![License MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![](https://img.shields.io/badge/npm-%3E%3D7.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D16.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/browsers-last%202%20versions%2C%20not%20ie%20%3C%3D11-orange)
[![Twitter](https://img.shields.io/twitter/follow/ChainSafeth.svg?label=Twitter)](https://twitter.com/ChainSafeth)
[![Discord](https://img.shields.io/discord/593655374469660673.svg?label=Discord\&logo=discord)](https://discord.gg/Q6A3YA2)

> Noise libp2p handshake for js-libp2p

# About

<!--

!IMPORTANT!

Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.

To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts

To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.

-->

This repository contains TypeScript implementation of noise protocol, an encryption protocol used in libp2p.

## Usage

Install with `yarn add @chainsafe/libp2p-noise` or `npm i @chainsafe/libp2p-noise`.

Example of using default noise configuration and passing it to the libp2p config:

```ts
import {createLibp2p} from "libp2p"
import {noise} from "@chainsafe/libp2p-noise"

//custom noise configuration, pass it instead of `noise()`
//x25519 private key
const n = noise({ staticNoiseKey });

const libp2p = await createLibp2p({
connectionEncrypters: [noise()],
//... other options
})
```

See the [NoiseInit](https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/noise.ts#L22-L30) interface for noise configuration options.

## API

This module exposes an implementation of the [ConnectionEncrypter](https://libp2p.github.io/js-libp2p/interfaces/_libp2p_interface.ConnectionEncrypter.html) interface.

## Bring your own crypto

You can provide a custom crypto implementation (instead of the default, based on [@noble](https://paulmillr.com/noble/)) by adding a `crypto` field to the init argument passed to the `Noise` factory.

The implementation must conform to the `ICryptoInterface`, defined in <https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/crypto.ts>

# Install

```console
$ npm i @chainsafe/libp2p-noise
```

## Browser `<script>` tag

Loading this module through a script tag will make its exports available as `ChainsafeLibp2pNoise` in the global namespace.

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

# API Docs

- <https://ChainSafe.github.io/js-libp2p-noise>

# License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](https://github.com/ChainSafe/js-libp2p-noise/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/ChainSafe/js-libp2p-noise/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.
188 changes: 188 additions & 0 deletions packages/connection-encrypter-noise/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"name": "@chainsafe/libp2p-noise",
"version": "16.1.4",
"description": "Noise libp2p handshake for js-libp2p",
"author": "ChainSafe <[email protected]>",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/ChainSafe/js-libp2p-noise#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/ChainSafe/js-libp2p-noise.git"
},
"bugs": {
"url": "https://github.com/ChainSafe/js-libp2p-noise/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"crypto",
"libp2p",
"noise"
],
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"release": {
"branches": [
"master"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"releaseRules": [
{
"breaking": true,
"release": "major"
},
{
"revert": true,
"release": "patch"
},
{
"type": "feat",
"release": "minor"
},
{
"type": "fix",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
},
{
"type": "test",
"release": "patch"
},
{
"type": "deps",
"release": "patch"
},
{
"scope": "no-release",
"release": false
}
]
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"presetConfig": {
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Trivial Changes"
},
{
"type": "docs",
"section": "Documentation"
},
{
"type": "deps",
"section": "Dependencies"
},
{
"type": "test",
"section": "Tests"
}
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
[
"@semantic-release/git",
{
"assets": [
"CHANGELOG.md",
"package.json"
]
}
]
]
},
"scripts": {
"bench": "node benchmarks/benchmark.js",
"clean": "aegir clean",
"dep-check": "aegir dep-check",
"build": "aegir build",
"lint": "aegir lint",
"lint:fix": "aegir lint --fix",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker",
"test:electron-main": "aegir test -t electron-main",
"test:interop": "aegir test -t node -f dist/test/interop.js",
"docs": "aegir docs",
"proto:gen": "protons ./src/proto/payload.proto",
"prepublish": "npm run build",
"release": "aegir release"
},
"dependencies": {
"@chainsafe/as-chacha20poly1305": "^0.1.0",
"@chainsafe/as-sha256": "^1.0.0",
"@libp2p/crypto": "^5.0.0",
"@libp2p/interface": "^2.9.0",
"@libp2p/peer-id": "^5.0.0",
"@libp2p/utils": "^6.7.1",
"@noble/ciphers": "^1.1.3",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"protons-runtime": "^5.5.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^5.0.0",
"wherearewe": "^2.0.1"
},
"devDependencies": {
"@chainsafe/libp2p-yamux": "^7.0.0",
"@libp2p/daemon-client": "^9.0.0",
"@libp2p/daemon-server": "^8.0.0",
"@libp2p/interface-compliance-tests": "^6.0.0",
"@libp2p/interop": "^13.0.0",
"@libp2p/logger": "^5.0.0",
"@libp2p/tcp": "^10.0.0",
"@multiformats/multiaddr": "^12.1.0",
"@types/sinon": "^17.0.1",
"aegir": "^47.0.18",
"execa": "^9.3.0",
"go-libp2p": "^1.0.3",
"iso-random-stream": "^2.0.2",
"libp2p": "^2.0.0",
"mkdirp": "^3.0.0",
"multiformats": "^13.2.2",
"p-defer": "^4.0.0",
"protons": "^7.6.0",
"sinon": "^21.0.0",
"sinon-ts": "^2.0.0"
},
"browser": {
"./dist/src/crypto/index.js": "./dist/src/crypto/index.browser.js"
}
}
5 changes: 5 additions & 0 deletions packages/connection-encrypter-noise/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const NOISE_MSG_MAX_LENGTH_BYTES = 65535
export const NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG = NOISE_MSG_MAX_LENGTH_BYTES - 16

export const DUMP_SESSION_KEYS = Boolean(globalThis.process?.env?.DUMP_SESSION_KEYS)
export const CHACHA_TAG_LENGTH = 16
27 changes: 27 additions & 0 deletions packages/connection-encrypter-noise/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { ICrypto, KeyPair } from './types.js'
import type { Uint8ArrayList } from 'uint8arraylist'

/** Underlying crypto implementation, meant to be overridable */
export interface ICryptoInterface {
hashSHA256(data: Uint8Array | Uint8ArrayList): Uint8Array

getHKDF(ck: Uint8Array, ikm: Uint8Array): [Uint8Array, Uint8Array, Uint8Array]

generateX25519KeyPair(): KeyPair
generateX25519KeyPairFromSeed(seed: Uint8Array): KeyPair
generateX25519SharedKey(privateKey: Uint8Array | Uint8ArrayList, publicKey: Uint8Array | Uint8ArrayList): Uint8Array

chaCha20Poly1305Encrypt(plaintext: Uint8Array | Uint8ArrayList, nonce: Uint8Array, ad: Uint8Array, k: Uint8Array): Uint8ArrayList | Uint8Array
chaCha20Poly1305Decrypt(ciphertext: Uint8Array | Uint8ArrayList, nonce: Uint8Array, ad: Uint8Array, k: Uint8Array, dst?: Uint8Array): Uint8ArrayList | Uint8Array
}

export function wrapCrypto (crypto: ICryptoInterface): ICrypto {
return {
generateKeypair: crypto.generateX25519KeyPair,
dh: (keypair, publicKey) => crypto.generateX25519SharedKey(keypair.privateKey, publicKey).subarray(0, 32),
encrypt: crypto.chaCha20Poly1305Encrypt,
decrypt: crypto.chaCha20Poly1305Decrypt,
hash: crypto.hashSHA256,
hkdf: crypto.getHKDF
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { pureJsCrypto } from './js.js'

export const defaultCrypto = pureJsCrypto
Loading
Loading