11import { unmarshalPrivateKey } from '@libp2p/crypto/keys'
2- import { type MultiaddrConnection , type SecuredConnection , type PeerId , CodeError , type PrivateKey , serviceCapabilities , isPeerId } from '@libp2p/interface'
2+ import { type MultiaddrConnection , type SecuredConnection , type PeerId , CodeError , type PrivateKey , serviceCapabilities , isPeerId , type AbortOptions } from '@libp2p/interface'
33import { peerIdFromKeys } from '@libp2p/peer-id'
44import { decode } from 'it-length-prefixed'
55import { lpStream , type LengthPrefixedStream } from 'it-length-prefixed-stream'
@@ -72,10 +72,10 @@ export class Noise implements INoiseConnection {
7272 * @param connection - streaming iterable duplex that will be encrypted
7373 * @param remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
7474 */
75- public async secureOutbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( connection : Stream , remotePeer ?: PeerId ) : Promise < SecuredConnection < Stream , NoiseExtensions > >
75+ public async secureOutbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( connection : Stream , options ?: { remotePeer ?: PeerId , signal ?: AbortSignal } ) : Promise < SecuredConnection < Stream , NoiseExtensions > >
7676 public async secureOutbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( localPeer : PeerId , connection : Stream , remotePeer ?: PeerId ) : Promise < SecuredConnection < Stream , NoiseExtensions > >
7777 public async secureOutbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( ...args : any [ ] ) : Promise < SecuredConnection < Stream , NoiseExtensions > > {
78- const { localPeer, connection, remotePeer } = this . parseArgs < Stream > ( args )
78+ const { localPeer, connection, remotePeer, signal } = this . parseArgs < Stream > ( args )
7979
8080 const wrappedConnection = lpStream (
8181 connection ,
@@ -96,7 +96,9 @@ export class Noise implements INoiseConnection {
9696 const handshake = await this . performHandshakeInitiator (
9797 wrappedConnection ,
9898 privateKey ,
99- remoteIdentityKey
99+ remoteIdentityKey , {
100+ signal
101+ }
100102 )
101103 const conn = await this . createSecureConnection ( wrappedConnection , handshake )
102104
@@ -117,10 +119,10 @@ export class Noise implements INoiseConnection {
117119 * @param connection - streaming iterable duplex that will be encrypted.
118120 * @param remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades.
119121 */
120- public async secureInbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( connection : Stream , remotePeer ?: PeerId ) : Promise < SecuredConnection < Stream , NoiseExtensions > >
122+ public async secureInbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( connection : Stream , options ?: { remotePeer ?: PeerId , signal ?: AbortSignal } ) : Promise < SecuredConnection < Stream , NoiseExtensions > >
121123 public async secureInbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( localPeer : PeerId , connection : Stream , remotePeer ?: PeerId ) : Promise < SecuredConnection < Stream , NoiseExtensions > >
122124 public async secureInbound < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( ...args : any [ ] ) : Promise < SecuredConnection < Stream , NoiseExtensions > > {
123- const { localPeer, connection, remotePeer } = this . parseArgs < Stream > ( args )
125+ const { localPeer, connection, remotePeer, signal } = this . parseArgs < Stream > ( args )
124126
125127 const wrappedConnection = lpStream (
126128 connection ,
@@ -141,7 +143,9 @@ export class Noise implements INoiseConnection {
141143 const handshake = await this . performHandshakeResponder (
142144 wrappedConnection ,
143145 privateKey ,
144- remoteIdentityKey
146+ remoteIdentityKey , {
147+ signal
148+ }
145149 )
146150 const conn = await this . createSecureConnection ( wrappedConnection , handshake )
147151
@@ -162,7 +166,8 @@ export class Noise implements INoiseConnection {
162166 connection : LengthPrefixedStream ,
163167 // TODO: pass private key in noise constructor via Components
164168 privateKey : PrivateKey ,
165- remoteIdentityKey ?: Uint8Array | Uint8ArrayList
169+ remoteIdentityKey ?: Uint8Array | Uint8ArrayList ,
170+ options ?: AbortOptions
166171 ) : Promise < HandshakeResult > {
167172 let result : HandshakeResult
168173 try {
@@ -175,7 +180,7 @@ export class Noise implements INoiseConnection {
175180 prologue : this . prologue ,
176181 s : this . staticKey ,
177182 extensions : this . extensions
178- } )
183+ } , options )
179184 this . metrics ?. xxHandshakeSuccesses . increment ( )
180185 } catch ( e : unknown ) {
181186 this . metrics ?. xxHandshakeErrors . increment ( )
@@ -192,7 +197,8 @@ export class Noise implements INoiseConnection {
192197 connection : LengthPrefixedStream ,
193198 // TODO: pass private key in noise constructor via Components
194199 privateKey : PrivateKey ,
195- remoteIdentityKey ?: Uint8Array | Uint8ArrayList
200+ remoteIdentityKey ?: Uint8Array | Uint8ArrayList ,
201+ options ?: AbortOptions
196202 ) : Promise < HandshakeResult > {
197203 let result : HandshakeResult
198204 try {
@@ -205,7 +211,7 @@ export class Noise implements INoiseConnection {
205211 prologue : this . prologue ,
206212 s : this . staticKey ,
207213 extensions : this . extensions
208- } )
214+ } , options )
209215 this . metrics ?. xxHandshakeSuccesses . increment ( )
210216 } catch ( e : unknown ) {
211217 this . metrics ?. xxHandshakeErrors . increment ( )
@@ -241,7 +247,7 @@ export class Noise implements INoiseConnection {
241247 * TODO: remove this after `[email protected] ` is released and only support the 242248 * newer style
243249 */
244- private parseArgs < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( args : any [ ] ) : { localPeer : PeerId , connection : Stream , remotePeer ?: PeerId } {
250+ private parseArgs < Stream extends Duplex < AsyncGenerator < Uint8Array | Uint8ArrayList > > = MultiaddrConnection > ( args : any [ ] ) : { localPeer : PeerId , connection : Stream , remotePeer ?: PeerId , signal ?: AbortSignal } {
245251 // if the first argument is a peer id, we're using the [email protected] style 246252 if ( isPeerId ( args [ 0 ] ) ) {
247253 return {
@@ -256,7 +262,8 @@ export class Noise implements INoiseConnection {
256262 return {
257263 localPeer : this . components . peerId ,
258264 connection : args [ 0 ] ,
259- remotePeer : args [ 1 ]
265+ remotePeer : args [ 1 ] ?. remotePeer ,
266+ signal : args [ 1 ] ?. signal
260267 }
261268 }
262269 }
0 commit comments