@libp2p/identify 3.0.39 → 4.0.0-049bfa0fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/identify-push.d.ts +2 -2
- package/dist/src/identify-push.d.ts.map +1 -1
- package/dist/src/identify-push.js +24 -33
- package/dist/src/identify-push.js.map +1 -1
- package/dist/src/identify.d.ts +2 -2
- package/dist/src/identify.d.ts.map +1 -1
- package/dist/src/identify.js +52 -58
- package/dist/src/identify.js.map +1 -1
- package/dist/src/index.d.ts +0 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils.d.ts +3 -10
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +7 -26
- package/dist/src/utils.js.map +1 -1
- package/package.json +15 -18
- package/src/identify-push.ts +26 -34
- package/src/identify.ts +59 -66
- package/src/index.ts +0 -7
- package/src/utils.ts +10 -31
- package/dist/typedoc-urls.json +0 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/identify",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-049bfa0fa",
|
|
4
4
|
"description": "Implementation of the Identify Protocol",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/protocol-identify#readme",
|
|
@@ -45,30 +45,27 @@
|
|
|
45
45
|
"doc-check": "aegir doc-check"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@libp2p/crypto": "
|
|
49
|
-
"@libp2p/interface": "
|
|
50
|
-
"@libp2p/interface-internal": "
|
|
51
|
-
"@libp2p/peer-id": "
|
|
52
|
-
"@libp2p/peer-record": "
|
|
53
|
-
"@libp2p/utils": "
|
|
54
|
-
"@multiformats/multiaddr": "^
|
|
55
|
-
"@multiformats/multiaddr-matcher": "^
|
|
56
|
-
"it-drain": "^3.0.
|
|
57
|
-
"it-parallel": "^3.0.
|
|
58
|
-
"it-protobuf-stream": "^2.0.2",
|
|
48
|
+
"@libp2p/crypto": "5.1.9-049bfa0fa",
|
|
49
|
+
"@libp2p/interface": "3.0.0-049bfa0fa",
|
|
50
|
+
"@libp2p/interface-internal": "3.0.0-049bfa0fa",
|
|
51
|
+
"@libp2p/peer-id": "6.0.0-049bfa0fa",
|
|
52
|
+
"@libp2p/peer-record": "9.0.0-049bfa0fa",
|
|
53
|
+
"@libp2p/utils": "7.0.0-049bfa0fa",
|
|
54
|
+
"@multiformats/multiaddr": "^13.0.1",
|
|
55
|
+
"@multiformats/multiaddr-matcher": "^3.0.1",
|
|
56
|
+
"it-drain": "^3.0.10",
|
|
57
|
+
"it-parallel": "^3.0.13",
|
|
59
58
|
"main-event": "^1.0.1",
|
|
60
|
-
"protons-runtime": "^5.
|
|
59
|
+
"protons-runtime": "^5.6.0",
|
|
61
60
|
"uint8arraylist": "^2.4.8",
|
|
62
61
|
"uint8arrays": "^5.1.0"
|
|
63
62
|
},
|
|
64
63
|
"devDependencies": {
|
|
65
|
-
"@libp2p/logger": "
|
|
66
|
-
"aegir": "^47.0.
|
|
64
|
+
"@libp2p/logger": "6.0.0-049bfa0fa",
|
|
65
|
+
"aegir": "^47.0.22",
|
|
67
66
|
"delay": "^6.0.0",
|
|
68
67
|
"it-length-prefixed": "^10.0.1",
|
|
69
|
-
"
|
|
70
|
-
"it-pushable": "^3.2.3",
|
|
71
|
-
"protons": "^7.6.1",
|
|
68
|
+
"protons": "^7.7.0",
|
|
72
69
|
"sinon-ts": "^2.0.0"
|
|
73
70
|
},
|
|
74
71
|
"sideEffects": false
|
package/src/identify-push.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { serviceCapabilities } from '@libp2p/interface'
|
|
2
2
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
3
|
-
import { debounce } from '@libp2p/utils
|
|
4
|
-
import {
|
|
3
|
+
import { debounce, pbStream } from '@libp2p/utils'
|
|
4
|
+
import { CODE_P2P } from '@multiformats/multiaddr'
|
|
5
5
|
import drain from 'it-drain'
|
|
6
6
|
import parallel from 'it-parallel'
|
|
7
|
-
import { pbStream } from 'it-protobuf-stream'
|
|
8
7
|
import { setMaxListeners } from 'main-event'
|
|
9
8
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
10
9
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
@@ -16,7 +15,7 @@ import {
|
|
|
16
15
|
import { Identify as IdentifyMessage } from './pb/message.js'
|
|
17
16
|
import { AbstractIdentify, consumeIdentifyMessage, defaultValues } from './utils.js'
|
|
18
17
|
import type { IdentifyPush as IdentifyPushInterface, IdentifyPushComponents, IdentifyPushInit } from './index.js'
|
|
19
|
-
import type { Stream, Startable,
|
|
18
|
+
import type { Stream, Startable, Connection } from '@libp2p/interface'
|
|
20
19
|
import type { ConnectionManager } from '@libp2p/interface-internal'
|
|
21
20
|
|
|
22
21
|
export class IdentifyPush extends AbstractIdentify implements Startable, IdentifyPushInterface {
|
|
@@ -64,22 +63,21 @@ export class IdentifyPush extends AbstractIdentify implements Startable, Identif
|
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
try {
|
|
67
|
-
const listenAddresses = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(
|
|
66
|
+
const listenAddresses = this.components.addressManager.getAddresses().map(ma => ma.decapsulateCode(CODE_P2P))
|
|
68
67
|
const peerRecord = new PeerRecord({
|
|
69
|
-
peerId: this.peerId,
|
|
68
|
+
peerId: this.components.peerId,
|
|
70
69
|
multiaddrs: listenAddresses
|
|
71
70
|
})
|
|
72
|
-
const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.privateKey)
|
|
73
|
-
const supportedProtocols = this.registrar.getProtocols()
|
|
74
|
-
const peer = await this.peerStore.get(this.peerId)
|
|
71
|
+
const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.components.privateKey)
|
|
72
|
+
const supportedProtocols = this.components.registrar.getProtocols()
|
|
73
|
+
const peer = await this.components.peerStore.get(this.components.peerId)
|
|
75
74
|
const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion))
|
|
76
75
|
const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') ?? uint8ArrayFromString(this.host.protocolVersion))
|
|
77
76
|
const self = this
|
|
78
77
|
|
|
79
78
|
async function * pushToConnections (): AsyncGenerator<() => Promise<void>> {
|
|
80
79
|
for (const connection of self.connectionManager.getConnections()) {
|
|
81
|
-
const peer = await self.peerStore.get(connection.remotePeer)
|
|
82
|
-
const log = connection.log.newScope('identify-push')
|
|
80
|
+
const peer = await self.components.peerStore.get(connection.remotePeer)
|
|
83
81
|
|
|
84
82
|
if (!peer.protocols.includes(self.protocol)) {
|
|
85
83
|
continue
|
|
@@ -115,8 +113,9 @@ export class IdentifyPush extends AbstractIdentify implements Startable, Identif
|
|
|
115
113
|
signal
|
|
116
114
|
})
|
|
117
115
|
} catch (err: any) {
|
|
118
|
-
// Just log errors
|
|
119
|
-
log.
|
|
116
|
+
// Just log errors if the stream was opened
|
|
117
|
+
const log = stream?.log.newScope('identify-push')
|
|
118
|
+
log?.error('could not push identify update to peer', err)
|
|
120
119
|
stream?.abort(err)
|
|
121
120
|
}
|
|
122
121
|
}
|
|
@@ -134,32 +133,25 @@ export class IdentifyPush extends AbstractIdentify implements Startable, Identif
|
|
|
134
133
|
/**
|
|
135
134
|
* Reads the Identify Push message from the given `connection`
|
|
136
135
|
*/
|
|
137
|
-
async handleProtocol (
|
|
138
|
-
const
|
|
139
|
-
const log = connection.log.newScope('identify-push')
|
|
136
|
+
async handleProtocol (stream: Stream, connection: Connection): Promise<void> {
|
|
137
|
+
const log = stream.log.newScope('identify-push')
|
|
140
138
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
139
|
+
if (this.components.peerId.equals(connection.remotePeer)) {
|
|
140
|
+
throw new Error('received push from ourselves?')
|
|
141
|
+
}
|
|
145
142
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
143
|
+
const options = {
|
|
144
|
+
signal: AbortSignal.timeout(this.timeout)
|
|
145
|
+
}
|
|
149
146
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
147
|
+
const pb = pbStream(stream, {
|
|
148
|
+
maxDataLength: this.maxMessageSize
|
|
149
|
+
}).pb(IdentifyMessage)
|
|
153
150
|
|
|
154
|
-
|
|
155
|
-
|
|
151
|
+
const message = await pb.read(options)
|
|
152
|
+
await stream.close(options)
|
|
156
153
|
|
|
157
|
-
|
|
158
|
-
} catch (err: any) {
|
|
159
|
-
log.error('received invalid message', err)
|
|
160
|
-
stream.abort(err)
|
|
161
|
-
return
|
|
162
|
-
}
|
|
154
|
+
await consumeIdentifyMessage(this.components.peerStore, this.components.events, log, connection, message)
|
|
163
155
|
|
|
164
156
|
log.trace('handled push from %p', connection.remotePeer)
|
|
165
157
|
}
|
package/src/identify.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
|
|
2
|
-
import { InvalidMessageError,
|
|
2
|
+
import { InvalidMessageError, serviceCapabilities } from '@libp2p/interface'
|
|
3
3
|
import { peerIdFromCID } from '@libp2p/peer-id'
|
|
4
4
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
5
|
-
import { isGlobalUnicast } from '@libp2p/utils
|
|
6
|
-
import {
|
|
7
|
-
import { CODE_IP6, CODE_IP6ZONE, protocols } from '@multiformats/multiaddr'
|
|
5
|
+
import { isGlobalUnicast, isPrivate, pbStream } from '@libp2p/utils'
|
|
6
|
+
import { CODE_IP6, CODE_IP6ZONE, CODE_P2P } from '@multiformats/multiaddr'
|
|
8
7
|
import { IP_OR_DOMAIN, TCP } from '@multiformats/multiaddr-matcher'
|
|
9
|
-
import { pbStream } from 'it-protobuf-stream'
|
|
10
8
|
import { setMaxListeners } from 'main-event'
|
|
11
9
|
import {
|
|
12
10
|
MULTICODEC_IDENTIFY_PROTOCOL_NAME,
|
|
@@ -15,7 +13,7 @@ import {
|
|
|
15
13
|
import { Identify as IdentifyMessage } from './pb/message.js'
|
|
16
14
|
import { AbstractIdentify, consumeIdentifyMessage, defaultValues, getCleanMultiaddr } from './utils.js'
|
|
17
15
|
import type { Identify as IdentifyInterface, IdentifyComponents, IdentifyInit } from './index.js'
|
|
18
|
-
import type { IdentifyResult, AbortOptions, Connection, Stream, Startable,
|
|
16
|
+
import type { IdentifyResult, AbortOptions, Connection, Stream, Startable, Logger } from '@libp2p/interface'
|
|
19
17
|
|
|
20
18
|
export class Identify extends AbstractIdentify implements Startable, IdentifyInterface {
|
|
21
19
|
constructor (components: IdentifyComponents, init: IdentifyInit = {}) {
|
|
@@ -30,14 +28,7 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt
|
|
|
30
28
|
components.events.addEventListener('connection:open', (evt) => {
|
|
31
29
|
const connection = evt.detail
|
|
32
30
|
this.identify(connection)
|
|
33
|
-
.catch(
|
|
34
|
-
if (err.name === UnsupportedProtocolError.name) {
|
|
35
|
-
// the remote did not support identify, ignore the error
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
this.log.error('error during identify trigged by connection:open', err)
|
|
40
|
-
})
|
|
31
|
+
.catch(() => {})
|
|
41
32
|
})
|
|
42
33
|
}
|
|
43
34
|
}
|
|
@@ -48,6 +39,7 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt
|
|
|
48
39
|
|
|
49
40
|
async _identify (connection: Connection, options: AbortOptions = {}): Promise<IdentifyMessage> {
|
|
50
41
|
let stream: Stream | undefined
|
|
42
|
+
let log: Logger | undefined
|
|
51
43
|
|
|
52
44
|
if (options.signal == null) {
|
|
53
45
|
const signal = AbortSignal.timeout(this.timeout)
|
|
@@ -64,17 +56,21 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt
|
|
|
64
56
|
...options,
|
|
65
57
|
runOnLimitedConnection: this.runOnLimitedConnection
|
|
66
58
|
})
|
|
59
|
+
log = stream.log.newScope('identify')
|
|
67
60
|
|
|
68
61
|
const pb = pbStream(stream, {
|
|
69
62
|
maxDataLength: this.maxMessageSize
|
|
70
63
|
}).pb(IdentifyMessage)
|
|
71
64
|
|
|
65
|
+
log('read response')
|
|
72
66
|
const message = await pb.read(options)
|
|
73
67
|
|
|
74
|
-
|
|
68
|
+
log('close write')
|
|
69
|
+
await pb.unwrap().unwrap().close(options)
|
|
75
70
|
|
|
76
71
|
return message
|
|
77
72
|
} catch (err: any) {
|
|
73
|
+
log?.error('identify failed - %e', err)
|
|
78
74
|
stream?.abort(err)
|
|
79
75
|
throw err
|
|
80
76
|
}
|
|
@@ -94,43 +90,41 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt
|
|
|
94
90
|
|
|
95
91
|
const key = publicKeyFromProtobuf(publicKey)
|
|
96
92
|
const id = peerIdFromCID(key.toCID())
|
|
97
|
-
const log = connection.log.newScope('identify')
|
|
98
93
|
|
|
99
94
|
if (!connection.remotePeer.equals(id)) {
|
|
100
95
|
throw new InvalidMessageError('Identified peer does not match the expected peer')
|
|
101
96
|
}
|
|
102
97
|
|
|
103
|
-
if (this.peerId.equals(id)) {
|
|
98
|
+
if (this.components.peerId.equals(id)) {
|
|
104
99
|
throw new InvalidMessageError('Identified peer is our own peer id?')
|
|
105
100
|
}
|
|
106
101
|
|
|
107
102
|
// if the observed address is publicly routable, add it to the address
|
|
108
103
|
// manager for verification via AutoNAT
|
|
109
|
-
this.maybeAddObservedAddress(observedAddr
|
|
104
|
+
this.maybeAddObservedAddress(observedAddr)
|
|
110
105
|
|
|
111
|
-
log('completed for peer %p and protocols %o', id, protocols)
|
|
106
|
+
this.log('completed for peer %p and protocols %o', id, protocols)
|
|
112
107
|
|
|
113
|
-
return consumeIdentifyMessage(this.peerStore, this.events, log, connection, message)
|
|
108
|
+
return consumeIdentifyMessage(this.components.peerStore, this.components.events, this.log, connection, message)
|
|
114
109
|
}
|
|
115
110
|
|
|
116
|
-
private maybeAddObservedAddress (observedAddr: Uint8Array | undefined
|
|
111
|
+
private maybeAddObservedAddress (observedAddr: Uint8Array | undefined): void {
|
|
117
112
|
const cleanObservedAddr = getCleanMultiaddr(observedAddr)
|
|
118
113
|
|
|
119
114
|
if (cleanObservedAddr == null) {
|
|
120
115
|
return
|
|
121
116
|
}
|
|
122
117
|
|
|
123
|
-
log.trace('our observed address was %a', cleanObservedAddr)
|
|
118
|
+
this.log.trace('our observed address was %a', cleanObservedAddr)
|
|
124
119
|
|
|
125
120
|
if (isPrivate(cleanObservedAddr)) {
|
|
126
|
-
this.log.trace('our observed address was private')
|
|
127
121
|
return
|
|
128
122
|
}
|
|
129
123
|
|
|
130
124
|
const tuples = cleanObservedAddr.getComponents()
|
|
131
125
|
|
|
132
126
|
if (((tuples[0].code === CODE_IP6) || (tuples[0].code === CODE_IP6ZONE && tuples[1].code === CODE_IP6)) && !isGlobalUnicast(cleanObservedAddr)) {
|
|
133
|
-
log.trace('our observed address was IPv6 but not a global unicast address')
|
|
127
|
+
this.log.trace('our observed address was IPv6 but not a global unicast address')
|
|
134
128
|
return
|
|
135
129
|
}
|
|
136
130
|
|
|
@@ -142,63 +136,62 @@ export class Identify extends AbstractIdentify implements Startable, IdentifyInt
|
|
|
142
136
|
return
|
|
143
137
|
}
|
|
144
138
|
|
|
145
|
-
log.trace('storing the observed address')
|
|
146
|
-
this.addressManager.addObservedAddr(cleanObservedAddr)
|
|
139
|
+
this.log.trace('storing the observed address')
|
|
140
|
+
this.components.addressManager.addObservedAddr(cleanObservedAddr)
|
|
147
141
|
}
|
|
148
142
|
|
|
149
143
|
/**
|
|
150
144
|
* Sends the `Identify` response with the Signed Peer Record
|
|
151
145
|
* to the requesting peer over the given `connection`
|
|
152
146
|
*/
|
|
153
|
-
async handleProtocol (
|
|
154
|
-
const
|
|
155
|
-
const log = connection.log.newScope('identify')
|
|
147
|
+
async handleProtocol (stream: Stream, connection: Connection): Promise<void> {
|
|
148
|
+
const log = stream.log.newScope('identify')
|
|
156
149
|
|
|
157
150
|
const signal = AbortSignal.timeout(this.timeout)
|
|
158
|
-
|
|
159
151
|
setMaxListeners(Infinity, signal)
|
|
160
152
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (multiaddrs.length > 0 && signedPeerRecord == null) {
|
|
167
|
-
const peerRecord = new PeerRecord({
|
|
168
|
-
peerId: this.peerId,
|
|
169
|
-
multiaddrs
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
const envelope = await RecordEnvelope.seal(peerRecord, this.privateKey)
|
|
173
|
-
signedPeerRecord = envelope.marshal().subarray()
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
let observedAddr: Uint8Array | undefined = connection.remoteAddr.bytes
|
|
177
|
-
|
|
178
|
-
if (!IP_OR_DOMAIN.matches(connection.remoteAddr)) {
|
|
179
|
-
observedAddr = undefined
|
|
180
|
-
}
|
|
153
|
+
const peerData = await this.components.peerStore.get(this.components.peerId, {
|
|
154
|
+
signal
|
|
155
|
+
})
|
|
156
|
+
const multiaddrs = this.components.addressManager.getAddresses().map(ma => ma.decapsulateCode(CODE_P2P))
|
|
157
|
+
let signedPeerRecord = peerData.peerRecordEnvelope
|
|
181
158
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
agentVersion: this.host.agentVersion,
|
|
187
|
-
publicKey: publicKeyToProtobuf(this.privateKey.publicKey),
|
|
188
|
-
listenAddrs: multiaddrs.map(addr => addr.bytes),
|
|
189
|
-
signedPeerRecord,
|
|
190
|
-
observedAddr,
|
|
191
|
-
protocols: peerData.protocols
|
|
192
|
-
}, {
|
|
193
|
-
signal
|
|
159
|
+
if (multiaddrs.length > 0 && signedPeerRecord == null) {
|
|
160
|
+
const peerRecord = new PeerRecord({
|
|
161
|
+
peerId: this.components.peerId,
|
|
162
|
+
multiaddrs
|
|
194
163
|
})
|
|
195
164
|
|
|
196
|
-
await
|
|
165
|
+
const envelope = await RecordEnvelope.seal(peerRecord, this.components.privateKey, {
|
|
197
166
|
signal
|
|
198
167
|
})
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
168
|
+
signedPeerRecord = envelope.marshal().subarray()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let observedAddr: Uint8Array | undefined = connection.remoteAddr.bytes
|
|
172
|
+
|
|
173
|
+
if (!IP_OR_DOMAIN.matches(connection.remoteAddr)) {
|
|
174
|
+
observedAddr = undefined
|
|
202
175
|
}
|
|
176
|
+
|
|
177
|
+
const pb = pbStream(stream).pb(IdentifyMessage)
|
|
178
|
+
|
|
179
|
+
log('send response')
|
|
180
|
+
await pb.write({
|
|
181
|
+
protocolVersion: this.host.protocolVersion,
|
|
182
|
+
agentVersion: this.host.agentVersion,
|
|
183
|
+
publicKey: publicKeyToProtobuf(this.components.privateKey.publicKey),
|
|
184
|
+
listenAddrs: multiaddrs.map(addr => addr.bytes),
|
|
185
|
+
signedPeerRecord,
|
|
186
|
+
observedAddr,
|
|
187
|
+
protocols: peerData.protocols
|
|
188
|
+
}, {
|
|
189
|
+
signal
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
log('close write')
|
|
193
|
+
await pb.unwrap().unwrap().close({
|
|
194
|
+
signal
|
|
195
|
+
})
|
|
203
196
|
}
|
|
204
197
|
}
|
package/src/index.ts
CHANGED
|
@@ -55,13 +55,6 @@ export interface IdentifyInit {
|
|
|
55
55
|
*/
|
|
56
56
|
protocolPrefix?: string
|
|
57
57
|
|
|
58
|
-
/**
|
|
59
|
-
* What details we should send as part of an identify message
|
|
60
|
-
*
|
|
61
|
-
* @deprecated Use `nodeInfo.userAgent` in the main libp2p config instead
|
|
62
|
-
*/
|
|
63
|
-
agentVersion?: string
|
|
64
|
-
|
|
65
58
|
/**
|
|
66
59
|
* How long we should wait for a remote peer to send their identify response
|
|
67
60
|
*
|
package/src/utils.ts
CHANGED
|
@@ -7,8 +7,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
|
7
7
|
import { IDENTIFY_PROTOCOL_VERSION, MAX_IDENTIFY_MESSAGE_SIZE, MAX_PUSH_CONCURRENCY } from './consts.js'
|
|
8
8
|
import type { IdentifyComponents, IdentifyInit } from './index.js'
|
|
9
9
|
import type { Identify as IdentifyMessage } from './pb/message.js'
|
|
10
|
-
import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, Logger, Connection, Peer, PeerData, PeerStore,
|
|
11
|
-
import type { AddressManager, Registrar } from '@libp2p/interface-internal'
|
|
10
|
+
import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, Logger, Connection, Peer, PeerData, PeerStore, Startable, Stream } from '@libp2p/interface'
|
|
12
11
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
13
12
|
import type { TypedEventTarget } from 'main-event'
|
|
14
13
|
|
|
@@ -38,14 +37,6 @@ export function getCleanMultiaddr (addr: Uint8Array | string | null | undefined)
|
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
|
|
41
|
-
export function getAgentVersion (nodeInfo: NodeInfo, agentVersion?: string): string {
|
|
42
|
-
if (agentVersion != null) {
|
|
43
|
-
return agentVersion
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return nodeInfo.userAgent
|
|
47
|
-
}
|
|
48
|
-
|
|
49
40
|
export async function consumeIdentifyMessage (peerStore: PeerStore, events: TypedEventTarget<Libp2pEvents>, log: Logger, connection: Connection, message: IdentifyMessage): Promise<IdentifyResult> {
|
|
50
41
|
log('received identify from %p', connection.remotePeer)
|
|
51
42
|
|
|
@@ -191,31 +182,21 @@ export abstract class AbstractIdentify implements Startable {
|
|
|
191
182
|
agentVersion: string
|
|
192
183
|
}
|
|
193
184
|
|
|
185
|
+
protected components: IdentifyComponents
|
|
194
186
|
protected protocol: string
|
|
195
187
|
protected started: boolean
|
|
196
188
|
protected readonly timeout: number
|
|
197
|
-
protected readonly peerId: PeerId
|
|
198
|
-
protected readonly privateKey: PrivateKey
|
|
199
|
-
protected readonly peerStore: PeerStore
|
|
200
|
-
protected readonly registrar: Registrar
|
|
201
|
-
protected readonly addressManager: AddressManager
|
|
202
189
|
private readonly maxInboundStreams: number
|
|
203
190
|
private readonly maxOutboundStreams: number
|
|
204
191
|
protected readonly maxMessageSize: number
|
|
205
192
|
protected readonly maxObservedAddresses: number
|
|
206
|
-
protected readonly events: TypedEventTarget<Libp2pEvents>
|
|
207
193
|
protected readonly runOnLimitedConnection: boolean
|
|
208
194
|
protected readonly log: Logger
|
|
209
195
|
|
|
210
196
|
constructor (components: IdentifyComponents, init: AbstractIdentifyInit) {
|
|
211
197
|
this.protocol = init.protocol
|
|
212
198
|
this.started = false
|
|
213
|
-
this.
|
|
214
|
-
this.privateKey = components.privateKey
|
|
215
|
-
this.peerStore = components.peerStore
|
|
216
|
-
this.registrar = components.registrar
|
|
217
|
-
this.addressManager = components.addressManager
|
|
218
|
-
this.events = components.events
|
|
199
|
+
this.components = components
|
|
219
200
|
this.log = init.log
|
|
220
201
|
|
|
221
202
|
this.timeout = init.timeout ?? defaultValues.timeout
|
|
@@ -228,8 +209,10 @@ export abstract class AbstractIdentify implements Startable {
|
|
|
228
209
|
// Store self host metadata
|
|
229
210
|
this.host = {
|
|
230
211
|
protocolVersion: `${init.protocolPrefix ?? defaultValues.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
|
|
231
|
-
agentVersion:
|
|
212
|
+
agentVersion: components.nodeInfo.userAgent
|
|
232
213
|
}
|
|
214
|
+
|
|
215
|
+
this.handleProtocol = this.handleProtocol.bind(this)
|
|
233
216
|
}
|
|
234
217
|
|
|
235
218
|
isStarted (): boolean {
|
|
@@ -241,18 +224,14 @@ export abstract class AbstractIdentify implements Startable {
|
|
|
241
224
|
return
|
|
242
225
|
}
|
|
243
226
|
|
|
244
|
-
await this.peerStore.merge(this.peerId, {
|
|
227
|
+
await this.components.peerStore.merge(this.components.peerId, {
|
|
245
228
|
metadata: {
|
|
246
229
|
AgentVersion: uint8ArrayFromString(this.host.agentVersion),
|
|
247
230
|
ProtocolVersion: uint8ArrayFromString(this.host.protocolVersion)
|
|
248
231
|
}
|
|
249
232
|
})
|
|
250
233
|
|
|
251
|
-
await this.registrar.handle(this.protocol,
|
|
252
|
-
void this.handleProtocol(data).catch(err => {
|
|
253
|
-
this.log.error(err)
|
|
254
|
-
})
|
|
255
|
-
}, {
|
|
234
|
+
await this.components.registrar.handle(this.protocol, this.handleProtocol, {
|
|
256
235
|
maxInboundStreams: this.maxInboundStreams,
|
|
257
236
|
maxOutboundStreams: this.maxOutboundStreams,
|
|
258
237
|
runOnLimitedConnection: this.runOnLimitedConnection
|
|
@@ -262,10 +241,10 @@ export abstract class AbstractIdentify implements Startable {
|
|
|
262
241
|
}
|
|
263
242
|
|
|
264
243
|
async stop (): Promise<void> {
|
|
265
|
-
await this.registrar.unhandle(this.protocol)
|
|
244
|
+
await this.components.registrar.unhandle(this.protocol)
|
|
266
245
|
|
|
267
246
|
this.started = false
|
|
268
247
|
}
|
|
269
248
|
|
|
270
|
-
protected abstract handleProtocol (
|
|
249
|
+
protected abstract handleProtocol (stream: Stream, connection: Connection): Promise<void>
|
|
271
250
|
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"Identify": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.Identify.html",
|
|
3
|
-
".:Identify": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.Identify.html",
|
|
4
|
-
"IdentifyComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyComponents.html",
|
|
5
|
-
".:IdentifyComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyComponents.html",
|
|
6
|
-
"IdentifyInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyInit.html",
|
|
7
|
-
".:IdentifyInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyInit.html",
|
|
8
|
-
"IdentifyPush": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyPush.html",
|
|
9
|
-
".:IdentifyPush": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyPush.html",
|
|
10
|
-
"IdentifyPushComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyPushComponents.html",
|
|
11
|
-
".:IdentifyPushComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyPushComponents.html",
|
|
12
|
-
"IdentifyPushInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyPushInit.html",
|
|
13
|
-
".:IdentifyPushInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_identify.IdentifyPushInit.html",
|
|
14
|
-
"identify": "https://libp2p.github.io/js-libp2p/functions/_libp2p_identify.identify.html",
|
|
15
|
-
".:identify": "https://libp2p.github.io/js-libp2p/functions/_libp2p_identify.identify.html",
|
|
16
|
-
"identifyPush": "https://libp2p.github.io/js-libp2p/functions/_libp2p_identify.identifyPush.html",
|
|
17
|
-
".:identifyPush": "https://libp2p.github.io/js-libp2p/functions/_libp2p_identify.identifyPush.html"
|
|
18
|
-
}
|