@libp2p/noise 12.0.1 → 16.1.4-8484de8a2
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 +95 -0
- package/dist/index.min.js +16 -20
- package/dist/index.min.js.map +7 -0
- package/dist/src/constants.d.ts +1 -0
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +1 -0
- package/dist/src/constants.js.map +1 -1
- package/dist/src/crypto/index.browser.d.ts +2 -0
- package/dist/src/crypto/index.browser.d.ts.map +1 -0
- package/dist/src/crypto/index.browser.js +3 -0
- package/dist/src/crypto/index.browser.js.map +1 -0
- package/dist/src/crypto/index.d.ts +3 -0
- package/dist/src/crypto/index.d.ts.map +1 -0
- package/dist/src/crypto/index.js +188 -0
- package/dist/src/crypto/index.js.map +1 -0
- package/dist/src/crypto/js.d.ts.map +1 -1
- package/dist/src/crypto/js.js +18 -17
- package/dist/src/crypto/js.js.map +1 -1
- package/dist/src/crypto.d.ts +11 -10
- package/dist/src/crypto.d.ts.map +1 -1
- package/dist/src/crypto.js +10 -1
- package/dist/src/crypto.js.map +1 -1
- package/dist/src/encoder.d.ts +5 -10
- package/dist/src/encoder.d.ts.map +1 -1
- package/dist/src/encoder.js +10 -51
- package/dist/src/encoder.js.map +1 -1
- package/dist/src/errors.d.ts +6 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +9 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/index.d.ts +50 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +38 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/logger.d.ts +9 -10
- package/dist/src/logger.d.ts.map +1 -1
- package/dist/src/logger.js +39 -28
- package/dist/src/logger.js.map +1 -1
- package/dist/src/metrics.d.ts +1 -1
- package/dist/src/metrics.d.ts.map +1 -1
- package/dist/src/noise.d.ts +33 -30
- package/dist/src/noise.d.ts.map +1 -1
- package/dist/src/noise.js +122 -78
- package/dist/src/noise.js.map +1 -1
- package/dist/src/nonce.d.ts +2 -3
- package/dist/src/nonce.d.ts.map +1 -1
- package/dist/src/nonce.js +3 -2
- package/dist/src/nonce.js.map +1 -1
- package/dist/src/performHandshake.d.ts +5 -0
- package/dist/src/performHandshake.d.ts.map +1 -0
- package/dist/src/performHandshake.js +69 -0
- package/dist/src/performHandshake.js.map +1 -0
- package/dist/src/proto/payload.d.ts +4 -3
- package/dist/src/proto/payload.d.ts.map +1 -1
- package/dist/src/proto/payload.js +48 -29
- package/dist/src/proto/payload.js.map +1 -1
- package/dist/src/protocol.d.ts +71 -0
- package/dist/src/protocol.d.ts.map +1 -0
- package/dist/src/protocol.js +257 -0
- package/dist/src/protocol.js.map +1 -0
- package/dist/src/types.d.ts +98 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils.d.ts +10 -19
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +159 -57
- package/dist/src/utils.js.map +1 -1
- package/package.json +43 -48
- package/src/constants.ts +1 -0
- package/src/crypto/index.browser.ts +3 -0
- package/src/crypto/index.ts +219 -0
- package/src/crypto/js.ts +25 -27
- package/src/crypto.ts +21 -10
- package/src/encoder.ts +10 -67
- package/src/errors.ts +10 -0
- package/src/index.ts +54 -6
- package/src/logger.ts +46 -32
- package/src/metrics.ts +1 -1
- package/src/noise.ts +176 -131
- package/src/nonce.ts +7 -7
- package/src/performHandshake.ts +91 -0
- package/src/proto/payload.proto +2 -1
- package/src/proto/payload.ts +53 -31
- package/src/protocol.ts +313 -0
- package/src/types.ts +105 -0
- package/src/utils.ts +189 -79
- package/dist/src/@types/basic.d.ts +0 -5
- package/dist/src/@types/basic.d.ts.map +0 -1
- package/dist/src/@types/basic.js +0 -2
- package/dist/src/@types/basic.js.map +0 -1
- package/dist/src/@types/handshake-interface.d.ts +0 -15
- package/dist/src/@types/handshake-interface.d.ts.map +0 -1
- package/dist/src/@types/handshake-interface.js +0 -2
- package/dist/src/@types/handshake-interface.js.map +0 -1
- package/dist/src/@types/handshake.d.ts +0 -40
- package/dist/src/@types/handshake.d.ts.map +0 -1
- package/dist/src/@types/handshake.js +0 -2
- package/dist/src/@types/handshake.js.map +0 -1
- package/dist/src/@types/libp2p.d.ts +0 -10
- package/dist/src/@types/libp2p.d.ts.map +0 -1
- package/dist/src/@types/libp2p.js +0 -2
- package/dist/src/@types/libp2p.js.map +0 -1
- package/dist/src/crypto/streaming.d.ts +0 -7
- package/dist/src/crypto/streaming.d.ts.map +0 -1
- package/dist/src/crypto/streaming.js +0 -50
- package/dist/src/crypto/streaming.js.map +0 -1
- package/dist/src/handshake-xx.d.ts +0 -33
- package/dist/src/handshake-xx.d.ts.map +0 -1
- package/dist/src/handshake-xx.js +0 -139
- package/dist/src/handshake-xx.js.map +0 -1
- package/dist/src/handshakes/abstract-handshake.d.ts +0 -36
- package/dist/src/handshakes/abstract-handshake.d.ts.map +0 -1
- package/dist/src/handshakes/abstract-handshake.js +0 -140
- package/dist/src/handshakes/abstract-handshake.js.map +0 -1
- package/dist/src/handshakes/xx.d.ts +0 -18
- package/dist/src/handshakes/xx.d.ts.map +0 -1
- package/dist/src/handshakes/xx.js +0 -161
- package/dist/src/handshakes/xx.js.map +0 -1
- package/src/@types/basic.ts +0 -5
- package/src/@types/handshake-interface.ts +0 -12
- package/src/@types/handshake.ts +0 -48
- package/src/@types/libp2p.ts +0 -10
- package/src/crypto/streaming.ts +0 -58
- package/src/handshake-xx.ts +0 -177
- package/src/handshakes/abstract-handshake.ts +0 -180
- package/src/handshakes/xx.ts +0 -184
package/dist/src/constants.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,0BAA0B,QAAQ,CAAA;AAC/C,eAAO,MAAM,sCAAsC,QAAkC,CAAA;AAErF,eAAO,MAAM,iBAAiB,SAAsD,CAAA"}
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,0BAA0B,QAAQ,CAAA;AAC/C,eAAO,MAAM,sCAAsC,QAAkC,CAAA;AAErF,eAAO,MAAM,iBAAiB,SAAsD,CAAA;AACpF,eAAO,MAAM,iBAAiB,KAAK,CAAA"}
|
package/dist/src/constants.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const NOISE_MSG_MAX_LENGTH_BYTES = 65535;
|
|
2
2
|
export const NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG = NOISE_MSG_MAX_LENGTH_BYTES - 16;
|
|
3
3
|
export const DUMP_SESSION_KEYS = Boolean(globalThis.process?.env?.DUMP_SESSION_KEYS);
|
|
4
|
+
export const CHACHA_TAG_LENGTH = 16;
|
|
4
5
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,0BAA0B,GAAG,KAAK,CAAA;AAC/C,MAAM,CAAC,MAAM,sCAAsC,GAAG,0BAA0B,GAAG,EAAE,CAAA;AAErF,MAAM,CAAC,MAAM,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,iBAAiB,CAAC,CAAA"}
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,0BAA0B,GAAG,KAAK,CAAA;AAC/C,MAAM,CAAC,MAAM,sCAAsC,GAAG,0BAA0B,GAAG,EAAE,CAAA;AAErF,MAAM,CAAC,MAAM,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,EAAE,iBAAiB,CAAC,CAAA;AACpF,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../../../src/crypto/index.browser.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,yCAAe,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../../src/crypto/index.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAEtC,MAAM,CAAC,MAAM,aAAa,GAAG,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/crypto/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAuHpD,eAAO,MAAM,aAAa,EAAE,gBAuF3B,CAAA"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { newInstance, ChaCha20Poly1305 } from '@chainsafe/as-chacha20poly1305';
|
|
3
|
+
import { digest } from '@chainsafe/as-sha256';
|
|
4
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
5
|
+
import { isElectronMain } from 'wherearewe';
|
|
6
|
+
import { pureJsCrypto } from './js.js';
|
|
7
|
+
const ctx = newInstance();
|
|
8
|
+
const asImpl = new ChaCha20Poly1305(ctx);
|
|
9
|
+
const CHACHA_POLY1305 = 'chacha20-poly1305';
|
|
10
|
+
const PKCS8_PREFIX = Buffer.from([0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20]);
|
|
11
|
+
const X25519_PREFIX = Buffer.from([0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x03, 0x21, 0x00]);
|
|
12
|
+
const nodeCrypto = {
|
|
13
|
+
hashSHA256(data) {
|
|
14
|
+
const hash = crypto.createHash('sha256');
|
|
15
|
+
if (data instanceof Uint8Array) {
|
|
16
|
+
return hash.update(data).digest();
|
|
17
|
+
}
|
|
18
|
+
for (const buf of data) {
|
|
19
|
+
hash.update(buf);
|
|
20
|
+
}
|
|
21
|
+
return hash.digest();
|
|
22
|
+
},
|
|
23
|
+
chaCha20Poly1305Encrypt(plaintext, nonce, ad, k) {
|
|
24
|
+
const cipher = crypto.createCipheriv(CHACHA_POLY1305, k, nonce, {
|
|
25
|
+
authTagLength: 16
|
|
26
|
+
});
|
|
27
|
+
cipher.setAAD(ad, { plaintextLength: plaintext.byteLength });
|
|
28
|
+
if (plaintext instanceof Uint8Array) {
|
|
29
|
+
const updated = cipher.update(plaintext);
|
|
30
|
+
const final = cipher.final();
|
|
31
|
+
const tag = cipher.getAuthTag();
|
|
32
|
+
return Buffer.concat([updated, final, tag], updated.byteLength + final.byteLength + tag.byteLength);
|
|
33
|
+
}
|
|
34
|
+
const output = new Uint8ArrayList();
|
|
35
|
+
for (const buf of plaintext) {
|
|
36
|
+
output.append(cipher.update(buf));
|
|
37
|
+
}
|
|
38
|
+
const final = cipher.final();
|
|
39
|
+
if (final.byteLength > 0) {
|
|
40
|
+
output.append(final);
|
|
41
|
+
}
|
|
42
|
+
output.append(cipher.getAuthTag());
|
|
43
|
+
return output;
|
|
44
|
+
},
|
|
45
|
+
chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, _dst) {
|
|
46
|
+
const authTag = ciphertext.subarray(ciphertext.length - 16);
|
|
47
|
+
const decipher = crypto.createDecipheriv(CHACHA_POLY1305, k, nonce, {
|
|
48
|
+
authTagLength: 16
|
|
49
|
+
});
|
|
50
|
+
let text;
|
|
51
|
+
if (ciphertext instanceof Uint8Array) {
|
|
52
|
+
text = ciphertext.subarray(0, ciphertext.length - 16);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
text = ciphertext.sublist(0, ciphertext.length - 16);
|
|
56
|
+
}
|
|
57
|
+
decipher.setAAD(ad, {
|
|
58
|
+
plaintextLength: text.byteLength
|
|
59
|
+
});
|
|
60
|
+
decipher.setAuthTag(authTag);
|
|
61
|
+
if (text instanceof Uint8Array) {
|
|
62
|
+
const output = decipher.update(text);
|
|
63
|
+
const final = decipher.final();
|
|
64
|
+
if (final.byteLength > 0) {
|
|
65
|
+
return Buffer.concat([output, final], output.byteLength + final.byteLength);
|
|
66
|
+
}
|
|
67
|
+
return output;
|
|
68
|
+
}
|
|
69
|
+
const output = new Uint8ArrayList();
|
|
70
|
+
for (const buf of text) {
|
|
71
|
+
output.append(decipher.update(buf));
|
|
72
|
+
}
|
|
73
|
+
const final = decipher.final();
|
|
74
|
+
if (final.byteLength > 0) {
|
|
75
|
+
output.append(final);
|
|
76
|
+
}
|
|
77
|
+
return output;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const asCrypto = {
|
|
81
|
+
hashSHA256(data) {
|
|
82
|
+
return digest(data.subarray());
|
|
83
|
+
},
|
|
84
|
+
chaCha20Poly1305Encrypt(plaintext, nonce, ad, k) {
|
|
85
|
+
return asImpl.seal(k, nonce, plaintext.subarray(), ad);
|
|
86
|
+
},
|
|
87
|
+
chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst) {
|
|
88
|
+
const plaintext = asImpl.open(k, nonce, ciphertext.subarray(), ad, dst);
|
|
89
|
+
if (!plaintext) {
|
|
90
|
+
throw new Error('Invalid chacha20poly1305 decryption');
|
|
91
|
+
}
|
|
92
|
+
return plaintext;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
// benchmarks show that for chacha20poly1305
|
|
96
|
+
// the as implementation is faster for smaller payloads(<1200)
|
|
97
|
+
// and the node implementation is faster for larger payloads
|
|
98
|
+
export const defaultCrypto = {
|
|
99
|
+
...pureJsCrypto,
|
|
100
|
+
hashSHA256(data) {
|
|
101
|
+
return nodeCrypto.hashSHA256(data);
|
|
102
|
+
},
|
|
103
|
+
chaCha20Poly1305Encrypt(plaintext, nonce, ad, k) {
|
|
104
|
+
if (plaintext.byteLength < 1200) {
|
|
105
|
+
return asCrypto.chaCha20Poly1305Encrypt(plaintext, nonce, ad, k);
|
|
106
|
+
}
|
|
107
|
+
return nodeCrypto.chaCha20Poly1305Encrypt(plaintext, nonce, ad, k);
|
|
108
|
+
},
|
|
109
|
+
chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst) {
|
|
110
|
+
if (ciphertext.byteLength < 1200) {
|
|
111
|
+
return asCrypto.chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst);
|
|
112
|
+
}
|
|
113
|
+
return nodeCrypto.chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst);
|
|
114
|
+
},
|
|
115
|
+
generateX25519KeyPair() {
|
|
116
|
+
const { publicKey, privateKey } = crypto.generateKeyPairSync('x25519', {
|
|
117
|
+
publicKeyEncoding: {
|
|
118
|
+
type: 'spki',
|
|
119
|
+
format: 'der'
|
|
120
|
+
},
|
|
121
|
+
privateKeyEncoding: {
|
|
122
|
+
type: 'pkcs8',
|
|
123
|
+
format: 'der'
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
publicKey: publicKey.subarray(X25519_PREFIX.length),
|
|
128
|
+
privateKey: privateKey.subarray(PKCS8_PREFIX.length)
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
generateX25519KeyPairFromSeed(seed) {
|
|
132
|
+
const privateKey = crypto.createPrivateKey({
|
|
133
|
+
key: Buffer.concat([
|
|
134
|
+
PKCS8_PREFIX,
|
|
135
|
+
seed
|
|
136
|
+
], PKCS8_PREFIX.byteLength + seed.byteLength),
|
|
137
|
+
type: 'pkcs8',
|
|
138
|
+
format: 'der'
|
|
139
|
+
});
|
|
140
|
+
const publicKey = crypto.createPublicKey(privateKey)
|
|
141
|
+
.export({
|
|
142
|
+
type: 'spki',
|
|
143
|
+
format: 'der'
|
|
144
|
+
}).subarray(X25519_PREFIX.length);
|
|
145
|
+
return {
|
|
146
|
+
publicKey,
|
|
147
|
+
privateKey: seed
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
generateX25519SharedKey(privateKey, publicKey) {
|
|
151
|
+
if (publicKey instanceof Uint8Array) {
|
|
152
|
+
publicKey = Buffer.concat([
|
|
153
|
+
X25519_PREFIX,
|
|
154
|
+
publicKey
|
|
155
|
+
], X25519_PREFIX.byteLength + publicKey.byteLength);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
publicKey = new Uint8ArrayList(X25519_PREFIX, publicKey).subarray();
|
|
159
|
+
}
|
|
160
|
+
if (privateKey instanceof Uint8Array) {
|
|
161
|
+
privateKey = Buffer.concat([
|
|
162
|
+
PKCS8_PREFIX,
|
|
163
|
+
privateKey
|
|
164
|
+
], PKCS8_PREFIX.byteLength + privateKey.byteLength);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
privateKey = new Uint8ArrayList(PKCS8_PREFIX, privateKey).subarray();
|
|
168
|
+
}
|
|
169
|
+
return crypto.diffieHellman({
|
|
170
|
+
publicKey: crypto.createPublicKey({
|
|
171
|
+
key: Buffer.from(publicKey.buffer, publicKey.byteOffset, publicKey.byteLength),
|
|
172
|
+
type: 'spki',
|
|
173
|
+
format: 'der'
|
|
174
|
+
}),
|
|
175
|
+
privateKey: crypto.createPrivateKey({
|
|
176
|
+
key: Buffer.from(privateKey.buffer, privateKey.byteOffset, privateKey.byteLength),
|
|
177
|
+
type: 'pkcs8',
|
|
178
|
+
format: 'der'
|
|
179
|
+
})
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
// no chacha20-poly1305 in electron https://github.com/electron/electron/issues/24024
|
|
184
|
+
if (isElectronMain) {
|
|
185
|
+
defaultCrypto.chaCha20Poly1305Encrypt = asCrypto.chaCha20Poly1305Encrypt;
|
|
186
|
+
defaultCrypto.chaCha20Poly1305Decrypt = asCrypto.chaCha20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I,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,UAAU,EAAE,SAAS,CAAC,UAAU,CAAC;gBAC9E,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,KAAK;aACd,CAAC;YACF,UAAU,EAAE,MAAM,CAAC,gBAAgB,CAAC;gBAClC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,UAAU,CAAC;gBACjF,IAAI,EAAE,OAAO;gBACb,MAAM,EAAE,KAAK;aACd,CAAC;SACH,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AAED,qFAAqF;AACrF,IAAI,cAAc,EAAE,CAAC;IACnB,aAAa,CAAC,uBAAuB,GAAG,QAAQ,CAAC,uBAAuB,CAAA;IACxE,aAAa,CAAC,uBAAuB,GAAG,QAAQ,CAAC,uBAAuB,CAAA;AAC1E,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js.d.ts","sourceRoot":"","sources":["../../../src/crypto/js.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"js.d.ts","sourceRoot":"","sources":["../../../src/crypto/js.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAIpD,eAAO,MAAM,YAAY,EAAE,gBA+C1B,CAAA"}
|
package/dist/src/crypto/js.js
CHANGED
|
@@ -1,42 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { chacha20poly1305 } from '@noble/ciphers/chacha';
|
|
2
|
+
import { x25519 } from '@noble/curves/ed25519';
|
|
3
|
+
import { extract, expand } from '@noble/hashes/hkdf';
|
|
4
|
+
import { sha256 } from '@noble/hashes/sha2';
|
|
5
5
|
export const pureJsCrypto = {
|
|
6
6
|
hashSHA256(data) {
|
|
7
|
-
return sha256(data);
|
|
7
|
+
return sha256(data.subarray());
|
|
8
8
|
},
|
|
9
9
|
getHKDF(ck, ikm) {
|
|
10
|
-
const
|
|
10
|
+
const prk = extract(sha256, ikm, ck);
|
|
11
|
+
const okmU8Array = expand(sha256, prk, undefined, 96);
|
|
12
|
+
const okm = okmU8Array;
|
|
11
13
|
const k1 = okm.subarray(0, 32);
|
|
12
14
|
const k2 = okm.subarray(32, 64);
|
|
13
15
|
const k3 = okm.subarray(64, 96);
|
|
14
16
|
return [k1, k2, k3];
|
|
15
17
|
},
|
|
16
18
|
generateX25519KeyPair() {
|
|
17
|
-
const
|
|
19
|
+
const secretKey = x25519.utils.randomSecretKey();
|
|
20
|
+
const publicKey = x25519.getPublicKey(secretKey);
|
|
18
21
|
return {
|
|
19
|
-
publicKey
|
|
20
|
-
privateKey:
|
|
22
|
+
publicKey,
|
|
23
|
+
privateKey: secretKey
|
|
21
24
|
};
|
|
22
25
|
},
|
|
23
26
|
generateX25519KeyPairFromSeed(seed) {
|
|
24
|
-
const
|
|
27
|
+
const publicKey = x25519.getPublicKey(seed);
|
|
25
28
|
return {
|
|
26
|
-
publicKey
|
|
27
|
-
privateKey:
|
|
29
|
+
publicKey,
|
|
30
|
+
privateKey: seed
|
|
28
31
|
};
|
|
29
32
|
},
|
|
30
33
|
generateX25519SharedKey(privateKey, publicKey) {
|
|
31
|
-
return x25519.
|
|
34
|
+
return x25519.getSharedSecret(privateKey.subarray(), publicKey.subarray());
|
|
32
35
|
},
|
|
33
36
|
chaCha20Poly1305Encrypt(plaintext, nonce, ad, k) {
|
|
34
|
-
|
|
35
|
-
return ctx.seal(nonce, plaintext, ad);
|
|
37
|
+
return chacha20poly1305(k, nonce, ad).encrypt(plaintext.subarray());
|
|
36
38
|
},
|
|
37
39
|
chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst) {
|
|
38
|
-
|
|
39
|
-
return ctx.open(nonce, ciphertext, ad, dst);
|
|
40
|
+
return chacha20poly1305(k, nonce, ad).decrypt(ciphertext.subarray(), dst);
|
|
40
41
|
}
|
|
41
42
|
};
|
|
42
43
|
//# sourceMappingURL=js.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js.js","sourceRoot":"","sources":["../../../src/crypto/js.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"js.js","sourceRoot":"","sources":["../../../src/crypto/js.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAK3C,MAAM,CAAC,MAAM,YAAY,GAAqB;IAC5C,UAAU,CAAE,IAAiC;QAC3C,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;IAChC,CAAC;IAED,OAAO,CAAE,EAAc,EAAE,GAAe;QACtC,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;QACpC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE,CAAC,CAAA;QACrD,MAAM,GAAG,GAAG,UAAU,CAAA;QAEtB,MAAM,EAAE,GAAG,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;QAC9B,MAAM,EAAE,GAAG,GAAG,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAA;QAC/B,MAAM,EAAE,GAAG,GAAG,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAA;QAE/B,OAAO,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAA;IACrB,CAAC;IAED,qBAAqB;QACnB,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,CAAA;QAChD,MAAM,SAAS,GAAG,MAAM,CAAC,YAAY,CAAC,SAAS,CAAC,CAAA;QAEhD,OAAO;YACL,SAAS;YACT,UAAU,EAAE,SAAS;SACtB,CAAA;IACH,CAAC;IAED,6BAA6B,CAAE,IAAgB;QAC7C,MAAM,SAAS,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;QAE3C,OAAO;YACL,SAAS;YACT,UAAU,EAAE,IAAI;SACjB,CAAA;IACH,CAAC;IAED,uBAAuB,CAAE,UAAuC,EAAE,SAAsC;QACtG,OAAO,MAAM,CAAC,eAAe,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAE,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC5E,CAAC;IAED,uBAAuB,CAAE,SAAsC,EAAE,KAAiB,EAAE,EAAc,EAAE,CAAa;QAC/G,OAAO,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAA;IACrE,CAAC;IAED,uBAAuB,CAAE,UAAuC,EAAE,KAAiB,EAAE,EAAc,EAAE,CAAa,EAAE,GAAgB;QAClI,OAAO,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAE,GAAG,CAAC,CAAA;IAC3E,CAAC;CACF,CAAA"}
|
package/dist/src/crypto.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
1
|
+
import type { ICrypto, KeyPair } from './types.js';
|
|
2
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
3
|
+
/** Underlying crypto implementation, meant to be overridable */
|
|
4
4
|
export interface ICryptoInterface {
|
|
5
|
-
hashSHA256
|
|
6
|
-
getHKDF
|
|
7
|
-
generateX25519KeyPair
|
|
8
|
-
generateX25519KeyPairFromSeed
|
|
9
|
-
generateX25519SharedKey
|
|
10
|
-
chaCha20Poly1305Encrypt
|
|
11
|
-
chaCha20Poly1305Decrypt
|
|
5
|
+
hashSHA256(data: Uint8Array | Uint8ArrayList): Uint8Array;
|
|
6
|
+
getHKDF(ck: Uint8Array, ikm: Uint8Array): [Uint8Array, Uint8Array, Uint8Array];
|
|
7
|
+
generateX25519KeyPair(): KeyPair;
|
|
8
|
+
generateX25519KeyPairFromSeed(seed: Uint8Array): KeyPair;
|
|
9
|
+
generateX25519SharedKey(privateKey: Uint8Array | Uint8ArrayList, publicKey: Uint8Array | Uint8ArrayList): Uint8Array;
|
|
10
|
+
chaCha20Poly1305Encrypt(plaintext: Uint8Array | Uint8ArrayList, nonce: Uint8Array, ad: Uint8Array, k: Uint8Array): Uint8ArrayList | Uint8Array;
|
|
11
|
+
chaCha20Poly1305Decrypt(ciphertext: Uint8Array | Uint8ArrayList, nonce: Uint8Array, ad: Uint8Array, k: Uint8Array, dst?: Uint8Array): Uint8ArrayList | Uint8Array;
|
|
12
12
|
}
|
|
13
|
+
export declare function wrapCrypto(crypto: ICryptoInterface): ICrypto;
|
|
13
14
|
//# sourceMappingURL=crypto.d.ts.map
|
package/dist/src/crypto.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,gEAAgE;AAChE,MAAM,WAAW,gBAAgB;IAC/B,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,cAAc,GAAG,UAAU,CAAA;IAEzD,OAAO,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,CAAA;IAE9E,qBAAqB,IAAI,OAAO,CAAA;IAChC,6BAA6B,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAA;IACxD,uBAAuB,CAAC,UAAU,EAAE,UAAU,GAAG,cAAc,EAAE,SAAS,EAAE,UAAU,GAAG,cAAc,GAAG,UAAU,CAAA;IAEpH,uBAAuB,CAAC,SAAS,EAAE,UAAU,GAAG,cAAc,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,cAAc,GAAG,UAAU,CAAA;IAC9I,uBAAuB,CAAC,UAAU,EAAE,UAAU,GAAG,cAAc,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,GAAG,CAAC,EAAE,UAAU,GAAG,cAAc,GAAG,UAAU,CAAA;CAClK;AAED,wBAAgB,UAAU,CAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAS7D"}
|
package/dist/src/crypto.js
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export function wrapCrypto(crypto) {
|
|
2
|
+
return {
|
|
3
|
+
generateKeypair: crypto.generateX25519KeyPair,
|
|
4
|
+
dh: (keypair, publicKey) => crypto.generateX25519SharedKey(keypair.privateKey, publicKey).subarray(0, 32),
|
|
5
|
+
encrypt: crypto.chaCha20Poly1305Encrypt,
|
|
6
|
+
decrypt: crypto.chaCha20Poly1305Decrypt,
|
|
7
|
+
hash: crypto.hashSHA256,
|
|
8
|
+
hkdf: crypto.getHKDF
|
|
9
|
+
};
|
|
10
|
+
}
|
|
2
11
|
//# sourceMappingURL=crypto.js.map
|
package/dist/src/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAiBA,MAAM,UAAU,UAAU,CAAE,MAAwB;IAClD,OAAO;QACL,eAAe,EAAE,MAAM,CAAC,qBAAqB;QAC7C,EAAE,EAAE,CAAC,OAAO,EAAE,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,uBAAuB,CAAC,OAAO,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC;QACzG,OAAO,EAAE,MAAM,CAAC,uBAAuB;QACvC,OAAO,EAAE,MAAM,CAAC,uBAAuB;QACvC,IAAI,EAAE,MAAM,CAAC,UAAU;QACvB,IAAI,EAAE,MAAM,CAAC,OAAO;KACrB,CAAA;AACH,CAAC"}
|
package/dist/src/encoder.d.ts
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { MessageBuffer } from './@types/handshake.js';
|
|
3
|
-
import type { LengthDecoderFunction } from 'it-length-prefixed';
|
|
1
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
4
2
|
export declare const uint16BEEncode: {
|
|
5
3
|
(value: number): Uint8Array;
|
|
6
4
|
bytes: number;
|
|
7
5
|
};
|
|
8
|
-
export declare const uint16BEDecode:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export declare function decode0(input: bytes): MessageBuffer;
|
|
13
|
-
export declare function decode1(input: bytes): MessageBuffer;
|
|
14
|
-
export declare function decode2(input: bytes): MessageBuffer;
|
|
6
|
+
export declare const uint16BEDecode: {
|
|
7
|
+
(data: Uint8Array | Uint8ArrayList): number;
|
|
8
|
+
bytes: number;
|
|
9
|
+
};
|
|
15
10
|
//# sourceMappingURL=encoder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/encoder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/encoder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,eAAO,MAAM,cAAc;YAAW,MAAM,GAAG,UAAU;;CAKxD,CAAA;AAGD,eAAO,MAAM,cAAc;WAAU,UAAU,GAAG,cAAc,GAAG,MAAM;;CAWxE,CAAA"}
|
package/dist/src/encoder.js
CHANGED
|
@@ -1,63 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const allocUnsafe = (len) => {
|
|
3
|
-
if (globalThis.Buffer) {
|
|
4
|
-
return globalThis.Buffer.allocUnsafe(len);
|
|
5
|
-
}
|
|
6
|
-
return new Uint8Array(len);
|
|
7
|
-
};
|
|
1
|
+
import { allocUnsafe as uint8ArrayAllocUnsafe } from 'uint8arrays/alloc';
|
|
8
2
|
export const uint16BEEncode = (value) => {
|
|
9
|
-
const target =
|
|
10
|
-
|
|
3
|
+
const target = uint8ArrayAllocUnsafe(2);
|
|
4
|
+
target[0] = value >> 8;
|
|
5
|
+
target[1] = value;
|
|
11
6
|
return target;
|
|
12
7
|
};
|
|
13
8
|
uint16BEEncode.bytes = 2;
|
|
14
9
|
export const uint16BEDecode = (data) => {
|
|
15
|
-
if (data.length < 2)
|
|
10
|
+
if (data.length < 2) {
|
|
16
11
|
throw RangeError('Could not decode int16BE');
|
|
12
|
+
}
|
|
17
13
|
if (data instanceof Uint8Array) {
|
|
18
|
-
|
|
14
|
+
let value = 0;
|
|
15
|
+
value += data[0] << 8;
|
|
16
|
+
value += data[1];
|
|
17
|
+
return value;
|
|
19
18
|
}
|
|
20
19
|
return data.getUint16(0);
|
|
21
20
|
};
|
|
22
21
|
uint16BEDecode.bytes = 2;
|
|
23
|
-
// Note: IK and XX encoder usage is opposite (XX uses in stages encode0 where IK uses encode1)
|
|
24
|
-
export function encode0(message) {
|
|
25
|
-
return uint8ArrayConcat([message.ne, message.ciphertext], message.ne.length + message.ciphertext.length);
|
|
26
|
-
}
|
|
27
|
-
export function encode1(message) {
|
|
28
|
-
return uint8ArrayConcat([message.ne, message.ns, message.ciphertext], message.ne.length + message.ns.length + message.ciphertext.length);
|
|
29
|
-
}
|
|
30
|
-
export function encode2(message) {
|
|
31
|
-
return uint8ArrayConcat([message.ns, message.ciphertext], message.ns.length + message.ciphertext.length);
|
|
32
|
-
}
|
|
33
|
-
export function decode0(input) {
|
|
34
|
-
if (input.length < 32) {
|
|
35
|
-
throw new Error('Cannot decode stage 0 MessageBuffer: length less than 32 bytes.');
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
ne: input.subarray(0, 32),
|
|
39
|
-
ciphertext: input.subarray(32, input.length),
|
|
40
|
-
ns: new Uint8Array(0)
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
export function decode1(input) {
|
|
44
|
-
if (input.length < 80) {
|
|
45
|
-
throw new Error('Cannot decode stage 1 MessageBuffer: length less than 80 bytes.');
|
|
46
|
-
}
|
|
47
|
-
return {
|
|
48
|
-
ne: input.subarray(0, 32),
|
|
49
|
-
ns: input.subarray(32, 80),
|
|
50
|
-
ciphertext: input.subarray(80, input.length)
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
export function decode2(input) {
|
|
54
|
-
if (input.length < 48) {
|
|
55
|
-
throw new Error('Cannot decode stage 2 MessageBuffer: length less than 48 bytes.');
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
ne: new Uint8Array(0),
|
|
59
|
-
ns: input.subarray(0, 48),
|
|
60
|
-
ciphertext: input.subarray(48, input.length)
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
22
|
//# sourceMappingURL=encoder.js.map
|
package/dist/src/encoder.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.js","sourceRoot":"","sources":["../../src/encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"encoder.js","sourceRoot":"","sources":["../../src/encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAGxE,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAc,EAAE;IAC1D,MAAM,MAAM,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAA;IACvC,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,CAAA;IACtB,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAA;IACjB,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AACD,cAAc,CAAC,KAAK,GAAG,CAAC,CAAA;AAExB,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,IAAiC,EAAU,EAAE;IAC1E,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAAC,MAAM,UAAU,CAAC,0BAA0B,CAAC,CAAA;IAAC,CAAC;IAErE,IAAI,IAAI,YAAY,UAAU,EAAE,CAAC;QAC/B,IAAI,KAAK,GAAG,CAAC,CAAA;QACb,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;QACrB,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,CAAA;QAChB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;AAC1B,CAAC,CAAA;AACD,cAAc,CAAC,KAAK,GAAG,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,0BAA2B,SAAQ,KAAK;IAC5C,IAAI,EAAE,MAAM,CAAA;gBAEN,OAAO,SAA4B;IAKhD,MAAM,CAAC,QAAQ,CAAC,IAAI,iCAAgC;CACrD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export class InvalidCryptoExchangeError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(message = 'Invalid crypto exchange') {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = InvalidCryptoExchangeError.code;
|
|
6
|
+
}
|
|
7
|
+
static code = 'ERR_INVALID_CRYPTO_EXCHANGE';
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,0BAA2B,SAAQ,KAAK;IAC5C,IAAI,CAAQ;IAEnB,YAAa,OAAO,GAAG,yBAAyB;QAC9C,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC,IAAI,CAAA;IAC7C,CAAC;IAED,MAAM,CAAU,IAAI,GAAG,6BAA6B,CAAA"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* This repository contains TypeScript implementation of noise protocol, an encryption protocol used in libp2p.
|
|
5
|
+
*
|
|
6
|
+
* ## Usage
|
|
7
|
+
*
|
|
8
|
+
* Install with `yarn add @libp2p/noise` or `npm i @libp2p/noise`.
|
|
9
|
+
*
|
|
10
|
+
* Example of using default noise configuration and passing it to the libp2p config:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import {createLibp2p} from "libp2p"
|
|
14
|
+
* import {noise} from "@libp2p/noise"
|
|
15
|
+
*
|
|
16
|
+
* //custom noise configuration, pass it instead of `noise()`
|
|
17
|
+
* //x25519 private key
|
|
18
|
+
* const n = noise({ staticNoiseKey });
|
|
19
|
+
*
|
|
20
|
+
* const libp2p = await createLibp2p({
|
|
21
|
+
* connectionEncrypters: [noise()],
|
|
22
|
+
* //... other options
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* See the [NoiseInit](https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/noise.ts#L22-L30) interface for noise configuration options.
|
|
27
|
+
*
|
|
28
|
+
* ## API
|
|
29
|
+
*
|
|
30
|
+
* This module exposes an implementation of the [ConnectionEncrypter](https://libp2p.github.io/js-libp2p/interfaces/_libp2p_interface.ConnectionEncrypter.html) interface.
|
|
31
|
+
*
|
|
32
|
+
* ## Bring your own crypto
|
|
33
|
+
*
|
|
34
|
+
* 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
|
+
*
|
|
36
|
+
* The implementation must conform to the `ICryptoInterface`, defined in <https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/crypto.ts>
|
|
37
|
+
*/
|
|
38
|
+
import type { NoiseInit, NoiseExtensions } from './noise.js';
|
|
39
|
+
import type { KeyPair } from './types.js';
|
|
40
|
+
import type { ComponentLogger, ConnectionEncrypter, Metrics, PeerId, PrivateKey, Upgrader } from '@libp2p/interface';
|
|
5
41
|
export { pureJsCrypto } from './crypto/js.js';
|
|
6
|
-
export
|
|
42
|
+
export type { ICryptoInterface } from './crypto.js';
|
|
43
|
+
export type { NoiseInit, NoiseExtensions, KeyPair };
|
|
44
|
+
export interface NoiseComponents {
|
|
45
|
+
peerId: PeerId;
|
|
46
|
+
privateKey: PrivateKey;
|
|
47
|
+
logger: ComponentLogger;
|
|
48
|
+
upgrader: Upgrader;
|
|
49
|
+
metrics?: Metrics;
|
|
50
|
+
}
|
|
51
|
+
export declare function noise(init?: NoiseInit): (components: NoiseComponents) => ConnectionEncrypter<NoiseExtensions>;
|
|
7
52
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAEpH,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AACnD,YAAY,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,CAAA;AAEnD,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,eAAe,CAAA;IACvB,QAAQ,EAAE,QAAQ,CAAA;IAClB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,wBAAgB,KAAK,CAAE,IAAI,GAAE,SAAc,GAAG,CAAC,UAAU,EAAE,eAAe,KAAK,mBAAmB,CAAC,eAAe,CAAC,CAElH"}
|