@libp2p/plaintext 1.1.6 → 2.0.0-18dd3cb26
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 +1 -1
- package/dist/index.min.js +4 -5
- package/dist/src/index.d.ts +4 -10
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +33 -56
- package/dist/src/index.js.map +1 -1
- package/dist/src/pb/proto.d.ts +4 -4
- package/dist/src/pb/proto.d.ts.map +1 -1
- package/dist/src/pb/proto.js +26 -17
- package/dist/src/pb/proto.js.map +1 -1
- package/package.json +10 -9
- package/src/index.ts +35 -64
- package/src/pb/proto.proto +1 -1
- package/src/pb/proto.ts +26 -18
- package/dist/typedoc-urls.json +0 -8
package/dist/src/index.d.ts
CHANGED
|
@@ -13,22 +13,16 @@
|
|
|
13
13
|
*
|
|
14
14
|
* const node = await createLibp2p({
|
|
15
15
|
* // ...other options
|
|
16
|
-
*
|
|
16
|
+
* connectionEncrypters: [
|
|
17
17
|
* plaintext()
|
|
18
18
|
* ]
|
|
19
19
|
* })
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
|
-
import type { ComponentLogger, ConnectionEncrypter } from '@libp2p/interface';
|
|
22
|
+
import type { ComponentLogger, ConnectionEncrypter, PrivateKey } from '@libp2p/interface';
|
|
23
23
|
export interface PlaintextComponents {
|
|
24
|
+
privateKey: PrivateKey;
|
|
24
25
|
logger: ComponentLogger;
|
|
25
26
|
}
|
|
26
|
-
export
|
|
27
|
-
/**
|
|
28
|
-
* The peer id exchange must complete within this many milliseconds
|
|
29
|
-
* (default: 1000)
|
|
30
|
-
*/
|
|
31
|
-
timeout?: number;
|
|
32
|
-
}
|
|
33
|
-
export declare function plaintext(init?: PlaintextInit): (components: PlaintextComponents) => ConnectionEncrypter;
|
|
27
|
+
export declare function plaintext(): (components: PlaintextComponents) => ConnectionEncrypter;
|
|
34
28
|
//# 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":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAQH,OAAO,KAAK,EAAE,eAAe,EAA+B,mBAAmB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAQH,OAAO,KAAK,EAAE,eAAe,EAA+B,mBAAmB,EAAqB,UAAU,EAA2B,MAAM,mBAAmB,CAAA;AAMlK,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,eAAe,CAAA;CACxB;AAyFD,wBAAgB,SAAS,IAAK,CAAC,UAAU,EAAE,mBAAmB,KAAK,mBAAmB,CAErF"}
|
package/dist/src/index.js
CHANGED
|
@@ -13,101 +13,78 @@
|
|
|
13
13
|
*
|
|
14
14
|
* const node = await createLibp2p({
|
|
15
15
|
* // ...other options
|
|
16
|
-
*
|
|
16
|
+
* connectionEncrypters: [
|
|
17
17
|
* plaintext()
|
|
18
18
|
* ]
|
|
19
19
|
* })
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
|
-
import {
|
|
23
|
-
import { UnexpectedPeerError, InvalidCryptoExchangeError, serviceCapabilities } from '@libp2p/interface';
|
|
24
|
-
import {
|
|
25
|
-
import { createFromPubKey } from '@libp2p/peer-id-factory';
|
|
22
|
+
import { publicKeyFromRaw } from '@libp2p/crypto/keys';
|
|
23
|
+
import { UnexpectedPeerError, InvalidCryptoExchangeError, serviceCapabilities, ProtocolError } from '@libp2p/interface';
|
|
24
|
+
import { peerIdFromPublicKey } from '@libp2p/peer-id';
|
|
26
25
|
import { pbStream } from 'it-protobuf-stream';
|
|
27
|
-
import {
|
|
26
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
|
|
27
|
+
import { Exchange, KeyType } from './pb/proto.js';
|
|
28
28
|
const PROTOCOL = '/plaintext/2.0.0';
|
|
29
29
|
class Plaintext {
|
|
30
30
|
protocol = PROTOCOL;
|
|
31
|
+
privateKey;
|
|
31
32
|
log;
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
constructor(components) {
|
|
34
|
+
this.privateKey = components.privateKey;
|
|
34
35
|
this.log = components.logger.forComponent('libp2p:plaintext');
|
|
35
|
-
this.timeout = init.timeout ?? 1000;
|
|
36
36
|
}
|
|
37
37
|
[Symbol.toStringTag] = '@libp2p/plaintext';
|
|
38
38
|
[serviceCapabilities] = [
|
|
39
39
|
'@libp2p/connection-encryption'
|
|
40
40
|
];
|
|
41
|
-
async secureInbound(
|
|
42
|
-
return this._encrypt(
|
|
41
|
+
async secureInbound(conn, options) {
|
|
42
|
+
return this._encrypt(conn, options);
|
|
43
43
|
}
|
|
44
|
-
async secureOutbound(
|
|
45
|
-
return this._encrypt(
|
|
44
|
+
async secureOutbound(conn, options) {
|
|
45
|
+
return this._encrypt(conn, options);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Encrypt connection
|
|
49
49
|
*/
|
|
50
|
-
async _encrypt(
|
|
51
|
-
const signal = AbortSignal.timeout(this.timeout);
|
|
50
|
+
async _encrypt(conn, options) {
|
|
52
51
|
const pb = pbStream(conn).pb(Exchange);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
type = KeyType.Ed25519;
|
|
56
|
-
}
|
|
57
|
-
else if (localId.type === 'secp256k1') {
|
|
58
|
-
type = KeyType.Secp256k1;
|
|
59
|
-
}
|
|
60
|
-
this.log('write pubkey exchange to peer %p', remoteId);
|
|
52
|
+
this.log('write pubkey exchange to peer %p', options?.remotePeer);
|
|
53
|
+
const publicKey = this.privateKey.publicKey;
|
|
61
54
|
const [, response] = await Promise.all([
|
|
62
55
|
// Encode the public key and write it to the remote peer
|
|
63
56
|
pb.write({
|
|
64
|
-
id:
|
|
57
|
+
id: publicKey.toMultihash().bytes,
|
|
65
58
|
pubkey: {
|
|
66
|
-
Type: type,
|
|
67
|
-
Data:
|
|
59
|
+
Type: KeyType[publicKey.type],
|
|
60
|
+
Data: publicKey.raw
|
|
68
61
|
}
|
|
69
|
-
},
|
|
70
|
-
signal
|
|
71
|
-
}),
|
|
62
|
+
}, options),
|
|
72
63
|
// Get the Exchange message
|
|
73
|
-
pb.read(
|
|
74
|
-
signal
|
|
75
|
-
})
|
|
64
|
+
pb.read(options)
|
|
76
65
|
]);
|
|
77
66
|
let peerId;
|
|
78
67
|
try {
|
|
79
68
|
if (response.pubkey == null) {
|
|
80
|
-
throw new
|
|
69
|
+
throw new ProtocolError('Public key missing');
|
|
81
70
|
}
|
|
82
|
-
if (response.pubkey.Data.
|
|
83
|
-
throw new
|
|
71
|
+
if (response.pubkey.Data.byteLength === 0) {
|
|
72
|
+
throw new ProtocolError('Public key data too short');
|
|
84
73
|
}
|
|
85
74
|
if (response.id == null) {
|
|
86
|
-
throw new
|
|
87
|
-
}
|
|
88
|
-
let pubKey;
|
|
89
|
-
if (response.pubkey.Type === KeyType.RSA) {
|
|
90
|
-
pubKey = supportedKeys.rsa.unmarshalRsaPublicKey(response.pubkey.Data);
|
|
91
|
-
}
|
|
92
|
-
else if (response.pubkey.Type === KeyType.Ed25519) {
|
|
93
|
-
pubKey = supportedKeys.ed25519.unmarshalEd25519PublicKey(response.pubkey.Data);
|
|
94
|
-
}
|
|
95
|
-
else if (response.pubkey.Type === KeyType.Secp256k1) {
|
|
96
|
-
pubKey = supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(response.pubkey.Data);
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
throw new Error('Unknown public key type');
|
|
75
|
+
throw new ProtocolError('Remote id missing');
|
|
100
76
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
77
|
+
const pubKey = publicKeyFromRaw(response.pubkey.Data);
|
|
78
|
+
peerId = peerIdFromPublicKey(pubKey);
|
|
79
|
+
if (!uint8ArrayEquals(peerId.toMultihash().bytes, response.id)) {
|
|
80
|
+
throw new InvalidCryptoExchangeError('Public key did not match id');
|
|
104
81
|
}
|
|
105
82
|
}
|
|
106
83
|
catch (err) {
|
|
107
84
|
this.log.error(err);
|
|
108
|
-
throw new InvalidCryptoExchangeError('
|
|
85
|
+
throw new InvalidCryptoExchangeError('Invalid public key - ' + err.message);
|
|
109
86
|
}
|
|
110
|
-
if (
|
|
87
|
+
if (options?.remotePeer != null && !peerId.equals(options?.remotePeer)) {
|
|
111
88
|
throw new UnexpectedPeerError();
|
|
112
89
|
}
|
|
113
90
|
this.log('plaintext key exchange completed successfully with peer %p', peerId);
|
|
@@ -117,7 +94,7 @@ class Plaintext {
|
|
|
117
94
|
};
|
|
118
95
|
}
|
|
119
96
|
}
|
|
120
|
-
export function plaintext(
|
|
121
|
-
return (components) => new Plaintext(components
|
|
97
|
+
export function plaintext() {
|
|
98
|
+
return (components) => new Plaintext(components);
|
|
122
99
|
}
|
|
123
100
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACvH,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;A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YACvE,MAAM,IAAI,mBAAmB,EAAE,CAAA;QACjC,CAAC;QAED,IAAI,CAAC,GAAG,CAAC,4DAA4D,EAAE,MAAM,CAAC,CAAA;QAE9E,OAAO;YACL,IAAI,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE;YAC1B,UAAU,EAAE,MAAM;SACnB,CAAA;IACH,CAAC;CACF;AAED,MAAM,UAAU,SAAS;IACvB,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,CAAA;AAClD,CAAC"}
|
package/dist/src/pb/proto.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Codec, type DecodeOptions } from 'protons-runtime';
|
|
2
2
|
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
3
3
|
export interface Exchange {
|
|
4
4
|
id?: Uint8Array;
|
|
@@ -7,12 +7,12 @@ export interface Exchange {
|
|
|
7
7
|
export declare namespace Exchange {
|
|
8
8
|
const codec: () => Codec<Exchange>;
|
|
9
9
|
const encode: (obj: Partial<Exchange>) => Uint8Array;
|
|
10
|
-
const decode: (buf: Uint8Array | Uint8ArrayList) => Exchange;
|
|
10
|
+
const decode: (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<Exchange>) => Exchange;
|
|
11
11
|
}
|
|
12
12
|
export declare enum KeyType {
|
|
13
13
|
RSA = "RSA",
|
|
14
14
|
Ed25519 = "Ed25519",
|
|
15
|
-
|
|
15
|
+
secp256k1 = "secp256k1",
|
|
16
16
|
ECDSA = "ECDSA"
|
|
17
17
|
}
|
|
18
18
|
export declare namespace KeyType {
|
|
@@ -25,6 +25,6 @@ export interface PublicKey {
|
|
|
25
25
|
export declare namespace PublicKey {
|
|
26
26
|
const codec: () => Codec<PublicKey>;
|
|
27
27
|
const encode: (obj: Partial<PublicKey>) => Uint8Array;
|
|
28
|
-
const decode: (buf: Uint8Array | Uint8ArrayList) => PublicKey;
|
|
28
|
+
const decode: (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<PublicKey>) => PublicKey;
|
|
29
29
|
}
|
|
30
30
|
//# sourceMappingURL=proto.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proto.d.ts","sourceRoot":"","sources":["../../../src/pb/proto.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"proto.d.ts","sourceRoot":"","sources":["../../../src/pb/proto.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,KAAK,EAAiB,KAAK,aAAa,EAAuC,MAAM,iBAAiB,CAAA;AAEpH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,MAAM,WAAW,QAAQ;IACvB,EAAE,CAAC,EAAE,UAAU,CAAA;IACf,MAAM,CAAC,EAAE,SAAS,CAAA;CACnB;AAED,yBAAiB,QAAQ,CAAC;IAGjB,MAAM,KAAK,QAAO,MAAM,QAAQ,CAmDtC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,QAAQ,CAAC,KAAG,UAE/C,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,SAAS,cAAc,QAAQ,CAAC,KAAG,QAEzF,CAAA;CACF;AAED,oBAAY,OAAO;IACjB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,KAAK,UAAU;CAChB;AASD,yBAAiB,OAAO,CAAC;IAChB,MAAM,KAAK,QAAO,MAAM,OAAO,CAErC,CAAA;CACF;AACD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,OAAO,CAAA;IACb,IAAI,EAAE,UAAU,CAAA;CACjB;AAED,yBAAiB,SAAS,CAAC;IAGlB,MAAM,KAAK,QAAO,MAAM,SAAS,CAoDvC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,SAAS,CAAC,KAAG,UAEhD,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,SAAS,cAAc,SAAS,CAAC,KAAG,SAE1F,CAAA;CACF"}
|
package/dist/src/pb/proto.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
5
|
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
6
|
-
import { encodeMessage,
|
|
6
|
+
import { decodeMessage, encodeMessage, enumeration, message } from 'protons-runtime';
|
|
7
|
+
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc';
|
|
7
8
|
export var Exchange;
|
|
8
9
|
(function (Exchange) {
|
|
9
10
|
let _codec;
|
|
@@ -24,21 +25,26 @@ export var Exchange;
|
|
|
24
25
|
if (opts.lengthDelimited !== false) {
|
|
25
26
|
w.ldelim();
|
|
26
27
|
}
|
|
27
|
-
}, (reader, length) => {
|
|
28
|
+
}, (reader, length, opts = {}) => {
|
|
28
29
|
const obj = {};
|
|
29
30
|
const end = length == null ? reader.len : reader.pos + length;
|
|
30
31
|
while (reader.pos < end) {
|
|
31
32
|
const tag = reader.uint32();
|
|
32
33
|
switch (tag >>> 3) {
|
|
33
|
-
case 1:
|
|
34
|
+
case 1: {
|
|
34
35
|
obj.id = reader.bytes();
|
|
35
36
|
break;
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
}
|
|
38
|
+
case 2: {
|
|
39
|
+
obj.pubkey = PublicKey.codec().decode(reader, reader.uint32(), {
|
|
40
|
+
limits: opts.limits?.pubkey
|
|
41
|
+
});
|
|
38
42
|
break;
|
|
39
|
-
|
|
43
|
+
}
|
|
44
|
+
default: {
|
|
40
45
|
reader.skipType(tag & 7);
|
|
41
46
|
break;
|
|
47
|
+
}
|
|
42
48
|
}
|
|
43
49
|
}
|
|
44
50
|
return obj;
|
|
@@ -49,22 +55,22 @@ export var Exchange;
|
|
|
49
55
|
Exchange.encode = (obj) => {
|
|
50
56
|
return encodeMessage(obj, Exchange.codec());
|
|
51
57
|
};
|
|
52
|
-
Exchange.decode = (buf) => {
|
|
53
|
-
return decodeMessage(buf, Exchange.codec());
|
|
58
|
+
Exchange.decode = (buf, opts) => {
|
|
59
|
+
return decodeMessage(buf, Exchange.codec(), opts);
|
|
54
60
|
};
|
|
55
61
|
})(Exchange || (Exchange = {}));
|
|
56
62
|
export var KeyType;
|
|
57
63
|
(function (KeyType) {
|
|
58
64
|
KeyType["RSA"] = "RSA";
|
|
59
65
|
KeyType["Ed25519"] = "Ed25519";
|
|
60
|
-
KeyType["
|
|
66
|
+
KeyType["secp256k1"] = "secp256k1";
|
|
61
67
|
KeyType["ECDSA"] = "ECDSA";
|
|
62
68
|
})(KeyType || (KeyType = {}));
|
|
63
69
|
var __KeyTypeValues;
|
|
64
70
|
(function (__KeyTypeValues) {
|
|
65
71
|
__KeyTypeValues[__KeyTypeValues["RSA"] = 0] = "RSA";
|
|
66
72
|
__KeyTypeValues[__KeyTypeValues["Ed25519"] = 1] = "Ed25519";
|
|
67
|
-
__KeyTypeValues[__KeyTypeValues["
|
|
73
|
+
__KeyTypeValues[__KeyTypeValues["secp256k1"] = 2] = "secp256k1";
|
|
68
74
|
__KeyTypeValues[__KeyTypeValues["ECDSA"] = 3] = "ECDSA";
|
|
69
75
|
})(__KeyTypeValues || (__KeyTypeValues = {}));
|
|
70
76
|
(function (KeyType) {
|
|
@@ -92,24 +98,27 @@ export var PublicKey;
|
|
|
92
98
|
if (opts.lengthDelimited !== false) {
|
|
93
99
|
w.ldelim();
|
|
94
100
|
}
|
|
95
|
-
}, (reader, length) => {
|
|
101
|
+
}, (reader, length, opts = {}) => {
|
|
96
102
|
const obj = {
|
|
97
103
|
Type: KeyType.RSA,
|
|
98
|
-
Data:
|
|
104
|
+
Data: uint8ArrayAlloc(0)
|
|
99
105
|
};
|
|
100
106
|
const end = length == null ? reader.len : reader.pos + length;
|
|
101
107
|
while (reader.pos < end) {
|
|
102
108
|
const tag = reader.uint32();
|
|
103
109
|
switch (tag >>> 3) {
|
|
104
|
-
case 1:
|
|
110
|
+
case 1: {
|
|
105
111
|
obj.Type = KeyType.codec().decode(reader);
|
|
106
112
|
break;
|
|
107
|
-
|
|
113
|
+
}
|
|
114
|
+
case 2: {
|
|
108
115
|
obj.Data = reader.bytes();
|
|
109
116
|
break;
|
|
110
|
-
|
|
117
|
+
}
|
|
118
|
+
default: {
|
|
111
119
|
reader.skipType(tag & 7);
|
|
112
120
|
break;
|
|
121
|
+
}
|
|
113
122
|
}
|
|
114
123
|
}
|
|
115
124
|
return obj;
|
|
@@ -120,8 +129,8 @@ export var PublicKey;
|
|
|
120
129
|
PublicKey.encode = (obj) => {
|
|
121
130
|
return encodeMessage(obj, PublicKey.codec());
|
|
122
131
|
};
|
|
123
|
-
PublicKey.decode = (buf) => {
|
|
124
|
-
return decodeMessage(buf, PublicKey.codec());
|
|
132
|
+
PublicKey.decode = (buf, opts) => {
|
|
133
|
+
return decodeMessage(buf, PublicKey.codec(), opts);
|
|
125
134
|
};
|
|
126
135
|
})(PublicKey || (PublicKey = {}));
|
|
127
136
|
//# sourceMappingURL=proto.js.map
|
package/dist/src/pb/proto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proto.js","sourceRoot":"","sources":["../../../src/pb/proto.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,+BAA+B;AAC/B,oDAAoD;AACpD,8EAA8E;AAC9E,0DAA0D;AAE1D,OAAO,
|
|
1
|
+
{"version":3,"file":"proto.js","sourceRoot":"","sources":["../../../src/pb/proto.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,+BAA+B;AAC/B,oDAAoD;AACpD,8EAA8E;AAC9E,0DAA0D;AAE1D,OAAO,EAAc,aAAa,EAAsB,aAAa,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACpH,OAAO,EAAE,KAAK,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAQ5D,MAAM,KAAW,QAAQ,CA+D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,QAgEzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/plaintext",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-18dd3cb26",
|
|
4
4
|
"description": "An insecure connection encrypter",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/connection-encrypter-plaintext#readme",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"build": "aegir build",
|
|
42
42
|
"test": "aegir test",
|
|
43
43
|
"clean": "aegir clean",
|
|
44
|
-
"generate": "protons ./src/pb/
|
|
44
|
+
"generate": "protons ./src/pb/proto.proto",
|
|
45
45
|
"lint": "aegir lint",
|
|
46
46
|
"test:chrome": "aegir test -t browser --cov",
|
|
47
47
|
"test:chrome-webworker": "aegir test -t webworker",
|
|
@@ -52,18 +52,19 @@
|
|
|
52
52
|
"doc-check": "aegir doc-check"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@libp2p/crypto": "
|
|
56
|
-
"@libp2p/interface": "
|
|
57
|
-
"@libp2p/peer-id
|
|
58
|
-
"@libp2p/peer-id": "^4.2.4",
|
|
55
|
+
"@libp2p/crypto": "5.0.0-18dd3cb26",
|
|
56
|
+
"@libp2p/interface": "2.0.0-18dd3cb26",
|
|
57
|
+
"@libp2p/peer-id": "5.0.0-18dd3cb26",
|
|
59
58
|
"it-protobuf-stream": "^1.1.3",
|
|
60
59
|
"it-stream-types": "^2.0.1",
|
|
61
60
|
"protons-runtime": "^5.4.0",
|
|
62
|
-
"uint8arraylist": "^2.4.8"
|
|
61
|
+
"uint8arraylist": "^2.4.8",
|
|
62
|
+
"uint8arrays": "^5.1.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@libp2p/
|
|
66
|
-
"@libp2p/
|
|
65
|
+
"@libp2p/crypto": "5.0.0-18dd3cb26",
|
|
66
|
+
"@libp2p/interface-compliance-tests": "6.0.0-18dd3cb26",
|
|
67
|
+
"@libp2p/logger": "5.0.0-18dd3cb26",
|
|
67
68
|
"@multiformats/multiaddr": "^12.2.3",
|
|
68
69
|
"aegir": "^44.0.1",
|
|
69
70
|
"protons": "^7.5.0",
|
package/src/index.ts
CHANGED
|
@@ -13,45 +13,38 @@
|
|
|
13
13
|
*
|
|
14
14
|
* const node = await createLibp2p({
|
|
15
15
|
* // ...other options
|
|
16
|
-
*
|
|
16
|
+
* connectionEncrypters: [
|
|
17
17
|
* plaintext()
|
|
18
18
|
* ]
|
|
19
19
|
* })
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
import {
|
|
24
|
-
import { UnexpectedPeerError, InvalidCryptoExchangeError, serviceCapabilities } from '@libp2p/interface'
|
|
25
|
-
import {
|
|
26
|
-
import { createFromPubKey } from '@libp2p/peer-id-factory'
|
|
23
|
+
import { publicKeyFromRaw } from '@libp2p/crypto/keys'
|
|
24
|
+
import { UnexpectedPeerError, InvalidCryptoExchangeError, serviceCapabilities, ProtocolError } from '@libp2p/interface'
|
|
25
|
+
import { peerIdFromPublicKey } from '@libp2p/peer-id'
|
|
27
26
|
import { pbStream } from 'it-protobuf-stream'
|
|
28
|
-
import {
|
|
29
|
-
import
|
|
27
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
28
|
+
import { Exchange, KeyType } from './pb/proto.js'
|
|
29
|
+
import type { ComponentLogger, Logger, MultiaddrConnection, ConnectionEncrypter, SecuredConnection, PrivateKey, SecureConnectionOptions } from '@libp2p/interface'
|
|
30
30
|
import type { Duplex } from 'it-stream-types'
|
|
31
31
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
32
32
|
|
|
33
33
|
const PROTOCOL = '/plaintext/2.0.0'
|
|
34
34
|
|
|
35
35
|
export interface PlaintextComponents {
|
|
36
|
+
privateKey: PrivateKey
|
|
36
37
|
logger: ComponentLogger
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
export interface PlaintextInit {
|
|
40
|
-
/**
|
|
41
|
-
* The peer id exchange must complete within this many milliseconds
|
|
42
|
-
* (default: 1000)
|
|
43
|
-
*/
|
|
44
|
-
timeout?: number
|
|
45
|
-
}
|
|
46
|
-
|
|
47
40
|
class Plaintext implements ConnectionEncrypter {
|
|
48
41
|
public protocol: string = PROTOCOL
|
|
42
|
+
private readonly privateKey: PrivateKey
|
|
49
43
|
private readonly log: Logger
|
|
50
|
-
private readonly timeout: number
|
|
51
44
|
|
|
52
|
-
constructor (components: PlaintextComponents
|
|
45
|
+
constructor (components: PlaintextComponents) {
|
|
46
|
+
this.privateKey = components.privateKey
|
|
53
47
|
this.log = components.logger.forComponent('libp2p:plaintext')
|
|
54
|
-
this.timeout = init.timeout ?? 1000
|
|
55
48
|
}
|
|
56
49
|
|
|
57
50
|
readonly [Symbol.toStringTag] = '@libp2p/plaintext'
|
|
@@ -60,87 +53,65 @@ class Plaintext implements ConnectionEncrypter {
|
|
|
60
53
|
'@libp2p/connection-encryption'
|
|
61
54
|
]
|
|
62
55
|
|
|
63
|
-
async secureInbound
|
|
64
|
-
return this._encrypt(
|
|
56
|
+
async secureInbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
|
|
57
|
+
return this._encrypt(conn, options)
|
|
65
58
|
}
|
|
66
59
|
|
|
67
|
-
async secureOutbound
|
|
68
|
-
return this._encrypt(
|
|
60
|
+
async secureOutbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
|
|
61
|
+
return this._encrypt(conn, options)
|
|
69
62
|
}
|
|
70
63
|
|
|
71
64
|
/**
|
|
72
65
|
* Encrypt connection
|
|
73
66
|
*/
|
|
74
|
-
async _encrypt
|
|
75
|
-
const signal = AbortSignal.timeout(this.timeout)
|
|
67
|
+
async _encrypt<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
|
|
76
68
|
const pb = pbStream(conn).pb(Exchange)
|
|
77
69
|
|
|
78
|
-
|
|
70
|
+
this.log('write pubkey exchange to peer %p', options?.remotePeer)
|
|
79
71
|
|
|
80
|
-
|
|
81
|
-
type = KeyType.Ed25519
|
|
82
|
-
} else if (localId.type === 'secp256k1') {
|
|
83
|
-
type = KeyType.Secp256k1
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
this.log('write pubkey exchange to peer %p', remoteId)
|
|
72
|
+
const publicKey = this.privateKey.publicKey
|
|
87
73
|
|
|
88
74
|
const [
|
|
89
75
|
, response
|
|
90
76
|
] = await Promise.all([
|
|
91
77
|
// Encode the public key and write it to the remote peer
|
|
92
78
|
pb.write({
|
|
93
|
-
id:
|
|
79
|
+
id: publicKey.toMultihash().bytes,
|
|
94
80
|
pubkey: {
|
|
95
|
-
Type: type,
|
|
96
|
-
Data:
|
|
81
|
+
Type: KeyType[publicKey.type],
|
|
82
|
+
Data: publicKey.raw
|
|
97
83
|
}
|
|
98
|
-
},
|
|
99
|
-
signal
|
|
100
|
-
}),
|
|
84
|
+
}, options),
|
|
101
85
|
// Get the Exchange message
|
|
102
|
-
pb.read(
|
|
103
|
-
signal
|
|
104
|
-
})
|
|
86
|
+
pb.read(options)
|
|
105
87
|
])
|
|
106
88
|
|
|
107
89
|
let peerId
|
|
108
90
|
try {
|
|
109
91
|
if (response.pubkey == null) {
|
|
110
|
-
throw new
|
|
92
|
+
throw new ProtocolError('Public key missing')
|
|
111
93
|
}
|
|
112
94
|
|
|
113
|
-
if (response.pubkey.Data.
|
|
114
|
-
throw new
|
|
95
|
+
if (response.pubkey.Data.byteLength === 0) {
|
|
96
|
+
throw new ProtocolError('Public key data too short')
|
|
115
97
|
}
|
|
116
98
|
|
|
117
99
|
if (response.id == null) {
|
|
118
|
-
throw new
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
let pubKey: PubKey
|
|
122
|
-
|
|
123
|
-
if (response.pubkey.Type === KeyType.RSA) {
|
|
124
|
-
pubKey = supportedKeys.rsa.unmarshalRsaPublicKey(response.pubkey.Data)
|
|
125
|
-
} else if (response.pubkey.Type === KeyType.Ed25519) {
|
|
126
|
-
pubKey = supportedKeys.ed25519.unmarshalEd25519PublicKey(response.pubkey.Data)
|
|
127
|
-
} else if (response.pubkey.Type === KeyType.Secp256k1) {
|
|
128
|
-
pubKey = supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(response.pubkey.Data)
|
|
129
|
-
} else {
|
|
130
|
-
throw new Error('Unknown public key type')
|
|
100
|
+
throw new ProtocolError('Remote id missing')
|
|
131
101
|
}
|
|
132
102
|
|
|
133
|
-
|
|
103
|
+
const pubKey = publicKeyFromRaw(response.pubkey.Data)
|
|
104
|
+
peerId = peerIdFromPublicKey(pubKey)
|
|
134
105
|
|
|
135
|
-
if (!peerId.
|
|
136
|
-
throw new
|
|
106
|
+
if (!uint8ArrayEquals(peerId.toMultihash().bytes, response.id)) {
|
|
107
|
+
throw new InvalidCryptoExchangeError('Public key did not match id')
|
|
137
108
|
}
|
|
138
109
|
} catch (err: any) {
|
|
139
110
|
this.log.error(err)
|
|
140
|
-
throw new InvalidCryptoExchangeError('
|
|
111
|
+
throw new InvalidCryptoExchangeError('Invalid public key - ' + err.message)
|
|
141
112
|
}
|
|
142
113
|
|
|
143
|
-
if (
|
|
114
|
+
if (options?.remotePeer != null && !peerId.equals(options?.remotePeer)) {
|
|
144
115
|
throw new UnexpectedPeerError()
|
|
145
116
|
}
|
|
146
117
|
|
|
@@ -153,6 +124,6 @@ class Plaintext implements ConnectionEncrypter {
|
|
|
153
124
|
}
|
|
154
125
|
}
|
|
155
126
|
|
|
156
|
-
export function plaintext (
|
|
157
|
-
return (components) => new Plaintext(components
|
|
127
|
+
export function plaintext (): (components: PlaintextComponents) => ConnectionEncrypter {
|
|
128
|
+
return (components) => new Plaintext(components)
|
|
158
129
|
}
|