@peerbit/log 4.1.11 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/benchmark/payload.js +98 -49
  2. package/dist/benchmark/payload.js.map +1 -1
  3. package/dist/src/clock.d.ts +1 -1
  4. package/dist/src/clock.d.ts.map +1 -1
  5. package/dist/src/clock.js +165 -115
  6. package/dist/src/clock.js.map +1 -1
  7. package/dist/src/entry-index.d.ts.map +1 -1
  8. package/dist/src/entry-index.js +4 -3
  9. package/dist/src/entry-index.js.map +1 -1
  10. package/dist/src/entry-shallow.js +119 -69
  11. package/dist/src/entry-shallow.js.map +1 -1
  12. package/dist/src/entry-v0.d.ts.map +1 -1
  13. package/dist/src/entry-v0.js +451 -381
  14. package/dist/src/entry-v0.js.map +1 -1
  15. package/dist/src/entry.d.ts +1 -1
  16. package/dist/src/heads-cache.d.ts +2 -3
  17. package/dist/src/heads-cache.d.ts.map +1 -1
  18. package/dist/src/heads-cache.js +128 -64
  19. package/dist/src/heads-cache.js.map +1 -1
  20. package/dist/src/log-sorting.d.ts +1 -1
  21. package/dist/src/log-sorting.d.ts.map +1 -1
  22. package/dist/src/log.js +838 -802
  23. package/dist/src/log.js.map +1 -1
  24. package/dist/src/logger.d.ts +1 -3
  25. package/dist/src/logger.d.ts.map +1 -1
  26. package/dist/src/logger.js +2 -3
  27. package/dist/src/logger.js.map +1 -1
  28. package/dist/src/payload.js +80 -43
  29. package/dist/src/payload.js.map +1 -1
  30. package/dist/src/snapshot.d.ts +1 -1
  31. package/dist/src/snapshot.d.ts.map +1 -1
  32. package/dist/src/snapshot.js +87 -45
  33. package/dist/src/snapshot.js.map +1 -1
  34. package/package.json +17 -17
  35. package/src/clock.ts +1 -5
  36. package/src/entry-index.ts +5 -3
  37. package/src/entry-v0.ts +6 -6
  38. package/src/heads-cache.ts +2 -4
  39. package/src/log-sorting.ts +1 -1
  40. package/src/logger.ts +2 -3
  41. package/src/snapshot.ts +8 -6
@@ -1,13 +1,37 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
2
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
3
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
4
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
5
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
6
+ var _, done = false;
7
+ for (var i = decorators.length - 1; i >= 0; i--) {
8
+ var context = {};
9
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
10
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
11
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
12
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
13
+ if (kind === "accessor") {
14
+ if (result === void 0) continue;
15
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
16
+ if (_ = accept(result.get)) descriptor.get = _;
17
+ if (_ = accept(result.set)) descriptor.set = _;
18
+ if (_ = accept(result.init)) initializers.unshift(_);
19
+ }
20
+ else if (_ = accept(result)) {
21
+ if (kind === "field") initializers.unshift(_);
22
+ else descriptor[key] = _;
23
+ }
24
+ }
25
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
26
+ done = true;
6
27
  };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
28
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
29
+ var useValue = arguments.length > 2;
30
+ for (var i = 0; i < initializers.length; i++) {
31
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
32
+ }
33
+ return useValue ? value : void 0;
9
34
  };
10
- var EntryV0_1;
11
35
  import { field, fixedArray, option, serialize, variant, vec, } from "@dao-xyz/borsh";
12
36
  import {} from "@peerbit/blocks-interface";
13
37
  import { AccessError, DecryptedThing, Ed25519PublicKey, MaybeEncrypted, PublicSignKey, SignatureWithKey, X25519Keypair, X25519PublicKey, randomBytes, sha256Base64, toBase64, } from "@peerbit/crypto";
@@ -19,9 +43,11 @@ import { NO_ENCODING } from "./encoding.js";
19
43
  import { ShallowEntry, ShallowMeta } from "./entry-shallow.js";
20
44
  import { EntryType } from "./entry-type.js";
21
45
  import { Entry } from "./entry.js";
22
- import { logger } from "./logger.js";
46
+ import { logger as baseLogger } from "./logger.js";
23
47
  import { Payload } from "./payload.js";
24
48
  import { equals } from "./utils.js";
