@libp2p/peer-record 0.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.
Files changed (38) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +179 -0
  3. package/dist/src/envelope/envelope.d.ts +77 -0
  4. package/dist/src/envelope/envelope.js +241 -0
  5. package/dist/src/envelope/index.d.ts +47 -0
  6. package/dist/src/envelope/index.d.ts.map +1 -0
  7. package/dist/src/envelope/index.js +126 -0
  8. package/dist/src/envelope/index.js.map +1 -0
  9. package/dist/src/errors.d.ts +4 -0
  10. package/dist/src/errors.d.ts.map +1 -0
  11. package/dist/src/errors.js +4 -0
  12. package/dist/src/errors.js.map +1 -0
  13. package/dist/src/index.d.ts +3 -0
  14. package/dist/src/index.d.ts.map +1 -0
  15. package/dist/src/index.js +3 -0
  16. package/dist/src/index.js.map +1 -0
  17. package/dist/src/peer-record/consts.d.ts +3 -0
  18. package/dist/src/peer-record/consts.d.ts.map +1 -0
  19. package/dist/src/peer-record/consts.js +7 -0
  20. package/dist/src/peer-record/consts.js.map +1 -0
  21. package/dist/src/peer-record/index.d.ts +44 -0
  22. package/dist/src/peer-record/index.d.ts.map +1 -0
  23. package/dist/src/peer-record/index.js +71 -0
  24. package/dist/src/peer-record/index.js.map +1 -0
  25. package/dist/src/peer-record/peer-record.d.ts +133 -0
  26. package/dist/src/peer-record/peer-record.js +365 -0
  27. package/package.json +164 -0
  28. package/src/envelope/envelope.d.ts +77 -0
  29. package/src/envelope/envelope.js +241 -0
  30. package/src/envelope/envelope.proto +19 -0
  31. package/src/envelope/index.ts +159 -0
  32. package/src/errors.ts +4 -0
  33. package/src/index.ts +3 -0
  34. package/src/peer-record/consts.js +9 -0
  35. package/src/peer-record/index.ts +104 -0
  36. package/src/peer-record/peer-record.d.ts +133 -0
  37. package/src/peer-record/peer-record.js +365 -0
  38. package/src/peer-record/peer-record.proto +18 -0
