@libp2p/noise 17.0.1-71e8ee163 → 17.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -35
- package/dist/index.min.js +2 -4
- package/dist/index.min.js.map +4 -4
- package/dist/src/crypto/index.browser.js +1 -1
- package/dist/src/crypto/index.d.ts +5 -1
- package/dist/src/crypto/index.d.ts.map +1 -1
- package/dist/src/crypto/index.js +4 -3
- package/dist/src/crypto/index.js.map +1 -1
- package/dist/src/crypto/js.d.ts +1 -1
- package/dist/src/crypto.d.ts +1 -1
- package/dist/src/index.d.ts +25 -14
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +23 -12
- package/dist/src/index.js.map +1 -1
- package/dist/src/logger.d.ts +2 -2
- package/dist/src/logger.js +1 -1
- package/dist/src/noise.d.ts +3 -3
- package/dist/src/noise.js +6 -6
- package/dist/src/nonce.js +1 -1
- package/dist/src/nonce.js.map +1 -1
- package/dist/src/performHandshake.d.ts +1 -1
- package/dist/src/performHandshake.js +3 -3
- package/dist/src/proto/payload.d.ts +34 -4
- package/dist/src/proto/payload.d.ts.map +1 -1
- package/dist/src/proto/payload.js +95 -13
- package/dist/src/proto/payload.js.map +1 -1
- package/dist/src/protocol.d.ts +3 -3
- package/dist/src/protocol.d.ts.map +1 -1
- package/dist/src/protocol.js +2 -2
- package/dist/src/types.d.ts +2 -2
- package/dist/src/utils.d.ts +2 -2
- package/dist/src/utils.js +1 -1
- package/dist/typedoc-urls.json +17 -0
- package/package.json +61 -35
- package/src/crypto/index.browser.ts +1 -1
- package/src/crypto/index.ts +8 -5
- package/src/crypto/js.ts +2 -2
- package/src/crypto.ts +1 -1
- package/src/index.ts +26 -15
- package/src/logger.ts +3 -3
- package/src/noise.ts +10 -10
- package/src/nonce.ts +1 -1
- package/src/performHandshake.ts +4 -4
- package/src/proto/payload.ts +133 -9
- package/src/protocol.ts +3 -3
- package/src/types.ts +2 -2
- package/src/utils.ts +2 -2
package/dist/src/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Nonce } from './nonce.
|
|
2
|
-
import type { NoiseExtensions, NoiseHandshakePayload } from './proto/payload.
|
|
1
|
+
import type { Nonce } from './nonce.ts';
|
|
2
|
+
import type { NoiseExtensions, NoiseHandshakePayload } from './proto/payload.ts';
|
|
3
3
|
import type { ConnectionEncrypter, Logger, PrivateKey, PublicKey } from '@libp2p/interface';
|
|
4
4
|
import type { LengthPrefixedStream } from '@libp2p/utils';
|
|
5
5
|
import type { Uint8ArrayList } from 'uint8arraylist';
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Uint8ArrayList } from 'uint8arraylist';
|
|
2
|
-
import { NoiseHandshakePayload } from './proto/payload.
|
|
2
|
+
import { NoiseHandshakePayload } from './proto/payload.ts';
|
|
3
3
|
import type { MetricsRegistry } from './metrics.ts';
|
|
4
|
-
import type { NoiseExtensions } from './proto/payload.
|
|
4
|
+
import type { NoiseExtensions } from './proto/payload.ts';
|
|
5
5
|
import type { HandshakeResult } from './types.ts';
|
|
6
6
|
import type { MessageStream, PrivateKey, PublicKey } from '@libp2p/interface';
|
|
7
7
|
export declare function createHandshakePayload(privateKey: PrivateKey, staticPublicKey: Uint8Array | Uint8ArrayList, extensions?: NoiseExtensions): Promise<Uint8Array | Uint8ArrayList>;
|
package/dist/src/utils.js
CHANGED
|
@@ -6,7 +6,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat';
|
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
7
7
|
import { CHACHA_TAG_LENGTH, NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from "./constants.js";
|
|
8
8
|
import { uint16BEEncode, uint16BEDecode } from "./encoder.js";
|
|
9
|
-
import { NoiseHandshakePayload } from
|
|
9
|
+
import { NoiseHandshakePayload } from "./proto/payload.js";
|
|
10
10
|
export async function createHandshakePayload(privateKey, staticPublicKey, extensions) {
|
|
11
11
|
const identitySig = await privateKey.sign(getSignaturePayload(staticPublicKey));
|
|
12
12
|
return NoiseHandshakePayload.encode({
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"asCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.crypto.asCrypto.html",
|
|
3
|
+
"./crypto:asCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.crypto.asCrypto.html",
|
|
4
|
+
"defaultCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.crypto.defaultCrypto.html",
|
|
5
|
+
"./crypto:defaultCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.crypto.defaultCrypto.html",
|
|
6
|
+
"nodeCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.crypto.nodeCrypto.html",
|
|
7
|
+
"./crypto:nodeCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.crypto.nodeCrypto.html",
|
|
8
|
+
"ICryptoInterface": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_noise.index.ICryptoInterface.html",
|
|
9
|
+
"KeyPair": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_noise.index.KeyPair.html",
|
|
10
|
+
"NoiseComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_noise.index.NoiseComponents.html",
|
|
11
|
+
".:NoiseComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_noise.index.NoiseComponents.html",
|
|
12
|
+
"NoiseExtensions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_noise.index.NoiseExtensions.html",
|
|
13
|
+
"NoiseInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_noise.index.NoiseInit.html",
|
|
14
|
+
"pureJsCrypto": "https://libp2p.github.io/js-libp2p/variables/_libp2p_noise.index.pureJsCrypto.html",
|
|
15
|
+
"noise": "https://libp2p.github.io/js-libp2p/functions/_libp2p_noise.index.noise.html",
|
|
16
|
+
".:noise": "https://libp2p.github.io/js-libp2p/functions/_libp2p_noise.index.noise.html"
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/noise",
|
|
3
|
-
"version": "17.0.
|
|
3
|
+
"version": "17.0.2",
|
|
4
4
|
"description": "Noise libp2p handshake for js-libp2p",
|
|
5
5
|
"author": "ChainSafe <info@chainsafe.io>",
|
|
6
6
|
"license": "Apache-2.0 OR MIT",
|
|
@@ -23,6 +23,22 @@
|
|
|
23
23
|
],
|
|
24
24
|
"type": "module",
|
|
25
25
|
"types": "./dist/src/index.d.ts",
|
|
26
|
+
"typesVersions": {
|
|
27
|
+
"*": {
|
|
28
|
+
"*": [
|
|
29
|
+
"*",
|
|
30
|
+
"dist/*",
|
|
31
|
+
"dist/src/*",
|
|
32
|
+
"dist/src/*/index"
|
|
33
|
+
],
|
|
34
|
+
"src/*": [
|
|
35
|
+
"*",
|
|
36
|
+
"dist/*",
|
|
37
|
+
"dist/src/*",
|
|
38
|
+
"dist/src/*/index"
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
26
42
|
"files": [
|
|
27
43
|
"src",
|
|
28
44
|
"dist",
|
|
@@ -32,63 +48,73 @@
|
|
|
32
48
|
"exports": {
|
|
33
49
|
".": {
|
|
34
50
|
"types": "./dist/src/index.d.ts",
|
|
35
|
-
"import": "./dist/src/index.js"
|
|
51
|
+
"import": "./dist/src/index.js",
|
|
52
|
+
"module-sync": "./dist/src/index.js"
|
|
53
|
+
},
|
|
54
|
+
"./crypto": {
|
|
55
|
+
"types": "./dist/src/crypto/index.d.ts",
|
|
56
|
+
"import": "./dist/src/crypto/index.js",
|
|
57
|
+
"module-sync": "./dist/src/crypto/index.js"
|
|
36
58
|
}
|
|
37
59
|
},
|
|
38
60
|
"scripts": {
|
|
39
|
-
"bench": "node benchmarks/benchmark.js",
|
|
40
61
|
"clean": "aegir clean",
|
|
62
|
+
"lint": "aegir lint",
|
|
41
63
|
"dep-check": "aegir dep-check",
|
|
64
|
+
"doc-check": "aegir doc-check",
|
|
42
65
|
"build": "aegir build",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
66
|
+
"bench": "node benchmarks/benchmark.js",
|
|
67
|
+
"generate": "protons ./src/proto/payload.proto",
|
|
45
68
|
"test": "aegir test",
|
|
46
|
-
"test:
|
|
47
|
-
"test:
|
|
69
|
+
"test:chrome": "aegir test -t browser --cov",
|
|
70
|
+
"test:chrome-webworker": "aegir test -t webworker",
|
|
71
|
+
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
72
|
+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
73
|
+
"test:node": "aegir test -t node --cov",
|
|
48
74
|
"test:electron-main": "aegir test -t electron-main",
|
|
49
|
-
"test:interop": "aegir test -t node -f dist/test/interop.js"
|
|
50
|
-
"docs": "aegir docs",
|
|
51
|
-
"proto:gen": "protons ./src/proto/payload.proto",
|
|
52
|
-
"prepublish": "npm run build"
|
|
75
|
+
"test:interop": "aegir test -t node -f dist/test/interop.js"
|
|
53
76
|
},
|
|
54
77
|
"dependencies": {
|
|
55
78
|
"@chainsafe/as-chacha20poly1305": "^0.1.0",
|
|
56
79
|
"@chainsafe/as-sha256": "^1.2.0",
|
|
57
|
-
"@libp2p/crypto": "5.1.
|
|
58
|
-
"@libp2p/interface": "3.
|
|
59
|
-
"@libp2p/peer-id": "6.0.
|
|
60
|
-
"@libp2p/utils": "7.
|
|
80
|
+
"@libp2p/crypto": "^5.1.23",
|
|
81
|
+
"@libp2p/interface": "^3.3.0",
|
|
82
|
+
"@libp2p/peer-id": "^6.0.15",
|
|
83
|
+
"@libp2p/utils": "^7.4.1",
|
|
61
84
|
"@noble/ciphers": "^2.0.1",
|
|
62
85
|
"@noble/curves": "^2.0.1",
|
|
63
86
|
"@noble/hashes": "^2.0.1",
|
|
64
|
-
"protons-runtime": "^
|
|
65
|
-
"uint8arraylist": "^
|
|
66
|
-
"uint8arrays": "^
|
|
87
|
+
"protons-runtime": "^7.0.0",
|
|
88
|
+
"uint8arraylist": "^3.0.2",
|
|
89
|
+
"uint8arrays": "^6.1.1",
|
|
67
90
|
"wherearewe": "^2.0.1"
|
|
68
91
|
},
|
|
69
92
|
"devDependencies": {
|
|
70
|
-
"@libp2p/daemon-client": "10.0.
|
|
71
|
-
"@libp2p/daemon-server": "9.0.
|
|
72
|
-
"@libp2p/interface-compliance-tests": "7.0.
|
|
73
|
-
"@libp2p/interop": "14.0.
|
|
74
|
-
"@libp2p/logger": "6.
|
|
75
|
-
"@libp2p/tcp": "11.0.
|
|
76
|
-
"@libp2p/yamux": "8.0.
|
|
77
|
-
"@multiformats/multiaddr": "^13.0.
|
|
78
|
-
"@types/sinon": "^
|
|
79
|
-
"aegir": "^
|
|
80
|
-
"
|
|
81
|
-
"
|
|
93
|
+
"@libp2p/daemon-client": "^10.0.38",
|
|
94
|
+
"@libp2p/daemon-server": "^9.0.38",
|
|
95
|
+
"@libp2p/interface-compliance-tests": "^7.0.32",
|
|
96
|
+
"@libp2p/interop": "^14.0.38",
|
|
97
|
+
"@libp2p/logger": "^6.2.13",
|
|
98
|
+
"@libp2p/tcp": "^11.0.28",
|
|
99
|
+
"@libp2p/yamux": "^8.0.3",
|
|
100
|
+
"@multiformats/multiaddr": "^13.0.3",
|
|
101
|
+
"@types/sinon": "^22.0.0",
|
|
102
|
+
"aegir": "^48.1.1",
|
|
103
|
+
"benchmark": "^2.1.4",
|
|
104
|
+
"execa": "^10.0.0",
|
|
105
|
+
"go-libp2p": "^1.7.0",
|
|
82
106
|
"iso-random-stream": "^2.0.2",
|
|
83
|
-
"
|
|
107
|
+
"it-pair": "^2.0.6",
|
|
108
|
+
"libp2p": "^3.3.11",
|
|
84
109
|
"mkdirp": "^3.0.1",
|
|
85
|
-
"multiformats": "^
|
|
110
|
+
"multiformats": "^14.0.0",
|
|
86
111
|
"p-defer": "^4.0.1",
|
|
87
|
-
"protons": "^
|
|
88
|
-
"sinon": "^
|
|
112
|
+
"protons": "^9.0.1",
|
|
113
|
+
"sinon": "^22.0.0",
|
|
89
114
|
"sinon-ts": "^2.0.0"
|
|
90
115
|
},
|
|
91
116
|
"browser": {
|
|
92
117
|
"./dist/src/crypto/index.js": "./dist/src/crypto/index.browser.js"
|
|
93
|
-
}
|
|
118
|
+
},
|
|
119
|
+
"sideEffects": false
|
|
94
120
|
}
|
package/src/crypto/index.ts
CHANGED
|
@@ -3,16 +3,19 @@ import { newInstance, ChaCha20Poly1305 } from '@chainsafe/as-chacha20poly1305'
|
|
|
3
3
|
import { digest } from '@chainsafe/as-sha256'
|
|
4
4
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
5
|
import { isElectronMain } from 'wherearewe'
|
|
6
|
-
import { pureJsCrypto } from './js.
|
|
7
|
-
import type { ICryptoInterface } from '../crypto.
|
|
8
|
-
import type { KeyPair } from '../types.
|
|
6
|
+
import { pureJsCrypto } from './js.ts'
|
|
7
|
+
import type { ICryptoInterface } from '../crypto.ts'
|
|
8
|
+
import type { KeyPair } from '../types.ts'
|
|
9
9
|
|
|
10
10
|
const ctx = newInstance()
|
|
11
11
|
const asImpl = new ChaCha20Poly1305(ctx)
|
|
12
12
|
const CHACHA_POLY1305 = 'chacha20-poly1305'
|
|
13
13
|
const PKCS8_PREFIX = Buffer.from([0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20])
|
|
14
14
|
const X25519_PREFIX = Buffer.from([0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x03, 0x21, 0x00])
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
export { pureJsCrypto }
|
|
17
|
+
|
|
18
|
+
export const nodeCrypto: Pick<ICryptoInterface, 'hashSHA256' | 'chaCha20Poly1305Encrypt' | 'chaCha20Poly1305Decrypt'> = {
|
|
16
19
|
hashSHA256 (data) {
|
|
17
20
|
const hash = crypto.createHash('sha256')
|
|
18
21
|
|
|
@@ -104,7 +107,7 @@ const nodeCrypto: Pick<ICryptoInterface, 'hashSHA256' | 'chaCha20Poly1305Encrypt
|
|
|
104
107
|
}
|
|
105
108
|
}
|
|
106
109
|
|
|
107
|
-
const asCrypto: Pick<ICryptoInterface, 'hashSHA256' | 'chaCha20Poly1305Encrypt' | 'chaCha20Poly1305Decrypt'> = {
|
|
110
|
+
export const asCrypto: Pick<ICryptoInterface, 'hashSHA256' | 'chaCha20Poly1305Encrypt' | 'chaCha20Poly1305Decrypt'> = {
|
|
108
111
|
hashSHA256 (data) {
|
|
109
112
|
return digest(data.subarray())
|
|
110
113
|
},
|
package/src/crypto/js.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { chacha20poly1305 } from '@noble/ciphers/chacha.js'
|
|
|
2
2
|
import { x25519 } from '@noble/curves/ed25519.js'
|
|
3
3
|
import { extract, expand } from '@noble/hashes/hkdf.js'
|
|
4
4
|
import { sha256 } from '@noble/hashes/sha2.js'
|
|
5
|
-
import type { ICryptoInterface } from '../crypto.
|
|
6
|
-
import type { KeyPair } from '../types.
|
|
5
|
+
import type { ICryptoInterface } from '../crypto.ts'
|
|
6
|
+
import type { KeyPair } from '../types.ts'
|
|
7
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
|
|
9
9
|
export const pureJsCrypto: ICryptoInterface = {
|
package/src/crypto.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,27 +3,38 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This repository contains TypeScript implementation of noise protocol, an encryption protocol used in libp2p.
|
|
5
5
|
*
|
|
6
|
+
* Formerly published as [`@chainsafe/libp2p-noise`](https://www.npmjs.com/package/@chainsafe/libp2p-noise).
|
|
7
|
+
*
|
|
6
8
|
* ## Usage
|
|
7
9
|
*
|
|
8
|
-
* Install with `
|
|
10
|
+
* Install with `npm i @libp2p/noise`.
|
|
9
11
|
*
|
|
10
12
|
* Example of using default noise configuration and passing it to the libp2p config:
|
|
11
13
|
*
|
|
12
14
|
* ```ts
|
|
13
|
-
* import {createLibp2p} from
|
|
14
|
-
* import {noise} from
|
|
15
|
-
*
|
|
16
|
-
* //custom noise configuration, pass it instead of `noise()`
|
|
17
|
-
* //x25519 private key
|
|
18
|
-
* const n = noise({ staticNoiseKey });
|
|
15
|
+
* import { createLibp2p } from 'libp2p'
|
|
16
|
+
* import { noise } from '@libp2p/noise'
|
|
19
17
|
*
|
|
20
18
|
* const libp2p = await createLibp2p({
|
|
21
19
|
* connectionEncrypters: [noise()],
|
|
22
|
-
*
|
|
20
|
+
* // ... other options
|
|
23
21
|
* })
|
|
24
22
|
* ```
|
|
25
23
|
*
|
|
26
|
-
* See the [NoiseInit](https://github.com/
|
|
24
|
+
* See the [NoiseInit](https://github.com/libp2p/js-libp2p/blob/main/packages/connection-encrypter-noise/src/noise.ts) interface for noise configuration options.
|
|
25
|
+
*
|
|
26
|
+
* To use a fixed x25519 static key instead of generating one per session, pass it as `staticNoiseKey`:
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { createLibp2p } from 'libp2p'
|
|
30
|
+
* import { noise, pureJsCrypto } from '@libp2p/noise'
|
|
31
|
+
*
|
|
32
|
+
* const staticNoiseKey = pureJsCrypto.generateX25519KeyPair().privateKey
|
|
33
|
+
*
|
|
34
|
+
* const node = await createLibp2p({
|
|
35
|
+
* connectionEncrypters: [noise({ staticNoiseKey })]
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
27
38
|
*
|
|
28
39
|
* ## API
|
|
29
40
|
*
|
|
@@ -33,16 +44,16 @@
|
|
|
33
44
|
*
|
|
34
45
|
* 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.
|
|
35
46
|
*
|
|
36
|
-
* The implementation must conform to the `ICryptoInterface`, defined in <https://github.com/
|
|
47
|
+
* The implementation must conform to the `ICryptoInterface`, defined in <https://github.com/libp2p/js-libp2p/blob/main/packages/connection-encrypter-noise/src/crypto.ts>
|
|
37
48
|
*/
|
|
38
49
|
|
|
39
|
-
import { Noise } from './noise.
|
|
40
|
-
import type { NoiseInit, NoiseExtensions } from './noise.
|
|
41
|
-
import type { KeyPair } from './types.
|
|
50
|
+
import { Noise } from './noise.ts'
|
|
51
|
+
import type { NoiseInit, NoiseExtensions } from './noise.ts'
|
|
52
|
+
import type { KeyPair } from './types.ts'
|
|
42
53
|
import type { ComponentLogger, ConnectionEncrypter, Metrics, PeerId, PrivateKey, Upgrader } from '@libp2p/interface'
|
|
43
54
|
|
|
44
|
-
export { pureJsCrypto } from './crypto/js.
|
|
45
|
-
export type { ICryptoInterface } from './crypto.
|
|
55
|
+
export { pureJsCrypto } from './crypto/js.ts'
|
|
56
|
+
export type { ICryptoInterface } from './crypto.ts'
|
|
46
57
|
export type { NoiseInit, NoiseExtensions, KeyPair }
|
|
47
58
|
|
|
48
59
|
export interface NoiseComponents {
|
package/src/logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
2
|
-
import { DUMP_SESSION_KEYS } from './constants.
|
|
3
|
-
import type { CipherState } from './protocol.
|
|
4
|
-
import type { KeyPair } from './types.
|
|
2
|
+
import { DUMP_SESSION_KEYS } from './constants.ts'
|
|
3
|
+
import type { CipherState } from './protocol.ts'
|
|
4
|
+
import type { KeyPair } from './types.ts'
|
|
5
5
|
import type { Logger } from '@libp2p/interface'
|
|
6
6
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
7
7
|
|
package/src/noise.ts
CHANGED
|
@@ -3,17 +3,17 @@ import { InvalidCryptoExchangeError, serviceCapabilities } from '@libp2p/interfa
|
|
|
3
3
|
import { peerIdFromPublicKey } from '@libp2p/peer-id'
|
|
4
4
|
import { lpStream } from '@libp2p/utils'
|
|
5
5
|
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
6
|
-
import { NOISE_MSG_MAX_LENGTH_BYTES } from './constants.
|
|
7
|
-
import { defaultCrypto } from './crypto/index.
|
|
8
|
-
import { wrapCrypto } from './crypto.
|
|
9
|
-
import { uint16BEDecode, uint16BEEncode } from './encoder.
|
|
10
|
-
import { registerMetrics } from './metrics.
|
|
11
|
-
import { performHandshakeInitiator, performHandshakeResponder } from './performHandshake.
|
|
6
|
+
import { NOISE_MSG_MAX_LENGTH_BYTES } from './constants.ts'
|
|
7
|
+
import { defaultCrypto } from './crypto/index.ts'
|
|
8
|
+
import { wrapCrypto } from './crypto.ts'
|
|
9
|
+
import { uint16BEDecode, uint16BEEncode } from './encoder.ts'
|
|
10
|
+
import { registerMetrics } from './metrics.ts'
|
|
11
|
+
import { performHandshakeInitiator, performHandshakeResponder } from './performHandshake.ts'
|
|
12
12
|
import { toMessageStream } from './utils.ts'
|
|
13
|
-
import type { ICryptoInterface } from './crypto.
|
|
14
|
-
import type { NoiseComponents } from './index.
|
|
15
|
-
import type { MetricsRegistry } from './metrics.
|
|
16
|
-
import type { HandshakeResult, ICrypto, INoiseConnection, INoiseExtensions, KeyPair } from './types.
|
|
13
|
+
import type { ICryptoInterface } from './crypto.ts'
|
|
14
|
+
import type { NoiseComponents } from './index.ts'
|
|
15
|
+
import type { MetricsRegistry } from './metrics.ts'
|
|
16
|
+
import type { HandshakeResult, ICrypto, INoiseConnection, INoiseExtensions, KeyPair } from './types.ts'
|
|
17
17
|
import type { SecuredConnection, PrivateKey, PublicKey, StreamMuxerFactory, SecureConnectionOptions, Logger, MessageStream } from '@libp2p/interface'
|
|
18
18
|
import type { LengthPrefixedStream } from '@libp2p/utils'
|
|
19
19
|
|
package/src/nonce.ts
CHANGED
|
@@ -9,7 +9,7 @@ export const MIN_NONCE = 0
|
|
|
9
9
|
// this MAX_NONCE is still a large number of messages, so the practical effect of this is negligible.
|
|
10
10
|
export const MAX_NONCE = 0xffffffff
|
|
11
11
|
|
|
12
|
-
const ERR_MAX_NONCE = '
|
|
12
|
+
const ERR_MAX_NONCE = 'Cipherstate has reached maximum n, a new handshake must be performed'
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* The nonce is an uint that's increased over time.
|
package/src/performHandshake.ts
CHANGED
|
@@ -4,10 +4,10 @@ import {
|
|
|
4
4
|
logRemoteEphemeralKey,
|
|
5
5
|
logRemoteStaticKey,
|
|
6
6
|
logCipherState
|
|
7
|
-
} from './logger.
|
|
8
|
-
import { ZEROLEN, XXHandshakeState } from './protocol.
|
|
9
|
-
import { createHandshakePayload, decodeHandshakePayload } from './utils.
|
|
10
|
-
import type { HandshakeResult, HandshakeParams } from './types.
|
|
7
|
+
} from './logger.ts'
|
|
8
|
+
import { ZEROLEN, XXHandshakeState } from './protocol.ts'
|
|
9
|
+
import { createHandshakePayload, decodeHandshakePayload } from './utils.ts'
|
|
10
|
+
import type { HandshakeResult, HandshakeParams } from './types.ts'
|
|
11
11
|
import type { AbortOptions } from '@libp2p/interface'
|
|
12
12
|
|
|
13
13
|
export async function performHandshakeInitiator (init: HandshakeParams, options?: AbortOptions): Promise<HandshakeResult> {
|
package/src/proto/payload.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { decodeMessage, encodeMessage, MaxLengthError, message } from 'protons-runtime'
|
|
1
|
+
import { decodeMessage, encodeMessage, MaxLengthError, message, streamMessage } from 'protons-runtime'
|
|
2
2
|
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
3
3
|
import type { Codec, DecodeOptions } from 'protons-runtime'
|
|
4
4
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
@@ -18,14 +18,14 @@ export namespace NoiseExtensions {
|
|
|
18
18
|
w.fork()
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
if (obj.webtransportCerthashes != null) {
|
|
21
|
+
if (obj.webtransportCerthashes != null && obj.webtransportCerthashes.length > 0) {
|
|
22
22
|
for (const value of obj.webtransportCerthashes) {
|
|
23
23
|
w.uint32(10)
|
|
24
24
|
w.bytes(value)
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
if (obj.streamMuxers != null) {
|
|
28
|
+
if (obj.streamMuxers != null && obj.streamMuxers.length > 0) {
|
|
29
29
|
for (const value of obj.streamMuxers) {
|
|
30
30
|
w.uint32(18)
|
|
31
31
|
w.string(value)
|
|
@@ -49,7 +49,7 @@ export namespace NoiseExtensions {
|
|
|
49
49
|
switch (tag >>> 3) {
|
|
50
50
|
case 1: {
|
|
51
51
|
if (opts.limits?.webtransportCerthashes != null && obj.webtransportCerthashes.length === opts.limits.webtransportCerthashes) {
|
|
52
|
-
throw new MaxLengthError('Decode error -
|
|
52
|
+
throw new MaxLengthError('Decode error - repeated field "webtransportCerthashes" had too many elements')
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
obj.webtransportCerthashes.push(reader.bytes())
|
|
@@ -57,7 +57,7 @@ export namespace NoiseExtensions {
|
|
|
57
57
|
}
|
|
58
58
|
case 2: {
|
|
59
59
|
if (opts.limits?.streamMuxers != null && obj.streamMuxers.length === opts.limits.streamMuxers) {
|
|
60
|
-
throw new MaxLengthError('Decode error -
|
|
60
|
+
throw new MaxLengthError('Decode error - repeated field "streamMuxers" had too many elements')
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
obj.streamMuxers.push(reader.string())
|
|
@@ -71,19 +71,83 @@ export namespace NoiseExtensions {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
return obj
|
|
74
|
+
}, function * (reader, length, prefix, opts = {}) {
|
|
75
|
+
const obj = {
|
|
76
|
+
webtransportCerthashes: 0,
|
|
77
|
+
streamMuxers: 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const end = length == null ? reader.len : reader.pos + length
|
|
81
|
+
|
|
82
|
+
while (reader.pos < end) {
|
|
83
|
+
const tag = reader.uint32()
|
|
84
|
+
|
|
85
|
+
switch (tag >>> 3) {
|
|
86
|
+
case 1: {
|
|
87
|
+
if (opts.limits?.webtransportCerthashes != null && obj.webtransportCerthashes === opts.limits.webtransportCerthashes) {
|
|
88
|
+
throw new MaxLengthError('Streaming decode error - repeated field "webtransportCerthashes" had too many elements')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
yield {
|
|
92
|
+
field: `${prefix}.webtransportCerthashes[]`,
|
|
93
|
+
index: obj.webtransportCerthashes,
|
|
94
|
+
value: reader.bytes()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
obj.webtransportCerthashes++
|
|
98
|
+
|
|
99
|
+
break
|
|
100
|
+
}
|
|
101
|
+
case 2: {
|
|
102
|
+
if (opts.limits?.streamMuxers != null && obj.streamMuxers === opts.limits.streamMuxers) {
|
|
103
|
+
throw new MaxLengthError('Streaming decode error - repeated field "streamMuxers" had too many elements')
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
yield {
|
|
107
|
+
field: `${prefix}.streamMuxers[]`,
|
|
108
|
+
index: obj.streamMuxers,
|
|
109
|
+
value: reader.string()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
obj.streamMuxers++
|
|
113
|
+
|
|
114
|
+
break
|
|
115
|
+
}
|
|
116
|
+
default: {
|
|
117
|
+
reader.skipType(tag & 7)
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
74
122
|
})
|
|
75
123
|
}
|
|
76
124
|
|
|
77
125
|
return _codec
|
|
78
126
|
}
|
|
79
127
|
|
|
80
|
-
export
|
|
128
|
+
export interface NoiseExtensionsWebtransportCerthashesFieldEvent {
|
|
129
|
+
field: '$.webtransportCerthashes[]'
|
|
130
|
+
index: number
|
|
131
|
+
value: Uint8Array
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface NoiseExtensionsStreamMuxersFieldEvent {
|
|
135
|
+
field: '$.streamMuxers[]'
|
|
136
|
+
index: number
|
|
137
|
+
value: string
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function encode (obj: Partial<NoiseExtensions>): Uint8Array<ArrayBuffer> {
|
|
81
141
|
return encodeMessage(obj, NoiseExtensions.codec())
|
|
82
142
|
}
|
|
83
143
|
|
|
84
|
-
export
|
|
144
|
+
export function decode (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<NoiseExtensions>): NoiseExtensions {
|
|
85
145
|
return decodeMessage(buf, NoiseExtensions.codec(), opts)
|
|
86
146
|
}
|
|
147
|
+
|
|
148
|
+
export function stream (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<NoiseExtensions>): Generator<NoiseExtensionsWebtransportCerthashesFieldEvent | NoiseExtensionsStreamMuxersFieldEvent> {
|
|
149
|
+
return streamMessage(buf, NoiseExtensions.codec(), opts)
|
|
150
|
+
}
|
|
87
151
|
}
|
|
88
152
|
|
|
89
153
|
export interface NoiseHandshakePayload {
|
|
@@ -154,17 +218,77 @@ export namespace NoiseHandshakePayload {
|
|
|
154
218
|
}
|
|
155
219
|
|
|
156
220
|
return obj
|
|
221
|
+
}, function * (reader, length, prefix, opts = {}) {
|
|
222
|
+
const end = length == null ? reader.len : reader.pos + length
|
|
223
|
+
|
|
224
|
+
while (reader.pos < end) {
|
|
225
|
+
const tag = reader.uint32()
|
|
226
|
+
|
|
227
|
+
switch (tag >>> 3) {
|
|
228
|
+
case 1: {
|
|
229
|
+
yield {
|
|
230
|
+
field: `${prefix}.identityKey`,
|
|
231
|
+
value: reader.bytes()
|
|
232
|
+
}
|
|
233
|
+
break
|
|
234
|
+
}
|
|
235
|
+
case 2: {
|
|
236
|
+
yield {
|
|
237
|
+
field: `${prefix}.identitySig`,
|
|
238
|
+
value: reader.bytes()
|
|
239
|
+
}
|
|
240
|
+
break
|
|
241
|
+
}
|
|
242
|
+
case 4: {
|
|
243
|
+
yield * NoiseExtensions.codec().stream(reader, reader.uint32(), `${prefix}.extensions`, {
|
|
244
|
+
limits: opts.limits?.extensions
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
break
|
|
248
|
+
}
|
|
249
|
+
default: {
|
|
250
|
+
reader.skipType(tag & 7)
|
|
251
|
+
break
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
157
255
|
})
|
|
158
256
|
}
|
|
159
257
|
|
|
160
258
|
return _codec
|
|
161
259
|
}
|
|
162
260
|
|
|
163
|
-
export
|
|
261
|
+
export interface NoiseHandshakePayloadIdentityKeyFieldEvent {
|
|
262
|
+
field: '$.identityKey'
|
|
263
|
+
value: Uint8Array
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface NoiseHandshakePayloadIdentitySigFieldEvent {
|
|
267
|
+
field: '$.identitySig'
|
|
268
|
+
value: Uint8Array
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export interface NoiseHandshakePayloadExtensionsWebtransportCerthashesFieldEvent {
|
|
272
|
+
field: '$.extensions.webtransportCerthashes[]'
|
|
273
|
+
index: number
|
|
274
|
+
value: Uint8Array
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export interface NoiseHandshakePayloadExtensionsStreamMuxersFieldEvent {
|
|
278
|
+
field: '$.extensions.streamMuxers[]'
|
|
279
|
+
index: number
|
|
280
|
+
value: string
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function encode (obj: Partial<NoiseHandshakePayload>): Uint8Array<ArrayBuffer> {
|
|
164
284
|
return encodeMessage(obj, NoiseHandshakePayload.codec())
|
|
165
285
|
}
|
|
166
286
|
|
|
167
|
-
export
|
|
287
|
+
export function decode (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<NoiseHandshakePayload>): NoiseHandshakePayload {
|
|
168
288
|
return decodeMessage(buf, NoiseHandshakePayload.codec(), opts)
|
|
169
289
|
}
|
|
290
|
+
|
|
291
|
+
export function stream (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<NoiseHandshakePayload>): Generator<NoiseHandshakePayloadIdentityKeyFieldEvent | NoiseHandshakePayloadIdentitySigFieldEvent | NoiseHandshakePayloadExtensionsWebtransportCerthashesFieldEvent | NoiseHandshakePayloadExtensionsStreamMuxersFieldEvent> {
|
|
292
|
+
return streamMessage(buf, NoiseHandshakePayload.codec(), opts)
|
|
293
|
+
}
|
|
170
294
|
}
|
package/src/protocol.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
2
2
|
import { fromString as uint8ArrayFromString } from 'uint8arrays'
|
|
3
3
|
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
4
|
-
import { InvalidCryptoExchangeError } from './errors.
|
|
5
|
-
import { Nonce } from './nonce.
|
|
6
|
-
import type { ICipherState, ISymmetricState, IHandshakeState, KeyPair, ICrypto } from './types.
|
|
4
|
+
import { InvalidCryptoExchangeError } from './errors.ts'
|
|
5
|
+
import { Nonce } from './nonce.ts'
|
|
6
|
+
import type { ICipherState, ISymmetricState, IHandshakeState, KeyPair, ICrypto } from './types.ts'
|
|
7
7
|
|
|
8
8
|
// Code in this file is a direct translation of a subset of the noise protocol https://noiseprotocol.org/noise.html,
|
|
9
9
|
// agnostic to libp2p's usage of noise
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Nonce } from './nonce.
|
|
2
|
-
import type { NoiseExtensions, NoiseHandshakePayload } from './proto/payload.
|
|
1
|
+
import type { Nonce } from './nonce.ts'
|
|
2
|
+
import type { NoiseExtensions, NoiseHandshakePayload } from './proto/payload.ts'
|
|
3
3
|
import type { ConnectionEncrypter, Logger, PrivateKey, PublicKey } from '@libp2p/interface'
|
|
4
4
|
import type { LengthPrefixedStream } from '@libp2p/utils'
|
|
5
5
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
package/src/utils.ts
CHANGED
|
@@ -6,9 +6,9 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
7
|
import { CHACHA_TAG_LENGTH, NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants.ts'
|
|
8
8
|
import { uint16BEEncode, uint16BEDecode } from './encoder.ts'
|
|
9
|
-
import { NoiseHandshakePayload } from './proto/payload.
|
|
9
|
+
import { NoiseHandshakePayload } from './proto/payload.ts'
|
|
10
10
|
import type { MetricsRegistry } from './metrics.ts'
|
|
11
|
-
import type { NoiseExtensions } from './proto/payload.
|
|
11
|
+
import type { NoiseExtensions } from './proto/payload.ts'
|
|
12
12
|
import type { HandshakeResult } from './types.ts'
|
|
13
13
|
import type { AbortOptions, MessageStream, PrivateKey, PublicKey, StreamCloseEvent } from '@libp2p/interface'
|
|
14
14
|
import type { SendResult } from '@libp2p/utils'
|