@libp2p/identify 1.0.21 → 2.0.0

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.
@@ -6,4 +6,6 @@ export declare const MULTICODEC_IDENTIFY_PROTOCOL_NAME = "id";
6
6
  export declare const MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME = "id/push";
7
7
  export declare const MULTICODEC_IDENTIFY_PROTOCOL_VERSION = "1.0.0";
8
8
  export declare const MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION = "1.0.0";
9
+ export declare const MAX_IDENTIFY_MESSAGE_SIZE: number;
10
+ export declare const MAX_PUSH_CONCURRENCY = 32;
9
11
  //# sourceMappingURL=consts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../src/consts.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,eAAe,CAAA;AAC5C,eAAO,MAAM,mBAAmB,mBAAmB,CAAA;AACnD,eAAO,MAAM,wBAAwB,wBAAwB,CAAA;AAE7D,eAAO,MAAM,yBAAyB,UAAU,CAAA;AAChD,eAAO,MAAM,iCAAiC,OAAO,CAAA;AACrD,eAAO,MAAM,sCAAsC,YAAY,CAAA;AAC/D,eAAO,MAAM,oCAAoC,UAAU,CAAA;AAC3D,eAAO,MAAM,yCAAyC,UAAU,CAAA"}
1
+ {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../src/consts.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,eAAe,CAAA;AAC5C,eAAO,MAAM,mBAAmB,mBAAmB,CAAA;AACnD,eAAO,MAAM,wBAAwB,wBAAwB,CAAA;AAE7D,eAAO,MAAM,yBAAyB,UAAU,CAAA;AAChD,eAAO,MAAM,iCAAiC,OAAO,CAAA;AACrD,eAAO,MAAM,sCAAsC,YAAY,CAAA;AAC/D,eAAO,MAAM,oCAAoC,UAAU,CAAA;AAC3D,eAAO,MAAM,yCAAyC,UAAU,CAAA;AAGhE,eAAO,MAAM,yBAAyB,QAAW,CAAA;AAGjD,eAAO,MAAM,oBAAoB,KAAK,CAAA"}
@@ -6,4 +6,8 @@ export const MULTICODEC_IDENTIFY_PROTOCOL_NAME = 'id';
6
6
  export const MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME = 'id/push';
7
7
  export const MULTICODEC_IDENTIFY_PROTOCOL_VERSION = '1.0.0';
8
8
  export const MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION = '1.0.0';
9
+ // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52
10
+ export const MAX_IDENTIFY_MESSAGE_SIZE = 1024 * 8;
11
+ // https://github.com/libp2p/go-libp2p/blob/0385ec924bad172f74a74db09939e97c079b1420/p2p/protocol/identify/id.go#L47C7-L47C25
12
+ export const MAX_PUSH_CONCURRENCY = 32;
9
13
  //# sourceMappingURL=consts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.js","sourceRoot":"","sources":["../../src/consts.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,YAAY,CAAA,CAAC,aAAa;AAC1D,MAAM,CAAC,MAAM,mBAAmB,GAAG,gBAAgB,CAAA,CAAC,aAAa;AACjE,MAAM,CAAC,MAAM,wBAAwB,GAAG,qBAAqB,CAAA,CAAC,aAAa;AAE3E,MAAM,CAAC,MAAM,yBAAyB,GAAG,OAAO,CAAA;AAChD,MAAM,CAAC,MAAM,iCAAiC,GAAG,IAAI,CAAA;AACrD,MAAM,CAAC,MAAM,sCAAsC,GAAG,SAAS,CAAA;AAC/D,MAAM,CAAC,MAAM,oCAAoC,GAAG,OAAO,CAAA;AAC3D,MAAM,CAAC,MAAM,yCAAyC,GAAG,OAAO,CAAA"}
1
+ {"version":3,"file":"consts.js","sourceRoot":"","sources":["../../src/consts.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,YAAY,CAAA,CAAC,aAAa;AAC1D,MAAM,CAAC,MAAM,mBAAmB,GAAG,gBAAgB,CAAA,CAAC,aAAa;AACjE,MAAM,CAAC,MAAM,wBAAwB,GAAG,qBAAqB,CAAA,CAAC,aAAa;AAE3E,MAAM,CAAC,MAAM,yBAAyB,GAAG,OAAO,CAAA;AAChD,MAAM,CAAC,MAAM,iCAAiC,GAAG,IAAI,CAAA;AACrD,MAAM,CAAC,MAAM,sCAAsC,GAAG,SAAS,CAAA;AAC/D,MAAM,CAAC,MAAM,oCAAoC,GAAG,OAAO,CAAA;AAC3D,MAAM,CAAC,MAAM,yCAAyC,GAAG,OAAO,CAAA;AAEhE,oHAAoH;AACpH,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,GAAG,CAAC,CAAA;AAEjD,6HAA6H;AAC7H,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAA"}
@@ -0,0 +1,18 @@
1
+ import { AbstractIdentify } from './utils.js';
2
+ import type { IdentifyPush as IdentifyPushInterface, IdentifyPushComponents, IdentifyPushInit } from './index.js';
3
+ import type { Startable } from '@libp2p/interface';
4
+ import type { IncomingStreamData } from '@libp2p/interface-internal';
5
+ export declare class IdentifyPush extends AbstractIdentify implements Startable, IdentifyPushInterface {
6
+ private readonly connectionManager;
7
+ private readonly concurrency;
8
+ constructor(components: IdentifyPushComponents, init?: IdentifyPushInit);
9
+ /**
10
+ * Calls `push` on all peer connections
11
+ */
12
+ push(): Promise<void>;
13
+ /**
14
+ * Reads the Identify Push message from the given `connection`
15
+ */
16
+ handleProtocol(data: IncomingStreamData): Promise<void>;
17
+ }
18
+ //# sourceMappingURL=identify-push.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identify-push.d.ts","sourceRoot":"","sources":["../../src/identify-push.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,gBAAgB,EAAyC,MAAM,YAAY,CAAA;AACpF,OAAO,KAAK,EAAE,YAAY,IAAI,qBAAqB,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AACjH,OAAO,KAAK,EAAU,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,EAAqB,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAEvF,qBAAa,YAAa,SAAQ,gBAAiB,YAAW,SAAS,EAAE,qBAAqB;IAC5F,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;gBAEvB,UAAU,EAAE,sBAAsB,EAAE,IAAI,GAAE,gBAAqB;IAkB5E;;OAEG;IACG,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAqE5B;;OAEG;IACG,cAAc,CAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;CA4B/D"}
@@ -0,0 +1,120 @@
1
+ /* eslint-disable complexity */
2
+ import { setMaxListeners } from '@libp2p/interface';
3
+ import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record';
4
+ import { protocols } from '@multiformats/multiaddr';
5
+ import drain from 'it-drain';
6
+ import parallel from 'it-parallel';
7
+ import { pbStream } from 'it-protobuf-stream';
8
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
9
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
10
+ import { MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME, MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION } from './consts.js';
11
+ import { Identify as IdentifyMessage } from './pb/message.js';
12
+ import { AbstractIdentify, consumeIdentifyMessage, defaultValues } from './utils.js';
13
+ export class IdentifyPush extends AbstractIdentify {
14
+ connectionManager;
15
+ concurrency;
16
+ constructor(components, init = {}) {
17
+ super(components, {
18
+ ...init,
19
+ protocol: `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`,
20
+ log: components.logger.forComponent('libp2p:identify-push')
21
+ });
22
+ this.connectionManager = components.connectionManager;
23
+ this.concurrency = init.concurrency ?? defaultValues.concurrency;
24
+ if ((init.runOnSelfUpdate ?? defaultValues.runOnSelfUpdate)) {
25
+ // When self peer record changes, trigger identify-push
26
+ components.events.addEventListener('self:peer:update', (evt) => {
27
+ void this.push().catch(err => { this.log.error(err); });
28
+ });
29
+ }
30
+ }
31
+ /**
32
+ * Calls `push` on all peer connections
33
+ */
34
+ async push() {
35
+ // Do not try to push if we are not running
36
+ if (!this.isStarted()) {
37
+ return;
38
+ }
39
+ const listenAddresses = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code));
40
+ const peerRecord = new PeerRecord({
41
+ peerId: this.peerId,
42
+ multiaddrs: listenAddresses
43
+ });
44
+ const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.peerId);
45
+ const supportedProtocols = this.registrar.getProtocols();
46
+ const peer = await this.peerStore.get(this.peerId);
47
+ const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion));
48
+ const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') ?? uint8ArrayFromString(this.host.protocolVersion));
49
+ const self = this;
50
+ async function* pushToConnections() {
51
+ for (const connection of self.connectionManager.getConnections()) {
52
+ const peer = await self.peerStore.get(connection.remotePeer);
53
+ if (!peer.protocols.includes(self.protocol)) {
54
+ continue;
55
+ }
56
+ yield async () => {
57
+ let stream;
58
+ const signal = AbortSignal.timeout(self.timeout);
59
+ setMaxListeners(Infinity, signal);
60
+ try {
61
+ stream = await connection.newStream(self.protocol, {
62
+ signal,
63
+ runOnTransientConnection: self.runOnTransientConnection
64
+ });
65
+ const pb = pbStream(stream, {
66
+ maxDataLength: self.maxMessageSize
67
+ }).pb(IdentifyMessage);
68
+ await pb.write({
69
+ listenAddrs: listenAddresses.map(ma => ma.bytes),
70
+ signedPeerRecord: signedPeerRecord.marshal(),
71
+ protocols: supportedProtocols,
72
+ agentVersion,
73
+ protocolVersion
74
+ }, {
75
+ signal
76
+ });
77
+ await stream.close({
78
+ signal
79
+ });
80
+ }
81
+ catch (err) {
82
+ // Just log errors
83
+ self.log.error('could not push identify update to peer', err);
84
+ stream?.abort(err);
85
+ }
86
+ };
87
+ }
88
+ }
89
+ await drain(parallel(pushToConnections(), {
90
+ concurrency: this.concurrency
91
+ }));
92
+ }
93
+ /**
94
+ * Reads the Identify Push message from the given `connection`
95
+ */
96
+ async handleProtocol(data) {
97
+ const { connection, stream } = data;
98
+ try {
99
+ if (this.peerId.equals(connection.remotePeer)) {
100
+ throw new Error('received push from ourselves?');
101
+ }
102
+ const options = {
103
+ signal: AbortSignal.timeout(this.timeout)
104
+ };
105
+ const pb = pbStream(stream, {
106
+ maxDataLength: this.maxMessageSize
107
+ }).pb(IdentifyMessage);
108
+ const message = await pb.read(options);
109
+ await stream.close(options);
110
+ await consumeIdentifyMessage(this.peerStore, this.events, this.log, connection, message);
111
+ }
112
+ catch (err) {
113
+ this.log.error('received invalid message', err);
114
+ stream.abort(err);
115
+ return;
116
+ }
117
+ this.log('handled push from %p', connection.remotePeer);
118
+ }
119
+ }
120
+ //# sourceMappingURL=identify-push.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identify-push.js","sourceRoot":"","sources":["../../src/identify-push.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,QAAQ,MAAM,aAAa,CAAA;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,y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,WAAW,EAAE,IAAI,CAAC,WAAW;SAC9B,CAAC,CAAC,CAAA;IACL,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,cAAc,CAAE,IAAwB;QAC5C,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;QAEnC,IAAI,CAAC;YACH,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC9C,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAA;YAClD,CAAC;YAED,MAAM,OAAO,GAAG;gBACd,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;aAC1C,CAAA;YAED,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,EAAE;gBAC1B,aAAa,EAAE,IAAI,CAAC,cAAc;aACnC,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,CAAA;YAEtB,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YACtC,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YAE3B,MAAM,sBAAsB,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;QAC1F,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,0BAA0B,EAAE,GAAG,CAAC,CAAA;YAC/C,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACjB,OAAM;QACR,CAAC;QAED,IAAI,CAAC,GAAG,CAAC,sBAAsB,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;IACzD,CAAC;CACF"}
@@ -1,53 +1,16 @@
1
1
  import { Identify as IdentifyMessage } from './pb/message.js';
