zkjson 0.8.2 → 0.8.3

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.
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports["default"] = void 0;
7
7
  var _newMemEmptyTrie = _interopRequireDefault(require("./newMemEmptyTrie.js"));
8
8
  var _encoder = require("./encoder.js");
9
+ var _doc_tree = _interopRequireDefault(require("./doc_tree.js"));
9
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
10
11
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
11
12
  function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
@@ -19,21 +20,23 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
19
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); }
20
21
  var CollectionTree = exports["default"] = /*#__PURE__*/function () {
21
22
  function CollectionTree(_ref) {
22
- var _ref$size_val = _ref.size_val,
23
+ var _ref$size_json = _ref.size_json,
24
+ size_json = _ref$size_json === void 0 ? 256 : _ref$size_json,
25
+ _ref$size_val = _ref.size_val,
23
26
  size_val = _ref$size_val === void 0 ? 256 : _ref$size_val,
24
27
  _ref$size_path = _ref.size_path,
25
28
  size_path = _ref$size_path === void 0 ? 32 : _ref$size_path,
26
- _ref$size_json = _ref.size_json,
27
- size_json = _ref$size_json === void 0 ? 256 : _ref$size_json,
28
29
  _ref$level = _ref.level,
29
30
  level = _ref$level === void 0 ? 184 : _ref$level,
30
31
  kv = _ref.kv;
31
32
  _classCallCheck(this, CollectionTree);
32
33
  this.kv = kv;
33
- this.size_val = size_val;
34
- this.size_path = size_path;
35
- this.size_json = size_json;
36
34
  this.level = level;
35
+ this.doc = new _doc_tree["default"]({
36
+ size_val: this.size_val = size_val,
37
+ size_path: this.size_path = size_path,
38
+ size_json: this.size_json = size_json
39
+ });
37
40
  }
38
41
  return _createClass(CollectionTree, [{
39
42
  key: "init",
@@ -60,15 +63,11 @@ var CollectionTree = exports["default"] = /*#__PURE__*/function () {
60
63
  key: "insert",
61
64
  value: function () {
62
65
  var _insert = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_key, _val) {
63
- var doc, id, val;
64
66
  return _regenerator().w(function (_context2) {
65
67
  while (1) switch (_context2.n) {
66
68
  case 0:
67
- doc = (0, _encoder.encode)(_val);
68
- id = (0, _encoder.toIndex)(_key);
69
- val = (0, _encoder.pad)((0, _encoder.toSignal)(doc), this.size_json);
70
69
  _context2.n = 1;
71
- return this.tree.insert(id, val);
70
+ return this.tree.insert((0, _encoder.toIndex)(_key), (0, _encoder.pad)((0, _encoder.toSignal)((0, _encoder.encode)(_val)), this.size_json));
72
71
  case 1:
73
72
  return _context2.a(2, _context2.v);
74
73
  }
@@ -83,15 +82,11 @@ var CollectionTree = exports["default"] = /*#__PURE__*/function () {
83
82
  key: "update",
84
83
  value: function () {
85
84
  var _update = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_key, _val) {
86
- var doc, id, val;
87
85
  return _regenerator().w(function (_context3) {
88
86
  while (1) switch (_context3.n) {
89
87
  case 0:
90
- doc = (0, _encoder.encode)(_val);
91
- id = (0, _encoder.toIndex)(_key);
92
- val = (0, _encoder.pad)((0, _encoder.toSignal)(doc), this.size_json);
93
88
  _context3.n = 1;
94
- return this.tree.update(id, val);
89
+ return this.tree.update((0, _encoder.toIndex)(_key), (0, _encoder.pad)((0, _encoder.toSignal)((0, _encoder.encode)(_val)), this.size_json));
95
90
  case 1:
96
91
  return _context3.a(2, _context3.v);
97
92
  }
@@ -106,13 +101,11 @@ var CollectionTree = exports["default"] = /*#__PURE__*/function () {
106
101
  key: "delete",
107
102
  value: function () {
108
103
  var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_key) {
109
- var id;
110
104
  return _regenerator().w(function (_context4) {
111
105
  while (1) switch (_context4.n) {
112
106
  case 0:
113
- id = (0, _encoder.toIndex)(_key);
114
107
  _context4.n = 1;
115
- return this.tree["delete"](id);
108
+ return this.tree["delete"]((0, _encoder.toIndex)(_key));
116
109
  case 1:
117
110
  return _context4.a(2, _context4.v);
118
111
  }
@@ -127,13 +120,11 @@ var CollectionTree = exports["default"] = /*#__PURE__*/function () {
127
120
  key: "get",
128
121
  value: function () {
129
122
  var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(_key) {
130
- var id;
131
123
  return _regenerator().w(function (_context5) {
132
124
  while (1) switch (_context5.n) {
133
125
  case 0:
134
- id = (0, _encoder.toIndex)(_key);
135
126
  _context5.n = 1;
136
- return this.tree.find(id);
127
+ return this.tree.find((0, _encoder.toIndex)(_key));
137
128
  case 1:
138
129
  return _context5.a(2, _context5.v);
139
130
  }
@@ -144,5 +135,49 @@ var CollectionTree = exports["default"] = /*#__PURE__*/function () {
144
135
  }
145
136
  return get;
146
137
  }()
138
+ }, {
139
+ key: "getInputs",
140
+ value: function () {
141
+ var _getInputs = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(_ref2) {
142
+ var id, json, path, val, query, doc_inputs, res, siblings, i;
143
+ return _regenerator().w(function (_context6) {
144
+ while (1) switch (_context6.n) {
145
+ case 0:
146
+ id = _ref2.id, json = _ref2.json, path = _ref2.path, val = _ref2.val, query = _ref2.query;
147
+ _context6.n = 1;
148
+ return this.doc.getInputs({
149
+ json: json,
150
+ path: path,
151
+ val: val,
152
+ query: query
153
+ });
154
+ case 1:
155
+ doc_inputs = _context6.v;
156
+ _context6.n = 2;
157
+ return this.get(id);
158
+ case 2:
159
+ res = _context6.v;
160
+ siblings = res.siblings;
161
+ for (i = 0; i < siblings.length; i++) siblings[i] = this.tree.F.toObject(siblings[i]);
162
+ while (siblings.length < this.level) siblings.push(0);
163
+ siblings = siblings.map(function (s) {
164
+ return s.toString();
165
+ });
166
+ return _context6.a(2, {
167
+ json: doc_inputs.json,
168
+ path: doc_inputs.path,
169
+ val: doc_inputs.val,
170
+ root: this.tree.F.toObject(this.tree.root).toString(),
171
+ siblings: siblings,
172
+ key: (0, _encoder.toIndex)(id)
173
+ });
174
+ }
175
+ }, _callee6, this);
176
+ }));
177
+ function getInputs(_x7) {
178
+ return _getInputs.apply(this, arguments);
179
+ }
180
+ return getInputs;
181
+ }()
147
182
  }]);
148
183
  }();
package/cjs/db_tree.js CHANGED
@@ -7,6 +7,7 @@ exports["default"] = void 0;
7
7
  var _newMemEmptyTrie = _interopRequireDefault(require("./newMemEmptyTrie.js"));
8
8
  var _ramda = require("ramda");
9
9
  var _collection_tree = _interopRequireDefault(require("./collection_tree.js"));
10
+ var _encoder = require("./encoder.js");
10
11
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
12
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
13
  function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
@@ -25,32 +26,26 @@ var to64 = function to64(hash) {
25
26
  var buf = Buffer.from(hex, "hex");
26
27
  return buf.toString("base64");
27
28
  };
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
- };
34
29
  var DBTree = exports["default"] = /*#__PURE__*/function () {
35
30
  function DBTree(_ref) {
36
- var _ref$size_val = _ref.size_val,
31
+ var _ref$size_json = _ref.size_json,
32
+ size_json = _ref$size_json === void 0 ? 256 : _ref$size_json,
33
+ _ref$level_col = _ref.level_col,
34
+ level_col = _ref$level_col === void 0 ? 24 : _ref$level_col,
35
+ _ref$size_val = _ref.size_val,
37
36
  size_val = _ref$size_val === void 0 ? 256 : _ref$size_val,
38
37
  _ref$size_path = _ref.size_path,
39
38
  size_path = _ref$size_path === void 0 ? 32 : _ref$size_path,
40
39
  _ref$level = _ref.level,
41
40
  level = _ref$level === void 0 ? 184 : _ref$level,
42
- _ref$size_json = _ref.size_json,
43
- size_json = _ref$size_json === void 0 ? 256 : _ref$size_json,
44
- _ref$level_col = _ref.level_col,
45
- level_col = _ref$level_col === void 0 ? 24 : _ref$level_col,
46
41
  kv = _ref.kv;
47
42
  _classCallCheck(this, DBTree);
48
43
  this.kv = kv;
44
+ this.level = level;
49
45
  this.level_col = level_col;
46
+ this.size_json = size_json;
50
47
  this.size_val = size_val;
51
48
  this.size_path = size_path;
52
- this.level = level;
53
- this.size_json = size_json;
54
49
  this.count = 0;
55
50
  this.ids = {};
56
51
  this.cols = [];
@@ -275,10 +270,10 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
275
270
  while (1) switch (_context6.n) {
276
271
  case 0:
277
272
  col = new _collection_tree["default"]({
273
+ size_json: this.size_json,
278
274
  size_val: this.size_val,
279
275
  size_path: this.size_path,
280
276
  level: this.level,
281
- size_json: this.size_json,
282
277
  kv: (_this$kv2 = this.kv) === null || _this$kv2 === void 0 ? void 0 : _this$kv2.call(this, "dir_".concat(id))
283
278
  });
284
279
  _context6.n = 1;
@@ -507,5 +502,59 @@ var DBTree = exports["default"] = /*#__PURE__*/function () {
507
502
  }
508
503
  return getCol;
509
504
  }()
505
+ }, {
506
+ key: "getInputs",
507
+ value: function () {
508
+ var _getInputs = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12(_ref2) {
509
+ var id, col_id, json, path, val, query, col_root, col_res, col_siblings, i, col_key, col, col_inputs;
510
+ return _regenerator().w(function (_context12) {
511
+ while (1) switch (_context12.n) {
512
+ case 0:
513
+ id = _ref2.id, col_id = _ref2.col_id, json = _ref2.json, path = _ref2.path, val = _ref2.val, query = _ref2.query;
514
+ col_root = this.tree.F.toObject(this.tree.root).toString();
515
+ _context12.n = 1;
516
+ return this.getCol(col_id);
517
+ case 1:
518
+ col_res = _context12.v;
519
+ col_siblings = col_res.siblings;
520
+ for (i = 0; i < col_siblings.length; i++) col_siblings[i] = this.tree.F.toObject(col_siblings[i]);
521
+ while (col_siblings.length < this.level_col) col_siblings.push(0);
522
+ col_siblings = col_siblings.map(function (s) {
523
+ return s.toString();
524
+ });
525
+ col_key = col_id;
526
+ _context12.n = 2;
527
+ return this.getColTree(col_id);
528
+ case 2:
529
+ col = _context12.v;
530
+ _context12.n = 3;
531
+ return col.getInputs({
532
+ id: id,
533
+ json: json,
534
+ path: path,
535
+ val: val,
536
+ query: query
537
+ });
538
+ case 3:
539
+ col_inputs = _context12.v;
540
+ return _context12.a(2, {
541
+ path: col_inputs.path,
542
+ val: col_inputs.val,
543
+ json: col_inputs.json,
544
+ root: col_inputs.root,
545
+ siblings: col_inputs.siblings,
546
+ key: (0, _encoder.toIndex)(id),
547
+ col_key: col_key,
548
+ col_siblings: col_siblings,
549
+ col_root: col_root
550
+ });
551
+ }
552
+ }, _callee12, this);
553
+ }));
554
+ function getInputs(_x18) {
555
+ return _getInputs.apply(this, arguments);
556
+ }
557
+ return getInputs;
558
+ }()
510
559
  }]);
511
560
  }();
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _ramda = require("ramda");
8
+ var _encoder = require("./encoder.js");
9
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
10
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
11
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
12
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
13
+ function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
14
+ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
15
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
16
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
17
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
18
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
19
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
20
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
21
+ 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); }
22
+ var DocTree = exports["default"] = /*#__PURE__*/function () {
23
+ function DocTree(_ref) {
24
+ var _ref$size_val = _ref.size_val,
25
+ size_val = _ref$size_val === void 0 ? 256 : _ref$size_val,
26
+ _ref$size_path = _ref.size_path,
27
+ size_path = _ref$size_path === void 0 ? 32 : _ref$size_path,
28
+ _ref$size_json = _ref.size_json,
29
+ size_json = _ref$size_json === void 0 ? 256 : _ref$size_json;
30
+ _classCallCheck(this, DocTree);
31
+ this.size_val = size_val;
32
+ this.size_path = size_path;
33
+ this.size_json = size_json;
34
+ }
35
+ return _createClass(DocTree, [{
36
+ key: "getInputs",
37
+ value: function () {
38
+ var _getInputs = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref2) {
39
+ var query, json, path;
40
+ return _regenerator().w(function (_context) {
41
+ while (1) switch (_context.n) {
42
+ case 0:
43
+ query = _ref2.query, json = _ref2.json, path = _ref2.path;
44
+ return _context.a(2, {
45
+ json: (0, _encoder.pad)((0, _encoder.toSignal)((0, _encoder.encode)(json)), this.size_json),
46
+ path: (0, _encoder.pad)((0, _encoder.toSignal)((0, _encoder.encodePath)(path)), this.size_path),
47
+ val: (0, _ramda.isNil)(query) ? (0, _encoder.pad)((0, _encoder.toSignal)((0, _encoder.encodeVal)(this.getVal(json, path))), this.size_val) : (0, _encoder.pad)((0, _encoder.toSignal)((0, _encoder.encodeQuery)(query)), this.size_val)
48
+ });
49
+ }
50
+ }, _callee, this);
51
+ }));
52
+ function getInputs(_x) {
53
+ return _getInputs.apply(this, arguments);
54
+ }
55
+ return getInputs;
56
+ }()
57
+ }, {
58
+ key: "_getVal",
59
+ value: function _getVal(j, p) {
60
+ if (p.length === 0) return j;else {
61
+ var sp = p[0].split("[");
62
+ var _iterator = _createForOfIteratorHelper(sp),
63
+ _step;
64
+ try {
65
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
66
+ var v = _step.value;
67
+ if (/]$/.test(v)) j = j[v.replace(/]$/, "") * 1];else j = j[v];
68
+ }
69
+ } catch (err) {
70
+ _iterator.e(err);
71
+ } finally {
72
+ _iterator.f();
73
+ }
74
+ return this._getVal(j, p.slice(1));
75
+ }
76
+ }
77
+ }, {
78
+ key: "getVal",
79
+ value: function getVal(j, p) {
80
+ if (p === "") return j;
81
+ return this._getVal(j, p.split("."));
82
+ }
83
+ }]);
84
+ }();
package/cjs/index.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  var _exportNames = {
7
7
  DBTree: true,
8
+ Prover: true,
8
9
  CollectionTree: true,
9
10
  DB: true,
10
11
  Doc: true,
@@ -47,7 +48,14 @@ Object.defineProperty(exports, "NFT", {
47
48
  return _nft["default"];
48
49
  }
49
50
  });
51
+ Object.defineProperty(exports, "Prover", {
52
+ enumerable: true,
53
+ get: function get() {
54
+ return _prover["default"];
55
+ }
56
+ });
50
57
  var _db_tree = _interopRequireDefault(require("./db_tree.js"));
58
+ var _prover = _interopRequireDefault(require("./prover.js"));
51
59
  var _collection_tree = _interopRequireDefault(require("./collection_tree.js"));
52
60
  var _db = _interopRequireDefault(require("./db.js"));
53
61
  var _doc = _interopRequireDefault(require("./doc.js"));
package/cjs/prover.js ADDED
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _snarkjs = require("snarkjs");
8
+ var _encoder = require("./encoder.js");
9
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
10
+ function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
11
+ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
12
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
13
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
14
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
15
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
16
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
17
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
18
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
19
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
20
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
21
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
22
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
23
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
24
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
25
+ 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); }
26
+ var Prover = exports["default"] = /*#__PURE__*/function () {
27
+ function Prover(_ref) {
28
+ var wasm = _ref.wasm,
29
+ zkey = _ref.zkey;
30
+ _classCallCheck(this, Prover);
31
+ this.wasm = wasm;
32
+ this.zkey = zkey;
33
+ }
34
+ return _createClass(Prover, [{
35
+ key: "_getVal",
36
+ value: function _getVal(j, p) {
37
+ if (p.length === 0) return j;else {
38
+ var sp = p[0].split("[");
39
+ var _iterator = _createForOfIteratorHelper(sp),
40
+ _step;
41
+ try {
42
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
43
+ var v = _step.value;
44
+ if (/]$/.test(v)) j = j[v.replace(/]$/, "") * 1];else j = j[v];
45
+ }
46
+ } catch (err) {
47
+ _iterator.e(err);
48
+ } finally {
49
+ _iterator.f();
50
+ }
51
+ return this._getVal(j, p.slice(1));
52
+ }
53
+ }
54
+ }, {
55
+ key: "getVal",
56
+ value: function getVal(j, p) {
57
+ if (p === "") return j;
58
+ return this._getVal(j, p.split("."));
59
+ }
60
+ }, {
61
+ key: "genProof",
62
+ value: function () {
63
+ var _genProof = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(inputs) {
64
+ var _yield$groth16$fullPr, proof, publicSignals;
65
+ return _regenerator().w(function (_context) {
66
+ while (1) switch (_context.n) {
67
+ case 0:
68
+ _context.n = 1;
69
+ return _snarkjs.groth16.fullProve(inputs, this.wasm, this.zkey);
70
+ case 1:
71
+ _yield$groth16$fullPr = _context.v;
72
+ proof = _yield$groth16$fullPr.proof;
73
+ publicSignals = _yield$groth16$fullPr.publicSignals;
74
+ return _context.a(2, [].concat(_toConsumableArray(proof.pi_a.slice(0, 2)), _toConsumableArray(proof.pi_b[0].slice(0, 2).reverse()), _toConsumableArray(proof.pi_b[1].slice(0, 2).reverse()), _toConsumableArray(proof.pi_c.slice(0, 2)), _toConsumableArray(publicSignals)));
75
+ }
76
+ }, _callee, this);
77
+ }));
78
+ function genProof(_x) {
79
+ return _genProof.apply(this, arguments);
80
+ }
81
+ return genProof;
82
+ }()
83
+ }]);
84
+ }();
@@ -1,43 +1,65 @@
1
1
  import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
