-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make local peer id optional (#440)
`[email protected]` will remove the `localPeer` argument from the `secureInbound` and `secureOutbound` methods of the `ConnectionEncrypter` interface. Unfortunately the `js-libp2p` monorepo has a dependency on this module so the change cannot be released until this module is compatible... with the unreleased change. This PR tests the first argument to `secureInbound` and `secureOutbound` to ensure that it is actually a `PeerId`. If not it shuffles all the arguments along by one place. This PR can be reverted and the first argument removed once `[email protected]` is released.
- Loading branch information
1 parent
0a349b6
commit 5f92b50
Showing
5 changed files
with
101 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { unmarshalPrivateKey } from '@libp2p/crypto/keys' | ||
import { type MultiaddrConnection, type SecuredConnection, type PeerId, CodeError, type PrivateKey, serviceCapabilities } from '@libp2p/interface' | ||
import { type MultiaddrConnection, type SecuredConnection, type PeerId, CodeError, type PrivateKey, serviceCapabilities, isPeerId } from '@libp2p/interface' | ||
import { peerIdFromKeys } from '@libp2p/peer-id' | ||
import { decode } from 'it-length-prefixed' | ||
import { lpStream, type LengthPrefixedStream } from 'it-length-prefixed-stream' | ||
|
@@ -72,7 +72,11 @@ export class Noise implements INoiseConnection { | |
* @param connection - streaming iterable duplex that will be encrypted | ||
* @param remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer. | ||
*/ | ||
public async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, NoiseExtensions>> { | ||
public async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, NoiseExtensions>> | ||
public async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, NoiseExtensions>> | ||
public async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (...args: any[]): Promise<SecuredConnection<Stream, NoiseExtensions>> { | ||
const { localPeer, connection, remotePeer } = this.parseArgs<Stream>(args) | ||
|
||
const wrappedConnection = lpStream( | ||
connection, | ||
{ | ||
|
@@ -113,7 +117,11 @@ export class Noise implements INoiseConnection { | |
* @param connection - streaming iterable duplex that will be encrypted. | ||
* @param remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades. | ||
*/ | ||
public async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, NoiseExtensions>> { | ||
public async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, NoiseExtensions>> | ||
public async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, NoiseExtensions>> | ||
public async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (...args: any[]): Promise<SecuredConnection<Stream, NoiseExtensions>> { | ||
const { localPeer, connection, remotePeer } = this.parseArgs<Stream>(args) | ||
|
||
const wrappedConnection = lpStream( | ||
connection, | ||
{ | ||
|
@@ -226,4 +234,30 @@ export class Noise implements INoiseConnection { | |
|
||
return user | ||
} | ||
|
||
/** | ||
* Detect call signature in `[email protected]` or `[email protected]` style. | ||
* | ||
* TODO: remove this after `[email protected]` is released and only support the | ||
* newer style | ||
*/ | ||
private parseArgs <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (args: any[]): { localPeer: PeerId, connection: Stream, remotePeer?: PeerId } { | ||
// if the first argument is a peer id, we're using the [email protected] style | ||
if (isPeerId(args[0])) { | ||
return { | ||
localPeer: args[0], | ||
connection: args[1], | ||
remotePeer: args[2] | ||
} | ||
} else { | ||
// handle upcoming changes in [email protected] where the first argument is the | ||
// connection and the second is optionally the remote peer | ||
// @see https://github.com/libp2p/js-libp2p/pull/2304 | ||
return { | ||
localPeer: this.components.peerId, | ||
connection: args[0], | ||
remotePeer: args[1] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters