@peerbit/log 4.0.4 → 4.0.5

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 (59) hide show
  1. package/dist/src/clock.d.ts.map +1 -1
  2. package/dist/src/clock.js +20 -11
  3. package/dist/src/clock.js.map +1 -1
  4. package/dist/src/entry-create.d.ts +26 -0
  5. package/dist/src/entry-create.d.ts.map +1 -0
  6. package/dist/src/entry-create.js +5 -0
  7. package/dist/src/entry-create.js.map +1 -0
  8. package/dist/src/entry-index.d.ts +2 -1
  9. package/dist/src/entry-index.d.ts.map +1 -1
  10. package/dist/src/entry-index.js +2 -1
  11. package/dist/src/entry-index.js.map +1 -1
  12. package/dist/src/entry-shallow.d.ts +29 -0
  13. package/dist/src/entry-shallow.d.ts.map +1 -0
  14. package/dist/src/entry-shallow.js +75 -0
  15. package/dist/src/entry-shallow.js.map +1 -0
  16. package/dist/src/entry-type.d.ts +5 -0
  17. package/dist/src/entry-type.d.ts.map +1 -0
  18. package/dist/src/entry-type.js +6 -0
  19. package/dist/src/entry-type.js.map +1 -0
  20. package/dist/src/entry-v0.d.ts +121 -0
  21. package/dist/src/entry-v0.d.ts.map +1 -0
  22. package/dist/src/entry-v0.js +465 -0
  23. package/dist/src/entry-v0.js.map +1 -0
  24. package/dist/src/entry.d.ts +31 -153
  25. package/dist/src/entry.d.ts.map +1 -1
  26. package/dist/src/entry.js +24 -581
  27. package/dist/src/entry.js.map +1 -1
  28. package/dist/src/heads-cache.d.ts +1 -1
  29. package/dist/src/heads-cache.d.ts.map +1 -1
  30. package/dist/src/heads-cache.js +0 -1
  31. package/dist/src/heads-cache.js.map +1 -1
  32. package/dist/src/index.d.ts +5 -0
  33. package/dist/src/index.d.ts.map +1 -1
  34. package/dist/src/index.js +5 -0
  35. package/dist/src/index.js.map +1 -1
  36. package/dist/src/log.d.ts +6 -4
  37. package/dist/src/log.d.ts.map +1 -1
  38. package/dist/src/log.js +22 -82
  39. package/dist/src/log.js.map +1 -1
  40. package/dist/src/payload.d.ts +17 -0
  41. package/dist/src/payload.d.ts.map +1 -0
  42. package/dist/src/payload.js +53 -0
  43. package/dist/src/payload.js.map +1 -0
  44. package/dist/src/trim.d.ts +2 -1
  45. package/dist/src/trim.d.ts.map +1 -1
  46. package/dist/src/trim.js.map +1 -1
  47. package/package.json +3 -3
  48. package/src/clock.ts +3 -2
  49. package/src/entry-create.ts +30 -0
  50. package/src/entry-index.ts +3 -6
  51. package/src/entry-shallow.ts +61 -0
  52. package/src/entry-type.ts +4 -0
  53. package/src/entry-v0.ts +594 -0
  54. package/src/entry.ts +50 -693
  55. package/src/heads-cache.ts +1 -1
  56. package/src/index.ts +5 -0
  57. package/src/log.ts +23 -94
  58. package/src/payload.ts +44 -0
  59. package/src/trim.ts +2 -1