2
  import { pad, toSignal, encode, toIndex } from "./encoder.js"
3
+ import Doc from "./doc_tree.js"
3
4
 
4
5
  export default class CollectionTree {
5
6
  constructor({
7
+ size_json = 256,
6
8
  size_val = 256,
7
9
  size_path = 32,
8
- size_json = 256,
9
10
  level = 184,
10
11
  kv,
11
12
  }) {
12
13
  this.kv = kv
13
- this.size_val = size_val
14
- this.size_path = size_path
15
- this.size_json = size_json
16
14
  this.level = level
15
+ this.doc = new Doc({
16
+ size_val: (this.size_val = size_val),
17
+ size_path: (this.size_path = size_path),
18
+ size_json: (this.size_json = size_json),
19
+ })
17
20
  }
21
+
18
22
  async init() {
19
23
  this.tree = await newMemEmptyTrie(this.kv)
20
24
  }
25
+
21
26
  async insert(_key, _val) {
22
- const doc = encode(_val)
23
- const id = toIndex(_key)
24
- const val = pad(toSignal(doc), this.size_json)
25
- return await this.tree.insert(id, val)
27
+ return await this.tree.insert(
28
+ toIndex(_key),
29
+ pad(toSignal(encode(_val)), this.size_json),
30
+ )
26
31
  }
32
+
27
33
  async update(_key, _val) {
28
- const doc = encode(_val)
29
- const id = toIndex(_key)
30
- const val = pad(toSignal(doc), this.size_json)
31
- return await this.tree.update(id, val)
34
+ return await this.tree.update(
35
+ toIndex(_key),
36
+ pad(toSignal(encode(_val)), this.size_json),
37
+ )
32
38
  }
33
39
 
34
40
  async delete(_key) {
35
- const id = toIndex(_key)
36
- return await this.tree.delete(id)
41
+ return await this.tree.delete(toIndex(_key))
37
42
  }
38
43
 
39
44
  async get(_key) {
40
- const id = toIndex(_key)
41
- return await this.tree.find(id)
45
+ return await this.tree.find(toIndex(_key))
46
+ }
47
+
48
+ async getInputs({ id, json, path, val, query }) {
49
+ const doc_inputs = await this.doc.getInputs({ json, path, val, query })
50
+ const res = await this.get(id)
51
+ let siblings = res.siblings
52
+ for (let i = 0; i < siblings.length; i++)
53
+ siblings[i] = this.tree.F.toObject(siblings[i])
54
+ while (siblings.length < this.level) siblings.push(0)
55
+ siblings = siblings.map(s => s.toString())
56
+ return {
57
+ json: doc_inputs.json,
58
+ path: doc_inputs.path,
59
+ val: doc_inputs.val,
60
+ root: this.tree.F.toObject(this.tree.root).toString(),
61
+ siblings,
62
+ key: toIndex(id),
63
+ }
42
64
  }
43
65
  }
