Skip to content

Commit 8426920

Browse files
fix!: update stream types (#316)
* deps!: update stream types libp2p streams are now explicit about the types of sync/sources they provide, showing that they are `AsyncGenerator`s and not just `AsyncIterable`s. Refs: achingbrain/it-stream-types#45 BREAKING CHANGE: the type of the source/sink properties have changed * fix: interop Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> --------- Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> Co-authored-by: Marin Petrunic <marin.petrunic@gmail.com>
1 parent de40989 commit 8426920

5 files changed

Lines changed: 27 additions & 26 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ lerna-debug.log*
1717
coverage/*
1818
package-lock.json
1919
yarn.lock
20+
.vscode

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
},
7070
"dependencies": {
7171
"@libp2p/crypto": "^1.0.11",
72-
"@libp2p/interface-connection-encrypter": "^3.0.5",
72+
"@libp2p/interface-connection-encrypter": "^4.0.0",
7373
"@libp2p/interface-keys": "^1.0.6",
7474
"@libp2p/interface-metrics": "^4.0.4",
7575
"@libp2p/interface-peer-id": "^2.0.0",
@@ -79,31 +79,31 @@
7979
"@stablelib/hkdf": "^1.0.1",
8080
"@stablelib/sha256": "^1.0.1",
8181
"@stablelib/x25519": "^1.0.3",
82-
"it-length-prefixed": "^8.0.2",
82+
"it-length-prefixed": "^9.0.1",
8383
"it-pair": "^2.0.2",
84-
"it-pb-stream": "^3.2.0",
85-
"it-pipe": "^2.0.3",
86-
"it-stream-types": "^1.0.4",
84+
"it-pb-stream": "^4.0.1",
85+
"it-pipe": "^3.0.1",
86+
"it-stream-types": "^2.0.1",
8787
"protons-runtime": "^5.0.0",
8888
"uint8arraylist": "^2.3.2",
8989
"uint8arrays": "^4.0.2"
9090
},
9191
"devDependencies": {
92-
"@libp2p/daemon-client": "^5.0.0",
93-
"@libp2p/daemon-server": "^4.0.1",
94-
"@libp2p/interface-connection-encrypter-compliance-tests": "^4.0.0",
95-
"@libp2p/interop": "^7.0.3",
96-
"@libp2p/mplex": "^7.0.0",
92+
"@chainsafe/libp2p-yamux": "^4.0.1",
93+
"@libp2p/daemon-client": "^6.0.3",
94+
"@libp2p/daemon-server": "^5.0.2",
95+
"@libp2p/interface-connection-encrypter-compliance-tests": "^5.0.0",
96+
"@libp2p/interop": "^8.0.1",
9797
"@libp2p/peer-id-factory": "^2.0.0",
98-
"@libp2p/tcp": "^6.0.2",
98+
"@libp2p/tcp": "^7.0.0",
9999
"@multiformats/multiaddr": "^12.1.0",
100100
"aegir": "^38.1.7",
101101
"benchmark": "^2.1.4",
102102
"execa": "^7.0.0",
103-
"go-libp2p": "^0.0.6",
103+
"go-libp2p": "^1.0.3",
104104
"iso-random-stream": "^2.0.2",
105-
"libp2p": "^0.43.2",
106-
"mkdirp": "^2.0.0",
105+
"libp2p": "0.44.0-59f34a21",
106+
"mkdirp": "^3.0.0",
107107
"p-defer": "^4.0.0",
108108
"protons": "^7.0.0",
109109
"sinon": "^15.0.0",

src/crypto/streaming.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } fr
77
import { uint16BEEncode } from '../encoder.js'
88

99
// Returns generator that encrypts payload from the user
10-
export function encryptStream (handshake: IHandshake, metrics?: MetricsRegistry): Transform<Uint8Array> {
10+
export function encryptStream (handshake: IHandshake, metrics?: MetricsRegistry): Transform<AsyncIterable<Uint8Array>> {
1111
return async function * (source) {
1212
for await (const chunk of source) {
1313
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG) {
@@ -27,7 +27,7 @@ export function encryptStream (handshake: IHandshake, metrics?: MetricsRegistry)
2727
}
2828

2929
// Decrypt received payload to the user
30-
export function decryptStream (handshake: IHandshake, metrics?: MetricsRegistry): Transform<Uint8ArrayList, Uint8Array> {
30+
export function decryptStream (handshake: IHandshake, metrics?: MetricsRegistry): Transform<AsyncIterable<Uint8ArrayList>, AsyncIterable<Uint8Array>> {
3131
return async function * (source) {
3232
for await (const chunk of source) {
3333
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES) {

src/noise.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { pbStream, ProtobufStream } from 'it-pb-stream'
44
import { duplexPair } from 'it-pair/duplex'
55
import { pipe } from 'it-pipe'
66
import { decode } from 'it-length-prefixed'
7-
import type { Duplex } from 'it-stream-types'
7+
import type { Duplex, Source } from 'it-stream-types'
88
import type { bytes } from './@types/basic.js'
99
import type { IHandshake } from './@types/handshake-interface.js'
1010
import type { INoiseConnection, KeyPair } from './@types/libp2p.js'
@@ -66,11 +66,11 @@ export class Noise implements INoiseConnection {
6666
* Encrypt outgoing data to the remote party (handshake as initiator)
6767
*
6868
* @param {PeerId} localPeer - PeerId of the receiving peer
69-
* @param {Duplex<Uint8Array>} connection - streaming iterable duplex that will be encrypted
69+
* @param {Duplex<AsyncGenerator<Uint8Array>, AsyncIterable<Uint8Array>, Promise<void>>} connection - streaming iterable duplex that will be encrypted
7070
* @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
7171
* @returns {Promise<SecuredConnection>}
7272
*/
73-
public async secureOutbound (localPeer: PeerId, connection: Duplex<Uint8Array>, remotePeer?: PeerId): Promise<SecuredConnection<NoiseExtensions>> {
73+
public async secureOutbound (localPeer: PeerId, connection: Duplex<AsyncGenerator<Uint8Array>, AsyncIterable<Uint8Array>, Promise<void>>, remotePeer?: PeerId): Promise<SecuredConnection<NoiseExtensions>> {
7474
const wrappedConnection = pbStream(
7575
connection,
7676
{
@@ -98,11 +98,11 @@ export class Noise implements INoiseConnection {
9898
* Decrypt incoming data (handshake as responder).
9999
*
100100
* @param {PeerId} localPeer - PeerId of the receiving peer.
101-
* @param {Duplex<Uint8Array>} connection - streaming iterable duplex that will be encryption.
101+
* @param {Duplex<AsyncGenerator<Uint8Array>, AsyncIterable<Uint8Array>, Promise<void>>} connection - streaming iterable duplex that will be encryption.
102102
* @param {PeerId} remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades.
103103
* @returns {Promise<SecuredConnection>}
104104
*/
105-
public async secureInbound (localPeer: PeerId, connection: Duplex<Uint8Array>, remotePeer?: PeerId): Promise<SecuredConnection<NoiseExtensions>> {
105+
public async secureInbound (localPeer: PeerId, connection: Duplex<AsyncGenerator<Uint8Array>, AsyncIterable<Uint8Array>, Promise<void>>, remotePeer?: PeerId): Promise<SecuredConnection<NoiseExtensions>> {
106106
const wrappedConnection = pbStream(
107107
connection,
108108
{
@@ -171,9 +171,9 @@ export class Noise implements INoiseConnection {
171171
}
172172

173173
private async createSecureConnection (
174-
connection: ProtobufStream,
174+
connection: ProtobufStream<Duplex<AsyncGenerator<Uint8Array>, AsyncIterable<Uint8Array>, Promise<void>>>,
175175
handshake: IHandshake
176-
): Promise<Duplex<Uint8Array>> {
176+
): Promise<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>> {
177177
// Create encryption box/unbox wrapper
178178
const [secure, user] = duplexPair<Uint8Array>()
179179
const network = connection.unwrap()
@@ -182,7 +182,7 @@ export class Noise implements INoiseConnection {
182182
secure, // write to wrapper
183183
encryptStream(handshake, this.metrics), // encrypt data + prefix with message length
184184
network, // send to the remote peer
185-
decode({ lengthDecoder: uint16BEDecode }), // read message length prefix
185+
(source) => decode(source, { lengthDecoder: uint16BEDecode }), // read message length prefix
186186
decryptStream(handshake, this.metrics), // decrypt the incoming data
187187
secure // pipe to the wrapper
188188
)

test/interop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { path as p2pd } from 'go-libp2p'
99
import { execa } from 'execa'
1010
import pDefer from 'p-defer'
1111
import { logger } from '@libp2p/logger'
12-
import { mplex } from '@libp2p/mplex'
12+
import { yamux } from '@chainsafe/libp2p-yamux'
1313
import fs from 'fs'
1414
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'
1515
import type { PeerId } from '@libp2p/interface-peer-id'
@@ -77,7 +77,7 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
7777
listen: ['/ip4/0.0.0.0/tcp/0']
7878
},
7979
transports: [tcp()],
80-
streamMuxers: [mplex()],
80+
streamMuxers: [yamux()],
8181
connectionEncryption: [noise()]
8282
}
8383

0 commit comments

Comments
 (0)