11import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'
2- import { serviceCapabilities } from '@libp2p/interface'
2+ import { InvalidCryptoExchangeError , serviceCapabilities } from '@libp2p/interface'
33import { peerIdFromPublicKey } from '@libp2p/peer-id'
44import { decode } from 'it-length-prefixed'
55import { lpStream , type LengthPrefixedStream } from 'it-length-prefixed-stream'
@@ -14,18 +14,21 @@ import { type MetricsRegistry, registerMetrics } from './metrics.js'
1414import { performHandshakeInitiator , performHandshakeResponder } from './performHandshake.js'
1515import { decryptStream , encryptStream } from './streaming.js'
1616import type { NoiseComponents } from './index.js'
17- import type { NoiseExtensions } from './proto/payload.js'
1817import type { HandshakeResult , ICrypto , INoiseConnection , KeyPair } from './types.js'
19- import type { MultiaddrConnection , SecuredConnection , PeerId , PrivateKey , PublicKey , AbortOptions } from '@libp2p/interface'
18+ import type { MultiaddrConnection , SecuredConnection , PeerId , PrivateKey , PublicKey , AbortOptions , StreamMuxerFactory } from '@libp2p/interface'
2019import type { Duplex } from 'it-stream-types'
2120import type { Uint8ArrayList } from 'uint8arraylist'
2221
22+ export interface NoiseExtensions {
23+ webtransportCerthashes : Uint8Array [ ]
24+ }
25+
2326export interface NoiseInit {
2427 /**
2528 * x25519 private key, reuse for faster handshakes
2629 */
2730 staticNoiseKey ?: Uint8Array
28- extensions ?: NoiseExtensions
31+ extensions ?: Partial < NoiseExtensions >
2932 crypto ?: ICryptoInterface
3033 prologueBytes ?: Uint8Array
3134}
@@ -47,7 +50,10 @@ export class Noise implements INoiseConnection {
4750 this . components = components
4851 const _crypto = crypto ?? defaultCrypto
4952 this . crypto = wrapCrypto ( _crypto )
50- this . extensions = extensions
53+ this . extensions = {
54+ webtransportCerthashes : [ ] ,
55+ ...extensions
56+ }
5157 this . metrics = metrics ? registerMetrics ( metrics ) : undefined
5258
5359 if ( staticNoiseKey ) {
@@ -100,7 +106,30 @@ export class Noise implements INoiseConnection {
100106 return {
101107 conn : connection ,
102108 remoteExtensions : handshake . payload . extensions ,
103- remotePeer : peerIdFromPublicKey ( publicKey )
109+ remotePeer : peerIdFromPublicKey ( publicKey ) ,
110+ streamMuxer : this . getStreamMuxer ( handshake . payload . extensions ?. streamMuxers )
111+ }
112+ }
113+
114+ private getStreamMuxer ( protocols ?: string [ ] ) : StreamMuxerFactory | undefined {
115+ if ( protocols == null ) {
116+ return
117+ }
118+
119+ const streamMuxers = this . components . upgrader . getStreamMuxers ( )
120+
121+ if ( streamMuxers != null ) {
122+ for ( const protocol of protocols ) {
123+ const streamMuxer = streamMuxers . get ( protocol )
124+
125+ if ( streamMuxer != null ) {
126+ return streamMuxer
127+ }
128+ }
129+ }
130+
131+ if ( protocols . length ) {
132+ throw new InvalidCryptoExchangeError ( 'Early muxer negotiation was requested but the initiator and responder had no common muxers' )
104133 }
105134 }
106135
@@ -138,7 +167,8 @@ export class Noise implements INoiseConnection {
138167 return {
139168 conn : connection ,
140169 remoteExtensions : handshake . payload . extensions ,
141- remotePeer : peerIdFromPublicKey ( publicKey )
170+ remotePeer : peerIdFromPublicKey ( publicKey ) ,
171+ streamMuxer : this . getStreamMuxer ( handshake . payload . extensions ?. streamMuxers )
142172 }
143173 }
144174
@@ -162,7 +192,11 @@ export class Noise implements INoiseConnection {
162192 crypto : this . crypto ,
163193 prologue : this . prologue ,
164194 s : this . staticKey ,
165- extensions : this . extensions
195+ extensions : {
196+ streamMuxers : [ ...this . components . upgrader . getStreamMuxers ( ) . keys ( ) ] ,
197+ webtransportCerthashes : [ ] ,
198+ ...this . extensions
199+ }
166200 } , options )
167201 this . metrics ?. xxHandshakeSuccesses . increment ( )
168202 } catch ( e : unknown ) {
@@ -192,7 +226,11 @@ export class Noise implements INoiseConnection {
192226 crypto : this . crypto ,
193227 prologue : this . prologue ,
194228 s : this . staticKey ,
195- extensions : this . extensions
229+ extensions : {
230+ streamMuxers : [ ...this . components . upgrader . getStreamMuxers ( ) . keys ( ) ] ,
231+ webtransportCerthashes : [ ] ,
232+ ...this . extensions
233+ }
196234 } , options )
197235 this . metrics ?. xxHandshakeSuccesses . increment ( )
198236 } catch ( e : unknown ) {
0 commit comments