zkjson 0.1.25 → 0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- package/contracts/ZKIPFS.sol +96 -0
- package/package.json +1 -1
- package/uint.js +1 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
2
|
+
|
3
|
+
pragma solidity >=0.7.0 <0.9.0;
|
4
|
+
import "./ZKQuery.sol";
|
5
|
+
import "hardhat/console.sol";
|
6
|
+
|
7
|
+
interface VerifierIPFS {
|
8
|
+
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[43] calldata _pubSignals) external view returns (bool);
|
9
|
+
}
|
10
|
+
|
11
|
+
contract ZKIPFS is ZKQuery{
|
12
|
+
uint constant SIZE_PATH = 5;
|
13
|
+
uint constant SIZE_VAL = 5;
|
14
|
+
address public verifierIPFS;
|
15
|
+
bytes constant ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
16
|
+
|
17
|
+
function charArrayToString(bytes1[46] memory charArray) public pure returns (string memory) {
|
18
|
+
bytes memory byteArray = new bytes(charArray.length);
|
19
|
+
for(uint i = 0; i < charArray.length; i++) {
|
20
|
+
byteArray[i] = charArray[i];
|
21
|
+
}
|
22
|
+
return string(byteArray);
|
23
|
+
}
|
24
|
+
|
25
|
+
function concat(string memory a, string memory b) public pure returns (string memory) {
|
26
|
+
return string(abi.encodePacked(a, b));
|
27
|
+
}
|
28
|
+
|
29
|
+
function uriEqual(string memory a, string memory b) public pure returns (bool) {
|
30
|
+
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
|
31
|
+
}
|
32
|
+
|
33
|
+
function toCID(uint[34] memory source) public pure returns (string memory) {
|
34
|
+
uint zeroes = 0;
|
35
|
+
uint length = 0;
|
36
|
+
uint pbegin = 0;
|
37
|
+
uint pend = source.length;
|
38
|
+
while (pbegin != pend && source[pbegin] == 0) {
|
39
|
+
pbegin++;
|
40
|
+
zeroes++;
|
41
|
+
}
|
42
|
+
uint size = 46;
|
43
|
+
uint[46] memory b58;
|
44
|
+
while (pbegin != pend) {
|
45
|
+
uint carry = source[pbegin];
|
46
|
+
uint i = 0;
|
47
|
+
for ( uint it1 = size - 1; (carry != 0 || i < length); it1-- ) {
|
48
|
+
carry += (256 * b58[it1]);
|
49
|
+
b58[it1] = carry % 58;
|
50
|
+
carry = (carry / 58);
|
51
|
+
i++;
|
52
|
+
if(it1 == 0) break;
|
53
|
+
}
|
54
|
+
length = i;
|
55
|
+
pbegin++;
|
56
|
+
}
|
57
|
+
uint it2 = size - length;
|
58
|
+
while (it2 != size && b58[it2] == 0) it2++;
|
59
|
+
bytes1[46] memory str;
|
60
|
+
uint i = 0;
|
61
|
+
for (; it2 < size; ++it2){
|
62
|
+
str[i] = ALPHABET[b58[it2]];
|
63
|
+
i++;
|
64
|
+
}
|
65
|
+
return charArrayToString(str);
|
66
|
+
}
|
67
|
+
|
68
|
+
function ipfsURI(uint[34] memory source) public pure returns (string memory) {
|
69
|
+
return concat("ipfs://", toCID(source));
|
70
|
+
}
|
71
|
+
|
72
|
+
function _validateQueryIPFS(uint[] memory path, uint[] memory zkp, uint size_path, uint size_val) internal pure returns(uint[] memory){
|
73
|
+
require(zkp[8] == 1, "value doesn't exist");
|
74
|
+
uint len = 41;
|
75
|
+
for(uint i = len; i < len + size_path; i++){
|
76
|
+
require((path.length <= i - len && zkp[i] == 0) || path[i - len] == zkp[i], "wrong path");
|
77
|
+
}
|
78
|
+
uint[] memory value = new uint[](size_val);
|
79
|
+
for(uint i = len + size_path; i < len + size_path + size_val; i++){
|
80
|
+
value[i - (len + size_val)] = zkp[i];
|
81
|
+
}
|
82
|
+
return toArr(value);
|
83
|
+
}
|
84
|
+
|
85
|
+
function validateQuery(string memory URI, uint[] memory path, uint[] memory zkp) internal pure returns(uint[] memory){
|
86
|
+
uint[34] memory hash;
|
87
|
+
hash[0] = 18;
|
88
|
+
hash[1] = 32;
|
89
|
+
for(uint i = 9; i < 41; i++) hash[i - 7] = zkp[i];
|
90
|
+
string memory CID = ipfsURI(hash);
|
91
|
+
require(uriEqual(CID,URI), "wrong CID");
|
92
|
+
verify(zkp, VerifierIPFS.verifyProof.selector, verifierIPFS);
|
93
|
+
return _validateQueryIPFS(path, zkp, SIZE_PATH, SIZE_VAL);
|
94
|
+
}
|
95
|
+
|
96
|
+
}
|
package/package.json
CHANGED