Skip to content

Commit

Permalink
chore: identify to protobufjs instead of protons
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 13, 2021
1 parent 55bfbe2 commit 5159d23
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 61 deletions.
2 changes: 1 addition & 1 deletion examples/libp2p-in-the-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"libp2p-mplex": "^0.10.0",
"libp2p-noise": "^2.0.0",
"libp2p-webrtc-star": "^0.20.0",
"libp2p-websockets": "^0.14.0"
"libp2p-websockets": "^0.15.0"
},
"devDependencies": {
"@babel/cli": "^7.13.10",
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"build:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
"build:proto-types:identify": "pbts -o src/identify/message.d.ts src/identify/message.js",
"build:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
"build:proto-types:address-book": "pbts -o src/peer-store/persistent/pb/address-book.d.ts src/peer-store/persistent/pb/address-book.js",
"build:proto-types:proto-book": "pbts -o src/peer-store/persistent/pb/proto-book.d.ts src/peer-store/persistent/pb/proto-book.js",
Expand Down Expand Up @@ -100,7 +102,6 @@
"it-map": "^1.0.4",
"it-merge": "1.0.0",
"it-pipe": "^1.1.0",
"it-protocol-buffers": "^0.2.0",
"it-take": "1.0.0",
"libp2p-crypto": "^0.19.0",
"libp2p-interfaces": "^0.10.0",
Expand All @@ -121,7 +122,6 @@
"peer-id": "^0.14.2",
"private-ip": "^2.1.0",
"protobufjs": "^6.10.2",
"protons": "^2.0.0",
"retimer": "^3.0.0",
"sanitize-filename": "^1.6.3",
"set-delayed-interval": "^1.0.0",
Expand All @@ -147,13 +147,13 @@
"it-pair": "^1.0.0",
"it-pushable": "^1.4.0",
"libp2p": ".",
"libp2p-bootstrap": "^0.12.0",
"libp2p-delegated-content-routing": "^0.9.0",
"libp2p-delegated-peer-routing": "^0.8.0",
"libp2p-floodsub": "libp2p/js-libp2p-floodsub#chore/update-deps-and-remove-protons",
"libp2p-bootstrap": "^0.12.3",
"libp2p-delegated-content-routing": "^0.10.0",
"libp2p-delegated-peer-routing": "^0.9.0",
"libp2p-floodsub": "^0.25.0",
"libp2p-gossipsub": "^0.8.0",
"libp2p-kad-dht": "^0.21.0",
"libp2p-mdns": "^0.15.0",
"libp2p-mdns": "^0.16.0",
"libp2p-mplex": "^0.10.1",
"libp2p-noise": "^2.0.0",
"libp2p-tcp": "^0.15.1",
Expand Down
26 changes: 12 additions & 14 deletions src/identify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const log = Object.assign(debug('libp2p:identify'), {
error: debug('libp2p:identify:err')
})
const errCode = require('err-code')
// @ts-ignore it-protocol-buffers does not have types
const pb = require('it-protocol-buffers')
const lp = require('it-length-prefixed')
const { pipe } = require('it-pipe')
const { collect, take, consume } = require('streaming-iterables')
Expand Down Expand Up @@ -99,12 +97,12 @@ class IdentifyService {
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)

await pipe(
[{
[Message.Identify.encode({
listenAddrs,
signedPeerRecord,
protocols
}],
pb.encode(Message),
}).finish()],
lp.encode(),
stream,
consume
)
Expand Down Expand Up @@ -165,12 +163,12 @@ class IdentifyService {

let message
try {
message = Message.decode(data)
message = Message.Identify.decode(data)
} catch (err) {
throw errCode(err, codes.ERR_INVALID_MESSAGE)
}

let {
const {
publicKey,
listenAddrs,
protocols,
Expand All @@ -185,7 +183,7 @@ class IdentifyService {
}

// Get the observedAddr if there is one
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
const cleanObservedAddr = IdentifyService.getCleanMultiaddr(observedAddr)

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

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

// TODO: Add and score our observed addr
log('received observed address of %s', observedAddr)
log('received observed address of %s', cleanObservedAddr)
// this.addressManager.addObservedAddr(observedAddr)
}

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

const message = Message.encode({
const message = Message.Identify.encode({
protocolVersion: this._host.protocolVersion,
agentVersion: this._host.agentVersion,
publicKey,
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
signedPeerRecord,
observedAddr: connection.remoteAddr.bytes,
protocols
})
}).finish()

try {
await pipe(
Expand Down Expand Up @@ -293,7 +291,7 @@ class IdentifyService {
toBuffer,
collect
)
message = Message.decode(data)
message = Message.Identify.decode(data)
} catch (err) {
return log.error('received invalid message', err)
}
Expand All @@ -313,7 +311,7 @@ class IdentifyService {
// LEGACY: Update peers data in PeerStore
try {
this.peerStore.addressBook.set(id,
message.listenAddrs.map((/** @type {string} */ addr) => new Multiaddr(addr)))
message.listenAddrs.map((addr) => new Multiaddr(addr)))
} catch (err) {
log.error('received invalid addrs', err)
}
Expand Down
95 changes: 95 additions & 0 deletions src/identify/message.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as $protobuf from "protobufjs";
/** Properties of an Identify. */
export interface IIdentify {

/** Identify protocolVersion */
protocolVersion?: (string|null);

/** Identify agentVersion */
agentVersion?: (string|null);

/** Identify publicKey */
publicKey?: (Uint8Array|null);

/** Identify listenAddrs */
listenAddrs?: (Uint8Array[]|null);

/** Identify observedAddr */
observedAddr?: (Uint8Array|null);

/** Identify protocols */
protocols?: (string[]|null);

/** Identify signedPeerRecord */
signedPeerRecord?: (Uint8Array|null);
}

/** Represents an Identify. */
export class Identify implements IIdentify {

/**
* Constructs a new Identify.
* @param [p] Properties to set
*/
constructor(p?: IIdentify);

/** Identify protocolVersion. */
public protocolVersion: string;

/** Identify agentVersion. */
public agentVersion: string;

/** Identify publicKey. */
public publicKey: Uint8Array;

/** Identify listenAddrs. */
public listenAddrs: Uint8Array[];

/** Identify observedAddr. */
public observedAddr: Uint8Array;

/** Identify protocols. */
public protocols: string[];

/** Identify signedPeerRecord. */
public signedPeerRecord: Uint8Array;

/**
* Encodes the specified Identify message. Does not implicitly {@link Identify.verify|verify} messages.
* @param m Identify message or plain object to encode
* @param [w] Writer to encode to
* @returns Writer
*/
public static encode(m: IIdentify, w?: $protobuf.Writer): $protobuf.Writer;

/**
* Decodes an Identify message from the specified reader or buffer.
* @param r Reader or buffer to decode from
* @param [l] Message length if known beforehand
* @returns Identify
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Identify;

/**
* Creates an Identify message from a plain object. Also converts values to their respective internal types.
* @param d Plain object
* @returns Identify
*/
public static fromObject(d: { [k: string]: any }): Identify;

/**
* Creates a plain object from an Identify message. Also converts values to other types if specified.
* @param m Identify
* @param [o] Conversion options
* @returns Plain object
*/
public static toObject(m: Identify, o?: $protobuf.IConversionOptions): { [k: string]: any };

/**
* Converts this Identify to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
}
Loading

0 comments on commit 5159d23

Please sign in to comment.