49
+ const log = baseLogger.newScope("entry-v0");
50
+ const traceLogger = log.trace;
25
51
  const isMaybeEryptionPublicKey = (o) => {
26
52
  if (!o) {
27
53
  return true;
@@ -34,72 +60,102 @@ const isMaybeEryptionPublicKey = (o) => {
34
60
  }
35
61
  return false;
36
62
  };
37
- let Meta = class Meta {
38
- clock;
39
- gid; // graph id
40
- next;
41
- type;
42
- data; // Optional metadata
43
- constructor(properties) {
44
- this.gid = properties.gid;
45
- this.clock = properties.clock;
46
- this.type = properties.type;
47
- this.data = properties.data;
48
- this.next = properties.next;
49
- }
50
- };
51
- __decorate([
52
- field({ type: Clock }),
53
- __metadata("design:type", Clock)
54
- ], Meta.prototype, "clock", void 0);
55
- __decorate([
56
- field({ type: "string" }),
57
- __metadata("design:type", String)
58
- ], Meta.prototype, "gid", void 0);
59
- __decorate([
60
- field({ type: vec("string") }),
61
- __metadata("design:type", Array)
62
- ], Meta.prototype, "next", void 0);
63
- __decorate([
64
- field({ type: "u8" }),
65
- __metadata("design:type", Number)
66
- ], Meta.prototype, "type", void 0);
67
- __decorate([
68
- field({ type: option(Uint8Array) }),
69
- __metadata("design:type", Uint8Array)
70
- ], Meta.prototype, "data", void 0);
71
- Meta = __decorate([
72
- variant(0),
73
- __metadata("design:paramtypes", [Object])
74
- ], Meta);
63
+ let Meta = (() => {
64
+ let _classDecorators = [variant(0)];
65
+ let _classDescriptor;
66
+ let _classExtraInitializers = [];
67
+ let _classThis;
68
+ let _clock_decorators;
69
+ let _clock_initializers = [];
70
+ let _clock_extraInitializers = [];
71
+ let _gid_decorators;
72
+ let _gid_initializers = [];
73
+ let _gid_extraInitializers = [];
74
+ let _next_decorators;
75
+ let _next_initializers = [];
76
+ let _next_extraInitializers = [];
77
+ let _type_decorators;
78
+ let _type_initializers = [];
79
+ let _type_extraInitializers = [];
80
+ let _data_decorators;
81
+ let _data_initializers = [];
82
+ let _data_extraInitializers = [];
83
+ var Meta = class {
84
+ static { _classThis = this; }
85
+ static {
86
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
87
+ _clock_decorators = [field({ type: Clock })];
88
+ _gid_decorators = [field({ type: "string" })];
89
+ _next_decorators = [field({ type: vec("string") })];
90
+ _type_decorators = [field({ type: "u8" })];
91
+ _data_decorators = [field({ type: option(Uint8Array) })];
92
+ __esDecorate(null, null, _clock_decorators, { kind: "field", name: "clock", static: false, private: false, access: { has: obj => "clock" in obj, get: obj => obj.clock, set: (obj, value) => { obj.clock = value; } }, metadata: _metadata }, _clock_initializers, _clock_extraInitializers);
93
+ __esDecorate(null, null, _gid_decorators, { kind: "field", name: "gid", static: false, private: false, access: { has: obj => "gid" in obj, get: obj => obj.gid, set: (obj, value) => { obj.gid = value; } }, metadata: _metadata }, _gid_initializers, _gid_extraInitializers);
94
+ __esDecorate(null, null, _next_decorators, { kind: "field", name: "next", static: false, private: false, access: { has: obj => "next" in obj, get: obj => obj.next, set: (obj, value) => { obj.next = value; } }, metadata: _metadata }, _next_initializers, _next_extraInitializers);
95
+ __esDecorate(null, null, _type_decorators, { kind: "field", name: "type", static: false, private: false, access: { has: obj => "type" in obj, get: obj => obj.type, set: (obj, value) => { obj.type = value; } }, metadata: _metadata }, _type_initializers, _type_extraInitializers);
96
+ __esDecorate(null, null, _data_decorators, { kind: "field", name: "data", static: false, private: false, access: { has: obj => "data" in obj, get: obj => obj.data, set: (obj, value) => { obj.data = value; } }, metadata: _metadata }, _data_initializers, _data_extraInitializers);
97
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
98
+ Meta = _classThis = _classDescriptor.value;
99
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
100
+ __runInitializers(_classThis, _classExtraInitializers);
101
+ }
102
+ clock = __runInitializers(this, _clock_initializers, void 0);
103
+ gid = (__runInitializers(this, _clock_extraInitializers), __runInitializers(this, _gid_initializers, void 0)); // graph id
104
+ next = (__runInitializers(this, _gid_extraInitializers), __runInitializers(this, _next_initializers, void 0));
105
+ type = (__runInitializers(this, _next_extraInitializers), __runInitializers(this, _type_initializers, void 0));
106
+ data = (__runInitializers(this, _type_extraInitializers), __runInitializers(this, _data_initializers, void 0)); // Optional metadata
107
+ constructor(properties) {
108
+ __runInitializers(this, _data_extraInitializers);
109
+ this.gid = properties.gid;
110
+ this.clock = properties.clock;
111
+ this.type = properties.type;
112
+ this.data = properties.data;
113
+ this.next = properties.next;
114
+ }
115
+ };
116
+ return Meta = _classThis;
117
+ })();
75
118
  export { Meta };
76
- let Signatures = class Signatures {
77
- signatures;
78
- constructor(properties) {
79
- if (properties) {
80
- this.signatures = properties.signatures;
119
+ let Signatures = (() => {
120
+ let _classDecorators = [variant(0)];
121
+ let _classDescriptor;
122
+ let _classExtraInitializers = [];
123
+ let _classThis;
124
+ let _signatures_decorators;
125
+ let _signatures_initializers = [];
126
+ let _signatures_extraInitializers = [];
127
+ var Signatures = class {
128
+ static { _classThis = this; }
129
+ static {
130
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
131
+ _signatures_decorators = [field({ type: vec(MaybeEncrypted) })];
132
+ __esDecorate(null, null, _signatures_decorators, { kind: "field", name: "signatures", static: false, private: false, access: { has: obj => "signatures" in obj, get: obj => obj.signatures, set: (obj, value) => { obj.signatures = value; } }, metadata: _metadata }, _signatures_initializers, _signatures_extraInitializers);
133
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
134
+ Signatures = _classThis = _classDescriptor.value;
135
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
136
+ __runInitializers(_classThis, _classExtraInitializers);
81
137
  }
82
- }
83
- equals(other) {
84
- if (this.signatures.length !== other.signatures.length) {
85
- return false;
138
+ signatures = __runInitializers(this, _signatures_initializers, void 0);
139
+ constructor(properties) {
140
+ __runInitializers(this, _signatures_extraInitializers);
141
+ if (properties) {
142
+ this.signatures = properties.signatures;
143
+ }
86
144
  }
87
- for (let i = 0; i < this.signatures.length; i++) {
88
- if (!this.signatures[i].equals(other.signatures[i])) {
145
+ equals(other) {
146
+ if (this.signatures.length !== other.signatures.length) {
89
147
  return false;
90
148
  }
149
+ for (let i = 0; i < this.signatures.length; i++) {
150
+ if (!this.signatures[i].equals(other.signatures[i])) {
151
+ return false;
152
+ }
153
+ }
154
+ return true;
91
155
  }
92
- return true;
93
- }
94
- };
95
- __decorate([
96
- field({ type: vec(MaybeEncrypted) }),
97
- __metadata("design:type", Array)
98
- ], Signatures.prototype, "signatures", void 0);
99
- Signatures = __decorate([
100
- variant(0),
101
- __metadata("design:paramtypes", [Object])
102
- ], Signatures);
156
+ };
157
+ return Signatures = _classThis;
158
+ })();
103
159
  export { Signatures };
104
160
  const maybeEncrypt = (thing, keypair, receiver) => {
105
161
  const receivers = receiver
@@ -121,345 +177,359 @@ const maybeEncrypt = (thing, keypair, receiver) => {
121
177
  value: thing,
122
178
  });
123
179
  };
124
- let EntryV0 = EntryV0_1 = class EntryV0 extends Entry {
125
- _meta;
126
- _payload;
127
- _reserved;
128
- _signatures;
129
- hash; // "zd...Foo", we'll set the hash after persisting the entry
130
- createdLocally;
131
- _keychain;
132
- _encoding;
133
- constructor(obj) {
134
- super();
135
- this._meta = obj.meta;
136
- this._payload = obj.payload;
137
- this._signatures = obj.signatures;
138
- this._reserved = new Uint8Array([0, 0, 0, 0]);
139
- this.createdLocally = obj.createdLocally;
140
- }
141
- init(props) {
142
- if (props instanceof Entry) {
143
- this._keychain = props._keychain;
144
- this._encoding = props._encoding;
180
+ let EntryV0 = (() => {
181
+ let _classDecorators = [variant(0)];
182
+ let _classDescriptor;
183
+ let _classExtraInitializers = [];
184
+ let _classThis;
185
+ let _classSuper = Entry;
186
+ let __meta_decorators;
187
+ let __meta_initializers = [];
188
+ let __meta_extraInitializers = [];
189
+ let __payload_decorators;
190
+ let __payload_initializers = [];
191
+ let __payload_extraInitializers = [];
192
+ let __reserved_decorators;
193
+ let __reserved_initializers = [];
194
+ let __reserved_extraInitializers = [];
195
+ let __signatures_decorators;
196
+ let __signatures_initializers = [];
197
+ let __signatures_extraInitializers = [];
198
+ let _hash_decorators;
199
+ let _hash_initializers = [];
200
+ let _hash_extraInitializers = [];
201
+ var EntryV0 = class extends _classSuper {
202
+ static { _classThis = this; }
203
+ static {
204
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
205
+ __meta_decorators = [field({ type: MaybeEncrypted })];
206
+ __payload_decorators = [field({ type: MaybeEncrypted })];
207
+ __reserved_decorators = [field({ type: fixedArray("u8", 4) })];
208
+ __signatures_decorators = [field({ type: option(Signatures) })];
209
+ _hash_decorators = [field({ type: option("string") })];
210
+ __esDecorate(null, null, __meta_decorators, { kind: "field", name: "_meta", static: false, private: false, access: { has: obj => "_meta" in obj, get: obj => obj._meta, set: (obj, value) => { obj._meta = value; } }, metadata: _metadata }, __meta_initializers, __meta_extraInitializers);
211
+ __esDecorate(null, null, __payload_decorators, { kind: "field", name: "_payload", static: false, private: false, access: { has: obj => "_payload" in obj, get: obj => obj._payload, set: (obj, value) => { obj._payload = value; } }, metadata: _metadata }, __payload_initializers, __payload_extraInitializers);
212
+ __esDecorate(null, null, __reserved_decorators, { kind: "field", name: "_reserved", static: false, private: false, access: { has: obj => "_reserved" in obj, get: obj => obj._reserved, set: (obj, value) => { obj._reserved = value; } }, metadata: _metadata }, __reserved_initializers, __reserved_extraInitializers);
213
+ __esDecorate(null, null, __signatures_decorators, { kind: "field", name: "_signatures", static: false, private: false, access: { has: obj => "_signatures" in obj, get: obj => obj._signatures, set: (obj, value) => { obj._signatures = value; } }, metadata: _metadata }, __signatures_initializers, __signatures_extraInitializers);
214
+ __esDecorate(null, null, _hash_decorators, { kind: "field", name: "hash", static: false, private: false, access: { has: obj => "hash" in obj, get: obj => obj.hash, set: (obj, value) => { obj.hash = value; } }, metadata: _metadata }, _hash_initializers, _hash_extraInitializers);
215
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
216
+ EntryV0 = _classThis = _classDescriptor.value;
217
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
218
+ __runInitializers(_classThis, _classExtraInitializers);
145
219
  }
146
- else {
147
- this._keychain = props.keychain;
148
- this._encoding = props.encoding;
220
+ _meta = __runInitializers(this, __meta_initializers, void 0);
221
+ _payload = (__runInitializers(this, __meta_extraInitializers), __runInitializers(this, __payload_initializers, void 0));
222
+ _reserved = (__runInitializers(this, __payload_extraInitializers), __runInitializers(this, __reserved_initializers, void 0));
223
+ _signatures = (__runInitializers(this, __reserved_extraInitializers), __runInitializers(this, __signatures_initializers, void 0));
224
+ hash = (__runInitializers(this, __signatures_extraInitializers), __runInitializers(this, _hash_initializers, void 0)); // "zd...Foo", we'll set the hash after persisting the entry
225
+ createdLocally = __runInitializers(this, _hash_extraInitializers);
226
+ _keychain;
227
+ _encoding;
228
+ constructor(obj) {
229
+ super();
230
+ this._meta = obj.meta;
231
+ this._payload = obj.payload;
232
+ this._signatures = obj.signatures;
233
+ this._reserved = new Uint8Array([0, 0, 0, 0]);
234
+ this.createdLocally = obj.createdLocally;
149
235
  }
150
- return this;
151
- }
152
- get encoding() {
153
- if (!this._encoding) {
154
- throw new Error("Not initialized");
236
+ init(props) {
237
+ if (props instanceof Entry) {
238
+ this._keychain = props._keychain;
239
+ this._encoding = props._encoding;
240
+ }
241
+ else {
242
+ this._keychain = props.keychain;
243
+ this._encoding = props.encoding;
244
+ }
245
+ return this;
155
246
  }
156
- return this._encoding;
157
- }
158
- get meta() {
159
- return this._meta.decrypted.getValue(Meta);
160
- }
161
- async getMeta() {
162
- await this._meta.decrypt(this._keychain);
163
- return this.meta;
164
- }
165
- async getClock() {
166
- return (await this.getMeta()).clock;
167
- }
168
- get gid() {
169
- return this._meta.decrypted.getValue(Meta).gid;
170
- }
171
- async getGid() {
172
- return (await this.getMeta()).gid;
173
- }
174
- get payload() {
175
- const payload = this._payload.decrypted.getValue(Payload);
176
- payload.encoding = payload.encoding || this.encoding;
177
- return payload;
178
- }
179
- async getPayload() {
180
- if (this._payload instanceof DecryptedThing) {
181
- return this.payload;
247
+ get encoding() {
248
+ if (!this._encoding) {
249
+ throw new Error("Not initialized");
250
+ }
251
+ return this._encoding;
182
252
  }
183
- await this._payload.decrypt(this._keychain);
184
- return this.payload;
185
- }
186
- async getPayloadValue() {
187
- const payload = await this.getPayload();
188
- return payload.isDecoded ? payload.value : payload.getValue(this.encoding);
189
- }
190
- get publicKeys() {
191
- return this.signatures.map((x) => x.publicKey);
192
- }
193
- get next() {
194
- return this.meta.next;
195
- }
196
- async getNext() {
197
- return (await this.getMeta()).next;
198
- }
199
- _size;
200
- set size(number) {
201
- this._size = number;
202
- }
203
- get size() {
204
- if (this._size == null) {
205
- throw new Error("Size not set. Size is set when entry is, created, loaded or joined");
253
+ get meta() {
254
+ return this._meta.decrypted.getValue(Meta);
206
255
  }
207
- return this._size;
208
- }
209
- /**
210
- * Will only return signatures I can decrypt
211
- * @returns signatures
212
- */
213
- get signatures() {
214
- const signatures = this._signatures.signatures.filter((x) => {
215
- try {
216
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
217
- x.decrypted;
218
- return true;
256
+ async getMeta() {
257
+ await this._meta.decrypt(this._keychain);
258
+ return this.meta;
259
+ }
260
+ async getClock() {
261
+ return (await this.getMeta()).clock;
262
+ }
263
+ get gid() {
264
+ return this._meta.decrypted.getValue(Meta).gid;
265
+ }
266
+ async getGid() {
267
+ return (await this.getMeta()).gid;
268
+ }
269
+ get payload() {
270
+ const payload = this._payload.decrypted.getValue(Payload);
271
+ payload.encoding = payload.encoding || this.encoding;
272
+ return payload;
273
+ }
274
+ async getPayload() {
275
+ if (this._payload instanceof DecryptedThing) {
276
+ return this.payload;
219
277
  }
220
- catch (error) {
221
- return false;
278
+ await this._payload.decrypt(this._keychain);
279
+ return this.payload;
280
+ }
281
+ async getPayloadValue() {
282
+ const payload = await this.getPayload();
283
+ return payload.isDecoded ? payload.value : payload.getValue(this.encoding);
284
+ }
285
+ get publicKeys() {
286
+ return this.signatures.map((x) => x.publicKey);
287
+ }
288
+ get next() {
289
+ return this.meta.next;
290
+ }
291
+ async getNext() {
292
+ return (await this.getMeta()).next;
293
+ }
294
+ _size;
295
+ set size(number) {
296
+ this._size = number;
297
+ }
298
+ get size() {
299
+ if (this._size == null) {
300
+ throw new Error("Size not set. Size is set when entry is, created, loaded or joined");
222
301
  }
223
- }).map((x) => x.decrypted.getValue(SignatureWithKey));
224
- if (signatures.length === 0) {
225
- this._signatures?.signatures.forEach((x) => x.clear());
226
- throw new Error("Failed to resolve any signature");
302
+ return this._size;
227
303
  }
228
- return signatures;
229
- }
230
- /**
231
- * Will only return signatures I can decrypt
232
- * @returns signatures
233
- */
234
- async getSignatures() {
235
- const results = await Promise.allSettled(this._signatures.signatures.map((x) => x.decrypt(this._keychain)));
236
- if (logger.level === "debug" || logger.level === "trace") {
237
- for (const [i, result] of results.entries()) {
238
- if (result.status === "rejected") {
239
- logger.debug("Failed to decrypt signature with index: " + i);
304
+ /**
305
+ * Will only return signatures I can decrypt
306
+ * @returns signatures
307
+ */
308
+ get signatures() {
309
+ const signatures = this._signatures.signatures.filter((x) => {
310
+ try {
311
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
312
+ x.decrypted;
313
+ return true;
314
+ }
315
+ catch (error) {
316
+ return false;
240
317
  }
318
+ }).map((x) => x.decrypted.getValue(SignatureWithKey));
319
+ if (signatures.length === 0) {
320
+ this._signatures?.signatures.forEach((x) => x.clear());
321
+ throw new Error("Failed to resolve any signature");
241
322
  }
323
+ return signatures;
242
324
  }
243
- return this.signatures;
244
- }
245
- /**
246
- * Will only verify signatures I can decrypt
247
- * @returns true if all are verified
248
- */
249
- async verifySignatures() {
250
- const signatures = await this.getSignatures();
251
- if (signatures.length === 0) {
252
- return false;
325
+ /**
326
+ * Will only return signatures I can decrypt
327
+ * @returns signatures
328
+ */
329
+ async getSignatures() {
330
+ const results = await Promise.allSettled(this._signatures.signatures.map((x) => x.decrypt(this._keychain)));
331
+ if (traceLogger?.enabled) {
332
+ for (const [i, result] of results.entries()) {
333
+ if (result.status === "rejected") {
334
+ traceLogger("Failed to decrypt signature with index: " + i);
335
+ }
336
+ }
337
+ }
338
+ return this.signatures;
253
339
  }
254
- const signable = EntryV0_1.toSignable(this);
255
- const signableBytes = serialize(signable);
256
- for (const signature of signatures) {
257
- if (!(await verify(signature, signableBytes))) {
340
+ /**
341
+ * Will only verify signatures I can decrypt
342
+ * @returns true if all are verified
343
+ */
344
+ async verifySignatures() {
345
+ const signatures = await this.getSignatures();
346
+ if (signatures.length === 0) {
258
347
  return false;
259
348
  }
349
+ const signable = EntryV0.toSignable(this);
350
+ const signableBytes = serialize(signable);
351
+ for (const signature of signatures) {
352
+ if (!(await verify(signature, signableBytes))) {
353
+ return false;
354
+ }
355
+ }
356
+ return true;
260
357
  }
261
- return true;
262
- }
263
- static toSignable(entry) {
264
- // TODO fix types
265
- const trimmed = new EntryV0_1({
266
- meta: entry._meta,
267
- payload: entry._payload,
268
- reserved: entry._reserved,
269
- signatures: undefined,
270
- hash: undefined,
271
- });
272
- return trimmed;
273
- }
274
- toSignable() {
275
- if (this._signatures) {
276
- throw new Error("Expected signatures to be undefined");
358
+ static toSignable(entry) {
359
+ // TODO fix types
360
+ const trimmed = new EntryV0({
361
+ meta: entry._meta,
362
+ payload: entry._payload,
363
+ reserved: entry._reserved,
364
+ signatures: undefined,
365
+ hash: undefined,
366
+ });
367
+ return trimmed;
277
368
  }
278
- if (this.hash) {
279
- throw new Error("Expected hash to be undefined");
369
+ toSignable() {
370
+ if (this._signatures) {
371
+ throw new Error("Expected signatures to be undefined");
372
+ }
373
+ if (this.hash) {
374
+ throw new Error("Expected hash to be undefined");
375
+ }
376
+ return EntryV0.toSignable(this);
280
377
  }
281
- return EntryV0_1.toSignable(this);
282
- }
283
- equals(other) {
284
- if (other instanceof EntryV0_1) {
285
- return (equals(this._reserved, other._reserved) &&
286
- this._meta.equals(other._meta) &&
287
- this._signatures.equals(other._signatures) &&
288
- this._payload.equals(other._payload)); // dont compare hashes because the hash is a function of the other properties
289
- }
290
- return false;
291
- }
292
- async delete(store) {
293
- if (!this.hash) {
294
- throw new Error("Missing hash");
378
+ equals(other) {
379
+ if (other instanceof EntryV0) {
380
+ return (equals(this._reserved, other._reserved) &&
381
+ this._meta.equals(other._meta) &&
382
+ this._signatures.equals(other._signatures) &&
383
+ this._payload.equals(other._payload)); // dont compare hashes because the hash is a function of the other properties
384
+ }
385
+ return false;
295
386
  }
296
- await store.rm(this.hash);
297
- }
298
- static createGid(seed) {
299
- return seed ? sha256Base64(seed) : toBase64(randomBytes(32));
300
- }
301
- static async create(properties) {
302
- if (!properties.encoding || !properties?.meta?.next) {
303
- properties = {
304
- ...properties,
305
- meta: {
306
- ...properties?.meta,
307
- next: properties.meta?.next ? properties.meta?.next : [],
308
- },
309
- encoding: properties.encoding ? properties.encoding : NO_ENCODING,
310
- };
311
- }
312
- if (!properties.encoding) {
313
- throw new Error("Missing encoding options");
314
- }
315
- if (properties.data == null)
316
- throw new Error("Entry requires data");
317
- if (properties.meta?.next == null || !Array.isArray(properties.meta.next))
318
- throw new Error("'next' argument is not an array");
319
- // Clean the next objects and convert to hashes
320
- const nexts = properties.meta?.next;
321
- const payloadToSave = new Payload({
322
- data: properties.encoding.encoder(properties.data),
323
- value: properties.data,
324
- encoding: properties.encoding,
325
- });
326
- let clock = properties.meta?.clock;
327
- if (!clock) {
328
- const hlc = new HLC();
329
- for (const next of nexts) {
330
- hlc.update(next.meta.clock.timestamp);
387
+ async delete(store) {
388
+ if (!this.hash) {
389
+ throw new Error("Missing hash");
331
390
  }
332
- if (properties.encryption?.receiver.signatures &&
333
- properties.encryption?.receiver.meta) {
334
- throw new Error("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");
391
+ await store.rm(this.hash);
392
+ }
393
+ static createGid(seed) {
394
+ return seed ? sha256Base64(seed) : toBase64(randomBytes(32));
395
+ }
396
+ static async create(properties) {
397
+ if (!properties.encoding || !properties?.meta?.next) {
398
+ properties = {
399
+ ...properties,
400
+ meta: {
401
+ ...properties?.meta,
402
+ next: properties.meta?.next ? properties.meta?.next : [],
403
+ },
404
+ encoding: properties.encoding ? properties.encoding : NO_ENCODING,
405
+ };
406
+ }
407
+ if (!properties.encoding) {
408
+ throw new Error("Missing encoding options");
335
409
  }
336
- clock = new Clock({
337
- id: properties.identity.publicKey.bytes,
338
- timestamp: hlc.now(),
410
+ if (properties.data == null)
411
+ throw new Error("Entry requires data");
412
+ if (properties.meta?.next == null || !Array.isArray(properties.meta.next))
413
+ throw new Error("'next' argument is not an array");
414
+ // Clean the next objects and convert to hashes
415
+ const nexts = properties.meta?.next;
416
+ const payloadToSave = new Payload({
417
+ data: properties.encoding.encoder(properties.data),
418
+ value: properties.data,
419
+ encoding: properties.encoding,
339
420
  });
340
- }
341
- else {
342
- const cv = clock;
343
- // check if nexts, that all nexts are happening BEFORE this clock value (else clock make no sense)
344
- for (const n of nexts) {
345
- if (Timestamp.compare(n.meta.clock.timestamp, cv.timestamp) >= 0) {
346
- throw new Error("Expecting next(s) to happen before entry, got: " +
347
- n.meta.clock.timestamp +
348
- " > " +
349
- cv.timestamp);
421
+ let clock = properties.meta?.clock;
422
+ if (!clock) {
423
+ const hlc = new HLC();
424
+ for (const next of nexts) {
425
+ hlc.update(next.meta.clock.timestamp);
426
+ }
427
+ if (properties.encryption?.receiver.signatures &&
428
+ properties.encryption?.receiver.meta) {
429
+ throw new Error("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");
350
430
  }
431
+ clock = new Clock({
432
+ id: properties.identity.publicKey.bytes,
433
+ timestamp: hlc.now(),
434
+ });
351
435
  }
352
- }
353
- const nextHashes = [];
354
- let maxChainLength = 0n;
355
- let gid = null;
356
- if (nexts?.length > 0) {
357
- // take min gid as our gid
358
- if (properties.meta?.gid) {
359
- throw new Error("Expecting '.meta.gid' property to be undefined if '.meta.next' is provided");
436
+ else {
437
+ const cv = clock;
438
+ // check if nexts, that all nexts are happening BEFORE this clock value (else clock make no sense)
439
+ for (const n of nexts) {
440
+ if (Timestamp.compare(n.meta.clock.timestamp, cv.timestamp) >= 0) {
441
+ throw new Error("Expecting next(s) to happen before entry, got: " +
442
+ n.meta.clock.timestamp +
443
+ " > " +
444
+ cv.timestamp);
445
+ }
446
+ }
360
447
  }
361
- for (const n of nexts) {
362
- if (!n.hash) {
363
- throw new Error("Expecting hash to be defined to next entries");
448
+ const nextHashes = [];
449
+ let gid = null;
450
+ if (nexts?.length > 0) {
451
+ // take min gid as our gid
452
+ if (properties.meta?.gid) {
453
+ throw new Error("Expecting '.meta.gid' property to be undefined if '.meta.next' is provided");
364
454
  }
365
- nextHashes.push(n.hash);
366
- gid =
367
- gid == null
368
- ? n.meta.gid
369
- : n.meta.gid < gid
455
+ for (const n of nexts) {
456
+ if (!n.hash) {
457
+ throw new Error("Expecting hash to be defined to next entries");
458
+ }
459
+ nextHashes.push(n.hash);
460
+ gid =
461
+ gid == null
370
462
  ? n.meta.gid
371
- : gid;
463
+ : n.meta.gid < gid
464
+ ? n.meta.gid
465
+ : gid;
466
+ }
467
+ }
468
+ else {
469
+ gid =
470
+ properties.meta?.gid ||
471
+ (await EntryV0.createGid(properties.meta?.gidSeed));
372
472
  }
473
+ const metadataEncrypted = await maybeEncrypt(new Meta({
474
+ clock,
475
+ gid: gid,
476
+ type: properties.meta?.type ?? EntryType.APPEND,
477
+ data: properties.meta?.data,
478
+ next: nextHashes,
479
+ }), properties.encryption?.keypair, properties.encryption?.receiver.meta);
480
+ const payload = await maybeEncrypt(payloadToSave, properties.encryption?.keypair, properties.encryption?.receiver.payload);
481
+ // Sign id, encrypted payload, clock, nexts, refs
482
+ const entry = new EntryV0({
483
+ meta: metadataEncrypted,
484
+ payload,
485
+ signatures: undefined,
486
+ createdLocally: true,
487
+ });
488
+ const signers = properties.signers || [
489
+ properties.identity.sign.bind(properties.identity),
490
+ ];
491
+ const signableBytes = serialize(entry.toSignable());
492
+ let signatures = await Promise.all(signers.map((signer) => signer(signableBytes)));
493
+ signatures = signatures.sort((a, b) => compare(a.signature, b.signature));
494
+ const encryptedSignatures = [];
495
+ const encryptAllSignaturesWithSameKey = isMaybeEryptionPublicKey(properties.encryption?.receiver?.signatures);
496
+ for (const signature of signatures) {
497
+ const encryptionRecievers = encryptAllSignaturesWithSameKey
498
+ ? properties.encryption?.receiver?.signatures
499
+ : properties.encryption?.receiver?.signatures?.[signature.publicKey.hashcode()]; // TODO types
500
+ const signatureEncrypted = await maybeEncrypt(signature, properties.encryption?.keypair, encryptionRecievers);
501
+ encryptedSignatures.push(signatureEncrypted);
502
+ }
503
+ entry._signatures = new Signatures({
504
+ signatures: encryptedSignatures,
505
+ });
506
+ if (properties.canAppend && !(await properties.canAppend(entry))) {
507
+ throw new AccessError("Not allowed to append");
508
+ }
509
+ // Append hash
510
+ entry.hash = await Entry.toMultihash(properties.store, entry);
511
+ entry.init({ encoding: properties.encoding });
512
+ return entry;
373
513
  }
374
- else {
375
- gid =
376
- properties.meta?.gid ||
377
- (await EntryV0_1.createGid(properties.meta?.gidSeed));
378
- }
379
- maxChainLength += 1n; // include this
380
- const metadataEncrypted = await maybeEncrypt(new Meta({
381
- clock,
382
- gid: gid,
383
- type: properties.meta?.type ?? EntryType.APPEND,
384
- data: properties.meta?.data,
385
- next: nextHashes,
386
- }), properties.encryption?.keypair, properties.encryption?.receiver.meta);
387
- const payload = await maybeEncrypt(payloadToSave, properties.encryption?.keypair, properties.encryption?.receiver.payload);
388
- // Sign id, encrypted payload, clock, nexts, refs
389
- const entry = new EntryV0_1({
390
- meta: metadataEncrypted,
391
- payload,
392
- signatures: undefined,
393
- createdLocally: true,
394
- });
395
- const signers = properties.signers || [
396
- properties.identity.sign.bind(properties.identity),
397
- ];
398
- const signableBytes = serialize(entry.toSignable());
399
- let signatures = await Promise.all(signers.map((signer) => signer(signableBytes)));
400
- signatures = signatures.sort((a, b) => compare(a.signature, b.signature));
401
- const encryptedSignatures = [];
402
- const encryptAllSignaturesWithSameKey = isMaybeEryptionPublicKey(properties.encryption?.receiver?.signatures);
403
- for (const signature of signatures) {
404
- const encryptionRecievers = encryptAllSignaturesWithSameKey
405
- ? properties.encryption?.receiver?.signatures
406
- : properties.encryption?.receiver?.signatures?.[signature.publicKey.hashcode()]; // TODO types
407
- const signatureEncrypted = await maybeEncrypt(signature, properties.encryption?.keypair, encryptionRecievers);
408
- encryptedSignatures.push(signatureEncrypted);
409
- }
410
- entry._signatures = new Signatures({
411
- signatures: encryptedSignatures,
412
- });
413
- if (properties.canAppend && !(await properties.canAppend(entry))) {
414
- throw new AccessError("Not allowed to append");
415
- }
416
- // Append hash
417
- entry.hash = await Entry.toMultihash(properties.store, entry);
418
- entry.init({ encoding: properties.encoding });
419
- return entry;
420
- }
421
- get payloadByteLength() {
422
- return this._payload.byteLength;
423
- }
424
- toShallow(isHead) {
425
- return new ShallowEntry({
426
- hash: this.hash,
427
- payloadSize: this._payload.byteLength,
428
- head: isHead,
429
- meta: new ShallowMeta({
430
- gid: this.meta.gid,
431
- data: this.meta.data,
432
- clock: this.meta.clock,
433
- next: this.meta.next,
434
- type: this.meta.type,
435
- }),
436
- });
437
- }
438
- };
439
- __decorate([
440
- field({ type: MaybeEncrypted }),
441
- __metadata("design:type", MaybeEncrypted)
442
- ], EntryV0.prototype, "_meta", void 0);
443
- __decorate([
444
- field({ type: MaybeEncrypted }),
445
- __metadata("design:type", MaybeEncrypted)
446
- ], EntryV0.prototype, "_payload", void 0);
447
- __decorate([
448
- field({ type: fixedArray("u8", 4) }),
449
- __metadata("design:type", Uint8Array)
450
- ], EntryV0.prototype, "_reserved", void 0);
451
- __decorate([
452
- field({ type: option(Signatures) }),
453
- __metadata("design:type", Signatures)
454
- ], EntryV0.prototype, "_signatures", void 0);
455
- __decorate([
456
- 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
457
- ,
458
- __metadata("design:type", String)
459
- ], EntryV0.prototype, "hash", void 0);
460
- EntryV0 = EntryV0_1 = __decorate([
461
- variant(0),
462
- __metadata("design:paramtypes", [Object])
463
- ], EntryV0);
514
+ get payloadByteLength() {
515
+ return this._payload.byteLength;
516
+ }
517
+ toShallow(isHead) {
518
+ return new ShallowEntry({
519
+ hash: this.hash,
520
+ payloadSize: this._payload.byteLength,
521
+ head: isHead,
522
+ meta: new ShallowMeta({
523
+ gid: this.meta.gid,
524
+ data: this.meta.data,
525
+ clock: this.meta.clock,
526
+ next: this.meta.next,
527
+ type: this.meta.type,
528
+ }),
529
+ });
530
+ }
531
+ };
532
+ return EntryV0 = _classThis;
533
+ })();
464
534
  export { EntryV0 };
465
535
  //# sourceMappingURL=entry-v0.js.map