Skip to content

Commit 9d5a07f

Browse files
authored
chore!: update protons and connection encryption interface (#193)
* chore: update protons and connection encryption interface * address PR comments
1 parent 15f7a6e commit 9d5a07f

File tree

3 files changed

+94
-21
lines changed

3 files changed

+94
-21
lines changed

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
"dependencies": {
6969
"@libp2p/crypto": "^1.0.0",
70-
"@libp2p/interface-connection-encrypter": "^1.0.2",
70+
"@libp2p/interface-connection-encrypter": "^2.0.1",
7171
"@libp2p/interface-keys": "^1.0.2",
7272
"@libp2p/interface-peer-id": "^1.0.2",
7373
"@libp2p/logger": "^2.0.0",
@@ -78,22 +78,23 @@
7878
"@stablelib/x25519": "^1.0.1",
7979
"it-length-prefixed": "^8.0.2",
8080
"it-pair": "^2.0.2",
81-
"it-pb-stream": "^2.0.1",
82-
"it-pipe": "^2.0.3",
8381
"it-stream-types": "^1.0.4",
84-
"protons-runtime": "^2.0.1",
85-
"uint8arraylist": "^2.0.0",
86-
"uint8arrays": "^3.0.0"
82+
"it-pb-stream": "^2.0.2",
83+
"it-pipe": "^2.0.3",
84+
"protons-runtime": "^3.1.0",
85+
"uint8arraylist": "^2.3.2",
86+
"uint8arrays": "^3.1.0"
8787
},
8888
"devDependencies": {
89-
"@libp2p/interface-connection-encrypter-compliance-tests": "^1.0.1",
89+
"@libp2p/interface-connection-encrypter-compliance-tests": "^2.0.1",
9090
"@libp2p/peer-id-factory": "^1.0.8",
9191
"aegir": "^37.3.0",
9292
"benchmark": "^2.1.4",
9393
"iso-random-stream": "^2.0.2",
9494
"mkdirp": "^1.0.4",
95-
"protons": "^4.0.0",
96-
"sinon": "^14.0.0"
95+
"protons": "^5.1.0",
96+
"sinon": "^14.0.0",
97+
"util": "^0.12.4"
9798
},
9899
"browser": {
99100
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js",

src/noise.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export class Noise implements INoiseConnection {
5252
* Encrypt outgoing data to the remote party (handshake as initiator)
5353
*
5454
* @param {PeerId} localPeer - PeerId of the receiving peer
55-
* @param {any} connection - streaming iterable duplex that will be encrypted
55+
* @param {Duplex<Uint8Array>} connection - streaming iterable duplex that will be encrypted
5656
* @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
5757
* @returns {Promise<SecuredConnection>}
5858
*/
59-
public async secureOutbound (localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecuredConnection> {
59+
public async secureOutbound (localPeer: PeerId, connection: Duplex<Uint8Array>, remotePeer: PeerId): Promise<SecuredConnection> {
6060
const wrappedConnection = pbStream(
6161
connection,
6262
{
@@ -84,11 +84,11 @@ export class Noise implements INoiseConnection {
8484
* Decrypt incoming data (handshake as responder).
8585
*
8686
* @param {PeerId} localPeer - PeerId of the receiving peer.
87-
* @param {any} connection - streaming iterable duplex that will be encryption.
87+
* @param {Duplex<Uint8Array>} connection - streaming iterable duplex that will be encryption.
8888
* @param {PeerId} remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades.
8989
* @returns {Promise<SecuredConnection>}
9090
*/
91-
public async secureInbound (localPeer: PeerId, connection: any, remotePeer?: PeerId): Promise<SecuredConnection> {
91+
public async secureInbound (localPeer: PeerId, connection: Duplex<Uint8Array>, remotePeer?: PeerId): Promise<SecuredConnection> {
9292
const wrappedConnection = pbStream(
9393
connection,
9494
{

src/proto/payload.ts

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable import/export */
22
/* eslint-disable @typescript-eslint/no-namespace */
33

4-
import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
5-
import type { Codec } from 'protons-runtime'
4+
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
65
import type { Uint8ArrayList } from 'uint8arraylist'
6+
import type { Codec } from 'protons-runtime'
77

88
export namespace pb {
99
export interface NoiseHandshakePayload {
@@ -13,15 +13,87 @@ export namespace pb {
1313
}
1414

1515
export namespace NoiseHandshakePayload {
16+
let _codec: Codec<NoiseHandshakePayload>
17+
1618
export const codec = (): Codec<NoiseHandshakePayload> => {
17-
return message<NoiseHandshakePayload>({
18-
1: { name: 'identityKey', codec: bytes },
19-
2: { name: 'identitySig', codec: bytes },
20-
3: { name: 'data', codec: bytes }
21-
})
19+
if (_codec == null) {
20+
_codec = message<NoiseHandshakePayload>((obj, writer, opts = {}) => {
21+
if (opts.lengthDelimited !== false) {
22+
writer.fork()
23+
}
24+
25+
if (obj.identityKey != null) {
26+
writer.uint32(10)
27+
writer.bytes(obj.identityKey)
28+
} else {
29+
throw new Error('Protocol error: required field "identityKey" was not found in object')
30+
}
31+
32+
if (obj.identitySig != null) {
33+
writer.uint32(18)
34+
writer.bytes(obj.identitySig)
35+
} else {
36+
throw new Error('Protocol error: required field "identitySig" was not found in object')
37+
}
38+
39+
if (obj.data != null) {
40+
writer.uint32(26)
41+
writer.bytes(obj.data)
42+
} else {
43+
throw new Error('Protocol error: required field "data" was not found in object')
44+
}
45+
46+
if (opts.lengthDelimited !== false) {
47+
writer.ldelim()
48+
}
49+
}, (reader, length) => {
50+
const obj: any = {
51+
identityKey: new Uint8Array(0),
52+
identitySig: new Uint8Array(0),
53+
data: new Uint8Array(0)
54+
}
55+
56+
const end = length == null ? reader.len : reader.pos + length
57+
58+
while (reader.pos < end) {
59+
const tag = reader.uint32()
60+
61+
switch (tag >>> 3) {
62+
case 1:
63+
obj.identityKey = reader.bytes()
64+
break
65+
case 2:
66+
obj.identitySig = reader.bytes()
67+
break
68+
case 3:
69+
obj.data = reader.bytes()
70+
break
71+
default:
72+
reader.skipType(tag & 7)
73+
break
74+
}
75+
}
76+
77+
if (obj.identityKey == null) {
78+
throw new Error('Protocol error: value for required field "identityKey" was not found in protobuf')
79+
}
80+
81+
if (obj.identitySig == null) {
82+
throw new Error('Protocol error: value for required field "identitySig" was not found in protobuf')
83+
}
84+
85+
if (obj.data == null) {
86+
throw new Error('Protocol error: value for required field "data" was not found in protobuf')
87+
}
88+
89+
return obj
90+
})
91+
}
92+
93+
return _codec
2294
}
2395

24-
export const encode = (obj: NoiseHandshakePayload): Uint8ArrayList => {
96+
export const encode = (obj: NoiseHandshakePayload): Uint8Array => {
2597
return encodeMessage(obj, NoiseHandshakePayload.codec())
2698
}
2799

0 commit comments

Comments
 (0)