@joclaim/attestor-core 0.2.0 → 0.2.3

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 (420) hide show
  1. package/README.md +21 -15
  2. package/lib/avs/abis/avsDirectoryABI.d.ts +60 -0
  3. package/lib/avs/abis/avsDirectoryABI.js +340 -0
  4. package/lib/avs/abis/delegationABI.d.ts +126 -0
  5. package/lib/avs/abis/delegationABI.js +1 -0
  6. package/lib/avs/abis/registryABI.d.ts +136 -0
  7. package/lib/avs/abis/registryABI.js +725 -0
  8. package/lib/avs/client/create-claim-on-avs.d.ts +12 -0
  9. package/lib/avs/client/create-claim-on-avs.js +138 -0
  10. package/lib/avs/config.d.ts +7 -0
  11. package/lib/avs/config.js +20 -0
  12. package/lib/avs/contracts/ReclaimServiceManager.d.ts +697 -0
  13. package/lib/avs/contracts/ReclaimServiceManager.js +1 -0
  14. package/lib/avs/contracts/common.d.ts +21 -0
  15. package/lib/avs/contracts/common.js +1 -0
  16. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.d.ts +888 -0
  17. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +1169 -0
  18. package/lib/avs/contracts/factories/index.d.ts +1 -0
  19. package/{src/avs/contracts/factories/index.ts → lib/avs/contracts/factories/index.js} +1 -1
  20. package/{src/avs/contracts/index.ts → lib/avs/contracts/index.d.ts} +0 -3
  21. package/lib/avs/contracts/index.js +2 -0
  22. package/lib/avs/types/index.d.ts +55 -0
  23. package/lib/avs/types/index.js +1 -0
  24. package/lib/avs/utils/contracts.d.ts +21 -0
  25. package/lib/avs/utils/contracts.js +33 -0
  26. package/lib/avs/utils/register.d.ts +27 -0
  27. package/lib/avs/utils/register.js +78 -0
  28. package/lib/avs/utils/tasks.d.ts +22 -0
  29. package/lib/avs/utils/tasks.js +40 -0
  30. package/lib/client/create-claim.d.ts +5 -0
  31. package/lib/client/create-claim.js +437 -0
  32. package/lib/client/index.d.ts +3 -0
  33. package/lib/client/index.js +3 -0
  34. package/lib/client/tunnels/make-rpc-tcp-tunnel.d.ts +16 -0
  35. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +51 -0
  36. package/lib/client/tunnels/make-rpc-tls-tunnel.d.ts +26 -0
  37. package/lib/client/tunnels/make-rpc-tls-tunnel.js +131 -0
  38. package/lib/client/utils/attestor-pool.d.ts +8 -0
  39. package/lib/client/utils/attestor-pool.js +25 -0
  40. package/lib/client/utils/client-socket.d.ts +11 -0
  41. package/lib/client/utils/client-socket.js +98 -0
  42. package/lib/client/utils/message-handler.d.ts +4 -0
  43. package/lib/client/utils/message-handler.js +87 -0
  44. package/lib/config/index.d.ts +30 -0
  45. package/lib/config/index.js +43 -0
  46. package/lib/external-rpc/benchmark.d.ts +1 -0
  47. package/lib/external-rpc/benchmark.js +69 -0
  48. package/lib/external-rpc/event-bus.d.ts +7 -0
  49. package/lib/external-rpc/event-bus.js +14 -0
  50. package/lib/external-rpc/handle-incoming-msg.d.ts +2 -0
  51. package/lib/external-rpc/handle-incoming-msg.js +233 -0
  52. package/lib/external-rpc/index.d.ts +3 -0
  53. package/lib/external-rpc/index.js +3 -0
  54. package/lib/external-rpc/jsc-polyfills/1.d.ts +14 -0
  55. package/lib/external-rpc/jsc-polyfills/1.js +82 -0
  56. package/lib/external-rpc/jsc-polyfills/2.d.ts +1 -0
  57. package/lib/external-rpc/jsc-polyfills/2.js +20 -0
  58. package/lib/external-rpc/jsc-polyfills/event.d.ts +10 -0
  59. package/lib/external-rpc/jsc-polyfills/event.js +14 -0
  60. package/lib/external-rpc/jsc-polyfills/index.d.ts +2 -0
  61. package/lib/external-rpc/jsc-polyfills/index.js +2 -0
  62. package/lib/external-rpc/jsc-polyfills/ws.d.ts +21 -0
  63. package/lib/external-rpc/jsc-polyfills/ws.js +81 -0
  64. package/lib/external-rpc/setup-browser.d.ts +6 -0
  65. package/lib/external-rpc/setup-browser.js +33 -0
  66. package/lib/external-rpc/setup-jsc.d.ts +24 -0
  67. package/lib/external-rpc/setup-jsc.js +22 -0
  68. package/lib/external-rpc/types.d.ts +213 -0
  69. package/lib/external-rpc/types.js +1 -0
  70. package/lib/external-rpc/utils.d.ts +20 -0
  71. package/lib/external-rpc/utils.js +100 -0
  72. package/lib/external-rpc/zk.d.ts +14 -0
  73. package/lib/external-rpc/zk.js +63 -0
  74. package/lib/index.d.ts +9 -0
  75. package/lib/index.js +9 -0
  76. package/lib/mechain/abis/governanceABI.d.ts +50 -0
  77. package/lib/mechain/abis/governanceABI.js +458 -0
  78. package/lib/mechain/abis/taskABI.d.ts +157 -0
  79. package/lib/mechain/abis/taskABI.js +509 -0
  80. package/lib/mechain/client/create-claim-on-mechain.d.ts +10 -0
  81. package/lib/mechain/client/create-claim-on-mechain.js +28 -0
  82. package/lib/mechain/client/index.d.ts +1 -0
  83. package/lib/mechain/client/index.js +1 -0
  84. package/lib/mechain/constants/index.d.ts +3 -0
  85. package/{src/mechain/constants/index.ts → lib/mechain/constants/index.js} +3 -5
  86. package/lib/mechain/index.d.ts +2 -0
  87. package/lib/mechain/index.js +2 -0
  88. package/lib/mechain/types/index.d.ts +23 -0
  89. package/lib/mechain/types/index.js +1 -0
  90. package/lib/proto/api.d.ts +633 -0
  91. package/lib/proto/api.js +4258 -0
  92. package/lib/proto/tee-bundle.d.ts +135 -0
  93. package/lib/proto/tee-bundle.js +1161 -0
  94. package/lib/providers/http/index.d.ts +18 -0
  95. package/lib/providers/http/index.js +658 -0
  96. package/lib/providers/http/patch-parse5-tree.d.ts +6 -0
  97. package/lib/providers/http/patch-parse5-tree.js +33 -0
  98. package/lib/providers/http/utils.d.ts +77 -0
  99. package/lib/providers/http/utils.js +324 -0
  100. package/lib/providers/index.d.ts +4 -0
  101. package/lib/providers/index.js +4 -0
  102. package/lib/scripts/build-browser.d.ts +1 -0
  103. package/lib/scripts/build-browser.js +37 -0
  104. package/lib/scripts/build-jsc.d.ts +1 -0
  105. package/lib/scripts/build-jsc.js +49 -0
  106. package/lib/scripts/check-avs-registration.d.ts +1 -0
  107. package/lib/scripts/check-avs-registration.js +26 -0
  108. package/lib/scripts/fallbacks/crypto.d.ts +1 -0
  109. package/lib/scripts/fallbacks/crypto.js +1 -0
  110. package/lib/scripts/fallbacks/empty.d.ts +3 -0
  111. package/lib/scripts/fallbacks/empty.js +1 -0
  112. package/lib/scripts/fallbacks/re2.d.ts +1 -0
  113. package/lib/scripts/fallbacks/re2.js +4 -0
  114. package/lib/scripts/fallbacks/snarkjs.d.ts +1 -0
  115. package/lib/scripts/fallbacks/snarkjs.js +1 -0
  116. package/lib/scripts/generate-provider-types.d.ts +5 -0
  117. package/lib/scripts/generate-provider-types.js +78 -0
  118. package/lib/scripts/generate-receipt.d.ts +9 -0
  119. package/lib/scripts/generate-receipt.js +90 -0
  120. package/lib/scripts/generate-toprf-keys.d.ts +1 -0
  121. package/lib/scripts/generate-toprf-keys.js +20 -0
  122. package/lib/scripts/jsc-cli-rpc.d.ts +1 -0
  123. package/lib/scripts/jsc-cli-rpc.js +37 -0
  124. package/lib/scripts/register-avs-operator.d.ts +1 -0
  125. package/lib/scripts/register-avs-operator.js +4 -0
  126. package/lib/scripts/start-server.d.ts +1 -0
  127. package/lib/scripts/start-server.js +13 -0
  128. package/lib/scripts/update-avs-metadata.d.ts +1 -0
  129. package/lib/scripts/update-avs-metadata.js +19 -0
  130. package/lib/scripts/utils.d.ts +1 -0
  131. package/lib/scripts/utils.js +7 -0
  132. package/lib/scripts/whitelist-operator.d.ts +1 -0
  133. package/lib/scripts/whitelist-operator.js +15 -0
  134. package/lib/server/create-server.d.ts +7 -0
  135. package/lib/server/create-server.js +122 -0
  136. package/lib/server/handlers/claimTeeBundle.d.ts +6 -0
  137. package/lib/server/handlers/claimTeeBundle.js +206 -0
  138. package/lib/server/handlers/claimTunnel.d.ts +2 -0
  139. package/lib/server/handlers/claimTunnel.js +73 -0
  140. package/lib/server/handlers/completeClaimOnChain.d.ts +2 -0
  141. package/lib/server/handlers/completeClaimOnChain.js +22 -0
  142. package/lib/server/handlers/createClaimOnChain.d.ts +2 -0
  143. package/lib/server/handlers/createClaimOnChain.js +26 -0
  144. package/lib/server/handlers/createTaskOnMechain.d.ts +2 -0
  145. package/lib/server/handlers/createTaskOnMechain.js +47 -0
  146. package/lib/server/handlers/createTunnel.d.ts +2 -0
  147. package/lib/server/handlers/createTunnel.js +93 -0
  148. package/lib/server/handlers/disconnectTunnel.d.ts +2 -0
  149. package/lib/server/handlers/disconnectTunnel.js +5 -0
  150. package/lib/server/handlers/fetchCertificateBytes.d.ts +2 -0
  151. package/lib/server/handlers/fetchCertificateBytes.js +41 -0
  152. package/lib/server/handlers/index.d.ts +4 -0
  153. package/lib/server/handlers/index.js +22 -0
  154. package/lib/server/handlers/init.d.ts +2 -0
  155. package/lib/server/handlers/init.js +30 -0
  156. package/lib/server/handlers/toprf.d.ts +2 -0
  157. package/lib/server/handlers/toprf.js +16 -0
  158. package/lib/server/index.d.ts +4 -0
  159. package/lib/server/index.js +4 -0
  160. package/lib/server/provider-api.d.ts +9 -0
  161. package/lib/server/provider-api.js +98 -0
  162. package/lib/server/provider-store.d.ts +53 -0
  163. package/lib/server/provider-store.js +80 -0
  164. package/lib/server/session-api.d.ts +9 -0
  165. package/lib/server/session-api.js +95 -0
  166. package/lib/server/session-store.d.ts +14 -0
  167. package/lib/server/session-store.js +36 -0
  168. package/lib/server/socket.d.ts +13 -0
  169. package/lib/server/socket.js +109 -0
  170. package/lib/server/tunnels/make-tcp-tunnel.d.ts +22 -0
  171. package/lib/server/tunnels/make-tcp-tunnel.js +177 -0
  172. package/lib/server/utils/apm.d.ts +11 -0
  173. package/lib/server/utils/apm.js +36 -0
  174. package/lib/server/utils/assert-valid-claim-request.d.ts +31 -0
  175. package/lib/server/utils/assert-valid-claim-request.js +229 -0
  176. package/lib/server/utils/config-env.d.ts +1 -0
  177. package/lib/server/utils/config-env.js +4 -0
  178. package/lib/server/utils/dns.d.ts +1 -0
  179. package/lib/server/utils/dns.js +18 -0
  180. package/lib/server/utils/gcp-attestation.d.ts +17 -0
  181. package/lib/server/utils/gcp-attestation.js +289 -0
  182. package/lib/server/utils/generics.d.ts +22 -0
  183. package/lib/server/utils/generics.js +51 -0
  184. package/lib/server/utils/iso.d.ts +1 -0
  185. package/lib/server/utils/iso.js +256 -0
  186. package/lib/server/utils/keep-alive.d.ts +7 -0
  187. package/lib/server/utils/keep-alive.js +38 -0
  188. package/lib/server/utils/nitro-attestation.d.ts +33 -0
  189. package/lib/server/utils/nitro-attestation.js +325 -0
  190. package/lib/server/utils/process-handshake.d.ts +13 -0
  191. package/lib/server/utils/process-handshake.js +214 -0
  192. package/lib/server/utils/proxy-session.d.ts +1 -0
  193. package/lib/server/utils/proxy-session.js +6 -0
  194. package/lib/server/utils/tee-oprf-verification.d.ts +22 -0
  195. package/lib/server/utils/tee-oprf-verification.js +160 -0
  196. package/lib/server/utils/tee-transcript-reconstruction.d.ts +24 -0
  197. package/lib/server/utils/tee-transcript-reconstruction.js +187 -0
  198. package/lib/server/utils/tee-verification.d.ts +27 -0
  199. package/lib/server/utils/tee-verification.js +365 -0
  200. package/lib/server/utils/validation.d.ts +2 -0
  201. package/lib/server/utils/validation.js +38 -0
  202. package/lib/types/bgp.d.ts +11 -0
  203. package/lib/types/bgp.js +1 -0
  204. package/lib/types/claims.d.ts +73 -0
  205. package/lib/types/claims.js +1 -0
  206. package/lib/types/client.d.ts +163 -0
  207. package/lib/types/client.js +1 -0
  208. package/lib/types/general.d.ts +54 -0
  209. package/lib/types/general.js +1 -0
  210. package/lib/types/handlers.d.ts +10 -0
  211. package/lib/types/handlers.js +1 -0
  212. package/lib/types/index.d.ts +10 -0
  213. package/lib/types/index.js +10 -0
  214. package/lib/types/providers.d.ts +161 -0
  215. package/lib/types/providers.gen.d.ts +443 -0
  216. package/lib/types/providers.gen.js +10 -0
  217. package/lib/types/providers.js +1 -0
  218. package/lib/types/rpc.d.ts +35 -0
  219. package/lib/types/rpc.js +1 -0
  220. package/lib/types/signatures.d.ts +28 -0
  221. package/lib/types/signatures.js +1 -0
  222. package/lib/types/tunnel.d.ts +18 -0
  223. package/lib/types/tunnel.js +1 -0
  224. package/lib/types/zk.d.ts +28 -0
  225. package/lib/types/zk.js +1 -0
  226. package/lib/utils/auth.d.ts +8 -0
  227. package/lib/utils/auth.js +59 -0
  228. package/lib/utils/b64-json.d.ts +2 -0
  229. package/lib/utils/b64-json.js +17 -0
  230. package/lib/utils/bgp-listener.d.ts +7 -0
  231. package/lib/utils/bgp-listener.js +119 -0
  232. package/lib/utils/claims.d.ts +33 -0
  233. package/lib/utils/claims.js +101 -0
  234. package/lib/utils/env.d.ts +3 -0
  235. package/lib/utils/env.js +15 -0
  236. package/lib/utils/error.d.ts +26 -0
  237. package/lib/utils/error.js +50 -0
  238. package/lib/utils/generics.d.ts +114 -0
  239. package/lib/utils/generics.js +317 -0
  240. package/lib/utils/http-parser.d.ts +59 -0
  241. package/lib/utils/http-parser.js +246 -0
  242. package/lib/utils/index.d.ts +13 -0
  243. package/lib/utils/index.js +13 -0
  244. package/lib/utils/logger.d.ts +13 -0
  245. package/lib/utils/logger.js +91 -0
  246. package/lib/utils/prepare-packets.d.ts +16 -0
  247. package/lib/utils/prepare-packets.js +62 -0
  248. package/lib/utils/redactions.d.ts +62 -0
  249. package/lib/utils/redactions.js +148 -0
  250. package/lib/utils/retries.d.ts +12 -0
  251. package/lib/utils/retries.js +24 -0
  252. package/lib/utils/signatures/eth.d.ts +2 -0
  253. package/lib/utils/signatures/eth.js +29 -0
  254. package/lib/utils/signatures/index.d.ts +5 -0
  255. package/lib/utils/signatures/index.js +7 -0
  256. package/lib/utils/socket-base.d.ts +23 -0
  257. package/lib/utils/socket-base.js +90 -0
  258. package/lib/utils/tls.d.ts +2 -0
  259. package/{src/utils/tls.ts → lib/utils/tls.js} +28 -35
  260. package/lib/utils/ws.d.ts +7 -0
  261. package/lib/utils/ws.js +22 -0
  262. package/lib/utils/zk.d.ts +70 -0
  263. package/lib/utils/zk.js +572 -0
  264. package/package.json +19 -12
  265. package/src/avs/abis/avsDirectoryABI.ts +0 -340
  266. package/src/avs/abis/delegationABI.ts +0 -1
  267. package/src/avs/abis/registryABI.ts +0 -725
  268. package/src/avs/client/create-claim-on-avs.ts +0 -206
  269. package/src/avs/config.ts +0 -25
  270. package/src/avs/contracts/ReclaimServiceManager.ts +0 -1457
  271. package/src/avs/contracts/common.ts +0 -44
  272. package/src/avs/contracts/factories/ReclaimServiceManager__factory.ts +0 -1213
  273. package/src/avs/tests/test.operator.ts +0 -413
  274. package/src/avs/tests/utils.ts +0 -51
  275. package/src/avs/types/index.ts +0 -60
  276. package/src/avs/utils/contracts.ts +0 -66
  277. package/src/avs/utils/register.ts +0 -125
  278. package/src/avs/utils/tasks.ts +0 -76
  279. package/src/client/create-claim.ts +0 -626
  280. package/src/client/index.ts +0 -3
  281. package/src/client/tunnels/make-rpc-tcp-tunnel.ts +0 -78
  282. package/src/client/tunnels/make-rpc-tls-tunnel.ts +0 -172
  283. package/src/client/utils/attestor-pool.ts +0 -35
  284. package/src/client/utils/client-socket.ts +0 -160
  285. package/src/client/utils/message-handler.ts +0 -116
  286. package/src/config/index.ts +0 -65
  287. package/src/external-rpc/benchmark.ts +0 -102
  288. package/src/external-rpc/event-bus.ts +0 -19
  289. package/src/external-rpc/global.d.ts +0 -20
  290. package/src/external-rpc/handle-incoming-msg.ts +0 -308
  291. package/src/external-rpc/index.ts +0 -3
  292. package/src/external-rpc/jsc-polyfills/1.ts +0 -117
  293. package/src/external-rpc/jsc-polyfills/2.ts +0 -24
  294. package/src/external-rpc/jsc-polyfills/event.ts +0 -16
  295. package/src/external-rpc/jsc-polyfills/index.ts +0 -2
  296. package/src/external-rpc/jsc-polyfills/ws.ts +0 -105
  297. package/src/external-rpc/setup-browser.ts +0 -42
  298. package/src/external-rpc/setup-jsc.ts +0 -48
  299. package/src/external-rpc/types.ts +0 -289
  300. package/src/external-rpc/utils.ts +0 -126
  301. package/src/external-rpc/zk.ts +0 -79
  302. package/src/index.ts +0 -9
  303. package/src/mechain/abis/governanceABI.ts +0 -458
  304. package/src/mechain/abis/taskABI.ts +0 -509
  305. package/src/mechain/client/create-claim-on-mechain.ts +0 -52
  306. package/src/mechain/client/index.ts +0 -1
  307. package/src/mechain/index.ts +0 -2
  308. package/src/mechain/types/index.ts +0 -29
  309. package/src/proto/api.ts +0 -5285
  310. package/src/proto/tee-bundle.ts +0 -1413
  311. package/src/providers/http/index.ts +0 -873
  312. package/src/providers/http/patch-parse5-tree.ts +0 -49
  313. package/src/providers/http/utils.ts +0 -439
  314. package/src/providers/index.ts +0 -8
  315. package/src/scripts/build-browser.sh +0 -9
  316. package/src/scripts/build-browser.ts +0 -40
  317. package/src/scripts/build-jsc.ts +0 -55
  318. package/src/scripts/check-avs-registration.ts +0 -38
  319. package/src/scripts/contract-data-gen.sh +0 -8
  320. package/src/scripts/fallbacks/crypto.ts +0 -1
  321. package/src/scripts/fallbacks/empty.ts +0 -2
  322. package/src/scripts/fallbacks/re2.ts +0 -5
  323. package/src/scripts/fallbacks/snarkjs.ts +0 -5
  324. package/src/scripts/generate-certs.sh +0 -11
  325. package/src/scripts/generate-proto.sh +0 -5
  326. package/src/scripts/generate-provider-types.ts +0 -121
  327. package/src/scripts/generate-receipt.ts +0 -138
  328. package/src/scripts/generate-toprf-keys.ts +0 -30
  329. package/src/scripts/jsc-cli-rpc.ts +0 -48
  330. package/src/scripts/register-avs-operator.ts +0 -5
  331. package/src/scripts/start-server.ts +0 -17
  332. package/src/scripts/update-avs-metadata.ts +0 -26
  333. package/src/scripts/utils.ts +0 -8
  334. package/src/scripts/whitelist-operator.ts +0 -22
  335. package/src/server/create-server.ts +0 -169
  336. package/src/server/handlers/claimTeeBundle.ts +0 -308
  337. package/src/server/handlers/claimTunnel.ts +0 -106
  338. package/src/server/handlers/completeClaimOnChain.ts +0 -36
  339. package/src/server/handlers/createClaimOnChain.ts +0 -39
  340. package/src/server/handlers/createTaskOnMechain.ts +0 -80
  341. package/src/server/handlers/createTunnel.ts +0 -128
  342. package/src/server/handlers/disconnectTunnel.ts +0 -11
  343. package/src/server/handlers/fetchCertificateBytes.ts +0 -66
  344. package/src/server/handlers/index.ts +0 -24
  345. package/src/server/handlers/init.ts +0 -46
  346. package/src/server/handlers/toprf.ts +0 -25
  347. package/src/server/index.ts +0 -4
  348. package/src/server/provider-api.ts +0 -118
  349. package/src/server/provider-store.ts +0 -117
  350. package/src/server/session-api.ts +0 -115
  351. package/src/server/session-store.ts +0 -60
  352. package/src/server/socket.ts +0 -156
  353. package/src/server/tunnels/make-tcp-tunnel.ts +0 -275
  354. package/src/server/utils/apm.ts +0 -49
  355. package/src/server/utils/assert-valid-claim-request.ts +0 -375
  356. package/src/server/utils/config-env.ts +0 -6
  357. package/src/server/utils/dns.ts +0 -25
  358. package/src/server/utils/gcp-attestation.ts +0 -415
  359. package/src/server/utils/generics.ts +0 -68
  360. package/src/server/utils/iso.ts +0 -258
  361. package/src/server/utils/keep-alive.ts +0 -50
  362. package/src/server/utils/nitro-attestation.ts +0 -396
  363. package/src/server/utils/process-handshake.ts +0 -311
  364. package/src/server/utils/proxy-session.ts +0 -6
  365. package/src/server/utils/tee-oprf-verification.ts +0 -231
  366. package/src/server/utils/tee-transcript-reconstruction.ts +0 -254
  367. package/src/server/utils/tee-verification.ts +0 -513
  368. package/src/server/utils/validation.ts +0 -57
  369. package/src/tests/auth.test.ts +0 -105
  370. package/src/tests/bgp-listener.test.ts +0 -193
  371. package/src/tests/claim-creation.test.ts +0 -415
  372. package/src/tests/describe-with-server.ts +0 -94
  373. package/src/tests/gcp-attestation.test.ts +0 -206
  374. package/src/tests/http-parser.test.ts +0 -135
  375. package/src/tests/http-provider-utils.test.ts +0 -3306
  376. package/src/tests/http-provider.test.ts +0 -125
  377. package/src/tests/jsc.test_mac.ts +0 -296
  378. package/src/tests/mock-provider-server.ts +0 -106
  379. package/src/tests/mocks.ts +0 -25
  380. package/src/tests/proof_bundle.bin +0 -0
  381. package/src/tests/rpc-communication.test.ts +0 -115
  382. package/src/tests/rpc-tunnel.test.ts +0 -239
  383. package/src/tests/signatures.test.ts +0 -37
  384. package/src/tests/tcp-tunnel.test.ts +0 -154
  385. package/src/tests/tee-bundle.test.ts +0 -321
  386. package/src/tests/tee-signatures.test.ts +0 -81
  387. package/src/tests/utils.ts +0 -108
  388. package/src/tests/verification_bundle.pb +0 -0
  389. package/src/tests/verification_bundle_tee.pb +0 -0
  390. package/src/tests/zk.test.ts +0 -453
  391. package/src/types/bgp.ts +0 -17
  392. package/src/types/claims.ts +0 -79
  393. package/src/types/client.ts +0 -205
  394. package/src/types/general.ts +0 -61
  395. package/src/types/handlers.ts +0 -16
  396. package/src/types/index.ts +0 -10
  397. package/src/types/providers.gen.ts +0 -135
  398. package/src/types/providers.ts +0 -203
  399. package/src/types/rpc.ts +0 -46
  400. package/src/types/signatures.ts +0 -29
  401. package/src/types/tunnel.ts +0 -25
  402. package/src/types/zk.ts +0 -31
  403. package/src/utils/auth.ts +0 -92
  404. package/src/utils/b64-json.ts +0 -25
  405. package/src/utils/bgp-listener.ts +0 -159
  406. package/src/utils/claims.ts +0 -132
  407. package/src/utils/env.ts +0 -21
  408. package/src/utils/error.ts +0 -76
  409. package/src/utils/generics.ts +0 -429
  410. package/src/utils/http-parser.ts +0 -312
  411. package/src/utils/index.ts +0 -13
  412. package/src/utils/logger.ts +0 -114
  413. package/src/utils/prepare-packets.ts +0 -98
  414. package/src/utils/redactions.ts +0 -203
  415. package/src/utils/retries.ts +0 -41
  416. package/src/utils/signatures/eth.ts +0 -35
  417. package/src/utils/signatures/index.ts +0 -11
  418. package/src/utils/socket-base.ts +0 -132
  419. package/src/utils/ws.ts +0 -30
  420. package/src/utils/zk.ts +0 -908
