@peerbit/log 1.0.2

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 (92) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +11 -0
  3. package/lib/esm/change.d.ts +5 -0
  4. package/lib/esm/change.js +2 -0
  5. package/lib/esm/change.js.map +1 -0
  6. package/lib/esm/clock.d.ts +87 -0
  7. package/lib/esm/clock.js +260 -0
  8. package/lib/esm/clock.js.map +1 -0
  9. package/lib/esm/difference.d.ts +1 -0
  10. package/lib/esm/difference.js +20 -0
  11. package/lib/esm/difference.js.map +1 -0
  12. package/lib/esm/encoding.d.ts +7 -0
  13. package/lib/esm/encoding.js +20 -0
  14. package/lib/esm/encoding.js.map +1 -0
  15. package/lib/esm/entry-index.d.ts +21 -0
  16. package/lib/esm/entry-index.js +63 -0
  17. package/lib/esm/entry-index.js.map +1 -0
  18. package/lib/esm/entry-with-refs.d.ts +5 -0
  19. package/lib/esm/entry-with-refs.js +2 -0
  20. package/lib/esm/entry-with-refs.js.map +1 -0
  21. package/lib/esm/entry.d.ts +179 -0
  22. package/lib/esm/entry.js +591 -0
  23. package/lib/esm/entry.js.map +1 -0
  24. package/lib/esm/find-uniques.d.ts +1 -0
  25. package/lib/esm/find-uniques.js +12 -0
  26. package/lib/esm/find-uniques.js.map +1 -0
  27. package/lib/esm/heads-cache.d.ts +64 -0
  28. package/lib/esm/heads-cache.js +317 -0
  29. package/lib/esm/heads-cache.js.map +1 -0
  30. package/lib/esm/heads.d.ts +63 -0
  31. package/lib/esm/heads.js +143 -0
  32. package/lib/esm/heads.js.map +1 -0
  33. package/lib/esm/hrtime.d.ts +5 -0
  34. package/lib/esm/hrtime.js +71 -0
  35. package/lib/esm/hrtime.js.map +1 -0
  36. package/lib/esm/index.d.ts +11 -0
  37. package/lib/esm/index.js +11 -0
  38. package/lib/esm/index.js.map +1 -0
  39. package/lib/esm/is-defined.d.ts +1 -0
  40. package/lib/esm/is-defined.js +2 -0
  41. package/lib/esm/is-defined.js.map +1 -0
  42. package/lib/esm/log-errors.d.ts +5 -0
  43. package/lib/esm/log-errors.js +6 -0
  44. package/lib/esm/log-errors.js.map +1 -0
  45. package/lib/esm/log-sorting.d.ts +44 -0
  46. package/lib/esm/log-sorting.js +86 -0
  47. package/lib/esm/log-sorting.js.map +1 -0
  48. package/lib/esm/log.d.ts +205 -0
  49. package/lib/esm/log.js +1004 -0
  50. package/lib/esm/log.js.map +1 -0
  51. package/lib/esm/logger.d.ts +2 -0
  52. package/lib/esm/logger.js +4 -0
  53. package/lib/esm/logger.js.map +1 -0
  54. package/lib/esm/package.json +3 -0
  55. package/lib/esm/snapshot.d.ts +22 -0
  56. package/lib/esm/snapshot.js +83 -0
  57. package/lib/esm/snapshot.js.map +1 -0
  58. package/lib/esm/trim.d.ts +49 -0
  59. package/lib/esm/trim.js +203 -0
  60. package/lib/esm/trim.js.map +1 -0
  61. package/lib/esm/types.d.ts +6 -0
  62. package/lib/esm/types.js +23 -0
  63. package/lib/esm/types.js.map +1 -0
  64. package/lib/esm/utils.d.ts +2 -0
  65. package/lib/esm/utils.js +3 -0
  66. package/lib/esm/utils.js.map +1 -0
  67. package/lib/esm/values.d.ts +33 -0
  68. package/lib/esm/values.js +141 -0
  69. package/lib/esm/values.js.map +1 -0
  70. package/package.json +79 -0
  71. package/src/change.ts +2 -0
  72. package/src/clock.ts +280 -0
  73. package/src/difference.ts +22 -0
  74. package/src/encoding.ts +27 -0
  75. package/src/entry-index.ts +78 -0
  76. package/src/entry-with-refs.ts +6 -0
  77. package/src/entry.ts +749 -0
  78. package/src/find-uniques.ts +14 -0
  79. package/src/heads-cache.ts +400 -0
  80. package/src/heads.ts +208 -0
  81. package/src/hrtime.ts +78 -0
  82. package/src/index.ts +11 -0
  83. package/src/is-defined.ts +1 -0
  84. package/src/log-errors.ts +9 -0
  85. package/src/log-sorting.ts +108 -0
  86. package/src/log.ts +1262 -0
  87. package/src/logger.ts +3 -0
  88. package/src/snapshot.ts +103 -0
  89. package/src/trim.ts +269 -0
  90. package/src/types.ts +12 -0
  91. package/src/utils.ts +2 -0
  92. package/src/values.ts +193 -0
