Skip to content

Commit f436ef9

Browse files
committed
chore: identify to protobufjs instead of protons
1 parent 55bfbe2 commit f436ef9

File tree

6 files changed

+472
-57
lines changed

6 files changed

+472
-57
lines changed

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020
"scripts": {
2121
"lint": "aegir lint",
2222
"build": "aegir build",
23-
"build:proto": "npm run build:proto:circuit && npm run build:proto:plaintext && npm run build:proto:address-book && npm run build:proto:proto-book && npm run build:proto:peer-record && npm run build:proto:envelope",
23+
"build:proto": "npm run build:proto:circuit && npm run build:proto:identify && npm run build:proto:plaintext && npm run build:proto:address-book && npm run build:proto:proto-book && npm run build:proto:peer-record && npm run build:proto:envelope",
2424
"build:proto:circuit": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/circuit/protocol/index.js ./src/circuit/protocol/index.proto",
25+
"build:proto:identify": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/identify/message.js ./src/identify/message.proto",
2526
"build:proto:plaintext": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/insecure/proto.js ./src/insecure/proto.proto",
2627
"build:proto:address-book": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/peer-store/persistent/pb/address-book.js ./src/peer-store/persistent/pb/address-book.proto",
2728
"build:proto:proto-book": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/peer-store/persistent/pb/proto-book.js ./src/peer-store/persistent/pb/proto-book.proto",
2829
"build:proto:peer-record": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/peer-record/peer-record.js ./src/record/peer-record/peer-record.proto",
2930
"build:proto:envelope": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/envelope/envelope.js ./src/record/envelope/envelope.proto",
30-
"build:proto-types": "npm run build:proto-types:circuit && npm run build:proto-types:plaintext && npm run build:proto-types:address-book && npm run build:proto-types:proto-book && npm run build:proto-types:peer-record && npm run build:proto-types:envelope",
31+
"build:proto-types": "npm run build:proto-types:circuit && npm run build:proto-types:identify && npm run build:proto-types:plaintext && npm run build:proto-types:address-book && npm run build:proto-types:proto-book && npm run build:proto-types:peer-record && npm run build:proto-types:envelope",
3132
"build:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
33+
"build:proto-types:identify": "pbts -o src/identify/message.d.ts src/identify/message.js",
3234
"build:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
3335
"build:proto-types:address-book": "pbts -o src/peer-store/persistent/pb/address-book.d.ts src/peer-store/persistent/pb/address-book.js",
3436
"build:proto-types:proto-book": "pbts -o src/peer-store/persistent/pb/proto-book.d.ts src/peer-store/persistent/pb/proto-book.js",
@@ -100,7 +102,6 @@
100102
"it-map": "^1.0.4",
101103
"it-merge": "1.0.0",
102104
"it-pipe": "^1.1.0",
103-
"it-protocol-buffers": "^0.2.0",
104105
"it-take": "1.0.0",
105106
"libp2p-crypto": "^0.19.0",
106107
"libp2p-interfaces": "^0.10.0",
@@ -121,7 +122,6 @@
121122
"peer-id": "^0.14.2",
122123
"private-ip": "^2.1.0",
123124
"protobufjs": "^6.10.2",
124-
"protons": "^2.0.0",
125125
"retimer": "^3.0.0",
126126
"sanitize-filename": "^1.6.3",
127127
"set-delayed-interval": "^1.0.0",
@@ -148,8 +148,8 @@
148148
"it-pushable": "^1.4.0",
149149
"libp2p": ".",
150150
"libp2p-bootstrap": "^0.12.0",
151-
"libp2p-delegated-content-routing": "^0.9.0",
152-
"libp2p-delegated-peer-routing": "^0.8.0",
151+
"libp2p-delegated-content-routing": "^0.10.0",
152+
"libp2p-delegated-peer-routing": "^0.9.0",
153153
"libp2p-floodsub": "libp2p/js-libp2p-floodsub#chore/update-deps-and-remove-protons",
154154
"libp2p-gossipsub": "^0.8.0",
155155
"libp2p-kad-dht": "^0.21.0",

src/identify/index.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const log = Object.assign(debug('libp2p:identify'), {
55
error: debug('libp2p:identify:err')
66
})
77
const errCode = require('err-code')
8-
// @ts-ignore it-protocol-buffers does not have types
9-
const pb = require('it-protocol-buffers')
108
const lp = require('it-length-prefixed')
119
const { pipe } = require('it-pipe')
1210
const { collect, take, consume } = require('streaming-iterables')
@@ -99,12 +97,12 @@ class IdentifyService {
9997
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
10098

10199
await pipe(
102-
[{
100+
[Message.Identify.encode({
103101
listenAddrs,
104102
signedPeerRecord,
105103
protocols
106-
}],
107-
pb.encode(Message),
104+
}).finish()],
105+
lp.encode(),
108106
stream,
109107
consume
110108
)
@@ -165,12 +163,12 @@ class IdentifyService {
165163

166164
let message
167165
try {
168-
message = Message.decode(data)
166+
message = Message.Identify.decode(data)
169167
} catch (err) {
170168
throw errCode(err, codes.ERR_INVALID_MESSAGE)
171169
}
172170

173-
let {
171+
const {
174172
publicKey,
175173
listenAddrs,
176174
protocols,
@@ -185,7 +183,7 @@ class IdentifyService {
185183
}
186184

187185
// Get the observedAddr if there is one
188-
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
186+
const cleanObservedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
189187

190188
try {
191189
const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
@@ -199,7 +197,7 @@ class IdentifyService {
199197

200198
// LEGACY: Update peers data in PeerStore
201199
try {
202-
this.peerStore.addressBook.set(id, listenAddrs.map((/** @type {string} */ addr) => new Multiaddr(addr)))
200+
this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
203201
} catch (err) {
204202
log.error('received invalid addrs', err)
205203
}
@@ -208,7 +206,7 @@ class IdentifyService {
208206
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
209207

210208
// TODO: Add and score our observed addr
211-
log('received observed address of %s', observedAddr)
209+
log('received observed address of %s', cleanObservedAddr)
212210
// this.addressManager.addObservedAddr(observedAddr)
213211
}
214212

@@ -251,15 +249,15 @@ class IdentifyService {
251249
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
252250
const protocols = this.peerStore.protoBook.get(this.peerId) || []
253251

254-
const message = Message.encode({
252+
const message = Message.Identify.encode({
255253
protocolVersion: this._host.protocolVersion,
256254
agentVersion: this._host.agentVersion,
257255
publicKey,
258256
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
259257
signedPeerRecord,
260258
observedAddr: connection.remoteAddr.bytes,
261259
protocols
262-
})
260+
}).finish()
263261

264262
try {
265263
await pipe(
@@ -293,7 +291,7 @@ class IdentifyService {
293291
toBuffer,
294292
collect
295293
)
296-
message = Message.decode(data)
294+
message = Message.Identify.decode(data)
297295
} catch (err) {
298296
return log.error('received invalid message', err)
299297
}
@@ -313,7 +311,7 @@ class IdentifyService {
313311
// LEGACY: Update peers data in PeerStore
314312
try {
315313
this.peerStore.addressBook.set(id,
316-
message.listenAddrs.map((/** @type {string} */ addr) => new Multiaddr(addr)))
314+
message.listenAddrs.map((addr) => new Multiaddr(addr)))
317315
} catch (err) {
318316
log.error('received invalid addrs', err)
319317
}

src/identify/message.d.ts

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as $protobuf from "protobufjs";
2+
/** Properties of an Identify. */
3+
export interface IIdentify {
4+
5+
/** Identify protocolVersion */
6+
protocolVersion?: (string|null);
7+
8+
/** Identify agentVersion */
9+
agentVersion?: (string|null);
10+
11+
/** Identify publicKey */
12+
publicKey?: (Uint8Array|null);
13+
14+
/** Identify listenAddrs */
15+
listenAddrs?: (Uint8Array[]|null);
16+
17+
/** Identify observedAddr */
18+
observedAddr?: (Uint8Array|null);
19+
20+
/** Identify protocols */
21+
protocols?: (string[]|null);
22+
23+
/** Identify signedPeerRecord */
24+
signedPeerRecord?: (Uint8Array|null);
25+
}
26+
27+
/** Represents an Identify. */
28+
export class Identify implements IIdentify {
29+
30+
/**
31+
* Constructs a new Identify.
32+
* @param [p] Properties to set
33+
*/
34+
constructor(p?: IIdentify);
35+
36+
/** Identify protocolVersion. */
37+
public protocolVersion: string;
38+
39+
/** Identify agentVersion. */
40+
public agentVersion: string;
41+
42+
/** Identify publicKey. */
43+
public publicKey: Uint8Array;
44+
45+
/** Identify listenAddrs. */
46+
public listenAddrs: Uint8Array[];
47+
48+
/** Identify observedAddr. */
49+
public observedAddr: Uint8Array;
50+
51+
/** Identify protocols. */
52+
public protocols: string[];
53+
54+
/** Identify signedPeerRecord. */
55+
public signedPeerRecord: Uint8Array;
56+
57+
/**
58+
* Encodes the specified Identify message. Does not implicitly {@link Identify.verify|verify} messages.
59+
* @param m Identify message or plain object to encode
60+
* @param [w] Writer to encode to
61+
* @returns Writer
62+
*/
63+
public static encode(m: IIdentify, w?: $protobuf.Writer): $protobuf.Writer;
64+
65+
/**
66+
* Decodes an Identify message from the specified reader or buffer.
67+
* @param r Reader or buffer to decode from
68+
* @param [l] Message length if known beforehand
69+
* @returns Identify
70+
* @throws {Error} If the payload is not a reader or valid buffer
71+
* @throws {$protobuf.util.ProtocolError} If required fields are missing
72+
*/
73+
public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Identify;
74+
75+
/**
76+
* Creates an Identify message from a plain object. Also converts values to their respective internal types.
77+
* @param d Plain object
78+
* @returns Identify
79+
*/
80+
public static fromObject(d: { [k: string]: any }): Identify;
81+
82+
/**
83+
* Creates a plain object from an Identify message. Also converts values to other types if specified.
84+
* @param m Identify
85+
* @param [o] Conversion options
86+
* @returns Plain object
87+
*/
88+
public static toObject(m: Identify, o?: $protobuf.IConversionOptions): { [k: string]: any };
89+
90+
/**
91+
* Converts this Identify to JSON.
92+
* @returns JSON object
93+
*/
94+
public toJSON(): { [k: string]: any };
95+
}

0 commit comments

Comments
 (0)