@@ -0,0 +1,4 @@
1
+ export declare const codes: {
2
+ ERR_SIGNATURE_NOT_VALID: string;
3
+ };
4
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,KAAK;;CAEjB,CAAA"}
@@ -0,0 +1,4 @@
1
+ export const codes = {
2
+ ERR_SIGNATURE_NOT_VALID: 'ERR_SIGNATURE_NOT_VALID'
3
+ };
4
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,uBAAuB,EAAE,yBAAyB;CACnD,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { RecordEnvelope } from './envelope/index.js';
2
+ export { PeerRecord } from './peer-record/index.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { RecordEnvelope } from './envelope/index.js';
2
+ export { PeerRecord } from './peer-record/index.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export const ENVELOPE_DOMAIN_PEER_RECORD: "libp2p-peer-record";
2
+ export const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD: Uint8Array;
3
+ //# sourceMappingURL=consts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../../src/peer-record/consts.js"],"names":[],"mappings":"AAEA,+DAA+D;AAK/D,2DAAwE"}
@@ -0,0 +1,7 @@
1
+ // The domain string used for peer records contained in a Envelope.
2
+ export const ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record';
3
+ // The type hint used to identify peer records in a Envelope.
4
+ // Defined in https://github.com/multiformats/multicodec/blob/master/table.csv
5
+ // with name "libp2p-peer-record"
6
+ export const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = Uint8Array.from([3, 1]);
7
+ //# sourceMappingURL=consts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consts.js","sourceRoot":"","sources":["../../../src/peer-record/consts.js"],"names":[],"mappings":"AACA,mEAAmE;AACnE,MAAM,CAAC,MAAM,2BAA2B,GAAG,oBAAoB,CAAA;AAE/D,6DAA6D;AAC7D,8EAA8E;AAC9E,iCAAiC;AACjC,MAAM,CAAC,MAAM,iCAAiC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA"}
@@ -0,0 +1,44 @@
1
+ import { Multiaddr } from '@multiformats/multiaddr';
2
+ import { PeerId } from '@libp2p/peer-id';
3
+ export interface PeerRecordOptions {
4
+ peerId: PeerId;
5
+ /**
6
+ * Addresses of the associated peer.
7
+ */
8
+ multiaddrs?: Multiaddr[];
9
+ /**
10
+ * Monotonically-increasing sequence counter that's used to order PeerRecords in time.
11
+ */
12
+ seqNumber?: number;
13
+ }
14
+ /**
15
+ * The PeerRecord is used for distributing peer routing records across the network.
16
+ * It contains the peer's reachable listen addresses.
17
+ */
18
+ export declare class PeerRecord {
19
+ /**
20
+ * Unmarshal Peer Record Protobuf.
21
+ *
22
+ * @param {Uint8Array} buf - marshaled peer record.
23
+ * @returns {PeerRecord}
24
+ */
25
+ static createFromProtobuf: (buf: Uint8Array) => PeerRecord;
26
+ static DOMAIN: string;
27
+ static CODEC: Uint8Array;
28
+ peerId: PeerId;
29
+ multiaddrs: Multiaddr[];
30
+ seqNumber: number;
31
+ domain: string;
32
+ codec: Uint8Array;
33
+ private marshaled?;
34
+ constructor(options: PeerRecordOptions);
35
+ /**
36
+ * Marshal a record to be used in an envelope
37
+ */
38
+ marshal(): Uint8Array;
39
+ /**
40
+ * Returns true if `this` record equals the `other`
41
+ */
42
+ equals(other: unknown): boolean;
43
+ }
44
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/peer-record/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAQxC,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,UAAU,CAAC,EAAE,SAAS,EAAE,CAAA;IAExB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;GAGG;AACH,qBAAa,UAAU;IACrB;;;;;OAKG;IACH,MAAM,CAAC,kBAAkB,QAAS,UAAU,gBAO3C;IAED,MAAM,CAAC,MAAM,SAA8B;IAC3C,MAAM,CAAC,KAAK,aAAoC;IAEzC,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,SAAS,EAAE,CAAA;IACvB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,SAAoB;IAC1B,KAAK,aAAmB;IAC/B,OAAO,CAAC,SAAS,CAAC,CAAY;gBAEjB,OAAO,EAAE,iBAAiB;IAQvC;;OAEG;IACH,OAAO;IAcP;;OAEG;IACH,MAAM,CAAE,KAAK,EAAE,OAAO;CAsBvB"}
@@ -0,0 +1,71 @@
1
+ import { Multiaddr } from '@multiformats/multiaddr';
2
+ import { PeerId } from '@libp2p/peer-id';
3
+ import { arrayEquals } from '@libp2p/utils/array-equals';
4
+ import { PeerRecord as Protobuf } from './peer-record.js';
5
+ import { ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD } from './consts.js';
6
+ /**
7
+ * The PeerRecord is used for distributing peer routing records across the network.
8
+ * It contains the peer's reachable listen addresses.
9
+ */
10
+ export class PeerRecord {
11
+ constructor(options) {
12
+ this.domain = PeerRecord.DOMAIN;
13
+ this.codec = PeerRecord.CODEC;
14
+ const { peerId, multiaddrs, seqNumber } = options;
15
+ this.peerId = peerId;
16
+ this.multiaddrs = multiaddrs ?? [];
17
+ this.seqNumber = seqNumber ?? Date.now();
18
+ }
19
+ /**
20
+ * Marshal a record to be used in an envelope
21
+ */
22
+ marshal() {
23
+ if (this.marshaled == null) {
24
+ this.marshaled = Protobuf.encode({
25
+ peerId: this.peerId.toBytes(),
26
+ seq: this.seqNumber,
27
+ addresses: this.multiaddrs.map((m) => ({
28
+ multiaddr: m.bytes
29
+ }))
30
+ }).finish();
31
+ }
32
+ return this.marshaled;
33
+ }
34
+ /**
35
+ * Returns true if `this` record equals the `other`
36
+ */
37
+ equals(other) {
38
+ if (!(other instanceof PeerRecord)) {
39
+ return false;
40
+ }
41
+ // Validate PeerId
42
+ if (!this.peerId.equals(other.peerId)) {
43
+ return false;
44
+ }
45
+ // Validate seqNumber
46
+ if (this.seqNumber !== other.seqNumber) {
47
+ return false;
48
+ }
49
+ // Validate multiaddrs
50
+ if (!arrayEquals(this.multiaddrs, other.multiaddrs)) {
51
+ return false;
52
+ }
53
+ return true;
54
+ }
55
+ }
56
+ /**
57
+ * Unmarshal Peer Record Protobuf.
58
+ *
59
+ * @param {Uint8Array} buf - marshaled peer record.
60
+ * @returns {PeerRecord}
61
+ */
62
+ PeerRecord.createFromProtobuf = (buf) => {
63
+ const peerRecord = Protobuf.decode(buf);
64
+ const peerId = PeerId.fromBytes(peerRecord.peerId);
65
+ const multiaddrs = (peerRecord.addresses || []).map((a) => new Multiaddr(a.multiaddr));
66
+ const seqNumber = Number(peerRecord.seq);
67
+ return new PeerRecord({ peerId, multiaddrs, seqNumber });
68
+ };
69
+ PeerRecord.DOMAIN = ENVELOPE_DOMAIN_PEER_RECORD;
70
+ PeerRecord.CODEC = ENVELOPE_PAYLOAD_TYPE_PEER_RECORD;
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/peer-record/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,UAAU,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EACL,2BAA2B,EAC3B,iCAAiC,EAClC,MAAM,aAAa,CAAA;AAgBpB;;;GAGG;AACH,MAAM,OAAO,UAAU;IA0BrB,YAAa,OAA0B;QAJhC,WAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC1B,UAAK,GAAG,UAAU,CAAC,KAAK,CAAA;QAI7B,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;QAEjD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,UAAU,GAAG,UAAU,IAAI,EAAE,CAAA;QAClC,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,CAAA;IAC1C,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,EAAE;YAC1B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC;gBAC/B,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;gBAC7B,GAAG,EAAE,IAAI,CAAC,SAAS;gBACnB,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;oBACrC,SAAS,EAAE,CAAC,CAAC,KAAK;iBACnB,CAAC,CAAC;aACJ,CAAC,CAAC,MAAM,EAAE,CAAA;SACZ;QAED,OAAO,IAAI,CAAC,SAAS,CAAA;IACvB,CAAC;IAED;;OAEG;IACH,MAAM,CAAE,KAAc;QACpB,IAAI,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC,EAAE;YAClC,OAAO,KAAK,CAAA;SACb;QAED,kBAAkB;QAClB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE;YACrC,OAAO,KAAK,CAAA;SACb;QAED,qBAAqB;QACrB,IAAI,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,EAAE;YACtC,OAAO,KAAK,CAAA;SACb;QAED,sBAAsB;QACtB,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,EAAE;YACnD,OAAO,KAAK,CAAA;SACb;QAED,OAAO,IAAI,CAAA;IACb,CAAC;;AA1ED;;;;;GAKG;AACI,6BAAkB,GAAG,CAAC,GAAe,EAAE,EAAE;IAC9C,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACvC,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IAClD,MAAM,UAAU,GAAG,CAAC,UAAU,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAA;IACtF,MAAM,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;IAExC,OAAO,IAAI,UAAU,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC,CAAA;AAC1D,CAAC,CAAA;AAEM,iBAAM,GAAG,2BAA2B,CAAA;AACpC,gBAAK,GAAG,iCAAiC,CAAA"}
@@ -0,0 +1,133 @@
1
+ import * as $protobuf from "protobufjs";
2
+ /** Properties of a PeerRecord. */
3
+ export interface IPeerRecord {
4
+
5
+ /** PeerRecord peerId */
6
+ peerId?: (Uint8Array|null);
7
+
8
+ /** PeerRecord seq */
9
+ seq?: (number|null);
10
+
11
+ /** PeerRecord addresses */
12
+ addresses?: (PeerRecord.IAddressInfo[]|null);
13
+ }
14
+
15
+ /** Represents a PeerRecord. */
16
+ export class PeerRecord implements IPeerRecord {
17
+
18
+ /**
19
+ * Constructs a new PeerRecord.
20
+ * @param [p] Properties to set
21
+ */
22
+ constructor(p?: IPeerRecord);
23
+
24
+ /** PeerRecord peerId. */
25
+ public peerId: Uint8Array;
26
+
27
+ /** PeerRecord seq. */
28
+ public seq: number;
29
+
30
+ /** PeerRecord addresses. */
31
+ public addresses: PeerRecord.IAddressInfo[];
32
+
33
+ /**
34
+ * Encodes the specified PeerRecord message. Does not implicitly {@link PeerRecord.verify|verify} messages.
35
+ * @param m PeerRecord message or plain object to encode
36
+ * @param [w] Writer to encode to
37
+ * @returns Writer
38
+ */
39
+ public static encode(m: IPeerRecord, w?: $protobuf.Writer): $protobuf.Writer;
40
+
41
+ /**
42
+ * Decodes a PeerRecord message from the specified reader or buffer.
43
+ * @param r Reader or buffer to decode from
44
+ * @param [l] Message length if known beforehand
45
+ * @returns PeerRecord
46
+ * @throws {Error} If the payload is not a reader or valid buffer
47
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
48
+ */
49
+ public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerRecord;
50
+
51
+ /**
52
+ * Creates a PeerRecord message from a plain object. Also converts values to their respective internal types.
53
+ * @param d Plain object
54
+ * @returns PeerRecord
55
+ */
56
+ public static fromObject(d: { [k: string]: any }): PeerRecord;
57
+
58
+ /**
59
+ * Creates a plain object from a PeerRecord message. Also converts values to other types if specified.
60
+ * @param m PeerRecord
61
+ * @param [o] Conversion options
62
+ * @returns Plain object
63
+ */
64
+ public static toObject(m: PeerRecord, o?: $protobuf.IConversionOptions): { [k: string]: any };
65
+
66
+ /**
67
+ * Converts this PeerRecord to JSON.
68
+ * @returns JSON object
69
+ */
70
+ public toJSON(): { [k: string]: any };
71
+ }
72
+
73
+ export namespace PeerRecord {
74
+
75
+ /** Properties of an AddressInfo. */
76
+ interface IAddressInfo {
77
+
78
+ /** AddressInfo multiaddr */
79
+ multiaddr?: (Uint8Array|null);
80
+ }
81
+
82
+ /** Represents an AddressInfo. */
83
+ class AddressInfo implements IAddressInfo {
84
+
85
+ /**
86
+ * Constructs a new AddressInfo.
87
+ * @param [p] Properties to set
88
+ */
89
+ constructor(p?: PeerRecord.IAddressInfo);
90
+
91
+ /** AddressInfo multiaddr. */
92
+ public multiaddr: Uint8Array;
93
+
94
+ /**
95
+ * Encodes the specified AddressInfo message. Does not implicitly {@link PeerRecord.AddressInfo.verify|verify} messages.
96
+ * @param m AddressInfo message or plain object to encode
97
+ * @param [w] Writer to encode to
98
+ * @returns Writer
99
+ */
100
+ public static encode(m: PeerRecord.IAddressInfo, w?: $protobuf.Writer): $protobuf.Writer;
101
+
102
+ /**
103
+ * Decodes an AddressInfo message from the specified reader or buffer.
104
+ * @param r Reader or buffer to decode from
105
+ * @param [l] Message length if known beforehand
106
+ * @returns AddressInfo
107
+ * @throws {Error} If the payload is not a reader or valid buffer
108
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
109
+ */
110
+ public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): PeerRecord.AddressInfo;
111
+
112
+ /**
113
+ * Creates an AddressInfo message from a plain object. Also converts values to their respective internal types.
114
+ * @param d Plain object
115
+ * @returns AddressInfo
116
+ */
117
+ public static fromObject(d: { [k: string]: any }): PeerRecord.AddressInfo;
118
+
119
+ /**
120
+ * Creates a plain object from an AddressInfo message. Also converts values to other types if specified.
121
+ * @param m AddressInfo
122
+ * @param [o] Conversion options
123
+ * @returns Plain object
124
+ */
125
+ public static toObject(m: PeerRecord.AddressInfo, o?: $protobuf.IConversionOptions): { [k: string]: any };
126
+
127
+ /**
128
+ * Converts this AddressInfo to JSON.
129
+ * @returns JSON object
130
+ */
131
+ public toJSON(): { [k: string]: any };
132
+ }
133
+ }
@@ -0,0 +1,365 @@
1
+ /*eslint-disable*/
2
+ import $protobuf from "protobufjs/minimal.js";
3
+
4
+ // Common aliases
5
+ const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
6
+
7
+ // Exported root namespace
8
+ const $root = $protobuf.roots["libp2p-peer-record"] || ($protobuf.roots["libp2p-peer-record"] = {});
9
+
10
+ export const PeerRecord = $root.PeerRecord = (() => {
11
+
12
+ /**
13
+ * Properties of a PeerRecord.
14
+ * @exports IPeerRecord
15
+ * @interface IPeerRecord
16
+ * @property {Uint8Array|null} [peerId] PeerRecord peerId
17
+ * @property {number|null} [seq] PeerRecord seq
18
+ * @property {Array.<PeerRecord.IAddressInfo>|null} [addresses] PeerRecord addresses
19
+ */
20
+
21
+ /**
22
+ * Constructs a new PeerRecord.
23
+ * @exports PeerRecord
24
+ * @classdesc Represents a PeerRecord.
25
+ * @implements IPeerRecord
26
+ * @constructor
27
+ * @param {IPeerRecord=} [p] Properties to set
28
+ */
29
+ function PeerRecord(p) {
30
+ this.addresses = [];
31
+ if (p)
32
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
33
+ if (p[ks[i]] != null)
34
+ this[ks[i]] = p[ks[i]];
35
+ }
36
+
37
+ /**
38
+ * PeerRecord peerId.
39
+ * @member {Uint8Array} peerId
40
+ * @memberof PeerRecord
41
+ * @instance
42
+ */
43
+ PeerRecord.prototype.peerId = $util.newBuffer([]);
44
+
45
+ /**
46
+ * PeerRecord seq.
47
+ * @member {number} seq
48
+ * @memberof PeerRecord
49
+ * @instance
50
+ */
51
+ PeerRecord.prototype.seq = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
52
+
53
+ /**
54
+ * PeerRecord addresses.
55
+ * @member {Array.<PeerRecord.IAddressInfo>} addresses
56
+ * @memberof PeerRecord
57
+ * @instance
58
+ */
59
+ PeerRecord.prototype.addresses = $util.emptyArray;
60
+
61
+ /**
62
+ * Encodes the specified PeerRecord message. Does not implicitly {@link PeerRecord.verify|verify} messages.
63
+ * @function encode
64
+ * @memberof PeerRecord
65
+ * @static
66
+ * @param {IPeerRecord} m PeerRecord message or plain object to encode
67
+ * @param {$protobuf.Writer} [w] Writer to encode to
68
+ * @returns {$protobuf.Writer} Writer
69
+ */
70
+ PeerRecord.encode = function encode(m, w) {
71
+ if (!w)
72
+ w = $Writer.create();
73
+ if (m.peerId != null && Object.hasOwnProperty.call(m, "peerId"))
74
+ w.uint32(10).bytes(m.peerId);
75
+ if (m.seq != null && Object.hasOwnProperty.call(m, "seq"))
76
+ w.uint32(16).uint64(m.seq);
77
+ if (m.addresses != null && m.addresses.length) {
78
+ for (var i = 0; i < m.addresses.length; ++i)
79
+ $root.PeerRecord.AddressInfo.encode(m.addresses[i], w.uint32(26).fork()).ldelim();
80
+ }
81
+ return w;
82
+ };
83
+
84
+ /**
85
+ * Decodes a PeerRecord message from the specified reader or buffer.
86
+ * @function decode
87
+ * @memberof PeerRecord
88
+ * @static
89
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
90
+ * @param {number} [l] Message length if known beforehand
91
+ * @returns {PeerRecord} PeerRecord
92
+ * @throws {Error} If the payload is not a reader or valid buffer
93
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
94
+ */
95
+ PeerRecord.decode = function decode(r, l) {
96
+ if (!(r instanceof $Reader))
97
+ r = $Reader.create(r);
98
+ var c = l === undefined ? r.len : r.pos + l, m = new $root.PeerRecord();
99
+ while (r.pos < c) {
100
+ var t = r.uint32();
101
+ switch (t >>> 3) {
102
+ case 1:
103
+ m.peerId = r.bytes();
104
+ break;
105
+ case 2:
106
+ m.seq = r.uint64();
107
+ break;
108
+ case 3:
109
+ if (!(m.addresses && m.addresses.length))
110
+ m.addresses = [];
111
+ m.addresses.push($root.PeerRecord.AddressInfo.decode(r, r.uint32()));
112
+ break;
113
+ default:
114
+ r.skipType(t & 7);
115
+ break;
116
+ }
117
+ }
118
+ return m;
119
+ };
120
+
121
+ /**
122
+ * Creates a PeerRecord message from a plain object. Also converts values to their respective internal types.
123
+ * @function fromObject
124
+ * @memberof PeerRecord
125
+ * @static
126
+ * @param {Object.<string,*>} d Plain object
127
+ * @returns {PeerRecord} PeerRecord
128
+ */
129
+ PeerRecord.fromObject = function fromObject(d) {
130
+ if (d instanceof $root.PeerRecord)
131
+ return d;
132
+ var m = new $root.PeerRecord();
133
+ if (d.peerId != null) {
134
+ if (typeof d.peerId === "string")
135
+ $util.base64.decode(d.peerId, m.peerId = $util.newBuffer($util.base64.length(d.peerId)), 0);
136
+ else if (d.peerId.length)
137
+ m.peerId = d.peerId;
138
+ }
139
+ if (d.seq != null) {
140
+ if ($util.Long)
141
+ (m.seq = $util.Long.fromValue(d.seq)).unsigned = true;
142
+ else if (typeof d.seq === "string")
143
+ m.seq = parseInt(d.seq, 10);
144
+ else if (typeof d.seq === "number")
145
+ m.seq = d.seq;
146
+ else if (typeof d.seq === "object")
147
+ m.seq = new $util.LongBits(d.seq.low >>> 0, d.seq.high >>> 0).toNumber(true);
148
+ }
149
+ if (d.addresses) {
150
+ if (!Array.isArray(d.addresses))
151
+ throw TypeError(".PeerRecord.addresses: array expected");
152
+ m.addresses = [];
153
+ for (var i = 0; i < d.addresses.length; ++i) {
154
+ if (typeof d.addresses[i] !== "object")
155
+ throw TypeError(".PeerRecord.addresses: object expected");
156
+ m.addresses[i] = $root.PeerRecord.AddressInfo.fromObject(d.addresses[i]);
157
+ }
158
+ }
159
+ return m;
160
+ };
161
+
162
+ /**
163
+ * Creates a plain object from a PeerRecord message. Also converts values to other types if specified.
164
+ * @function toObject
165
+ * @memberof PeerRecord
166
+ * @static
167
+ * @param {PeerRecord} m PeerRecord
168
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
169
+ * @returns {Object.<string,*>} Plain object
170
+ */
171
+ PeerRecord.toObject = function toObject(m, o) {
172
+ if (!o)
173
+ o = {};
174
+ var d = {};
175
+ if (o.arrays || o.defaults) {
176
+ d.addresses = [];
177
+ }
178
+ if (o.defaults) {
179
+ if (o.bytes === String)
180
+ d.peerId = "";
181
+ else {
182
+ d.peerId = [];
183
+ if (o.bytes !== Array)
184
+ d.peerId = $util.newBuffer(d.peerId);
185
+ }
186
+ if ($util.Long) {
187
+ var n = new $util.Long(0, 0, true);
188
+ d.seq = o.longs === String ? n.toString() : o.longs === Number ? n.toNumber() : n;
189
+ } else
190
+ d.seq = o.longs === String ? "0" : 0;
191
+ }
192
+ if (m.peerId != null && m.hasOwnProperty("peerId")) {
193
+ d.peerId = o.bytes === String ? $util.base64.encode(m.peerId, 0, m.peerId.length) : o.bytes === Array ? Array.prototype.slice.call(m.peerId) : m.peerId;
194
+ }
195
+ if (m.seq != null && m.hasOwnProperty("seq")) {
196
+ if (typeof m.seq === "number")
197
+ d.seq = o.longs === String ? String(m.seq) : m.seq;
198
+ else
199
+ d.seq = o.longs === String ? $util.Long.prototype.toString.call(m.seq) : o.longs === Number ? new $util.LongBits(m.seq.low >>> 0, m.seq.high >>> 0).toNumber(true) : m.seq;
200
+ }
201
+ if (m.addresses && m.addresses.length) {
202
+ d.addresses = [];
203
+ for (var j = 0; j < m.addresses.length; ++j) {
204
+ d.addresses[j] = $root.PeerRecord.AddressInfo.toObject(m.addresses[j], o);
205
+ }
206
+ }
207
+ return d;
208
+ };
209
+
210
+ /**
211
+ * Converts this PeerRecord to JSON.
212
+ * @function toJSON
213
+ * @memberof PeerRecord
214
+ * @instance
215
+ * @returns {Object.<string,*>} JSON object
216
+ */
217
+ PeerRecord.prototype.toJSON = function toJSON() {
218
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
219
+ };
220
+
221
+ PeerRecord.AddressInfo = (function() {
222
+
223
+ /**
224
+ * Properties of an AddressInfo.
225
+ * @memberof PeerRecord
226
+ * @interface IAddressInfo
227
+ * @property {Uint8Array|null} [multiaddr] AddressInfo multiaddr
228
+ */
229
+
230
+ /**
231
+ * Constructs a new AddressInfo.
232
+ * @memberof PeerRecord
233
+ * @classdesc Represents an AddressInfo.
234
+ * @implements IAddressInfo
235
+ * @constructor
236
+ * @param {PeerRecord.IAddressInfo=} [p] Properties to set
237
+ */
238
+ function AddressInfo(p) {
239
+ if (p)
240
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
241
+ if (p[ks[i]] != null)
242
+ this[ks[i]] = p[ks[i]];
243
+ }
244
+
245
+ /**
246
+ * AddressInfo multiaddr.
247
+ * @member {Uint8Array} multiaddr
248
+ * @memberof PeerRecord.AddressInfo
249
+ * @instance
250
+ */
251
+ AddressInfo.prototype.multiaddr = $util.newBuffer([]);
252
+
253
+ /**
254
+ * Encodes the specified AddressInfo message. Does not implicitly {@link PeerRecord.AddressInfo.verify|verify} messages.
255
+ * @function encode
256
+ * @memberof PeerRecord.AddressInfo
257
+ * @static
258
+ * @param {PeerRecord.IAddressInfo} m AddressInfo message or plain object to encode
259
+ * @param {$protobuf.Writer} [w] Writer to encode to
260
+ * @returns {$protobuf.Writer} Writer
261
+ */
262
+ AddressInfo.encode = function encode(m, w) {
263
+ if (!w)
264
+ w = $Writer.create();
265
+ if (m.multiaddr != null && Object.hasOwnProperty.call(m, "multiaddr"))
266
+ w.uint32(10).bytes(m.multiaddr);
267
+ return w;
268
+ };
269
+
270
+ /**
271
+ * Decodes an AddressInfo message from the specified reader or buffer.
272
+ * @function decode
273
+ * @memberof PeerRecord.AddressInfo
274
+ * @static
275
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
276
+ * @param {number} [l] Message length if known beforehand
277
+ * @returns {PeerRecord.AddressInfo} AddressInfo
278
+ * @throws {Error} If the payload is not a reader or valid buffer
279
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
280
+ */
281
+ AddressInfo.decode = function decode(r, l) {
282
+ if (!(r instanceof $Reader))
283
+ r = $Reader.create(r);
284
+ var c = l === undefined ? r.len : r.pos + l, m = new $root.PeerRecord.AddressInfo();
285
+ while (r.pos < c) {
286
+ var t = r.uint32();
287
+ switch (t >>> 3) {
288
+ case 1:
289
+ m.multiaddr = r.bytes();
290
+ break;
291
+ default:
292
+ r.skipType(t & 7);
293
+ break;
294
+ }
295
+ }
296
+ return m;
297
+ };
298
+
299
+ /**
300
+ * Creates an AddressInfo message from a plain object. Also converts values to their respective internal types.
301
+ * @function fromObject
302
+ * @memberof PeerRecord.AddressInfo
303
+ * @static
304
+ * @param {Object.<string,*>} d Plain object
305
+ * @returns {PeerRecord.AddressInfo} AddressInfo
306
+ */
307
+ AddressInfo.fromObject = function fromObject(d) {
308
+ if (d instanceof $root.PeerRecord.AddressInfo)
309
+ return d;
310
+ var m = new $root.PeerRecord.AddressInfo();
311
+ if (d.multiaddr != null) {
312
+ if (typeof d.multiaddr === "string")
313
+ $util.base64.decode(d.multiaddr, m.multiaddr = $util.newBuffer($util.base64.length(d.multiaddr)), 0);
314
+ else if (d.multiaddr.length)
315
+ m.multiaddr = d.multiaddr;
316
+ }
317
+ return m;
318
+ };
319
+
320
+ /**
321
+ * Creates a plain object from an AddressInfo message. Also converts values to other types if specified.
322
+ * @function toObject
323
+ * @memberof PeerRecord.AddressInfo
324
+ * @static
325
+ * @param {PeerRecord.AddressInfo} m AddressInfo
326
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
327
+ * @returns {Object.<string,*>} Plain object
328
+ */
329
+ AddressInfo.toObject = function toObject(m, o) {
330
+ if (!o)
331
+ o = {};
332
+ var d = {};
333
+ if (o.defaults) {
334
+ if (o.bytes === String)
335
+ d.multiaddr = "";
336
+ else {
337
+ d.multiaddr = [];
338
+ if (o.bytes !== Array)
339
+ d.multiaddr = $util.newBuffer(d.multiaddr);
340
+ }
341
+ }
342
+ if (m.multiaddr != null && m.hasOwnProperty("multiaddr")) {
343
+ d.multiaddr = o.bytes === String ? $util.base64.encode(m.multiaddr, 0, m.multiaddr.length) : o.bytes === Array ? Array.prototype.slice.call(m.multiaddr) : m.multiaddr;
344
+ }
345
+ return d;
346
+ };
347
+
348
+ /**
349
+ * Converts this AddressInfo to JSON.
350
+ * @function toJSON
351
+ * @memberof PeerRecord.AddressInfo
352
+ * @instance
353
+ * @returns {Object.<string,*>} JSON object
354
+ */
355
+ AddressInfo.prototype.toJSON = function toJSON() {
356
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
357
+ };
358
+
359
+ return AddressInfo;
360
+ })();
361
+
362
+ return PeerRecord;
363
+ })();
364
+
365
+ export { $root as default };