@@ -26,7 +26,7 @@ import { peerIdFromBytes } from '@libp2p/peer-id'
2626import { createFromPubKey } from '@libp2p/peer-id-factory'
2727import { pbStream } from 'it-protobuf-stream'
2828import { Exchange , KeyType , PublicKey } from './pb/proto.js'
29- import type { ComponentLogger , Logger , MultiaddrConnection , ConnectionEncrypter , SecuredConnection , PeerId , PublicKey as PubKey } from '@libp2p/interface'
29+ import type { ComponentLogger , Logger , MultiaddrConnection , ConnectionEncrypter , SecuredConnection , PeerId , PublicKey as PubKey , SecureConnectionOptions } from '@libp2p/interface'
3030import type { Duplex } from 'it-stream-types'
3131import type { Uint8ArrayList } from 'uint8arraylist'
3232
@@ -37,24 +37,14 @@ export interface PlaintextComponents {
3737 logger : ComponentLogger
3838}
3939
40- export interface PlaintextInit {
41- /**
42- * The peer id exchange must complete within this many milliseconds
43- * (default: 1000)
44- */
45- timeout ?: number
46- }
47-
4840class Plaintext implements ConnectionEncrypter {
4941 public protocol : string = PROTOCOL
5042 private readonly peerId : PeerId
5143 private readonly log : Logger
52- private readonly timeout : number
5344
54- constructor ( components : PlaintextComponents , init : PlaintextInit = { } ) {
45+ constructor ( components : PlaintextComponents ) {
5546 this . peerId = components . peerId
5647 this . log = components . logger . forComponent ( 'libp2p:plaintext' )
57- this . timeout = init . timeout ?? 1000
5848 }
5949
6050 readonly [ Symbol . toStringTag ] = '@libp2p/plaintext'
@@ -63,19 +53,18 @@ class Plaintext implements ConnectionEncrypter {
6353 '@libp2p/connection-encryption'
6454 ]
6555
66- async secureInbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( conn : Stream , remoteId ?: PeerId ) : Promise < SecuredConnection < Stream > > {
67- return this . _encrypt ( this . peerId , conn , remoteId )
56+ async secureInbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( conn : Stream , options ?: SecureConnectionOptions ) : Promise < SecuredConnection < Stream > > {
57+ return this . _encrypt ( this . peerId , conn , options )
6858 }
6959
70- async secureOutbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( conn : Stream , remoteId ?: PeerId ) : Promise < SecuredConnection < Stream > > {
71- return this . _encrypt ( this . peerId , conn , remoteId )
60+ async secureOutbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( conn : Stream , options ?: SecureConnectionOptions ) : Promise < SecuredConnection < Stream > > {
61+ return this . _encrypt ( this . peerId , conn , options )
7262 }
7363
7464 /**
7565 * Encrypt connection
7666 */
77- async _encrypt < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( localId : PeerId , conn : Stream , remoteId ?: PeerId ) : Promise < SecuredConnection < Stream > > {
78- const signal = AbortSignal . timeout ( this . timeout )
67+ async _encrypt < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( localId : PeerId , conn : Stream , options ?: SecureConnectionOptions ) : Promise < SecuredConnection < Stream > > {
7968 const pb = pbStream ( conn ) . pb ( Exchange )
8069
8170 let type = KeyType . RSA
@@ -86,7 +75,7 @@ class Plaintext implements ConnectionEncrypter {
8675 type = KeyType . Secp256k1
8776 }
8877
89- this . log ( 'write pubkey exchange to peer %p' , remoteId )
78+ this . log ( 'write pubkey exchange to peer %p' , options ?. remotePeer )
9079
9180 const [
9281 , response
@@ -98,13 +87,9 @@ class Plaintext implements ConnectionEncrypter {
9887 Type : type ,
9988 Data : localId . publicKey == null ? new Uint8Array ( 0 ) : ( PublicKey . decode ( localId . publicKey ) . Data ?? new Uint8Array ( 0 ) )
10089 }
101- } , {
102- signal
103- } ) ,
90+ } , options ) ,
10491 // Get the Exchange message
105- pb . read ( {
106- signal
107- } )
92+ pb . read ( options )
10893 ] )
10994
11095 let peerId
@@ -143,7 +128,7 @@ class Plaintext implements ConnectionEncrypter {
143128 throw new InvalidCryptoExchangeError ( 'Remote did not provide its public key' )
144129 }
145130
146- if ( remoteId != null && ! peerId . equals ( remoteId ) ) {
131+ if ( options ?. remotePeer != null && ! peerId . equals ( options ?. remotePeer ) ) {
147132 throw new UnexpectedPeerError ( )
148133 }
149134
@@ -156,6 +141,6 @@ class Plaintext implements ConnectionEncrypter {
156141 }
157142}
158143
159- export function plaintext ( init ?: PlaintextInit ) : ( components : PlaintextComponents ) => ConnectionEncrypter {
160- return ( components ) => new Plaintext ( components , init )
144+ export function plaintext ( ) : ( components : PlaintextComponents ) => ConnectionEncrypter {
145+ return ( components ) => new Plaintext ( components )
161146}
0 commit comments