@reclaimprotocol/attestor-core 5.0.2 → 5.0.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.
Files changed (122) hide show
  1. package/LICENSE +660 -660
  2. package/browser/resources/attestor-browser.min.mjs +31 -31
  3. package/lib/avs/abis/avsDirectoryABI.js +340 -0
  4. package/lib/avs/abis/delegationABI.js +1 -0
  5. package/lib/avs/abis/registryABI.js +725 -0
  6. package/lib/avs/client/create-claim-on-avs.js +138 -0
  7. package/lib/avs/config.js +20 -0
  8. package/lib/avs/contracts/ReclaimServiceManager.js +1 -0
  9. package/lib/avs/contracts/common.js +1 -0
  10. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1169 -0
  11. package/lib/avs/contracts/factories/index.js +4 -0
  12. package/lib/avs/contracts/index.js +2 -0
  13. package/lib/avs/types/index.js +1 -0
  14. package/lib/avs/utils/contracts.js +33 -0
  15. package/lib/avs/utils/register.js +78 -0
  16. package/lib/avs/utils/tasks.js +40 -0
  17. package/lib/client/create-claim.js +433 -0
  18. package/lib/client/index.js +3 -0
  19. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +51 -0
  20. package/lib/client/tunnels/make-rpc-tls-tunnel.js +131 -0
  21. package/lib/client/utils/attestor-pool.js +25 -0
  22. package/lib/client/utils/client-socket.js +98 -0
  23. package/lib/client/utils/message-handler.js +87 -0
  24. package/lib/config/index.d.ts +2 -2
  25. package/lib/config/index.js +44 -0
  26. package/lib/external-rpc/benchmark.js +69 -0
  27. package/lib/external-rpc/event-bus.js +14 -0
  28. package/lib/external-rpc/handle-incoming-msg.js +233 -0
  29. package/lib/external-rpc/index.js +101 -54
  30. package/lib/external-rpc/jsc-polyfills/1.js +82 -0
  31. package/lib/external-rpc/jsc-polyfills/2.js +20 -0
  32. package/lib/external-rpc/jsc-polyfills/event.js +14 -0
  33. package/lib/external-rpc/jsc-polyfills/index.js +2 -0
  34. package/lib/external-rpc/jsc-polyfills/ws.js +81 -0
  35. package/lib/external-rpc/setup-browser.js +33 -0
  36. package/lib/external-rpc/setup-jsc.js +22 -0
  37. package/lib/external-rpc/types.js +1 -0
  38. package/lib/external-rpc/utils.js +100 -0
  39. package/lib/external-rpc/zk.js +63 -0
  40. package/lib/index.js +121 -72
  41. package/lib/mechain/abis/governanceABI.js +458 -0
  42. package/lib/mechain/abis/taskABI.js +509 -0
  43. package/lib/mechain/client/create-claim-on-mechain.js +28 -0
  44. package/lib/mechain/client/index.js +1 -0
  45. package/lib/mechain/constants/index.js +3 -0
  46. package/lib/mechain/index.js +2 -0
  47. package/lib/mechain/types/index.js +1 -0
  48. package/lib/proto/api.d.ts +2 -0
  49. package/lib/proto/api.js +4273 -0
  50. package/lib/proto/tee-bundle.js +1316 -0
  51. package/lib/providers/http/index.js +658 -0
  52. package/lib/providers/http/patch-parse5-tree.js +33 -0
  53. package/lib/providers/http/utils.js +324 -0
  54. package/lib/providers/index.js +4 -0
  55. package/lib/scripts/fetch-ec2-metadata.d.ts +1 -0
  56. package/lib/server/create-server.js +103 -0
  57. package/lib/server/handlers/claimTeeBundle.js +252 -0
  58. package/lib/server/handlers/claimTunnel.js +73 -0
  59. package/lib/server/handlers/completeClaimOnChain.js +22 -0
  60. package/lib/server/handlers/createClaimOnChain.js +26 -0
  61. package/lib/server/handlers/createTaskOnMechain.js +47 -0
  62. package/lib/server/handlers/createTunnel.js +93 -0
  63. package/lib/server/handlers/disconnectTunnel.js +5 -0
  64. package/lib/server/handlers/fetchCertificateBytes.js +41 -0
  65. package/lib/server/handlers/index.js +22 -0
  66. package/lib/server/handlers/init.js +32 -0
  67. package/lib/server/handlers/toprf.js +16 -0
  68. package/lib/server/index.js +4 -0
  69. package/lib/server/socket.js +109 -0
  70. package/lib/server/tunnels/make-tcp-tunnel.js +177 -0
  71. package/lib/server/utils/apm.js +36 -0
  72. package/lib/server/utils/assert-valid-claim-request.d.ts +1 -1
  73. package/lib/server/utils/assert-valid-claim-request.js +204 -0
  74. package/lib/server/utils/config-env.js +4 -0
  75. package/lib/server/utils/dns.js +18 -0
  76. package/lib/server/utils/gcp-attestation.js +289 -0
  77. package/lib/server/utils/generics.d.ts +1 -0
  78. package/lib/server/utils/generics.js +51 -0
  79. package/lib/server/utils/iso.js +256 -0
  80. package/lib/server/utils/keep-alive.js +38 -0
  81. package/lib/server/utils/nitro-attestation.js +325 -0
  82. package/lib/server/utils/process-handshake.js +215 -0
  83. package/lib/server/utils/proxy-session.js +6 -0
  84. package/lib/server/utils/tee-oprf-mpc-verification.js +90 -0
  85. package/lib/server/utils/tee-oprf-verification.js +174 -0
  86. package/lib/server/utils/tee-transcript-reconstruction.js +187 -0
  87. package/lib/server/utils/tee-verification.js +421 -0
  88. package/lib/server/utils/validation.js +38 -0
  89. package/lib/types/bgp.js +1 -0
  90. package/lib/types/claims.js +1 -0
  91. package/lib/types/client.js +1 -0
  92. package/lib/types/general.js +1 -0
  93. package/lib/types/handlers.js +1 -0
  94. package/lib/types/index.js +10 -0
  95. package/lib/types/providers.d.ts +1 -0
  96. package/lib/types/providers.gen.js +10 -0
  97. package/lib/types/providers.js +1 -0
  98. package/lib/types/rpc.js +1 -0
  99. package/lib/types/signatures.js +1 -0
  100. package/lib/types/tunnel.js +1 -0
  101. package/lib/types/zk.js +1 -0
  102. package/lib/utils/auth.js +59 -0
  103. package/lib/utils/b64-json.js +17 -0
  104. package/lib/utils/bgp-listener.js +119 -0
  105. package/lib/utils/claims.js +99 -0
  106. package/lib/utils/env.js +15 -0
  107. package/lib/utils/error.js +50 -0
  108. package/lib/utils/generics.js +317 -0
  109. package/lib/utils/http-parser.d.ts +2 -1
  110. package/lib/utils/http-parser.js +246 -0
  111. package/lib/utils/index.js +13 -0
  112. package/lib/utils/logger.js +91 -0
  113. package/lib/utils/prepare-packets.js +62 -0
  114. package/lib/utils/redactions.js +148 -0
  115. package/lib/utils/retries.js +24 -0
  116. package/lib/utils/signatures/eth.js +29 -0
  117. package/lib/utils/signatures/index.js +7 -0
  118. package/lib/utils/socket-base.js +90 -0
  119. package/lib/utils/tls.js +58 -0
  120. package/lib/utils/ws.js +22 -0
  121. package/lib/utils/zk.js +577 -0
  122. package/package.json +3 -2
