@reclaimprotocol/attestor-core 5.0.1-beta.2 → 5.0.1-beta.3
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/lib/browser/avs/abis/avsDirectoryABI.d.ts +60 -0
- package/lib/browser/avs/abis/avsDirectoryABI.js +343 -0
- package/lib/browser/avs/abis/delegationABI.d.ts +126 -0
- package/lib/browser/avs/abis/delegationABI.js +4 -0
- package/lib/browser/avs/abis/registryABI.d.ts +136 -0
- package/lib/browser/avs/abis/registryABI.js +728 -0
- package/lib/browser/avs/client/create-claim-on-avs.d.ts +12 -0
- package/lib/browser/avs/client/create-claim-on-avs.js +168 -0
- package/lib/browser/avs/config.d.ts +7 -0
- package/lib/browser/avs/config.js +26 -0
- package/lib/browser/avs/contracts/ReclaimServiceManager.d.ts +601 -0
- package/lib/browser/avs/contracts/ReclaimServiceManager.js +0 -0
- package/lib/browser/avs/contracts/common.d.ts +50 -0
- package/lib/browser/avs/contracts/common.js +0 -0
- package/lib/browser/avs/contracts/factories/ReclaimServiceManager__factory.d.ts +890 -0
- package/lib/browser/avs/contracts/factories/ReclaimServiceManager__factory.js +1183 -0
- package/lib/browser/avs/contracts/factories/index.d.ts +1 -0
- package/lib/browser/avs/contracts/factories/index.js +4 -0
- package/lib/browser/avs/contracts/index.d.ts +3 -0
- package/lib/browser/avs/contracts/index.js +6 -0
- package/lib/browser/avs/types/index.d.ts +55 -0
- package/lib/browser/avs/types/index.js +0 -0
- package/lib/browser/avs/utils/contracts.d.ts +21 -0
- package/lib/browser/avs/utils/contracts.js +53 -0
- package/lib/browser/avs/utils/register.d.ts +27 -0
- package/lib/browser/avs/utils/register.js +74 -0
- package/lib/browser/avs/utils/tasks.d.ts +22 -0
- package/lib/browser/avs/utils/tasks.js +48 -0
- package/lib/browser/client/create-claim.d.ts +5 -0
- package/lib/browser/client/create-claim.js +461 -0
- package/lib/browser/client/index.d.ts +3 -0
- package/lib/browser/client/index.js +3 -0
- package/lib/browser/client/tunnels/make-rpc-tcp-tunnel.d.ts +16 -0
- package/lib/browser/client/tunnels/make-rpc-tcp-tunnel.js +53 -0
- package/lib/browser/client/tunnels/make-rpc-tls-tunnel.d.ts +26 -0
- package/lib/browser/client/tunnels/make-rpc-tls-tunnel.js +127 -0
- package/lib/browser/client/utils/attestor-pool.d.ts +8 -0
- package/lib/browser/client/utils/attestor-pool.js +24 -0
- package/lib/browser/client/utils/client-socket.d.ts +11 -0
- package/lib/browser/client/utils/client-socket.js +120 -0
- package/lib/browser/client/utils/message-handler.d.ts +4 -0
- package/lib/browser/client/utils/message-handler.js +97 -0
- package/lib/browser/config/index.d.ts +31 -0
- package/lib/browser/config/index.js +62 -0
- package/lib/browser/external-rpc/benchmark.d.ts +1 -0
- package/lib/browser/external-rpc/benchmark.js +82 -0
- package/lib/browser/external-rpc/event-bus.d.ts +7 -0
- package/lib/browser/external-rpc/event-bus.js +17 -0
- package/lib/browser/external-rpc/global.d.js +0 -0
- package/lib/browser/external-rpc/handle-incoming-msg.d.ts +2 -0
- package/lib/browser/external-rpc/handle-incoming-msg.js +241 -0
- package/lib/browser/external-rpc/index.d.ts +3 -0
- package/lib/browser/external-rpc/index.js +3 -0
- package/lib/browser/external-rpc/jsc-polyfills/1.d.ts +14 -0
- package/lib/browser/external-rpc/jsc-polyfills/1.js +80 -0
- package/lib/browser/external-rpc/jsc-polyfills/2.d.ts +1 -0
- package/lib/browser/external-rpc/jsc-polyfills/2.js +15 -0
- package/lib/browser/external-rpc/jsc-polyfills/event.d.ts +10 -0
- package/lib/browser/external-rpc/jsc-polyfills/event.js +19 -0
- package/lib/browser/external-rpc/jsc-polyfills/index.d.ts +2 -0
- package/lib/browser/external-rpc/jsc-polyfills/index.js +2 -0
- package/lib/browser/external-rpc/jsc-polyfills/ws.d.ts +21 -0
- package/lib/browser/external-rpc/jsc-polyfills/ws.js +83 -0
- package/lib/browser/external-rpc/setup-browser.d.ts +6 -0
- package/lib/browser/external-rpc/setup-browser.js +33 -0
- package/lib/browser/external-rpc/setup-jsc.d.ts +24 -0
- package/lib/browser/external-rpc/setup-jsc.js +22 -0
- package/lib/browser/external-rpc/types.d.ts +213 -0
- package/lib/browser/external-rpc/types.js +0 -0
- package/lib/browser/external-rpc/utils.d.ts +20 -0
- package/lib/browser/external-rpc/utils.js +100 -0
- package/lib/browser/external-rpc/zk.d.ts +14 -0
- package/lib/browser/external-rpc/zk.js +58 -0
- package/lib/browser/index.browser.js +13 -0
- package/lib/browser/index.d.ts +9 -0
- package/lib/browser/index.js +13 -0
- package/lib/browser/mechain/abis/governanceABI.d.ts +50 -0
- package/lib/browser/mechain/abis/governanceABI.js +461 -0
- package/lib/browser/mechain/abis/taskABI.d.ts +157 -0
- package/lib/browser/mechain/abis/taskABI.js +512 -0
- package/lib/browser/mechain/client/create-claim-on-mechain.d.ts +10 -0
- package/lib/browser/mechain/client/create-claim-on-mechain.js +33 -0
- package/lib/browser/mechain/client/index.d.ts +1 -0
- package/lib/browser/mechain/client/index.js +1 -0
- package/lib/browser/mechain/constants/index.d.ts +3 -0
- package/lib/browser/mechain/constants/index.js +8 -0
- package/lib/browser/mechain/index.d.ts +2 -0
- package/lib/browser/mechain/index.js +2 -0
- package/lib/browser/mechain/types/index.d.ts +23 -0
- package/lib/browser/mechain/types/index.js +0 -0
- package/lib/browser/proto/api.d.ts +651 -0
- package/lib/browser/proto/api.js +4250 -0
- package/lib/browser/proto/tee-bundle.d.ts +156 -0
- package/lib/browser/proto/tee-bundle.js +1296 -0
- package/lib/browser/providers/http/index.d.ts +18 -0
- package/lib/browser/providers/http/index.js +640 -0
- package/lib/browser/providers/http/patch-parse5-tree.d.ts +6 -0
- package/lib/browser/providers/http/patch-parse5-tree.js +34 -0
- package/lib/browser/providers/http/utils.d.ts +77 -0
- package/lib/browser/providers/http/utils.js +283 -0
- package/lib/browser/providers/index.d.ts +4 -0
- package/lib/browser/providers/index.js +7 -0
- package/lib/browser/types/bgp.d.ts +11 -0
- package/lib/browser/types/bgp.js +0 -0
- package/lib/browser/types/claims.d.ts +70 -0
- package/lib/browser/types/claims.js +0 -0
- package/lib/browser/types/client.d.ts +163 -0
- package/lib/browser/types/client.js +0 -0
- package/lib/browser/types/general.d.ts +77 -0
- package/lib/browser/types/general.js +0 -0
- package/lib/browser/types/handlers.d.ts +10 -0
- package/lib/browser/types/handlers.js +0 -0
- package/lib/browser/types/index.d.ts +10 -0
- package/lib/browser/types/index.js +10 -0
- package/lib/browser/types/providers.d.ts +161 -0
- package/lib/browser/types/providers.gen.d.ts +443 -0
- package/lib/browser/types/providers.gen.js +16 -0
- package/lib/browser/types/providers.js +0 -0
- package/lib/browser/types/rpc.d.ts +35 -0
- package/lib/browser/types/rpc.js +0 -0
- package/lib/browser/types/signatures.d.ts +28 -0
- package/lib/browser/types/signatures.js +0 -0
- package/lib/browser/types/tunnel.d.ts +18 -0
- package/lib/browser/types/tunnel.js +0 -0
- package/lib/browser/types/zk.d.ts +38 -0
- package/lib/browser/types/zk.js +0 -0
- package/lib/browser/utils/auth.d.ts +8 -0
- package/lib/browser/utils/auth.js +71 -0
- package/lib/browser/utils/b64-json.d.ts +2 -0
- package/lib/browser/utils/b64-json.js +17 -0
- package/lib/browser/utils/claims.d.ts +33 -0
- package/lib/browser/utils/claims.js +89 -0
- package/lib/browser/utils/env.d.ts +3 -0
- package/lib/browser/utils/env.js +19 -0
- package/lib/browser/utils/error.d.ts +26 -0
- package/lib/browser/utils/error.js +54 -0
- package/lib/browser/utils/generics.d.ts +114 -0
- package/lib/browser/utils/generics.js +268 -0
- package/lib/browser/utils/http-parser.d.ts +59 -0
- package/lib/browser/utils/http-parser.js +201 -0
- package/lib/browser/utils/index.browser.js +12 -0
- package/lib/browser/utils/index.d.ts +12 -0
- package/lib/browser/utils/index.js +12 -0
- package/lib/browser/utils/logger.browser.js +88 -0
- package/lib/browser/utils/logger.d.ts +14 -0
- package/lib/browser/utils/logger.js +88 -0
- package/lib/browser/utils/prepare-packets.d.ts +16 -0
- package/lib/browser/utils/prepare-packets.js +69 -0
- package/lib/browser/utils/redactions.d.ts +73 -0
- package/lib/browser/utils/redactions.js +135 -0
- package/lib/browser/utils/retries.d.ts +12 -0
- package/lib/browser/utils/retries.js +26 -0
- package/lib/browser/utils/signatures/eth.d.ts +2 -0
- package/lib/browser/utils/signatures/eth.js +31 -0
- package/lib/browser/utils/signatures/index.d.ts +5 -0
- package/lib/browser/utils/signatures/index.js +12 -0
- package/lib/browser/utils/socket-base.d.ts +23 -0
- package/lib/browser/utils/socket-base.js +96 -0
- package/lib/browser/utils/tls.d.ts +2 -0
- package/lib/browser/utils/tls.js +58 -0
- package/lib/browser/utils/ws.d.ts +7 -0
- package/lib/browser/utils/ws.js +22 -0
- package/lib/browser/utils/zk.d.ts +71 -0
- package/lib/browser/utils/zk.js +625 -0
- package/lib/index.browser.d.ts +9 -0
- package/lib/types/general.d.ts +1 -0
- package/lib/utils/index.browser.d.ts +12 -0
- package/lib/utils/logger.browser.d.ts +14 -0
- package/package.json +18 -2
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { hexlify } from "ethers";
|
|
2
|
+
import { createClaimOnAvs } from "../../avs/client/create-claim-on-avs.js";
|
|
3
|
+
import { createClaimOnAttestor } from "../../client/index.js";
|
|
4
|
+
import { benchmark } from "../../external-rpc/benchmark.js";
|
|
5
|
+
import { generateRpcRequestId, getCurrentMemoryUsage, getWsApiUrlFromBaseUrl, RPC_MSG_BRIDGE, sendMessageToApp, waitForResponse } from "../../external-rpc/utils.js";
|
|
6
|
+
import { ALL_ENC_ALGORITHMS, makeExternalRpcOprfOperator, makeExternalRpcZkOperator } from "../../external-rpc/zk.js";
|
|
7
|
+
import { createClaimOnMechain } from "../../mechain/client/create-claim-on-mechain.js";
|
|
8
|
+
import { extractHTMLElement, extractJSONValueIndex, generateRequstAndResponseFromTranscript } from "../../providers/http/utils.js";
|
|
9
|
+
import { B64_JSON_REVIVER } from "../../utils/b64-json.js";
|
|
10
|
+
import { AttestorError, getIdentifierFromClaimInfo, logger, makeLogger, uint8ArrayToStr } from "../../utils/index.js";
|
|
11
|
+
async function handleIncomingMessage(data) {
|
|
12
|
+
let id = "";
|
|
13
|
+
try {
|
|
14
|
+
const req = typeof data === "string" ? JSON.parse(data, B64_JSON_REVIVER) : data;
|
|
15
|
+
id = req.id || "";
|
|
16
|
+
const rslt = await _handleIncomingMessage(req);
|
|
17
|
+
if (!rslt) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
respond(rslt);
|
|
21
|
+
} catch (err) {
|
|
22
|
+
console.error("Error in RPC", { id, error: err });
|
|
23
|
+
respond({
|
|
24
|
+
type: "error",
|
|
25
|
+
data: {
|
|
26
|
+
message: err.message,
|
|
27
|
+
stack: err.stack
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function respond(data2) {
|
|
32
|
+
const res = {
|
|
33
|
+
...data2,
|
|
34
|
+
id,
|
|
35
|
+
isResponse: true
|
|
36
|
+
};
|
|
37
|
+
return sendMessageToApp(res);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function _handleIncomingMessage(req) {
|
|
41
|
+
const { id: reqId, type: reqType } = req;
|
|
42
|
+
if (!reqId || !reqType) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
RPC_MSG_BRIDGE.dispatch(req);
|
|
46
|
+
if ("isResponse" in req && req.isResponse) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!reqId) {
|
|
50
|
+
logger.warn({ req }, "Window RPC request missing ID");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
logger.info({ req }, "processing RPC request");
|
|
54
|
+
switch (req.type) {
|
|
55
|
+
case "createClaim":
|
|
56
|
+
const claimTunnelRes = await createClaimOnAttestor({
|
|
57
|
+
...req.request,
|
|
58
|
+
context: req.request.context ? JSON.parse(req.request.context) : void 0,
|
|
59
|
+
zkOperators: getZkOperators(
|
|
60
|
+
req.request.zkOperatorMode,
|
|
61
|
+
req.request.zkEngine
|
|
62
|
+
),
|
|
63
|
+
oprfOperators: getOprfOperators(
|
|
64
|
+
req.request.zkOperatorMode,
|
|
65
|
+
req.request.zkEngine
|
|
66
|
+
),
|
|
67
|
+
client: {
|
|
68
|
+
url: getWsApiUrlFromBaseUrl(),
|
|
69
|
+
authRequest: req.request.authRequest
|
|
70
|
+
},
|
|
71
|
+
logger,
|
|
72
|
+
onStep(step) {
|
|
73
|
+
sendMessageToApp({
|
|
74
|
+
type: "createClaimStep",
|
|
75
|
+
step: { name: "attestor-progress", step },
|
|
76
|
+
id: req.id
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
updateProviderParams: req.request.updateProviderParams ? updateProviderParams : void 0
|
|
80
|
+
});
|
|
81
|
+
const response = mapToCreateClaimResponse(claimTunnelRes);
|
|
82
|
+
return { type: "createClaimDone", response };
|
|
83
|
+
case "createClaimOnAvs":
|
|
84
|
+
const avsRes = await createClaimOnAvs({
|
|
85
|
+
...req.request,
|
|
86
|
+
payer: req.request.payer === "attestor" ? { attestor: getWsApiUrlFromBaseUrl() } : void 0,
|
|
87
|
+
context: req.request.context ? JSON.parse(req.request.context) : void 0,
|
|
88
|
+
zkOperators: getZkOperators(
|
|
89
|
+
req.request.zkOperatorMode,
|
|
90
|
+
req.request.zkEngine
|
|
91
|
+
),
|
|
92
|
+
oprfOperators: getOprfOperators(
|
|
93
|
+
req.request.zkOperatorMode,
|
|
94
|
+
req.request.zkEngine
|
|
95
|
+
),
|
|
96
|
+
logger,
|
|
97
|
+
onStep(step) {
|
|
98
|
+
sendMessageToApp({
|
|
99
|
+
type: "createClaimOnAvsStep",
|
|
100
|
+
step,
|
|
101
|
+
id: req.id
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
type: "createClaimOnAvsDone",
|
|
107
|
+
response: avsRes
|
|
108
|
+
};
|
|
109
|
+
case "createClaimOnMechain":
|
|
110
|
+
const mechainRes = await createClaimOnMechain({
|
|
111
|
+
...req.request,
|
|
112
|
+
context: req.request.context ? JSON.parse(req.request.context) : void 0,
|
|
113
|
+
zkOperators: getZkOperators(
|
|
114
|
+
req.request.zkOperatorMode,
|
|
115
|
+
req.request.zkEngine
|
|
116
|
+
),
|
|
117
|
+
oprfOperators: getOprfOperators(
|
|
118
|
+
req.request.zkOperatorMode,
|
|
119
|
+
req.request.zkEngine
|
|
120
|
+
),
|
|
121
|
+
client: { url: getWsApiUrlFromBaseUrl() },
|
|
122
|
+
logger,
|
|
123
|
+
onStep(step) {
|
|
124
|
+
sendMessageToApp({
|
|
125
|
+
type: "createClaimOnMechainStep",
|
|
126
|
+
step,
|
|
127
|
+
id: req.id
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
const claimResponses = [];
|
|
132
|
+
for (let i = 0; i < mechainRes.responses.length; i++) {
|
|
133
|
+
claimResponses[i] = mapToCreateClaimResponse(mechainRes.responses[i]);
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
type: "createClaimOnMechainDone",
|
|
137
|
+
response: { taskId: mechainRes.taskId, data: claimResponses }
|
|
138
|
+
};
|
|
139
|
+
case "extractHtmlElement":
|
|
140
|
+
return {
|
|
141
|
+
type: "extractHtmlElementDone",
|
|
142
|
+
response: extractHTMLElement(
|
|
143
|
+
req.request.html,
|
|
144
|
+
req.request.xpathExpression,
|
|
145
|
+
req.request.contentsOnly
|
|
146
|
+
)
|
|
147
|
+
};
|
|
148
|
+
case "extractJSONValueIndex":
|
|
149
|
+
return {
|
|
150
|
+
type: "extractJSONValueIndexDone",
|
|
151
|
+
response: extractJSONValueIndex(
|
|
152
|
+
req.request.json,
|
|
153
|
+
req.request.jsonPath
|
|
154
|
+
)
|
|
155
|
+
};
|
|
156
|
+
case "getCurrentMemoryUsage":
|
|
157
|
+
return {
|
|
158
|
+
type: "getCurrentMemoryUsageDone",
|
|
159
|
+
response: await getCurrentMemoryUsage()
|
|
160
|
+
};
|
|
161
|
+
case "setLogLevel":
|
|
162
|
+
makeLogger(
|
|
163
|
+
true,
|
|
164
|
+
req.request.logLevel,
|
|
165
|
+
req.request.sendLogsToApp ? (level, message) => sendMessageToApp({
|
|
166
|
+
type: "log",
|
|
167
|
+
level,
|
|
168
|
+
message,
|
|
169
|
+
id: req.id
|
|
170
|
+
}) : void 0
|
|
171
|
+
);
|
|
172
|
+
return { type: "setLogLevelDone", response: void 0 };
|
|
173
|
+
case "benchmarkZK":
|
|
174
|
+
return { type: "benchmarkZKDone", response: await benchmark() };
|
|
175
|
+
case "ping":
|
|
176
|
+
return { type: "pingDone", response: { pong: (/* @__PURE__ */ new Date()).toJSON() } };
|
|
177
|
+
default:
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function getZkOperators(mode = "default", zkEngine = "snarkjs") {
|
|
182
|
+
if (mode === "default") {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const operators = {};
|
|
186
|
+
for (const alg of ALL_ENC_ALGORITHMS) {
|
|
187
|
+
operators[alg] = makeExternalRpcZkOperator(alg, zkEngine);
|
|
188
|
+
}
|
|
189
|
+
return operators;
|
|
190
|
+
}
|
|
191
|
+
function getOprfOperators(mode = "default", zkEngine = "snarkjs") {
|
|
192
|
+
if (mode === "default") {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const operators = {};
|
|
196
|
+
for (const alg of ALL_ENC_ALGORITHMS) {
|
|
197
|
+
operators[alg] = makeExternalRpcOprfOperator(alg, zkEngine);
|
|
198
|
+
}
|
|
199
|
+
return operators;
|
|
200
|
+
}
|
|
201
|
+
async function updateProviderParams(transcript, tlsVersion) {
|
|
202
|
+
const { req, res } = generateRequstAndResponseFromTranscript(
|
|
203
|
+
transcript,
|
|
204
|
+
tlsVersion
|
|
205
|
+
);
|
|
206
|
+
const id = generateRpcRequestId();
|
|
207
|
+
const waitForRes = waitForResponse("updateProviderParams", id);
|
|
208
|
+
sendMessageToApp({
|
|
209
|
+
type: "updateProviderParams",
|
|
210
|
+
id,
|
|
211
|
+
request: {
|
|
212
|
+
request: {
|
|
213
|
+
...req,
|
|
214
|
+
body: req.body ? uint8ArrayToStr(req.body) : void 0
|
|
215
|
+
},
|
|
216
|
+
response: { ...res, body: uint8ArrayToStr(res.body) }
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
return waitForRes;
|
|
220
|
+
}
|
|
221
|
+
function mapToCreateClaimResponse(res) {
|
|
222
|
+
if (!res.claim) {
|
|
223
|
+
throw AttestorError.fromProto(res.error);
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
identifier: getIdentifierFromClaimInfo(res.claim),
|
|
227
|
+
claimData: res.claim,
|
|
228
|
+
witnesses: [
|
|
229
|
+
{
|
|
230
|
+
id: res.signatures.attestorAddress,
|
|
231
|
+
url: getWsApiUrlFromBaseUrl()
|
|
232
|
+
}
|
|
233
|
+
],
|
|
234
|
+
signatures: [
|
|
235
|
+
hexlify(res.signatures.claimSignature).toLowerCase()
|
|
236
|
+
]
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
export {
|
|
240
|
+
handleIncomingMessage
|
|
241
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ExternalRPCIncomingMsg, ExternalRPCOutgoingMsg } from '#src/external-rpc/types.ts';
|
|
2
|
+
declare global {
|
|
3
|
+
function readline(): string;
|
|
4
|
+
function print(...args: any[]): void;
|
|
5
|
+
function debug(...args: any[]): void;
|
|
6
|
+
function quit(): void;
|
|
7
|
+
type JSCIncomingMsg = ExternalRPCIncomingMsg | {
|
|
8
|
+
type: 'quit';
|
|
9
|
+
} | {
|
|
10
|
+
type: 'init';
|
|
11
|
+
attestorBaseUrl: string;
|
|
12
|
+
};
|
|
13
|
+
type JSCOutgoingMsg = ExternalRPCOutgoingMsg;
|
|
14
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { TextDecoder, TextEncoder } from "@kayahr/text-encoding";
|
|
2
|
+
import { crypto, uint8ArrayToBinaryStr } from "@reclaimprotocol/tls";
|
|
3
|
+
import { fromByteArray, toByteArray } from "base64-js";
|
|
4
|
+
import { EventTarget } from "event-target-shim";
|
|
5
|
+
if (typeof global === "undefined") {
|
|
6
|
+
globalThis.global = globalThis;
|
|
7
|
+
}
|
|
8
|
+
if (typeof console === "undefined") {
|
|
9
|
+
globalThis.console = {
|
|
10
|
+
log: print,
|
|
11
|
+
error: print,
|
|
12
|
+
warn: print,
|
|
13
|
+
info: print,
|
|
14
|
+
debug: print
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
if (typeof globalThis.crypto === "undefined") {
|
|
18
|
+
globalThis.crypto = {
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
getRandomValues(arr) {
|
|
21
|
+
const randVals = crypto.randomBytes(arr.length);
|
|
22
|
+
for (let i = 0; i < arr.length; i++) {
|
|
23
|
+
arr[i] = randVals[i];
|
|
24
|
+
}
|
|
25
|
+
return arr;
|
|
26
|
+
},
|
|
27
|
+
randomBytes(length) {
|
|
28
|
+
return crypto.randomBytes(length);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (typeof globalThis.TextEncoder === "undefined") {
|
|
33
|
+
globalThis.TextEncoder = TextEncoder;
|
|
34
|
+
}
|
|
35
|
+
if (typeof globalThis.TextDecoder === "undefined") {
|
|
36
|
+
globalThis.TextDecoder = TextDecoder;
|
|
37
|
+
}
|
|
38
|
+
if (typeof globalThis.EventTarget === "undefined") {
|
|
39
|
+
globalThis.EventTarget = EventTarget;
|
|
40
|
+
}
|
|
41
|
+
if (typeof globalThis.atob === "undefined") {
|
|
42
|
+
globalThis.atob = (a) => uint8ArrayToBinaryStr(toByteArray(a));
|
|
43
|
+
}
|
|
44
|
+
if (typeof globalThis.btoa === "undefined") {
|
|
45
|
+
let binaryStringToUint8Array = function(binaryString) {
|
|
46
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
47
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
48
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
49
|
+
}
|
|
50
|
+
return uint8Array;
|
|
51
|
+
};
|
|
52
|
+
globalThis.btoa = (b) => fromByteArray(binaryStringToUint8Array(b));
|
|
53
|
+
}
|
|
54
|
+
if (typeof globalThis.clearTimeout === "undefined") {
|
|
55
|
+
const ogSettimeout = globalThis.setTimeout;
|
|
56
|
+
if (!ogSettimeout) {
|
|
57
|
+
throw new Error("setTimeout is not defined, no polyfill yet");
|
|
58
|
+
}
|
|
59
|
+
globalThis.setTimeout = (fn, delayMs, ...args) => {
|
|
60
|
+
let aborted = false;
|
|
61
|
+
const abortableFn = (...args2) => {
|
|
62
|
+
if (aborted) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
return fn(...args2);
|
|
66
|
+
};
|
|
67
|
+
const val = ogSettimeout(abortableFn, delayMs, ...args);
|
|
68
|
+
return {
|
|
69
|
+
original: val,
|
|
70
|
+
abort() {
|
|
71
|
+
aborted = true;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
globalThis.clearTimeout = (id) => {
|
|
76
|
+
if (typeof id === "object" && !!id && "abort" in id) {
|
|
77
|
+
id.abort();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { URL as WHATWG_URL } from "whatwg-url";
|
|
2
|
+
import { CloseEventPolyfill, ErrorEventPolyfill, EventPolyfill, MessageEventPolyfill } from "../../../external-rpc/jsc-polyfills/event.js";
|
|
3
|
+
import { RPCWebSocket } from "../../../external-rpc/jsc-polyfills/ws.js";
|
|
4
|
+
if (typeof globalThis.URL === "undefined") {
|
|
5
|
+
globalThis.URL = WHATWG_URL;
|
|
6
|
+
}
|
|
7
|
+
if (typeof globalThis.Event === "undefined") {
|
|
8
|
+
globalThis.Event = EventPolyfill;
|
|
9
|
+
globalThis.ErrorEvent = ErrorEventPolyfill;
|
|
10
|
+
globalThis.CloseEvent = CloseEventPolyfill;
|
|
11
|
+
globalThis.MessageEvent = MessageEventPolyfill;
|
|
12
|
+
}
|
|
13
|
+
if (typeof globalThis.WebSocket === "undefined") {
|
|
14
|
+
globalThis.WebSocket = RPCWebSocket;
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class EventPolyfill implements Event {
|
|
2
|
+
type: string;
|
|
3
|
+
constructor(type: string, eventInitDict?: EventInit);
|
|
4
|
+
}
|
|
5
|
+
export declare class ErrorEventPolyfill extends EventPolyfill {
|
|
6
|
+
}
|
|
7
|
+
export declare class CloseEventPolyfill extends EventPolyfill {
|
|
8
|
+
}
|
|
9
|
+
export declare class MessageEventPolyfill extends EventPolyfill {
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class EventPolyfill {
|
|
2
|
+
type;
|
|
3
|
+
constructor(type, eventInitDict) {
|
|
4
|
+
this.type = type;
|
|
5
|
+
Object.assign(this, eventInitDict);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
class ErrorEventPolyfill extends EventPolyfill {
|
|
9
|
+
}
|
|
10
|
+
class CloseEventPolyfill extends EventPolyfill {
|
|
11
|
+
}
|
|
12
|
+
class MessageEventPolyfill extends EventPolyfill {
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
CloseEventPolyfill,
|
|
16
|
+
ErrorEventPolyfill,
|
|
17
|
+
EventPolyfill,
|
|
18
|
+
MessageEventPolyfill
|
|
19
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class RPCWebSocket extends EventTarget implements WebSocket {
|
|
2
|
+
#private;
|
|
3
|
+
readonly CONNECTING: 0;
|
|
4
|
+
readonly OPEN: 1;
|
|
5
|
+
readonly CLOSING: 2;
|
|
6
|
+
readonly CLOSED: 3;
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly binaryType: BinaryType;
|
|
9
|
+
readonly bufferedAmount: number;
|
|
10
|
+
readonly extensions: string;
|
|
11
|
+
readonly url: string;
|
|
12
|
+
readonly protocol: string;
|
|
13
|
+
readyState: number;
|
|
14
|
+
onopen: ((this: WebSocket, ev: Event) => any) | null;
|
|
15
|
+
onerror: ((this: WebSocket, ev: Event) => any) | null;
|
|
16
|
+
onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
|
|
17
|
+
onmessage: ((this: WebSocket, ev: MessageEvent<any>) => any) | null;
|
|
18
|
+
constructor(url: string);
|
|
19
|
+
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
|
|
20
|
+
close(code?: number, reason?: string): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { RPC_MSG_BRIDGE, rpcRequest } from "../../../external-rpc/utils.js";
|
|
2
|
+
class RPCWebSocket extends EventTarget {
|
|
3
|
+
CONNECTING = 0;
|
|
4
|
+
OPEN = 1;
|
|
5
|
+
CLOSING = 2;
|
|
6
|
+
CLOSED = 3;
|
|
7
|
+
id = `ws_${Date.now()}`;
|
|
8
|
+
binaryType = "arraybuffer";
|
|
9
|
+
bufferedAmount = 0;
|
|
10
|
+
extensions = "";
|
|
11
|
+
url;
|
|
12
|
+
protocol;
|
|
13
|
+
readyState = this.CONNECTING;
|
|
14
|
+
onopen;
|
|
15
|
+
onerror;
|
|
16
|
+
onclose;
|
|
17
|
+
onmessage;
|
|
18
|
+
#cancelRpcBridge;
|
|
19
|
+
constructor(url) {
|
|
20
|
+
super();
|
|
21
|
+
this.url = url;
|
|
22
|
+
this.protocol = "";
|
|
23
|
+
this.#onMessage = this.#onMessage.bind(this);
|
|
24
|
+
rpcRequest({
|
|
25
|
+
type: "connectWs",
|
|
26
|
+
request: { id: this.id, url }
|
|
27
|
+
}).then(() => this.#callOpen(new Event("open"))).catch((error) => this.#callError(new ErrorEvent("error", { error })));
|
|
28
|
+
}
|
|
29
|
+
send(data) {
|
|
30
|
+
if (typeof data !== "string" && !ArrayBuffer.isView(data)) {
|
|
31
|
+
throw new TypeError("Data must be a string, Uint8Array or ArrayBuffer");
|
|
32
|
+
}
|
|
33
|
+
rpcRequest({ type: "sendWsMessage", request: { id: this.id, data } });
|
|
34
|
+
}
|
|
35
|
+
close(code, reason) {
|
|
36
|
+
this.readyState = this.CLOSING;
|
|
37
|
+
rpcRequest({
|
|
38
|
+
type: "disconnectWs",
|
|
39
|
+
request: { id: this.id, code, reason }
|
|
40
|
+
}).then(() => this.#callClose(new CloseEvent("close", { code, reason }))).catch((error) => this.#callError(new ErrorEvent("error", { error })));
|
|
41
|
+
}
|
|
42
|
+
#callOpen(ev) {
|
|
43
|
+
this.readyState = this.OPEN;
|
|
44
|
+
this.onopen?.call(this, ev);
|
|
45
|
+
this.dispatchEvent(ev);
|
|
46
|
+
this.#cancelRpcBridge?.();
|
|
47
|
+
this.#cancelRpcBridge = RPC_MSG_BRIDGE.addListener(this.#onMessage);
|
|
48
|
+
}
|
|
49
|
+
#callError(ev) {
|
|
50
|
+
this.readyState = this.CLOSED;
|
|
51
|
+
this.onerror?.call(this, ev);
|
|
52
|
+
this.dispatchEvent(ev);
|
|
53
|
+
}
|
|
54
|
+
#callClose(ev) {
|
|
55
|
+
this.readyState = this.CLOSED;
|
|
56
|
+
this.onclose?.call(this, ev);
|
|
57
|
+
this.dispatchEvent(ev);
|
|
58
|
+
}
|
|
59
|
+
#onMessage = (msg) => {
|
|
60
|
+
if (msg.type === "sendWsMessage" && msg.request.id === this.id) {
|
|
61
|
+
const data = msg.request.data;
|
|
62
|
+
const event = new MessageEvent("message", { data });
|
|
63
|
+
this.onmessage?.call(this, event);
|
|
64
|
+
this.dispatchEvent(event);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (msg.type === "disconnectWs" && msg.request.id === this.id) {
|
|
68
|
+
if (!msg.request.err) {
|
|
69
|
+
this.#callClose(
|
|
70
|
+
new CloseEvent("close", { code: 1e3, reason: "Normal Closure" })
|
|
71
|
+
);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.#callError(
|
|
75
|
+
new ErrorEvent("error", { error: new Error(msg.request.err) })
|
|
76
|
+
);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
RPCWebSocket
|
|
83
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { setCryptoImplementation } from "@reclaimprotocol/tls";
|
|
2
|
+
import { webcryptoCrypto } from "@reclaimprotocol/tls/webcrypto";
|
|
3
|
+
import { handleIncomingMessage } from "../../external-rpc/handle-incoming-msg.js";
|
|
4
|
+
import { getWsApiUrlFromBaseUrl } from "../../external-rpc/utils.js";
|
|
5
|
+
import { logger, makeLogger } from "../../utils/index.js";
|
|
6
|
+
makeLogger(true);
|
|
7
|
+
setCryptoImplementation(webcryptoCrypto);
|
|
8
|
+
function setupWindowRpc(baseUrl, channel = "attestor-core") {
|
|
9
|
+
if (baseUrl) {
|
|
10
|
+
globalThis.ATTESTOR_BASE_URL = baseUrl;
|
|
11
|
+
} else if (typeof window !== "undefined" && window.location) {
|
|
12
|
+
globalThis.ATTESTOR_BASE_URL = window.location.toString();
|
|
13
|
+
} else {
|
|
14
|
+
throw new Error("No base URL provided and window.location unavailable");
|
|
15
|
+
}
|
|
16
|
+
if (channel) {
|
|
17
|
+
globalThis.RPC_CHANNEL_NAME = channel;
|
|
18
|
+
} else if (!globalThis.RPC_CHANNEL_NAME) {
|
|
19
|
+
throw new Error("No channel name provided and globalThis.RPC_CHANNEL_NAME unavailable");
|
|
20
|
+
}
|
|
21
|
+
if (typeof window !== "undefined") {
|
|
22
|
+
window.addEventListener(
|
|
23
|
+
"message",
|
|
24
|
+
(ev) => handleIncomingMessage(ev.data),
|
|
25
|
+
false
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
logger.info({ defaultUrl: getWsApiUrlFromBaseUrl() }, "window RPC setup");
|
|
29
|
+
}
|
|
30
|
+
export * from "../../index.js";
|
|
31
|
+
export {
|
|
32
|
+
setupWindowRpc
|
|
33
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import '#src/external-rpc/jsc-polyfills/index.ts';
|
|
2
|
+
import * as AttestorRPCImport from '#src/external-rpc/index.ts';
|
|
3
|
+
declare global {
|
|
4
|
+
/**
|
|
5
|
+
* `sendMessage` function should be provided by the host of the JS environment for sending messages to host
|
|
6
|
+
*/
|
|
7
|
+
function sendMessage(channelName: string, message: any): void | Promise<void>;
|
|
8
|
+
var AttestorRPC: typeof AttestorRPCImport & {
|
|
9
|
+
/**
|
|
10
|
+
* Sets up the library to run in JS environments like QuickJS or JavascriptCore.
|
|
11
|
+
*
|
|
12
|
+
* RPC will communicate with user by sending messages using `AttestorRPCChannel` by `<channel>.postMessage(message: string)`,
|
|
13
|
+
* for example: `globalThis['attestor-core'].postMessage(message: string)`.
|
|
14
|
+
*
|
|
15
|
+
* @param baseUrl
|
|
16
|
+
* @param channel The name of the channel for sending messages. Default channel is 'attestor-core'.
|
|
17
|
+
*/
|
|
18
|
+
setupJsRpc(baseUrl: string, channel?: string): void;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Sets up the library to run in JS environments like QuickJS or JavascriptCore.
|
|
23
|
+
*/
|
|
24
|
+
export declare function setupJsRpc(baseUrl: string, channel?: string): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import "../../external-rpc/jsc-polyfills/index.js";
|
|
2
|
+
1;
|
|
3
|
+
import { setCryptoImplementation } from "@reclaimprotocol/tls";
|
|
4
|
+
import { pureJsCrypto } from "@reclaimprotocol/tls/purejs-crypto";
|
|
5
|
+
import * as AttestorRPCImport from "../../external-rpc/index.js";
|
|
6
|
+
import { makeLogger } from "../../utils/logger.js";
|
|
7
|
+
setCryptoImplementation(pureJsCrypto);
|
|
8
|
+
makeLogger(true);
|
|
9
|
+
function setupJsRpc(baseUrl, channel = "attestor-core") {
|
|
10
|
+
globalThis.ATTESTOR_BASE_URL = baseUrl;
|
|
11
|
+
globalThis.RPC_CHANNEL_NAME = channel;
|
|
12
|
+
const rpcChannel = {
|
|
13
|
+
postMessage(message) {
|
|
14
|
+
return globalThis.sendMessage(channel, message);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
globalThis[channel] = rpcChannel;
|
|
18
|
+
}
|
|
19
|
+
globalThis.AttestorRPC = { ...AttestorRPCImport, setupJsRpc };
|
|
20
|
+
export {
|
|
21
|
+
setupJsRpc
|
|
22
|
+
};
|