zkjson 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/collection.js CHANGED
@@ -1,4 +1,4 @@
1
- import { newMemEmptyTrie } from "./circomlibjs.js"
1
+ import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
2
  import { pad, toSignal, encode, toIndex } from "./encoder.js"
3
3
  import Doc from "./doc.js"
4
4
 
@@ -0,0 +1,43 @@
1
+ import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
+ import { pad, toSignal, encode, toIndex } from "./encoder.js"
3
+
4
+ export default class CollectionTree {
5
+ constructor({
6
+ size_val = 256,
7
+ size_path = 32,
8
+ size_json = 256,
9
+ level = 184,
10
+ kv,
11
+ }) {
12
+ this.kv = kv
13
+ this.size_val = size_val
14
+ this.size_path = size_path
15
+ this.size_json = size_json
16
+ this.level = level
17
+ }
18
+ async init() {
19
+ this.tree = await newMemEmptyTrie(this.kv)
20
+ }
21
+ 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)
26
+ }
27
+ 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)
32
+ }
33
+
34
+ async delete(_key) {
35
+ const id = toIndex(_key)
36
+ return await this.tree.delete(id)
37
+ }
38
+
39
+ async get(_key) {
40
+ const id = toIndex(_key)
41
+ return await this.tree.find(id)
42
+ }
43
+ }
package/esm/db.js CHANGED
@@ -1,4 +1,4 @@
1
- import { newMemEmptyTrie } from "./circomlibjs.js"
1
+ import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
2
  import { groth16 } from "snarkjs"
3
3
  import { is, indexOf, range, isNil } from "ramda"
4
4
  import { pad, toSignal, encode, toIndex } from "./encoder.js"
package/esm/db_tree.js ADDED
@@ -0,0 +1,122 @@
1
+ import newMemEmptyTrie from "./newMemEmptyTrie.js"
2
+ import { is, indexOf, isNil } from "ramda"
3
+ import Collection from "./collection_tree.js"
4
+
5
+ export default class DBTree {
6
+ constructor({
7
+ size_val = 256,
8
+ size_path = 32,
9
+ level = 184,
10
+ size_json = 256,
11
+ level_col = 24,
12
+ kv,
13
+ }) {
14
+ this.kv = kv
15
+ this.level_col = level_col
16
+ this.size_val = size_val
17
+ this.size_path = size_path
18
+ this.level = level
19
+ this.size_json = size_json
20
+ this.count = 0
21
+ this.ids = {}
22
+ this.cols = []
23
+ }
24
+
25
+ async init() {
26
+ this.kvi = this.kv?.("__db__")
27
+ this.tree = await newMemEmptyTrie(this.kvi)
28
+ if (this.kvi) {
29
+ const count = this.kvi.get("__count__")
30
+ if (!isNil(count)) this.count = count
31
+ }
32
+ }
33
+ async exists(num) {
34
+ const ex = this.ids[num] || (await this.tree.find(num)).found
35
+ if (ex) this.ids[num] = true
36
+ return ex
37
+ }
38
+ async getID(num) {
39
+ if (!isNil(num)) {
40
+ if (await this.exists(num)) throw Error("id exists")
41
+ return num
42
+ } else {
43
+ while (await this.exists(this.count)) this.count++
44
+ return this.count
45
+ }
46
+ }
47
+ async addCollection(num) {
48
+ if (!isNil(num) && (!is(Number, num) || Math.round(num) !== num)) {
49
+ throw Error("id is not an integer")
50
+ }
51
+ let id = null
52
+ if (!isNil(num)) {
53
+ if (this.cols[num]) throw Error("collection exists")
54
+ const col = await this.tree.find(num)
55
+ if (col.found) throw Error("collection exists")
56
+ id = num
57
+ } else {
58
+ id = await this.getID(num)
59
+ }
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
70
+ const root = _col.tree.F.toObject(_col.tree.root).toString()
71
+ await this.tree.insert(id, [root])
72
+ if (id === this.count) {
73
+ this.count++
74
+ if (this.kvi) this.kvi.put("__count__", this.count)
75
+ }
76
+ return id
77
+ }
78
+ getColTree(col) {
79
+ const _col = this.cols[col]
80
+ if (!_col) throw Error("collection doesn't exist")
81
+ return _col
82
+ }
83
+ async insert(col, _key, _val) {
84
+ const _col = this.getColTree(col)
85
+ let update = false
86
+ let res_doc
87
+ if ((await _col.get(_key)).found) {
88
+ update = true
89
+ res_doc = await _col.update(_key, _val)
90
+ } else {
91
+ res_doc = await _col.insert(_key, _val)
92
+ }
93
+ const res_col = await this.updateDB(_col, col)
94
+ return { update, doc: res_doc, col: res_col, tree: _col.tree }
95
+ }
96
+ async updateDB(_col, col) {
97
+ const root = _col.tree.F.toObject(_col.tree.root).toString()
98
+ const colD = col
99
+ return await this.tree.update(colD, [root])
100
+ }
101
+ async update(col, _key, _val) {
102
+ const _col = this.getColTree(col)
103
+ const res_doc = await _col.update(_key, _val)
104
+ const res_col = await this.updateDB(_col, col)
105
+ return { doc: res_doc, col: res_col, tree: _col.tree }
106
+ }
107
+
108
+ async delete(col, _key) {
109
+ const _col = this.getColTree(col)
110
+ const res_doc = await _col.delete(_key)
111
+ const res_col = await this.updateDB(_col, col)
112
+ return { doc: res_doc, col: res_col, tree: _col.tree }
113
+ }
114
+
115
+ async get(col, _key) {
116
+ const _col = this.getColTree(col)
117
+ return await _col.get(_key)
118
+ }
119
+ async getCol(col) {
120
+ return await this.tree.find(col)
121
+ }
122
+ }
package/esm/index.js CHANGED
@@ -1,7 +1,9 @@
1
+ import DBTree from "./db_tree.js"
2
+ import CollectionTree from "./collection_tree.js"
1
3
  import DB from "./db.js"
2
4
  import Doc from "./doc.js"
3
5
  import Collection from "./collection.js"
4
6
  import NFT from "./nft.js"
5
- export { DB, Collection, Doc, NFT }
7
+ export { DB, Collection, Doc, NFT, DBTree, CollectionTree }
6
8
  export * from "./encoder.js"
7
9
  export * from "./parse.js"
@@ -0,0 +1,11 @@
1
+ import SMTMemDb from "./circomlibjs/SMTMemDb.js"
2
+ import SMT from "./circomlibjs/SMT.js"
3
+ import getHashes from "./circomlibjs/getHashes.js"
4
+
5
+ export default async function newMemEmptyTrie(kv) {
6
+ const { hash0, hash1, F } = await getHashes()
7
+ const db = new SMTMemDb(F, kv)
8
+ const rt = await db.getRoot()
9
+ const smt = new SMT(db, rt, hash0, hash1, F)
10
+ return smt
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "exports": {
6
6
  ".": {