@@ -0,0 +1,246 @@
1
+ import { asciiToUint8Array, concatenateUint8Arrays } from '@joclaim/tls';
2
+ import { findIndexInUint8Array, uint8ArrayToStr } from '../utils/generics.js';
3
+ import { REDACTION_CHAR_CODE } from '../utils/redactions.js';
4
+ const HTTP_HEADER_LINE_END = asciiToUint8Array('\r\n');
5
+ /**
6
+ * parses http/1.1 responses
7
+ */
8
+ export function makeHttpResponseParser() {
9
+ /** the HTTP response data */
10
+ const res = {
11
+ statusCode: 0,
12
+ statusMessage: '',
13
+ headers: {},
14
+ body: new Uint8Array(),
15
+ complete: false,
16
+ headersComplete: false,
17
+ headerIndices: new Map(),
18
+ headerEndIdx: 0
19
+ };
20
+ let remainingBodyBytes = 0;
21
+ let isChunked = false;
22
+ let remaining = new Uint8Array();
23
+ let currentByteIdx = 0;
24
+ return {
25
+ res,
26
+ /**
27
+ * Parse the next chunk of data
28
+ * @param data the data to parse
29
+ */
30
+ onChunk(data) {
31
+ // concatenate the remaining data from the last chunk
32
+ remaining = concatenateUint8Arrays([remaining, data]);
33
+ // if we don't have the headers yet, keep reading lines
34
+ // as each header is in a line
35
+ if (!res.headersComplete) {
36
+ for (let line = getLine(); typeof line !== 'undefined'; line = getLine()) {
37
+ // first line is the HTTP version, status code & message
38
+ if (!res.statusCode) {
39
+ const [, statusCode, statusMessage] = line.match(/HTTP\/\d\.\d (\d+) (.*)/) || [];
40
+ res.statusCode = Number(statusCode);
41
+ res.statusMessage = statusMessage;
42
+ res.statusLineEndIndex = currentByteIdx - HTTP_HEADER_LINE_END.length;
43
+ }
44
+ else if (line === '') { // empty line signifies end of headers
45
+ res.headersComplete = true;
46
+ res.headerEndIdx = currentByteIdx - 4;
47
+ // if the response is chunked, we need to process the body differently
48
+ if (res.headers['transfer-encoding']?.includes('chunked')) {
49
+ isChunked = true;
50
+ res.chunks = [];
51
+ break;
52
+ // if the response has a content-length, we know how many bytes to read
53
+ }
54
+ else if (res.headers['content-length']) {
55
+ remainingBodyBytes = Number(res.headers['content-length']);
56
+ break;
57
+ }
58
+ else {
59
+ remainingBodyBytes = -1;
60
+ break;
61
+ // otherwise,
62
+ // no content-length, no chunked transfer encoding
63
+ // means wait till the stream ends
64
+ // https://stackoverflow.com/a/11376887
65
+ }
66
+ }
67
+ else if (!res.complete) { // parse the header
68
+ const [key, value] = line.split(': ');
69
+ res.headers[key.toLowerCase()] = value;
70
+ res.headerIndices[key.toLowerCase()] = {
71
+ fromIndex: currentByteIdx - line.length - HTTP_HEADER_LINE_END.length,
72
+ toIndex: currentByteIdx - HTTP_HEADER_LINE_END.length
73
+ };
74
+ }
75
+ else {
76
+ throw new Error('got more data after response was complete');
77
+ }
78
+ }
79
+ }
80
+ if (res.headersComplete) {
81
+ if (remainingBodyBytes) {
82
+ readBody();
83
+ // if no more body bytes to read,
84
+ // and the response was not chunked we're done
85
+ if (!remainingBodyBytes && !isChunked) {
86
+ res.complete = true;
87
+ }
88
+ }
89
+ if (res.headers['content-length'] === '0') {
90
+ res.complete = true;
91
+ }
92
+ if (isChunked) {
93
+ for (let line = getLine(); typeof line !== 'undefined'; line = getLine()) {
94
+ if (line === '') {
95
+ continue;
96
+ }
97
+ const chunkSize = Number.parseInt(line, 16);
98
+ // if chunk size is 0, we're done
99
+ if (!chunkSize) {
100
+ res.complete = true;
101
+ continue;
102
+ }
103
+ res.chunks?.push({
104
+ fromIndex: currentByteIdx,
105
+ toIndex: currentByteIdx + chunkSize,
106
+ });
107
+ // otherwise read the chunk
108
+ remainingBodyBytes = chunkSize;
109
+ readBody();
110
+ // if we read all the data we had,
111
+ // but there's still data left,
112
+ // break the loop and wait for the next chunk
113
+ if (remainingBodyBytes) {
114
+ break;
115
+ }
116
+ }
117
+ }
118
+ }
119
+ },
120
+ /**
121
+ * Call to prevent further parsing; indicating the end of the request
122
+ * Checks that the response is valid & complete, otherwise throws an error
123
+ */
124
+ streamEnded() {
125
+ if (!res.headersComplete) {
126
+ throw new Error('stream ended before headers were complete');
127
+ }
128
+ if (remaining.length) {
129
+ throw new Error('stream ended before remaining data arrived');
130
+ }
131
+ if (remainingBodyBytes > 0) {
132
+ throw new Error('stream ended before all body bytes were received');
133
+ }
134
+ res.complete = true;
135
+ }
136
+ };
137
+ function readBody() {
138
+ if (res.complete) {
139
+ throw new Error('got more data after response was complete');
140
+ }
141
+ if (!res.bodyStartIndex) {
142
+ res.bodyStartIndex = currentByteIdx;
143
+ }
144
+ let bytesToCopy;
145
+ if (remainingBodyBytes === -1) {
146
+ // all bytes are body bytes
147
+ bytesToCopy = remaining.length;
148
+ }
149
+ else {
150
+ // take the number of bytes we need to read, or the number of bytes remaining
151
+ // and append to the bytes of the body
152
+ bytesToCopy = Math.min(remainingBodyBytes, remaining.length);
153
+ remainingBodyBytes -= bytesToCopy;
154
+ }
155
+ res.body = concatenateUint8Arrays([
156
+ res.body,
157
+ remaining.slice(0, bytesToCopy)
158
+ ]);
159
+ remaining = remaining.slice(bytesToCopy);
160
+ currentByteIdx += bytesToCopy;
161
+ }
162
+ function getLine() {
163
+ // find end of line, if it exists
164
+ // otherwise return undefined
165
+ const idx = findIndexInUint8Array(remaining, HTTP_HEADER_LINE_END);
166
+ if (idx === -1) {
167
+ return undefined;
168
+ }
169
+ const line = uint8ArrayToStr(remaining.slice(0, idx));
170
+ remaining = remaining.slice(idx + HTTP_HEADER_LINE_END.length);
171
+ currentByteIdx += idx + HTTP_HEADER_LINE_END.length;
172
+ return line;
173
+ }
174
+ }
175
+ /**
176
+ * Read the HTTP request from a TLS receipt transcript.
177
+ * @param receipt the transcript to read from or application messages if they were extracted beforehand
178
+ * @returns the parsed HTTP request
179
+ */
180
+ export function getHttpRequestDataFromTranscript(receipt) {
181
+ const clientMsgs = receipt
182
+ .filter(s => s.sender === 'client');
183
+ // if the first message is redacted, we can't parse it
184
+ // as we don't know what the request was
185
+ if (clientMsgs[0].message[0] === REDACTION_CHAR_CODE) {
186
+ throw new Error('First client message request is redacted. Cannot parse');
187
+ }
188
+ const request = {
189
+ method: '',
190
+ url: '',
191
+ protocol: '',
192
+ headers: {}
193
+ };
194
+ let requestBuffer = concatenateUint8Arrays(clientMsgs.map(m => m.message));
195
+ // keep reading lines until we get to the end of the headers
196
+ for (let line = getLine(); typeof line !== 'undefined'; line = getLine()) {
197
+ if (line === '') {
198
+ break;
199
+ }
200
+ if (!request.method) {
201
+ const [, method, url, protocol] = line.match(/(\w+) (.*) (.*)/) || [];
202
+ request.method = method.toLowerCase();
203
+ request.url = url;
204
+ request.protocol = protocol;
205
+ }
206
+ else {
207
+ let keyIdx = line.indexOf(':');
208
+ if (keyIdx === -1) {
209
+ keyIdx = line.length - 1;
210
+ }
211
+ const key = line.slice(0, keyIdx)
212
+ .toLowerCase()
213
+ .trim();
214
+ const value = line.slice(keyIdx + 1)
215
+ .trim();
216
+ const oldValue = request.headers[key];
217
+ if (typeof oldValue === 'string') {
218
+ request.headers[key] = [oldValue, value];
219
+ }
220
+ else if (Array.isArray(oldValue)) {
221
+ oldValue.push(value);
222
+ }
223
+ else {
224
+ request.headers[key] = value;
225
+ }
226
+ }
227
+ }
228
+ //the rest is request body
229
+ if (requestBuffer.length) {
230
+ request.body = requestBuffer;
231
+ }
232
+ if (!request.method) {
233
+ throw new Error('Client request is incomplete');
234
+ }
235
+ return request;
236
+ function getLine() {
237
+ const idx = findIndexInUint8Array(requestBuffer, HTTP_HEADER_LINE_END);
238
+ if (idx === -1) {
239
+ return undefined;
240
+ }
241
+ const line = uint8ArrayToStr(requestBuffer.slice(0, idx));
242
+ requestBuffer = requestBuffer
243
+ .slice(idx + HTTP_HEADER_LINE_END.length);
244
+ return line;
245
+ }
246
+ }
@@ -0,0 +1,13 @@
1
+ export * from './generics.ts';
2
+ export * from './logger.ts';
3
+ export * from './redactions.ts';
4
+ export * from './http-parser.ts';
5
+ export * from './zk.ts';
6
+ export * from './claims.ts';
7
+ export * from './error.ts';
8
+ export * from './prepare-packets.ts';
9
+ export * from './signatures/index.ts';
10
+ export * from './auth.ts';
11
+ export * from './b64-json.ts';
12
+ export * from './bgp-listener.ts';
13
+ export * from './tls.ts';
@@ -0,0 +1,13 @@
1
+ export * from "./generics.js";
2
+ export * from "./logger.js";
3
+ export * from "./redactions.js";
4
+ export * from "./http-parser.js";
5
+ export * from "./zk.js";
6
+ export * from "./claims.js";
7
+ export * from "./error.js";
8
+ export * from "./prepare-packets.js";
9
+ export * from "./signatures/index.js";
10
+ export * from "./auth.js";
11
+ export * from "./b64-json.js";
12
+ export * from "./bgp-listener.js";
13
+ export * from "./tls.js";
@@ -0,0 +1,13 @@
1
+ import type { LogLevel } from '../types/index.js';
2
+ export declare let logger: import("pino").Logger<never, boolean>;
3
+ /**
4
+ * Creates a logger instance with optional redaction of PII.
5
+ * Replaces default logger
6
+ * See PII_PROPERTIES for the list of properties that will be redacted.
7
+ *
8
+ * @param redactPii - whether to redact PII from logs
9
+ * @param level - the log level to use
10
+ * @param onLog - a callback to call when a log is written
11
+ */
12
+ export declare function makeLogger(redactPii: boolean, level?: LogLevel, onLog?: (level: LogLevel, log: any) => void): import("pino").Logger<never, boolean>;
13
+ export declare function redact(json: any): any;
@@ -0,0 +1,91 @@
1
+ import { pino, stdTimeFunctions } from 'pino';
2
+ import { getEnvVariable } from '../utils/env.js';
3
+ const PII_PROPERTIES = ['ownerPrivateKey', 'secretParams'];
4
+ const redactedText = '[REDACTED]';
5
+ const envLevel = getEnvVariable('LOG_LEVEL');
6
+ export let logger = pino();
7
+ makeLogger(false, envLevel);
8
+ /**
9
+ * Creates a logger instance with optional redaction of PII.
10
+ * Replaces default logger
11
+ * See PII_PROPERTIES for the list of properties that will be redacted.
12
+ *
13
+ * @param redactPii - whether to redact PII from logs
14
+ * @param level - the log level to use
15
+ * @param onLog - a callback to call when a log is written
16
+ */
17
+ export function makeLogger(redactPii, level, onLog) {
18
+ const opts = {
19
+ // Log human readable time stamps instead of epoch time
20
+ timestamp: stdTimeFunctions.isoTime,
21
+ };
22
+ if (redactPii) {
23
+ opts.formatters = { log: redact };
24
+ opts.serializers = { redact };
25
+ opts.browser = {
26
+ write: {
27
+ fatal: log => writeLog('fatal', log),
28
+ error: log => writeLog('error', log),
29
+ warn: log => writeLog('warn', log),
30
+ info: log => writeLog('info', log),
31
+ debug: log => writeLog('debug', log),
32
+ trace: log => writeLog('trace', log),
33
+ }
34
+ };
35
+ }
36
+ const pLogger = pino(opts);
37
+ pLogger.level = level || 'info';
38
+ logger = pLogger;
39
+ return pLogger;
40
+ function writeLog(level, log) {
41
+ log = redact(log);
42
+ const { msg, ...obj } = log;
43
+ if (console[level]) {
44
+ console[level](obj, msg);
45
+ }
46
+ else {
47
+ console.log(obj, msg);
48
+ }
49
+ onLog?.(level, log);
50
+ }
51
+ }
52
+ function isObjectProperty(property) {
53
+ return (typeof property) === 'object'
54
+ && !Array.isArray(property)
55
+ && property !== null;
56
+ }
57
+ function getReplacer() {
58
+ // Store references to previously visited objects
59
+ const references = new WeakSet();
60
+ return function (key, value) {
61
+ const isObject = (typeof value) === 'object' && value !== null;
62
+ if (isObject) {
63
+ if (references.has(value)) {
64
+ return '[CIRCULAR]';
65
+ }
66
+ references.add(value);
67
+ }
68
+ return value;
69
+ };
70
+ }
71
+ export function redact(json) {
72
+ const isObject = isObjectProperty(json);
73
+ if (!isObject && !Array.isArray(json)) {
74
+ return json;
75
+ }
76
+ const redacted = JSON.parse(JSON.stringify(json, getReplacer()));
77
+ for (const prop in redacted) {
78
+ if (PII_PROPERTIES.includes(prop)) {
79
+ redacted[prop] = redactedText;
80
+ }
81
+ if (Array.isArray(redacted[prop])) {
82
+ for (const [index, value] of redacted[prop].entries()) {
83
+ redacted[prop][index] = redact(value);
84
+ }
85
+ }
86
+ else if (isObjectProperty(redacted[prop])) {
87
+ redacted[prop] = redact(redacted[prop]);
88
+ }
89
+ }
90
+ return redacted;
91
+ }
@@ -0,0 +1,16 @@
1
+ import type { CipherSuite, TLSPacketContext } from '@joclaim/tls';
2
+ import type { ClaimTunnelRequest_TranscriptMessage as TranscriptMessage } from '../proto/api.js';
3
+ import type { CompleteTLSPacket, Logger, MessageRevealInfo, PrepareZKProofsBaseOpts, Transcript } from '../types/index.js';
4
+ export type PreparePacketsForRevealOpts = {
5
+ cipherSuite: CipherSuite;
6
+ logger: Logger;
7
+ /**
8
+ * Progress of Zk proof generation
9
+ */
10
+ onZkProgress?(blocksDone: number, totalBlocks: number): void;
11
+ } & PrepareZKProofsBaseOpts;
12
+ /**
13
+ * Prepares the packets for reveal to the server
14
+ * according to the specified reveal type
15
+ */
16
+ export declare function preparePacketsForReveal(tlsTranscript: Transcript<CompleteTLSPacket>, reveals: Map<TLSPacketContext, MessageRevealInfo>, { onZkProgress, ...opts }: PreparePacketsForRevealOpts): Promise<TranscriptMessage[]>;
@@ -0,0 +1,62 @@
1
+ import { concatenateUint8Arrays, crypto } from '@joclaim/tls';
2
+ import { TranscriptMessageSenderType } from '../proto/api.js';
3
+ import { makeZkProofGenerator } from '../utils/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
19
+ };
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
+ await proofGenerator.addPacketToProve(message, reveal, (proofs, toprfs) => (msg.reveal = { zkReveal: { proofs, toprfs } }), () => {
44
+ const next = tlsTranscript
45
+ .slice(i + 1)
46
+ .find(t => t.sender === sender);
47
+ return next?.message;
48
+ });
49
+ break;
50
+ default:
51
+ // no reveal
52
+ break;
53
+ }
54
+ }));
55
+ const zkPacketsTotal = proofGenerator.getTotalChunksToProve();
56
+ onZkProgress?.(zkPacketsDone, zkPacketsTotal);
57
+ await proofGenerator.generateProofs(() => {
58
+ zkPacketsDone += 1;
59
+ onZkProgress?.(zkPacketsDone, zkPacketsTotal);
60
+ });
61
+ return transcript;
62
+ }
@@ -0,0 +1,62 @@
1
+ import type { ArraySlice, RedactedOrHashedArraySlice, TOPRFProofParams } from '../types/index.js';
2
+ export declare const REDACTION_CHAR = "*";
3
+ export declare const REDACTION_CHAR_CODE: number;
4
+ type SliceWithReveal<T> = {
5
+ block: T;
6
+ redactedPlaintext: Uint8Array;
7
+ /**
8
+ * If the block has some TOPRF claims -- they'll be set here
9
+ */
10
+ toprfs?: TOPRFProofParams[];
11
+ /**
12
+ * If text was replaced in the previous block w TOPRF but
13
+ * it overshot into this block. The "length" specifies how much
14
+ * of it got overshot into this block
15
+ */
16
+ overshotToprfFromPrevBlock?: {
17
+ length: number;
18
+ };
19
+ };
20
+ export type RevealedSlices<T> = 'all' | SliceWithReveal<T>[];
21
+ /**
22
+ * Check if a redacted string is congruent with the original string.
23
+ * @param redacted the redacted content, redacted content is replaced by '*'
24
+ * @param original the original content
25
+ */
26
+ export declare function isRedactionCongruent<T extends string | Uint8Array>(redacted: T, original: T): boolean;
27
+ /**
28
+ * Is the string fully redacted?
29
+ */
30
+ export declare function isFullyRedacted<T extends string | Uint8Array>(redacted: T): boolean;
31
+ /**
32
+ * Given some plaintext blocks and a redaction function, return the blocks that
33
+ * need to be revealed to the other party
34
+ *
35
+ * Use case: we get the response for a request in several blocks, and want to redact
36
+ * pieces that go through multiple blocks. We can use this function to get the
37
+ * blocks that need to be revealed to the other party
38
+ *
39
+ * @example if we received ["secret is 12","345","678. Thanks"]. We'd want
40
+ * to redact the "12345678" and reveal the rest. We'd pass in the blocks and
41
+ * the redact function will return the redactions, namely [10,19].
42
+ * The function will return the blocks ["secret is **","***. Thanks"].
43
+ * The middle block is fully redacted, so it's not returned
44
+ *
45
+ * @param blocks blocks to reveal
46
+ * @param redact function that returns the redactions
47
+ * @returns blocks to reveal
48
+ */
49
+ export declare function getBlocksToReveal<T extends {
50
+ plaintext: Uint8Array;
51
+ }>(blocks: T[], redact: (total: Uint8Array) => RedactedOrHashedArraySlice[], performOprf: (plaintext: Uint8Array) => Promise<TOPRFProofParams>): Promise<"all" | SliceWithReveal<T>[]>;
52
+ /**
53
+ * Redact the following slices from the total
54
+ */
55
+ export declare function redactSlices(total: Uint8Array, slices: ArraySlice[]): Uint8Array<ArrayBuffer>;
56
+ /**
57
+ * Converts the binary hash to an ASCII string of the expected length.
58
+ * If the hash is shorter than the expected length, it will be padded with
59
+ * '0' characters. If it's longer, it will be truncated.
60
+ */
61
+ export declare function binaryHashToStr(hash: Uint8Array, expLength: number): string;
62
+ export {};
@@ -0,0 +1,148 @@
1
+ import { base64Encode } from '@bufbuild/protobuf/wire';
2
+ import { concatenateUint8Arrays } from '@joclaim/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
+ // eslint-disable-next-line unicorn/no-for-loop
12
+ for (let i = 0; i < redacted.length; i++) {
13
+ const element = redacted[i];
14
+ const areSame = element === original[i]
15
+ || (typeof element === 'string' && element === REDACTION_CHAR)
16
+ || (typeof element === 'number' && element === REDACTION_CHAR_CODE);
17
+ if (!areSame) {
18
+ return false;
19
+ }
20
+ }
21
+ return true;
22
+ }
23
+ /**
24
+ * Is the string fully redacted?
25
+ */
26
+ export function isFullyRedacted(redacted) {
27
+ for (const element of redacted) {
28
+ if (element !== REDACTION_CHAR
29
+ && element !== REDACTION_CHAR_CODE) {
30
+ return false;
31
+ }
32
+ }
33
+ return true;
34
+ }
35
+ /**
36
+ * Given some plaintext blocks and a redaction function, return the blocks that
37
+ * need to be revealed to the other party
38
+ *
39
+ * Use case: we get the response for a request in several blocks, and want to redact
40
+ * pieces that go through multiple blocks. We can use this function to get the
41
+ * blocks that need to be revealed to the other party
42
+ *
43
+ * @example if we received ["secret is 12","345","678. Thanks"]. We'd want
44
+ * to redact the "12345678" and reveal the rest. We'd pass in the blocks and
45
+ * the redact function will return the redactions, namely [10,19].
46
+ * The function will return the blocks ["secret is **","***. Thanks"].
47
+ * The middle block is fully redacted, so it's not returned
48
+ *
49
+ * @param blocks blocks to reveal
50
+ * @param redact function that returns the redactions
51
+ * @returns blocks to reveal
52
+ */
53
+ export async function getBlocksToReveal(blocks, redact, performOprf) {
54
+ const slicesWithReveal = blocks.map(block => ({
55
+ block,
56
+ // copy the plaintext to avoid mutating the original
57
+ redactedPlaintext: new Uint8Array(block.plaintext)
58
+ }));
59
+ const total = concatenateUint8Arrays(blocks.map(b => b.plaintext));
60
+ const redactions = redact(total);
61
+ if (!redactions.length) {
62
+ return 'all';
63
+ }
64
+ let blockIdx = 0;
65
+ let cursorInBlock = 0;
66
+ let cursor = 0;
67
+ for (const redaction of redactions) {
68
+ await redactBlocks(redaction);
69
+ }
70
+ // only reveal blocks that have some data to reveal,
71
+ // or are completely plaintext
72
+ return slicesWithReveal
73
+ .filter(s => !isFullyRedacted(s.redactedPlaintext));
74
+ async function redactBlocks(slice) {
75
+ while (cursor < slice.fromIndex) {
76
+ advance();
77
+ }
78
+ if (slice.hash) {
79
+ const plaintext = total.slice(slice.fromIndex, slice.toIndex);
80
+ const { nullifier, responses, mask } = await performOprf(plaintext);
81
+ // set the TOPRF claim on the first blocks this
82
+ // redaction covers
83
+ const toprf = {
84
+ nullifier,
85
+ responses,
86
+ dataLocation: {
87
+ fromIndex: cursorInBlock,
88
+ length: slice.toIndex - slice.fromIndex
89
+ },
90
+ mask,
91
+ plaintext
92
+ };
93
+ const startBlockIdx = blockIdx;
94
+ const block = slicesWithReveal[blockIdx];
95
+ block.toprfs ||= [];
96
+ block.toprfs.push(toprf);
97
+ const nullifierStr = binaryHashToStr(nullifier, toprf.dataLocation.length);
98
+ let i = 0;
99
+ let overshootLen = 0;
100
+ while (cursor < slice.toIndex) {
101
+ if (blockIdx !== startBlockIdx) {
102
+ overshootLen += 1;
103
+ }
104
+ slicesWithReveal[blockIdx].redactedPlaintext[cursorInBlock]
105
+ = nullifierStr.charCodeAt(i);
106
+ advance();
107
+ i += 1;
108
+ }
109
+ if (overshootLen) {
110
+ slicesWithReveal[blockIdx]
111
+ .overshotToprfFromPrevBlock = { length: overshootLen };
112
+ }
113
+ }
114
+ while (cursor < slice.toIndex) {
115
+ slicesWithReveal[blockIdx]
116
+ .redactedPlaintext[cursorInBlock] = REDACTION_CHAR_CODE;
117
+ advance();
118
+ }
119
+ }
120
+ function advance() {
121
+ cursor += 1;
122
+ cursorInBlock += 1;
123
+ if (cursorInBlock >= blocks[blockIdx].plaintext.length) {
124
+ blockIdx += 1;
125
+ cursorInBlock = 0;
126
+ }
127
+ }
128
+ }
129
+ /**
130
+ * Redact the following slices from the total
131
+ */
132
+ export function redactSlices(total, slices) {
133
+ const redacted = new Uint8Array(total);
134
+ for (const slice of slices) {
135
+ for (let i = slice.fromIndex; i < slice.toIndex; i++) {
136
+ redacted[i] = REDACTION_CHAR_CODE;
137
+ }
138
+ }
139
+ return redacted;
140
+ }
141
+ /**
142
+ * Converts the binary hash to an ASCII string of the expected length.
143
+ * If the hash is shorter than the expected length, it will be padded with
144
+ * '0' characters. If it's longer, it will be truncated.
145
+ */
146
+ export function binaryHashToStr(hash, expLength) {
147
+ return base64Encode(hash).padEnd(expLength, '0').slice(0, expLength);
148
+ }
@@ -0,0 +1,12 @@
1
+ import type { Logger } from '../types/index.js';
2
+ type RetryLoopOptions = {
3
+ maxRetries?: number;
4
+ logger: Logger;
5
+ shouldRetry: (error: Error) => boolean;
6
+ };
7
+ /**
8
+ * Execute a function, and upon failure -- retry
9
+ * based on specified options.
10
+ */
11
+ export declare function executeWithRetries<T>(code: (attempt: number) => Promise<T>, { maxRetries, shouldRetry, logger, }: RetryLoopOptions): Promise<T>;
12
+ export {};