@reclaimprotocol/attestor-core 5.0.1-beta.2 → 5.0.1-beta.22

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 (145) hide show
  1. package/browser/resources/attestor-browser.min.mjs +4512 -0
  2. package/lib/avs/abis/avsDirectoryABI.js +338 -341
  3. package/lib/avs/abis/delegationABI.js +1 -4
  4. package/lib/avs/abis/registryABI.js +719 -722
  5. package/lib/avs/client/create-claim-on-avs.js +129 -157
  6. package/lib/avs/config.js +18 -24
  7. package/lib/avs/contracts/ReclaimServiceManager.js +1 -0
  8. package/lib/avs/contracts/common.js +1 -0
  9. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1139 -1156
  10. package/lib/avs/contracts/factories/index.js +4 -4
  11. package/lib/avs/contracts/index.js +2 -6
  12. package/lib/avs/types/index.js +1 -0
  13. package/lib/avs/utils/contracts.js +30 -50
  14. package/lib/avs/utils/register.js +75 -70
  15. package/lib/avs/utils/tasks.js +38 -45
  16. package/lib/client/create-claim.js +402 -431
  17. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +46 -48
  18. package/lib/client/tunnels/make-rpc-tls-tunnel.js +125 -121
  19. package/lib/client/utils/attestor-pool.js +23 -22
  20. package/lib/client/utils/client-socket.js +86 -109
  21. package/lib/client/utils/message-handler.js +79 -89
  22. package/lib/config/index.js +40 -58
  23. package/lib/external-rpc/benchmark.js +61 -74
  24. package/lib/external-rpc/event-bus.js +12 -15
  25. package/lib/external-rpc/handle-incoming-msg.js +216 -225
  26. package/lib/external-rpc/jsc-polyfills/1.js +70 -68
  27. package/lib/external-rpc/jsc-polyfills/2.js +17 -12
  28. package/lib/external-rpc/jsc-polyfills/event.js +10 -15
  29. package/lib/external-rpc/jsc-polyfills/index.js +2 -2
  30. package/lib/external-rpc/jsc-polyfills/ws.js +77 -79
  31. package/lib/external-rpc/setup-browser.js +28 -28
  32. package/lib/external-rpc/setup-jsc.js +17 -17
  33. package/lib/external-rpc/types.js +1 -0
  34. package/lib/external-rpc/utils.js +89 -89
  35. package/lib/external-rpc/zk.js +55 -50
  36. package/lib/index.js +2 -6
  37. package/lib/mechain/abis/governanceABI.js +457 -460
  38. package/lib/mechain/abis/taskABI.js +502 -505
  39. package/lib/mechain/client/create-claim-on-mechain.js +24 -29
  40. package/lib/mechain/constants/index.js +3 -8
  41. package/lib/mechain/types/index.js +1 -0
  42. package/lib/proto/api.js +4200 -4087
  43. package/lib/proto/tee-bundle.js +1261 -1241
  44. package/lib/providers/http/index.js +616 -603
  45. package/lib/providers/http/patch-parse5-tree.js +27 -29
  46. package/lib/providers/http/utils.js +289 -248
  47. package/lib/providers/index.js +3 -6
  48. package/lib/server/create-server.js +89 -91
  49. package/lib/server/handlers/claimTeeBundle.js +231 -211
  50. package/lib/server/handlers/claimTunnel.js +66 -73
  51. package/lib/server/handlers/completeClaimOnChain.js +20 -25
  52. package/lib/server/handlers/createClaimOnChain.js +21 -27
  53. package/lib/server/handlers/createTaskOnMechain.js +40 -50
  54. package/lib/server/handlers/createTunnel.js +85 -90
  55. package/lib/server/handlers/disconnectTunnel.js +4 -7
  56. package/lib/server/handlers/fetchCertificateBytes.js +37 -53
  57. package/lib/server/handlers/index.js +21 -24
  58. package/lib/server/handlers/init.js +27 -28
  59. package/lib/server/handlers/toprf.js +13 -16
  60. package/lib/server/socket.js +97 -100
  61. package/lib/server/tunnels/make-tcp-tunnel.js +161 -186
  62. package/lib/server/utils/apm.js +32 -25
  63. package/lib/server/utils/assert-valid-claim-request.js +305 -334
  64. package/lib/server/utils/config-env.js +2 -2
  65. package/lib/server/utils/dns.js +12 -18
  66. package/lib/server/utils/gcp-attestation.js +233 -181
  67. package/lib/server/utils/generics.d.ts +1 -1
  68. package/lib/server/utils/generics.js +43 -37
  69. package/lib/server/utils/iso.js +253 -256
  70. package/lib/server/utils/keep-alive.js +36 -36
  71. package/lib/server/utils/nitro-attestation.js +295 -220
  72. package/lib/server/utils/oprf-raw.js +48 -55
  73. package/lib/server/utils/process-handshake.js +200 -218
  74. package/lib/server/utils/proxy-session.js +5 -5
  75. package/lib/server/utils/tee-oprf-mpc-verification.js +82 -78
  76. package/lib/server/utils/tee-oprf-verification.js +165 -142
  77. package/lib/server/utils/tee-transcript-reconstruction.js +176 -129
  78. package/lib/server/utils/tee-verification.js +397 -334
  79. package/lib/server/utils/validation.js +30 -37
  80. package/lib/types/bgp.js +1 -0
  81. package/lib/types/claims.js +1 -0
  82. package/lib/types/client.js +1 -0
  83. package/lib/types/general.js +1 -0
  84. package/lib/types/handlers.js +1 -0
  85. package/lib/types/providers.d.ts +3 -2
  86. package/lib/types/providers.gen.js +9 -15
  87. package/lib/types/providers.js +1 -0
  88. package/lib/types/rpc.js +1 -0
  89. package/lib/types/signatures.d.ts +1 -2
  90. package/lib/types/signatures.js +1 -0
  91. package/lib/types/tunnel.js +1 -0
  92. package/lib/types/zk.js +1 -0
  93. package/lib/utils/auth.js +54 -66
  94. package/lib/utils/b64-json.js +15 -15
  95. package/lib/utils/bgp-listener.js +107 -111
  96. package/lib/utils/claims.js +89 -80
  97. package/lib/utils/env.js +13 -17
  98. package/lib/utils/error.js +43 -47
  99. package/lib/utils/generics.js +284 -235
  100. package/lib/utils/http-parser.js +232 -187
  101. package/lib/utils/logger.js +80 -71
  102. package/lib/utils/prepare-packets.js +69 -67
  103. package/lib/utils/redactions.js +163 -121
  104. package/lib/utils/retries.js +22 -24
  105. package/lib/utils/signatures/eth.js +29 -28
  106. package/lib/utils/signatures/index.js +5 -10
  107. package/lib/utils/socket-base.js +84 -88
  108. package/lib/utils/tls.js +28 -28
  109. package/lib/utils/ws.js +19 -19
  110. package/lib/utils/zk.js +542 -582
  111. package/package.json +12 -5
  112. package/lib/external-rpc/global.d.js +0 -0
  113. package/lib/scripts/build-browser.d.ts +0 -1
  114. package/lib/scripts/build-jsc.d.ts +0 -1
  115. package/lib/scripts/build-lib.d.ts +0 -1
  116. package/lib/scripts/check-avs-registration.d.ts +0 -1
  117. package/lib/scripts/check-avs-registration.js +0 -28
  118. package/lib/scripts/fallbacks/crypto.d.ts +0 -1
  119. package/lib/scripts/fallbacks/crypto.js +0 -4
  120. package/lib/scripts/fallbacks/empty.d.ts +0 -3
  121. package/lib/scripts/fallbacks/empty.js +0 -4
  122. package/lib/scripts/fallbacks/re2.d.ts +0 -1
  123. package/lib/scripts/fallbacks/re2.js +0 -7
  124. package/lib/scripts/fallbacks/snarkjs.d.ts +0 -1
  125. package/lib/scripts/fallbacks/snarkjs.js +0 -10
  126. package/lib/scripts/fallbacks/stwo.d.ts +0 -6
  127. package/lib/scripts/fallbacks/stwo.js +0 -159
  128. package/lib/scripts/generate-provider-types.d.ts +0 -5
  129. package/lib/scripts/generate-provider-types.js +0 -101
  130. package/lib/scripts/generate-receipt.d.ts +0 -9
  131. package/lib/scripts/generate-receipt.js +0 -101
  132. package/lib/scripts/generate-toprf-keys.d.ts +0 -1
  133. package/lib/scripts/generate-toprf-keys.js +0 -24
  134. package/lib/scripts/jsc-cli-rpc.d.ts +0 -1
  135. package/lib/scripts/jsc-cli-rpc.js +0 -35
  136. package/lib/scripts/register-avs-operator.d.ts +0 -1
  137. package/lib/scripts/register-avs-operator.js +0 -3
  138. package/lib/scripts/start-server.d.ts +0 -1
  139. package/lib/scripts/start-server.js +0 -11
  140. package/lib/scripts/update-avs-metadata.d.ts +0 -1
  141. package/lib/scripts/update-avs-metadata.js +0 -20
  142. package/lib/scripts/utils.d.ts +0 -1
  143. package/lib/scripts/utils.js +0 -10
  144. package/lib/scripts/whitelist-operator.d.ts +0 -1
  145. package/lib/scripts/whitelist-operator.js +0 -16