2
+ import { AbstractIdentify } from './utils.js';
2
3
  import type { Identify as IdentifyInterface, IdentifyComponents, IdentifyInit } from './index.js';
3
4
  import type { IdentifyResult, AbortOptions, Connection, Startable } from '@libp2p/interface';
4
5
  import type { IncomingStreamData } from '@libp2p/interface-internal';
5
- export declare class Identify implements Startable, IdentifyInterface {
6
- #private;
7
- private readonly identifyProtocolStr;
8
- private readonly identifyPushProtocolStr;
9
- readonly host: {
10
- protocolVersion: string;
11
- agentVersion: string;
12
- };
13
- private started;
14
- private readonly timeout;
15
- private readonly peerId;
16
- private readonly peerStore;
17
- private readonly registrar;
18
- private readonly connectionManager;
19
- private readonly addressManager;
20
- private readonly maxInboundStreams;
21
- private readonly maxOutboundStreams;
22
- private readonly maxPushIncomingStreams;
23
- private readonly maxPushOutgoingStreams;
24
- private readonly maxIdentifyMessageSize;
25
- private readonly maxObservedAddresses;
26
- private readonly events;
27
- private readonly runOnTransientConnection;
28
- private readonly log;
6
+ export declare class Identify extends AbstractIdentify implements Startable, IdentifyInterface {
29
7
  constructor(components: IdentifyComponents, init?: IdentifyInit);
30
- isStarted(): boolean;
31
- start(): Promise<void>;
32
- stop(): Promise<void>;
33
- /**
34
- * Send an Identify Push update to the list of connections
35
- */
36
- pushToConnections(connections: Connection[]): Promise<void>;
37
- /**
38
- * Calls `push` on all peer connections
39
- */
40
- push(): Promise<void>;
41
8
  _identify(connection: Connection, options?: AbortOptions): Promise<IdentifyMessage>;
42
9
  identify(connection: Connection, options?: AbortOptions): Promise<IdentifyResult>;
43
10
  /**
44
11
  * Sends the `Identify` response with the Signed Peer Record
45
12
  * to the requesting peer over the given `connection`
46
13
  */
47
- _handleIdentify(data: IncomingStreamData): Promise<void>;
48
- /**
49
- * Reads the Identify Push message from the given `connection`
50
- */
51
- _handlePush(data: IncomingStreamData): Promise<void>;
14
+ handleProtocol(data: IncomingStreamData): Promise<void>;
52
15
  }
53
16
  //# sourceMappingURL=identify.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"identify.d.ts","sourceRoot":"","sources":["../../src/identify.ts"],"names":[],"mappings":"AA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AU,EAAE,kBAAkB,EAAE,IAAI,GAAE,YAAiB;IAgB9D,SAAS,CAAE,UAAU,EAAE,UAAU,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,eAAe,CAAC;IAmCxF,QAAQ,CAAE,UAAU,EAAE,UAAU,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,cAAc,CAAC;IAqC5F;;;OAGG;IACG,cAAc,CAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;CAmD/D"}
@@ -1,74 +1,20 @@
1
1
  /* eslint-disable complexity */
2
- import { CodeError, ERR_NOT_FOUND, setMaxListeners } from '@libp2p/interface';
2
+ import { CodeError, setMaxListeners } from '@libp2p/interface';
3
3
  import { peerIdFromKeys } from '@libp2p/peer-id';
4
4
  import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record';
5
- import { multiaddr, protocols } from '@multiformats/multiaddr';
5
+ import { protocols } from '@multiformats/multiaddr';
6
6
  import { IP_OR_DOMAIN } from '@multiformats/multiaddr-matcher';
7
7
  import { pbStream } from 'it-protobuf-stream';
8
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
9
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
10
- import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe';
11
- import { IDENTIFY_PROTOCOL_VERSION, MULTICODEC_IDENTIFY_PROTOCOL_NAME, MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME, MULTICODEC_IDENTIFY_PROTOCOL_VERSION, MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION } from './consts.js';
8
+ import { MULTICODEC_IDENTIFY_PROTOCOL_NAME, MULTICODEC_IDENTIFY_PROTOCOL_VERSION } from './consts.js';
12
9
  import { Identify as IdentifyMessage } from './pb/message.js';
13
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52
14
- const MAX_IDENTIFY_MESSAGE_SIZE = 1024 * 8;
15
- const defaultValues = {
16
- protocolPrefix: 'ipfs',
17
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L48
18
- timeout: 60000,
19
- maxInboundStreams: 1,
20
- maxOutboundStreams: 1,
21
- maxPushIncomingStreams: 1,
22
- maxPushOutgoingStreams: 1,
23
- maxObservedAddresses: 10,
24
- maxIdentifyMessageSize: 8192,
25
- runOnConnectionOpen: true,
26
- runOnTransientConnection: true
27
- };
28
- export class Identify {
29
- identifyProtocolStr;
30
- identifyPushProtocolStr;
31
- host;
32
- started;
33
- timeout;
34
- peerId;
35
- peerStore;
36
- registrar;
37
- connectionManager;
38
- addressManager;
39
- maxInboundStreams;
40
- maxOutboundStreams;
41
- maxPushIncomingStreams;
42
- maxPushOutgoingStreams;
43
- maxIdentifyMessageSize;
44
- maxObservedAddresses;
45
- events;
46
- runOnTransientConnection;
47
- log;
10
+ import { AbstractIdentify, consumeIdentifyMessage, defaultValues, getCleanMultiaddr } from './utils.js';
11
+ export class Identify extends AbstractIdentify {
48
12
  constructor(components, init = {}) {
49
- this.started = false;
50
- this.peerId = components.peerId;
51
- this.peerStore = components.peerStore;
52
- this.registrar = components.registrar;
53
- this.addressManager = components.addressManager;
54
- this.connectionManager = components.connectionManager;
55
- this.events = components.events;
56
- this.log = components.logger.forComponent('libp2p:identify');
57
- this.identifyProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`;
58
- this.identifyPushProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`;
59
- this.timeout = init.timeout ?? defaultValues.timeout;
60
- this.maxInboundStreams = init.maxInboundStreams ?? defaultValues.maxInboundStreams;
61
- this.maxOutboundStreams = init.maxOutboundStreams ?? defaultValues.maxOutboundStreams;
62
- this.maxPushIncomingStreams = init.maxPushIncomingStreams ?? defaultValues.maxPushIncomingStreams;
63
- this.maxPushOutgoingStreams = init.maxPushOutgoingStreams ?? defaultValues.maxPushOutgoingStreams;
64
- this.maxIdentifyMessageSize = init.maxIdentifyMessageSize ?? defaultValues.maxIdentifyMessageSize;
65
- this.maxObservedAddresses = init.maxObservedAddresses ?? defaultValues.maxObservedAddresses;
66
- this.runOnTransientConnection = init.runOnTransientConnection ?? defaultValues.runOnTransientConnection;
67
- // Store self host metadata
68
- this.host = {
69
- protocolVersion: `${init.protocolPrefix ?? defaultValues.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
70
- agentVersion: init.agentVersion ?? `${components.nodeInfo.name}/${components.nodeInfo.version}`
71
- };
13
+ super(components, {
14
+ ...init,
15
+ protocol: `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`,
16
+ log: components.logger.forComponent('libp2p:identify')
17
+ });
72
18
  if (init.runOnConnectionOpen ?? defaultValues.runOnConnectionOpen) {
73
19
  // When a new connection happens, trigger identify
74
20
  components.events.addEventListener('connection:open', (evt) => {
@@ -76,129 +22,6 @@ export class Identify {
76
22
  this.identify(connection).catch(err => { this.log.error('error during identify trigged by connection:open', err); });
77
23
  });
78
24
  }
79
- // When self peer record changes, trigger identify-push
80
- components.events.addEventListener('self:peer:update', (evt) => {
81
- void this.push().catch(err => { this.log.error(err); });
82
- });
83
- // Append user agent version to default AGENT_VERSION depending on the environment
84
- if (this.host.agentVersion === `${components.nodeInfo.name}/${components.nodeInfo.version}`) {
85
- if (isNode || isElectronMain) {
86
- this.host.agentVersion += ` UserAgent=${globalThis.process.version}`;
87
- }
88
- else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
89
- this.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`;
90
- }
91
- }
92
- }
93
- isStarted() {
94
- return this.started;
95
- }
96
- async start() {
97
- if (this.started) {
98
- return;
99
- }
100
- await this.peerStore.merge(this.peerId, {
101
- metadata: {
102
- AgentVersion: uint8ArrayFromString(this.host.agentVersion),
103
- ProtocolVersion: uint8ArrayFromString(this.host.protocolVersion)
104
- }
105
- });
106
- await this.registrar.handle(this.identifyProtocolStr, (data) => {
107
- void this._handleIdentify(data).catch(err => {
108
- this.log.error(err);
109
- });
110
- }, {
111
- maxInboundStreams: this.maxInboundStreams,
112
- maxOutboundStreams: this.maxOutboundStreams,
113
- runOnTransientConnection: this.runOnTransientConnection
114
- });
115
- await this.registrar.handle(this.identifyPushProtocolStr, (data) => {
116
- void this._handlePush(data).catch(err => {
117
- this.log.error(err);
118
- });
119
- }, {
120
- maxInboundStreams: this.maxPushIncomingStreams,
121
- maxOutboundStreams: this.maxPushOutgoingStreams,
122
- runOnTransientConnection: this.runOnTransientConnection
123
- });
124
- this.started = true;
125
- }
126
- async stop() {
127
- await this.registrar.unhandle(this.identifyProtocolStr);
128
- await this.registrar.unhandle(this.identifyPushProtocolStr);
129
- this.started = false;
130
- }
131
- /**
132
- * Send an Identify Push update to the list of connections
133
- */
134
- async pushToConnections(connections) {
135
- const listenAddresses = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code));
136
- const peerRecord = new PeerRecord({
137
- peerId: this.peerId,
138
- multiaddrs: listenAddresses
139
- });
140
- const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.peerId);
141
- const supportedProtocols = this.registrar.getProtocols();
142
- const peer = await this.peerStore.get(this.peerId);
143
- const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion));
144
- const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') ?? uint8ArrayFromString(this.host.protocolVersion));
145
- const pushes = connections.map(async (connection) => {
146
- let stream;
147
- const signal = AbortSignal.timeout(this.timeout);
148
- setMaxListeners(Infinity, signal);
149
- try {
150
- stream = await connection.newStream(this.identifyPushProtocolStr, {
151
- signal,
152
- runOnTransientConnection: this.runOnTransientConnection
153
- });
154
- const pb = pbStream(stream, {
155
- maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
156
- }).pb(IdentifyMessage);
157
- await pb.write({
158
- listenAddrs: listenAddresses.map(ma => ma.bytes),
159
- signedPeerRecord: signedPeerRecord.marshal(),
160
- protocols: supportedProtocols,
161
- agentVersion,
162
- protocolVersion
163
- }, {
164
- signal
165
- });
166
- await stream.close({
167
- signal
168
- });
169
- }
170
- catch (err) {
171
- // Just log errors
172
- this.log.error('could not push identify update to peer', err);
173
- stream?.abort(err);
174
- }
175
- });
176
- await Promise.all(pushes);
177
- }
178
- /**
179
- * Calls `push` on all peer connections
180
- */
181
- async push() {
182
- // Do not try to push if we are not running
183
- if (!this.isStarted()) {
184
- return;
185
- }
186
- const connections = [];
187
- await Promise.all(this.connectionManager.getConnections().map(async (conn) => {
188
- try {
189
- const peer = await this.peerStore.get(conn.remotePeer);
190
- if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
191
- return;
192
- }
193
- connections.push(conn);
194
- }
195
- catch (err) {
196
- if (err.code !== ERR_NOT_FOUND) {
197
- throw err;
198
- }
199
- }
200
- }));
201
- await this.pushToConnections(connections);
202
25
  }
