@reclaimprotocol/attestor-core 5.0.1-beta.2 → 5.0.1-beta.21
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/browser/resources/attestor-browser.min.mjs +4512 -0
- package/lib/external-rpc/index.js +10399 -3
- package/lib/index.js +8323 -10
- package/package.json +9 -4
- package/lib/avs/abis/avsDirectoryABI.js +0 -343
- package/lib/avs/abis/delegationABI.js +0 -4
- package/lib/avs/abis/registryABI.js +0 -728
- package/lib/avs/client/create-claim-on-avs.js +0 -168
- package/lib/avs/config.js +0 -26
- package/lib/avs/contracts/ReclaimServiceManager.js +0 -0
- package/lib/avs/contracts/common.js +0 -0
- package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +0 -1183
- package/lib/avs/contracts/factories/index.js +0 -4
- package/lib/avs/contracts/index.js +0 -6
- package/lib/avs/types/index.js +0 -0
- package/lib/avs/utils/contracts.js +0 -53
- package/lib/avs/utils/register.js +0 -74
- package/lib/avs/utils/tasks.js +0 -48
- package/lib/client/create-claim.js +0 -461
- package/lib/client/index.js +0 -3
- package/lib/client/tunnels/make-rpc-tcp-tunnel.js +0 -53
- package/lib/client/tunnels/make-rpc-tls-tunnel.js +0 -127
- package/lib/client/utils/attestor-pool.js +0 -24
- package/lib/client/utils/client-socket.js +0 -120
- package/lib/client/utils/message-handler.js +0 -97
- package/lib/config/index.js +0 -62
- package/lib/external-rpc/benchmark.js +0 -82
- package/lib/external-rpc/event-bus.js +0 -17
- package/lib/external-rpc/global.d.js +0 -0
- package/lib/external-rpc/handle-incoming-msg.js +0 -241
- package/lib/external-rpc/jsc-polyfills/1.js +0 -80
- package/lib/external-rpc/jsc-polyfills/2.js +0 -15
- package/lib/external-rpc/jsc-polyfills/event.js +0 -19
- package/lib/external-rpc/jsc-polyfills/index.js +0 -2
- package/lib/external-rpc/jsc-polyfills/ws.js +0 -83
- package/lib/external-rpc/setup-browser.js +0 -33
- package/lib/external-rpc/setup-jsc.js +0 -22
- package/lib/external-rpc/types.js +0 -0
- package/lib/external-rpc/utils.js +0 -100
- package/lib/external-rpc/zk.js +0 -58
- package/lib/mechain/abis/governanceABI.js +0 -461
- package/lib/mechain/abis/taskABI.js +0 -512
- package/lib/mechain/client/create-claim-on-mechain.js +0 -33
- package/lib/mechain/client/index.js +0 -1
- package/lib/mechain/constants/index.js +0 -8
- package/lib/mechain/index.js +0 -2
- package/lib/mechain/types/index.js +0 -0
- package/lib/proto/api.js +0 -4250
- package/lib/proto/tee-bundle.js +0 -1296
- package/lib/providers/http/index.js +0 -640
- package/lib/providers/http/patch-parse5-tree.js +0 -34
- package/lib/providers/http/utils.js +0 -283
- package/lib/providers/index.js +0 -7
- package/lib/scripts/check-avs-registration.js +0 -28
- package/lib/scripts/fallbacks/crypto.js +0 -4
- package/lib/scripts/fallbacks/empty.js +0 -4
- package/lib/scripts/fallbacks/re2.js +0 -7
- package/lib/scripts/fallbacks/snarkjs.js +0 -10
- package/lib/scripts/fallbacks/stwo.js +0 -159
- package/lib/scripts/generate-provider-types.js +0 -101
- package/lib/scripts/generate-receipt.js +0 -101
- package/lib/scripts/generate-toprf-keys.js +0 -24
- package/lib/scripts/jsc-cli-rpc.js +0 -35
- package/lib/scripts/register-avs-operator.js +0 -3
- package/lib/scripts/start-server.js +0 -11
- package/lib/scripts/update-avs-metadata.js +0 -20
- package/lib/scripts/utils.js +0 -10
- package/lib/scripts/whitelist-operator.js +0 -16
- package/lib/server/create-server.js +0 -105
- package/lib/server/handlers/claimTeeBundle.js +0 -232
- package/lib/server/handlers/claimTunnel.js +0 -80
- package/lib/server/handlers/completeClaimOnChain.js +0 -29
- package/lib/server/handlers/createClaimOnChain.js +0 -32
- package/lib/server/handlers/createTaskOnMechain.js +0 -57
- package/lib/server/handlers/createTunnel.js +0 -98
- package/lib/server/handlers/disconnectTunnel.js +0 -8
- package/lib/server/handlers/fetchCertificateBytes.js +0 -57
- package/lib/server/handlers/index.js +0 -25
- package/lib/server/handlers/init.js +0 -33
- package/lib/server/handlers/toprf.js +0 -19
- package/lib/server/index.js +0 -4
- package/lib/server/socket.js +0 -112
- package/lib/server/tunnels/make-tcp-tunnel.js +0 -202
- package/lib/server/utils/apm.js +0 -29
- package/lib/server/utils/assert-valid-claim-request.js +0 -354
- package/lib/server/utils/config-env.js +0 -4
- package/lib/server/utils/dns.js +0 -24
- package/lib/server/utils/gcp-attestation.js +0 -237
- package/lib/server/utils/generics.js +0 -45
- package/lib/server/utils/iso.js +0 -259
- package/lib/server/utils/keep-alive.js +0 -38
- package/lib/server/utils/nitro-attestation.js +0 -249
- package/lib/server/utils/oprf-raw.js +0 -61
- package/lib/server/utils/process-handshake.js +0 -233
- package/lib/server/utils/proxy-session.js +0 -6
- package/lib/server/utils/tee-oprf-mpc-verification.js +0 -86
- package/lib/server/utils/tee-oprf-verification.js +0 -151
- package/lib/server/utils/tee-transcript-reconstruction.js +0 -140
- package/lib/server/utils/tee-verification.js +0 -358
- package/lib/server/utils/validation.js +0 -45
- package/lib/types/bgp.js +0 -0
- package/lib/types/claims.js +0 -0
- package/lib/types/client.js +0 -0
- package/lib/types/general.js +0 -0
- package/lib/types/handlers.js +0 -0
- package/lib/types/index.js +0 -10
- package/lib/types/providers.gen.js +0 -16
- package/lib/types/providers.js +0 -0
- package/lib/types/rpc.js +0 -0
- package/lib/types/signatures.js +0 -0
- package/lib/types/tunnel.js +0 -0
- package/lib/types/zk.js +0 -0
- package/lib/utils/auth.js +0 -71
- package/lib/utils/b64-json.js +0 -17
- package/lib/utils/bgp-listener.js +0 -123
- package/lib/utils/claims.js +0 -89
- package/lib/utils/env.js +0 -19
- package/lib/utils/error.js +0 -54
- package/lib/utils/generics.js +0 -268
- package/lib/utils/http-parser.js +0 -201
- package/lib/utils/index.js +0 -13
- package/lib/utils/logger.js +0 -82
- package/lib/utils/prepare-packets.js +0 -69
- package/lib/utils/redactions.js +0 -135
- package/lib/utils/retries.js +0 -26
- package/lib/utils/signatures/eth.js +0 -31
- package/lib/utils/signatures/index.js +0 -12
- package/lib/utils/socket-base.js +0 -96
- package/lib/utils/tls.js +0 -58
- package/lib/utils/ws.js +0 -22
- package/lib/utils/zk.js +0 -625
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import "../../providers/http/patch-parse5-tree.js";
|
|
2
|
-
import { concatenateUint8Arrays } from "@reclaimprotocol/tls";
|
|
3
|
-
import {
|
|
4
|
-
ArrayExpression,
|
|
5
|
-
ExpressionStatement,
|
|
6
|
-
ObjectExpression,
|
|
7
|
-
parseScript,
|
|
8
|
-
Property,
|
|
9
|
-
Syntax
|
|
10
|
-
} from "esprima-next";
|
|
11
|
-
import { JSONPath } from "jsonpath-plus";
|
|
12
|
-
import { parse } from "parse5";
|
|
13
|
-
import { adapter as htmlAdapter } from "parse5-htmlparser2-tree-adapter";
|
|
14
|
-
import RE2 from "re2";
|
|
15
|
-
import xpath from "xpath";
|
|
16
|
-
import { getHttpRequestDataFromTranscript, isApplicationData, makeHttpResponseParser, REDACTION_CHAR_CODE } from "../../utils/index.js";
|
|
17
|
-
function extractHTMLElement(html, xpathExpression, contentsOnly) {
|
|
18
|
-
const { start, end } = extractHTMLElementIndex(html, xpathExpression, contentsOnly);
|
|
19
|
-
return html.slice(start, end);
|
|
20
|
-
}
|
|
21
|
-
function extractHTMLElements(html, xpathExpression, contentsOnly) {
|
|
22
|
-
const indexes = extractHTMLElementsIndexes(html, xpathExpression, contentsOnly);
|
|
23
|
-
const res = [];
|
|
24
|
-
for (const { start, end } of indexes) {
|
|
25
|
-
res.push(html.slice(start, end));
|
|
26
|
-
}
|
|
27
|
-
return res;
|
|
28
|
-
}
|
|
29
|
-
function extractHTMLElementIndex(html, xpathExpression, contentsOnly) {
|
|
30
|
-
return extractHTMLElementsIndexes(html, xpathExpression, contentsOnly)[0];
|
|
31
|
-
}
|
|
32
|
-
function extractHTMLElementsIndexes(html, xpathExpression, contentsOnly) {
|
|
33
|
-
return extractHTMLElementIndexesParse5(html, xpathExpression, contentsOnly);
|
|
34
|
-
}
|
|
35
|
-
function extractHTMLElementIndexesParse5(html, xpathExpression, contentsOnly) {
|
|
36
|
-
const domLight = parse(
|
|
37
|
-
html,
|
|
38
|
-
{ treeAdapter: htmlAdapter, sourceCodeLocationInfo: true }
|
|
39
|
-
);
|
|
40
|
-
domLight["name"] = "root";
|
|
41
|
-
const parsedPath = xpath.parse(xpathExpression);
|
|
42
|
-
const nodes = parsedPath.select({
|
|
43
|
-
node: domLight,
|
|
44
|
-
allowAnyNamespaceForNoPrefix: true
|
|
45
|
-
});
|
|
46
|
-
if (!nodes.length) {
|
|
47
|
-
throw new Error(`Failed to find XPath: "${xpathExpression}"`);
|
|
48
|
-
}
|
|
49
|
-
return nodes.map((node) => getNodeRange(node, contentsOnly));
|
|
50
|
-
}
|
|
51
|
-
function getNodeRange(node, contentsOnly) {
|
|
52
|
-
if (!contentsOnly) {
|
|
53
|
-
return { start: node.startIndex, end: node.endIndex };
|
|
54
|
-
}
|
|
55
|
-
if (!("firstChild" in node) || !node.firstChild) {
|
|
56
|
-
throw new Error(`Node "${node["name"]}" has no children`);
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
start: node.firstChild.startIndex,
|
|
60
|
-
end: node.lastChild.endIndex
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function extractJSONValueIndex(json, jsonPath) {
|
|
64
|
-
return extractJSONValueIndexes(json, jsonPath)[0];
|
|
65
|
-
}
|
|
66
|
-
function extractJSONValueIndexes(json, jsonPath) {
|
|
67
|
-
const pointers = JSONPath({
|
|
68
|
-
path: jsonPath,
|
|
69
|
-
json: JSON.parse(json),
|
|
70
|
-
wrap: false,
|
|
71
|
-
resultType: "pointer",
|
|
72
|
-
eval: "safe",
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
ignoreEvalErrors: true
|
|
75
|
-
});
|
|
76
|
-
if (!pointers) {
|
|
77
|
-
throw new Error("jsonPath not found");
|
|
78
|
-
}
|
|
79
|
-
const tree = parseScript("(" + json + ")", { range: true });
|
|
80
|
-
if (tree.body[0] instanceof ExpressionStatement && (tree.body[0].expression instanceof ObjectExpression || tree.body[0].expression instanceof ArrayExpression)) {
|
|
81
|
-
const traversePointers = Array.isArray(pointers) ? pointers : [pointers];
|
|
82
|
-
const res = [];
|
|
83
|
-
for (const pointer of traversePointers) {
|
|
84
|
-
const index = traverse(tree.body[0].expression, "", [pointer]);
|
|
85
|
-
if (index) {
|
|
86
|
-
res.push({
|
|
87
|
-
start: index.start - 1,
|
|
88
|
-
//account for '('
|
|
89
|
-
end: index.end - 1
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return res;
|
|
94
|
-
}
|
|
95
|
-
throw new Error("jsonPath not found");
|
|
96
|
-
}
|
|
97
|
-
function traverse(o, path, pointers) {
|
|
98
|
-
if (o instanceof ObjectExpression) {
|
|
99
|
-
for (const p of o.properties) {
|
|
100
|
-
if (!(p instanceof Property)) {
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
const localPath = p.key.type === Syntax.Literal ? path + "/" + p.key.value : path;
|
|
104
|
-
if (pointers.includes(localPath) && "range" in p && Array.isArray(p.range)) {
|
|
105
|
-
return {
|
|
106
|
-
start: p.range[0],
|
|
107
|
-
end: p.range[1]
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
if (p.value instanceof ObjectExpression || p.value instanceof ArrayExpression) {
|
|
111
|
-
const res = traverse(p.value, localPath, pointers);
|
|
112
|
-
if (res) {
|
|
113
|
-
return res;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (o instanceof ArrayExpression) {
|
|
119
|
-
for (let i = 0; i < o.elements.length; i++) {
|
|
120
|
-
const element = o.elements[i];
|
|
121
|
-
if (!element) {
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
const localPath = path + "/" + i;
|
|
125
|
-
if (pointers.includes(localPath) && "range" in element && Array.isArray(element.range)) {
|
|
126
|
-
return {
|
|
127
|
-
start: element.range[0],
|
|
128
|
-
end: element.range[1]
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
if (element instanceof ObjectExpression) {
|
|
132
|
-
const res = traverse(element, localPath, pointers);
|
|
133
|
-
if (res) {
|
|
134
|
-
return res;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (element instanceof ArrayExpression) {
|
|
138
|
-
const res = traverse(element, localPath, pointers);
|
|
139
|
-
if (res) {
|
|
140
|
-
return res;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
function buildHeaders(input) {
|
|
148
|
-
const headers = [];
|
|
149
|
-
for (const [key, value] of Object.entries(input || {})) {
|
|
150
|
-
headers.push(`${key}: ${value}`);
|
|
151
|
-
}
|
|
152
|
-
return headers;
|
|
153
|
-
}
|
|
154
|
-
function convertResponsePosToAbsolutePos(pos, bodyStartIdx, chunks) {
|
|
155
|
-
if (chunks?.length) {
|
|
156
|
-
let chunkBodyStart = 0;
|
|
157
|
-
for (const chunk of chunks) {
|
|
158
|
-
const chunkSize = chunk.toIndex - chunk.fromIndex;
|
|
159
|
-
if (pos >= chunkBodyStart && pos <= chunkBodyStart + chunkSize) {
|
|
160
|
-
return pos - chunkBodyStart + chunk.fromIndex;
|
|
161
|
-
}
|
|
162
|
-
chunkBodyStart += chunkSize;
|
|
163
|
-
}
|
|
164
|
-
throw new Error("position out of range");
|
|
165
|
-
}
|
|
166
|
-
return bodyStartIdx + pos;
|
|
167
|
-
}
|
|
168
|
-
function getRedactionsForChunkHeaders(from, to, chunks) {
|
|
169
|
-
const res = [];
|
|
170
|
-
if (!chunks?.length) {
|
|
171
|
-
return res;
|
|
172
|
-
}
|
|
173
|
-
for (let i = 1; i < chunks?.length; i++) {
|
|
174
|
-
const chunk = chunks[i];
|
|
175
|
-
if (chunk.fromIndex > from && chunk.fromIndex < to) {
|
|
176
|
-
res.push({
|
|
177
|
-
fromIndex: chunks[i - 1].toIndex,
|
|
178
|
-
toIndex: chunk.fromIndex
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return res;
|
|
183
|
-
}
|
|
184
|
-
function parseHttpResponse(buff) {
|
|
185
|
-
const parser = makeHttpResponseParser();
|
|
186
|
-
parser.onChunk(buff);
|
|
187
|
-
parser.streamEnded();
|
|
188
|
-
return parser.res;
|
|
189
|
-
}
|
|
190
|
-
function makeRegex(str) {
|
|
191
|
-
return RE2(str, "sgiu");
|
|
192
|
-
}
|
|
193
|
-
const TEMPLATE_START_CHARCODE = "{".charCodeAt(0);
|
|
194
|
-
const TEMPLATE_END_CHARCODE = "}".charCodeAt(0);
|
|
195
|
-
function matchRedactedStrings(templateString, redactedString) {
|
|
196
|
-
if (templateString.length === 0 && redactedString?.length === 0) {
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
|
-
if (!redactedString) {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
let ts = -1;
|
|
203
|
-
let rs = -1;
|
|
204
|
-
while (ts < templateString.length && rs < redactedString.length) {
|
|
205
|
-
let ct = getTChar();
|
|
206
|
-
let cr = getRChar();
|
|
207
|
-
if (ct !== cr) {
|
|
208
|
-
if (ct === TEMPLATE_START_CHARCODE && cr === REDACTION_CHAR_CODE) {
|
|
209
|
-
if (getTChar() !== TEMPLATE_START_CHARCODE) {
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
while ((ct = getTChar()) !== TEMPLATE_END_CHARCODE && ct !== -1) {
|
|
213
|
-
}
|
|
214
|
-
while ((ct = getTChar()) !== TEMPLATE_END_CHARCODE && ct !== -1) {
|
|
215
|
-
}
|
|
216
|
-
if (ct === -1) {
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
while ((cr = getRChar()) === REDACTION_CHAR_CODE && cr !== -1) {
|
|
220
|
-
}
|
|
221
|
-
if (cr === -1) {
|
|
222
|
-
return getTChar() === -1;
|
|
223
|
-
}
|
|
224
|
-
rs--;
|
|
225
|
-
} else {
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
function getTChar() {
|
|
231
|
-
ts++;
|
|
232
|
-
if (ts < templateString.length) {
|
|
233
|
-
return templateString[ts];
|
|
234
|
-
} else {
|
|
235
|
-
return -1;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
function getRChar() {
|
|
239
|
-
if (!redactedString) {
|
|
240
|
-
return -1;
|
|
241
|
-
}
|
|
242
|
-
rs++;
|
|
243
|
-
if (rs < redactedString.length) {
|
|
244
|
-
return redactedString[rs];
|
|
245
|
-
} else {
|
|
246
|
-
return -1;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
return ts === templateString.length && rs === redactedString.length;
|
|
250
|
-
}
|
|
251
|
-
function generateRequstAndResponseFromTranscript(transcript, tlsVersion) {
|
|
252
|
-
const allPackets = transcript;
|
|
253
|
-
const packets = [];
|
|
254
|
-
for (const b of allPackets) {
|
|
255
|
-
if (b.message.type !== "ciphertext" || !isApplicationData(b.message, tlsVersion)) {
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
const plaintext = tlsVersion === "TLS1_3" ? b.message.plaintext.slice(0, -1) : b.message.plaintext;
|
|
259
|
-
packets.push({
|
|
260
|
-
message: plaintext,
|
|
261
|
-
sender: b.sender
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
const req = getHttpRequestDataFromTranscript(packets);
|
|
265
|
-
const responsePackets = concatenateUint8Arrays(packets.filter((p) => p.sender === "server").map((p) => p.message).filter((b) => !b.every((b2) => b2 === REDACTION_CHAR_CODE)));
|
|
266
|
-
const res = parseHttpResponse(responsePackets);
|
|
267
|
-
return { req, res };
|
|
268
|
-
}
|
|
269
|
-
export {
|
|
270
|
-
buildHeaders,
|
|
271
|
-
convertResponsePosToAbsolutePos,
|
|
272
|
-
extractHTMLElement,
|
|
273
|
-
extractHTMLElementIndex,
|
|
274
|
-
extractHTMLElements,
|
|
275
|
-
extractHTMLElementsIndexes,
|
|
276
|
-
extractJSONValueIndex,
|
|
277
|
-
extractJSONValueIndexes,
|
|
278
|
-
generateRequstAndResponseFromTranscript,
|
|
279
|
-
getRedactionsForChunkHeaders,
|
|
280
|
-
makeRegex,
|
|
281
|
-
matchRedactedStrings,
|
|
282
|
-
parseHttpResponse
|
|
283
|
-
};
|
package/lib/providers/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import "src/server/utils/config-env";
|
|
2
|
-
import { getContracts } from "../avs/utils/contracts.js";
|
|
3
|
-
async function main() {
|
|
4
|
-
const { wallet, contract } = getContracts();
|
|
5
|
-
const meta = await contract.taskCreationMetadata();
|
|
6
|
-
console.log(
|
|
7
|
-
"Metadata parameters:",
|
|
8
|
-
`maxTaskCreationDelayS: ${meta.maxTaskCreationDelayS}`,
|
|
9
|
-
`minSignaturesPerTask: ${meta.minSignaturesPerTask}`,
|
|
10
|
-
`maxTaskLifetimeS: ${meta.maxTaskLifetimeS}`
|
|
11
|
-
);
|
|
12
|
-
console.log(`Checking registration for operator ${wallet.address}`);
|
|
13
|
-
const operatorAddr = wallet.address;
|
|
14
|
-
const metadata = await contract.getMetadataForOperator(operatorAddr).catch((err) => {
|
|
15
|
-
if (err.message.includes("Operator not found")) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
throw err;
|
|
19
|
-
});
|
|
20
|
-
if (!metadata) {
|
|
21
|
-
console.log("Operator not registered");
|
|
22
|
-
const isWhitelisted = await contract.isOperatorWhitelisted(operatorAddr);
|
|
23
|
-
console.log(`Is whitelisted: ${isWhitelisted}`);
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
console.log("Operator registered, URL:", metadata.url);
|
|
27
|
-
}
|
|
28
|
-
void main();
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
const Base64 = {
|
|
2
|
-
fromUint8Array(arr) {
|
|
3
|
-
let binary = "";
|
|
4
|
-
for (const element of arr) {
|
|
5
|
-
binary += String.fromCharCode(element);
|
|
6
|
-
}
|
|
7
|
-
return btoa(binary);
|
|
8
|
-
},
|
|
9
|
-
toUint8Array(str) {
|
|
10
|
-
const binary = atob(str);
|
|
11
|
-
const arr = new Uint8Array(binary.length);
|
|
12
|
-
for (let i = 0; i < binary.length; i++) {
|
|
13
|
-
arr[i] = binary.charCodeAt(i);
|
|
14
|
-
}
|
|
15
|
-
return arr;
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
function getS2Circuits() {
|
|
19
|
-
const s2 = window["s2circuits"];
|
|
20
|
-
if (!s2) {
|
|
21
|
-
throw new Error("s2circuits not loaded. Make sure s2circuits.js is loaded before using stwo.");
|
|
22
|
-
}
|
|
23
|
-
return s2;
|
|
24
|
-
}
|
|
25
|
-
function assertU32Counter(counter) {
|
|
26
|
-
if (!Number.isInteger(counter) || counter < 0 || counter > 4294967295) {
|
|
27
|
-
throw new RangeError("counter must be a uint32 integer (0 to 4294967295)");
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
let wasmInitialized = false;
|
|
31
|
-
let initPromise;
|
|
32
|
-
async function ensureWasmInitialized(fetcher, logger) {
|
|
33
|
-
if (wasmInitialized) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (initPromise) {
|
|
37
|
-
return initPromise;
|
|
38
|
-
}
|
|
39
|
-
initPromise = (async () => {
|
|
40
|
-
try {
|
|
41
|
-
const s2 = getS2Circuits();
|
|
42
|
-
const wasmBytes = await fetcher.fetch("stwo", "s2circuits_bg.wasm", logger);
|
|
43
|
-
s2.initSync({ module: wasmBytes });
|
|
44
|
-
wasmInitialized = true;
|
|
45
|
-
} catch (err) {
|
|
46
|
-
initPromise = void 0;
|
|
47
|
-
throw err;
|
|
48
|
-
}
|
|
49
|
-
})();
|
|
50
|
-
return initPromise;
|
|
51
|
-
}
|
|
52
|
-
function serializeWitness(algorithm, input) {
|
|
53
|
-
if (!input.noncesAndCounters?.length) {
|
|
54
|
-
throw new Error("noncesAndCounters must be a non-empty array");
|
|
55
|
-
}
|
|
56
|
-
const { noncesAndCounters: [{ nonce, counter }] } = input;
|
|
57
|
-
assertU32Counter(counter);
|
|
58
|
-
const data = {
|
|
59
|
-
algorithm,
|
|
60
|
-
key: Base64.fromUint8Array(input.key),
|
|
61
|
-
nonce: Base64.fromUint8Array(nonce),
|
|
62
|
-
counter,
|
|
63
|
-
plaintext: Base64.fromUint8Array(input.out),
|
|
64
|
-
// out = decrypted plaintext
|
|
65
|
-
ciphertext: Base64.fromUint8Array(input.in)
|
|
66
|
-
// in = encrypted ciphertext
|
|
67
|
-
};
|
|
68
|
-
return new TextEncoder().encode(JSON.stringify(data));
|
|
69
|
-
}
|
|
70
|
-
function deserializeWitness(witness) {
|
|
71
|
-
const json = new TextDecoder().decode(witness);
|
|
72
|
-
return JSON.parse(json);
|
|
73
|
-
}
|
|
74
|
-
function makeStwoZkOperator({
|
|
75
|
-
algorithm,
|
|
76
|
-
fetcher
|
|
77
|
-
}) {
|
|
78
|
-
return {
|
|
79
|
-
generateWitness(input) {
|
|
80
|
-
return serializeWitness(algorithm, input);
|
|
81
|
-
},
|
|
82
|
-
async groth16Prove(witness, logger) {
|
|
83
|
-
await ensureWasmInitialized(fetcher, logger);
|
|
84
|
-
const s2 = getS2Circuits();
|
|
85
|
-
const data = deserializeWitness(witness);
|
|
86
|
-
const key = Base64.toUint8Array(data.key);
|
|
87
|
-
const nonce = Base64.toUint8Array(data.nonce);
|
|
88
|
-
const plaintext = Base64.toUint8Array(data.plaintext);
|
|
89
|
-
const ciphertext = Base64.toUint8Array(data.ciphertext);
|
|
90
|
-
let resultJson;
|
|
91
|
-
switch (data.algorithm) {
|
|
92
|
-
case "chacha20":
|
|
93
|
-
resultJson = s2.generate_chacha20_proof(key, nonce, data.counter, plaintext, ciphertext);
|
|
94
|
-
break;
|
|
95
|
-
case "aes-128-ctr":
|
|
96
|
-
resultJson = s2.generate_aes128_ctr_proof(key, nonce, data.counter, plaintext, ciphertext);
|
|
97
|
-
break;
|
|
98
|
-
case "aes-256-ctr":
|
|
99
|
-
resultJson = s2.generate_aes256_ctr_proof(key, nonce, data.counter, plaintext, ciphertext);
|
|
100
|
-
break;
|
|
101
|
-
default:
|
|
102
|
-
throw new Error(`Unsupported algorithm: ${data.algorithm}`);
|
|
103
|
-
}
|
|
104
|
-
const result = JSON.parse(resultJson);
|
|
105
|
-
if (result.error) {
|
|
106
|
-
throw new Error(`Stwo proof generation failed: ${result.error}`);
|
|
107
|
-
}
|
|
108
|
-
if (!result.proof) {
|
|
109
|
-
throw new Error("Stwo proof generation failed: no proof returned");
|
|
110
|
-
}
|
|
111
|
-
return { proof: result.proof };
|
|
112
|
-
},
|
|
113
|
-
async groth16Verify(publicSignals, proof, logger) {
|
|
114
|
-
await ensureWasmInitialized(fetcher, logger);
|
|
115
|
-
const s2 = getS2Circuits();
|
|
116
|
-
const expectedNonce = publicSignals.noncesAndCounters[0]?.nonce;
|
|
117
|
-
const expectedCounter = publicSignals.noncesAndCounters[0]?.counter;
|
|
118
|
-
const expectedCiphertext = publicSignals.in;
|
|
119
|
-
const expectedPlaintext = publicSignals.out;
|
|
120
|
-
if (!expectedNonce || expectedCounter === void 0) {
|
|
121
|
-
logger?.warn("Invalid publicSignals: missing nonce or counter");
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
assertU32Counter(expectedCounter);
|
|
125
|
-
const proofStr = typeof proof === "string" ? proof : new TextDecoder().decode(proof);
|
|
126
|
-
let resultJson;
|
|
127
|
-
if (algorithm === "chacha20") {
|
|
128
|
-
resultJson = s2.verify_chacha20_proof(
|
|
129
|
-
proofStr,
|
|
130
|
-
expectedNonce,
|
|
131
|
-
expectedCounter,
|
|
132
|
-
expectedPlaintext,
|
|
133
|
-
expectedCiphertext
|
|
134
|
-
);
|
|
135
|
-
} else {
|
|
136
|
-
resultJson = s2.verify_aes_ctr_proof(
|
|
137
|
-
proofStr,
|
|
138
|
-
expectedNonce,
|
|
139
|
-
expectedCounter,
|
|
140
|
-
expectedPlaintext,
|
|
141
|
-
expectedCiphertext
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
const result = JSON.parse(resultJson);
|
|
145
|
-
if (result.error) {
|
|
146
|
-
logger?.warn({ error: result.error }, "Stwo STARK verification failed");
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
return result.valid === true;
|
|
150
|
-
},
|
|
151
|
-
release() {
|
|
152
|
-
wasmInitialized = false;
|
|
153
|
-
initPromise = void 0;
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
export {
|
|
158
|
-
makeStwoZkOperator
|
|
159
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { readdir, readFile, writeFile } from "fs/promises";
|
|
2
|
-
import { compile } from "json-schema-to-typescript";
|
|
3
|
-
import { parse } from "yaml";
|
|
4
|
-
const PROVIDER_SCHEMAS_PATH = "./provider-schemas";
|
|
5
|
-
const GEN_TS_FILENAME = "./src/types/providers.gen.ts";
|
|
6
|
-
const BinaryDataType = "BinaryData";
|
|
7
|
-
async function main() {
|
|
8
|
-
const folders = await findAllProviderFolders();
|
|
9
|
-
console.log(`Generating for ${folders.length} provider folders`);
|
|
10
|
-
let ts = "/* eslint-disable */\n/* Generated file. Do not edit */";
|
|
11
|
-
ts += `
|
|
12
|
-
|
|
13
|
-
type ${BinaryDataType} = Uint8Array | string
|
|
14
|
-
`;
|
|
15
|
-
let providerTypeMap = "\nexport interface ProvidersConfig {\n";
|
|
16
|
-
let providerSchemaMap = "\nexport const PROVIDER_SCHEMAS = {\n";
|
|
17
|
-
for (const folder of folders) {
|
|
18
|
-
const {
|
|
19
|
-
schemaTitle: paramsSchemaTitle,
|
|
20
|
-
ts: paramsSchemaTs,
|
|
21
|
-
jsonTitle: paramsJsonTitle
|
|
22
|
-
} = await generateTsFromYamlSchema(folder, "parameters");
|
|
23
|
-
const {
|
|
24
|
-
schemaTitle: secretParamsSchemaTitle,
|
|
25
|
-
ts: secretParamsSchemaTs,
|
|
26
|
-
jsonTitle: secretParamsJsonTitle
|
|
27
|
-
} = await generateTsFromYamlSchema(
|
|
28
|
-
folder,
|
|
29
|
-
"secret-parameters"
|
|
30
|
-
);
|
|
31
|
-
ts += `
|
|
32
|
-
${paramsSchemaTs}
|
|
33
|
-
${secretParamsSchemaTs}`;
|
|
34
|
-
providerTypeMap += ` ${folder}: {
|
|
35
|
-
`;
|
|
36
|
-
providerTypeMap += ` parameters: ${paramsSchemaTitle}
|
|
37
|
-
`;
|
|
38
|
-
providerTypeMap += ` secretParameters: ${secretParamsSchemaTitle}
|
|
39
|
-
`;
|
|
40
|
-
providerTypeMap += " }\n";
|
|
41
|
-
providerSchemaMap += ` ${folder}: {
|
|
42
|
-
`;
|
|
43
|
-
providerSchemaMap += ` parameters: ${paramsJsonTitle},
|
|
44
|
-
`;
|
|
45
|
-
providerSchemaMap += ` secretParameters: ${secretParamsJsonTitle}
|
|
46
|
-
`;
|
|
47
|
-
providerSchemaMap += " },\n";
|
|
48
|
-
}
|
|
49
|
-
providerTypeMap += "}\n";
|
|
50
|
-
providerSchemaMap += "}\n";
|
|
51
|
-
ts += providerTypeMap;
|
|
52
|
-
ts += providerSchemaMap;
|
|
53
|
-
await writeFile(GEN_TS_FILENAME, ts);
|
|
54
|
-
console.log(`Wrote to ${GEN_TS_FILENAME}`);
|
|
55
|
-
}
|
|
56
|
-
async function getJsonSchemaForProvider(name, type) {
|
|
57
|
-
const paramsYaml = await readFile(
|
|
58
|
-
`${PROVIDER_SCHEMAS_PATH}/${name}/${type}.yaml`,
|
|
59
|
-
{ encoding: "utf-8" }
|
|
60
|
-
);
|
|
61
|
-
const paramsJson = parse(paramsYaml);
|
|
62
|
-
return paramsJson;
|
|
63
|
-
}
|
|
64
|
-
async function generateTsFromYamlSchema(name, type) {
|
|
65
|
-
const paramsJson = await getJsonSchemaForProvider(name, type);
|
|
66
|
-
let paramsSchemaTs = await compile(
|
|
67
|
-
paramsJson,
|
|
68
|
-
"",
|
|
69
|
-
{
|
|
70
|
-
additionalProperties: false,
|
|
71
|
-
bannerComment: "",
|
|
72
|
-
ignoreMinAndMaxItems: true,
|
|
73
|
-
declareExternallyReferenced: false,
|
|
74
|
-
customName({ type: type2, format }) {
|
|
75
|
-
if (type2 === "string" && format === "binary") {
|
|
76
|
-
return BinaryDataType;
|
|
77
|
-
}
|
|
78
|
-
return void 0;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
const jsonTitle = `${paramsJson.title}Json`;
|
|
83
|
-
paramsSchemaTs += `
|
|
84
|
-
export const ${jsonTitle} = ${JSON.stringify(paramsJson)}`;
|
|
85
|
-
return {
|
|
86
|
-
ts: paramsSchemaTs,
|
|
87
|
-
schemaTitle: paramsJson.title,
|
|
88
|
-
jsonTitle
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
async function findAllProviderFolders() {
|
|
92
|
-
const providerFolders = await readdir(
|
|
93
|
-
PROVIDER_SCHEMAS_PATH,
|
|
94
|
-
{ withFileTypes: true }
|
|
95
|
-
);
|
|
96
|
-
return providerFolders.filter((p) => p.isDirectory()).map((p) => p.name);
|
|
97
|
-
}
|
|
98
|
-
void main();
|
|
99
|
-
export {
|
|
100
|
-
generateTsFromYamlSchema
|
|
101
|
-
};
|