Skip to content

Commit

Permalink
Merge branch 'release-v2.0' into fix/remove-private-key-from-peer-id
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Aug 15, 2024
2 parents d8777a8 + 5b046c0 commit 939c741
Show file tree
Hide file tree
Showing 259 changed files with 3,080 additions and 2,555 deletions.
3 changes: 1 addition & 2 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ const node = await createLibp2p({
noise()
],
connectionManager: {
maxConnections: Infinity,
minConnections: 0
maxConnections: Infinity
}
})
```
Expand Down
6 changes: 0 additions & 6 deletions doc/LIMITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ const node = await createLibp2p({
*/
maxConnections: 100,

/**
* If the number of open connections goes below this number, the node
* will try to connect to randomly selected peers from the peer store
*/
minConnections: 50,

/**
* How many connections can be open but not yet upgraded
*/
Expand Down
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"
}
}
3 changes: 0 additions & 3 deletions interop/test/fixtures/get-libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ const IP = process.env.ip ?? '0.0.0.0'
export async function getLibp2p (): Promise<Libp2p<{ ping: PingService }>> {
const options: Libp2pOptions<{ ping: PingService, identify: Identify }> = {
start: true,
connectionManager: {
minConnections: 0
},
connectionGater: {
denyDialMultiaddr: async () => false
},
Expand Down
5 changes: 3 additions & 2 deletions packages/connection-encrypter-plaintext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"build": "aegir build",
"test": "aegir test",
"clean": "aegir clean",
"generate": "protons ./src/pb/index.proto",
"generate": "protons ./src/pb/proto.proto",
"lint": "aegir lint",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
Expand All @@ -57,7 +57,8 @@
"it-protobuf-stream": "^1.1.3",
"it-stream-types": "^2.0.1",
"protons-runtime": "^5.4.0",
"uint8arraylist": "^2.4.8"
"uint8arraylist": "^2.4.8",
"uint8arrays": "^5.1.0"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^5.4.11",
Expand Down
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,6 +31,7 @@ import type { Uint8ArrayList } from 'uint8arraylist'
const PROTOCOL = '/plaintext/2.0.0'

export interface PlaintextComponents {
peerId: PeerId
logger: ComponentLogger
}

Expand All @@ -44,10 +45,12 @@ export interface PlaintextInit {

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

constructor (components: PlaintextComponents, init: PlaintextInit = {}) {
this.peerId = components.peerId
this.log = components.logger.forComponent('libp2p:plaintext')
this.timeout = init.timeout ?? 1000
}
Expand All @@ -58,12 +61,12 @@ class Plaintext implements ConnectionEncrypter {
'@libp2p/connection-encryption'
]

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

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

/**
Expand Down
40 changes: 24 additions & 16 deletions packages/connection-encrypter-plaintext/src/pb/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
/* eslint-disable @typescript-eslint/no-empty-interface */

import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, enumeration, message } from 'protons-runtime'
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
import type { Uint8ArrayList } from 'uint8arraylist'

export interface Exchange {
Expand Down Expand Up @@ -36,7 +36,7 @@ export namespace Exchange {
if (opts.lengthDelimited !== false) {
w.ldelim()
}
}, (reader, length) => {
}, (reader, length, opts = {}) => {
const obj: any = {}

const end = length == null ? reader.len : reader.pos + length
Expand All @@ -45,15 +45,20 @@ export namespace Exchange {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
case 1: {
obj.id = reader.bytes()
break
case 2:
obj.pubkey = PublicKey.codec().decode(reader, reader.uint32())
}
case 2: {
obj.pubkey = PublicKey.codec().decode(reader, reader.uint32(), {
limits: opts.limits?.pubkey
})
break
default:
}
default: {
reader.skipType(tag & 7)
break
}
}
}

Expand All @@ -68,8 +73,8 @@ export namespace Exchange {
return encodeMessage(obj, Exchange.codec())
}

export const decode = (buf: Uint8Array | Uint8ArrayList): Exchange => {
return decodeMessage(buf, Exchange.codec())
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<Exchange>): Exchange => {
return decodeMessage(buf, Exchange.codec(), opts)
}
}

Expand Down Expand Up @@ -120,10 +125,10 @@ export namespace PublicKey {
if (opts.lengthDelimited !== false) {
w.ldelim()
}
}, (reader, length) => {
}, (reader, length, opts = {}) => {
const obj: any = {
Type: KeyType.RSA,
Data: new Uint8Array(0)
Data: uint8ArrayAlloc(0)
}

const end = length == null ? reader.len : reader.pos + length
Expand All @@ -132,15 +137,18 @@ export namespace PublicKey {
const tag = reader.uint32()

switch (tag >>> 3) {
case 1:
case 1: {
obj.Type = KeyType.codec().decode(reader)
break
case 2:
}
case 2: {
obj.Data = reader.bytes()
break
default:
}
default: {
reader.skipType(tag & 7)
break
}
}
}

Expand All @@ -155,7 +163,7 @@ export namespace PublicKey {
return encodeMessage(obj, PublicKey.codec())
}

export const decode = (buf: Uint8Array | Uint8ArrayList): PublicKey => {
return decodeMessage(buf, PublicKey.codec())
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<PublicKey>): PublicKey => {
return decodeMessage(buf, PublicKey.codec(), opts)
}
}
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
27 changes: 17 additions & 10 deletions packages/connection-encrypter-plaintext/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/* eslint-env mocha */

import {
InvalidCryptoExchangeError,
UnexpectedPeerError
} from '@libp2p/interface'
import { mockMultiaddrConnPair } from '@libp2p/interface-compliance-tests/mocks'
import { defaultLogger } from '@libp2p/logger'
import { peerIdFromBytes } from '@libp2p/peer-id'
Expand All @@ -19,6 +15,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 +25,11 @@ describe('plaintext', () => {
])

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

await Promise.all([
encrypter.secureInbound(remotePeer, inbound),
encrypter.secureOutbound(localPeer, outbound, wrongPeer)
encrypterRemote.secureInbound(inbound),
encrypter.secureOutbound(outbound, wrongPeer)
]).then(() => expect.fail('should have failed'), (err) => {
expect(err).to.exist()
expect(err).to.have.property('code', UnexpectedPeerError.code)
expect(err).to.have.property('name', 'UnexpectedPeerError')
})
})

it('should fail if the peer does not provide its public key', async () => {
const peer = await createRSAPeerId()
remotePeer = peerIdFromBytes(peer.toBytes())

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

const { inbound, outbound } = mockMultiaddrConnPair({
remotePeer,
addrs: [
Expand All @@ -67,9 +74,9 @@ describe('plaintext', () => {
})

await expect(Promise.all([
encrypter.secureInbound(localPeer, inbound),
encrypter.secureOutbound(remotePeer, outbound, localPeer)
encrypter.secureInbound(inbound),
encrypterRemote.secureOutbound(outbound, localPeer)
]))
.to.eventually.be.rejected.with.property('code', InvalidCryptoExchangeError.code)
.to.eventually.be.rejected.with.property('name', 'InvalidCryptoExchangeError')
})
})
19 changes: 19 additions & 0 deletions packages/connection-encrypter-tls/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* The handshake timed out
*/
export class HandshakeTimeoutError extends Error {
constructor (message = 'Handshake timeout') {
super(message)
this.name = 'HandshakeTimeoutError'
}
}

/**
* The certificate was invalid
*/
export class InvalidCertificateError extends Error {
constructor (message = 'Invalid certificate') {
super(message)
this.name = 'InvalidCertificateError'
}
}
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
8 changes: 4 additions & 4 deletions packages/connection-encrypter-tls/src/pb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
/* eslint-disable @typescript-eslint/no-empty-interface */

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

export enum KeyType {
Expand Down Expand Up @@ -54,7 +54,7 @@ export namespace PublicKey {
if (opts.lengthDelimited !== false) {
w.ldelim()
}
}, (reader, length) => {
}, (reader, length, opts = {}) => {
const obj: any = {}

const end = length == null ? reader.len : reader.pos + length
Expand Down Expand Up @@ -89,7 +89,7 @@ export namespace PublicKey {
return encodeMessage(obj, PublicKey.codec())
}

export const decode = (buf: Uint8Array | Uint8ArrayList): PublicKey => {
return decodeMessage(buf, PublicKey.codec())
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<PublicKey>): PublicKey => {
return decodeMessage(buf, PublicKey.codec(), opts)
}
}
Loading

0 comments on commit 939c741

Please sign in to comment.