@peerbit/stream-interface 1.0.10 → 2.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.
@@ -7,16 +7,19 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
- var DataMessage_1, Hello_1, Goodbye_1, PingPong_1, Ping_1, Pong_1;
10
+ var DataMessage_1, ACK_1, Hello_1, Goodbye_1;
11
11
  import { variant, vec, field, serialize, deserialize, fixedArray, option } from "@dao-xyz/borsh";
12
12
  import { equals } from "uint8arrays";
13
- import { PublicSignKey, SignatureWithKey, verify, randomBytes, sha256Base64, sha256 } from "@peerbit/crypto";
13
+ import { PublicSignKey, SignatureWithKey, verify, randomBytes, sha256Base64, getPublicKeyFromPeerId } from "@peerbit/crypto";
14
+ export const ID_LENGTH = 32;
15
+ const WEEK_MS = 7 * 24 * 60 * 60 + 1000;
16
+ const SIGNATURES_SIZE_ENCODING = "u8"; // with 7 steps you know everyone in the world?, so u8 *should* suffice
14
17
  /**
15
18
  * The default msgID implementation
16
19
  * Child class can override this.
17
20
  */
18
21
  export const getMsgId = async (msg) => {
19
- // first bytes is discriminator,
22
+ // first bytes fis discriminator,
20
23
  // next 32 bytes should be an id
21
24
  //return Buffer.from(msg.slice(0, 33)).toString('base64');
22
25
  return sha256Base64(msg.subarray(0, 33)); // base64EncArr(msg, 0, ID_LENGTH + 1);
@@ -42,16 +45,179 @@ else {
42
45
  return output;
43
46
  };
44
47
  }