@@ -1,19 +1,14 @@
1
- class EventPolyfill {
2
- type;
3
- constructor(type, eventInitDict) {
4
- this.type = type;
5
- Object.assign(this, eventInitDict);
6
- }
1
+ // @ts-expect-error
2
+ export class EventPolyfill {
3
+ type;
4
+ constructor(type, eventInitDict) {
5
+ this.type = type;
6
+ Object.assign(this, eventInitDict);
7
+ }
7
8
  }
8
- class ErrorEventPolyfill extends EventPolyfill {
9
+ export class ErrorEventPolyfill extends EventPolyfill {
9
10
  }
10
- class CloseEventPolyfill extends EventPolyfill {
11
+ export class CloseEventPolyfill extends EventPolyfill {
11
12
  }
12
- class MessageEventPolyfill extends EventPolyfill {
13
+ export class MessageEventPolyfill extends EventPolyfill {
13
14
  }
14
- export {
15
- CloseEventPolyfill,
16
- ErrorEventPolyfill,
17
- EventPolyfill,
18
- MessageEventPolyfill
19
- };
@@ -1,2 +1,2 @@
1
- import "../../external-rpc/jsc-polyfills/1.js";
2
- import "../../external-rpc/jsc-polyfills/2.js";
1
+ import "./1.js";
2
+ import "./2.js";
@@ -1,83 +1,81 @@
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");
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 })));
32
30
  }
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;
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 } });
66
37
  }
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;
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 })));
78
46
  }
