@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,69 +1,71 @@
1
- import { concatenateUint8Arrays, crypto } from "@reclaimprotocol/tls";
2
- import {
3
- TranscriptMessageSenderType
4
- } from "../proto/api.js";
5
- import { makeZkProofGenerator } from "../utils/zk.js";
6
- async function preparePacketsForReveal(tlsTranscript, reveals, { onZkProgress, ...opts }) {
7
- const transcript = [];
8
- const proofGenerator = await makeZkProofGenerator(opts);
9
- let zkPacketsDone = 0;
10
- await Promise.all(tlsTranscript.map(async ({ message, sender }, i) => {
11
- const msg = {
12
- sender: sender === "client" ? TranscriptMessageSenderType.TRANSCRIPT_MESSAGE_SENDER_TYPE_CLIENT : TranscriptMessageSenderType.TRANSCRIPT_MESSAGE_SENDER_TYPE_SERVER,
13
- message: message.data,
14
- reveal: void 0
15
- };
16
- transcript.push(msg);
17
- const reveal = reveals.get(message);
18
- if (!reveal || message.type === "plaintext") {
19
- return;
20
- }
21
- switch (reveal?.type) {
22
- case "complete":
23
- msg.reveal = {
24
- directReveal: {
25
- key: await crypto.exportKey(message.encKey),
26
- iv: message.fixedIv,
27
- recordNumber: message.recordNumber
28
- }
1
+ import { concatenateUint8Arrays, crypto } from '@reclaimprotocol/tls';
2
+ import { TranscriptMessageSenderType } from "../proto/api.js";
3
+ import { makeZkProofGenerator } from "./zk.js";
4
+ /**
5
+ * Prepares the packets for reveal to the server
6
+ * according to the specified reveal type
7
+ */
8
+ export async function preparePacketsForReveal(tlsTranscript, reveals, { onZkProgress, ...opts }) {
9
+ const transcript = [];
10
+ const proofGenerator = await makeZkProofGenerator(opts);
11
+ let zkPacketsDone = 0;
12
+ await Promise.all(tlsTranscript.map(async ({ message, sender }, i) => {
13
+ const msg = {
14
+ sender: sender === 'client'
15
+ ? TranscriptMessageSenderType.TRANSCRIPT_MESSAGE_SENDER_TYPE_CLIENT
16
+ : TranscriptMessageSenderType.TRANSCRIPT_MESSAGE_SENDER_TYPE_SERVER,
17
+ message: message.data,
18
+ reveal: undefined
29
19
  };
30
- break;
31
- case "zk": {
32
- reveal.redactedPlaintext = concatenateUint8Arrays([
33
- reveal.redactedPlaintext,
34
- message.plaintext.slice(reveal.redactedPlaintext.length)
35
- ]);
36
- const oprfRawMarkers = reveal.oprfRawMarkers?.map((m) => ({
37
- dataLocation: m.dataLocation
38
- })) || [];
39
- const overshotOprfRawLength = reveal.overshotOprfRawFromPrevBlock?.length ?? 0;
40
- await proofGenerator.addPacketToProve(
41
- message,
42
- reveal,
43
- (proofs, toprfs) => msg.reveal = {
44
- zkReveal: { proofs, toprfs, oprfRawMarkers, overshotOprfRawLength }
45
- },
46
- () => {
47
- const next = tlsTranscript.slice(i + 1).find((t) => t.sender === sender);
48
- return next?.message;
49
- }
50
- );
51
- break;
52
- }
53
- default:
54
- break;
55
- }
56
- }));
57
- const zkPacketsTotal = proofGenerator.getTotalChunksToProve();
58
- onZkProgress?.(zkPacketsDone, zkPacketsTotal);
59
- await proofGenerator.generateProofs(
60
- () => {
61
- zkPacketsDone += 1;
62
- onZkProgress?.(zkPacketsDone, zkPacketsTotal);
63
- }
64
- );
65
- return transcript;
20
+ transcript.push(msg);
21
+ const reveal = reveals.get(message);
22
+ if (!reveal || message.type === 'plaintext') {
23
+ return;
24
+ }
25
+ switch (reveal?.type) {
26
+ case 'complete':
27
+ msg.reveal = {
28
+ directReveal: {
29
+ key: await crypto.exportKey(message.encKey),
30
+ iv: message.fixedIv,
31
+ recordNumber: message.recordNumber,
32
+ },
33
+ };
34
+ break;
35
+ case 'zk': {
36
+ // the redacted section can be smaller than the actual
37
+ // plaintext encrypted, in case of TLS1.3 as it has a
38
+ // content type suffix
39
+ reveal.redactedPlaintext = concatenateUint8Arrays([
40
+ reveal.redactedPlaintext,
41
+ message.plaintext.slice(reveal.redactedPlaintext.length)
42
+ ]);
43
+ // Capture oprfRawMarkers for server-side OPRF computation
44
+ const oprfRawMarkers = reveal.oprfRawMarkers?.map(m => ({
45
+ dataLocation: m.dataLocation
46
+ })) || [];
47
+ // Capture overshoot from previous block's oprf-raw marker
48
+ const overshotOprfRawLength = reveal.overshotOprfRawFromPrevBlock?.length ?? 0;
49
+ await proofGenerator.addPacketToProve(message, reveal, (proofs, toprfs) => (msg.reveal = {
50
+ zkReveal: { proofs, toprfs, oprfRawMarkers, overshotOprfRawLength }
51
+ }), () => {
52
+ const next = tlsTranscript
53
+ .slice(i + 1)
54
+ .find(t => t.sender === sender);
55
+ return next?.message;
56
+ });
57
+ break;
58
+ }
59
+ default:
60
+ // no reveal
61
+ break;
62
+ }
63
+ }));
64
+ const zkPacketsTotal = proofGenerator.getTotalChunksToProve();
65
+ onZkProgress?.(zkPacketsDone, zkPacketsTotal);
66
+ await proofGenerator.generateProofs(() => {
67
+ zkPacketsDone += 1;
68
+ onZkProgress?.(zkPacketsDone, zkPacketsTotal);
69
+ });
70
+ return transcript;
66
71
  }
67
- export {
68
- preparePacketsForReveal
69
- };
@@ -1,135 +1,177 @@
1
- import { base64Encode } from "@bufbuild/protobuf/wire";
2
- import { concatenateUint8Arrays } from "@reclaimprotocol/tls";
3
- const REDACTION_CHAR = "*";
4
- const REDACTION_CHAR_CODE = REDACTION_CHAR.charCodeAt(0);
5
- function isRedactionCongruent(redacted, original) {
6
- for (let i = 0; i < redacted.length; i++) {
7
- const element = redacted[i];
8
- const areSame = element === original[i] || typeof element === "string" && element === REDACTION_CHAR || typeof element === "number" && element === REDACTION_CHAR_CODE;
9
- if (!areSame) {
10
- return false;
1
+ import { base64Encode } from '@bufbuild/protobuf/wire';
2
+ import { concatenateUint8Arrays } from '@reclaimprotocol/tls';
3
+ export const REDACTION_CHAR = '*';
4
+ export const REDACTION_CHAR_CODE = REDACTION_CHAR.charCodeAt(0);
5
+ /**
6
+ * Check if a redacted string is congruent with the original string.
7
+ * @param redacted the redacted content, redacted content is replaced by '*'
8
+ * @param original the original content
9
+ */
10
+ export function isRedactionCongruent(redacted, original) {
11
+ for (let i = 0; i < redacted.length; i++) {
12
+ const element = redacted[i];
13
+ const areSame = element === original[i]
14
+ || (typeof element === 'string' && element === REDACTION_CHAR)
15
+ || (typeof element === 'number' && element === REDACTION_CHAR_CODE);
16
+ if (!areSame) {
17
+ return false;
18
+ }
11
19
  }
12
- }
13
- return true;
20
+ return true;
14
21
  }
15
- function isFullyRedacted(redacted) {
16
- for (const element of redacted) {
17
- if (element !== REDACTION_CHAR && element !== REDACTION_CHAR_CODE) {
18
- return false;
22
+ /**
23
+ * Is the string fully redacted?
24
+ */
25
+ export function isFullyRedacted(redacted) {
26
+ for (const element of redacted) {
27
+ if (element !== REDACTION_CHAR
28
+ && element !== REDACTION_CHAR_CODE) {
29
+ return false;
30
+ }
19
31
  }
20
- }
21
- return true;
32
+ return true;
22
33
  }
23
- async function getBlocksToReveal(blocks, redact, performOprf) {
24
- const slicesWithReveal = blocks.map((block) => ({
25
- block,
26
- // copy the plaintext to avoid mutating the original
27
- redactedPlaintext: new Uint8Array(block.plaintext)
28
- }));
29
- const total = concatenateUint8Arrays(blocks.map((b) => b.plaintext));
30
- const redactions = redact(total);
31
- if (!redactions.length) {
32
- return "all";
33
- }
34
- let blockIdx = 0;
35
- let cursorInBlock = 0;
36
- let cursor = 0;
37
- for (const redaction of redactions) {
38
- await redactBlocks(redaction);
39
- }
40
- return slicesWithReveal.filter((s) => !isFullyRedacted(s.redactedPlaintext));
41
- async function redactBlocks(slice) {
42
- while (cursor < slice.fromIndex) {
43
- advance();
34
+ /**
35
+ * Given some plaintext blocks and a redaction function, return the blocks that
36
+ * need to be revealed to the other party
37
+ *
38
+ * Use case: we get the response for a request in several blocks, and want to redact
39
+ * pieces that go through multiple blocks. We can use this function to get the
40
+ * blocks that need to be revealed to the other party
41
+ *
42
+ * @example if we received ["secret is 12","345","678. Thanks"]. We'd want
43
+ * to redact the "12345678" and reveal the rest. We'd pass in the blocks and
44
+ * the redact function will return the redactions, namely [10,19].
45
+ * The function will return the blocks ["secret is **","***. Thanks"].
46
+ * The middle block is fully redacted, so it's not returned
47
+ *
48
+ * @param blocks blocks to reveal
49
+ * @param redact function that returns the redactions
50
+ * @returns blocks to reveal
51
+ */
52
+ export async function getBlocksToReveal(blocks, redact, performOprf) {
53
+ const slicesWithReveal = blocks.map(block => ({
54
+ block,
55
+ // copy the plaintext to avoid mutating the original
56
+ redactedPlaintext: new Uint8Array(block.plaintext)
57
+ }));
58
+ const total = concatenateUint8Arrays(blocks.map(b => b.plaintext));
59
+ const redactions = redact(total);
60
+ if (!redactions.length) {
61
+ return 'all';
44
62
  }
45
- if (slice.hash === "oprf-raw") {
46
- const startBlockIdx = blockIdx;
47
- const startCursorInBlock = cursorInBlock;
48
- const totalLength = slice.toIndex - slice.fromIndex;
49
- const block = slicesWithReveal[blockIdx];
50
- block.oprfRawMarkers ||= [];
51
- block.oprfRawMarkers.push({
52
- dataLocation: {
53
- fromIndex: startCursorInBlock,
54
- length: totalLength
63
+ let blockIdx = 0;
64
+ let cursorInBlock = 0;
65
+ let cursor = 0;
66
+ for (const redaction of redactions) {
67
+ await redactBlocks(redaction);
68
+ }
69
+ // only reveal blocks that have some data to reveal,
70
+ // or are completely plaintext
71
+ return slicesWithReveal
72
+ .filter(s => !isFullyRedacted(s.redactedPlaintext));
73
+ async function redactBlocks(slice) {
74
+ while (cursor < slice.fromIndex) {
75
+ advance();
55
76
  }
56
- });
57
- let overshootLen = 0;
58
- while (cursor < slice.toIndex) {
59
- if (blockIdx !== startBlockIdx) {
60
- overshootLen += 1;
77
+ // Handle oprf-raw: don't redact, don't call OPRF, just mark position
78
+ // Server will compute OPRF and do replacement
79
+ if (slice.hash === 'oprf-raw') {
80
+ const startBlockIdx = blockIdx;
81
+ const startCursorInBlock = cursorInBlock;
82
+ const totalLength = slice.toIndex - slice.fromIndex;
83
+ // Set marker on first block
84
+ const block = slicesWithReveal[blockIdx];
85
+ block.oprfRawMarkers ||= [];
86
+ block.oprfRawMarkers.push({
87
+ dataLocation: {
88
+ fromIndex: startCursorInBlock,
89
+ length: totalLength
90
+ }
91
+ });
92
+ // Advance cursor past this slice, tracking overshoot
93
+ let overshootLen = 0;
94
+ while (cursor < slice.toIndex) {
95
+ if (blockIdx !== startBlockIdx) {
96
+ overshootLen += 1;
97
+ }
98
+ advance();
99
+ }
100
+ // If data overshot into next block, mark it
101
+ if (overshootLen) {
102
+ slicesWithReveal[blockIdx]
103
+ .overshotOprfRawFromPrevBlock = { length: overshootLen };
104
+ }
105
+ return;
61
106
  }
62
- advance();
63
- }
64
- if (overshootLen) {
65
- slicesWithReveal[blockIdx].overshotOprfRawFromPrevBlock = { length: overshootLen };
66
- }
67
- return;
68
- }
69
- if (slice.hash) {
70
- const plaintext = total.slice(slice.fromIndex, slice.toIndex);
71
- const { nullifier, responses, mask } = await performOprf(plaintext);
72
- const toprf = {
73
- nullifier,
74
- responses,
75
- dataLocation: {
76
- fromIndex: cursorInBlock,
77
- length: slice.toIndex - slice.fromIndex
78
- },
79
- mask,
80
- plaintext
81
- };
82
- const startBlockIdx = blockIdx;
83
- const block = slicesWithReveal[blockIdx];
84
- block.toprfs ||= [];
85
- block.toprfs.push(toprf);
86
- const nullifierStr = binaryHashToStr(nullifier, toprf.dataLocation.length);
87
- let i = 0;
88
- let overshootLen = 0;
89
- while (cursor < slice.toIndex) {
90
- if (blockIdx !== startBlockIdx) {
91
- overshootLen += 1;
107
+ if (slice.hash) {
108
+ const plaintext = total.slice(slice.fromIndex, slice.toIndex);
109
+ const { nullifier, responses, mask } = await performOprf(plaintext);
110
+ // set the TOPRF claim on the first blocks this
111
+ // redaction covers
112
+ const toprf = {
113
+ nullifier,
114
+ responses,
115
+ dataLocation: {
116
+ fromIndex: cursorInBlock,
117
+ length: slice.toIndex - slice.fromIndex
118
+ },
119
+ mask,
120
+ plaintext
121
+ };
122
+ const startBlockIdx = blockIdx;
123
+ const block = slicesWithReveal[blockIdx];
124
+ block.toprfs ||= [];
125
+ block.toprfs.push(toprf);
126
+ const nullifierStr = binaryHashToStr(nullifier, toprf.dataLocation.length);
127
+ let i = 0;
128
+ let overshootLen = 0;
129
+ while (cursor < slice.toIndex) {
130
+ if (blockIdx !== startBlockIdx) {
131
+ overshootLen += 1;
132
+ }
133
+ slicesWithReveal[blockIdx].redactedPlaintext[cursorInBlock]
134
+ = nullifierStr.charCodeAt(i);
135
+ advance();
136
+ i += 1;
137
+ }
138
+ if (overshootLen) {
139
+ slicesWithReveal[blockIdx]
140
+ .overshotToprfFromPrevBlock = { length: overshootLen };
141
+ }
142
+ }
143
+ while (cursor < slice.toIndex) {
144
+ slicesWithReveal[blockIdx]
145
+ .redactedPlaintext[cursorInBlock] = REDACTION_CHAR_CODE;
146
+ advance();
92
147
  }
93
- slicesWithReveal[blockIdx].redactedPlaintext[cursorInBlock] = nullifierStr.charCodeAt(i);
94
- advance();
95
- i += 1;
96
- }
97
- if (overshootLen) {
98
- slicesWithReveal[blockIdx].overshotToprfFromPrevBlock = { length: overshootLen };
99
- }
100
- }
101
- while (cursor < slice.toIndex) {
102
- slicesWithReveal[blockIdx].redactedPlaintext[cursorInBlock] = REDACTION_CHAR_CODE;
103
- advance();
104
148
  }
105
- }
106
- function advance() {
107
- cursor += 1;
108
- cursorInBlock += 1;
109
- if (cursorInBlock >= blocks[blockIdx].plaintext.length) {
110
- blockIdx += 1;
111
- cursorInBlock = 0;
149
+ function advance() {
150
+ cursor += 1;
151
+ cursorInBlock += 1;
152
+ if (cursorInBlock >= blocks[blockIdx].plaintext.length) {
153
+ blockIdx += 1;
154
+ cursorInBlock = 0;
155
+ }
112
156
  }
113
- }
114
157
  }
115
- function redactSlices(total, slices) {
116
- const redacted = new Uint8Array(total);
117
- for (const slice of slices) {
118
- for (let i = slice.fromIndex; i < slice.toIndex; i++) {
119
- redacted[i] = REDACTION_CHAR_CODE;
158
+ /**
159
+ * Redact the following slices from the total
160
+ */
161
+ export function redactSlices(total, slices) {
162
+ const redacted = new Uint8Array(total);
163
+ for (const slice of slices) {
164
+ for (let i = slice.fromIndex; i < slice.toIndex; i++) {
165
+ redacted[i] = REDACTION_CHAR_CODE;
166
+ }
120
167
  }
121
- }
122
- return redacted;
168
+ return redacted;
123
169
  }
124
- function binaryHashToStr(hash, expLength) {
125
- return base64Encode(hash).padEnd(expLength, "0").slice(0, expLength);
170
+ /**
171
+ * Converts the binary hash to an ASCII string of the expected length.
172
+ * If the hash is shorter than the expected length, it will be padded with
173
+ * '0' characters. If it's longer, it will be truncated.
174
+ */
175
+ export function binaryHashToStr(hash, expLength) {
176
+ return base64Encode(hash).padEnd(expLength, '0').slice(0, expLength);
126
177
  }
127
- export {
128
- REDACTION_CHAR,
129
- REDACTION_CHAR_CODE,
130
- binaryHashToStr,
131
- getBlocksToReveal,
132
- isFullyRedacted,
133
- isRedactionCongruent,
134
- redactSlices
135
- };
@@ -1,26 +1,24 @@
1
- async function executeWithRetries(code, {
2
- maxRetries = 3,
3
- shouldRetry,
4
- logger
5
- }) {
6
- let retries = 0;
7
- while (retries < maxRetries) {
8
- try {
9
- const result = await code(retries);
10
- return result;
11
- } catch (err) {
12
- retries += 1;
13
- if (retries >= maxRetries) {
14
- throw err;
15
- }
16
- if (!shouldRetry(err)) {
17
- throw err;
18
- }
19
- logger.info({ err, retries }, "retrying failed operation");
1
+ /**
2
+ * Execute a function, and upon failure -- retry
3
+ * based on specified options.
4
+ */
5
+ export async function executeWithRetries(code, { maxRetries = 3, shouldRetry, logger, }) {
6
+ let retries = 0;
7
+ while (retries < maxRetries) {
8
+ try {
9
+ const result = await code(retries);
10
+ return result;
11
+ }
12
+ catch (err) {
13
+ retries += 1;
14
+ if (retries >= maxRetries) {
15
+ throw err;
16
+ }
17
+ if (!shouldRetry(err)) {
18
+ throw err;
19
+ }
20
+ logger.info({ err, retries }, 'retrying failed operation');
21
+ }
20
22
  }
21
- }
22
- throw new Error("retries exhausted");
23
+ throw new Error('retries exhausted');
23
24
  }
24
- export {
25
- executeWithRetries
26
- };
@@ -1,31 +1,32 @@
1
- import { computeAddress, getBytes, hexlify, SigningKey, verifyMessage, Wallet } from "ethers";
2
- const ETH_SIGNATURE_PROVIDER = {
3
- getPublicKey(privateKey) {
4
- const pub = SigningKey.computePublicKey(privateKey, true);
5
- return getBytes(pub);
6
- },
7
- getAddress(publicKey) {
8
- const pubKeyHex = typeof publicKey === "string" ? publicKey : hexlify(publicKey);
9
- return computeAddress(pubKeyHex).toLowerCase();
10
- },
11
- async sign(data, privateKey) {
12
- const wallet = getEthWallet(privateKey);
13
- const signature = await wallet.signMessage(data);
14
- return getBytes(signature);
15
- },
16
- async verify(data, signature, addressBytes) {
17
- const address = typeof addressBytes === "string" ? addressBytes : hexlify(addressBytes);
18
- const signatureHex = typeof signature === "string" ? signature : hexlify(signature);
19
- const signerAddress = verifyMessage(data, signatureHex);
20
- return signerAddress.toLowerCase() === address.toLowerCase();
21
- }
1
+ import { computeAddress, getBytes, hexlify, SigningKey, verifyMessage, Wallet } from 'ethers';
2
+ export const ETH_SIGNATURE_PROVIDER = {
3
+ getPublicKey(privateKey) {
4
+ const pub = SigningKey.computePublicKey(privateKey, true);
5
+ return getBytes(pub);
6
+ },
7
+ getAddress(publicKey) {
8
+ // computeAddress in v6 expects hex string
9
+ const pubKeyHex = typeof publicKey === 'string' ? publicKey : hexlify(publicKey);
10
+ return computeAddress(pubKeyHex).toLowerCase();
11
+ },
12
+ async sign(data, privateKey) {
13
+ const wallet = getEthWallet(privateKey);
14
+ const signature = await wallet.signMessage(data);
15
+ return getBytes(signature);
16
+ },
17
+ async verify(data, signature, addressBytes) {
18
+ const address = typeof addressBytes === 'string'
19
+ ? addressBytes
20
+ : hexlify(addressBytes);
21
+ // verifyMessage in v6 expects SignatureLike (hex string)
22
+ const signatureHex = typeof signature === 'string' ? signature : hexlify(signature);
23
+ const signerAddress = verifyMessage(data, signatureHex);
24
+ return signerAddress.toLowerCase() === address.toLowerCase();
25
+ }
22
26
  };
23
27
  function getEthWallet(privateKey) {
24
- if (!privateKey) {
25
- throw new Error("Private key missing");
26
- }
27
- return new Wallet(privateKey);
28
+ if (!privateKey) {
29
+ throw new Error('Private key missing');
30
+ }
31
+ return new Wallet(privateKey);
28
32
  }
29
- export {
30
- ETH_SIGNATURE_PROVIDER
31
- };
@@ -1,12 +1,7 @@
1
1
  import { ServiceSignatureType } from "../../proto/api.js";
2
- import { ETH_SIGNATURE_PROVIDER } from "../../utils/signatures/eth.js";
3
- const SIGNATURES = {
4
- [ServiceSignatureType.SERVICE_SIGNATURE_TYPE_ETH]: ETH_SIGNATURE_PROVIDER
5
- };
6
- const SelectedServiceSignatureType = ServiceSignatureType.SERVICE_SIGNATURE_TYPE_ETH;
7
- const SelectedServiceSignature = SIGNATURES[SelectedServiceSignatureType];
8
- export {
9
- SIGNATURES,
10
- SelectedServiceSignature,
11
- SelectedServiceSignatureType
2
+ import { ETH_SIGNATURE_PROVIDER } from "./eth.js";
3
+ export const SIGNATURES = {
4
+ [ServiceSignatureType.SERVICE_SIGNATURE_TYPE_ETH]: ETH_SIGNATURE_PROVIDER,
12
5
  };
6
+ export const SelectedServiceSignatureType = ServiceSignatureType.SERVICE_SIGNATURE_TYPE_ETH;
7
+ export const SelectedServiceSignature = SIGNATURES[SelectedServiceSignatureType];