zkjson 0.1.16 → 0.1.18
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/contracts/ZKQuery.sol +8 -0
- package/encoder.js +108 -27
- package/package.json +1 -1
package/contracts/ZKQuery.sol
CHANGED
@@ -203,6 +203,13 @@ contract ZKQuery {
|
|
203
203
|
return true;
|
204
204
|
}
|
205
205
|
|
206
|
+
function _qCond (uint[] memory value, uint[] memory cond) internal pure returns (bool) {
|
207
|
+
uint[] memory _cond = toArr(cond);
|
208
|
+
require(value.length == _cond.length, "wrong query");
|
209
|
+
for(uint i = 0; i < value.length; i++) require(_cond[i] == value[i], "wrong query");
|
210
|
+
return true;
|
211
|
+
}
|
212
|
+
|
206
213
|
function getInt (uint[] memory path, uint[] memory raw) internal pure returns (int) {
|
207
214
|
uint[] memory value = getVal(path, raw);
|
208
215
|
return _qInt(value);
|
@@ -222,6 +229,7 @@ contract ZKQuery {
|
|
222
229
|
uint[] memory value = getVal(path, raw);
|
223
230
|
_qNull(value);
|
224
231
|
}
|
232
|
+
|
225
233
|
function verify(uint[] memory zkp, bytes4 selector, address addr) internal view returns (bool) {
|
226
234
|
uint size;
|
227
235
|
assembly {
|
package/encoder.js
CHANGED
@@ -395,7 +395,7 @@ function decode(arr) {
|
|
395
395
|
return json
|
396
396
|
}
|
397
397
|
|
398
|
-
const
|
398
|
+
const toIndex = str => {
|
399
399
|
return (
|
400
400
|
"1" +
|
401
401
|
str
|
@@ -405,7 +405,7 @@ const str2id = str => {
|
|
405
405
|
)
|
406
406
|
}
|
407
407
|
|
408
|
-
const
|
408
|
+
const fromIndex = id => {
|
409
409
|
let _id = id.toString().split("")
|
410
410
|
_id.shift()
|
411
411
|
return splitEvery(2, _id)
|
@@ -415,7 +415,7 @@ const id2str = id => {
|
|
415
415
|
.join("")
|
416
416
|
}
|
417
417
|
|
418
|
-
function
|
418
|
+
function toSignal(arr) {
|
419
419
|
const _arr = flatten(
|
420
420
|
arr.map(n => {
|
421
421
|
let str = splitEvery(8, n.toString().split(""))
|
@@ -428,30 +428,118 @@ function val2str(arr) {
|
|
428
428
|
return str
|
429
429
|
})
|
430
430
|
)
|
431
|
-
let
|
432
|
-
let
|
433
|
-
let
|
431
|
+
let _arr2 = []
|
432
|
+
let one = 0
|
433
|
+
let i = 0
|
434
|
+
let start = null
|
434
435
|
for (let v of _arr) {
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
436
|
+
_arr2.push(v)
|
437
|
+
if (v.length - 1 === 1) {
|
438
|
+
if (start === null) start = i
|
439
|
+
one += v.length - 1
|
440
|
+
if (one === 9) {
|
441
|
+
_arr2[start] = `0${one}${_arr2[start][1]}`
|
442
|
+
for (let i2 = start + 1; i2 <= i; i2++) _arr2[i2] = `${_arr2[i2][1]}`
|
443
|
+
one = 0
|
444
|
+
start = null
|
445
|
+
}
|
446
|
+
} else {
|
447
|
+
if (one > 2) {
|
448
|
+
_arr2[start] = `0${one}${_arr2[start][1]}`
|
449
|
+
for (let i2 = start + 1; i2 < i; i2++) _arr2[i2] = `${_arr2[i2][1]}`
|
450
|
+
}
|
451
|
+
one = 0
|
452
|
+
start = null
|
453
|
+
}
|
454
|
+
i++
|
455
|
+
}
|
456
|
+
if (one > 2) {
|
457
|
+
_arr2[start] = `0${one}${_arr2[start][1]}`
|
458
|
+
for (let i2 = start + 1; i2 <= i - 1; i2++) _arr2[i2] = `${_arr2[i2][1]}`
|
459
|
+
}
|
460
|
+
let _arr3 = []
|
461
|
+
let chain = null
|
462
|
+
let cur = 0
|
463
|
+
let num = ""
|
464
|
+
for (let v of _arr2) {
|
465
|
+
if (chain === null && +v[0] === 0) {
|
466
|
+
chain = +v[1]
|
467
|
+
cur = 1
|
468
|
+
num = v
|
469
|
+
} else if (chain !== null) {
|
470
|
+
num += v
|
471
|
+
cur++
|
472
|
+
if (chain == cur) {
|
473
|
+
_arr3.push(num)
|
474
|
+
chain = null
|
475
|
+
num = ""
|
476
|
+
cur = 0
|
477
|
+
}
|
478
|
+
} else {
|
479
|
+
_arr3.push(v)
|
439
480
|
}
|
440
|
-
len += v.length
|
441
|
-
str += v
|
442
481
|
}
|
443
|
-
if (
|
444
|
-
|
482
|
+
if (chain !== null) _arr3.push(num)
|
483
|
+
let arrs2 = []
|
484
|
+
let len2 = 0
|
485
|
+
let str2 = ""
|
486
|
+
for (let v of _arr3) {
|
487
|
+
if (len2 + v.length > 75) {
|
488
|
+
arrs2.push("1" + str2)
|
489
|
+
if (+v[0] === 0) {
|
490
|
+
let len3 = 75 - len2
|
491
|
+
if (len3 == 2 || len3 == 3) {
|
492
|
+
arrs2[arrs2.length - 1] += `1${v[2]}`
|
493
|
+
let new_len = +v[1] - 1
|
494
|
+
if (new_len === 2) {
|
495
|
+
v = `1${v[3]}1${v[4]}`
|
496
|
+
} else {
|
497
|
+
v = `0${new_len}${v.slice(3)}`
|
498
|
+
}
|
499
|
+
} else if (len3 > 3) {
|
500
|
+
let new_len = +v[1] - 2
|
501
|
+
let old_len = 2
|
502
|
+
if (len3 === 4) {
|
503
|
+
arrs2[arrs2.length - 1] += `1${v[2]}1${v[3]}`
|
504
|
+
} else {
|
505
|
+
old_len = len3 - 2
|
506
|
+
new_len = +v[1] - old_len
|
507
|
+
arrs2[arrs2.length - 1] += `0${old_len}${v.slice(2, 2 + old_len)}`
|
508
|
+
}
|
509
|
+
if (new_len === 1) {
|
510
|
+
v = `1${v[old_len + 2]}`
|
511
|
+
} else if (new_len === 2) {
|
512
|
+
v = `1${v[old_len + 2]}1${v[old_len + 3]}`
|
513
|
+
} else {
|
514
|
+
v = `0${new_len}${v.slice(old_len + 2)}`
|
515
|
+
}
|
516
|
+
}
|
517
|
+
}
|
518
|
+
len2 = 0
|
519
|
+
str2 = ""
|
520
|
+
}
|
521
|
+
len2 += v.length
|
522
|
+
str2 += v
|
523
|
+
}
|
524
|
+
if (str2 !== "") arrs2.push("1" + str2)
|
525
|
+
return arrs2
|
445
526
|
}
|
446
527
|
|
447
|
-
function
|
528
|
+
function fromSignal(arr) {
|
448
529
|
let _arr = []
|
449
530
|
let prev = ""
|
450
|
-
for (
|
531
|
+
for (let s of arr) {
|
532
|
+
s = s.slice(1)
|
451
533
|
let str = s.split("")
|
452
534
|
while (str.length > 0) {
|
453
535
|
const len = +str.shift()
|
454
|
-
if (len ===
|
536
|
+
if (len === 0) {
|
537
|
+
const len2 = +str.shift()
|
538
|
+
for (let i2 = 0; i2 < len2; i2++) {
|
539
|
+
_arr.push(+str[i2])
|
540
|
+
}
|
541
|
+
str = str.slice(len2)
|
542
|
+
} else if (len === 9) {
|
455
543
|
prev += str.slice(0, 8).join("")
|
456
544
|
str = str.slice(8)
|
457
545
|
} else {
|
@@ -465,13 +553,9 @@ function str2val(arr) {
|
|
465
553
|
return _arr
|
466
554
|
}
|
467
555
|
|
468
|
-
const toSignal = val2str
|
469
|
-
const fromSignal = str2val
|
470
|
-
const toIndex = str2id
|
471
|
-
const fromIndex = id2str
|
472
|
-
|
473
556
|
const path = p => toSignal(encodePath(p))
|
474
557
|
const val = v => toSignal(encodeVal(v))
|
558
|
+
const query = v => toSignal(encodeQuery(v))
|
475
559
|
|
476
560
|
function encodeQuery(v) {
|
477
561
|
if (!Array.isArray(v)) throw Error("query must be an array")
|
@@ -483,7 +567,7 @@ function encodeQuery(v) {
|
|
483
567
|
function decodeQuery(v) {
|
484
568
|
const op = opMap[v[0]]
|
485
569
|
if (isNil(op)) throw Error("op doens't exist")
|
486
|
-
return [op,
|
570
|
+
return [op, decodeVal(v.slice(1))]
|
487
571
|
}
|
488
572
|
|
489
573
|
module.exports = {
|
@@ -496,16 +580,13 @@ module.exports = {
|
|
496
580
|
pad,
|
497
581
|
_encode,
|
498
582
|
flattenPath,
|
499
|
-
str2id,
|
500
|
-
val2str,
|
501
|
-
str2val,
|
502
|
-
id2str,
|
503
583
|
toSignal,
|
504
584
|
fromSignal,
|
505
585
|
toIndex,
|
506
586
|
fromIndex,
|
507
587
|
path,
|
508
588
|
val,
|
589
|
+
query,
|
509
590
|
encodeQuery,
|
510
591
|
decodeQuery,
|
511
592
|
}
|