@@ -0,0 +1,81 @@
1
+ import { RPC_MSG_BRIDGE, rpcRequest } from "../utils.js";
2
+ export 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
+ })
28
+ .then(() => this.#callOpen(new Event('open')))
29
+ .catch(error => this.#callError(new ErrorEvent('error', { error })));
30
+ }
31
+ send(data) {
32
+ if (typeof data !== 'string'
33
+ && !ArrayBuffer.isView(data)) {
34
+ throw new TypeError('Data must be a string, Uint8Array or ArrayBuffer');
35
+ }
36
+ rpcRequest({ type: 'sendWsMessage', request: { id: this.id, data: data } });
37
+ }
38
+ close(code, reason) {
39
+ this.readyState = this.CLOSING;
40
+ rpcRequest({
41
+ type: 'disconnectWs',
42
+ request: { id: this.id, code, reason }
43
+ })
44
+ .then(() => this.#callClose(new CloseEvent('close', { code, reason })))
45
+ .catch(error => this.#callError(new ErrorEvent('error', { error })));
46
+ }
47
+ #callOpen(ev) {
48
+ this.readyState = this.OPEN;
49
+ this.onopen?.call(this, ev);
50
+ this.dispatchEvent(ev);
51
+ this.#cancelRpcBridge?.();
52
+ this.#cancelRpcBridge = RPC_MSG_BRIDGE.addListener(this.#onMessage);
53
+ }
54
+ #callError(ev) {
55
+ this.readyState = this.CLOSED;
56
+ this.onerror?.call(this, ev);
57
+ this.dispatchEvent(ev);
58
+ }
59
+ #callClose(ev) {
60
+ this.readyState = this.CLOSED;
61
+ this.onclose?.call(this, ev);
62
+ this.dispatchEvent(ev);
63
+ }
64
+ #onMessage = (msg) => {
65
+ if (msg.type === 'sendWsMessage' && msg.request.id === this.id) {
66
+ const data = msg.request.data;
67
+ const event = new MessageEvent('message', { data });
68
+ this.onmessage?.call(this, event);
69
+ this.dispatchEvent(event);
70
+ return;
71
+ }
72
+ if (msg.type === 'disconnectWs' && msg.request.id === this.id) {
73
+ if (!msg.request.err) {
74
+ this.#callClose(new CloseEvent('close', { code: 1000, reason: 'Normal Closure' }));
75
+ return;
76
+ }
77
+ this.#callError(new ErrorEvent('error', { error: new Error(msg.request.err) }));
78
+ return;
79
+ }
80
+ };
81
+ }
@@ -0,0 +1,33 @@
1
+ import { setCryptoImplementation } from '@reclaimprotocol/tls';
2
+ import { webcryptoCrypto } from '@reclaimprotocol/tls/webcrypto';
3
+ import { handleIncomingMessage } from "./handle-incoming-msg.js";
4
+ import { getWsApiUrlFromBaseUrl } from "./utils.js";
5
+ import { logger, makeLogger } from "../utils/index.js";
6
+ makeLogger(true);
7
+ setCryptoImplementation(webcryptoCrypto);
8
+ /**
9
+ * For browsers only. Sets up the current window to listen for RPC requests
10
+ * from React Native or other windows
11
+ */
12
+ export function setupWindowRpc(baseUrl, channel = 'attestor-core') {
13
+ if (baseUrl) {
14
+ globalThis.ATTESTOR_BASE_URL = baseUrl;
15
+ }
16
+ else if (typeof window !== 'undefined' && window.location) {
17
+ globalThis.ATTESTOR_BASE_URL = window.location.toString();
18
+ }
19
+ else {
20
+ throw new Error('No base URL provided and window.location unavailable');
21
+ }
22
+ if (channel) {
23
+ globalThis.RPC_CHANNEL_NAME = channel;
24
+ }
25
+ else if (!globalThis.RPC_CHANNEL_NAME) {
26
+ throw new Error('No channel name provided and globalThis.RPC_CHANNEL_NAME unavailable');
27
+ }
28
+ if (typeof window !== 'undefined') {
29
+ window.addEventListener('message', ev => handleIncomingMessage(ev.data), false);
30
+ }
31
+ logger.info({ defaultUrl: getWsApiUrlFromBaseUrl() }, 'window RPC setup');
32
+ }
33
+ export * from "../index.js";
@@ -0,0 +1,22 @@
1
+ import "./jsc-polyfills/index.js";
2
+ 1; // done to avoid eslint rule
3
+ import { setCryptoImplementation } from '@reclaimprotocol/tls';
4
+ import { pureJsCrypto } from '@reclaimprotocol/tls/purejs-crypto';
5
+ import * as AttestorRPCImport from "./index.js";
6
+ import { makeLogger } from "../utils/logger.js";
7
+ setCryptoImplementation(pureJsCrypto);
8
+ makeLogger(true);
9
+ /**
10
+ * Sets up the library to run in JS environments like QuickJS or JavascriptCore.
11
+ */
12
+ export function setupJsRpc(baseUrl, channel = 'attestor-core') {
13
+ globalThis.ATTESTOR_BASE_URL = baseUrl;
14
+ globalThis.RPC_CHANNEL_NAME = channel;
15
+ const rpcChannel = {
16
+ postMessage(message) {
17
+ return globalThis.sendMessage(channel, message);
18
+ }
19
+ };
20
+ globalThis[channel] = rpcChannel;
21
+ }
22
+ globalThis.AttestorRPC = { ...AttestorRPCImport, setupJsRpc };
@@ -0,0 +1 @@
1
+ import "../..";
@@ -0,0 +1,100 @@
1
+ import { WS_PATHNAME } from "../config/index.js";
2
+ import { EventBus } from "./event-bus.js";
3
+ import { B64_JSON_REPLACER } from "../utils/b64-json.js";
4
+ import { AttestorError } from "../utils/error.js";
5
+ export const RPC_MSG_BRIDGE = new EventBus();
6
+ // track memory usage
7
+ export async function getCurrentMemoryUsage() {
8
+ if (!window.crossOriginIsolated) {
9
+ return {
10
+ available: false,
11
+ content: 'N/A (page not cross-origin-isolated)'
12
+ };
13
+ }
14
+ else if (!performance.measureUserAgentSpecificMemory) {
15
+ return {
16
+ available: false,
17
+ content: 'N/A (performance.measureUserAgentSpecificMemory() is not available)',
18
+ };
19
+ }
20
+ else {
21
+ try {
22
+ const result = performance.measureUserAgentSpecificMemory();
23
+ const totalmb = Math.round(result.bytes / 1024 / 1024);
24
+ return { available: true, content: `${totalmb}mb` };
25
+ }
26
+ catch (error) {
27
+ if (error instanceof DOMException && error.name === 'SecurityError') {
28
+ return { available: false, content: `N/A (${error.message})` };
29
+ }
30
+ throw error;
31
+ }
32
+ }
33
+ }
34
+ export function generateRpcRequestId() {
35
+ return Math.random().toString(36).slice(2);
36
+ }
37
+ /**
38
+ * The window RPC will be served from the same origin as the API server.
39
+ * so we can get the API server's origin from the location.
40
+ */
41
+ export function getWsApiUrlFromBaseUrl() {
42
+ if (typeof ATTESTOR_BASE_URL !== 'string') {
43
+ throw new Error('ATTESTOR_BASE_URL is not set');
44
+ }
45
+ const parsed = new URL(ATTESTOR_BASE_URL);
46
+ const { host, protocol } = parsed;
47
+ const wsProtocol = protocol === 'https:' ? 'wss:' : 'ws:';
48
+ return `${wsProtocol}//${host}${WS_PATHNAME}`;
49
+ }
50
+ export function rpcRequest(opts) {
51
+ const id = generateRpcRequestId();
52
+ const waitForRes = waitForResponse(opts.type, id);
53
+ // @ts-expect-error
54
+ sendMessageToApp({
55
+ id,
56
+ type: opts.type,
57
+ request: opts.request,
58
+ });
59
+ return waitForRes;
60
+ }
61
+ export function waitForResponse(type, requestId, timeoutMs = 60_000) {
62
+ const returnType = `${type}Done`;
63
+ return new Promise((resolve, reject) => {
64
+ const timeout = setTimeout(() => {
65
+ reject(new AttestorError('ERROR_INTERNAL', `Timeout waiting for response: ${type}`, { requestId }));
66
+ cancel();
67
+ }, timeoutMs);
68
+ const cancel = RPC_MSG_BRIDGE.addListener(msg => {
69
+ if (msg.id !== requestId) {
70
+ return;
71
+ }
72
+ if (msg.type === 'error') {
73
+ reject(new Error(msg.data.message));
74
+ }
75
+ else if (msg.type === returnType) {
76
+ resolve(msg.response);
77
+ }
78
+ else {
79
+ return;
80
+ }
81
+ clearTimeout(timeout);
82
+ cancel();
83
+ });
84
+ });
85
+ }
86
+ /**
87
+ * Sends a message back to the host app
88
+ * @param data
89
+ */
90
+ export function sendMessageToApp(data) {
91
+ const str = JSON.stringify(data, B64_JSON_REPLACER);
92
+ if (!RPC_CHANNEL_NAME) {
93
+ throw new Error('global RPC_CHANNEL_NAME is not set');
94
+ }
95
+ const channel = globalThis[RPC_CHANNEL_NAME];
96
+ if (!channel) {
97
+ throw new Error(`RPC channel ${RPC_CHANNEL_NAME} not set on globalThis`);
98
+ }
99
+ channel.postMessage(str);
100
+ }
@@ -0,0 +1,63 @@
1
+ import { rpcRequest } from "./utils.js";
2
+ import { logger, makeDefaultZkOperator } from "../utils/index.js";
3
+ export const ALL_ENC_ALGORITHMS = [
4
+ 'aes-256-ctr',
5
+ 'aes-128-ctr',
6
+ 'chacha20',
7
+ ];
8
+ /**
9
+ * The goal of this RPC operator is if the attestor client
10
+ * is running in a WebView, it can call the native
11
+ * application to perform the ZK operations
12
+ */
13
+ export function makeExternalRpcZkOperator(algorithm, zkEngine = 'snarkjs') {
14
+ return {
15
+ async generateWitness(input) {
16
+ const operator = await makeDefaultZkOperator(algorithm, zkEngine, logger);
17
+ return operator.generateWitness(input);
18
+ },
19
+ groth16Prove(input) {
20
+ return callFnZk({ fn: 'groth16Prove', args: [input] });
21
+ },
22
+ groth16Verify(publicSignals, proof) {
23
+ return callFnZk({ fn: 'groth16Verify', args: [publicSignals, proof] });
24
+ },
25
+ };
26
+ }
27
+ function callFnZk(request) {
28
+ return rpcRequest({ type: 'executeZkFunctionV3', request });
29
+ }
30
+ /**
31
+ * The goal of this RPC operator is if the attestor client
32
+ * is running in a WebView, it can call the native
33
+ * application to perform the OPRF operations
34
+ */
35
+ export function makeExternalRpcOprfOperator(algorithm, zkEngine = 'snarkjs') {
36
+ return {
37
+ async generateWitness(input) {
38
+ const operator = await makeDefaultZkOperator(algorithm, zkEngine, logger);
39
+ return operator.generateWitness(input);
40
+ },
41
+ groth16Prove(input) {
42
+ return callFnOprf({ fn: 'groth16Prove', args: [input] });
43
+ },
44
+ groth16Verify(publicSignals, proof) {
45
+ return callFnOprf({ fn: 'groth16Verify', args: [publicSignals, proof] });
46
+ },
47
+ generateThresholdKeys(total, threshold) {
48
+ return callFnOprf({ fn: 'generateThresholdKeys', args: [total, threshold] });
49
+ },
50
+ generateOPRFRequestData(data, domainSeparator) {
51
+ return callFnOprf({ fn: 'generateOPRFRequestData', args: [data, domainSeparator] });
52
+ },
53
+ finaliseOPRF(serverPublicKey, request, responses) {
54
+ return callFnOprf({ fn: 'finaliseOPRF', args: [serverPublicKey, request, responses] });
55
+ },
56
+ evaluateOPRF(serverPrivateKey, request) {
57
+ return callFnOprf({ fn: 'evaluateOPRF', args: [serverPrivateKey, request] });
58
+ },
59
+ };
60
+ }
61
+ function callFnOprf(request) {
62
+ return rpcRequest({ type: 'executeOprfFunctionV3', request });
63
+ }