@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,89 +1,98 @@
1
- import canonicalize from "canonicalize";
2
- import { keccak256 } from "ethers";
1
+ import canonicalize from 'canonicalize';
2
+ import { keccak256 } from 'ethers';
3
3
  import { DEFAULT_METADATA } from "../config/index.js";
4
4
  import { ClaimTunnelResponse } from "../proto/api.js";
5
- import { SIGNATURES, strToUint8Array } from "../utils/index.js";
6
- function createSignDataForClaim(data) {
7
- const lines = [
8
- getIdentifierFromClaimInfo(data),
9
- // we lowercase the owner to ensure that the
10
- // ETH addresses always serialize the same way
11
- data.owner.toLowerCase(),
12
- data.timestampS.toString(),
13
- data.epoch.toString()
14
- ];
15
- return lines.join("\n");
5
+ import { SIGNATURES, strToUint8Array } from "../..";
6
+ /**
7
+ * Creates the standard string to sign for a claim.
8
+ * This data is what the attestor will sign when it successfully
9
+ * verifies a claim.
10
+ */
11
+ export function createSignDataForClaim(data) {
12
+ const lines = [
13
+ getIdentifierFromClaimInfo(data),
14
+ // we lowercase the owner to ensure that the
15
+ // ETH addresses always serialize the same way
16
+ data.owner.toLowerCase(),
17
+ data.timestampS.toString(),
18
+ data.epoch.toString(),
19
+ ];
20
+ return lines.join('\n');
16
21
  }
17
- async function assertValidClaimSignatures({
18
- signatures,
19
- ...res
20
- }, metadata = DEFAULT_METADATA) {
21
- if (!signatures) {
22
- throw new Error("No signatures provided");
23
- }
24
- const {
25
- resultSignature,
26
- claimSignature,
27
- attestorAddress
28
- } = signatures;
29
- const { verify } = SIGNATURES[metadata.signatureType];
30
- if (signatures?.resultSignature) {
31
- const resBytes = ClaimTunnelResponse.encode(ClaimTunnelResponse.create(res)).finish();
32
- const verified = await verify(resBytes, resultSignature, attestorAddress);
33
- if (!verified) {
34
- throw new Error("Invalid result signature");
22
+ /**
23
+ * Verify the claim tunnel response from a attestor.
24
+ *
25
+ * If you'd only like to verify the claim signature, you can
26
+ * optionally only pass "claim" & "signatures.claimSignature"
27
+ * to this function.
28
+ *
29
+ * The successful run of this function means that the claim
30
+ * is valid, and the attestor that signed the claim is valid.
31
+ */
32
+ export async function assertValidClaimSignatures({ signatures, ...res }, metadata = DEFAULT_METADATA) {
33
+ if (!signatures) {
34
+ throw new Error('No signatures provided');
35
+ }
36
+ const { resultSignature, claimSignature, attestorAddress } = signatures;
37
+ const { verify } = SIGNATURES[metadata.signatureType];
38
+ if (signatures?.resultSignature) {
39
+ const resBytes = ClaimTunnelResponse
40
+ .encode(ClaimTunnelResponse.create(res)).finish();
41
+ const verified = await verify(resBytes, resultSignature, attestorAddress);
42
+ if (!verified) {
43
+ throw new Error('Invalid result signature');
44
+ }
45
+ }
46
+ // claim wasn't generated -- i.e. the transcript
47
+ // did not contain the necessary data
48
+ if (!res.claim) {
49
+ return;
50
+ }
51
+ const signData = createSignDataForClaim(res.claim);
52
+ const verifiedClaim = await verify(strToUint8Array(signData), claimSignature, attestorAddress);
53
+ if (!verifiedClaim) {
54
+ throw new Error('Invalid claim signature');
35
55
  }
36
- }
37
- if (!res.claim) {
38
- return;
39
- }
40
- const signData = createSignDataForClaim(res.claim);
41
- const verifiedClaim = await verify(
42
- strToUint8Array(signData),
43
- claimSignature,
44
- attestorAddress
45
- );
46
- if (!verifiedClaim) {
47
- throw new Error("Invalid claim signature");
48
- }
49
56
  }
50
- function getIdentifierFromClaimInfo(info) {
51
- if (info.context?.length > 0) {
52
- try {
53
- const ctx = JSON.parse(info.context);
54
- info.context = canonicalStringify(ctx);
55
- } catch {
56
- throw new Error("unable to parse non-empty context. Must be JSON");
57
+ /**
58
+ * Generates a unique identifier for given claim info
59
+ * @param info
60
+ * @returns
61
+ */
62
+ export function getIdentifierFromClaimInfo(info) {
63
+ //re-canonicalize context if it's not empty
64
+ if (info.context?.length > 0) {
65
+ try {
66
+ const ctx = JSON.parse(info.context);
67
+ info.context = canonicalStringify(ctx);
68
+ }
69
+ catch {
70
+ throw new Error('unable to parse non-empty context. Must be JSON');
71
+ }
57
72
  }
58
- }
59
- const str = `${info.provider}
60
- ${info.parameters}
61
- ${info.context || ""}`;
62
- return keccak256(strToUint8Array(str)).toLowerCase();
73
+ const str = `${info.provider}\n${info.parameters}\n${info.context || ''}`;
74
+ //console.log('Identifier: ' + btoa(str))
75
+ return keccak256(strToUint8Array(str)).toLowerCase();
63
76
  }
64
- function canonicalStringify(params) {
65
- if (!params) {
66
- return "";
67
- }
68
- return canonicalize(params) || "";
77
+ /**
78
+ * Canonically stringifies an object, so that the same object will always
79
+ * produce the same string despite the order of keys
80
+ */
81
+ export function canonicalStringify(params) {
82
+ if (!params) {
83
+ return '';
84
+ }
85
+ // have to cast as ESM isn't correctly typing this
86
+ return canonicalize(params) || '';
69
87
  }
70
- function hashProviderParams(params) {
71
- const filteredParams = {
72
- url: params.url,
73
- method: params.method,
74
- body: params.body,
75
- responseMatches: params.responseMatches,
76
- responseRedactions: params.responseRedactions
77
- };
78
- const serializedParams = canonicalStringify(filteredParams);
79
- return keccak256(
80
- strToUint8Array(serializedParams)
81
- ).toLowerCase();
88
+ export function hashProviderParams(params) {
89
+ const filteredParams = {
90
+ url: params.url,
91
+ method: params.method,
92
+ body: params.body,
93
+ responseMatches: params.responseMatches,
94
+ responseRedactions: params.responseRedactions
95
+ };
96
+ const serializedParams = canonicalStringify(filteredParams);
97
+ return keccak256(strToUint8Array(serializedParams)).toLowerCase();
82
98
  }
83
- export {
84
- assertValidClaimSignatures,
85
- canonicalStringify,
86
- createSignDataForClaim,
87
- getIdentifierFromClaimInfo,
88
- hashProviderParams
89
- };
package/lib/utils/env.js CHANGED
@@ -1,19 +1,15 @@
1
- function detectEnvironment() {
2
- if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
3
- return "react-native";
4
- }
5
- if (typeof window !== "undefined") {
6
- return "browser";
7
- }
8
- return "node";
1
+ export function detectEnvironment() {
2
+ if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
3
+ return 'react-native';
4
+ }
5
+ if (typeof window !== 'undefined') {
6
+ return 'browser';
7
+ }
8
+ return 'node';
9
9
  }
10
- function getEnvVariable(name) {
11
- if (typeof process === "undefined") {
12
- return void 0;
13
- }
14
- return process?.env[name];
10
+ export function getEnvVariable(name) {
11
+ if (typeof process === 'undefined') {
12
+ return undefined;
13
+ }
14
+ return process?.env[name];
15
15
  }
16
- export {
17
- detectEnvironment,
18
- getEnvVariable
19
- };
@@ -1,54 +1,50 @@
1
1
  import { ErrorCode, ErrorData } from "../proto/api.js";
2
2
  const PROTO_ERROR = ErrorData.fromJSON({});
3
- class AttestorError extends Error {
4
- name = "AttestorError";
5
- code;
6
- data;
7
- constructor(code, message, data) {
8
- super(message);
9
- this.code = code;
10
- this.data = data;
11
- }
12
- /**
13
- * Encodes the error as a ErrorData
14
- * protobuf message
15
- */
16
- toProto() {
17
- return ErrorData.create({
18
- code: ErrorCode[this.code],
19
- message: this.message,
20
- data: JSON.stringify(this.data)
21
- });
22
- }
23
- static fromProto(data = PROTO_ERROR) {
24
- return new AttestorError(
25
- typeof data.code === "number" ? getKeyForValue(ErrorCode, data.code) || "UNRECOGNIZED" : data.code,
26
- data.message,
27
- data.data ? JSON.parse(data.data) : void 0
28
- );
29
- }
30
- static fromError(err, code = "ERROR_INTERNAL") {
31
- if (err instanceof AttestorError) {
32
- return err;
3
+ /**
4
+ * Represents an error that can be thrown by the Attestor Core
5
+ * or server. Provides a code, and optional data
6
+ * to pass along with the error.
7
+ */
8
+ export class AttestorError extends Error {
9
+ name = 'AttestorError';
10
+ code;
11
+ data;
12
+ constructor(code, message, data) {
13
+ super(message);
14
+ this.code = code;
15
+ this.data = data;
16
+ }
17
+ /**
18
+ * Encodes the error as a ErrorData
19
+ * protobuf message
20
+ */
21
+ toProto() {
22
+ return ErrorData.create({
23
+ code: ErrorCode[this.code],
24
+ message: this.message,
25
+ data: JSON.stringify(this.data)
26
+ });
27
+ }
28
+ static fromProto(data = PROTO_ERROR) {
29
+ return new AttestorError(typeof data.code === 'number'
30
+ ? getKeyForValue(ErrorCode, data.code) || 'UNRECOGNIZED'
31
+ : data.code, data.message, data.data ? JSON.parse(data.data) : undefined);
32
+ }
33
+ static fromError(err, code = 'ERROR_INTERNAL') {
34
+ if (err instanceof AttestorError) {
35
+ return err;
36
+ }
37
+ return new AttestorError(code, err.message);
38
+ }
39
+ static badRequest(message, data) {
40
+ return new AttestorError('ERROR_BAD_REQUEST', message, data);
33
41
  }
34
- return new AttestorError(code, err.message);
35
- }
36
- static badRequest(message, data) {
37
- return new AttestorError(
38
- "ERROR_BAD_REQUEST",
39
- message,
40
- data
41
- );
42
- }
43
42
  }
44
43
  function getKeyForValue(obj, value) {
45
- for (const key in obj) {
46
- if (obj[key] === value) {
47
- return key;
44
+ for (const key in obj) {
45
+ if (obj[key] === value) {
46
+ return key;
47
+ }
48
48
  }
49
- }
50
- return void 0;
49
+ return undefined;
51
50
  }
52
- export {
53
- AttestorError
54
- };