79
- };
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
+ };
80
81
  }
81
- export {
82
- RPCWebSocket
83
- };
@@ -1,33 +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";
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
5
  import { logger, makeLogger } from "../utils/index.js";
6
6
  makeLogger(true);
7
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");
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');
29
32
  }
30
33
  export * from "../index.js";
31
- export {
32
- setupWindowRpc
33
- };
@@ -1,22 +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";
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
6
  import { makeLogger } from "../utils/logger.js";
7
7
  setCryptoImplementation(pureJsCrypto);
8
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;
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;
18
21
  }
19
22
  globalThis.AttestorRPC = { ...AttestorRPCImport, setupJsRpc };
20
- export {
21
- setupJsRpc
22
- };
@@ -0,0 +1 @@
1
+ import "../..";
@@ -1,100 +1,100 @@
1
1
  import { WS_PATHNAME } from "../config/index.js";
2
- import { EventBus } from "../external-rpc/event-bus.js";
2
+ import { EventBus } from "./event-bus.js";
3
3
  import { B64_JSON_REPLACER } from "../utils/b64-json.js";
4
4
  import { AttestorError } from "../utils/error.js";
5
- const RPC_MSG_BRIDGE = new EventBus();
6
- async function getCurrentMemoryUsage() {
7
- if (!window.crossOriginIsolated) {
8
- return {
9
- available: false,
10
- content: "N/A (page not cross-origin-isolated)"
11
- };
12
- } else if (!performance.measureUserAgentSpecificMemory) {
13
- return {
14
- available: false,
15
- content: "N/A (performance.measureUserAgentSpecificMemory() is not available)"
16
- };
17
- } else {
18
- try {
19
- const result = performance.measureUserAgentSpecificMemory();
20
- const totalmb = Math.round(result.bytes / 1024 / 1024);
21
- return { available: true, content: `${totalmb}mb` };
22
- } catch (error) {
23
- if (error instanceof DOMException && error.name === "SecurityError") {
24
- return { available: false, content: `N/A (${error.message})` };
25
- }
26
- throw error;
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
+ }
27
32
  }
28
- }
29
33
  }
