@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.
- package/LICENSE +4 -0
- package/README.md +179 -0
- package/dist/src/envelope/envelope.d.ts +77 -0
- package/dist/src/envelope/envelope.js +241 -0
- package/dist/src/envelope/index.d.ts +47 -0
- package/dist/src/envelope/index.d.ts.map +1 -0
- package/dist/src/envelope/index.js +126 -0
- package/dist/src/envelope/index.js.map +1 -0
- package/dist/src/errors.d.ts +4 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +4 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/peer-record/consts.d.ts +3 -0
- package/dist/src/peer-record/consts.d.ts.map +1 -0
- package/dist/src/peer-record/consts.js +7 -0
- package/dist/src/peer-record/consts.js.map +1 -0
- package/dist/src/peer-record/index.d.ts +44 -0
- package/dist/src/peer-record/index.d.ts.map +1 -0
- package/dist/src/peer-record/index.js +71 -0
- package/dist/src/peer-record/index.js.map +1 -0
- package/dist/src/peer-record/peer-record.d.ts +133 -0
- package/dist/src/peer-record/peer-record.js +365 -0
- package/package.json +164 -0
- package/src/envelope/envelope.d.ts +77 -0
- package/src/envelope/envelope.js +241 -0
- package/src/envelope/envelope.proto +19 -0
- package/src/envelope/index.ts +159 -0
- package/src/errors.ts +4 -0
- package/src/index.ts +3 -0
- package/src/peer-record/consts.js +9 -0
- package/src/peer-record/index.ts +104 -0
- package/src/peer-record/peer-record.d.ts +133 -0
- package/src/peer-record/peer-record.js +365 -0
- package/src/peer-record/peer-record.proto +18 -0
|
@@ -0,0 +1,104 @@
|
|
|
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 {
|
|
6
|
+
ENVELOPE_DOMAIN_PEER_RECORD,
|
|
7
|
+
ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
|
|
8
|
+
} from './consts.js'
|
|
9
|
+
|
|
10
|
+
export interface PeerRecordOptions {
|
|
11
|
+
peerId: PeerId
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Addresses of the associated peer.
|
|
15
|
+
*/
|
|
16
|
+
multiaddrs?: Multiaddr[]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Monotonically-increasing sequence counter that's used to order PeerRecords in time.
|
|
20
|
+
*/
|
|
21
|
+
seqNumber?: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The PeerRecord is used for distributing peer routing records across the network.
|
|
26
|
+
* It contains the peer's reachable listen addresses.
|
|
27
|
+
*/
|
|
28
|
+
export class PeerRecord {
|
|
29
|
+
/**
|
|
30
|
+
* Unmarshal Peer Record Protobuf.
|
|
31
|
+
*
|
|
32
|
+
* @param {Uint8Array} buf - marshaled peer record.
|
|
33
|
+
* @returns {PeerRecord}
|
|
34
|
+
*/
|
|
35
|
+
static createFromProtobuf = (buf: Uint8Array) => {
|
|
36
|
+
const peerRecord = Protobuf.decode(buf)
|
|
37
|
+
const peerId = PeerId.fromBytes(peerRecord.peerId)
|
|
38
|
+
const multiaddrs = (peerRecord.addresses || []).map((a) => new Multiaddr(a.multiaddr))
|
|
39
|
+
const seqNumber = Number(peerRecord.seq)
|
|
40
|
+
|
|
41
|
+
return new PeerRecord({ peerId, multiaddrs, seqNumber })
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static DOMAIN = ENVELOPE_DOMAIN_PEER_RECORD
|
|
45
|
+
static CODEC = ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
|
|
46
|
+
|
|
47
|
+
public peerId: PeerId
|
|
48
|
+
public multiaddrs: Multiaddr[]
|
|
49
|
+
public seqNumber: number
|
|
50
|
+
public domain = PeerRecord.DOMAIN
|
|
51
|
+
public codec = PeerRecord.CODEC
|
|
52
|
+
private marshaled?: Uint8Array
|
|
53
|
+
|
|
54
|
+
constructor (options: PeerRecordOptions) {
|
|
55
|
+
const { peerId, multiaddrs, seqNumber } = options
|
|
56
|
+
|
|
57
|
+
this.peerId = peerId
|
|
58
|
+
this.multiaddrs = multiaddrs ?? []
|
|
59
|
+
this.seqNumber = seqNumber ?? Date.now()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Marshal a record to be used in an envelope
|
|
64
|
+
*/
|
|
65
|
+
marshal () {
|
|
66
|
+
if (this.marshaled == null) {
|
|
67
|
+
this.marshaled = Protobuf.encode({
|
|
68
|
+
peerId: this.peerId.toBytes(),
|
|
69
|
+
seq: this.seqNumber,
|
|
70
|
+
addresses: this.multiaddrs.map((m) => ({
|
|
71
|
+
multiaddr: m.bytes
|
|
72
|
+
}))
|
|
73
|
+
}).finish()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return this.marshaled
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Returns true if `this` record equals the `other`
|
|
81
|
+
*/
|
|
82
|
+
equals (other: unknown) {
|
|
83
|
+
if (!(other instanceof PeerRecord)) {
|
|
84
|
+
return false
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Validate PeerId
|
|
88
|
+
if (!this.peerId.equals(other.peerId)) {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Validate seqNumber
|
|
93
|
+
if (this.seqNumber !== other.seqNumber) {
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Validate multiaddrs
|
|
98
|
+
if (!arrayEquals(this.multiaddrs, other.multiaddrs)) {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return true
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -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 };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
message PeerRecord {
|
|
4
|
+
// AddressInfo is a wrapper around a binary multiaddr. It is defined as a
|
|
5
|
+
// separate message to allow us to add per-address metadata in the future.
|
|
6
|
+
message AddressInfo {
|
|
7
|
+
bytes multiaddr = 1;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// peer_id contains a libp2p peer id in its binary representation.
|
|
11
|
+
bytes peer_id = 1;
|
|
12
|
+
|
|
13
|
+
// seq contains a monotonically-increasing sequence counter to order PeerRecords in time.
|
|
14
|
+
uint64 seq = 2;
|
|
15
|
+
|
|
16
|
+
// addresses is a list of public listen addresses for the peer.
|
|
17
|
+
repeated AddressInfo addresses = 3;
|
|
18
|
+
}
|