@@ -16,6 +16,8 @@ import { uint16BEDecode, uint16BEEncode } from './encoder.js'
16
16
import { XXHandshake } from './handshake-xx.js'
17
17
import { getPayload } from './utils.js'
18
18
import type { NoiseExtensions } from './proto/payload.js'
19
+ import type { Metrics } from '@libp2p/interface-metrics'
20
+ import { MetricsRegistry , registerMetrics } from './metrics.js'
19
21
20
22
interface HandshakeParams {
21
23
connection : ProtobufStream
@@ -32,6 +34,7 @@ export interface NoiseInit {
32
34
extensions ?: NoiseExtensions
33
35
crypto ?: ICryptoInterface
34
36
prologueBytes ?: Uint8Array
37
+ metrics ?: Metrics
35
38
}
36
39
37
40
export class Noise implements INoiseConnection {
@@ -41,12 +44,14 @@ export class Noise implements INoiseConnection {
41
44
private readonly prologue : Uint8Array
42
45
private readonly staticKeys : KeyPair
43
46
private readonly extensions ?: NoiseExtensions
47
+ private readonly metrics ?: MetricsRegistry
44
48
45
49
constructor ( init : NoiseInit = { } ) {
46
- const { staticNoiseKey, extensions, crypto, prologueBytes } = init
50
+ const { staticNoiseKey, extensions, crypto, prologueBytes, metrics } = init
47
51
48
52
this . crypto = crypto ?? stablelib
49
53
this . extensions = extensions
54
+ this . metrics = metrics ? registerMetrics ( metrics ) : undefined
50
55
51
56
if ( staticNoiseKey ) {
52
57
// accepts x25519 private key of length 32
@@ -153,7 +158,9 @@ export class Noise implements INoiseConnection {
153
158
await handshake . propose ( )
154
159
await handshake . exchange ( )
155
160
await handshake . finish ( )
161
+ this . metrics ?. xxHandshakeSuccesses . increment ( )
156
162
} catch ( e : unknown ) {
163
+ this . metrics ?. xxHandshakeErrors . increment ( )
157
164
if ( e instanceof Error ) {
158
165
e . message = `Error occurred during XX handshake: ${ e . message } `
159
166
throw e
@@ -173,10 +180,10 @@ export class Noise implements INoiseConnection {
173
180
174
181
await pipe (
175
182
secure , // write to wrapper
176
- encryptStream ( handshake ) , // encrypt data + prefix with message length
183
+ encryptStream ( handshake , this . metrics ) , // encrypt data + prefix with message length
177
184
network , // send to the remote peer
178
185
decode ( { lengthDecoder : uint16BEDecode } ) , // read message length prefix
179
- decryptStream ( handshake ) , // decrypt the incoming data
186
+ decryptStream ( handshake , this . metrics ) , // decrypt the incoming data
180
187
secure // pipe to the wrapper
181
188
)
182
189
0 commit comments