@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,18 @@
1
+ import type { Provider, ProviderParams, ProviderSecretParams } from '../../types/index.js';
2
+ type HTTPProviderParams = ProviderParams<'http'>;
3
+ declare const HTTP_PROVIDER: Provider<'http'>;
4
+ export declare function substituteParamValues(currentParams: HTTPProviderParams, secretParams?: ProviderSecretParams<'http'>, ignoreMissingParams?: boolean): {
5
+ newParams: HTTPProviderParams;
6
+ extractedValues: {
7
+ [_: string]: string;
8
+ };
9
+ hiddenBodyParts: {
10
+ index: number;
11
+ length: number;
12
+ }[];
13
+ hiddenURLParts: {
14
+ index: number;
15
+ length: number;
16
+ }[];
17
+ };
18
+ export default HTTP_PROVIDER;
@@ -0,0 +1,658 @@
1
+ import { areUint8ArraysEqual, concatenateUint8Arrays, uint8ArrayToBinaryStr } from '@joclaim/tls';
2
+ import { utils } from 'ethers';
3
+ import { DEFAULT_HTTPS_PORT, RECLAIM_USER_AGENT } from '../../config/index.js';
4
+ import { AttestorVersion } from '../../proto/api.js';
5
+ import { buildHeaders, convertResponsePosToAbsolutePos, extractHTMLElementsIndexes, extractJSONValueIndexes, getRedactionsForChunkHeaders, makeRegex, matchRedactedStrings, parseHttpResponse, } from '../../providers/http/utils.js';
6
+ import { isValidProxySessionId } from '../../server/utils/proxy-session.js';
7
+ import { findIndexInUint8Array, getHttpRequestDataFromTranscript, logger, REDACTION_CHAR_CODE, strToUint8Array, uint8ArrayToStr, } from '../../utils/index.js';
8
+ const { base64 } = utils;
9
+ const OK_HTTP_HEADER = 'HTTP/1.1 200';
10
+ const dateHeaderRegex = '[dD]ate: ((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (?:[0-3][0-9]) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?:[0-9]{4}) (?:[01][0-9]|2[0-3])(?::[0-5][0-9]){2} GMT)';
11
+ const dateDiff = 1000 * 60 * 10; // allow 10 min difference
12
+ const HTTP_PROVIDER = {
13
+ hostPort: getHostPort,
14
+ writeRedactionMode(params) {
15
+ return ('writeRedactionMode' in params)
16
+ ? params.writeRedactionMode
17
+ : undefined;
18
+ },
19
+ geoLocation(params, secretParams) {
20
+ return ('geoLocation' in params)
21
+ ? getGeoLocation(params, secretParams)
22
+ : undefined;
23
+ },
24
+ proxySessionId(params, secretParams) {
25
+ return ('proxySessionId' in params)
26
+ ? getProxySessionId(params, secretParams)
27
+ : undefined;
28
+ },
29
+ additionalClientOptions(params) {
30
+ let defaultOptions = {
31
+ applicationLayerProtocols: ['http/1.1']
32
+ };
33
+ if ('additionalClientOptions' in params) {
34
+ defaultOptions = {
35
+ ...defaultOptions,
36
+ ...params.additionalClientOptions
37
+ };
38
+ }
39
+ return defaultOptions;
40
+ },
41
+ createRequest(secretParams, params, logger) {
42
+ if (!secretParams.cookieStr &&
43
+ !secretParams.authorisationHeader &&
44
+ !secretParams.headers) {
45
+ throw new Error('auth parameters are not set');
46
+ }
47
+ const pubHeaders = params.headers || {};
48
+ const secHeaders = { ...secretParams.headers };
49
+ if (secretParams.cookieStr) {
50
+ secHeaders['Cookie'] = secretParams.cookieStr;
51
+ }
52
+ if (secretParams.authorisationHeader) {
53
+ secHeaders['Authorization'] = secretParams.authorisationHeader;
54
+ }
55
+ const hasUserAgent = Object.keys(pubHeaders)
56
+ .some(k => k.toLowerCase() === 'user-agent') ||
57
+ Object.keys(secHeaders)
58
+ .some(k => k.toLowerCase() === 'user-agent');
59
+ if (!hasUserAgent) {
60
+ //only set user-agent if not set by provider
61
+ pubHeaders['User-Agent'] = RECLAIM_USER_AGENT;
62
+ }
63
+ const newParams = substituteParamValues(params, secretParams);
64
+ params = newParams.newParams;
65
+ const url = new URL(params.url);
66
+ const { pathname } = url;
67
+ const searchParams = params.url.includes('?') ? params.url.split('?')[1] : '';
68
+ logger.info({ url: params.url, path: pathname, query: searchParams.toString() });
69
+ const body = params.body instanceof Uint8Array
70
+ ? params.body
71
+ : strToUint8Array(params.body || '');
72
+ const contentLength = body.length;
73
+ const reqLine = `${params.method} ${pathname}${searchParams?.length ? '?' + searchParams : ''} HTTP/1.1`;
74
+ const secHeadersList = buildHeaders(secHeaders);
75
+ logger.info({ requestLine: reqLine });
76
+ const httpReqHeaderStr = [
77
+ reqLine,
78
+ `Host: ${getHostHeaderString(url)}`,
79
+ `Content-Length: ${contentLength}`,
80
+ 'Connection: close',
81
+ //no compression
82
+ 'Accept-Encoding: identity',
83
+ ...buildHeaders(pubHeaders),
84
+ ...secHeadersList,
85
+ '\r\n',
86
+ ].join('\r\n');
87
+ const headerStr = strToUint8Array(httpReqHeaderStr);
88
+ const data = concatenateUint8Arrays([headerStr, body]);
89
+ // hide all secret headers
90
+ const secHeadersStr = secHeadersList.join('\r\n');
91
+ const tokenStartIndex = findIndexInUint8Array(data, strToUint8Array(secHeadersStr));
92
+ const redactions = [
93
+ {
94
+ fromIndex: tokenStartIndex,
95
+ toIndex: tokenStartIndex + secHeadersStr.length,
96
+ }
97
+ ];
98
+ if (newParams.hiddenBodyParts?.length > 0) {
99
+ for (const hiddenBodyPart of newParams.hiddenBodyParts) {
100
+ if (hiddenBodyPart.length) {
101
+ redactions.push({
102
+ fromIndex: headerStr.length + hiddenBodyPart.index,
103
+ toIndex: headerStr.length + hiddenBodyPart.index + hiddenBodyPart.length,
104
+ });
105
+ }
106
+ }
107
+ }
108
+ if (newParams.hiddenURLParts?.length > 0) {
109
+ for (const hiddenURLPart of newParams.hiddenURLParts) {
110
+ if (hiddenURLPart.length) {
111
+ redactions.push({
112
+ fromIndex: hiddenURLPart.index,
113
+ toIndex: hiddenURLPart.index + hiddenURLPart.length,
114
+ });
115
+ }
116
+ }
117
+ }
118
+ redactions.sort((a, b) => a.toIndex - b.toIndex);
119
+ return {
120
+ data,
121
+ redactions: redactions,
122
+ };
123
+ },
124
+ getResponseRedactions({ response, params: rawParams, logger, ctx }) {
125
+ logger.debug({ response: base64.encode(response), params: rawParams });
126
+ const res = parseHttpResponse(response);
127
+ if (!rawParams.responseRedactions?.length) {
128
+ return [];
129
+ }
130
+ if (((res.statusCode / 100) >> 0) !== 2) {
131
+ logger.error({ response: base64.encode(response), params: rawParams });
132
+ throw new Error(`Expected status 2xx, got ${res.statusCode} (${res.statusMessage})`);
133
+ }
134
+ const newParams = substituteParamValues(rawParams, undefined, true);
135
+ const params = newParams.newParams;
136
+ const headerEndIndex = res.statusLineEndIndex;
137
+ const bodyStartIdx = res.bodyStartIndex ?? 0;
138
+ if (bodyStartIdx < 4) {
139
+ logger.error({ response: base64.encode(response) });
140
+ throw new Error('Failed to find response body');
141
+ }
142
+ const reveals = [
143
+ { fromIndex: 0, toIndex: headerEndIndex }
144
+ ];
145
+ //reveal double CRLF which separates headers from body
146
+ if (shouldRevealCrlf(ctx)) {
147
+ const crlfs = response
148
+ .slice(res.headerEndIdx, res.headerEndIdx + 4);
149
+ if (!areUint8ArraysEqual(crlfs, strToUint8Array('\r\n\r\n'))) {
150
+ logger.error({ response: base64.encode(response) });
151
+ throw new Error(`Failed to find header/body separator at index ${res.headerEndIdx}`);
152
+ }
153
+ }
154
+ reveals.push({ fromIndex: res.headerEndIdx, toIndex: res.headerEndIdx + 4 });
155
+ //reveal date header
156
+ if (res.headerIndices['date']) {
157
+ reveals.push(res.headerIndices['date']);
158
+ }
159
+ const body = uint8ArrayToBinaryStr(res.body);
160
+ const redactions = [];
161
+ for (const rs of params.responseRedactions || []) {
162
+ const processor = processRedactionRequest(body, rs, bodyStartIdx, res.chunks);
163
+ for (const { reveal, redactions: reds } of processor) {
164
+ reveals.push(reveal);
165
+ redactions.push(...reds);
166
+ }
167
+ }
168
+ reveals.sort((a, b) => a.toIndex - b.toIndex);
169
+ if (reveals.length > 1) {
170
+ let currentIndex = 0;
171
+ for (const r of reveals) {
172
+ if (currentIndex < r.fromIndex) {
173
+ redactions.push({ fromIndex: currentIndex, toIndex: r.fromIndex });
174
+ }
175
+ currentIndex = r.toIndex;
176
+ }
177
+ redactions.push({ fromIndex: currentIndex, toIndex: response.length });
178
+ }
179
+ for (const r of reveals) {
180
+ if (!r.hash) {
181
+ continue;
182
+ }
183
+ redactions.push(r);
184
+ }
185
+ redactions.sort((a, b) => a.toIndex - b.toIndex);
186
+ return redactions;
187
+ },
188
+ assertValidProviderReceipt({ receipt, params: paramsAny, logger, ctx }) {
189
+ logTranscript();
190
+ let extractedParams = {};
191
+ const secretParams = ('secretParams' in paramsAny)
192
+ ? paramsAny.secretParams
193
+ : undefined;
194
+ const newParams = substituteParamValues(paramsAny, secretParams, !secretParams);
195
+ const params = newParams.newParams;
196
+ extractedParams = { ...extractedParams, ...newParams.extractedValues };
197
+ const req = getHttpRequestDataFromTranscript(receipt);
198
+ if (req.method !== params.method.toLowerCase()) {
199
+ throw new Error(`Invalid method: ${req.method}`);
200
+ }
201
+ const url = new URL(params.url);
202
+ const { protocol, pathname } = url;
203
+ if (protocol !== 'https:') {
204
+ logger.error('params URL: %s', params.url);
205
+ throw new Error(`Expected protocol: https, found: ${protocol}`);
206
+ }
207
+ const searchParams = params.url.includes('?') ? params.url.split('?')[1] : '';
208
+ //brackets in URL path turn into %7B and %7D, so replace them back
209
+ const expectedPath = pathname.replaceAll('%7B', '{').replaceAll('%7D', '}') + (searchParams?.length ? '?' + searchParams : '');
210
+ if (!matchRedactedStrings(strToUint8Array(expectedPath), strToUint8Array(req.url))) {
211
+ logger.error('params URL: %s', params.url);
212
+ throw new Error(`Expected path: ${expectedPath}, found: ${req.url}`);
213
+ }
214
+ const expectedHostStr = getHostHeaderString(url);
215
+ if (req.headers.host !== expectedHostStr) {
216
+ throw new Error(`Expected host: ${expectedHostStr}, found: ${req.headers.host}`);
217
+ }
218
+ const connectionHeader = req.headers['connection'];
219
+ if (connectionHeader !== 'close') {
220
+ throw new Error(`Connection header must be "close", got "${connectionHeader}"`);
221
+ }
222
+ const serverBlocks = receipt
223
+ .filter(s => s.sender === 'server')
224
+ .map((r) => r.message)
225
+ // filter out fully redacted blocks
226
+ .filter(b => !b.every(b => b === REDACTION_CHAR_CODE));
227
+ const response = concatArrays(...serverBlocks);
228
+ let res;
229
+ res = uint8ArrayToStr(response);
230
+ const okRegex = makeRegex('^HTTP\\/1.1 2\\d{2}');
231
+ const matchRes = okRegex.exec(res);
232
+ if (!matchRes) {
233
+ const statusRegex = makeRegex('^HTTP\\/1.1 (\\d{3})');
234
+ const matchRes = statusRegex.exec(res);
235
+ if (matchRes && matchRes.length > 1) {
236
+ throw new Error(`Provider returned error ${matchRes[1]}`);
237
+ }
238
+ let lineEnd = res.indexOf('*');
239
+ if (lineEnd === -1) {
240
+ lineEnd = res.indexOf('\n');
241
+ }
242
+ if (lineEnd === -1) {
243
+ lineEnd = OK_HTTP_HEADER.length;
244
+ }
245
+ throw new Error(`Response did not start with \"HTTP/1.1 2XX\" got "${res.slice(0, lineEnd)}"`);
246
+ }
247
+ let bodyStart;
248
+ if (shouldRevealCrlf(ctx)) {
249
+ bodyStart = res.indexOf('\r\n\r\n', OK_HTTP_HEADER.length) + 4;
250
+ if (bodyStart < 4) {
251
+ throw new Error('Response body start not found');
252
+ }
253
+ }
254
+ else {
255
+ bodyStart = OK_HTTP_HEADER.length;
256
+ }
257
+ //validate server Date header if present
258
+ const dateHeader = makeRegex(dateHeaderRegex).exec(res);
259
+ if (dateHeader && dateHeader.length > 1) {
260
+ const serverDate = new Date(dateHeader[1]);
261
+ if ((Date.now() - serverDate.getTime()) > dateDiff) {
262
+ logger.info({ dateHeader: dateHeader[0], current: Date.now() }, 'date header is off');
263
+ // too many false positives
264
+ // throw new Error(
265
+ // `Server date is off by "${(Date.now() - serverDate.getTime()) / 1000} s"`
266
+ // )
267
+ }
268
+ }
269
+ const paramBody = params.body instanceof Uint8Array
270
+ ? params.body
271
+ : strToUint8Array(params.body || '');
272
+ if (paramBody.length > 0 && !matchRedactedStrings(paramBody, req.body)) {
273
+ throw new Error('request body mismatch');
274
+ }
275
+ //remove asterisks to account for chunks in the middle of revealed strings
276
+ if (!secretParams) {
277
+ res = res.slice(bodyStart).replace(/(\*){3,}/g, '');
278
+ }
279
+ for (const { type, value, invert } of params.responseMatches || []) {
280
+ const inv = Boolean(invert); // explicitly cast to boolean
281
+ switch (type) {
282
+ case 'regex':
283
+ const regexRes = makeRegex(value).exec(res);
284
+ const match = regexRes !== null;
285
+ if (match === inv) { // if both true or both false then fail
286
+ throw new Error('Invalid receipt.'
287
+ + ` Regex "${value}" ${invert ? 'matched' : 'didn\'t match'}`);
288
+ }
289
+ if (!match) {
290
+ continue;
291
+ }
292
+ const groups = regexRes?.groups;
293
+ for (const paramName in groups || []) {
294
+ if (paramName in extractedParams) {
295
+ throw new Error(`Duplicate parameter ${paramName}`);
296
+ }
297
+ const value = groups?.[paramName];
298
+ if (typeof value !== 'string') {
299
+ continue;
300
+ }
301
+ extractedParams[paramName] = value;
302
+ }
303
+ break;
304
+ case 'contains':
305
+ const includes = res.includes(value);
306
+ if (includes === inv) {
307
+ throw new Error(`Invalid receipt. Response ${invert ? 'contains' : 'does not contain'} "${value}"`);
308
+ }
309
+ break;
310
+ default:
311
+ throw new Error(`Invalid response match type ${type}`);
312
+ }
313
+ }
314
+ function concatArrays(...bufs) {
315
+ const totalSize = bufs.reduce((acc, e) => acc + e.length, 0);
316
+ const merged = new Uint8Array(totalSize);
317
+ let lenDone = 0;
318
+ for (const array of bufs) {
319
+ merged.set(array, lenDone);
320
+ lenDone += array.length;
321
+ }
322
+ return merged;
323
+ }
324
+ return { extractedParameters: extractedParams };
325
+ function logTranscript() {
326
+ const clientMsgs = receipt.filter(s => s.sender === 'client').map(m => m.message);
327
+ const serverMsgs = receipt.filter(s => s.sender === 'server').map(m => m.message);
328
+ const clientTranscript = base64.encode(concatenateUint8Arrays(clientMsgs));
329
+ const serverTranscript = base64.encode(concatenateUint8Arrays(serverMsgs));
330
+ logger.debug({ request: clientTranscript, response: serverTranscript, params: paramsAny });
331
+ }
332
+ },
333
+ };
334
+ // revealing CRLF is a breaking change -- and should only be done
335
+ // if the client's version supports it
336
+ function shouldRevealCrlf({ version }) {
337
+ return version >= AttestorVersion.ATTESTOR_VERSION_2_0_1;
338
+ }
339
+ function getHostPort(params, secretParams) {
340
+ const { host } = new URL(getURL(params, secretParams));
341
+ if (!host) {
342
+ throw new Error('url is incorrect');
343
+ }
344
+ return host;
345
+ }
346
+ /**
347
+ * Obtain the host header string from the URL.
348
+ * https://stackoverflow.com/a/3364396
349
+ */
350
+ function getHostHeaderString(url) {
351
+ const host = url.hostname;
352
+ const port = url.port;
353
+ return port && +port !== DEFAULT_HTTPS_PORT
354
+ ? `${host}:${port}`
355
+ : host;
356
+ }
357
+ const paramsRegex = /{{([^{}]+)}}/sgi;
358
+ function* processRedactionRequest(body, rs, bodyStartIdx, resChunks) {
359
+ let element = body;
360
+ let elementIdx = 0;
361
+ let elementLength = -1;
362
+ if (rs.xPath) {
363
+ const indexes = extractHTMLElementsIndexes(body, rs.xPath, !!rs.jsonPath);
364
+ for (const { start, end } of indexes) {
365
+ element = body.slice(start, end);
366
+ elementIdx = start;
367
+ elementLength = end - start;
368
+ if (rs.jsonPath) {
369
+ yield* processJsonPath();
370
+ }
371
+ else if (rs.regex) {
372
+ yield* processRegexp();
373
+ }
374
+ else {
375
+ yield* addRedaction();
376
+ }
377
+ }
378
+ }
379
+ else if (rs.jsonPath) {
380
+ yield* processJsonPath();
381
+ }
382
+ else if (rs.regex) {
383
+ yield* processRegexp();
384
+ }
385
+ else {
386
+ throw new Error('Expected either xPath, jsonPath or regex for redaction');
387
+ }
388
+ function* processJsonPath() {
389
+ const jsonPathIndexes = extractJSONValueIndexes(element, rs.jsonPath);
390
+ // eslint-disable-next-line max-depth
391
+ const eIndex = elementIdx;
392
+ for (const ji of jsonPathIndexes) {
393
+ const jStart = ji.start;
394
+ const jEnd = ji.end;
395
+ element = body.slice(eIndex + jStart, eIndex + jEnd);
396
+ elementIdx = eIndex + jStart;
397
+ elementLength = jEnd - jStart;
398
+ // eslint-disable-next-line max-depth
399
+ if (rs.regex) {
400
+ yield* processRegexp();
401
+ }
402
+ else {
403
+ yield* addRedaction();
404
+ }
405
+ }
406
+ }
407
+ function* processRegexp() {
408
+ logger.debug({
409
+ element: base64.encode(strToUint8Array(element)),
410
+ body: base64.encode(strToUint8Array(body))
411
+ });
412
+ const regexp = makeRegex(rs.regex);
413
+ const elem = element || body;
414
+ const match = regexp.exec(elem);
415
+ // eslint-disable-next-line max-depth
416
+ if (!match?.[0]) {
417
+ throw new Error(`regexp ${rs.regex} does not match found element '${base64.encode(strToUint8Array(elem))}'`);
418
+ }
419
+ elementIdx += match.index;
420
+ elementLength = regexp.lastIndex - match.index;
421
+ element = match[0];
422
+ if (rs.hash && (!match.groups || Object.keys(match.groups).length > 1)) {
423
+ throw new Error('Exactly one named capture group is needed per hashed redaction');
424
+ }
425
+ // if there are groups in the regex,
426
+ // we'll only hash the group values
427
+ if (!rs.hash || !match.groups) {
428
+ yield* addRedaction();
429
+ return;
430
+ }
431
+ const fullStr = match[0];
432
+ const grp = Object.values(match.groups)[0];
433
+ const grpIdx = fullStr.indexOf(grp);
434
+ // don't hash the entire regex, we'll hash the group values
435
+ elementLength = grpIdx;
436
+ element = fullStr.slice(0, grpIdx);
437
+ yield* addRedaction(null);
438
+ elementIdx += grpIdx;
439
+ element = grp;
440
+ elementLength = grp.length;
441
+ const reveal = getReveal(elementIdx, elementLength, rs.hash);
442
+ const chunkReds = getRedactionsForChunkHeaders(reveal.fromIndex, reveal.toIndex, resChunks);
443
+ if (chunkReds.length) {
444
+ throw new Error('Hash redactions cannot be performed if '
445
+ + 'the redacted string is split between 2'
446
+ + ' or more HTTP chunks');
447
+ }
448
+ yield { reveal, redactions: chunkReds };
449
+ elementIdx += grp.length;
450
+ element = fullStr.slice(grpIdx + grp.length);
451
+ elementLength = element.length;
452
+ yield* addRedaction(null);
453
+ }
454
+ // eslint-disable-next-line unicorn/consistent-function-scoping
455
+ function* addRedaction(hash = rs.hash, _resChunks = resChunks) {
456
+ if (elementIdx < 0 || !elementLength) {
457
+ return;
458
+ }
459
+ const reveal = getReveal(elementIdx, elementLength, hash || undefined);
460
+ yield {
461
+ reveal,
462
+ redactions: getRedactionsForChunkHeaders(reveal.fromIndex, reveal.toIndex, _resChunks)
463
+ };
464
+ }
465
+ function getReveal(startIdx, len, hash) {
466
+ const from = convertResponsePosToAbsolutePos(startIdx, bodyStartIdx, resChunks);
467
+ const to = convertResponsePosToAbsolutePos(startIdx + len, bodyStartIdx, resChunks);
468
+ return { fromIndex: from, toIndex: to, hash };
469
+ }
470
+ }
471
+ export function substituteParamValues(currentParams, secretParams, ignoreMissingParams) {
472
+ const params = JSON.parse(JSON.stringify(currentParams));
473
+ let extractedValues = {};
474
+ const hiddenURLParts = [];
475
+ const urlParams = extractAndReplaceTemplateValues(params.url, ignoreMissingParams);
476
+ if (urlParams) {
477
+ params.url = urlParams.newParam;
478
+ extractedValues = { ...urlParams.extractedValues };
479
+ if (urlParams.hiddenParts.length) {
480
+ const host = getHostHeaderString(new URL(params.url));
481
+ const offset = `https://${host}`.length - currentParams.method.length - 1; //space between method and start of the path
482
+ for (const hiddenURLPart of urlParams.hiddenParts) {
483
+ hiddenURLParts.push({ index: hiddenURLPart.index - offset, length: hiddenURLPart.length });
484
+ }
485
+ }
486
+ }
487
+ let bodyParams;
488
+ let hiddenBodyParts = [];
489
+ if (params.body) {
490
+ const strBody = typeof params.body === 'string' ? params.body : uint8ArrayToStr(params.body);
491
+ bodyParams = extractAndReplaceTemplateValues(strBody, ignoreMissingParams);
492
+ if (bodyParams) {
493
+ params.body = bodyParams.newParam;
494
+ extractedValues = { ...extractedValues, ...bodyParams.extractedValues };
495
+ hiddenBodyParts = bodyParams.hiddenParts;
496
+ }
497
+ }
498
+ const geoParams = extractAndReplaceTemplateValues(params.geoLocation);
499
+ if (geoParams) {
500
+ params.geoLocation = geoParams.newParam;
501
+ extractedValues = { ...extractedValues, ...geoParams.extractedValues };
502
+ }
503
+ const proxySessionIdParams = extractAndReplaceTemplateValues(params.proxySessionId);
504
+ if (proxySessionIdParams) {
505
+ params.proxySessionId = proxySessionIdParams.newParam;
506
+ extractedValues = { ...extractedValues, ...proxySessionIdParams.extractedValues };
507
+ }
508
+ if (params.responseRedactions) {
509
+ for (const r of params.responseRedactions) {
510
+ if (r.regex) {
511
+ const regexParams = extractAndReplaceTemplateValues(r.regex);
512
+ r.regex = regexParams?.newParam;
513
+ }
514
+ if (r.xPath) {
515
+ const xpathParams = extractAndReplaceTemplateValues(r.xPath);
516
+ r.xPath = xpathParams?.newParam;
517
+ }
518
+ if (r.jsonPath) {
519
+ const jsonPathParams = extractAndReplaceTemplateValues(r.jsonPath);
520
+ r.jsonPath = jsonPathParams?.newParam;
521
+ }
522
+ }
523
+ }
524
+ if (params.responseMatches) {
525
+ for (const r of params.responseMatches) {
526
+ if (r.value !== '') {
527
+ const matchParam = extractAndReplaceTemplateValues(r.value);
528
+ r.value = matchParam?.newParam;
529
+ extractedValues = { ...extractedValues, ...matchParam?.extractedValues };
530
+ }
531
+ }
532
+ }
533
+ return {
534
+ newParams: params,
535
+ extractedValues: extractedValues,
536
+ hiddenBodyParts: hiddenBodyParts,
537
+ hiddenURLParts: hiddenURLParts
538
+ };
539
+ function extractAndReplaceTemplateValues(param, ignoreMissingParams) {
540
+ if (!param) {
541
+ return null;
542
+ }
543
+ //const paramNames: Set<string> = new Set()
544
+ const extractedValues = {};
545
+ const hiddenParts = [];
546
+ let totalOffset = 0;
547
+ param = param.replace(paramsRegex, (match, pn, offset) => {
548
+ if (params.paramValues && pn in params.paramValues) {
549
+ extractedValues[pn] = params.paramValues[pn];
550
+ totalOffset += params.paramValues[pn].length - match.length;
551
+ return params.paramValues[pn];
552
+ }
553
+ else if (secretParams) {
554
+ if (secretParams?.paramValues && pn in secretParams?.paramValues) {
555
+ hiddenParts.push({
556
+ index: offset + totalOffset,
557
+ length: secretParams.paramValues[pn].length,
558
+ });
559
+ totalOffset += secretParams.paramValues[pn].length - match.length;
560
+ return secretParams.paramValues[pn];
561
+ }
562
+ else {
563
+ throw new Error(`parameter's "${pn}" value not found in paramValues and secret parameter's paramValues`);
564
+ }
565
+ }
566
+ else {
567
+ if (!(!!ignoreMissingParams)) {
568
+ throw new Error(`parameter's "${pn}" value not found in paramValues`);
569
+ }
570
+ else {
571
+ return match;
572
+ }
573
+ }
574
+ });
575
+ return {
576
+ newParam: param,
577
+ extractedValues: extractedValues,
578
+ hiddenParts: hiddenParts
579
+ };
580
+ }
581
+ }
582
+ function getGeoLocation(v2Params, secretParams) {
583
+ if (v2Params?.geoLocation) {
584
+ const paramNames = new Set();
585
+ let geo = v2Params.geoLocation;
586
+ //extract param names
587
+ let match = null;
588
+ while (match = paramsRegex.exec(geo)) {
589
+ paramNames.add(match[1]);
590
+ }
591
+ for (const pn of paramNames) {
592
+ if (v2Params.paramValues && pn in v2Params.paramValues) {
593
+ geo = geo?.replaceAll(`{{${pn}}}`, v2Params.paramValues[pn].toString());
594
+ }
595
+ else if (secretParams?.paramValues && pn in secretParams.paramValues) {
596
+ geo = geo?.replaceAll(`{{${pn}}}`, secretParams.paramValues[pn].toString());
597
+ }
598
+ else {
599
+ throw new Error(`parameter "${pn}" value not found in templateParams`);
600
+ }
601
+ }
602
+ const geoRegex = /^[A-Za-z]{2}$/sgiu;
603
+ if (!geoRegex.test(geo)) {
604
+ throw new Error(`Geolocation ${geo} is invalid`);
605
+ }
606
+ return geo;
607
+ }
608
+ return undefined;
609
+ }
610
+ function getProxySessionId(v2Params, secretParams) {
611
+ if (v2Params?.proxySessionId) {
612
+ const paramNames = new Set();
613
+ let proxySessionIdValue = v2Params.proxySessionId;
614
+ //extract param names
615
+ let match = null;
616
+ while (match = paramsRegex.exec(proxySessionIdValue)) {
617
+ paramNames.add(match[1]);
618
+ }
619
+ for (const pn of paramNames) {
620
+ if (v2Params.paramValues && pn in v2Params.paramValues) {
621
+ proxySessionIdValue = proxySessionIdValue?.replaceAll(`{{${pn}}}`, v2Params.paramValues[pn].toString());
622
+ }
623
+ else if (secretParams?.paramValues && pn in secretParams.paramValues) {
624
+ proxySessionIdValue = proxySessionIdValue?.replaceAll(`{{${pn}}}`, secretParams.paramValues[pn].toString());
625
+ }
626
+ else {
627
+ throw new Error(`parameter "${pn}" value not found in templateParams`);
628
+ }
629
+ }
630
+ if (!isValidProxySessionId(proxySessionIdValue)) {
631
+ throw new Error(`proxySessionId ${proxySessionIdValue} is invalid`);
632
+ }
633
+ return proxySessionIdValue;
634
+ }
635
+ return undefined;
636
+ }
637
+ function getURL(v2Params, secretParams) {
638
+ let hostPort = v2Params?.url;
639
+ const paramNames = new Set();
640
+ //extract param names
641
+ let match = null;
642
+ while (match = paramsRegex.exec(hostPort)) {
643
+ paramNames.add(match[1]);
644
+ }
645
+ for (const pn of paramNames) {
646
+ if (v2Params.paramValues && pn in v2Params.paramValues) {
647
+ hostPort = hostPort?.replaceAll(`{{${pn}}}`, v2Params.paramValues[pn].toString());
648
+ }
649
+ else if (secretParams?.paramValues && pn in secretParams.paramValues) {
650
+ hostPort = hostPort?.replaceAll(`{{${pn}}}`, secretParams.paramValues[pn].toString());
651
+ }
652
+ else {
653
+ throw new Error(`parameter "${pn}" value not found in templateParams`);
654
+ }
655
+ }
656
+ return hostPort;
657
+ }
658
+ export default HTTP_PROVIDER;
@@ -0,0 +1,6 @@
1
+ import { Node } from 'domhandler';
2
+ declare module 'xpath' {
3
+ function parse(expr: string): {
4
+ select(opts: unknown): Node[];
5
+ };
6
+ }