package/esm/db_tree.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
2
  import { is, indexOf, isNil } from "ramda"
3
3
  import Collection from "./collection_tree.js"
4
+ import { toIndex } from "./encoder.js"
4
5
 
5
6
  const to64 = hash => {
6
7
  const n = BigInt(hash)
@@ -9,28 +10,22 @@ const to64 = hash => {
9
10
  const buf = Buffer.from(hex, "hex")
10
11
  return buf.toString("base64")
11
12
  }
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
13
 
19
14
  export default class DBTree {
20
15
  constructor({
16
+ size_json = 256,
17
+ level_col = 24,
21
18
  size_val = 256,
22
19
  size_path = 32,
23
20
  level = 184,
24
- size_json = 256,
25
- level_col = 24,
26
21
  kv,
27
22
  }) {
28
23
  this.kv = kv
24
+ this.level = level
29
25
  this.level_col = level_col
26
+ this.size_json = size_json
30
27
  this.size_val = size_val
31
28
  this.size_path = size_path
32
- this.level = level
33
- this.size_json = size_json
34
29
  this.count = 0
35
30
  this.ids = {}
36
31
  this.cols = []
@@ -93,10 +88,10 @@ export default class DBTree {
93
88
  }
94
89
  async loadCol(id) {
95
90
  const col = new Collection({
91
+ size_json: this.size_json,
96
92
  size_val: this.size_val,
97
93
  size_path: this.size_path,
98
94
  level: this.level,
99
- size_json: this.size_json,
100
95
  kv: this.kv?.(`dir_${id}`),
101
96
  })
102
97
  await col.init()
@@ -148,4 +143,27 @@ export default class DBTree {
148
143
  async getCol(col) {
149
144
  return await this.tree.find(col)
150
145
  }
146
+ async getInputs({ id, col_id, json, path, val, query }) {
147
+ const col_root = this.tree.F.toObject(this.tree.root).toString()
148
+ const col_res = await this.getCol(col_id)
149
+ let col_siblings = col_res.siblings
150
+ for (let i = 0; i < col_siblings.length; i++)
151
+ col_siblings[i] = this.tree.F.toObject(col_siblings[i])
152
+ while (col_siblings.length < this.level_col) col_siblings.push(0)
153
+ col_siblings = col_siblings.map(s => s.toString())
154
+ const col_key = col_id
155
+ const col = await this.getColTree(col_id)
156
+ const col_inputs = await col.getInputs({ id, json, path, val, query })
157
+ return {
158
+ path: col_inputs.path,
159
+ val: col_inputs.val,
160
+ json: col_inputs.json,
161
+ root: col_inputs.root,
162
+ siblings: col_inputs.siblings,
163
+ key: toIndex(id),
164
+ col_key,
165
+ col_siblings,
166
+ col_root,
167
+ }
168
+ }
151
169
  }
@@ -0,0 +1,41 @@
1
+ import { isNil } from "ramda"
2
+ import {
3
+ encodeQuery,
4
+ pad,
5
+ toSignal,
6
+ encode,
7
+ encodePath,
8
+ encodeVal,
9
+ } from "./encoder.js"
10
+
11
+ export default class DocTree {
12
+ constructor({ size_val = 256, size_path = 32, size_json = 256 }) {
13
+ this.size_val = size_val
14
+ this.size_path = size_path
15
+ this.size_json = size_json
16
+ }
17
+ async getInputs({ query, json, path }) {
18
+ return {
19
+ json: pad(toSignal(encode(json)), this.size_json),
20
+ path: pad(toSignal(encodePath(path)), this.size_path),
21
+ val: isNil(query)
22
+ ? pad(toSignal(encodeVal(this.getVal(json, path))), this.size_val)
23
+ : pad(toSignal(encodeQuery(query)), this.size_val),
24
+ }
25
+ }
26
+ _getVal(j, p) {
27
+ if (p.length === 0) return j
28
+ else {
29
+ const sp = p[0].split("[")
30
+ for (let v of sp) {
31
+ if (/]$/.test(v)) j = j[v.replace(/]$/, "") * 1]
32
+ else j = j[v]
33
+ }
34
+ return this._getVal(j, p.slice(1))
35
+ }
36
+ }
37
+ getVal(j, p) {
38
+ if (p === "") return j
39
+ return this._getVal(j, p.split("."))
40
+ }
41
+ }
package/esm/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import DBTree from "./db_tree.js"
2
+ import Prover from "./prover.js"
2
3
  import CollectionTree from "./collection_tree.js"
3
4
  import DB from "./db.js"
4
5
  import Doc from "./doc.js"
5
6
  import Collection from "./collection.js"
6
7
  import NFT from "./nft.js"
7
- export { DB, Collection, Doc, NFT, DBTree, CollectionTree }
8
+ export { DB, Collection, Doc, NFT, DBTree, CollectionTree, Prover }
8
9
  export * from "./encoder.js"
9
10
  export * from "./parse.js"
package/esm/prover.js ADDED
@@ -0,0 +1,41 @@
1
+ import { groth16 } from "snarkjs"
2
+ import { toIndex } from "./encoder.js"
3
+
4
+ export default class Prover {
5
+ constructor({ wasm, zkey }) {
6
+ this.wasm = wasm
7
+ this.zkey = zkey
8
+ }
9
+
10
+ _getVal(j, p) {
11
+ if (p.length === 0) return j
12
+ else {
13
+ const sp = p[0].split("[")
14
+ for (let v of sp) {
15
+ if (/]$/.test(v)) j = j[v.replace(/]$/, "") * 1]
16
+ else j = j[v]
17
+ }
18
+ return this._getVal(j, p.slice(1))
19
+ }
20
+ }
21
+
22
+ getVal(j, p) {
23
+ if (p === "") return j
24
+ return this._getVal(j, p.split("."))
25
+ }
26
+
27
+ async genProof(inputs) {
28
+ const { proof, publicSignals } = await groth16.fullProve(
29
+ inputs,
30
+ this.wasm,
31
+ this.zkey,
32
+ )
33
+ return [
34
+ ...proof.pi_a.slice(0, 2),
35
+ ...proof.pi_b[0].slice(0, 2).reverse(),
36
+ ...proof.pi_b[1].slice(0, 2).reverse(),
37
+ ...proof.pi_c.slice(0, 2),
38
+ ...publicSignals,
39
+ ]
40
+ }
41
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "exports": {
6
6
  ".": {