Skip to content

Commit

Permalink
chore!: update protons and connection encryption interface (#193)
Browse files Browse the repository at this point in the history
* chore: update protons and connection encryption interface

* address PR comments
  • Loading branch information
mpetrunic authored Aug 11, 2022
1 parent 15f7a6e commit 9d5a07f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"dependencies": {
"@libp2p/crypto": "^1.0.0",
"@libp2p/interface-connection-encrypter": "^1.0.2",
"@libp2p/interface-connection-encrypter": "^2.0.1",
"@libp2p/interface-keys": "^1.0.2",
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/logger": "^2.0.0",
Expand All @@ -78,22 +78,23 @@
"@stablelib/x25519": "^1.0.1",
"it-length-prefixed": "^8.0.2",
"it-pair": "^2.0.2",
"it-pb-stream": "^2.0.1",
"it-pipe": "^2.0.3",
"it-stream-types": "^1.0.4",
"protons-runtime": "^2.0.1",
"uint8arraylist": "^2.0.0",
"uint8arrays": "^3.0.0"
"it-pb-stream": "^2.0.2",
"it-pipe": "^2.0.3",
"protons-runtime": "^3.1.0",
"uint8arraylist": "^2.3.2",
"uint8arrays": "^3.1.0"
},
"devDependencies": {
"@libp2p/interface-connection-encrypter-compliance-tests": "^1.0.1",
"@libp2p/interface-connection-encrypter-compliance-tests": "^2.0.1",
"@libp2p/peer-id-factory": "^1.0.8",
"aegir": "^37.3.0",
"benchmark": "^2.1.4",
"iso-random-stream": "^2.0.2",
"mkdirp": "^1.0.4",
"protons": "^4.0.0",
"sinon": "^14.0.0"
"protons": "^5.1.0",
"sinon": "^14.0.0",
"util": "^0.12.4"
},
"browser": {
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js",
Expand Down
8 changes: 4 additions & 4 deletions src/noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export class Noise implements INoiseConnection {
* Encrypt outgoing data to the remote party (handshake as initiator)
*
* @param {PeerId} localPeer - PeerId of the receiving peer
* @param {any} connection - streaming iterable duplex that will be encrypted
* @param {Duplex<Uint8Array>} connection - streaming iterable duplex that will be encrypted
* @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
* @returns {Promise<SecuredConnection>}
*/
public async secureOutbound (localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecuredConnection> {
public async secureOutbound (localPeer: PeerId, connection: Duplex<Uint8Array>, remotePeer: PeerId): Promise<SecuredConnection> {
const wrappedConnection = pbStream(
connection,
{
Expand Down Expand Up @@ -84,11 +84,11 @@ export class Noise implements INoiseConnection {
* Decrypt incoming data (handshake as responder).
*
* @param {PeerId} localPeer - PeerId of the receiving peer.
* @param {any} connection - streaming iterable duplex that will be encryption.
* @param {Duplex<Uint8Array>} connection - streaming iterable duplex that will be encryption.
* @param {PeerId} remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades.
* @returns {Promise<SecuredConnection>}
*/
public async secureInbound (localPeer: PeerId, connection: any, remotePeer?: PeerId): Promise<SecuredConnection> {
public async secureInbound (localPeer: PeerId, connection: Duplex<Uint8Array>, remotePeer?: PeerId): Promise<SecuredConnection> {
const wrappedConnection = pbStream(
connection,
{
Expand Down
88 changes: 80 additions & 8 deletions src/proto/payload.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable import/export */
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Codec } from 'protons-runtime'

export namespace pb {
export interface NoiseHandshakePayload {
Expand All @@ -13,15 +13,87 @@ export namespace pb {
}

export namespace NoiseHandshakePayload {
let _codec: Codec<NoiseHandshakePayload>

export const codec = (): Codec<NoiseHandshakePayload> => {
return message<NoiseHandshakePayload>({
1: { name: 'identityKey', codec: bytes },
2: { name: 'identitySig', codec: bytes },
3: { name: 'data', codec: bytes }
})
if (_codec == null) {
_codec = message<NoiseHandshakePayload>((obj, writer, opts = {}) => {
if (opts.lengthDelimited !== false) {
writer.fork()
}

if (obj.identityKey != null) {
writer.uint32(10)
writer.bytes(obj.identityKey)
} else {
throw new Error('Protocol error: required field "identityKey" was not found in object')
}

if (obj.identitySig != null) {
writer.uint32(18)
writer.bytes(obj.identitySig)
} else {
throw new Error('Protocol error: required field "identitySig" was not found in object')
}

if (obj.data != null) {
writer.uint32(26)
writer.bytes(obj.data)
} else {
throw new Error('Protocol error: required field "data" was not found in object')
}

if (opts.lengthDelimited !== false) {
writer.ldelim()
}
}, (reader, length) => {
const obj: any = {
identityKey: new Uint8Array(0),
identitySig: new Uint8Array(0),
data: new Uint8Array(0)
}

const end = length == null ? reader.len : reader.pos + length

while (reader.pos < end) {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
obj.identityKey = reader.bytes()
break
case 2:
obj.identitySig = reader.bytes()
break
case 3:
obj.data = reader.bytes()
break
default:
reader.skipType(tag & 7)
break
}
}

if (obj.identityKey == null) {
throw new Error('Protocol error: value for required field "identityKey" was not found in protobuf')
}

if (obj.identitySig == null) {
throw new Error('Protocol error: value for required field "identitySig" was not found in protobuf')
}

if (obj.data == null) {
throw new Error('Protocol error: value for required field "data" was not found in protobuf')
}

return obj
})
}

return _codec
}

export const encode = (obj: NoiseHandshakePayload): Uint8ArrayList => {
export const encode = (obj: NoiseHandshakePayload): Uint8Array => {
return encodeMessage(obj, NoiseHandshakePayload.codec())
}

Expand Down

0 comments on commit 9d5a07f

Please sign in to comment.