-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathlogger.ts
More file actions
51 lines (43 loc) · 1.69 KB
/
logger.ts
File metadata and controls
51 lines (43 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Logger, logger } from '@libp2p/logger'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { NoiseSession } from './@types/handshake.js'
import type { KeyPair } from './@types/libp2p.js'
import { DUMP_SESSION_KEYS } from './constants.js'
const log = logger('libp2p:noise')
export { log as logger }
let keyLogger: Logger
if (DUMP_SESSION_KEYS) {
keyLogger = log
} else {
keyLogger = Object.assign(() => { /* do nothing */ }, {
enabled: false,
trace: () => {},
error: () => {}
})
}
export function logLocalStaticKeys (s: KeyPair): void {
keyLogger(`LOCAL_STATIC_PUBLIC_KEY ${uint8ArrayToString(s.publicKey, 'hex')}`)
keyLogger(`LOCAL_STATIC_PRIVATE_KEY ${uint8ArrayToString(s.privateKey, 'hex')}`)
}
export function logLocalEphemeralKeys (e: KeyPair|undefined): void {
if (e) {
keyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${uint8ArrayToString(e.publicKey, 'hex')}`)
keyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${uint8ArrayToString(e.privateKey, 'hex')}`)
} else {
keyLogger('Missing local ephemeral keys.')
}
}
export function logRemoteStaticKey (rs: Uint8Array): void {
keyLogger(`REMOTE_STATIC_PUBLIC_KEY ${uint8ArrayToString(rs, 'hex')}`)
}
export function logRemoteEphemeralKey (re: Uint8Array): void {
keyLogger(`REMOTE_EPHEMERAL_PUBLIC_KEY ${uint8ArrayToString(re, 'hex')}`)
}
export function logCipherState (session: NoiseSession): void {
if (session.cs1 && session.cs2) {
keyLogger(`CIPHER_STATE_1 ${session.cs1.n.getUint64()} ${uint8ArrayToString(session.cs1.k, 'hex')}`)
keyLogger(`CIPHER_STATE_2 ${session.cs2.n.getUint64()} ${uint8ArrayToString(session.cs2.k, 'hex')}`)
} else {
keyLogger('Missing cipher state.')
}
}