Skip to content

Commit

Permalink
Merge branch 'release-v2.0' into fix/make-connection-securing-abortable
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Aug 14, 2024
2 parents 277f755 + dc8c1ec commit 40bc784
Show file tree
Hide file tree
Showing 70 changed files with 700 additions and 234 deletions.
5 changes: 5 additions & 0 deletions funding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"opRetro": {
"projectId": "0x966804cb492e1a4bde5d781a676a44a23d69aa5dd2562fa7a4f95bb606021c8b"
}
}
11 changes: 7 additions & 4 deletions packages/connection-encrypter-plaintext/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ import type { Uint8ArrayList } from 'uint8arraylist'
const PROTOCOL = '/plaintext/2.0.0'

export interface PlaintextComponents {
peerId: PeerId
logger: ComponentLogger
}

class Plaintext implements ConnectionEncrypter {
public protocol: string = PROTOCOL
private readonly peerId: PeerId
private readonly log: Logger

constructor (components: PlaintextComponents) {
this.peerId = components.peerId
this.log = components.logger.forComponent('libp2p:plaintext')
}

Expand All @@ -48,12 +51,12 @@ class Plaintext implements ConnectionEncrypter {
'@libp2p/connection-encryption'
]

async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(localId, conn, options)
async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(this.peerId, conn, options)
}

async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(localId, conn, options)
async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(this.peerId, conn, options)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import suite from '@libp2p/interface-compliance-tests/connection-encryption'
import { defaultLogger } from '@libp2p/logger'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { plaintext } from '../src/index.js'

describe('plaintext compliance', () => {
suite({
async setup () {
async setup (opts) {
return plaintext()({
peerId: opts?.peerId ?? await createEd25519PeerId(),
logger: defaultLogger()
})
},
Expand Down
19 changes: 15 additions & 4 deletions packages/connection-encrypter-plaintext/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('plaintext', () => {
let remotePeer: PeerId
let wrongPeer: PeerId
let encrypter: ConnectionEncrypter
let encrypterRemote: ConnectionEncrypter

beforeEach(async () => {
[localPeer, remotePeer, wrongPeer] = await Promise.all([
Expand All @@ -28,6 +29,11 @@ describe('plaintext', () => {
])

encrypter = plaintext()({
peerId: localPeer,
logger: defaultLogger()
})
encrypterRemote = plaintext()({
peerId: remotePeer,
logger: defaultLogger()
})
})
Expand All @@ -46,8 +52,8 @@ describe('plaintext', () => {
})

await Promise.all([
encrypter.secureInbound(remotePeer, inbound),
encrypter.secureOutbound(localPeer, outbound, {
encrypter.secureInbound(inbound),
encrypterRemote.secureOutbound(outbound, {
remotePeer: wrongPeer
})
]).then(() => expect.fail('should have failed'), (err) => {
Expand All @@ -60,6 +66,11 @@ describe('plaintext', () => {
const peer = await createRSAPeerId()
remotePeer = peerIdFromBytes(peer.toBytes())

encrypter = plaintext()({
peerId: remotePeer,
logger: defaultLogger()
})

const { inbound, outbound } = mockMultiaddrConnPair({
remotePeer,
addrs: [
Expand All @@ -69,8 +80,8 @@ describe('plaintext', () => {
})

await expect(Promise.all([
encrypter.secureInbound(localPeer, inbound),
encrypter.secureOutbound(remotePeer, outbound, {
encrypter.secureInbound(inbound),
encrypterRemote.secureOutbound(outbound, {
remotePeer: localPeer
})
]))
Expand Down
3 changes: 2 additions & 1 deletion packages/connection-encrypter-tls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
*/

import { TLS } from './tls.js'
import type { ComponentLogger, ConnectionEncrypter } from '@libp2p/interface'
import type { ComponentLogger, ConnectionEncrypter, PeerId } from '@libp2p/interface'

export const PROTOCOL = '/tls/1.0.0'

export interface TLSComponents {
peerId: PeerId
logger: ComponentLogger
}

Expand Down
14 changes: 8 additions & 6 deletions packages/connection-encrypter-tls/src/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import type { Uint8ArrayList } from 'uint8arraylist'
export class TLS implements ConnectionEncrypter {
public protocol: string = PROTOCOL
private readonly log: Logger
private readonly peerId: PeerId

constructor (components: TLSComponents) {
this.log = components.logger.forComponent('libp2p:tls')
this.peerId = components.peerId
}

readonly [Symbol.toStringTag] = '@libp2p/tls'
Expand All @@ -41,20 +43,20 @@ export class TLS implements ConnectionEncrypter {
'@libp2p/connection-encryption'
]

async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(localId, conn, true, options)
async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(conn, true, options)
}

async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(localId, conn, false, options)
async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (PeerId, conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
return this._encrypt(conn, false, options)
}

/**
* Encrypt connection
*/
async _encrypt <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
async _encrypt <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
const opts: TLSSocketOptions = {
...await generateCertificate(localId),
...await generateCertificate(this.peerId),
isServer,
// require TLS 1.3 or later
minVersion: 'TLSv1.3',
Expand Down
4 changes: 3 additions & 1 deletion packages/connection-encrypter-tls/test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import suite from '@libp2p/interface-compliance-tests/connection-encryption'
import { defaultLogger } from '@libp2p/logger'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { tls } from '../src/index.js'

describe('tls compliance', () => {
suite({
async setup () {
async setup (opts) {
return tls()({
peerId: opts?.peerId ?? await createEd25519PeerId(),
logger: defaultLogger()
})
},
Expand Down
18 changes: 14 additions & 4 deletions packages/connection-encrypter-tls/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('tls', () => {
])

encrypter = tls()({
peerId: await createEd25519PeerId(),
logger: defaultLogger()
})
})
Expand All @@ -46,8 +47,10 @@ describe('tls', () => {
})

await Promise.all([
encrypter.secureInbound(remotePeer, inbound),
encrypter.secureOutbound(localPeer, outbound, {
encrypter.secureInbound(inbound, {
remotePeer
}),
encrypter.secureOutbound(outbound, {
remotePeer: wrongPeer
})
]).then(() => expect.fail('should have failed'), (err) => {
Expand All @@ -60,6 +63,11 @@ describe('tls', () => {
const peer = await createRSAPeerId()
remotePeer = peerIdFromBytes(peer.toBytes())

encrypter = tls()({
peerId: remotePeer,
logger: defaultLogger()
})

const { inbound, outbound } = mockMultiaddrConnPair({
remotePeer,
addrs: [
Expand All @@ -69,8 +77,10 @@ describe('tls', () => {
})

await expect(Promise.all([
encrypter.secureInbound(localPeer, inbound),
encrypter.secureOutbound(remotePeer, outbound, {
encrypter.secureInbound(inbound, {
remotePeer
}),
encrypter.secureOutbound(outbound, {
remotePeer: localPeer
})
]))
Expand Down
Loading

0 comments on commit 40bc784

Please sign in to comment.