Skip to content

feat!: streams as EventTargets #3218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 1 addition & 2 deletions packages/connection-encrypter-plaintext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@libp2p/crypto": "^5.1.7",
"@libp2p/interface": "^2.10.5",
"@libp2p/peer-id": "^5.1.8",
"it-protobuf-stream": "^2.0.2",
"@libp2p/utils": "^6.7.1",
"protons-runtime": "^5.5.0",
"uint8arraylist": "^2.4.8",
"uint8arrays": "^5.1.0"
Expand All @@ -57,7 +57,6 @@
"@libp2p/crypto": "^5.1.7",
"@libp2p/logger": "^5.1.21",
"aegir": "^47.0.14",
"it-pair": "^2.0.6",
"protons": "^7.6.1",
"sinon": "^20.0.0"
},
Expand Down
10 changes: 5 additions & 5 deletions packages/connection-encrypter-plaintext/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import { publicKeyFromRaw } from '@libp2p/crypto/keys'
import { UnexpectedPeerError, InvalidCryptoExchangeError, serviceCapabilities, ProtocolError } from '@libp2p/interface'
import { peerIdFromPublicKey } from '@libp2p/peer-id'
import { pbStream } from 'it-protobuf-stream'
import { pbStream } from '@libp2p/utils'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { Exchange, KeyType } from './pb/proto.js'
import type { ComponentLogger, Logger, MultiaddrConnection, ConnectionEncrypter, SecuredConnection, PrivateKey, SecureConnectionOptions, SecurableStream } from '@libp2p/interface'
import type { ComponentLogger, Logger, MultiaddrConnection, ConnectionEncrypter, SecuredConnection, PrivateKey, SecureConnectionOptions, MessageStream } from '@libp2p/interface'

const PROTOCOL = '/plaintext/2.0.0'

Expand All @@ -52,18 +52,18 @@ class Plaintext implements ConnectionEncrypter {
'@libp2p/connection-encryption'
]

async secureInbound<Stream extends SecurableStream = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async secureInbound<Stream extends MessageStream = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection> {
return this._encrypt(conn, options)
}

async secureOutbound<Stream extends SecurableStream = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async secureOutbound<Stream extends MessageStream = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection> {
return this._encrypt(conn, options)
}

