zkjson 0.1.3 → 0.1.4
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/collection.js +24 -3
- package/db.js +173 -3
- package/doc.js +15 -0
- package/index.js +2 -1
- package/json.js +15 -0
- package/package.json +4 -4
package/collection.js
CHANGED
@@ -1,9 +1,30 @@
|
|
1
1
|
const newMemEmptyTrie = require("./circomlibjs").newMemEmptyTrie
|
2
2
|
const { pad, str2val, val2str, id2str, encode, str2id } = require("./encoder")
|
3
|
+
const Doc = require("./doc")
|
3
4
|
|
4
5
|
class Collection {
|
5
|
-
constructor(size = 16) {
|
6
|
+
constructor({ size = 5, size_json = 16, level = 100 }) {
|
6
7
|
this.size = size
|
8
|
+
this.size_json = size_json
|
9
|
+
this.level = level
|
10
|
+
this.doc = new Doc({ size: this.size, size_json: this.size_json })
|
11
|
+
}
|
12
|
+
async getInputs({ id, json, path, val }) {
|
13
|
+
const doc_inputs = await this.doc.getInputs({ json, path, val })
|
14
|
+
const res = await this.get(id)
|
15
|
+
let siblings = res.siblings
|
16
|
+
for (let i = 0; i < siblings.length; i++)
|
17
|
+
siblings[i] = this.tree.F.toObject(siblings[i])
|
18
|
+
while (siblings.length < this.level) siblings.push(0)
|
19
|
+
siblings = siblings.map(s => s.toString())
|
20
|
+
return {
|
21
|
+
json: doc_inputs.json,
|
22
|
+
path: doc_inputs.path,
|
23
|
+
val: doc_inputs.val,
|
24
|
+
root: this.tree.F.toObject(this.tree.root).toString(),
|
25
|
+
siblings,
|
26
|
+
key: str2id(id),
|
27
|
+
}
|
7
28
|
}
|
8
29
|
async init() {
|
9
30
|
this.tree = await newMemEmptyTrie()
|
@@ -11,13 +32,13 @@ class Collection {
|
|
11
32
|
async insert(_key, _val) {
|
12
33
|
const doc = encode(_val)
|
13
34
|
const id = str2id(_key)
|
14
|
-
const val = pad(val2str(doc), this.
|
35
|
+
const val = pad(val2str(doc), this.size_json)
|
15
36
|
return await this.tree.insert(id, val)
|
16
37
|
}
|
17
38
|
async update(_key, _val) {
|
18
39
|
const doc = encode(_val)
|
19
40
|
const id = str2id(_key)
|
20
|
-
const val = pad(val2str(doc), this.
|
41
|
+
const val = pad(val2str(doc), this.size_json)
|
21
42
|
return await this.tree.update(id, val)
|
22
43
|
}
|
23
44
|
|
package/db.js
CHANGED
@@ -1,20 +1,190 @@
|
|
1
1
|
const newMemEmptyTrie = require("./circomlibjs").newMemEmptyTrie
|
2
|
-
const {
|
2
|
+
const { range } = require("ramda")
|
3
|
+
const {
|
4
|
+
pad,
|
5
|
+
toSignal,
|
6
|
+
str2val,
|
7
|
+
val2str,
|
8
|
+
id2str,
|
9
|
+
encode,
|
10
|
+
str2id,
|
11
|
+
} = require("./encoder")
|
3
12
|
const Collection = require("./collection")
|
4
13
|
|
5
14
|
class DB {
|
6
|
-
constructor(size = 16) {
|
15
|
+
constructor({ size = 5, level = 40, size_json = 16, size_txs = 10 }) {
|
7
16
|
this.size = size
|
17
|
+
this.level = level
|
18
|
+
this.size_json = size_json
|
19
|
+
this.size_txs = size_txs
|
8
20
|
}
|
21
|
+
|
9
22
|
async init() {
|
10
23
|
this.tree = await newMemEmptyTrie()
|
11
24
|
this.cols = {}
|
12
25
|
}
|
26
|
+
|
27
|
+
parse(res, tree) {
|
28
|
+
const isOld0 = res.isOld0 ? "1" : "0"
|
29
|
+
const oldRoot = tree.F.toObject(res.oldRoot).toString()
|
30
|
+
const newRoot = tree.F.toObject(res.newRoot).toString()
|
31
|
+
const oldKey = res.isOld0 ? "0" : tree.F.toObject(res.oldKey).toString()
|
32
|
+
const oldValue = res.isOld0 ? "0" : tree.F.toObject(res.oldValue).toString()
|
33
|
+
let siblings = res.siblings
|
34
|
+
for (let i = 0; i < siblings.length; i++)
|
35
|
+
siblings[i] = tree.F.toObject(siblings[i])
|
36
|
+
while (siblings.length < this.level) siblings.push(0)
|
37
|
+
siblings = siblings.map(s => s.toString())
|
38
|
+
return { isOld0, oldRoot, oldKey, oldValue, siblings, newRoot }
|
39
|
+
}
|
40
|
+
|
41
|
+
async getRollupInputs({ queries }) {
|
42
|
+
let write, _json
|
43
|
+
let oldRoot = []
|
44
|
+
let newRoot = []
|
45
|
+
let oldKey = []
|
46
|
+
let oldValue = []
|
47
|
+
let siblings = []
|
48
|
+
let isOld0 = []
|
49
|
+
let oldRoot_db = []
|
50
|
+
let newRoot_db = []
|
51
|
+
let oldKey_db = []
|
52
|
+
let oldValue_db = []
|
53
|
+
let siblings_db = []
|
54
|
+
let isOld0_db = []
|
55
|
+
let newKey_db = []
|
56
|
+
let newKey = []
|
57
|
+
let _res
|
58
|
+
let json = []
|
59
|
+
let fnc = []
|
60
|
+
for (let i = 0; i < this.size_txs; i++) {
|
61
|
+
const v = queries[i]
|
62
|
+
if (!v) {
|
63
|
+
json.push(range(0, this.size_json).map(() => "0"))
|
64
|
+
fnc.push([0, 0])
|
65
|
+
newRoot.push(newRoot[i - 1])
|
66
|
+
oldRoot.push("0")
|
67
|
+
oldKey.push("0")
|
68
|
+
oldValue.push("0")
|
69
|
+
siblings.push(range(0, this.level).map(() => "0"))
|
70
|
+
isOld0.push("0")
|
71
|
+
oldRoot_db.push(newRoot_db[i - 1])
|
72
|
+
oldKey_db.push("0")
|
73
|
+
oldValue_db.push("0")
|
74
|
+
siblings_db.push(range(0, this.level).map(() => "0"))
|
75
|
+
isOld0_db.push("0")
|
76
|
+
newKey_db.push("0")
|
77
|
+
newKey.push("0")
|
78
|
+
continue
|
79
|
+
}
|
80
|
+
_json = v[2]
|
81
|
+
const { update, tree, col: res2, doc: res } = await this.insert(...v)
|
82
|
+
const icol = this.parse(res, tree)
|
83
|
+
const idb = this.parse(res2, this.tree)
|
84
|
+
_res = idb
|
85
|
+
const _newKey = str2id(v[1])
|
86
|
+
json.push(pad(val2str(encode(_json)), this.size_json))
|
87
|
+
const _newKey_db = str2id(v[0])
|
88
|
+
fnc.push(update ? [0, 1] : [1, 0])
|
89
|
+
newRoot.push(idb.newRoot)
|
90
|
+
oldRoot.push(icol.oldRoot)
|
91
|
+
oldKey.push(icol.oldKey)
|
92
|
+
oldValue.push(icol.oldValue)
|
93
|
+
siblings.push(icol.siblings)
|
94
|
+
isOld0.push(icol.isOld0)
|
95
|
+
oldRoot_db.push(idb.oldRoot)
|
96
|
+
newRoot_db.push(idb.newRoot)
|
97
|
+
oldKey_db.push(idb.oldKey)
|
98
|
+
oldValue_db.push(idb.oldValue)
|
99
|
+
siblings_db.push(idb.siblings)
|
100
|
+
isOld0_db.push(idb.isOld0)
|
101
|
+
newKey_db.push(_newKey_db)
|
102
|
+
newKey.push(_newKey)
|
103
|
+
}
|
104
|
+
|
105
|
+
return {
|
106
|
+
fnc,
|
107
|
+
oldRoot,
|
108
|
+
newRoot,
|
109
|
+
oldKey,
|
110
|
+
oldValue,
|
111
|
+
siblings,
|
112
|
+
isOld0,
|
113
|
+
oldRoot_db,
|
114
|
+
oldKey_db,
|
115
|
+
oldValue_db,
|
116
|
+
siblings_db,
|
117
|
+
isOld0_db,
|
118
|
+
newKey_db,
|
119
|
+
newKey,
|
120
|
+
json,
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
async getQueryInputs({ id, col_id, json }) {
|
125
|
+
const {
|
126
|
+
update,
|
127
|
+
tree,
|
128
|
+
col: res2,
|
129
|
+
doc: res,
|
130
|
+
} = await this.insert(col_id, id, json)
|
131
|
+
const icol = this.parse(res, tree)
|
132
|
+
const idb = this.parse(res2, this.tree)
|
133
|
+
const newKey = str2id(id)
|
134
|
+
const newKey_db = str2id(col_id)
|
135
|
+
return {
|
136
|
+
fnc: update ? [0, 1] : [1, 0],
|
137
|
+
oldRoot: icol.oldRoot,
|
138
|
+
oldKey: icol.oldKey,
|
139
|
+
oldValue: icol.oldValue,
|
140
|
+
siblings: icol.siblings,
|
141
|
+
isOld0: icol.isOld0,
|
142
|
+
oldRoot_db: idb.oldRoot,
|
143
|
+
oldKey_db: idb.oldKey,
|
144
|
+
oldValue_db: idb.oldValue,
|
145
|
+
siblings_db: idb.siblings,
|
146
|
+
isOld0_db: idb.isOld0,
|
147
|
+
newRoot: idb.newRoot,
|
148
|
+
newKey_db,
|
149
|
+
newKey,
|
150
|
+
json: pad(toSignal(encode(json)), this.size_json),
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
async getInputs({ id, col_id, json, path, val }) {
|
155
|
+
const col_root = this.tree.F.toObject(this.tree.root).toString()
|
156
|
+
const col_res = await this.getCol(id)
|
157
|
+
|
158
|
+
let col_siblings = col_res.siblings
|
159
|
+
for (let i = 0; i < col_siblings.length; i++)
|
160
|
+
col_siblings[i] = this.tree.F.toObject(col_siblings[i])
|
161
|
+
while (col_siblings.length < this.level) col_siblings.push(0)
|
162
|
+
col_siblings = col_siblings.map(s => s.toString())
|
163
|
+
const col_key = str2id(col_id)
|
164
|
+
const col = this.getColTree(col_id)
|
165
|
+
const col_inputs = await col.getInputs({ id, json, path, val })
|
166
|
+
return {
|
167
|
+
path: col_inputs.path,
|
168
|
+
val: col_inputs.val,
|
169
|
+
json: col_inputs.json,
|
170
|
+
root: col_inputs.root,
|
171
|
+
siblings: col_inputs.siblings,
|
172
|
+
key: str2id(id),
|
173
|
+
col_key,
|
174
|
+
col_siblings,
|
175
|
+
col_root,
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
13
179
|
async addCollection(_key) {
|
14
180
|
const id = str2id(_key)
|
15
181
|
const col = await this.tree.find(id)
|
16
182
|
if (col.found) throw Error("collection exists")
|
17
|
-
const _col = new Collection(
|
183
|
+
const _col = new Collection({
|
184
|
+
size: this.size,
|
185
|
+
level: this.level,
|
186
|
+
size_json: this.size_json,
|
187
|
+
})
|
18
188
|
await _col.init()
|
19
189
|
this.cols[_key] = _col
|
20
190
|
const root = _col.tree.F.toObject(_col.tree.root).toString()
|
package/doc.js
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
const { pad, toSignal, encode, encodePath, encodeVal } = require("./encoder")
|
2
|
+
|
3
|
+
module.exports = class Doc {
|
4
|
+
constructor({ size = 16, size_json = 100 }) {
|
5
|
+
this.size = size
|
6
|
+
this.size_json = size_json
|
7
|
+
}
|
8
|
+
async getInputs({ json, path, val }) {
|
9
|
+
return {
|
10
|
+
json: pad(toSignal(encode(json)), this.size_json),
|
11
|
+
path: pad(toSignal(encodePath(path)), this.size),
|
12
|
+
val: pad(toSignal(encodeVal(val)), this.size),
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
package/index.js
CHANGED
package/json.js
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
const { pad, toSignal, encode, encodePath, encodeVal } = require("./encoder")
|
2
|
+
|
3
|
+
module.exports = class Json {
|
4
|
+
constructor({ size = 16, size_json = 100 }) {
|
5
|
+
this.size = size
|
6
|
+
this.size_json = size_json
|
7
|
+
}
|
8
|
+
async getInputs({ json, path, val }) {
|
9
|
+
return {
|
10
|
+
json: pad(toSignal(encode(json)), this.size_json),
|
11
|
+
path: pad(toSignal(encodePath(path)), this.size),
|
12
|
+
val: pad(toSignal(encodeVal(val)), this.size),
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
package/package.json
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "zkjson",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.4",
|
4
4
|
"description": "Zero Knowledge Provable JSON",
|
5
5
|
"main": "index.js",
|
6
6
|
"license": "MIT",
|
7
7
|
"dependencies": {
|
8
8
|
"blake-hash": "^2.0.0",
|
9
|
-
"blake2b": "^2.1.
|
10
|
-
"ethers": "^
|
11
|
-
"ffjavascript": "^0.2.
|
9
|
+
"blake2b": "^2.1.3",
|
10
|
+
"ethers": "^5.5.1",
|
11
|
+
"ffjavascript": "^0.2.45",
|
12
12
|
"ramda": "^0.29.1"
|
13
13
|
}
|
14
14
|
}
|