@libp2p/identify 1.0.21-9d13a2f6a → 1.0.21
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/README.md +1 -24
- package/dist/index.min.js +3 -3
- package/dist/src/consts.d.ts +0 -2
- package/dist/src/consts.d.ts.map +1 -1
- package/dist/src/consts.js +0 -4
- package/dist/src/consts.js.map +1 -1
- package/dist/src/identify.d.ts +40 -3
- package/dist/src/identify.d.ts.map +1 -1
- package/dist/src/identify.js +334 -14
- package/dist/src/identify.js.map +1 -1
- package/dist/src/index.d.ts +17 -75
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -28
- package/dist/src/index.js.map +1 -1
- package/dist/typedoc-urls.json +12 -0
- package/package.json +9 -10
- package/src/consts.ts +0 -6
- package/src/identify.ts +404 -16
- package/src/index.ts +20 -81
- package/dist/src/identify-push.d.ts +0 -18
- package/dist/src/identify-push.d.ts.map +0 -1
- package/dist/src/identify-push.js +0 -120
- package/dist/src/identify-push.js.map +0 -1
- package/dist/src/utils.d.ts +0 -53
- package/dist/src/utils.d.ts.map +0 -1
- package/dist/src/utils.js +0 -217
- package/dist/src/utils.js.map +0 -1
- package/src/identify-push.ts +0 -146
- package/src/utils.ts +0 -273
|
@@ -1,120 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,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AC,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"}
|
package/dist/src/utils.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { type Multiaddr } from '@multiformats/multiaddr';
|
|
2
|
-
import type { IdentifyComponents, IdentifyInit } from './index.js';
|
|
3
|
-
import type { Identify as IdentifyMessage } from './pb/message.js';
|
|
4
|
-
import type { Libp2pEvents, IdentifyResult, Logger, Connection, TypedEventTarget, PeerStore, NodeInfo, Startable, PeerId, IncomingStreamData } from '@libp2p/interface';
|
|
5
|
-
import type { AddressManager, Registrar } from '@libp2p/interface-internal';
|
|
6
|
-
export declare const defaultValues: {
|
|
7
|
-
protocolPrefix: string;
|
|
8
|
-
timeout: number;
|
|
9
|
-
maxInboundStreams: number;
|
|
10
|
-
maxOutboundStreams: number;
|
|
11
|
-
maxObservedAddresses: number;
|
|
12
|
-
maxMessageSize: number;
|
|
13
|
-
runOnConnectionOpen: boolean;
|
|
14
|
-
runOnSelfUpdate: boolean;
|
|
15
|
-
runOnTransientConnection: boolean;
|
|
16
|
-
concurrency: number;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Takes the `addr` and converts it to a Multiaddr if possible
|
|
20
|
-
*/
|
|
21
|
-
export declare function getCleanMultiaddr(addr: Uint8Array | string | null | undefined): Multiaddr | undefined;
|
|
22
|
-
export declare function getAgentVersion(nodeInfo: NodeInfo, agentVersion?: string): string;
|
|
23
|
-
export declare function consumeIdentifyMessage(peerStore: PeerStore, events: TypedEventTarget<Libp2pEvents>, log: Logger, connection: Connection, message: IdentifyMessage): Promise<IdentifyResult>;
|
|
24
|
-
export interface AbstractIdentifyInit extends IdentifyInit {
|
|
25
|
-
protocol: string;
|
|
26
|
-
log: Logger;
|
|
27
|
-
}
|
|
28
|
-
export declare abstract class AbstractIdentify implements Startable {
|
|
29
|
-
readonly host: {
|
|
30
|
-
protocolVersion: string;
|
|
31
|
-
agentVersion: string;
|
|
32
|
-
};
|
|
33
|
-
protected protocol: string;
|
|
34
|
-
protected started: boolean;
|
|
35
|
-
protected readonly timeout: number;
|
|
36
|
-
protected readonly peerId: PeerId;
|
|
37
|
-
protected readonly peerStore: PeerStore;
|
|
38
|
-
protected readonly registrar: Registrar;
|
|
39
|
-
protected readonly addressManager: AddressManager;
|
|
40
|
-
private readonly maxInboundStreams;
|
|
41
|
-
private readonly maxOutboundStreams;
|
|
42
|
-
protected readonly maxMessageSize: number;
|
|
43
|
-
protected readonly maxObservedAddresses: number;
|
|
44
|
-
protected readonly events: TypedEventTarget<Libp2pEvents>;
|
|
45
|
-
protected readonly runOnTransientConnection: boolean;
|
|
46
|
-
protected readonly log: Logger;
|
|
47
|
-
constructor(components: IdentifyComponents, init: AbstractIdentifyInit);
|
|
48
|
-
isStarted(): boolean;
|
|
49
|
-
start(): Promise<void>;
|
|
50
|
-
stop(): Promise<void>;
|
|
51
|
-
protected abstract handleProtocol(data: IncomingStreamData): Promise<void>;
|
|
52
|
-
}
|
|
53
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/dist/src/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,yBAAyB,CAAA;AAInE,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAClE,OAAO,KAAK,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAClE,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAoB,MAAM,EAAE,UAAU,EAAE,gBAAgB,EAAkB,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACzM,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAE3E,eAAO,MAAM,aAAa;;;;;;;;;;;CAWzB,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAQtG;AAED,wBAAgB,eAAe,CAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAclF;AAED,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AAI,EAAE,oBAAoB;IAwBvE,SAAS,IAAK,OAAO;IAIf,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAyBvB,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAM5B,SAAS,CAAC,QAAQ,CAAC,cAAc,CAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;CAC5E"}
|
package/dist/src/utils.js
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface';
|
|
2
|
-
import { peerIdFromKeys } from '@libp2p/peer-id';
|
|
3
|
-
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record';
|
|
4
|
-
import { multiaddr } from '@multiformats/multiaddr';
|
|
5
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
6
|
-
import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe';
|
|
7
|
-
import { IDENTIFY_PROTOCOL_VERSION, MAX_IDENTIFY_MESSAGE_SIZE, MAX_PUSH_CONCURRENCY } from './consts.js';
|
|
8
|
-
export const defaultValues = {
|
|
9
|
-
protocolPrefix: 'ipfs',
|
|
10
|
-
timeout: 5000,
|
|
11
|
-
maxInboundStreams: 1,
|
|
12
|
-
maxOutboundStreams: 1,
|
|
13
|
-
maxObservedAddresses: 10,
|
|
14
|
-
maxMessageSize: MAX_IDENTIFY_MESSAGE_SIZE,
|
|
15
|
-
runOnConnectionOpen: true,
|
|
16
|
-
runOnSelfUpdate: true,
|
|
17
|
-
runOnTransientConnection: true,
|
|
18
|
-
concurrency: MAX_PUSH_CONCURRENCY
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Takes the `addr` and converts it to a Multiaddr if possible
|
|
22
|
-
*/
|
|
23
|
-
export function getCleanMultiaddr(addr) {
|
|
24
|
-
if (addr != null && addr.length > 0) {
|
|
25
|
-
try {
|
|
26
|
-
return multiaddr(addr);
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export function getAgentVersion(nodeInfo, agentVersion) {
|
|
33
|
-
if (agentVersion != null) {
|
|
34
|
-
return agentVersion;
|
|
35
|
-
}
|
|
36
|
-
agentVersion = `${nodeInfo.name}/${nodeInfo.version}`;
|
|
37
|
-
// Append user agent version to default AGENT_VERSION depending on the environment
|
|
38
|
-
if (isNode || isElectronMain) {
|
|
39
|
-
agentVersion += ` UserAgent=${globalThis.process.version}`;
|
|
40
|
-
}
|
|
41
|
-
else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
|
|
42
|
-
agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`;
|
|
43
|
-
}
|
|
44
|
-
return agentVersion;
|
|
45
|
-
}
|
|
46
|
-
export async function consumeIdentifyMessage(peerStore, events, log, connection, message) {
|
|
47
|
-
log('received identify from %p', connection.remotePeer);
|
|
48
|
-
if (message == null) {
|
|
49
|
-
throw new CodeError('message was null or undefined', 'ERR_INVALID_MESSAGE');
|
|
50
|
-
}
|
|
51
|
-
const peer = {};
|
|
52
|
-
if (message.listenAddrs.length > 0) {
|
|
53
|
-
peer.addresses = message.listenAddrs.map(buf => ({
|
|
54
|
-
isCertified: false,
|
|
55
|
-
multiaddr: multiaddr(buf)
|
|
56
|
-
}));
|
|
57
|
-
}
|
|
58
|
-
if (message.protocols.length > 0) {
|
|
59
|
-
peer.protocols = message.protocols;
|
|
60
|
-
}
|
|
61
|
-
if (message.publicKey != null) {
|
|
62
|
-
peer.publicKey = message.publicKey;
|
|
63
|
-
const peerId = await peerIdFromKeys(message.publicKey);
|
|
64
|
-
if (!peerId.equals(connection.remotePeer)) {
|
|
65
|
-
throw new CodeError('public key did not match remote PeerId', 'ERR_INVALID_PUBLIC_KEY');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
let output;
|
|
69
|
-
// if the peer record has been sent, prefer the addresses in the record as they are signed by the remote peer
|
|
70
|
-
if (message.signedPeerRecord != null) {
|
|
71
|
-
log('received signedPeerRecord from %p', connection.remotePeer);
|
|
72
|
-
let peerRecordEnvelope = message.signedPeerRecord;
|
|
73
|
-
const envelope = await RecordEnvelope.openAndCertify(peerRecordEnvelope, PeerRecord.DOMAIN);
|
|
74
|
-
let peerRecord = PeerRecord.createFromProtobuf(envelope.payload);
|
|
75
|
-
// Verify peerId
|
|
76
|
-
if (!peerRecord.peerId.equals(envelope.peerId)) {
|
|
77
|
-
throw new CodeError('signing key does not match PeerId in the PeerRecord', 'ERR_INVALID_SIGNING_KEY');
|
|
78
|
-
}
|
|
79
|
-
// Make sure remote peer is the one sending the record
|
|
80
|
-
if (!connection.remotePeer.equals(peerRecord.peerId)) {
|
|
81
|
-
throw new CodeError('signing key does not match remote PeerId', 'ERR_INVALID_PEER_RECORD_KEY');
|
|
82
|
-
}
|
|
83
|
-
let existingPeer;
|
|
84
|
-
try {
|
|
85
|
-
existingPeer = await peerStore.get(peerRecord.peerId);
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
if (err.code !== 'ERR_NOT_FOUND') {
|
|
89
|
-
throw err;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (existingPeer != null) {
|
|
93
|
-
// don't lose any existing metadata
|
|
94
|
-
peer.metadata = existingPeer.metadata;
|
|
95
|
-
// if we have previously received a signed record for this peer, compare it to the incoming one
|
|
96
|
-
if (existingPeer.peerRecordEnvelope != null) {
|
|
97
|
-
const storedEnvelope = await RecordEnvelope.createFromProtobuf(existingPeer.peerRecordEnvelope);
|
|
98
|
-
const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload);
|
|
99
|
-
// ensure seq is greater than, or equal to, the last received
|
|
100
|
-
if (storedRecord.seqNumber >= peerRecord.seqNumber) {
|
|
101
|
-
log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber);
|
|
102
|
-
peerRecord = storedRecord;
|
|
103
|
-
peerRecordEnvelope = existingPeer.peerRecordEnvelope;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
// store the signed record for next time
|
|
108
|
-
peer.peerRecordEnvelope = peerRecordEnvelope;
|
|
109
|
-
// override the stored addresses with the signed multiaddrs
|
|
110
|
-
peer.addresses = peerRecord.multiaddrs.map(multiaddr => ({
|
|
111
|
-
isCertified: true,
|
|
112
|
-
multiaddr
|
|
113
|
-
}));
|
|
114
|
-
output = {
|
|
115
|
-
seq: peerRecord.seqNumber,
|
|
116
|
-
addresses: peerRecord.multiaddrs
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
log('%p did not send a signed peer record', connection.remotePeer);
|
|
121
|
-
}
|
|
122
|
-
log('patching %p with', connection.remotePeer, peer);
|
|
123
|
-
await peerStore.patch(connection.remotePeer, peer);
|
|
124
|
-
if (message.agentVersion != null || message.protocolVersion != null) {
|
|
125
|
-
const metadata = {};
|
|
126
|
-
if (message.agentVersion != null) {
|
|
127
|
-
metadata.AgentVersion = uint8ArrayFromString(message.agentVersion);
|
|
128
|
-
}
|
|
129
|
-
if (message.protocolVersion != null) {
|
|
130
|
-
metadata.ProtocolVersion = uint8ArrayFromString(message.protocolVersion);
|
|
131
|
-
}
|
|
132
|
-
log('merging %p metadata', connection.remotePeer, metadata);
|
|
133
|
-
await peerStore.merge(connection.remotePeer, {
|
|
134
|
-
metadata
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
const result = {
|
|
138
|
-
peerId: connection.remotePeer,
|
|
139
|
-
protocolVersion: message.protocolVersion,
|
|
140
|
-
agentVersion: message.agentVersion,
|
|
141
|
-
publicKey: message.publicKey,
|
|
142
|
-
listenAddrs: message.listenAddrs.map(buf => multiaddr(buf)),
|
|
143
|
-
observedAddr: message.observedAddr == null ? undefined : multiaddr(message.observedAddr),
|
|
144
|
-
protocols: message.protocols,
|
|
145
|
-
signedPeerRecord: output,
|
|
146
|
-
connection
|
|
147
|
-
};
|
|
148
|
-
events.safeDispatchEvent('peer:identify', { detail: result });
|
|
149
|
-
return result;
|
|
150
|
-
}
|
|
151
|
-
export class AbstractIdentify {
|
|
152
|
-
host;
|
|
153
|
-
protocol;
|
|
154
|
-
started;
|
|
155
|
-
timeout;
|
|
156
|
-
peerId;
|
|
157
|
-
peerStore;
|
|
158
|
-
registrar;
|
|
159
|
-
addressManager;
|
|
160
|
-
maxInboundStreams;
|
|
161
|
-
maxOutboundStreams;
|
|
162
|
-
maxMessageSize;
|
|
163
|
-
maxObservedAddresses;
|
|
164
|
-
events;
|
|
165
|
-
runOnTransientConnection;
|
|
166
|
-
log;
|
|
167
|
-
constructor(components, init) {
|
|
168
|
-
this.protocol = init.protocol;
|
|
169
|
-
this.started = false;
|
|
170
|
-
this.peerId = components.peerId;
|
|
171
|
-
this.peerStore = components.peerStore;
|
|
172
|
-
this.registrar = components.registrar;
|
|
173
|
-
this.addressManager = components.addressManager;
|
|
174
|
-
this.events = components.events;
|
|
175
|
-
this.log = init.log;
|
|
176
|
-
this.timeout = init.timeout ?? defaultValues.timeout;
|
|
177
|
-
this.maxInboundStreams = init.maxInboundStreams ?? defaultValues.maxInboundStreams;
|
|
178
|
-
this.maxOutboundStreams = init.maxOutboundStreams ?? defaultValues.maxOutboundStreams;
|
|
179
|
-
this.maxMessageSize = init.maxMessageSize ?? defaultValues.maxMessageSize;
|
|
180
|
-
this.maxObservedAddresses = init.maxObservedAddresses ?? defaultValues.maxObservedAddresses;
|
|
181
|
-
this.runOnTransientConnection = init.runOnTransientConnection ?? defaultValues.runOnTransientConnection;
|
|
182
|
-
// Store self host metadata
|
|
183
|
-
this.host = {
|
|
184
|
-
protocolVersion: `${init.protocolPrefix ?? defaultValues.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
|
|
185
|
-
agentVersion: getAgentVersion(components.nodeInfo, init.agentVersion)
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
isStarted() {
|
|
189
|
-
return this.started;
|
|
190
|
-
}
|
|
191
|
-
async start() {
|
|
192
|
-
if (this.started) {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
await this.peerStore.merge(this.peerId, {
|
|
196
|
-
metadata: {
|
|
197
|
-
AgentVersion: uint8ArrayFromString(this.host.agentVersion),
|
|
198
|
-
ProtocolVersion: uint8ArrayFromString(this.host.protocolVersion)
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
await this.registrar.handle(this.protocol, (data) => {
|
|
202
|
-
void this.handleProtocol(data).catch(err => {
|
|
203
|
-
this.log.error(err);
|
|
204
|
-
});
|
|
205
|
-
}, {
|
|
206
|
-
maxInboundStreams: this.maxInboundStreams,
|
|
207
|
-
maxOutboundStreams: this.maxOutboundStreams,
|
|
208
|
-
runOnTransientConnection: this.runOnTransientConnection
|
|
209
|
-
});
|
|
210
|
-
this.started = true;
|
|
211
|
-
}
|
|
212
|
-
async stop() {
|
|
213
|
-
await this.registrar.unhandle(this.protocol);
|
|
214
|
-
this.started = false;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/src/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChE,OAAO,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC9G,OAAO,EAAE,yBAAy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MAAM,EAAE;YACtC,QAAQ,EAAE;gBACR,YAAY,EAAE,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC;gBAC1D,eAAe,EAAE,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC;aACjE;SACF,CAAC,CAAA;QAEF,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE;YAClD,KAAK,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;gBACzC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACrB,CAAC,CAAC,CAAA;QACJ,CAAC,EAAE;YACD,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;YACzC,kBAAkB,EAAE,IAAI,CAAC,kBAAkB;YAC3C,wBAAwB,EAAE,IAAI,CAAC,wBAAwB;SACxD,CAAC,CAAA;QAEF,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;IACrB,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAE5C,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;IACtB,CAAC;CAGF"}
|
package/src/identify-push.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/* eslint-disable complexity */
|
|
2
|
-
|
|
3
|
-
import { setMaxListeners } from '@libp2p/interface'
|
|
4
|
-
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
5
|
-
import { protocols } from '@multiformats/multiaddr'
|
|
6
|
-
import drain from 'it-drain'
|
|
7
|
-
import parallel from 'it-parallel'
|
|
8
|
-
import { pbStream } from 'it-protobuf-stream'
|
|
9
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
10
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
11
|
-
import {
|
|
12
|
-
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
|
|
13
|
-
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
|
|
14
|
-
} from './consts.js'
|
|
15
|
-
import { Identify as IdentifyMessage } from './pb/message.js'
|
|
16
|
-
import { AbstractIdentify, consumeIdentifyMessage, defaultValues } from './utils.js'
|
|
17
|
-
import type { IdentifyPush as IdentifyPushInterface, IdentifyPushComponents, IdentifyPushInit } from './index.js'
|
|
18
|
-
import type { Stream, Startable } from '@libp2p/interface'
|
|
19
|
-
import type { ConnectionManager, IncomingStreamData } from '@libp2p/interface-internal'
|
|
20
|
-
|
|
21
|
-
export class IdentifyPush extends AbstractIdentify implements Startable, IdentifyPushInterface {
|
|
22
|
-
private readonly connectionManager: ConnectionManager
|
|
23
|
-
private readonly concurrency: number
|
|
24
|
-
|
|
25
|
-
constructor (components: IdentifyPushComponents, init: IdentifyPushInit = {}) {
|
|
26
|
-
super(components, {
|
|
27
|
-
...init,
|
|
28
|
-
protocol: `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`,
|
|
29
|
-
log: components.logger.forComponent('libp2p:identify-push')
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
this.connectionManager = components.connectionManager
|
|
33
|
-
this.concurrency = init.concurrency ?? defaultValues.concurrency
|
|
34
|
-
|
|
35
|
-
if ((init.runOnSelfUpdate ?? defaultValues.runOnSelfUpdate)) {
|
|
36
|
-
// When self peer record changes, trigger identify-push
|
|
37
|
-
components.events.addEventListener('self:peer:update', (evt) => {
|
|
38
|
-
void this.push().catch(err => { this.log.error(err) })
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Calls `push` on all peer connections
|
|
45
|
-
*/
|
|
46
|
-
async push (): Promise<void> {
|
|
47
|
-
// Do not try to push if we are not running
|
|
48
|
-
if (!this.isStarted()) {
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const listenAddresses = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
53
|
-
const peerRecord = new PeerRecord({
|
|
54
|
-
peerId: this.peerId,
|
|
55
|
-
multiaddrs: listenAddresses
|
|
56
|
-
})
|
|
57
|
-
const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.peerId)
|
|
58
|
-
const supportedProtocols = this.registrar.getProtocols()
|
|
59
|
-
const peer = await this.peerStore.get(this.peerId)
|
|
60
|
-
const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion))
|
|
61
|
-
const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') ?? uint8ArrayFromString(this.host.protocolVersion))
|
|
62
|
-
const self = this
|
|
63
|
-
|
|
64
|
-
async function * pushToConnections (): AsyncGenerator<() => Promise<void>> {
|
|
65
|
-
for (const connection of self.connectionManager.getConnections()) {
|
|
66
|
-
const peer = await self.peerStore.get(connection.remotePeer)
|
|
67
|
-
|
|
68
|
-
if (!peer.protocols.includes(self.protocol)) {
|
|
69
|
-
continue
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
yield async () => {
|
|
73
|
-
let stream: Stream | undefined
|
|
74
|
-
const signal = AbortSignal.timeout(self.timeout)
|
|
75
|
-
|
|
76
|
-
setMaxListeners(Infinity, signal)
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
stream = await connection.newStream(self.protocol, {
|
|
80
|
-
signal,
|
|
81
|
-
runOnTransientConnection: self.runOnTransientConnection
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
const pb = pbStream(stream, {
|
|
85
|
-
maxDataLength: self.maxMessageSize
|
|
86
|
-
}).pb(IdentifyMessage)
|
|
87
|
-
|
|
88
|
-
await pb.write({
|
|
89
|
-
listenAddrs: listenAddresses.map(ma => ma.bytes),
|
|
90
|
-
signedPeerRecord: signedPeerRecord.marshal(),
|
|
91
|
-
protocols: supportedProtocols,
|
|
92
|
-
agentVersion,
|
|
93
|
-
protocolVersion
|
|
94
|
-
}, {
|
|
95
|
-
signal
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
await stream.close({
|
|
99
|
-
signal
|
|
100
|
-
})
|
|
101
|
-
} catch (err: any) {
|
|
102
|
-
// Just log errors
|
|
103
|
-
self.log.error('could not push identify update to peer', err)
|
|
104
|
-
stream?.abort(err)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
await drain(parallel(pushToConnections(), {
|
|
111
|
-
concurrency: this.concurrency
|
|
112
|
-
}))
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Reads the Identify Push message from the given `connection`
|
|
117
|
-
*/
|
|
118
|
-
async handleProtocol (data: IncomingStreamData): Promise<void> {
|
|
119
|
-
const { connection, stream } = data
|
|
120
|
-
|
|
121
|
-
try {
|
|
122
|
-
if (this.peerId.equals(connection.remotePeer)) {
|
|
123
|
-
throw new Error('received push from ourselves?')
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const options = {
|
|
127
|
-
signal: AbortSignal.timeout(this.timeout)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const pb = pbStream(stream, {
|
|
131
|
-
maxDataLength: this.maxMessageSize
|
|
132
|
-
}).pb(IdentifyMessage)
|
|
133
|
-
|
|
134
|
-
const message = await pb.read(options)
|
|
135
|
-
await stream.close(options)
|
|
136
|
-
|
|
137
|
-
await consumeIdentifyMessage(this.peerStore, this.events, this.log, connection, message)
|
|
138
|
-
} catch (err: any) {
|
|
139
|
-
this.log.error('received invalid message', err)
|
|
140
|
-
stream.abort(err)
|
|
141
|
-
return
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
this.log('handled push from %p', connection.remotePeer)
|
|
145
|
-
}
|
|
146
|
-
}
|