30
- function generateRpcRequestId() {
31
- return Math.random().toString(36).slice(2);
34
+ export function generateRpcRequestId() {
35
+ return Math.random().toString(36).slice(2);
32
36
  }
33
- function getWsApiUrlFromBaseUrl() {
34
- if (typeof ATTESTOR_BASE_URL !== "string") {
35
- throw new Error("ATTESTOR_BASE_URL is not set");
36
- }
37
- const parsed = new URL(ATTESTOR_BASE_URL);
38
- const { host, protocol } = parsed;
39
- const wsProtocol = protocol === "https:" ? "wss:" : "ws:";
40
- return `${wsProtocol}//${host}${WS_PATHNAME}`;
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}`;
41
49
  }
42
- function rpcRequest(opts) {
43
- const id = generateRpcRequestId();
44
- const waitForRes = waitForResponse(opts.type, id);
45
- sendMessageToApp({
46
- id,
47
- type: opts.type,
48
- request: opts.request
49
- });
50
- return waitForRes;
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;
51
60
  }
52
- function waitForResponse(type, requestId, timeoutMs = 6e4) {
53
- const returnType = `${type}Done`;
54
- return new Promise((resolve, reject) => {
55
- const timeout = setTimeout(() => {
56
- reject(
57
- new AttestorError(
58
- "ERROR_INTERNAL",
59
- `Timeout waiting for response: ${type}`,
60
- { requestId }
61
- )
62
- );
63
- cancel();
64
- }, timeoutMs);
65
- const cancel = RPC_MSG_BRIDGE.addListener((msg) => {
66
- if (msg.id !== requestId) {
67
- return;
68
- }
69
- if (msg.type === "error") {
70
- reject(new Error(msg.data.message));
71
- } else if (msg.type === returnType) {
72
- resolve(msg.response);
73
- } else {
74
- return;
75
- }
76
- clearTimeout(timeout);
77
- cancel();
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
+ });
78
84
  });
79
- });
80
85
  }
81
- function sendMessageToApp(data) {
82
- const str = JSON.stringify(data, B64_JSON_REPLACER);
83
- if (!RPC_CHANNEL_NAME) {
84
- throw new Error("global RPC_CHANNEL_NAME is not set");
85
- }
86
- const channel = globalThis[RPC_CHANNEL_NAME];
87
- if (!channel) {
88
- throw new Error(`RPC channel ${RPC_CHANNEL_NAME} not set on globalThis`);
89
- }
90
- channel.postMessage(str);
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);
91
100
  }
92
- export {
93
- RPC_MSG_BRIDGE,
94
- generateRpcRequestId,
95
- getCurrentMemoryUsage,
96
- getWsApiUrlFromBaseUrl,
97
- rpcRequest,
98
- sendMessageToApp,
99
- waitForResponse
100
- };
@@ -1,58 +1,63 @@
1
- import { rpcRequest } from "../external-rpc/utils.js";
1
+ import { rpcRequest } from "./utils.js";
2
2
  import { logger, makeDefaultZkOperator } from "../utils/index.js";
3
- const ALL_ENC_ALGORITHMS = [
4
- "aes-256-ctr",
5
- "aes-128-ctr",
6
- "chacha20"
3
+ export const ALL_ENC_ALGORITHMS = [
4
+ 'aes-256-ctr',
5
+ 'aes-128-ctr',
6
+ 'chacha20',
7
7
  ];
8
- function makeExternalRpcZkOperator(algorithm, zkEngine = "snarkjs") {
9
- return {
10
- async generateWitness(input) {
11
- const operator = await makeDefaultZkOperator(algorithm, zkEngine, logger);
12
- return operator.generateWitness(input);
13
- },
14
- groth16Prove(input) {
15
- return callFnZk({ fn: "groth16Prove", args: [input] });
16
- },
17
- groth16Verify(publicSignals, proof) {
18
- return callFnZk({ fn: "groth16Verify", args: [publicSignals, proof] });
19
- }
20
- };
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
+ };
21
26
  }
22
27
  function callFnZk(request) {
23
- return rpcRequest({ type: "executeZkFunctionV3", request });
28
+ return rpcRequest({ type: 'executeZkFunctionV3', request });
24
29
  }
25
- function makeExternalRpcOprfOperator(algorithm, zkEngine = "snarkjs") {
26
- return {
27
- async generateWitness(input) {
28
- const operator = await makeDefaultZkOperator(algorithm, zkEngine, logger);
29
- return operator.generateWitness(input);
30
- },
31
- groth16Prove(input) {
32
- return callFnOprf({ fn: "groth16Prove", args: [input] });
33
- },
34
- groth16Verify(publicSignals, proof) {
35
- return callFnOprf({ fn: "groth16Verify", args: [publicSignals, proof] });
36
- },
37
- generateThresholdKeys(total, threshold) {
38
- return callFnOprf({ fn: "generateThresholdKeys", args: [total, threshold] });
39
- },
40
- generateOPRFRequestData(data, domainSeparator) {
41
- return callFnOprf({ fn: "generateOPRFRequestData", args: [data, domainSeparator] });
42
- },
43
- finaliseOPRF(serverPublicKey, request, responses) {
44
- return callFnOprf({ fn: "finaliseOPRF", args: [serverPublicKey, request, responses] });
45
- },
46
- evaluateOPRF(serverPrivateKey, request) {
47
- return callFnOprf({ fn: "evaluateOPRF", args: [serverPrivateKey, request] });
48
- }
49
- };
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
+ };
50
60
  }
51
61
  function callFnOprf(request) {
52
- return rpcRequest({ type: "executeOprfFunctionV3", request });
62
+ return rpcRequest({ type: 'executeOprfFunctionV3', request });
53
63
  }
54
- export {
55
- ALL_ENC_ALGORITHMS,
56
- makeExternalRpcOprfOperator,
57
- makeExternalRpcZkOperator
58
- };
package/lib/index.js CHANGED
@@ -1,13 +1,9 @@
1
- import * as tls from "@reclaimprotocol/tls";
1
+ export * as tls from '@reclaimprotocol/tls';
2
2
  export * from "./client/utils/client-socket.js";
3
3
  export * from "./config/index.js";
4
4
  export * from "./types/index.js";
5
5
  export * from "./utils/index.js";
6
6
  export * from "./providers/index.js";
7
- import * as proto from "./proto/api.js";
7
+ export * as proto from "./proto/api.js";
8
8
  export * from "./client/index.js";
9
9
  export * from "./mechain/index.js";
10
- export {
11
- proto,
12
- tls
13
- };