45
- export const ID_LENGTH = 32;
46
- const WEEK_MS = 7 * 24 * 60 * 60 + 1000;
48
+ const coerceTo = (tos) => {
49
+ const toHashes = [];
50
+ let i = 0;
51
+ for (const to of tos) {
52
+ const hash = to instanceof PublicSignKey
53
+ ? to.hashcode()
54
+ : typeof to === "string"
55
+ ? to
56
+ : getPublicKeyFromPeerId(to).hashcode();
57
+ toHashes[i++] = hash;
58
+ }
59
+ return toHashes;
60
+ };
61
+ export class DeliveryMode {
62
+ }
63
+ /**
64
+ * when you just want to deliver at paths, but does not expect acknowledgement
65
+ */
66
+ let SilentDelivery = class SilentDelivery extends DeliveryMode {
67
+ to;
68
+ redundancy;
69
+ constructor(properties) {
70
+ super();
71
+ this.to = coerceTo(properties.to);
72
+ this.redundancy = properties.redundancy;
73
+ }
74
+ };
75
+ __decorate([
76
+ field({ type: vec("string") }),
77
+ __metadata("design:type", Array)
78
+ ], SilentDelivery.prototype, "to", void 0);
79
+ __decorate([
80
+ field({ type: "u8" }),
81
+ __metadata("design:type", Number)
82
+ ], SilentDelivery.prototype, "redundancy", void 0);
83
+ SilentDelivery = __decorate([
84
+ variant(0),
85
+ __metadata("design:paramtypes", [Object])
86
+ ], SilentDelivery);
87
+ export { SilentDelivery };
88
+ /**
89
+ * Deliver and expect acknowledgement
90
+ */
91
+ let AcknowledgeDelivery = class AcknowledgeDelivery extends DeliveryMode {
92
+ to;
93
+ redundancy;
94
+ constructor(properties) {
95
+ super();
96
+ this.to = coerceTo(properties.to);
97
+ this.redundancy = properties.redundancy;
98
+ }
99
+ };
100
+ __decorate([
101
+ field({ type: vec("string") }),
102
+ __metadata("design:type", Array)
103
+ ], AcknowledgeDelivery.prototype, "to", void 0);
104
+ __decorate([
105
+ field({ type: "u8" }),
106
+ __metadata("design:type", Number)
107
+ ], AcknowledgeDelivery.prototype, "redundancy", void 0);
108
+ AcknowledgeDelivery = __decorate([
109
+ variant(1),
110
+ __metadata("design:paramtypes", [Object])
111
+ ], AcknowledgeDelivery);
112
+ export { AcknowledgeDelivery };
113
+ /**
114
+ * Deliver but with greedy fanout so that we eventually reach our target
115
+ * Expect acknowledgement
116
+ */
117
+ let SeekDelivery = class SeekDelivery extends DeliveryMode {
118
+ to;
119
+ redundancy;
120
+ constructor(properties) {
121
+ super();
122
+ this.to = properties.to ? coerceTo(properties.to) : undefined;
123
+ this.redundancy = properties.redundancy;
124
+ }
125
+ };
126
+ __decorate([
127
+ field({ type: option(vec("string")) }),
128
+ __metadata("design:type", Array)
129
+ ], SeekDelivery.prototype, "to", void 0);
130
+ __decorate([
131
+ field({ type: "u8" }),
132
+ __metadata("design:type", Number)
133
+ ], SeekDelivery.prototype, "redundancy", void 0);
134
+ SeekDelivery = __decorate([
135
+ variant(2),
136
+ __metadata("design:paramtypes", [Object])
137
+ ], SeekDelivery);
138
+ export { SeekDelivery };
139
+ let TracedDelivery = class TracedDelivery extends DeliveryMode {
140
+ trace;
141
+ constructor(trace) {
142
+ super();
143
+ this.trace = trace;
144
+ }
145
+ };
146
+ __decorate([
147
+ field({ type: vec("string") }),
148
+ __metadata("design:type", Array)
149
+ ], TracedDelivery.prototype, "trace", void 0);
150
+ TracedDelivery = __decorate([
151
+ variant(3),
152
+ __metadata("design:paramtypes", [Array])
153
+ ], TracedDelivery);
154
+ export { TracedDelivery };
155
+ let AnyWhere = class AnyWhere extends DeliveryMode {
156
+ constructor() {
157
+ super();
158
+ }
159
+ };
160
+ AnyWhere = __decorate([
161
+ variant(4),
162
+ __metadata("design:paramtypes", [])
163
+ ], AnyWhere);
164
+ export { AnyWhere };
165
+ let Signatures = class Signatures {
166
+ signatures;
167
+ constructor(signatures = []) {
168
+ this.signatures = signatures;
169
+ }
170
+ equals(other) {
171
+ return (this.signatures.length === other.signatures.length &&
172
+ this.signatures.every((value, i) => other.signatures[i].equals(value)));
173
+ }
174
+ get publicKeys() {
175
+ return this.signatures.map((x) => x.publicKey);
176
+ }
177
+ };
178
+ __decorate([
179
+ field({ type: vec(SignatureWithKey, SIGNATURES_SIZE_ENCODING) }),
180
+ __metadata("design:type", Array)
181
+ ], Signatures.prototype, "signatures", void 0);
182
+ Signatures = __decorate([
183
+ variant(0),
184
+ __metadata("design:paramtypes", [Array])
185
+ ], Signatures);
186
+ export { Signatures };
187
+ class PeerInfo {
188
+ }
189
+ let MultiAddrinfo = class MultiAddrinfo extends PeerInfo {
190
+ multiaddrs;
191
+ constructor(multiaddrs) {
192
+ super();
193
+ this.multiaddrs = multiaddrs;
194
+ }
195
+ };
196
+ __decorate([
197
+ field({ type: vec("string") }),
198
+ __metadata("design:type", Array)
199
+ ], MultiAddrinfo.prototype, "multiaddrs", void 0);
200
+ MultiAddrinfo = __decorate([
201
+ variant(0),
202
+ __metadata("design:paramtypes", [Array])
203
+ ], MultiAddrinfo);
204
+ export { MultiAddrinfo };
47
205
  let MessageHeader = class MessageHeader {
48
206
  _id;
49
207
  _timestamp;
50
208
  _expires;
209
+ _origin;
210
+ // Not signed, since we might want to modify it during transit
211
+ mode;
212
+ // Not signed, since we might want to modify it during transit
213
+ signatures;
51
214
  constructor(properties) {
52
215
  this._id = properties?.id || randomBytes(ID_LENGTH);
53
216
  this._expires = properties?.expires || BigInt(+new Date() + WEEK_MS);
54
217
  this._timestamp = BigInt(+new Date());
218
+ this.signatures = new Signatures();
219
+ this._origin = properties?.origin;
220
+ this.mode = properties.mode;
55
221
  }
56
222
  get id() {
57
223
  return this._id;
@@ -62,6 +228,9 @@ let MessageHeader = class MessageHeader {
62
228
  get timetamp() {
63
229
  return this._timestamp;
64
230
  }
231
+ get origin() {
232
+ return this._origin;
233
+ }
65
234
  equals(other) {
66
235
  return this._expires === other.expires && equals(this._id, other.id);
67
236
  }
@@ -81,184 +250,106 @@ __decorate([
81
250
  field({ type: "u64" }),
82
251
  __metadata("design:type", BigInt)
83
252
  ], MessageHeader.prototype, "_expires", void 0);
253
+ __decorate([
254
+ field({ type: option(PeerInfo) }),
255
+ __metadata("design:type", MultiAddrinfo)
256
+ ], MessageHeader.prototype, "_origin", void 0);
257
+ __decorate([
258
+ field({ type: option(DeliveryMode) }),
259
+ __metadata("design:type", Object)
260
+ ], MessageHeader.prototype, "mode", void 0);
261
+ __decorate([
262
+ field({ type: option(Signatures) }),
263
+ __metadata("design:type", Object)
264
+ ], MessageHeader.prototype, "signatures", void 0);
84
265
  MessageHeader = __decorate([
85
266
  variant(0),
86
267
  __metadata("design:paramtypes", [Object])
87
268
  ], MessageHeader);
88
269
  export { MessageHeader };
89
- class PublicKeys {
90
- keys;
91
- constructor(keys) {
92
- this.keys = keys;
93
- }
94
- }
95
- __decorate([
96
- field({ type: vec(PublicSignKey) }),
97
- __metadata("design:type", Array)
98
- ], PublicKeys.prototype, "keys", void 0);
99
- const SIGNATURES_SIZE_ENCODING = "u8"; // with 7 steps you know everyone in the world?, so u8 *should* suffice
100
- let Signatures = class Signatures {
101
- signatures;
102
- constructor(signatures = []) {
103
- this.signatures = signatures;
104
- }
105
- equals(other) {
106
- return (this.signatures.length === other.signatures.length &&
107
- this.signatures.every((value, i) => other.signatures[i].equals(value)));
108
- }
109
- get publicKeys() {
110
- return this.signatures.map((x) => x.publicKey);
111
- }
112
- hashPublicKeys() {
113
- return sha256Base64(serialize(new PublicKeys(this.publicKeys)));
114
- }
270
+ const sign = async (obj, signer) => {
271
+ const mode = obj.header.mode;
272
+ obj.header.mode = undefined;
273
+ const signatures = obj.header.signatures;
274
+ obj.header.signatures = undefined;
275
+ const bytes = serialize(obj);
276
+ // reassign properties if some other process expects them
277
+ obj.header.signatures = signatures;
278
+ obj.header.mode = mode;
279
+ const signature = await signer(bytes);
280
+ obj.header.signatures = new Signatures(signatures ? [...signatures.signatures, signature] : [signature]);
281
+ obj.header.mode = mode;
282
+ return obj;
115
283
  };
116
- __decorate([
117
- field({ type: vec(SignatureWithKey, SIGNATURES_SIZE_ENCODING) }),
118
- __metadata("design:type", Array)
119
- ], Signatures.prototype, "signatures", void 0);
120
- Signatures = __decorate([
121
- variant(0),
122
- __metadata("design:paramtypes", [Array])
123
- ], Signatures);
124
- export { Signatures };
125
- const keyMap = new Map();
126
284
  const verifyMultiSig = async (message, expectSignatures) => {
127
- const signatures = message.signatures.signatures;
128
- if (signatures.length === 0) {
285
+ const signatures = message.header.signatures;
286
+ if (!signatures || signatures.signatures.length === 0) {
129
287
  return !expectSignatures;
130
288
  }
131
- await message.createPrefix();
132
- const dataGenerator = getMultiSigDataToSignHistory(message, 0);
133
- let done = false;
134
- for (const signature of signatures) {
135
- if (done) {
136
- throw new Error("Unexpected, the amount of signatures does not match the amount of data verify");
137
- }
138
- const data = dataGenerator.next();
139
- done = data.done;
140
- if (!(await verify(signature, data.value))) {
289
+ const to = message.header.mode;
290
+ message.header.mode = undefined;
291
+ message.header.signatures = undefined;
292
+ const bytes = serialize(message);
293
+ message.header.mode = to;
294
+ message.header.signatures = signatures;
295
+ for (const signature of signatures.signatures) {
296
+ if (!(await verify(signature, bytes))) {
141
297
  return false;
142
298
  }
143
299
  }
144
300
  return true;
145
301
  };
146
- const emptySignatures = serialize(new Signatures());
147
- function* getMultiSigDataToSignHistory(message, from = 0) {
148
- if (from === 0) {
149
- yield concatBytes([message.prefix, emptySignatures], message.prefix.length + emptySignatures.length);
150
- }
151
- for (let i = Math.max(from - 1, 0); i < message.signatures.signatures.length; i++) {
152
- const bytes = message.getSuffix(i); // TODO make more performant
153
- const concat = [message.prefix];
154
- let len = message.prefix.length;
155
- if (bytes instanceof Uint8Array) {
156
- concat.push(bytes);
157
- len += bytes.byteLength;
158
- }
159
- else {
160
- for (const arr of bytes) {
161
- concat.push(arr);
162
- len += arr.byteLength;
163
- }
164
- }
165
- yield concatBytes(concat, len);
166
- }
167
- return;
168
- }
169
302
  export class Message {
170
303
  static from(bytes) {
171
304
  if (bytes.get(0) === DATA_VARIANT) {
172
305
  // Data
173
306
  return DataMessage.from(bytes);
174
307
  }
308
+ else if (bytes.get(0) === ACKNOWLEDGE_VARIANT) {
309
+ return ACK.from(bytes);
310
+ }
175
311
  else if (bytes.get(0) === HELLO_VARIANT) {
176
- // heartbeat
177
312
  return Hello.from(bytes);
178
313
  }
179
314
  else if (bytes.get(0) === GOODBYE_VARIANT) {
180
- // heartbeat
181
315
  return Goodbye.from(bytes);
182
316
  }
183
- else if (bytes.get(0) === PING_VARIANT) {
184
- return PingPong.from(bytes);
185
- }
186
317
  throw new Error("Unsupported");
187
318
  }
319
+ async sign(signer) {
320
+ return sign(this, signer);
321
+ }
322
+ /* abstract equals(other: Message): boolean; */
323
+ _verified;
324
+ async verify(expectSignatures) {
325
+ return this._verified != null
326
+ ? this._verified
327
+ : (this._verified =
328
+ (await this.header.verify()) &&
329
+ (await verifyMultiSig(this, expectSignatures)));
330
+ }
188
331
  }
189
332
  // I pack data with this message
190
333
  const DATA_VARIANT = 0;
191
334
  let DataMessage = DataMessage_1 = class DataMessage extends Message {
192
335
  _header;
193
- _to; // not signed! TODO should we sign this?
194
- _signatures;
195
336
  _data;
196
337
  constructor(properties) {
197
338
  super();
198
339
  this._data = properties.data;
199
- this._header = properties.header || new MessageHeader();
200
- this._to = properties.to || [];
201
- this._signatures = properties.signatures || new Signatures();
340
+ this._header = properties.header;
202
341
  }
203
342
  get id() {
204
343
  return this._header.id;
205
344
  }
206
- get signatures() {
207
- return this._signatures;
208
- }
209
345
  get header() {
210
346
  return this._header;
211
347
  }
212
- get to() {
213
- return this._to;
214
- }
215
- set to(to) {
216
- this._serialized = undefined;
217
- this._to = to;
218
- }
219
- get sender() {
220
- return this.signatures.signatures[0].publicKey;
221
- }
222
348
  get data() {
223
349
  return this._data;
224
350
  }
225
- _serialized;
226
- get serialized() {
227
- return this.serialized;
228
- }
229
- _prefix;
230
- get prefix() {
231
- if (!this._prefix) {
232
- throw new Error("Prefix not created");
233
- }
234
- return this._prefix;
235
- }
236
- async createPrefix() {
237
- if (this._prefix) {
238
- return this._prefix;
239
- }
240
- const headerSer = serialize(this._header);
241
- const hashBytes = await sha256(this.data);
242
- this._prefix = concatBytes([new Uint8Array([DATA_VARIANT]), headerSer, hashBytes], 1 + headerSer.length + hashBytes.length);
243
- return this._prefix;
244
- }
245
- getSuffix(iteration) {
246
- return serialize(new Signatures(this.signatures.signatures.slice(0, iteration + 1)));
247
- }
248
- async sign(sign) {
249
- this._serialized = undefined; // because we will change this object, so the serialized version will not be applicable anymore
250
- await this.createPrefix();
251
- this.signatures.signatures.push(await sign(getMultiSigDataToSignHistory(this, this.signatures.signatures.length).next().value));
252
- return this;
253
- }
254
- async verify(expectSignatures) {
255
- return this._header.verify() && verifyMultiSig(this, expectSignatures);
256
- }
257
351
  /** Manually ser/der for performance gains */
258
352
  bytes() {
259
- if (this._serialized) {
260
- return this._serialized;
261
- }
262
353
  return serialize(this);
263
354
  }
264
355
  static from(bytes) {
@@ -267,41 +358,15 @@ let DataMessage = DataMessage_1 = class DataMessage extends Message {
267
358
  }
268
359
  const arr = bytes.subarray();
269
360
  const ret = deserialize(arr, DataMessage_1);
270
- ret._serialized = arr;
271
361
  return ret;
272
362
  }
273
- equals(other) {
274
- if (other instanceof DataMessage_1) {
275
- const a = equals(this.data, other.data) &&
276
- equals(this.id, other.id) &&
277
- this.to.length === other.to.length;
278
- if (!a) {
279
- return false;
280
- }
281
- for (let i = 0; i < this.to.length; i++) {
282
- if (this.to[i] !== other.to[i]) {
283
- return false;
284
- }
285
- }
286
- return this.signatures.equals(other.signatures);
287
- }
288
- return false;
289
- }
290
363
  };
291
364
  __decorate([
292
365
  field({ type: MessageHeader }),
293
366
  __metadata("design:type", MessageHeader)
294
367
  ], DataMessage.prototype, "_header", void 0);
295
368
  __decorate([
296
- field({ type: vec("string") }),
297
- __metadata("design:type", Array)
298
- ], DataMessage.prototype, "_to", void 0);
299
- __decorate([
300
- field({ type: Signatures }),
301
- __metadata("design:type", Signatures)
302
- ], DataMessage.prototype, "_signatures", void 0);
303
- __decorate([
304
- field({ type: Uint8Array }),
369
+ field({ type: option(Uint8Array) }),
305
370
  __metadata("design:type", Uint8Array)
306
371
  ], DataMessage.prototype, "_data", void 0);
307
372
  DataMessage = DataMessage_1 = __decorate([
@@ -309,97 +374,71 @@ DataMessage = DataMessage_1 = __decorate([
309
374
  __metadata("design:paramtypes", [Object])
310
375
  ], DataMessage);
311
376
  export { DataMessage };
312
- let NetworkInfo = class NetworkInfo {
313
- pingLatencies;
314
- constructor(pingLatencies) {
315
- this.pingLatencies = pingLatencies;
377
+ const ACKNOWLEDGE_VARIANT = 1;
378
+ let ACK = ACK_1 = class ACK extends Message {
379
+ header;
380
+ messageIdToAcknowledge;
381
+ seenCounter; // Number of times a peer has received the messageIdToAcknowledge before
382
+ constructor(properties) {
383
+ super();
384
+ this.header = properties.header;
385
+ this.messageIdToAcknowledge = properties.messageIdToAcknowledge;
386
+ this.seenCounter = Math.min(255, properties.seenCounter);
387
+ }
388
+ get id() {
389
+ return this.header.id;
390
+ }
391
+ bytes() {
392
+ return serialize(this);
393
+ }
394
+ static from(bytes) {
395
+ const result = deserialize(bytes.subarray(), ACK_1);
396
+ if (!result.header.signatures ||
397
+ result.header.signatures.signatures.length === 0) {
398
+ throw new Error("Missing sender on ACK");
399
+ }
400
+ return result;
316
401
  }
317
402
  };
318
403
  __decorate([
319
- field({ type: vec("u32", SIGNATURES_SIZE_ENCODING) }),
320
- __metadata("design:type", Array)
321
- ], NetworkInfo.prototype, "pingLatencies", void 0);
322
- NetworkInfo = __decorate([
323
- variant(0),
324
- __metadata("design:paramtypes", [Array])
325
- ], NetworkInfo);
326
- export { NetworkInfo };
327
- // I send this too all my peers
328
- const HELLO_VARIANT = 1;
404
+ field({ type: MessageHeader }),
405
+ __metadata("design:type", MessageHeader)
406
+ ], ACK.prototype, "header", void 0);
407
+ __decorate([
408
+ field({ type: fixedArray("u8", 32) }),
409
+ __metadata("design:type", Uint8Array)
410
+ ], ACK.prototype, "messageIdToAcknowledge", void 0);
411
+ __decorate([
412
+ field({ type: "u8" }),
413
+ __metadata("design:type", Number)
414
+ ], ACK.prototype, "seenCounter", void 0);
415
+ ACK = ACK_1 = __decorate([
416
+ variant(ACKNOWLEDGE_VARIANT),
417
+ __metadata("design:paramtypes", [Object])
418
+ ], ACK);
419
+ export { ACK };
420
+ const HELLO_VARIANT = 2;
329
421
  let Hello = Hello_1 = class Hello extends Message {
330
422
  header;
331
- multiaddrs;
332
- data;
333
- networkInfo;
334
- signatures;
335
- constructor(options) {
423
+ joined;
424
+ constructor(properties) {
336
425
  super();
337
- this.header = new MessageHeader();
338
- this.data = options?.data;
339
- this.multiaddrs =
340
- options?.multiaddrs?.filter((x) => !x.includes("/p2p-circuit/")) || []; // don't forward relay addresess (TODO ?)
341
- this.signatures = new Signatures();
342
- this.networkInfo = new NetworkInfo([]);
426
+ this.joined = properties.joined;
343
427
  }
344
- get sender() {
345
- return this.signatures.signatures[0].publicKey;
428
+ get id() {
429
+ return this.header.id;
346
430
  }
347
431
  bytes() {
348
432
  return serialize(this);
349
433
  }
350
434
  static from(bytes) {
351
435
  const result = deserialize(bytes.subarray(), Hello_1);
352
- if (result.signatures.signatures.length === 0) {
436
+ if (!result.header.signatures ||
437
+ result.header.signatures.signatures.length === 0) {
353
438
  throw new Error("Missing sender on Hello");
354
439
  }
355
440
  return result;
356
441
  }
357
- _prefix;
358
- get prefix() {
359
- if (!this._prefix) {
360
- throw new Error("Prefix not created");
361
- }
362
- return this._prefix;
363
- }
364
- async createPrefix() {
365
- if (this._prefix) {
366
- return this._prefix;
367
- }
368
- const headerSer = serialize(this.header);
369
- const hashBytes = this.data ? await sha256(this.data) : new Uint8Array();
370
- this._prefix = concatBytes([new Uint8Array([HELLO_VARIANT]), headerSer, hashBytes], 1 + headerSer.length + hashBytes.length);
371
- return this._prefix;
372
- }
373
- getSuffix(iteration) {
374
- return [
375
- serialize(new NetworkInfo(this.networkInfo.pingLatencies.slice(0, iteration + 1))),
376
- serialize(new Signatures(this.signatures.signatures.slice(0, iteration + 1)))
377
- ];
378
- }
379
- async sign(sign) {
380
- await this.createPrefix();
381
- const toSign = getMultiSigDataToSignHistory(this, this.signatures.signatures.length).next().value;
382
- this.signatures.signatures.push(await sign(toSign));
383
- return this;
384
- }
385
- async verify(expectSignatures) {
386
- return (this.header.verify() &&
387
- this.networkInfo.pingLatencies.length ===
388
- this.signatures.signatures.length - 1 &&
389
- verifyMultiSig(this, expectSignatures));
390
- }
391
- equals(other) {
392
- if (other instanceof Hello_1) {
393
- const dataEquals = (!!this.data && !!other.data && equals(this.data, other.data)) ||
394
- !this.data === !other.data;
395
- if (!dataEquals) {
396
- return false;
397
- }
398
- return (this.header.equals(other.header) &&
399
- this.signatures.equals(other.signatures));
400
- }
401
- return false;
402
- }
403
442
  };
404
443
  __decorate([
405
444
  field({ type: MessageHeader }),
@@ -408,281 +447,47 @@ __decorate([
408
447
  __decorate([
409
448
  field({ type: vec("string") }),
410
449
  __metadata("design:type", Array)
411
- ], Hello.prototype, "multiaddrs", void 0);
412
- __decorate([
413
- field({ type: option(Uint8Array) }),
414
- __metadata("design:type", Uint8Array)
415
- ], Hello.prototype, "data", void 0);
416
- __decorate([
417
- field({ type: NetworkInfo }),
418
- __metadata("design:type", NetworkInfo)
419
- ], Hello.prototype, "networkInfo", void 0);
420
- __decorate([
421
- field({ type: Signatures }),
422
- __metadata("design:type", Signatures)
423
- ], Hello.prototype, "signatures", void 0);
450
+ ], Hello.prototype, "joined", void 0);
424
451
  Hello = Hello_1 = __decorate([
425
452
  variant(HELLO_VARIANT),
426
453
  __metadata("design:paramtypes", [Object])
427
454
  ], Hello);
428
455
  export { Hello };
429
- // Me or some my peer disconnected
430
- const GOODBYE_VARIANT = 2;
456
+ const GOODBYE_VARIANT = 3;
431
457
  let Goodbye = Goodbye_1 = class Goodbye extends Message {
432
458
  header;
433
- early; // is early goodbye, I send this to my peers so when I disconnect, they can relay the message for me
434
- data; // not signed
435
- signatures;
459
+ leaving;
436
460
  constructor(properties) {
437
- // disconnected: PeerId | string,
438
461
  super();
439
- this.header = properties?.header || new MessageHeader();
440
- this.data = properties?.data;
441
- this.early = properties?.early;
442
- this.signatures = new Signatures();
462
+ this.header = properties.header;
463
+ this.leaving = properties.leaving;
443
464
  }
444
- get sender() {
445
- return this.signatures.signatures[0].publicKey;
465
+ get id() {
466
+ return this.header.id;
446
467
  }
447
468
  bytes() {
448
469
  return serialize(this);
449
470
  }
450
471
  static from(bytes) {
451
472
  const result = deserialize(bytes.subarray(), Goodbye_1);
452
- if (result.signatures.signatures.length === 0) {
473
+ if (!result.header.signatures ||
474
+ result.header.signatures.signatures.length === 0) {
453
475
  throw new Error("Missing sender on Goodbye");
454
476
  }
455
477
  return result;
456
478
  }
457
- _prefix;
458
- get prefix() {
459
- if (!this._prefix) {
460
- throw new Error("Prefix not created");
461
- }
462
- return this._prefix;
463
- }
464
- async createPrefix() {
465
- if (this._prefix) {
466
- return this._prefix;
467
- }
468
- const headerSer = serialize(this.header);
469
- const hashBytes = this.data ? await sha256(this.data) : new Uint8Array();
470
- this._prefix = concatBytes([new Uint8Array([GOODBYE_VARIANT]), headerSer, hashBytes], 1 + headerSer.length + 1 + hashBytes.length);
471
- return this._prefix;
472
- }
473
- getSuffix(iteration) {
474
- return serialize(new Signatures(this.signatures.signatures.slice(0, iteration + 1)));
475
- }
476
- async sign(sign) {
477
- await this.createPrefix();
478
- this.signatures.signatures.push(await sign(getMultiSigDataToSignHistory(this, this.signatures.signatures.length).next().value));
479
- return this;
480
- }
481
- async verify(expectSignatures) {
482
- return this.header.verify() && verifyMultiSig(this, expectSignatures);
483
- }
484
- equals(other) {
485
- if (other instanceof Goodbye_1) {
486
- if (this.early !== other.early) {
487
- return false;
488
- }
489
- const dataEquals = (!!this.data && !!other.data && equals(this.data, other.data)) ||
490
- !this.data === !other.data;
491
- if (!dataEquals) {
492
- return false;
493
- }
494
- return (this.header.equals(other.header) &&
495
- this.signatures.equals(other.signatures));
496
- }
497
- return false;
498
- }
499
479
  };
500
480
  __decorate([
501
481
  field({ type: MessageHeader }),
502
482
  __metadata("design:type", MessageHeader)
503
483
  ], Goodbye.prototype, "header", void 0);
504
484
  __decorate([
505
- field({ type: "bool" }),
506
- __metadata("design:type", Boolean)
507
- ], Goodbye.prototype, "early", void 0);
508
- __decorate([
509
- field({ type: option(Uint8Array) }),
510
- __metadata("design:type", Uint8Array)
511
- ], Goodbye.prototype, "data", void 0);
512
- __decorate([
513
- field({ type: Signatures }),
514
- __metadata("design:type", Signatures)
515
- ], Goodbye.prototype, "signatures", void 0);
485
+ field({ type: vec("string") }),
486
+ __metadata("design:type", Array)
487
+ ], Goodbye.prototype, "leaving", void 0);
516
488
  Goodbye = Goodbye_1 = __decorate([
517
489
  variant(GOODBYE_VARIANT),
518
490
  __metadata("design:paramtypes", [Object])
519
491
  ], Goodbye);
520
492
  export { Goodbye };
521
- const PING_VARIANT = 3;
522
- let PingPong = PingPong_1 = class PingPong extends Message {
523
- static from(bytes) {
524
- return deserialize(bytes.subarray(), PingPong_1);
525
- }
526
- bytes() {
527
- return serialize(this);
528
- }
529
- verify(_expectSignatures) {
530
- return Promise.resolve(true);
531
- }
532
- };
533
- PingPong = PingPong_1 = __decorate([
534
- variant(PING_VARIANT)
535
- ], PingPong);
536
- export { PingPong };
537
- let Ping = Ping_1 = class Ping extends PingPong {
538
- pingBytes;
539
- constructor() {
540
- super();
541
- this.pingBytes = randomBytes(32);
542
- }
543
- equals(other) {
544
- if (other instanceof Ping_1) {
545
- return equals(this.pingBytes, other.pingBytes);
546
- }
547
- return false;
548
- }
549
- };
550
- __decorate([
551
- field({ type: fixedArray("u8", 32) }),
552
- __metadata("design:type", Uint8Array)
553
- ], Ping.prototype, "pingBytes", void 0);
554
- Ping = Ping_1 = __decorate([
555
- variant(0),
556
- __metadata("design:paramtypes", [])
557
- ], Ping);
558
- export { Ping };
559
- let Pong = Pong_1 = class Pong extends PingPong {
560
- pingBytes;
561
- constructor(pingBytes) {
562
- super();
563
- this.pingBytes = pingBytes;
564
- }
565
- equals(other) {
566
- if (other instanceof Pong_1) {
567
- return equals(this.pingBytes, other.pingBytes);
568
- }
569
- return false;
570
- }
571
- };
572
- __decorate([
573
- field({ type: fixedArray("u8", 32) }),
574
- __metadata("design:type", Uint8Array)
575
- ], Pong.prototype, "pingBytes", void 0);
576
- Pong = Pong_1 = __decorate([
577
- variant(1),
578
- __metadata("design:paramtypes", [Uint8Array])
579
- ], Pong);
580
- export { Pong };
581
- let Connections = class Connections {
582
- connections;
583
- constructor(connections) {
584
- this.connections = connections;
585
- }
586
- equals(other) {
587
- if (this.connections.length !== other.connections.length) {
588
- return false;
589
- }
590
- for (let i = 0; i < this.connections.length; i++) {
591
- if (this.connections[i].length !== other.connections[i].length) {
592
- return false;
593
- }
594
- const a1 = this.connections[i][0];
595
- const a2 = this.connections[i][1];
596
- const b1 = other.connections[i][0];
597
- const b2 = other.connections[i][1];
598
- if (a1 === b1 && a2 === b2) {
599
- continue;
600
- }
601
- if (a1 === b2 && a2 === b1) {
602
- continue;
603
- }
604
- return false;
605
- }
606
- return true;
607
- }
608
- };
609
- __decorate([
610
- field({ type: vec(fixedArray("string", 2)) }),
611
- __metadata("design:type", Array)
612
- ], Connections.prototype, "connections", void 0);
613
- Connections = __decorate([
614
- variant(0),
615
- __metadata("design:paramtypes", [Array])
616
- ], Connections);
617
- export { Connections };
618
- // Share connections
619
- /* const NETWORK_INFO_VARIANT = 3;
620
- @variant(NETWORK_INFO_VARIANT)
621
- export class NetworkInfo extends Message {
622
-
623
- @field({ type: MessageHeader })
624
- header: MessageHeader;
625
-
626
- @field({ type: Connections })
627
- connections: Connections;
628
-
629
-
630
- @field({ type: Signatures })
631
- signatures: Signatures
632
-
633
-
634
- constructor(connections: [string, string][]) {
635
- super();
636
- this.header = new MessageHeader();
637
- this.connections = new Connections(connections);
638
- this.signatures = new Signatures()
639
- }
640
-
641
- getDataToSign(): Uint8Array {
642
- return this.serialize()
643
- }
644
-
645
- _prefix: Uint8Array | undefined
646
- get prefix(): Uint8Array {
647
- if (this._prefix)
648
- return this._prefix
649
- const header = serialize(this.header);
650
- const connections = serialize(this.connections);
651
- this._prefix = concatBytes([new Uint8Array([NETWORK_INFO_VARIANT]), header, connections], 1 + header.length + connections.length);
652
- return this._prefix;
653
- }
654
-
655
- sign(sign: (bytes: Uint8Array) => SignatureWithKey) {
656
- this.signatures.signatures.push(sign(getMultiSigDataToSignHistory(this, this.signatures.signatures.length).next().value!));
657
- return this;
658
- }
659
-
660
- verify(): boolean {
661
- return this.header.verify() && verifyMultiSig(this)
662
- }
663
-
664
-
665
- serialize() {
666
- return serialize(this)
667
- }
668
- static from(bytes: Uint8ArrayList): NetworkInfo {
669
- return deserialize(bytes.subarray(), NetworkInfo)
670
- }
671
-
672
- equals(other: Message) {
673
- if (other instanceof NetworkInfo) {
674
- if (!equals(this.header.id, other.header.id) || !this.header.equals(other.header)) { // TODO fix uneccessary copy
675
- return false;
676
- }
677
-
678
- if (!this.connections.equals(other.connections)) {
679
- return false;
680
- }
681
- return this.signatures.equals(other.signatures)
682
- }
683
- return false;
684
- }
685
- }
686
-
687
- */
688
493
  //# sourceMappingURL=messages.js.map