1
1
import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'
2
- import { serviceCapabilities } from '@libp2p/interface'
2
+ import { InvalidCryptoExchangeError , serviceCapabilities } from '@libp2p/interface'
3
3
import { peerIdFromPublicKey } from '@libp2p/peer-id'
4
4
import { decode } from 'it-length-prefixed'
5
5
import { lpStream , type LengthPrefixedStream } from 'it-length-prefixed-stream'
@@ -14,18 +14,21 @@ import { type MetricsRegistry, registerMetrics } from './metrics.js'
14
14
import { performHandshakeInitiator , performHandshakeResponder } from './performHandshake.js'
15
15
import { decryptStream , encryptStream } from './streaming.js'
16
16
import type { NoiseComponents } from './index.js'
17
- import type { NoiseExtensions } from './proto/payload.js'
18
17
import 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'
20
19
import type { Duplex } from 'it-stream-types'
21
20
import type { Uint8ArrayList } from 'uint8arraylist'
22
21
22
+ export interface NoiseExtensions {
23
+ webtransportCerthashes : Uint8Array [ ]
24
+ }
25
+
23
26
export interface NoiseInit {
24
27
/**
25
28
* x25519 private key, reuse for faster handshakes
26
29
*/
27
30
staticNoiseKey ?: Uint8Array
28
- extensions ?: NoiseExtensions
31
+ extensions ?: Partial < NoiseExtensions >
29
32
crypto ?: ICryptoInterface
30
33
prologueBytes ?: Uint8Array
31
34
}
@@ -47,7 +50,10 @@ export class Noise implements INoiseConnection {
47
50
this . components = components
48
51
const _crypto = crypto ?? defaultCrypto
49
52
this . crypto = wrapCrypto ( _crypto )
50
- this . extensions = extensions
53
+ this . extensions = {
54
+ webtransportCerthashes : [ ] ,
55
+ ...extensions
56
+ }
51
57
this . metrics = metrics ? registerMetrics ( metrics ) : undefined
52
58
53
59
if ( staticNoiseKey ) {
@@ -100,7 +106,30 @@ export class Noise implements INoiseConnection {
100
106
return {
101
107
conn : connection ,
102
108
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' )
104
133
}
105
134
}
106
135
@@ -138,7 +167,8 @@ export class Noise implements INoiseConnection {
138
167
return {
139
168
conn : connection ,
140
169
remoteExtensions : handshake . payload . extensions ,
141
- remotePeer : peerIdFromPublicKey ( publicKey )
170
+ remotePeer : peerIdFromPublicKey ( publicKey ) ,
171
+ streamMuxer : this . getStreamMuxer ( handshake . payload . extensions ?. streamMuxers )
142
172
}
143
173
}
144
174
@@ -162,7 +192,11 @@ export class Noise implements INoiseConnection {
162
192
crypto : this . crypto ,
163
193
prologue : this . prologue ,
164
194
s : this . staticKey ,
165
- extensions : this . extensions
195
+ extensions : {
196
+ streamMuxers : [ ...this . components . upgrader . getStreamMuxers ( ) . keys ( ) ] ,
197
+ webtransportCerthashes : [ ] ,
198
+ ...this . extensions
199
+ }
166
200
} , options )
167
201
this . metrics ?. xxHandshakeSuccesses . increment ( )
168
202
} catch ( e : unknown ) {
@@ -192,7 +226,11 @@ export class Noise implements INoiseConnection {
192
226
crypto : this . crypto ,
193
227
prologue : this . prologue ,
194
228
s : this . staticKey ,
195
- extensions : this . extensions
229
+ extensions : {
230
+ streamMuxers : [ ...this . components . upgrader . getStreamMuxers ( ) . keys ( ) ] ,
231
+ webtransportCerthashes : [ ] ,
232
+ ...this . extensions
233
+ }
196
234
} , options )
197
235
this . metrics ?. xxHandshakeSuccesses . increment ( )
198
236
} catch ( e : unknown ) {
0 commit comments