zkjson 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/db.js CHANGED
@@ -27,6 +27,13 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o =
27
27
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
28
28
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
29
29
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
30
+ var to64 = function to64(hash) {
31
+ var n = BigInt(hash);
32
+ var hex = n.toString(16);
33
+ if (hex.length % 2) hex = "0" + hex;
34
+ var buf = Buffer.from(hex, "hex");
35
+ return buf.toString("base64");
36
+ };
30
37
  var DB = exports["default"] = /*#__PURE__*/function () {
31
38
  function DB(_ref) {
32
39
  var _ref$size_val = _ref.size_val,
@@ -487,6 +494,13 @@ var DB = exports["default"] = /*#__PURE__*/function () {
487
494
  }
488
495
  return getInputs;
489
496
  }()
497
+ }, {
498
+ key: "hash",
499
+ value: function hash(format) {
500
+ var _hash = this.tree.F.toObject(this.tree.root);
501
+ if (format === "base64") return to64(_hash.toString());
502
+ return _hash.toString();
503
+ }
490
504
  }, {
491
505
  key: "getID",
492
506
  value: function getID(num) {
package/cjs/db_tree.js CHANGED
@@ -18,6 +18,19 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o =
18
18
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
19
19
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
20
20
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
21
+ var to64 = function to64(hash) {
22
+ var n = BigInt(hash);
23
+ var hex = n.toString(16);
24
+ if (hex.length % 2) hex = "0" + hex;
25
+ var buf = Buffer.from(hex, "hex");
26
+ return buf.toString("base64");
27
+ };
28
+ var from64 = function from64(b64) {
29
+ var buf = Buffer.from(b64, "base64");
30
+ var hex = buf.toString("hex");
31
+ var n = BigInt("0x" + hex);
32
+ return n.toString();
33
+ };
21
34
  var DBTree = exports["default"] = /*#__PURE__*/function () {
22
35
  function DBTree(_ref) {
23
36
  var _ref$size_val = _ref.size_val,
@@ -70,6 +83,13 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
70
83
  }
71
84
  return init;
72
85
  }()
86
+ }, {
87
+ key: "hash",
88
+ value: function hash(format) {
89
+ var _hash = this.tree.F.toObject(this.tree.root);
90
+ if (format === "base64") return to64(_hash.toString());
91
+ return _hash.toString();
92
+ }
73
93
  }, {
74
94
  key: "exists",
75
95
  value: function () {
@@ -147,7 +167,6 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
147
167
  key: "addCollection",
148
168
  value: function () {
149
169
  var _addCollection = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(num) {
150
- var _this$kv2;
151
170
  var id, col, _col, root;
152
171
  return _regenerator().w(function (_context4) {
153
172
  while (1) switch (_context4.n) {
@@ -188,18 +207,10 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
188
207
  case 6:
189
208
  id = _context4.v;
190
209
  case 7:
191
- _col = new _collection_tree["default"]({
192
- size_val: this.size_val,
193
- size_path: this.size_path,
194
- level: this.level,
195
- size_json: this.size_json,
196
- kv: (_this$kv2 = this.kv) === null || _this$kv2 === void 0 ? void 0 : _this$kv2.call(this, "dir_".concat(id))
197
- });
198
210
  _context4.n = 8;
199
- return _col.init();
211
+ return this.loadCol(id);
200
212
  case 8:
201
- this.cols[id] = _col;
202
- this.ids[id] = true;
213
+ _col = _context4.v;
203
214
  root = _col.tree.F.toObject(_col.tree.root).toString();
204
215
  _context4.n = 9;
205
216
  return this.tree.insert(id, [root]);
@@ -219,55 +230,140 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
219
230
  }()
220
231
  }, {
221
232
  key: "getColTree",
222
- value: function getColTree(col) {
223
- var _col = this.cols[col];
224
- if (!_col) throw Error("collection doesn't exist");
225
- return _col;
226
- }
233
+ value: function () {
234
+ var _getColTree = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(col) {
235
+ var _col;
236
+ return _regenerator().w(function (_context5) {
237
+ while (1) switch (_context5.n) {
238
+ case 0:
239
+ _col = this.cols[col];
240
+ if (_col) {
241
+ _context5.n = 2;
242
+ break;
243
+ }
244
+ _context5.n = 1;
245
+ return this.loadCollection(col);
246
+ case 1:
247
+ return _context5.a(2, _context5.v);
248
+ case 2:
249
+ return _context5.a(2, _col);
250
+ case 3:
251
+ return _context5.a(2);
252
+ }
253
+ }, _callee5, this);
254
+ }));
255
+ function getColTree(_x4) {
256
+ return _getColTree.apply(this, arguments);
257
+ }
258
+ return getColTree;
259
+ }()
260
+ }, {
261
+ key: "loadCol",
262
+ value: function () {
263
+ var _loadCol = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(id) {
264
+ var _this$kv2;
265
+ var col;
266
+ return _regenerator().w(function (_context6) {
267
+ while (1) switch (_context6.n) {
268
+ case 0:
269
+ col = new _collection_tree["default"]({
270
+ size_val: this.size_val,
271
+ size_path: this.size_path,
272
+ level: this.level,
273
+ size_json: this.size_json,
274
+ kv: (_this$kv2 = this.kv) === null || _this$kv2 === void 0 ? void 0 : _this$kv2.call(this, "dir_".concat(id))
275
+ });
276
+ _context6.n = 1;
277
+ return col.init();
278
+ case 1:
279
+ this.cols[id] = col;
280
+ this.ids[id] = true;
281
+ return _context6.a(2, col);
282
+ }
283
+ }, _callee6, this);
284
+ }));
285
+ function loadCol(_x5) {
286
+ return _loadCol.apply(this, arguments);
287
+ }
288
+ return loadCol;
289
+ }()
290
+ }, {
291
+ key: "loadCollection",
292
+ value: function () {
293
+ var _loadCollection = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(id) {
294
+ var hit;
295
+ return _regenerator().w(function (_context7) {
296
+ while (1) switch (_context7.n) {
297
+ case 0:
298
+ _context7.n = 1;
299
+ return this.tree.find(id);
300
+ case 1:
301
+ hit = _context7.v;
302
+ if (!(!hit || !hit.found)) {
303
+ _context7.n = 2;
304
+ break;
305
+ }
306
+ throw Error("collection doesn't exist");
307
+ case 2:
308
+ _context7.n = 3;
309
+ return this.loadCol(id);
310
+ case 3:
311
+ return _context7.a(2, _context7.v);
312
+ }
313
+ }, _callee7, this);
314
+ }));
315
+ function loadCollection(_x6) {
316
+ return _loadCollection.apply(this, arguments);
317
+ }
318
+ return loadCollection;
319
+ }()
227
320
  }, {
228
321
  key: "insert",
229
322
  value: function () {
230
- var _insert = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(col, _key, _val) {
323
+ var _insert = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(col, _key, _val) {
231
324
  var _col, update, res_doc, res_col;
232
- return _regenerator().w(function (_context5) {
233
- while (1) switch (_context5.n) {
325
+ return _regenerator().w(function (_context8) {
326
+ while (1) switch (_context8.n) {
234
327
  case 0:
235
- _col = this.getColTree(col);
328
+ _context8.n = 1;
329
+ return this.getColTree(col);
330
+ case 1:
331
+ _col = _context8.v;
236
332
  update = false;
237
- _context5.n = 1;
333
+ _context8.n = 2;
238
334
  return _col.get(_key);
239
- case 1:
240
- if (!_context5.v.found) {
241
- _context5.n = 3;
335
+ case 2:
336
+ if (!_context8.v.found) {
337
+ _context8.n = 4;
242
338
  break;
243
339
  }
244
340
  update = true;
245
- _context5.n = 2;
341
+ _context8.n = 3;
246
342
  return _col.update(_key, _val);
247
- case 2:
248
- res_doc = _context5.v;
249
- _context5.n = 5;
250
- break;
251
343
  case 3:
252
- _context5.n = 4;
253
- return _col.insert(_key, _val);
344
+ res_doc = _context8.v;
345
+ _context8.n = 6;
346
+ break;
254
347
  case 4:
255
- res_doc = _context5.v;
348
+ _context8.n = 5;
349
+ return _col.insert(_key, _val);
256
350
  case 5:
257
- _context5.n = 6;
258
- return this.updateDB(_col, col);
351
+ res_doc = _context8.v;
259
352
  case 6:
260
- res_col = _context5.v;
261
- return _context5.a(2, {
353
+ _context8.n = 7;
354
+ return this.updateDB(_col, col);
355
+ case 7:
356
+ res_col = _context8.v;
357
+ return _context8.a(2, {
262
358
  update: update,
263
359
  doc: res_doc,
264
360
  col: res_col,
265
361
  tree: _col.tree
266
362
  });
267
363
  }
268
- }, _callee5, this);
364
+ }, _callee8, this);
269
365
  }));
270
- function insert(_x4, _x5, _x6) {
366
+ function insert(_x7, _x8, _x9) {
271
367
  return _insert.apply(this, arguments);
272
368
  }
273
369
  return insert;
@@ -275,21 +371,21 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
275
371
  }, {
276
372
  key: "updateDB",
277
373
  value: function () {
278
- var _updateDB = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(_col, col) {
374
+ var _updateDB = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(_col, col) {
279
375
  var root, colD;
280
- return _regenerator().w(function (_context6) {
281
- while (1) switch (_context6.n) {
376
+ return _regenerator().w(function (_context9) {
377
+ while (1) switch (_context9.n) {
282
378
  case 0:
283
379
  root = _col.tree.F.toObject(_col.tree.root).toString();
284
380
  colD = col;
285
- _context6.n = 1;
381
+ _context9.n = 1;
286
382
  return this.tree.update(colD, [root]);
287
383
  case 1:
288
- return _context6.a(2, _context6.v);
384
+ return _context9.a(2, _context9.v);
289
385
  }
290
- }, _callee6, this);
386
+ }, _callee9, this);
291
387
  }));
292
- function updateDB(_x7, _x8) {
388
+ function updateDB(_x0, _x1) {
293
389
  return _updateDB.apply(this, arguments);
294
390
  }
295
391
  return updateDB;
@@ -297,29 +393,32 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
297
393
  }, {
298
394
  key: "update",
299
395
  value: function () {
300
- var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(col, _key, _val) {
396
+ var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(col, _key, _val) {
301
397
  var _col, res_doc, res_col;
302
- return _regenerator().w(function (_context7) {
303
- while (1) switch (_context7.n) {
398
+ return _regenerator().w(function (_context0) {
399
+ while (1) switch (_context0.n) {
304
400
  case 0:
305
- _col = this.getColTree(col);
306
- _context7.n = 1;
307
- return _col.update(_key, _val);
401
+ _context0.n = 1;
402
+ return this.getColTree(col);
308
403
  case 1:
309
- res_doc = _context7.v;
310
- _context7.n = 2;
311
- return this.updateDB(_col, col);
404
+ _col = _context0.v;
405
+ _context0.n = 2;
406
+ return _col.update(_key, _val);
312
407
  case 2:
313
- res_col = _context7.v;
314
- return _context7.a(2, {
408
+ res_doc = _context0.v;
409
+ _context0.n = 3;
410
+ return this.updateDB(_col, col);
411
+ case 3:
412
+ res_col = _context0.v;
413
+ return _context0.a(2, {
315
414
  doc: res_doc,
316
415
  col: res_col,
317
416
  tree: _col.tree
318
417
  });
319
418
  }
320
- }, _callee7, this);
419
+ }, _callee0, this);
321
420
  }));
322
- function update(_x9, _x0, _x1) {
421
+ function update(_x10, _x11, _x12) {
323
422
  return _update.apply(this, arguments);
324
423
  }
325
424
  return update;
@@ -327,29 +426,32 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
327
426
  }, {
328
427
  key: "delete",
329
428
  value: function () {
330
- var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(col, _key) {
429
+ var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(col, _key) {
331
430
  var _col, res_doc, res_col;
332
- return _regenerator().w(function (_context8) {
333
- while (1) switch (_context8.n) {
431
+ return _regenerator().w(function (_context1) {
432
+ while (1) switch (_context1.n) {
334
433
  case 0:
335
- _col = this.getColTree(col);
336
- _context8.n = 1;
337
- return _col["delete"](_key);
434
+ _context1.n = 1;
435
+ return this.getColTree(col);
338
436
  case 1:
339
- res_doc = _context8.v;
340
- _context8.n = 2;
341
- return this.updateDB(_col, col);
437
+ _col = _context1.v;
438
+ _context1.n = 2;
439
+ return _col["delete"](_key);
342
440
  case 2:
343
- res_col = _context8.v;
344
- return _context8.a(2, {
441
+ res_doc = _context1.v;
442
+ _context1.n = 3;
443
+ return this.updateDB(_col, col);
444
+ case 3:
445
+ res_col = _context1.v;
446
+ return _context1.a(2, {
345
447
  doc: res_doc,
346
448
  col: res_col,
347
449
  tree: _col.tree
348
450
  });
349
451
  }
350
- }, _callee8, this);
452
+ }, _callee1, this);
351
453
  }));
352
- function _delete(_x10, _x11) {
454
+ function _delete(_x13, _x14) {
353
455
  return _delete2.apply(this, arguments);
354
456
  }
355
457
  return _delete;
@@ -357,20 +459,23 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
357
459
  }, {
358
460
  key: "get",
359
461
  value: function () {
360
- var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(col, _key) {
462
+ var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(col, _key) {
361
463
  var _col;
362
- return _regenerator().w(function (_context9) {
363
- while (1) switch (_context9.n) {
464
+ return _regenerator().w(function (_context10) {
465
+ while (1) switch (_context10.n) {
364
466
  case 0:
365
- _col = this.getColTree(col);
366
- _context9.n = 1;
367
- return _col.get(_key);
467
+ _context10.n = 1;
468
+ return this.getColTree(col);
368
469
  case 1:
369
- return _context9.a(2, _context9.v);
470
+ _col = _context10.v;
471
+ _context10.n = 2;
472
+ return _col.get(_key);
473
+ case 2:
474
+ return _context10.a(2, _context10.v);
370
475
  }
371
- }, _callee9, this);
476
+ }, _callee10, this);
372
477
  }));
373
- function get(_x12, _x13) {
478
+ function get(_x15, _x16) {
374
479
  return _get.apply(this, arguments);
375
480
  }
376
481
  return get;
@@ -378,18 +483,18 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
378
483
  }, {
379
484
  key: "getCol",
380
485
  value: function () {
381
- var _getCol = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(col) {
382
- return _regenerator().w(function (_context0) {
383
- while (1) switch (_context0.n) {
486
+ var _getCol = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(col) {
487
+ return _regenerator().w(function (_context11) {
488
+ while (1) switch (_context11.n) {
384
489
  case 0:
385
- _context0.n = 1;
490
+ _context11.n = 1;
386
491
  return this.tree.find(col);
387
492
  case 1:
388
- return _context0.a(2, _context0.v);
493
+ return _context11.a(2, _context11.v);
389
494
  }
390
- }, _callee0, this);
495
+ }, _callee11, this);
391
496
  }));
392
- function getCol(_x14) {
497
+ function getCol(_x17) {
393
498
  return _getCol.apply(this, arguments);
394
499
  }
395
500
  return getCol;
package/esm/db.js CHANGED
@@ -4,6 +4,14 @@ import { is, indexOf, range, isNil } from "ramda"
4
4
  import { pad, toSignal, encode, toIndex } from "./encoder.js"
5
5
  import Collection from "./collection.js"
6
6
 
7
+ const to64 = hash => {
8
+ const n = BigInt(hash)
9
+ let hex = n.toString(16)
10
+ if (hex.length % 2) hex = "0" + hex
11
+ const buf = Buffer.from(hex, "hex")
12
+ return buf.toString("base64")
13
+ }
14
+
7
15
  export default class DB {
8
16
  constructor({
9
17
  size_val = 8,
@@ -279,6 +287,12 @@ export default class DB {
279
287
  col_root,
280
288
  }
281
289
  }
290
+ hash(format) {
291
+ const _hash = this.tree.F.toObject(this.tree.root)
292
+ if (format === "base64") return to64(_hash.toString())
293
+ return _hash.toString()
294
+ }
295
+
282
296
  getID(num) {
283
297
  if (!isNil(num)) {
284
298
  if (indexOf(num)(this.ids) !== -1) {
package/esm/db_tree.js CHANGED
@@ -2,6 +2,20 @@ import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
2
  import { is, indexOf, isNil } from "ramda"
3
3
  import Collection from "./collection_tree.js"
4
4
 
5
+ const to64 = hash => {
6
+ const n = BigInt(hash)
7
+ let hex = n.toString(16)
8
+ if (hex.length % 2) hex = "0" + hex
9
+ const buf = Buffer.from(hex, "hex")
10
+ return buf.toString("base64")
11
+ }
12
+ const from64 = b64 => {
13
+ const buf = Buffer.from(b64, "base64")
14
+ const hex = buf.toString("hex")
15
+ const n = BigInt("0x" + hex)
16
+ return n.toString()
17
+ }
18
+
5
19
  export default class DBTree {
6
20
  constructor({
7
21
  size_val = 256,
@@ -30,6 +44,11 @@ export default class DBTree {
30
44
  if (!isNil(count)) this.count = count
31
45
  }
32
46
  }
47
+ hash(format) {
48
+ const _hash = this.tree.F.toObject(this.tree.root)
49
+ if (format === "base64") return to64(_hash.toString())
50
+ return _hash.toString()
51
+ }
33
52
  async exists(num) {
34
53
  const ex = this.ids[num] || (await this.tree.find(num)).found
35
54
  if (ex) this.ids[num] = true
@@ -57,16 +76,7 @@ export default class DBTree {
57
76
  } else {
58
77
  id = await this.getID(num)
59
78
  }
60
- const _col = new Collection({
61
- size_val: this.size_val,
62
- size_path: this.size_path,
63
- level: this.level,
64
- size_json: this.size_json,
65
- kv: this.kv?.(`dir_${id}`),
66
- })
67
- await _col.init()
68
- this.cols[id] = _col
69
- this.ids[id] = true
79
+ const _col = await this.loadCol(id)
70
80
  const root = _col.tree.F.toObject(_col.tree.root).toString()
71
81
  await this.tree.insert(id, [root])
72
82
  if (id === this.count) {
@@ -75,13 +85,32 @@ export default class DBTree {
75
85
  }
76
86
  return id
77
87
  }
78
- getColTree(col) {
88
+ async getColTree(col) {
79
89
  const _col = this.cols[col]
80
- if (!_col) throw Error("collection doesn't exist")
81
- return _col
90
+ if (!_col) {
91
+ return await this.loadCollection(col)
92
+ } else return _col
93
+ }
94
+ async loadCol(id) {
95
+ const col = new Collection({
96
+ size_val: this.size_val,
97
+ size_path: this.size_path,
98
+ level: this.level,
99
+ size_json: this.size_json,
100
+ kv: this.kv?.(`dir_${id}`),
101
+ })
102
+ await col.init()
103
+ this.cols[id] = col
104
+ this.ids[id] = true
105
+ return col
106
+ }
107
+ async loadCollection(id) {
108
+ const hit = await this.tree.find(id)
109
+ if (!hit || !hit.found) throw Error("collection doesn't exist")
110
+ return await this.loadCol(id)
82
111
  }
83
112
  async insert(col, _key, _val) {
84
- const _col = this.getColTree(col)
113
+ const _col = await this.getColTree(col)
85
114
  let update = false
86
115
  let res_doc
87
116
  if ((await _col.get(_key)).found) {
@@ -99,21 +128,21 @@ export default class DBTree {
99
128
  return await this.tree.update(colD, [root])
100
129
  }
101
130
  async update(col, _key, _val) {
102
- const _col = this.getColTree(col)
131
+ const _col = await this.getColTree(col)
103
132
  const res_doc = await _col.update(_key, _val)
104
133
  const res_col = await this.updateDB(_col, col)
105
134
  return { doc: res_doc, col: res_col, tree: _col.tree }
106
135
  }
107
136
 
108
137
  async delete(col, _key) {
109
- const _col = this.getColTree(col)
138
+ const _col = await this.getColTree(col)
110
139
  const res_doc = await _col.delete(_key)
111
140
  const res_col = await this.updateDB(_col, col)
112
141
  return { doc: res_doc, col: res_col, tree: _col.tree }
113
142
  }
114
143
 
115
144
  async get(col, _key) {
116
- const _col = this.getColTree(col)
145
+ const _col = await this.getColTree(col)
117
146
  return await _col.get(_key)
118
147
  }
119
148
  async getCol(col) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "exports": {
6
6
  ".": {