@@ -0,0 +1,594 @@
1
+ import {
2
+ field,
3
+ fixedArray,
4
+ option,
5
+ serialize,
6
+ variant,
7
+ vec,
8
+ } from "@dao-xyz/borsh";
9
+ import { type Blocks } from "@peerbit/blocks-interface";
10
+ import {
11
+ AccessError,
12
+ DecryptedThing,
13
+ Ed25519PublicKey,
14
+ type Identity,
15
+ MaybeEncrypted,
16
+ PublicSignKey,
17
+ SignatureWithKey,
18
+ X25519Keypair,
19
+ X25519PublicKey,
20
+ randomBytes,
21
+ sha256Base64,
22
+ } from "@peerbit/crypto";
23
+ import { verify } from "@peerbit/crypto";
24
+ import { type Keychain } from "@peerbit/keychain";
25
+ import { compare } from "uint8arrays";
26
+ import { LamportClock as Clock, HLC, Timestamp } from "./clock.js";
27
+ import { type Encoding, NO_ENCODING } from "./encoding.js";
28
+ import { ShallowEntry, ShallowMeta } from "./entry-shallow.js";
29
+ import { EntryType } from "./entry-type.js";
30
+ import { type CanAppend, Entry } from "./entry.js";
31
+ import type { SortableEntry } from "./log-sorting.js";
32
+ import { logger } from "./logger.js";
33
+ import { Payload } from "./payload.js";
34
+ import { equals } from "./utils.js";
35
+
36
+ export type MaybeEncryptionPublicKey =
37
+ | X25519PublicKey
38
+ | X25519PublicKey[]
39
+ | Ed25519PublicKey
40
+ | Ed25519PublicKey[]
41
+ | undefined;
42
+
43
+ const isMaybeEryptionPublicKey = (o: any) => {
44
+ if (!o) {
45
+ return true;
46
+ }
47
+ if (o instanceof X25519PublicKey || o instanceof Ed25519PublicKey) {
48
+ return true;
49
+ }
50
+ if (Array.isArray(o)) {
51
+ return true; // assume entries are either X25519PublicKey or Ed25519PublicKey
52
+ }
53
+ return false;
54
+ };
55
+
56
+ export type EncryptionTemplateMaybeEncrypted = EntryEncryptionTemplate<
57
+ MaybeEncryptionPublicKey,
58
+ MaybeEncryptionPublicKey,
59
+ MaybeEncryptionPublicKey | { [key: string]: MaybeEncryptionPublicKey } // signature either all signature encrypted by same key, or each individually
60
+ >;
61
+ export interface EntryEncryption {
62
+ receiver: EncryptionTemplateMaybeEncrypted;
63
+ keypair: X25519Keypair;
64
+ }
65
+
66
+ export interface EntryEncryptionTemplate<A, B, C> {
67
+ meta: A;
68
+ payload: B;
69
+ signatures: C;
70
+ }
71
+
72
+ @variant(0)
73
+ export class Meta {
74
+ @field({ type: Clock })
75
+ clock: Clock;
76
+
77
+ @field({ type: "string" })
78
+ gid: string; // graph id
79
+
80
+ @field({ type: vec("string") })
81
+ next: string[];
82
+
83
+ @field({ type: "u8" })
84
+ type: EntryType;
85
+
86
+ @field({ type: option(Uint8Array) })
87
+ data?: Uint8Array; // Optional metadata
88
+
89
+ constructor(properties: {
90
+ gid: string;
91
+ clock: Clock;
92
+ type: EntryType;
93
+ data?: Uint8Array;
94
+ next: string[];
95
+ }) {
96
+ this.gid = properties.gid;
97
+ this.clock = properties.clock;
98
+ this.type = properties.type;
99
+ this.data = properties.data;
100
+ this.next = properties.next;
101
+ }
102
+ }
103
+
104
+ @variant(0)
105
+ export class Signatures {
106
+ @field({ type: vec(MaybeEncrypted) })
107
+ signatures!: MaybeEncrypted<SignatureWithKey>[];
108
+
109
+ constructor(properties?: { signatures: MaybeEncrypted<SignatureWithKey>[] }) {
110
+ if (properties) {
111
+ this.signatures = properties.signatures;
112
+ }
113
+ }
114
+
115
+ equals(other: Signatures) {
116
+ if (this.signatures.length !== other.signatures.length) {
117
+ return false;
118
+ }
119
+ for (let i = 0; i < this.signatures.length; i++) {
120
+ if (!this.signatures[i].equals(other.signatures[i])) {
121
+ return false;
122
+ }
123
+ }
124
+ return true;
125
+ }
126
+ }
127
+
128
+ const maybeEncrypt = <Q>(
129
+ thing: Q,
130
+ keypair?: X25519Keypair,
131
+ receiver?: MaybeEncryptionPublicKey,
132
+ ): Promise<MaybeEncrypted<Q>> | MaybeEncrypted<Q> => {
133
+ const receivers = receiver
134
+ ? Array.isArray(receiver)
135
+ ? receiver
136
+ : [receiver]
137
+ : undefined;
138
+ if (receivers?.length && receivers?.length > 0) {
139
+ if (!keypair) {
140
+ throw new Error("Keypair not provided");
141
+ }
142
+ return new DecryptedThing<Q>({
143
+ data: serialize(thing),
144
+ value: thing,
145
+ }).encrypt(keypair, receivers);
146
+ }
147
+ return new DecryptedThing<Q>({
148
+ data: serialize(thing),
149
+ value: thing,
150
+ });
151
+ };
152
+
153
+ @variant(0)
154
+ export class EntryV0<T>
155
+ extends Entry<T>
156
+ implements EntryEncryptionTemplate<Meta, Payload<T>, SignatureWithKey[]>
157
+ {
158
+ @field({ type: MaybeEncrypted })
159
+ _meta: MaybeEncrypted<Meta>;
160
+
161
+ @field({ type: MaybeEncrypted })
162
+ _payload: MaybeEncrypted<Payload<T>>;
163
+
164
+ @field({ type: fixedArray("u8", 4) })
165
+ _reserved?: Uint8Array;
166
+
167
+ @field({ type: option(Signatures) })
168
+ _signatures?: Signatures;
169
+
170
+ @field({ type: option("string") }) // we do option because we serialize and store this in a block without the hash, to receive the hash, which we later set
171
+ hash!: string; // "zd...Foo", we'll set the hash after persisting the entry
172
+
173
+ createdLocally?: boolean;
174
+
175
+ private _keychain?: Keychain;
176
+ private _encoding?: Encoding<T>;
177
+
178
+ constructor(obj: {
179
+ payload: MaybeEncrypted<Payload<T>>;
180
+ signatures?: Signatures;
181
+ meta: MaybeEncrypted<Meta>;
182
+ reserved?: Uint8Array; // intentational type 0 (not used)h
183
+ hash?: string;
184
+ createdLocally?: boolean;
185
+ }) {
186
+ super();
187
+ this._meta = obj.meta;
188
+ this._payload = obj.payload;
189
+ this._signatures = obj.signatures;
190
+ this._reserved = new Uint8Array([0, 0, 0, 0]);
191
+ this.createdLocally = obj.createdLocally;
192
+ }
193
+
194
+ init(
195
+ props:
196
+ | {
197
+ keychain?: Keychain;
198
+ encoding: Encoding<T>;
199
+ }
200
+ | EntryV0<T>,
201
+ ): this {
202
+ if (props instanceof Entry) {
203
+ this._keychain = props._keychain;
204
+ this._encoding = props._encoding;
205
+ } else {
206
+ this._keychain = props.keychain;
207
+ this._encoding = props.encoding;
208
+ }
209
+ return this;
210
+ }
211
+
212
+ get encoding() {
213
+ if (!this._encoding) {
214
+ throw new Error("Not initialized");
215
+ }
216
+ return this._encoding;
217
+ }
218
+
219
+ get meta(): Meta {
220
+ return this._meta.decrypted.getValue(Meta);
221
+ }
222
+
223
+ async getMeta(): Promise<Meta> {
224
+ await this._meta.decrypt(this._keychain);
225
+ return this.meta;
226
+ }
227
+
228
+ async getClock(): Promise<Clock> {
229
+ return (await this.getMeta()).clock;
230
+ }
231
+
232
+ get gid(): string {
233
+ return this._meta.decrypted.getValue(Meta).gid;
234
+ }
235
+
236
+ async getGid(): Promise<string> {
237
+ return (await this.getMeta()).gid;
238
+ }
239
+
240
+ get payload(): Payload<T> {
241
+ const payload = this._payload.decrypted.getValue(Payload);
242
+ payload.encoding = payload.encoding || this.encoding;
243
+ return payload;
244
+ }
245
+
246
+ async getPayload(): Promise<Payload<T>> {
247
+ if (this._payload instanceof DecryptedThing) {
248
+ return this.payload;
249
+ }
250
+
251
+ await this._payload.decrypt(this._keychain);
252
+ return this.payload;
253
+ }
254
+
255
+ async getPayloadValue(): Promise<T> {
256
+ const payload = await this.getPayload();
257
+ return payload.isDecoded ? payload.value : payload.getValue(this.encoding);
258
+ }
259
+
260
+ get publicKeys(): PublicSignKey[] {
261
+ return this.signatures.map((x) => x.publicKey);
262
+ }
263
+
264
+ get next(): string[] {
265
+ return this.meta.next;
266
+ }
267
+
268
+ async getNext(): Promise<string[]> {
269
+ return (await this.getMeta()).next;
270
+ }
271
+
272
+ private _size!: number;
273
+
274
+ set size(number: number) {
275
+ this._size = number;
276
+ }
277
+
278
+ get size(): number {
279
+ if (this._size == null) {
280
+ throw new Error(
281
+ "Size not set. Size is set when entry is, created, loaded or joined",
282
+ );
283
+ }
284
+ return this._size;
285
+ }
286
+
287
+ /**
288
+ * Will only return signatures I can decrypt
289
+ * @returns signatures
290
+ */
291
+ get signatures(): SignatureWithKey[] {
292
+ const signatures = this._signatures!.signatures.filter((x) => {
293
+ try {
294
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
295
+ x.decrypted;
296
+ return true;
297
+ } catch (error) {
298
+ return false;
299
+ }
300
+ }).map((x) => x.decrypted.getValue(SignatureWithKey));
301
+ if (signatures.length === 0) {
302
+ this._signatures?.signatures.forEach((x) => x.clear());
303
+ throw new Error("Failed to resolve any signature");
304
+ }
305
+ return signatures;
306
+ }
307
+ /**
308
+ * Will only return signatures I can decrypt
309
+ * @returns signatures
310
+ */
311
+ async getSignatures(): Promise<SignatureWithKey[]> {
312
+ const results = await Promise.allSettled(
313
+ this._signatures!.signatures.map((x) => x.decrypt(this._keychain)),
314
+ );
315
+
316
+ if (logger.level === "debug" || logger.level === "trace") {
317
+ for (const [i, result] of results.entries()) {
318
+ if (result.status === "rejected") {
319
+ logger.debug("Failed to decrypt signature with index: " + i);
320
+ }
321
+ }
322
+ }
323
+ return this.signatures;
324
+ }
325
+
326
+ /**
327
+ * Will only verify signatures I can decrypt
328
+ * @returns true if all are verified
329
+ */
330
+ async verifySignatures(): Promise<boolean> {
331
+ const signatures = await this.getSignatures();
332
+
333
+ if (signatures.length === 0) {
334
+ return false;
335
+ }
336
+
337
+ const signable = EntryV0.toSignable(this);
338
+ const signableBytes = serialize(signable);
339
+ for (const signature of signatures) {
340
+ if (!(await verify(signature, signableBytes))) {
341
+ return false;
342
+ }
343
+ }
344
+ return true;
345
+ }
346
+
347
+ static toSignable(entry: EntryV0<any>): Entry<any> {
348
+ // TODO fix types
349
+ const trimmed = new EntryV0({
350
+ meta: entry._meta,
351
+ payload: entry._payload,
352
+ reserved: entry._reserved,
353
+ signatures: undefined,
354
+ hash: undefined,
355
+ });
356
+ return trimmed;
357
+ }
358
+
359
+ toSignable(): Entry<any> {
360
+ if (this._signatures) {
361
+ throw new Error("Expected signatures to be undefined");
362
+ }
363
+
364
+ if (this.hash) {
365
+ throw new Error("Expected hash to be undefined");
366
+ }
367
+ return EntryV0.toSignable(this);
368
+ }
369
+
370
+ equals(other: Entry<T>) {
371
+ if (other instanceof EntryV0) {
372
+ return (
373
+ equals(this._reserved, other._reserved) &&
374
+ this._meta.equals(other._meta) &&
375
+ this._signatures!.equals(other._signatures!) &&
376
+ this._payload.equals(other._payload)
377
+ ); // dont compare hashes because the hash is a function of the other properties
378
+ }
379
+
380
+ return false;
381
+ }
382
+
383
+ async delete(store: Blocks): Promise<void> {
384
+ if (!this.hash) {
385
+ throw new Error("Missing hash");
386
+ }
387
+ await store.rm(this.hash);
388
+ }
389
+
390
+ static createGid(seed?: Uint8Array): Promise<string> {
391
+ return sha256Base64(seed || randomBytes(32));
392
+ }
393
+
394
+ static async create<T>(properties: {
395
+ store: Blocks;
396
+ data: T;
397
+ meta?: {
398
+ clock?: Clock;
399
+ gid?: string;
400
+ type?: EntryType;
401
+ gidSeed?: Uint8Array;
402
+ data?: Uint8Array;
403
+ next?: SortableEntry[];
404
+ };
405
+ encoding?: Encoding<T>;
406
+ canAppend?: CanAppend<T>;
407
+ encryption?: EntryEncryption;
408
+ identity: Identity;
409
+ signers?: ((
410
+ data: Uint8Array,
411
+ ) => Promise<SignatureWithKey> | SignatureWithKey)[];
412
+ }): Promise<Entry<T>> {
413
+ if (!properties.encoding || !properties?.meta?.next) {
414
+ properties = {
415
+ ...properties,
416
+ meta: {
417
+ ...properties?.meta,
418
+ next: properties.meta?.next ? properties.meta?.next : [],
419
+ },
420
+ encoding: properties.encoding ? properties.encoding : NO_ENCODING,
421
+ };
422
+ }
423
+
424
+ if (!properties.encoding) {
425
+ throw new Error("Missing encoding options");
426
+ }
427
+
428
+ if (properties.data == null) throw new Error("Entry requires data");
429
+ if (properties.meta?.next == null || !Array.isArray(properties.meta.next))
430
+ throw new Error("'next' argument is not an array");
431
+
432
+ // Clean the next objects and convert to hashes
433
+ const nexts = properties.meta?.next;
434
+
435
+ const payloadToSave = new Payload<T>({
436
+ data: properties.encoding.encoder(properties.data),
437
+ value: properties.data,
438
+ encoding: properties.encoding,
439
+ });
440
+
441
+ let clock: Clock | undefined = properties.meta?.clock;
442
+ if (!clock) {
443
+ const hlc = new HLC();
444
+ for (const next of nexts) {
445
+ hlc.update(next.meta.clock.timestamp);
446
+ }
447
+
448
+ if (
449
+ properties.encryption?.receiver.signatures &&
450
+ properties.encryption?.receiver.meta
451
+ ) {
452
+ throw new Error(
453
+ "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 receiver (encryption target) for the clock",
454
+ );
455
+ }
456
+ clock = new Clock({
457
+ id: properties.identity.publicKey.bytes,
458
+ timestamp: hlc.now(),
459
+ });
460
+ } else {
461
+ const cv = clock;
462
+ // check if nexts, that all nexts are happening BEFORE this clock value (else clock make no sense)
463
+ for (const n of nexts) {
464
+ if (Timestamp.compare(n.meta.clock.timestamp, cv.timestamp) >= 0) {
465
+ throw new Error(
466
+ "Expecting next(s) to happen before entry, got: " +
467
+ n.meta.clock.timestamp +
468
+ " > " +
469
+ cv.timestamp,
470
+ );
471
+ }
472
+ }
473
+ }
474
+
475
+ const nextHashes: string[] = [];
476
+ let maxChainLength = 0n;
477
+ let gid: string | null = null;
478
+ if (nexts?.length > 0) {
479
+ // take min gid as our gid
480
+ if (properties.meta?.gid) {
481
+ throw new Error(
482
+ "Expecting '.meta.gid' property to be undefined if '.meta.next' is provided",
483
+ );
484
+ }
485
+ for (const n of nexts) {
486
+ if (!n.hash) {
487
+ throw new Error("Expecting hash to be defined to next entries");
488
+ }
489
+ nextHashes.push(n.hash);
490
+ gid =
491
+ gid == null
492
+ ? n.meta.gid
493
+ : n.meta.gid < (gid as string)
494
+ ? n.meta.gid
495
+ : gid;
496
+ }
497
+ } else {
498
+ gid =
499
+ properties.meta?.gid ||
500
+ (await EntryV0.createGid(properties.meta?.gidSeed));
501
+ }
502
+
503
+ maxChainLength += 1n; // include this
504
+
505
+ const metadataEncrypted = await maybeEncrypt(
506
+ new Meta({
507
+ clock,
508
+ gid: gid!,
509
+ type: properties.meta?.type ?? EntryType.APPEND,
510
+ data: properties.meta?.data,
511
+ next: nextHashes,
512
+ }),
513
+ properties.encryption?.keypair,
514
+ properties.encryption?.receiver.meta,
515
+ );
516
+
517
+ const payload = await maybeEncrypt(
518
+ payloadToSave,
519
+ properties.encryption?.keypair,
520
+ properties.encryption?.receiver.payload,
521
+ );
522
+
523
+ // Sign id, encrypted payload, clock, nexts, refs
524
+ const entry: EntryV0<T> = new EntryV0<T>({
525
+ meta: metadataEncrypted,
526
+ payload,
527
+ signatures: undefined,
528
+ createdLocally: true,
529
+ });
530
+
531
+ const signers = properties.signers || [
532
+ properties.identity.sign.bind(properties.identity),
533
+ ];
534
+
535
+ const signableBytes = serialize(entry.toSignable());
536
+ let signatures = await Promise.all(
537
+ signers.map((signer) => signer(signableBytes)),
538
+ );
539
+ signatures = signatures.sort((a, b) => compare(a.signature, b.signature));
540
+
541
+ const encryptedSignatures: MaybeEncrypted<SignatureWithKey>[] = [];
542
+ const encryptAllSignaturesWithSameKey = isMaybeEryptionPublicKey(
543
+ properties.encryption?.receiver?.signatures,
544
+ );
545
+
546
+ for (const signature of signatures) {
547
+ const encryptionRecievers = encryptAllSignaturesWithSameKey
548
+ ? properties.encryption?.receiver?.signatures
549
+ : (properties.encryption?.receiver?.signatures as any)?.[
550
+ signature.publicKey.hashcode()
551
+ ]; // TODO types
552
+ const signatureEncrypted = await maybeEncrypt(
553
+ signature,
554
+ properties.encryption?.keypair,
555
+ encryptionRecievers,
556
+ );
557
+ encryptedSignatures.push(signatureEncrypted);
558
+ }
559
+
560
+ entry._signatures = new Signatures({
561
+ signatures: encryptedSignatures,
562
+ });
563
+
564
+ if (properties.canAppend && !(await properties.canAppend(entry))) {
565
+ throw new AccessError();
566
+ }
567
+
568
+ // Append hash
569
+ entry.hash = await Entry.toMultihash(properties.store, entry);
570
+
571
+ entry.init({ encoding: properties.encoding });
572
+
573
+ return entry;
574
+ }
575
+
576
+ get payloadByteLength() {
577
+ return this._payload.byteLength;
578
+ }
579
+
580
+ toShallow(isHead: boolean): ShallowEntry {
581
+ return new ShallowEntry({
582
+ hash: this.hash,
583
+ payloadSize: this._payload.byteLength,
584
+ head: isHead,
585
+ meta: new ShallowMeta({
586
+ gid: this.meta.gid,
587
+ data: this.meta.data,
588
+ clock: this.meta.clock,
589
+ next: this.meta.next,
590
+ type: this.meta.type,
591
+ }),
592
+ });
593
+ }
594
+ }