Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 6aee82a

Browse files
authored
feat: add peer:identify event to libp2p (#395)
This event is useful to eavesdrop on network behaviour and helps to debug situations where a remote peer is sending bad data in it's identify response (for example not including any routable addresses). An event is better than opening a protocol stream and sending more requests as remotes will frequently reset streams if there are too many concurrent identify requests, etc.
1 parent 7fd73ee commit 6aee82a

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

packages/interface-libp2p/src/index.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,63 @@ export interface PeerUpdate {
4444
previous?: Peer
4545
}
4646

47+
/**
48+
* Peer data signed by the remote Peer's public key
49+
*/
50+
export interface SignedPeerRecord {
51+
addresses: Multiaddr[]
52+
seq: bigint
53+
}
54+
55+
/**
56+
* Data returned from a successful identify response
57+
*/
58+
export interface IdentifyResult {
59+
/**
60+
* The remote Peer's PeerId
61+
*/
62+
peerId: PeerId
63+
64+
/**
65+
* The unsigned addresses they are listening on. Note - any multiaddrs present
66+
* in the signed peer record should be preferred to the value here.
67+
*/
68+
listenAddrs: Multiaddr[]
69+
70+
/**
71+
* The protocols the remote peer supports
72+
*/
73+
protocols: string[]
74+
75+
/**
76+
* The remote protocol version
77+
*/
78+
protocolVersion?: string
79+
80+
/**
81+
* The remote agent version
82+
*/
83+
agentVersion?: string
84+
85+
/**
86+
* The public key part of the remote PeerId - this is only useful for older
87+
* RSA-based PeerIds, the more modern Ed25519 and secp256k1 types have the
88+
* public key embedded in them
89+
*/
90+
publicKey?: Uint8Array
91+
92+
/**
93+
* If set this is the address that the remote peer saw the identify request
94+
* originate from
95+
*/
96+
observedAddr?: Multiaddr
97+
98+
/**
99+
* If sent by the remote peer this is the deserialized signed peer record
100+
*/
101+
signedPeerRecord?: SignedPeerRecord
102+
}
103+
47104
/**
48105
* Once you have a libp2p instance, you can listen to several events it emits,
49106
* so that you can be notified of relevant network events.
@@ -72,7 +129,7 @@ export interface Libp2pEvents {
72129
* @example
73130
*
74131
* ```js
75-
* libp2p.connectionManager.addEventListener('peer:connect', (event) => {
132+
* libp2p.addEventListener('peer:connect', (event) => {
76133
* const peerId = event.detail
77134
* // ...
78135
* })
@@ -88,14 +145,30 @@ export interface Libp2pEvents {
88145
* @example
89146
*
90147
* ```js
91-
* libp2p.connectionManager.addEventListener('peer:disconnect', (event) => {
148+
* libp2p.addEventListener('peer:disconnect', (event) => {
92149
* const peerId = event.detail
93150
* // ...
94151
* })
95152
* ```
96153
*/
97154
'peer:disconnect': CustomEvent<PeerId>
98155

156+
/**
157+
* This event is dispatched after a remote peer has successfully responded to the identify
158+
* protocol. Note that for this to be emitted, both peers must have an identify service
159+
* configured.
160+
*
161+
* @example
162+
*
163+
* ```js
164+
* libp2p.addEventListener('peer:identify', (event) => {
165+
* const identifyResult = event.detail
166+
* // ...
167+
* })
168+
* ```
169+
*/
170+
'peer:identify': CustomEvent<IdentifyResult>
171+
99172
/**
100173
* This event is dispatched when the peer store data for a peer has been
101174
* updated - e.g. their multiaddrs, protocols etc have changed.

0 commit comments

Comments
 (0)