@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,97 +1,87 @@
1
1
  import { RPCMessages } from "../../proto/api.js";
2
2
  import { AttestorError, extractArrayBufferFromWsData, getRpcRequest, getRpcRequestType, getRpcResponseType } from "../../utils/index.js";
3
- async function wsMessageHandler(data) {
4
- const buff = await extractArrayBufferFromWsData(data);
5
- const { messages } = RPCMessages.decode(buff);
6
- for (const msg of messages) {
7
- await handleMessage.call(this, msg);
8
- }
9
- }
10
- function handleMessage(msg) {
11
- this.logger?.trace({ msg }, "received message");
12
- if (msg.connectionTerminationAlert) {
13
- const err = AttestorError.fromProto(
14
- msg.connectionTerminationAlert
15
- );
16
- this.logger?.warn(
17
- {
18
- err: err.code !== "ERROR_NO_ERROR" ? err : void 0
19
- },
20
- "received connection termination alert"
21
- );
22
- this.dispatchRPCEvent("connection-terminated", err);
23
- return;
24
- }
25
- const rpcRequest = getRpcRequest(msg);
26
- if (rpcRequest) {
27
- if (rpcRequest.direction === "response" && rpcRequest.type === "error") {
28
- this.dispatchRPCEvent("rpc-response", {
29
- id: msg.id,
30
- error: AttestorError.fromProto(msg.requestError)
31
- });
32
- return;
33
- }
34
- const resType = getRpcResponseType(rpcRequest.type);
35
- if (rpcRequest.direction === "response") {
36
- this.dispatchRPCEvent("rpc-response", {
37
- id: msg.id,
38
- type: rpcRequest.type,
39
- data: msg[resType]
40
- });
41
- return;
3
+ export async function wsMessageHandler(data) {
4
+ // extract array buffer from WS data & decode proto
5
+ const buff = await extractArrayBufferFromWsData(data);
6
+ const { messages } = RPCMessages.decode(buff);
7
+ for (const msg of messages) {
8
+ await handleMessage.call(this, msg);
42
9
  }
43
- if (!this.isInitialised && rpcRequest.type !== "init") {
44
- this.logger.warn(
45
- { type: rpcRequest.type },
46
- "RPC request received before initialisation"
47
- );
48
- void this.sendMessage({
49
- id: msg.id,
50
- requestError: AttestorError.badRequest("Initialise connection first").toProto()
51
- });
52
- return;
10
+ }
11
+ export function handleMessage(msg) {
12
+ this.logger?.trace({ msg }, 'received message');
13
+ // handle connection termination alert
14
+ if (msg.connectionTerminationAlert) {
15
+ const err = AttestorError.fromProto(msg.connectionTerminationAlert);
16
+ this.logger?.warn({
17
+ err: err.code !== 'ERROR_NO_ERROR'
18
+ ? err
19
+ : undefined
20
+ }, 'received connection termination alert');
21
+ this.dispatchRPCEvent('connection-terminated', err);
22
+ return;
53
23
  }
54
- return new Promise((resolve, reject) => {
55
- this.dispatchRPCEvent("rpc-request", {
56
- requestId: msg.id,
57
- type: rpcRequest.type,
58
- data: msg[getRpcRequestType(rpcRequest.type)],
59
- respond: (res) => {
60
- if (!this.isOpen) {
61
- this.logger?.debug(
62
- { type: rpcRequest.type, res },
63
- "connection closed before responding"
64
- );
65
- reject(new Error("connection closed"));
24
+ const rpcRequest = getRpcRequest(msg);
25
+ if (rpcRequest) {
26
+ if (rpcRequest.direction === 'response'
27
+ && rpcRequest.type === 'error') {
28
+ this.dispatchRPCEvent('rpc-response', {
29
+ id: msg.id,
30
+ error: AttestorError.fromProto(msg.requestError)
31
+ });
66
32
  return;
67
- }
68
- if ("code" in res) {
69
- reject(res);
70
- return this.sendMessage({
71
- id: msg.id,
72
- requestError: res.toProto()
33
+ }
34
+ const resType = getRpcResponseType(rpcRequest.type);
35
+ if (rpcRequest.direction === 'response') {
36
+ this.dispatchRPCEvent('rpc-response', {
37
+ id: msg.id,
38
+ type: rpcRequest.type,
39
+ data: msg[resType]
73
40
  });
74
- }
75
- resolve();
76
- return this.sendMessage({ id: msg.id, [resType]: res });
41
+ return;
77
42
  }
78
- });
79
- });
80
- }
81
- if (msg.tunnelMessage) {
82
- this.dispatchRPCEvent("tunnel-message", msg.tunnelMessage);
83
- return;
84
- }
85
- if (msg.tunnelDisconnectEvent) {
86
- this.dispatchRPCEvent(
87
- "tunnel-disconnect-event",
88
- msg.tunnelDisconnectEvent
89
- );
90
- return;
91
- }
92
- this.logger.warn({ msg }, "unhandled message");
43
+ if (!this.isInitialised && rpcRequest.type !== 'init') {
44
+ this.logger.warn({ type: rpcRequest.type }, 'RPC request received before initialisation');
45
+ void this.sendMessage({
46
+ id: msg.id,
47
+ requestError: AttestorError
48
+ .badRequest('Initialise connection first')
49
+ .toProto()
50
+ });
51
+ return;
52
+ }
53
+ return new Promise((resolve, reject) => {
54
+ this.dispatchRPCEvent('rpc-request', {
55
+ requestId: msg.id,
56
+ type: rpcRequest.type,
57
+ data: msg[getRpcRequestType(rpcRequest.type)],
58
+ respond: (res) => {
59
+ if (!this.isOpen) {
60
+ this.logger?.debug({ type: rpcRequest.type, res }, 'connection closed before responding');
61
+ reject(new Error('connection closed'));
62
+ return;
63
+ }
64
+ if ('code' in res) {
65
+ reject(res);
66
+ return this.sendMessage({
67
+ id: msg.id,
68
+ requestError: res.toProto()
69
+ });
70
+ }
71
+ resolve();
72
+ return this
73
+ .sendMessage({ id: msg.id, [resType]: res });
74
+ },
75
+ });
76
+ });
77
+ }
78
+ if (msg.tunnelMessage) {
79
+ this.dispatchRPCEvent('tunnel-message', msg.tunnelMessage);
80
+ return;
81
+ }
82
+ if (msg.tunnelDisconnectEvent) {
83
+ this.dispatchRPCEvent('tunnel-disconnect-event', msg.tunnelDisconnectEvent);
84
+ return;
85
+ }
86
+ this.logger.warn({ msg }, 'unhandled message');
93
87
  }
94
- export {
95
- handleMessage,
96
- wsMessageHandler
97
- };
@@ -1,62 +1,44 @@
1
1
  import { AttestorVersion, ServiceSignatureType } from "../proto/api.js";
2
- const DEFAULT_ZK_CONCURRENCY = 10;
3
- const RECLAIM_USER_AGENT = "reclaim/0.0.1";
4
- const DEFAULT_HTTPS_PORT = 443;
5
- const WS_PATHNAME = "/ws";
6
- const BROWSER_RPC_PATHNAME = "/browser-rpc";
7
- const ATTESTOR_ADDRESS_PATHNAME = "/address";
8
- const DEFAULT_REMOTE_FILE_FETCH_BASE_URL = `${BROWSER_RPC_PATHNAME}/resources`;
9
- const API_SERVER_PORT = 8001;
10
- const CONNECTION_TIMEOUT_MS = 1e4;
11
- const DNS_SERVERS = [
12
- "8.8.8.8",
13
- "8.8.4.4"
2
+ export const DEFAULT_ZK_CONCURRENCY = 10;
3
+ export const RECLAIM_USER_AGENT = 'reclaim/0.0.1';
4
+ export const DEFAULT_HTTPS_PORT = 443;
5
+ export const WS_PATHNAME = '/ws';
6
+ export const BROWSER_RPC_PATHNAME = '/browser-rpc';
7
+ export const ATTESTOR_ADDRESS_PATHNAME = '/address';
8
+ export const DEFAULT_REMOTE_FILE_FETCH_BASE_URL = `${BROWSER_RPC_PATHNAME}/resources`;
9
+ export const API_SERVER_PORT = 8001;
10
+ // 10s
11
+ export const CONNECTION_TIMEOUT_MS = 10_000;
12
+ export const DNS_SERVERS = [
13
+ '8.8.8.8',
14
+ '8.8.4.4'
14
15
  ];
15
- const MAX_CLAIM_TIMESTAMP_DIFF_S = 10 * 60;
16
- const CURRENT_ATTESTOR_VERSION = AttestorVersion.ATTESTOR_VERSION_3_0_0;
17
- const DEFAULT_METADATA = {
18
- signatureType: ServiceSignatureType.SERVICE_SIGNATURE_TYPE_ETH,
19
- clientVersion: CURRENT_ATTESTOR_VERSION,
20
- auth: void 0
16
+ // 10m
17
+ export const MAX_CLAIM_TIMESTAMP_DIFF_S = 10 * 60;
18
+ export const CURRENT_ATTESTOR_VERSION = AttestorVersion.ATTESTOR_VERSION_3_0_0;
19
+ export const DEFAULT_METADATA = {
20
+ signatureType: ServiceSignatureType.SERVICE_SIGNATURE_TYPE_ETH,
21
+ clientVersion: CURRENT_ATTESTOR_VERSION,
22
+ auth: undefined
21
23
  };
22
- const PROVIDER_CTX = { version: CURRENT_ATTESTOR_VERSION };
23
- const PING_INTERVAL_MS = 1e4;
24
- const MAX_NO_DATA_INTERVAL_MS = 3e4;
25
- const MAX_PAYLOAD_SIZE = 512 * 1024 * 1024;
26
- const DEFAULT_AUTH_EXPIRY_S = 15 * 60;
27
- const DEFAULT_RPC_TIMEOUT_MS = 9e4;
28
- const TOPRF_DOMAIN_SEPARATOR = "reclaim-toprf";
29
- const MAX_CERT_SIZE_BYTES = 10 * 1024 * 1024 * 1024;
30
- const CERT_ALLOWED_MIMETYPES = [
31
- "application/x-x509-ca-cert",
32
- "application/x-x509-user-cert",
33
- "application/pkix-cert",
34
- "application/pkcs7-mime",
35
- "application/octet-stream"
24
+ export const PROVIDER_CTX = { version: CURRENT_ATTESTOR_VERSION };
25
+ export const PING_INTERVAL_MS = 10_000;
26
+ /**
27
+ * Maximum interval in seconds to wait for before assuming
28
+ * the connection is dead
29
+ * @default 30s
30
+ */
31
+ export const MAX_NO_DATA_INTERVAL_MS = 30_000;
32
+ export const MAX_PAYLOAD_SIZE = 512 * 1024 * 1024; // 512MB
33
+ export const DEFAULT_AUTH_EXPIRY_S = 15 * 60; // 15m
34
+ export const DEFAULT_RPC_TIMEOUT_MS = 90_000;
35
+ export const TOPRF_DOMAIN_SEPARATOR = 'reclaim-toprf';
36
+ export const MAX_CERT_SIZE_BYTES = 10 * 1024 * 1024 * 1024; // 10MB
37
+ export const CERT_ALLOWED_MIMETYPES = [
38
+ 'application/x-x509-ca-cert',
39
+ 'application/x-x509-user-cert',
40
+ 'application/pkix-cert',
41
+ 'application/pkcs7-mime',
42
+ 'application/octet-stream'
36
43
  ];
37
- const BGP_WS_URL = "wss://ris-live.ripe.net/v1/ws/?client=reclaim-hijack-detector";
38
- export {
39
- API_SERVER_PORT,
40
- ATTESTOR_ADDRESS_PATHNAME,
41
- BGP_WS_URL,
42
- BROWSER_RPC_PATHNAME,
43
- CERT_ALLOWED_MIMETYPES,
44
- CONNECTION_TIMEOUT_MS,
45
- CURRENT_ATTESTOR_VERSION,
46
- DEFAULT_AUTH_EXPIRY_S,
47
- DEFAULT_HTTPS_PORT,
48
- DEFAULT_METADATA,
49
- DEFAULT_REMOTE_FILE_FETCH_BASE_URL,
50
- DEFAULT_RPC_TIMEOUT_MS,
51
- DEFAULT_ZK_CONCURRENCY,
52
- DNS_SERVERS,
53
- MAX_CERT_SIZE_BYTES,
54
- MAX_CLAIM_TIMESTAMP_DIFF_S,
55
- MAX_NO_DATA_INTERVAL_MS,
56
- MAX_PAYLOAD_SIZE,
57
- PING_INTERVAL_MS,
58
- PROVIDER_CTX,
59
- RECLAIM_USER_AGENT,
60
- TOPRF_DOMAIN_SEPARATOR,
61
- WS_PATHNAME
62
- };
44
+ export const BGP_WS_URL = 'wss://ris-live.ripe.net/v1/ws/?client=reclaim-hijack-detector';
@@ -1,82 +1,69 @@
1
- import {
2
- crypto,
3
- encryptWrappedRecord,
4
- SUPPORTED_CIPHER_SUITE_MAP
5
- } from "@reclaimprotocol/tls";
1
+ import { crypto, encryptWrappedRecord, SUPPORTED_CIPHER_SUITE_MAP } from '@reclaimprotocol/tls';
6
2
  import { strToUint8Array } from "../utils/generics.js";
7
3
  import { logger } from "../utils/logger.js";
8
4
  import { makeZkProofGenerator } from "../utils/zk.js";
9
5
  const ZK_CIPHER_SUITES = [
10
- "TLS_CHACHA20_POLY1305_SHA256",
11
- "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
12
- "TLS_AES_128_GCM_SHA256"
6
+ 'TLS_CHACHA20_POLY1305_SHA256',
7
+ 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384',
8
+ 'TLS_AES_128_GCM_SHA256'
13
9
  ];
14
- async function benchmark() {
15
- let benchmarkRes = "";
16
- for (const cipherSuite of ZK_CIPHER_SUITES) {
17
- const now = Date.now();
18
- const alg = cipherSuite.includes("CHACHA20") ? "CHACHA20-POLY1305" : cipherSuite.includes("AES_256_GCM") ? "AES-256-GCM" : "AES-128-GCM";
19
- const keylength = alg === "AES-128-GCM" ? 16 : 32;
20
- const key = Buffer.alloc(keylength, 0);
21
- const {
22
- ivLength: fixedIvLength
23
- } = SUPPORTED_CIPHER_SUITE_MAP[cipherSuite];
24
- const fixedIv = Buffer.alloc(fixedIvLength, 0);
25
- const encKey = await crypto.importKey(alg, key);
26
- const vectors = [
27
- {
28
- plaintext: `My cool API secret is "my name jeff". Please don't reveal it`
29
- }
30
- ];
31
- const proofGenerator = await makeZkProofGenerator({
32
- logger,
33
- cipherSuite
34
- });
35
- for (const { plaintext } of vectors) {
36
- const plaintextArr = strToUint8Array(plaintext);
37
- const { ciphertext, iv } = await encryptWrappedRecord(
38
- plaintextArr,
39
- {
40
- key: encKey,
41
- iv: fixedIv,
42
- recordNumber: 0,
43
- recordHeaderOpts: {
44
- type: "WRAPPED_RECORD"
45
- },
46
- cipherSuite,
47
- version: cipherSuite.includes("ECDHE_") ? "TLS1_2" : "TLS1_3"
10
+ export async function benchmark() {
11
+ let benchmarkRes = '';
12
+ for (const cipherSuite of ZK_CIPHER_SUITES) {
13
+ const now = Date.now();
14
+ const alg = cipherSuite.includes('CHACHA20')
15
+ ? 'CHACHA20-POLY1305'
16
+ : (cipherSuite.includes('AES_256_GCM')
17
+ ? 'AES-256-GCM'
18
+ : 'AES-128-GCM');
19
+ const keylength = alg === 'AES-128-GCM' ? 16 : 32;
20
+ const key = Buffer.alloc(keylength, 0);
21
+ const { ivLength: fixedIvLength, } = SUPPORTED_CIPHER_SUITE_MAP[cipherSuite];
22
+ const fixedIv = Buffer.alloc(fixedIvLength, 0);
23
+ const encKey = await crypto.importKey(alg, key);
24
+ const vectors = [
25
+ {
26
+ plaintext: 'My cool API secret is "my name jeff". Please don\'t reveal it'
27
+ }
28
+ ];
29
+ const proofGenerator = await makeZkProofGenerator({
30
+ logger,
31
+ cipherSuite,
32
+ });
33
+ for (const { plaintext } of vectors) {
34
+ const plaintextArr = strToUint8Array(plaintext);
35
+ const { ciphertext, iv } = await encryptWrappedRecord(plaintextArr, {
36
+ key: encKey,
37
+ iv: fixedIv,
38
+ recordNumber: 0,
39
+ recordHeaderOpts: {
40
+ type: 'WRAPPED_RECORD'
41
+ },
42
+ cipherSuite,
43
+ version: cipherSuite.includes('ECDHE_')
44
+ ? 'TLS1_2'
45
+ : 'TLS1_3',
46
+ });
47
+ const packet = {
48
+ type: 'ciphertext',
49
+ encKey,
50
+ iv,
51
+ recordNumber: 0,
52
+ plaintext: plaintextArr,
53
+ ciphertext,
54
+ fixedIv: new Uint8Array(0),
55
+ data: ciphertext
56
+ };
57
+ await proofGenerator.addPacketToProve(packet, {
58
+ type: 'zk',
59
+ redactedPlaintext: plaintextArr,
60
+ }, () => { }, () => {
61
+ throw new Error('should not be called in benchmark');
62
+ });
63
+ await proofGenerator.generateProofs();
48
64
  }
49
- );
50
- const packet = {
51
- type: "ciphertext",
52
- encKey,
53
- iv,
54
- recordNumber: 0,
55
- plaintext: plaintextArr,
56
- ciphertext,
57
- fixedIv: new Uint8Array(0),
58
- data: ciphertext
59
- };
60
- await proofGenerator.addPacketToProve(
61
- packet,
62
- {
63
- type: "zk",
64
- redactedPlaintext: plaintextArr
65
- },
66
- () => {
67
- },
68
- () => {
69
- throw new Error("should not be called in benchmark");
70
- }
71
- );
72
- await proofGenerator.generateProofs();
65
+ benchmarkRes = benchmarkRes + `Benchmark ${alg} ok. Took ${Date.now() - now} ms \n`;
73
66
  }
74
- benchmarkRes = benchmarkRes + `Benchmark ${alg} ok. Took ${Date.now() - now} ms
75
- `;
76
- }
77
- logger.info(benchmarkRes);
78
- return benchmarkRes;
67
+ logger.info(benchmarkRes);
68
+ return benchmarkRes;
79
69
  }
80
- export {
81
- benchmark
82
- };
@@ -1,17 +1,14 @@
1
- class EventBus {
2
- #listeners = [];
3
- addListener(fn) {
4
- this.#listeners.push(fn);
5
- return () => {
6
- this.#listeners = this.#listeners.filter((l) => l !== fn);
7
- };
8
- }
9
- dispatch(data) {
10
- for (const listener of this.#listeners) {
11
- listener(data);
1
+ export class EventBus {
2
+ #listeners = [];
3
+ addListener(fn) {
4
+ this.#listeners.push(fn);
5
+ return () => {
6
+ this.#listeners = this.#listeners.filter(l => l !== fn);
7
+ };
8
+ }
9
+ dispatch(data) {
10
+ for (const listener of this.#listeners) {
11
+ listener(data);
12
+ }
12
13
  }
13
- }
14
14
  }
15
- export {
16
- EventBus
17
- };