/**
* Encrypt connection
*/
async _encrypt<Stream extends SecurableStream = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async _encrypt<Stream extends MessageStream = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection> {
const log = conn.log?.newScope('plaintext') ?? this.log
const pb = pbStream(conn).pb(Exchange)

Expand Down
6 changes: 3 additions & 3 deletions packages/connection-encrypter-plaintext/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { generateKeyPair } from '@libp2p/crypto/keys'
import { defaultLogger } from '@libp2p/logger'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { streamPair } from '@libp2p/utils'
import { expect } from 'aegir/chai'
import { duplexPair } from 'it-pair/duplex'
import sinon from 'sinon'
import { plaintext } from '../src/index.js'
import type { ConnectionEncrypter, PeerId } from '@libp2p/interface'
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('plaintext', () => {
})

it('should verify the public key and id match', async () => {
const [inbound, outbound] = duplexPair<any>()
const [inbound, outbound] = await streamPair()

await Promise.all([
encrypter.secureInbound(inbound),
Expand All @@ -57,7 +57,7 @@ describe('plaintext', () => {
logger: defaultLogger()
})

const [inbound, outbound] = duplexPair<any>()
const [inbound, outbound] = await streamPair()

await expect(Promise.all([
encrypter.secureInbound(inbound),
Expand Down
5 changes: 2 additions & 3 deletions packages/connection-encrypter-tls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,21 @@
"@libp2p/crypto": "^5.1.7",
"@libp2p/interface": "^2.10.5",
"@libp2p/peer-id": "^5.1.8",
"@libp2p/utils": "^6.7.1",
"@peculiar/asn1-schema": "^2.3.15",
"@peculiar/asn1-x509": "^2.3.15",
"@peculiar/webcrypto": "^1.5.0",
"@peculiar/x509": "^1.12.3",
"asn1js": "^3.0.6",
"it-queueless-pushable": "^2.0.1",
"it-stream-types": "^2.0.2",
"protons-runtime": "^5.5.0",
"uint8arraylist": "^2.4.8",
"uint8arrays": "^5.1.0"
},
"devDependencies": {
"@libp2p/logger": "^5.1.21",
"aegir": "^47.0.14",
"it-pair": "^2.0.6",
"protons": "^7.6.1",
"race-event": "^1.6.1",
"sinon": "^20.0.0",
"sinon-ts": "^2.0.0"
},
Expand Down
8 changes: 3 additions & 5 deletions packages/connection-encrypter-tls/src/tls.browser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { serviceCapabilities } from '@libp2p/interface'
import { PROTOCOL } from './index.js'
import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, SecureConnectionOptions } from '@libp2p/interface'
import type { Duplex } from 'it-stream-types'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, SecureConnectionOptions, MessageStream } from '@libp2p/interface'

export class TLS implements ConnectionEncrypter {
public protocol: string = PROTOCOL
Expand All @@ -17,11 +15,11 @@ export class TLS implements ConnectionEncrypter {
'@libp2p/connection-encryption'
]

async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async secureInbound <Stream extends MessageStream = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
throw new Error('TLS encryption is not possible in browsers')
}

async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async secureOutbound <Stream extends MessageStream = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
throw new Error('TLS encryption is not possible in browsers')
}
}
19 changes: 8 additions & 11 deletions packages/connection-encrypter-tls/src/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import { TLSSocket, connect } from 'node:tls'
import { InvalidCryptoExchangeError, serviceCapabilities } from '@libp2p/interface'
import { HandshakeTimeoutError } from './errors.js'
import { generateCertificate, verifyPeerCertificate, itToStream, streamToIt } from './utils.js'
import { generateCertificate, verifyPeerCertificate, toNodeDuplex, toMessageStream } from './utils.js'
import { PROTOCOL } from './index.js'
import type { TLSComponents } from './index.js'
import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, Logger, SecureConnectionOptions, CounterGroup, StreamMuxerFactory, SecurableStream } from '@libp2p/interface'
import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, Logger, SecureConnectionOptions, CounterGroup, StreamMuxerFactory, MessageStream } from '@libp2p/interface'
import type { TLSSocketOptions } from 'node:tls'

export class TLS implements ConnectionEncrypter {
Expand Down Expand Up @@ -75,18 +75,18 @@ export class TLS implements ConnectionEncrypter {
'@libp2p/connection-encryption'
]

async secureInbound <Stream extends SecurableStream = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async secureInbound <Stream extends MessageStream = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection> {
return this._encrypt(conn, true, options)
}

async secureOutbound <Stream extends SecurableStream = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async secureOutbound <Stream extends MessageStream = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection> {
return this._encrypt(conn, false, options)
}

/**
* Encrypt connection
*/
async _encrypt <Stream extends SecurableStream = MultiaddrConnection> (conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async _encrypt <Stream extends MessageStream = MultiaddrConnection> (conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection> {
const log = conn.log?.newScope('tls') ?? this.log
let streamMuxer: StreamMuxerFactory | undefined

Expand Down Expand Up @@ -134,14 +134,14 @@ export class TLS implements ConnectionEncrypter {
let socket: TLSSocket

if (isServer) {
socket = new TLSSocket(itToStream(conn), {
socket = new TLSSocket(toNodeDuplex(conn), {
...opts,
// require clients to send certificates
requestCert: true
})
} else {
socket = connect({
socket: itToStream(conn),
socket: toNodeDuplex(conn),
...opts
})
}
Expand Down Expand Up @@ -185,10 +185,7 @@ export class TLS implements ConnectionEncrypter {

resolve({
remotePeer,
conn: {
...conn,
...streamToIt(socket)
},
conn: toMessageStream(conn, socket),
streamMuxer
})
})
Expand Down
Loading
Loading