package/src/entry.ts ADDED
@@ -0,0 +1,749 @@
1
+ import { HLC, LamportClock as Clock, Timestamp } from "./clock.js";
2
+ import { isDefined } from "./is-defined.js";
3
+ import {
4
+ variant,
5
+ field,
6
+ serialize,
7
+ deserialize,
8
+ option,
9
+ vec,
10
+ fixedArray,
11
+ } from "@dao-xyz/borsh";
12
+
13
+ import {
14
+ DecryptedThing,
15
+ MaybeEncrypted,
16
+ PublicSignKey,
17
+ X25519PublicKey,
18
+ SignatureWithKey,
19
+ AccessError,
20
+ Ed25519PublicKey,
21
+ sha256Base64,
22
+ randomBytes,
23
+ Identity,
24
+ Keychain,
25
+ X25519Keypair,
26
+ } from "@peerbit/crypto";
27
+ import { verify } from "@peerbit/crypto";
28
+ import { compare, equals } from "@peerbit/uint8arrays";
29
+ import { Encoding, NO_ENCODING } from "./encoding.js";
30
+ import { StringArray } from "./types.js";
31
+ import { logger } from "./logger.js";
32
+ import { Blocks } from "@peerbit/blocks-interface";
33
+
34
+ export type MaybeEncryptionPublicKey =
35
+ | X25519PublicKey
36
+ | X25519PublicKey[]
37
+ | Ed25519PublicKey
38
+ | Ed25519PublicKey[]
39
+ | undefined;
40
+
41
+ const isMaybeEryptionPublicKey = (o: any) => {
42
+ if (!o) {
43
+ return true;
44
+ }
45
+ if (o instanceof X25519PublicKey || o instanceof Ed25519PublicKey) {
46
+ return true;
47
+ }
48
+ if (Array.isArray(o)) {
49
+ return true; // assume entries are either X25519PublicKey or Ed25519PublicKey
50
+ }
51
+ return false;
52
+ };
53
+
54
+ export type EncryptionTemplateMaybeEncrypted = EntryEncryptionTemplate<
55
+ MaybeEncryptionPublicKey,
56
+ MaybeEncryptionPublicKey,
57
+ MaybeEncryptionPublicKey | { [key: string]: MaybeEncryptionPublicKey }, // signature either all signature encrypted by same key, or each individually
58
+ MaybeEncryptionPublicKey
59
+ >;
60
+ export interface EntryEncryption {
61
+ reciever: EncryptionTemplateMaybeEncrypted;
62
+ keypair: X25519Keypair;
63
+ }
64
+
65
+ function arrayToHex(arr: Uint8Array): string {
66
+ return [...new Uint8Array(arr)]
67
+ .map((b) => b.toString(16).padStart(2, "0"))
68
+ .join("");
69
+ }
70
+
71
+ export function toBufferLE(num: bigint, width: number): Uint8Array {
72
+ const hex = num.toString(16);
73
+ const padded = hex.padStart(width * 2, "0").slice(0, width * 2);
74
+ const arr = padded.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16));
75
+ if (!arr) {
76
+ throw new Error("Unexpected");
77
+ }
78
+ const buffer = Uint8Array.from(arr);
79
+ buffer.reverse();
80
+ return buffer;
81
+ }
82
+
83
+ export function toBigIntLE(buf: Uint8Array): bigint {
84
+ const reversed = buf.reverse();
85
+ const hex = arrayToHex(reversed);
86
+ if (hex.length === 0) {
87
+ return BigInt(0);
88
+ }
89
+ return BigInt(`0x${hex}`);
90
+ }
91
+
92
+ export type CanAppend<T> = (canAppend: Entry<T>) => Promise<boolean> | boolean;
93
+
94
+ @variant(0)
95
+ export class Payload<T> {
96
+ @field({ type: Uint8Array })
97
+ data: Uint8Array;
98
+
99
+ encoding: Encoding<T>;
100
+
101
+ private _value?: T;
102
+
103
+ constructor(props: { data: Uint8Array; value?: T; encoding: Encoding<T> }) {
104
+ this.data = props.data;
105
+ this._value = props.value;
106
+ this.encoding = props?.encoding;
107
+ }
108
+
109
+ equals(other: Payload<T>): boolean {
110
+ return equals(this.data, other.data);
111
+ }
112
+
113
+ getValue(encoding: Encoding<T> = this.encoding || NO_ENCODING): T {
114
+ if (this._value != undefined) {
115
+ return this._value;
116
+ }
117
+ return encoding.decoder(this.data);
118
+ }
119
+ }
120
+
121
+ export interface EntryEncryptionTemplate<A, B, C, D> {
122
+ metadata: A;
123
+ payload: B;
124
+ signatures: C;
125
+ next: D;
126
+ }
127
+
128
+ export enum EntryType {
129
+ APPEND = 0, // Add more data
130
+ CUT = 1, // Delete or Create tombstone ... delete all nexts, i
131
+ }
132
+
133
+ @variant(0)
134
+ export class Metadata {
135
+ @field({ type: "string" })
136
+ gid: string; // graph id
137
+
138
+ @field({ type: Clock })
139
+ clock: Clock;
140
+
141
+ @field({ type: "u64" })
142
+ maxChainLength: bigint; // longest chain/merkle tree path frmo this node. maxChainLength := max ( maxChainLength(this.next) , 1)
143
+
144
+ @field({ type: "u8" })
145
+ type: EntryType;
146
+
147
+ constructor(properties?: {
148
+ gid: string;
149
+ clock: Clock;
150
+ maxChainLength: bigint;
151
+ type: EntryType;
152
+ }) {
153
+ if (properties) {
154
+ this.gid = properties.gid;
155
+ this.clock = properties.clock;
156
+ this.maxChainLength = properties.maxChainLength;
157
+ this.type = properties.type;
158
+ }
159
+ }
160
+ }
161
+
162
+ @variant(0)
163
+ export class Signatures {
164
+ @field({ type: vec(MaybeEncrypted) })
165
+ signatures: MaybeEncrypted<SignatureWithKey>[];
166
+
167
+ constructor(properties?: { signatures: MaybeEncrypted<SignatureWithKey>[] }) {
168
+ if (properties) {
169
+ this.signatures = properties.signatures;
170
+ }
171
+ }
172
+
173
+ equals(other: Signatures) {
174
+ if (this.signatures.length !== other.signatures.length) {
175
+ return false;
176
+ }
177
+ for (let i = 0; i < this.signatures.length; i++) {
178
+ if (!this.signatures[i].equals(other.signatures[i])) {
179
+ return false;
180
+ }
181
+ }
182
+ return true;
183
+ }
184
+ }
185
+
186
+ const maybeEncrypt = <Q>(
187
+ thing: Q,
188
+ keypair?: X25519Keypair,
189
+ reciever?: MaybeEncryptionPublicKey
190
+ ): Promise<MaybeEncrypted<Q>> | MaybeEncrypted<Q> => {
191
+ const recievers = reciever
192
+ ? Array.isArray(reciever)
193
+ ? reciever
194
+ : [reciever]
195
+ : undefined;
196
+ if (recievers?.length && recievers?.length > 0) {
197
+ if (!keypair) {
198
+ throw new Error("Keypair not provided");
199
+ }
200
+ return new DecryptedThing<Q>({
201
+ data: serialize(thing),
202
+ value: thing,
203
+ }).encrypt(keypair, ...recievers);
204
+ }
205
+ return new DecryptedThing<Q>({
206
+ data: serialize(thing),
207
+ value: thing,
208
+ });
209
+ };
210
+
211
+ @variant(0)
212
+ export class Entry<T>
213
+ implements
214
+ EntryEncryptionTemplate<
215
+ Metadata,
216
+ Payload<T>,
217
+ SignatureWithKey[],
218
+ Array<string>
219
+ >
220
+ {
221
+ @field({ type: MaybeEncrypted })
222
+ _metadata: MaybeEncrypted<Metadata>;
223
+
224
+ @field({ type: MaybeEncrypted })
225
+ _payload: MaybeEncrypted<Payload<T>>;
226
+
227
+ @field({ type: MaybeEncrypted })
228
+ _next: MaybeEncrypted<StringArray>; // Array of hashes (the tree)
229
+
230
+ @field({ type: fixedArray("u8", 4) })
231
+ _reserved: Uint8Array;
232
+
233
+ @field({ type: option(Signatures) })
234
+ _signatures?: Signatures;
235
+
236
+ @field({ type: option("string") }) // we do option because we serialize and store this in a block without the hash, to recieve the hash, which we later set
237
+ hash: string; // "zd...Foo", we'll set the hash after persisting the entry
238
+
239
+ createdLocally?: boolean;
240
+
241
+ private _keychain?: Keychain;
242
+ private _encoding?: Encoding<T>;
243
+
244
+ constructor(obj: {
245
+ payload: MaybeEncrypted<Payload<T>>;
246
+ signatures?: Signatures;
247
+ metadata: MaybeEncrypted<Metadata>;
248
+ next: MaybeEncrypted<StringArray>;
249
+ reserved?: Uint8Array; // intentational type 0 (not used)h
250
+ hash?: string;
251
+ createdLocally?: boolean;
252
+ }) {
253
+ this._metadata = obj.metadata;
254
+ this._payload = obj.payload;
255
+ this._signatures = obj.signatures;
256
+ this._next = obj.next;
257
+ this._reserved = obj.reserved || new Uint8Array([0, 0, 0, 0]);
258
+ this.createdLocally = obj.createdLocally;
259
+ }
260
+
261
+ init(
262
+ props:
263
+ | {
264
+ keychain?: Keychain;
265
+ encoding: Encoding<T>;
266
+ }
267
+ | Entry<T>
268
+ ): Entry<T> {
269
+ if (props instanceof Entry) {
270
+ this._keychain = props._keychain;
271
+ this._encoding = props._encoding;
272
+ } else {
273
+ this._keychain = props.keychain;
274
+ this._encoding = props.encoding;
275
+ }
276
+ return this;
277
+ }
278
+
279
+ get encoding() {
280
+ if (!this._encoding) {
281
+ throw new Error("Not initialized");
282
+ }
283
+ return this._encoding;
284
+ }
285
+
286
+ get metadata(): Metadata {
287
+ return this._metadata.decrypted.getValue(Metadata);
288
+ }
289
+
290
+ async getMetadata(): Promise<Metadata> {
291
+ await this._metadata.decrypt(this._keychain);
292
+ return this.metadata;
293
+ }
294
+
295
+ get gid(): string {
296
+ return this.metadata.gid;
297
+ }
298
+ async getGid(): Promise<string> {
299
+ return (await this.getMetadata()).gid;
300
+ }
301
+
302
+ async getClock(): Promise<Clock> {
303
+ return (await this.getMetadata()).clock;
304
+ }
305
+
306
+ get maxChainLength(): bigint {
307
+ return this._metadata.decrypted.getValue(Metadata).maxChainLength;
308
+ }
309
+
310
+ async getMaxChainLength(): Promise<bigint> {
311
+ return (await this.getMetadata()).maxChainLength;
312
+ }
313
+
314
+ get payload(): Payload<T> {
315
+ const payload = this._payload.decrypted.getValue(Payload);
316
+ payload.encoding = payload.encoding || this.encoding;
317
+ return payload;
318
+ }
319
+
320
+ async getPayload(): Promise<Payload<T>> {
321
+ if (this._payload instanceof DecryptedThing) {
322
+ return this.payload;
323
+ }
324
+
325
+ await this._payload.decrypt(this._keychain);
326
+ return this.payload;
327
+ }
328
+
329
+ async getPayloadValue(): Promise<T> {
330
+ const payload = await this.getPayload();
331
+ return payload.getValue(this.encoding);
332
+ }
333
+
334
+ get publicKeys(): PublicSignKey[] {
335
+ return this.signatures.map((x) => x.publicKey);
336
+ }
337
+
338
+ async getPublicKeys(): Promise<PublicSignKey[]> {
339
+ await this.getSignatures();
340
+ return this.publicKeys;
341
+ }
342
+
343
+ get next(): string[] {
344
+ return this._next.decrypted.getValue(StringArray).arr;
345
+ }
346
+
347
+ async getNext(): Promise<string[]> {
348
+ await this._next.decrypt(this._keychain);
349
+ return this.next;
350
+ }
351
+
352
+ /**
353
+ * Will only return signatures I can decrypt
354
+ * @returns signatures
355
+ */
356
+ get signatures(): SignatureWithKey[] {
357
+ const signatures = this._signatures!.signatures.filter((x) => {
358
+ try {
359
+ x.decrypted;
360
+ return true;
361
+ } catch (error) {
362
+ return false;
363
+ }
364
+ }).map((x) => x.decrypted.getValue(SignatureWithKey));
365
+ if (signatures.length === 0) {
366
+ this._signatures?.signatures.forEach((x) => x.clear());
367
+ throw new Error("Failed to resolve any signature");
368
+ }
369
+ return signatures;
370
+ }
371
+ /**
372
+ * Will only return signatures I can decrypt
373
+ * @returns signatures
374
+ */
375
+ async getSignatures(): Promise<SignatureWithKey[]> {
376
+ const results = await Promise.allSettled(
377
+ this._signatures!.signatures.map((x) => x.decrypt(this._keychain))
378
+ );
379
+
380
+ if (logger.level === "debug" || logger.level === "trace") {
381
+ for (const [i, result] of results.entries()) {
382
+ if (result.status === "rejected") {
383
+ logger.debug("Failed to decrypt signature with index: " + i);
384
+ }
385
+ }
386
+ }
387
+ return this.signatures;
388
+ }
389
+
390
+ /**
391
+ * Will only verify signatures I can decrypt
392
+ * @returns true if all are verified
393
+ */
394
+ async verifySignatures(): Promise<boolean> {
395
+ const signatures = await this.getSignatures();
396
+
397
+ if (signatures.length === 0) {
398
+ return false;
399
+ }
400
+
401
+ for (const signature of signatures) {
402
+ if (!(await verify(signature, Entry.toSignable(this)))) {
403
+ return false;
404
+ }
405
+ }
406
+ return true;
407
+ }
408
+
409
+ static toSignable(entry: Entry<any>): Uint8Array {
410
+ // TODO fix types
411
+ const trimmed = new Entry({
412
+ metadata: entry._metadata,
413
+ next: entry._next,
414
+ payload: entry._payload,
415
+ reserved: entry._reserved,
416
+ signatures: undefined,
417
+ hash: undefined,
418
+ });
419
+ return serialize(trimmed);
420
+ }
421
+
422
+ toSignable(): Uint8Array {
423
+ if (this._signatures) {
424
+ throw new Error("Expected signatures to be undefined");
425
+ }
426
+
427
+ if (this.hash) {
428
+ throw new Error("Expected hash to be undefined");
429
+ }
430
+ return Entry.toSignable(this);
431
+ }
432
+
433
+ equals(other: Entry<T>) {
434
+ return (
435
+ equals(this._reserved, other._reserved) &&
436
+ this._metadata.equals(other._metadata) &&
437
+ this._signatures!.equals(other._signatures!) &&
438
+ this._next.equals(other._next) &&
439
+ this._payload.equals(other._payload)
440
+ ); // dont compare hashes because the hash is a function of the other properties
441
+ }
442
+
443
+ async delete(store: Blocks): Promise<void> {
444
+ if (!this.hash) {
445
+ throw new Error("Missing hash");
446
+ }
447
+ await store.rm(this.hash);
448
+ }
449
+
450
+ static createGid(seed?: Uint8Array): Promise<string> {
451
+ return sha256Base64(seed || randomBytes(32));
452
+ }
453
+
454
+ static async create<T>(properties: {
455
+ store: Blocks;
456
+ gid?: string;
457
+ type?: EntryType;
458
+ gidSeed?: Uint8Array;
459
+ data: T;
460
+ encoding?: Encoding<T>;
461
+ canAppend?: CanAppend<T>;
462
+ next?: Entry<T>[];
463
+ clock?: Clock;
464
+ encryption?: EntryEncryption;
465
+ identity: Identity;
466
+ signers?: ((
467
+ data: Uint8Array
468
+ ) => Promise<SignatureWithKey> | SignatureWithKey)[];
469
+ }): Promise<Entry<T>> {
470
+ if (!properties.encoding || !properties.next) {
471
+ properties = {
472
+ ...properties,
473
+ next: properties.next ? properties.next : [],
474
+ encoding: properties.encoding ? properties.encoding : NO_ENCODING,
475
+ };
476
+ }
477
+
478
+ if (!properties.encoding) {
479
+ throw new Error("Missing encoding options");
480
+ }
481
+
482
+ if (!isDefined(properties.data)) throw new Error("Entry requires data");
483
+ if (!isDefined(properties.next) || !Array.isArray(properties.next))
484
+ throw new Error("'next' argument is not an array");
485
+
486
+ // Clean the next objects and convert to hashes
487
+ const nexts = properties.next;
488
+
489
+ const payloadToSave = new Payload<T>({
490
+ data: properties.encoding.encoder(properties.data),
491
+ value: properties.data,
492
+ encoding: properties.encoding,
493
+ });
494
+
495
+ let clock: Clock | undefined = properties.clock;
496
+ if (!clock) {
497
+ const hlc = new HLC();
498
+ for (const next of nexts) {
499
+ hlc.update(next.metadata.clock.timestamp);
500
+ }
501
+
502
+ if (
503
+ properties.encryption?.reciever.signatures &&
504
+ properties.encryption?.reciever.metadata
505
+ ) {
506
+ throw new Error(
507
+ "Signature is to be encrypted yet the clock is not, which contains the publicKey as id. Either provide a custom Clock value that is not sensitive or set the reciever (encryption target) for the clock"
508
+ );
509
+ }
510
+ clock = new Clock({
511
+ id: properties.identity.publicKey.bytes,
512
+ timestamp: hlc.now(),
513
+ });
514
+ } else {
515
+ const cv = clock;
516
+ // check if nexts, that all nexts are happening BEFORE this clock value (else clock make no sense)
517
+ for (const n of nexts) {
518
+ if (Timestamp.compare(n.metadata.clock.timestamp, cv.timestamp) >= 0) {
519
+ throw new Error(
520
+ "Expecting next(s) to happen before entry, got: " +
521
+ n.metadata.clock.timestamp +
522
+ " > " +
523
+ cv.timestamp
524
+ );
525
+ }
526
+ }
527
+ }
528
+
529
+ const payload = await maybeEncrypt(
530
+ payloadToSave,
531
+ properties.encryption?.keypair,
532
+ properties.encryption?.reciever.payload
533
+ );
534
+
535
+ const nextHashes: string[] = [];
536
+ let gid!: string;
537
+ let maxChainLength = 0n;
538
+ const maxClock = new Timestamp({ wallTime: 0n, logical: 0 });
539
+ if (nexts?.length > 0) {
540
+ // take min gid as our gid
541
+ for (const n of nexts) {
542
+ if (!n.hash) {
543
+ throw new Error("Expecting hash to be defined to next entries");
544
+ }
545
+ nextHashes.push(n.hash);
546
+ if (
547
+ maxChainLength < n.maxChainLength ||
548
+ maxChainLength == n.maxChainLength
549
+ ) {
550
+ maxChainLength = n.maxChainLength;
551
+ if (!gid) {
552
+ gid = n.metadata.gid;
553
+ continue;
554
+ }
555
+ // replace gid if next is from alonger chain, or from a later time, or same time but "smaller" gid
556
+ else if (
557
+ Timestamp.compare(n.metadata.clock.timestamp, maxClock) > 0 ||
558
+ (Timestamp.compare(n.metadata.clock.timestamp, maxClock) == 0 &&
559
+ n.metadata.gid < gid)
560
+ ) {
561
+ gid = n.metadata.gid;
562
+ }
563
+ }
564
+ }
565
+ if (!gid) {
566
+ throw new Error("Unexpected behaviour, could not find gid");
567
+ }
568
+ } else {
569
+ gid = properties.gid || (await Entry.createGid(properties.gidSeed));
570
+ }
571
+
572
+ maxChainLength += 1n; // include this
573
+
574
+ const metadataEncrypted = await maybeEncrypt(
575
+ new Metadata({
576
+ maxChainLength,
577
+ clock,
578
+ gid,
579
+ type: properties.type ?? EntryType.APPEND,
580
+ }),
581
+ properties.encryption?.keypair,
582
+ properties.encryption?.reciever.metadata
583
+ );
584
+
585
+ const next = nextHashes;
586
+ next?.forEach((next) => {
587
+ if (typeof next !== "string") {
588
+ throw new Error("Unsupported next type");
589
+ }
590
+ });
591
+
592
+ const nextEncrypted = await maybeEncrypt(
593
+ new StringArray({
594
+ arr: next,
595
+ }),
596
+ properties.encryption?.keypair,
597
+ properties.encryption?.reciever.next
598
+ );
599
+
600
+ // Sign id, encrypted payload, clock, nexts, refs
601
+ const entry: Entry<T> = new Entry<T>({
602
+ payload,
603
+ metadata: metadataEncrypted,
604
+ signatures: undefined,
605
+ createdLocally: true,
606
+ next: nextEncrypted, // Array of hashes
607
+ /* refs: properties.refs, */
608
+ });
609
+
610
+ const signers = properties.signers || [
611
+ properties.identity.sign.bind(properties.identity),
612
+ ];
613
+ const signable = entry.toSignable();
614
+ let signatures = await Promise.all(
615
+ signers.map((signer) => signer(signable))
616
+ );
617
+ signatures = signatures.sort((a, b) => compare(a.signature, b.signature));
618
+
619
+ const encryptedSignatures: MaybeEncrypted<SignatureWithKey>[] = [];
620
+ const encryptAllSignaturesWithSameKey = isMaybeEryptionPublicKey(
621
+ properties.encryption?.reciever?.signatures
622
+ );
623
+ for (const signature of signatures) {
624
+ const encryptionRecievers = encryptAllSignaturesWithSameKey
625
+ ? properties.encryption?.reciever?.signatures
626
+ : properties.encryption?.reciever?.signatures?.[
627
+ signature.publicKey.hashcode()
628
+ ];
629
+ const signatureEncrypted = await maybeEncrypt(
630
+ signature,
631
+ properties.encryption?.keypair,
632
+ encryptionRecievers
633
+ );
634
+ encryptedSignatures.push(signatureEncrypted);
635
+ }
636
+
637
+ entry._signatures = new Signatures({
638
+ signatures: encryptedSignatures,
639
+ });
640
+
641
+ if (properties.canAppend && !(await properties.canAppend(entry))) {
642
+ throw new AccessError();
643
+ }
644
+
645
+ // Append hash and signature
646
+ entry.hash = await Entry.toMultihash(properties.store, entry);
647
+
648
+ entry.init({ encoding: properties.encoding });
649
+
650
+ return entry;
651
+ }
652
+
653
+ /**
654
+ * Get the multihash of an Entry.
655
+ * @example
656
+ * const multfihash = await Entry.toMultihash(store, entry)
657
+ * console.log(multihash)
658
+ * // "Qm...Foo"
659
+ */
660
+ static async toMultihash<T>(store: Blocks, entry: Entry<T>): Promise<string> {
661
+ if (entry.hash) {
662
+ throw new Error("Expected hash to be missing");
663
+ }
664
+
665
+ const result = store.put(serialize(entry));
666
+ return result;
667
+ }
668
+
669
+ /**
670
+ * Create an Entry from a hash.
671
+ * @example
672
+ * const entry = await Entry.fromMultihash(store, "zd...Foo")
673
+ * console.log(entry)
674
+ * // { hash: "Zd...Foo", payload: "hello", next: [] }
675
+ */
676
+ static async fromMultihash<T>(
677
+ store: Blocks,
678
+ hash: string,
679
+ options?: { timeout?: number; replicate?: boolean }
680
+ ) {
681
+ if (!hash) throw new Error(`Invalid hash: ${hash}`);
682
+ const bytes = await store.get(hash, options);
683
+ if (!bytes) {
684
+ throw new Error("Failed to resolve block: " + hash);
685
+ }
686
+ const entry = deserialize(bytes, Entry);
687
+ entry.hash = hash;
688
+ return entry as Entry<T>;
689
+ }
690
+
691
+ /**
692
+ * Compares two entries.
693
+ * @param {Entry} a
694
+ * @param {Entry} b
695
+ * @returns {number} 1 if a is greater, -1 is b is greater
696
+ */
697
+ static compare<T>(a: Entry<T>, b: Entry<T>) {
698
+ const aClock = a.metadata.clock;
699
+ const bClock = b.metadata.clock;
700
+ const distance = Clock.compare(aClock, bClock);
701
+ if (distance === 0) return aClock.id < bClock.id ? -1 : 1;
702
+ return distance;
703
+ }
704
+
705
+ /**
706
+ * Check if an entry equals another entry.
707
+ * @param {Entry} a
708
+ * @param {Entry} b
709
+ * @returns {boolean}
710
+ */
711
+ static isEqual<T>(a: Entry<T>, b: Entry<T>) {
712
+ return a.hash === b.hash;
713
+ }
714
+
715
+ /**
716
+ * Check if an entry is a parent to another entry.
717
+ * @param {Entry} entry1 Entry to check
718
+ * @param {Entry} entry2 The parent Entry
719
+ * @returns {boolean}
720
+ */
721
+ static isDirectParent<T>(entry1: Entry<T>, entry2: Entry<T>) {
722
+ return entry2.next.indexOf(entry1.hash as any) > -1; // TODO fix types
723
+ }
724
+
725
+ /**
726
+ * Find entry's children from an Array of entries.
727
+ * Returns entry's children as an Array up to the last know child.
728
+ * @param {Entry} entry Entry for which to find the parents
729
+ * @param {Array<Entry<T>>} values Entries to search parents from
730
+ * @returns {Array<Entry<T>>}
731
+ */
732
+ static findDirectChildren<T>(
733
+ entry: Entry<T>,
734
+ values: Entry<T>[]
735
+ ): Entry<T>[] {
736
+ let stack: Entry<T>[] = [];
737
+ let parent = values.find((e) => Entry.isDirectParent(entry, e));
738
+ let prev = entry;
739
+ while (parent) {
740
+ stack.push(parent);
741
+ prev = parent;
742
+ parent = values.find((e) => Entry.isDirectParent(prev, e));
743
+ }
744
+ stack = stack.sort((a, b) =>
745
+ Clock.compare(a.metadata.clock, b.metadata.clock)
746
+ );
747
+ return stack;
748
+ }
749
+ }