203
26
  async _identify(connection, options = {}) {
204
27
  let stream;
@@ -211,12 +34,12 @@ export class Identify {
211
34
  };
212
35
  }
213
36
  try {
214
- stream = await connection.newStream(this.identifyProtocolStr, {
37
+ stream = await connection.newStream(this.protocol, {
215
38
  ...options,
216
39
  runOnTransientConnection: this.runOnTransientConnection
217
40
  });
218
41
  const pb = pbStream(stream, {
219
- maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
42
+ maxDataLength: this.maxMessageSize
220
43
  }).pb(IdentifyMessage);
221
44
  const message = await pb.read(options);
222
45
  await stream.close(options);
@@ -250,13 +73,13 @@ export class Identify {
250
73
  this.log('storing our observed address %a', cleanObservedAddr);
251
74
  this.addressManager.addObservedAddr(cleanObservedAddr);
252
75
  }
253
- return this.#consumeIdentifyMessage(connection, message);
76
+ return consumeIdentifyMessage(this.peerStore, this.events, this.log, connection, message);
254
77
  }
255
78
  /**
256
79
  * Sends the `Identify` response with the Signed Peer Record
257
80
  * to the requesting peer over the given `connection`
258
81
  */
259
- async _handleIdentify(data) {
82
+ async handleProtocol(data) {
260
83
  const { connection, stream } = data;
261
84
  const signal = AbortSignal.timeout(this.timeout);
262
85
  setMaxListeners(Infinity, signal);
@@ -298,148 +121,5 @@ export class Identify {
298
121
  stream.abort(err);
299
122
  }
300
123
  }
301
- /**
302
- * Reads the Identify Push message from the given `connection`
303
- */
304
- async _handlePush(data) {
305
- const { connection, stream } = data;
306
- try {
307
- if (this.peerId.equals(connection.remotePeer)) {
308
- throw new Error('received push from ourselves?');
309
- }
310
- const options = {
311
- signal: AbortSignal.timeout(this.timeout)
312
- };
313
- const pb = pbStream(stream, {
314
- maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
315
- }).pb(IdentifyMessage);
316
- const message = await pb.read(options);
317
- await stream.close(options);
318
- await this.#consumeIdentifyMessage(connection, message);
319
- }
320
- catch (err) {
321
- this.log.error('received invalid message', err);
322
- stream.abort(err);
323
- return;
324
- }
325
- this.log('handled push from %p', connection.remotePeer);
326
- }
327
- async #consumeIdentifyMessage(connection, message) {
328
- this.log('received identify from %p', connection.remotePeer);
329
- if (message == null) {
330
- throw new CodeError('message was null or undefined', 'ERR_INVALID_MESSAGE');
331
- }
332
- const peer = {};
333
- if (message.listenAddrs.length > 0) {
334
- peer.addresses = message.listenAddrs.map(buf => ({
335
- isCertified: false,
336
- multiaddr: multiaddr(buf)
337
- }));
338
- }
339
- if (message.protocols.length > 0) {
340
- peer.protocols = message.protocols;
341
- }
342
- if (message.publicKey != null) {
343
- peer.publicKey = message.publicKey;
344
- const peerId = await peerIdFromKeys(message.publicKey);
345
- if (!peerId.equals(connection.remotePeer)) {
346
- throw new CodeError('public key did not match remote PeerId', 'ERR_INVALID_PUBLIC_KEY');
347
- }
348
- }
349
- let output;
350
- // if the peer record has been sent, prefer the addresses in the record as they are signed by the remote peer
351
- if (message.signedPeerRecord != null) {
352
- this.log('received signedPeerRecord from %p', connection.remotePeer);
353
- let peerRecordEnvelope = message.signedPeerRecord;
354
- const envelope = await RecordEnvelope.openAndCertify(peerRecordEnvelope, PeerRecord.DOMAIN);
355
- let peerRecord = PeerRecord.createFromProtobuf(envelope.payload);
356
- // Verify peerId
357
- if (!peerRecord.peerId.equals(envelope.peerId)) {
358
- throw new CodeError('signing key does not match PeerId in the PeerRecord', 'ERR_INVALID_SIGNING_KEY');
359
- }
360
- // Make sure remote peer is the one sending the record
361
- if (!connection.remotePeer.equals(peerRecord.peerId)) {
362
- throw new CodeError('signing key does not match remote PeerId', 'ERR_INVALID_PEER_RECORD_KEY');
363
- }
364
- let existingPeer;
365
- try {
366
- existingPeer = await this.peerStore.get(peerRecord.peerId);
367
- }
368
- catch (err) {
369
- if (err.code !== 'ERR_NOT_FOUND') {
370
- throw err;
371
- }
372
- }
373
- if (existingPeer != null) {
374
- // don't lose any existing metadata
375
- peer.metadata = existingPeer.metadata;
376
- // if we have previously received a signed record for this peer, compare it to the incoming one
377
- if (existingPeer.peerRecordEnvelope != null) {
378
- const storedEnvelope = await RecordEnvelope.createFromProtobuf(existingPeer.peerRecordEnvelope);
379
- const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload);
380
- // ensure seq is greater than, or equal to, the last received
381
- if (storedRecord.seqNumber >= peerRecord.seqNumber) {
382
- this.log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber);
383
- peerRecord = storedRecord;
384
- peerRecordEnvelope = existingPeer.peerRecordEnvelope;
385
- }
386
- }
387
- }
388
- // store the signed record for next time
389
- peer.peerRecordEnvelope = peerRecordEnvelope;
390
- // override the stored addresses with the signed multiaddrs
391
- peer.addresses = peerRecord.multiaddrs.map(multiaddr => ({
392
- isCertified: true,
393
- multiaddr
394
- }));
395
- output = {
396
- seq: peerRecord.seqNumber,
397
- addresses: peerRecord.multiaddrs
398
- };
399
- }
400
- else {
401
- this.log('%p did not send a signed peer record', connection.remotePeer);
402
- }
403
- this.log('patching %p with', connection.remotePeer, peer);
404
- await this.peerStore.patch(connection.remotePeer, peer);
405
- if (message.agentVersion != null || message.protocolVersion != null) {
406
- const metadata = {};
407
- if (message.agentVersion != null) {
408
- metadata.AgentVersion = uint8ArrayFromString(message.agentVersion);
409
- }
410
- if (message.protocolVersion != null) {
411
- metadata.ProtocolVersion = uint8ArrayFromString(message.protocolVersion);
412
- }
413
- this.log('merging %p metadata', connection.remotePeer, metadata);
414
- await this.peerStore.merge(connection.remotePeer, {
415
- metadata
416
- });
417
- }
418
- const result = {
419
- peerId: connection.remotePeer,
420
- protocolVersion: message.protocolVersion,
421
- agentVersion: message.agentVersion,
422
- publicKey: message.publicKey,
423
- listenAddrs: message.listenAddrs.map(buf => multiaddr(buf)),
424
- observedAddr: message.observedAddr == null ? undefined : multiaddr(message.observedAddr),
425
- protocols: message.protocols,
426
- signedPeerRecord: output,
427
- connection
428
- };
429
- this.events.safeDispatchEvent('peer:identify', { detail: result });
430
- return result;
431
- }
432
- }
433
- /**
434
- * Takes the `addr` and converts it to a Multiaddr if possible
435
- */
436
- function getCleanMultiaddr(addr) {
437
- if (addr != null && addr.length > 0) {
438
- try {
439
- return multiaddr(addr);
440
- }
441
- catch {
442
- }
443
- }
444
124
  }
445
125
  //# sourceMappingURL=identify.js.map