@icp-sdk/core 5.0.0 → 5.2.0-beta.0

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 (331) hide show
  1. package/lib/cjs/agent/actor.d.ts +15 -1
  2. package/lib/cjs/agent/actor.d.ts.map +1 -1
  3. package/lib/cjs/agent/actor.js +37 -91
  4. package/lib/cjs/agent/actor.js.map +1 -1
  5. package/lib/cjs/agent/agent/api.d.ts +29 -6
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/api.js.map +1 -1
  8. package/lib/cjs/agent/agent/http/expiry.d.ts +36 -0
  9. package/lib/cjs/agent/agent/http/expiry.d.ts.map +1 -0
  10. package/lib/cjs/agent/agent/http/expiry.js +92 -0
  11. package/lib/cjs/agent/agent/http/expiry.js.map +1 -0
  12. package/lib/cjs/agent/agent/http/index.d.ts +28 -2
  13. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  14. package/lib/cjs/agent/agent/http/index.js +182 -90
  15. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  16. package/lib/cjs/agent/agent/http/transforms.d.ts +1 -35
  17. package/lib/cjs/agent/agent/http/transforms.d.ts.map +1 -1
  18. package/lib/cjs/agent/agent/http/transforms.js +6 -90
  19. package/lib/cjs/agent/agent/http/transforms.js.map +1 -1
  20. package/lib/cjs/agent/agent/http/types.d.ts +1 -1
  21. package/lib/cjs/agent/agent/http/types.d.ts.map +1 -1
  22. package/lib/cjs/agent/auth.d.ts +1 -1
  23. package/lib/cjs/agent/auth.d.ts.map +1 -1
  24. package/lib/cjs/agent/canisterStatus/index.d.ts +5 -4
  25. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  26. package/lib/cjs/agent/canisterStatus/index.js +6 -10
  27. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  28. package/lib/cjs/agent/cbor.js +2 -2
  29. package/lib/cjs/agent/cbor.js.map +1 -1
  30. package/lib/cjs/agent/certificate.d.ts +4 -4
  31. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  32. package/lib/cjs/agent/certificate.js +6 -10
  33. package/lib/cjs/agent/certificate.js.map +1 -1
  34. package/lib/cjs/agent/constants.d.ts +4 -4
  35. package/lib/cjs/agent/constants.d.ts.map +1 -1
  36. package/lib/cjs/agent/der.d.ts.map +1 -1
  37. package/lib/cjs/agent/der.js +26 -21
  38. package/lib/cjs/agent/der.js.map +1 -1
  39. package/lib/cjs/agent/errors.d.ts +45 -19
  40. package/lib/cjs/agent/errors.d.ts.map +1 -1
  41. package/lib/cjs/agent/errors.js +58 -18
  42. package/lib/cjs/agent/errors.js.map +1 -1
  43. package/lib/cjs/agent/index.d.ts +1 -0
  44. package/lib/cjs/agent/index.d.ts.map +1 -1
  45. package/lib/cjs/agent/index.js.map +1 -1
  46. package/lib/cjs/agent/observable.d.ts +1 -1
  47. package/lib/cjs/agent/observable.d.ts.map +1 -1
  48. package/lib/cjs/agent/polling/backoff.d.ts +7 -7
  49. package/lib/cjs/agent/polling/backoff.d.ts.map +1 -1
  50. package/lib/cjs/agent/polling/backoff.js +3 -5
  51. package/lib/cjs/agent/polling/backoff.js.map +1 -1
  52. package/lib/cjs/agent/polling/index.d.ts +11 -10
  53. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  54. package/lib/cjs/agent/polling/index.js +8 -6
  55. package/lib/cjs/agent/polling/index.js.map +1 -1
  56. package/lib/cjs/agent/polling/strategy.d.ts +2 -5
  57. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  58. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  59. package/lib/cjs/agent/polling/types.d.ts +18 -0
  60. package/lib/cjs/agent/polling/types.d.ts.map +1 -0
  61. package/lib/cjs/agent/polling/types.js +3 -0
  62. package/lib/cjs/agent/polling/types.js.map +1 -0
  63. package/lib/cjs/agent/public_key.d.ts +1 -1
  64. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  65. package/lib/cjs/agent/request_id.d.ts.map +1 -1
  66. package/lib/cjs/agent/request_id.js +7 -7
  67. package/lib/cjs/agent/request_id.js.map +1 -1
  68. package/lib/cjs/agent/subnetStatus/index.d.ts +5 -4
  69. package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
  70. package/lib/cjs/agent/subnetStatus/index.js +6 -10
  71. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  72. package/lib/cjs/agent/utils/buffer.d.ts.map +1 -1
  73. package/lib/cjs/agent/utils/buffer.js +4 -2
  74. package/lib/cjs/agent/utils/buffer.js.map +1 -1
  75. package/lib/cjs/agent/utils/certificateReject.d.ts +11 -0
  76. package/lib/cjs/agent/utils/certificateReject.d.ts.map +1 -0
  77. package/lib/cjs/agent/utils/certificateReject.js +19 -0
  78. package/lib/cjs/agent/utils/certificateReject.js.map +1 -0
  79. package/lib/cjs/agent/utils/createExpirableStore.d.ts +23 -0
  80. package/lib/cjs/agent/utils/createExpirableStore.d.ts.map +1 -0
  81. package/lib/cjs/agent/utils/createExpirableStore.js +18 -0
  82. package/lib/cjs/agent/utils/createExpirableStore.js.map +1 -0
  83. package/lib/cjs/agent/utils/expirableStore.d.ts +39 -0
  84. package/lib/cjs/agent/utils/expirableStore.d.ts.map +1 -0
  85. package/lib/cjs/agent/utils/expirableStore.js +14 -0
  86. package/lib/cjs/agent/utils/expirableStore.js.map +1 -0
  87. package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts +14 -0
  88. package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
  89. package/lib/cjs/agent/utils/inMemoryExpirableStore.js +46 -0
  90. package/lib/cjs/agent/utils/inMemoryExpirableStore.js.map +1 -0
  91. package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts +28 -0
  92. package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
  93. package/lib/cjs/agent/utils/indexedDBExpirableStore.js +139 -0
  94. package/lib/cjs/agent/utils/indexedDBExpirableStore.js.map +1 -0
  95. package/lib/cjs/agent/utils/readState.d.ts +3 -3
  96. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  97. package/lib/cjs/candid/candid-core.d.ts +1 -1
  98. package/lib/cjs/candid/candid-core.d.ts.map +1 -1
  99. package/lib/cjs/candid/candid-core.js +7 -9
  100. package/lib/cjs/candid/candid-core.js.map +1 -1
  101. package/lib/cjs/candid/candid-ui.d.ts.map +1 -1
  102. package/lib/cjs/candid/candid-ui.js +8 -24
  103. package/lib/cjs/candid/candid-ui.js.map +1 -1
  104. package/lib/cjs/candid/idl.d.ts +14 -1
  105. package/lib/cjs/candid/idl.d.ts.map +1 -1
  106. package/lib/cjs/candid/idl.js +164 -125
  107. package/lib/cjs/candid/idl.js.map +1 -1
  108. package/lib/cjs/candid/utils/leb128.d.ts.map +1 -1
  109. package/lib/cjs/candid/utils/leb128.js +1 -3
  110. package/lib/cjs/candid/utils/leb128.js.map +1 -1
  111. package/lib/cjs/identity/identity/delegation.d.ts +4 -3
  112. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  113. package/lib/cjs/identity/identity/delegation.js +3 -3
  114. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  115. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  116. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  117. package/lib/cjs/identity/identity/ecdsa.js +2 -4
  118. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  119. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  120. package/lib/cjs/identity/identity/ed25519.js +9 -10
  121. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  122. package/lib/cjs/identity/identity/partial.d.ts +1 -1
  123. package/lib/cjs/identity/identity/partial.d.ts.map +1 -1
  124. package/lib/cjs/identity/identity/webauthn.js +4 -5
  125. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  126. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  127. package/lib/cjs/identity/secp256k1/der.js +27 -22
  128. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  129. package/lib/cjs/identity/secp256k1/pem.js +1 -1
  130. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  131. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  132. package/lib/cjs/identity/secp256k1/secp256k1.js +6 -6
  133. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  134. package/lib/cjs/principal/principal.d.ts +2 -2
  135. package/lib/cjs/principal/principal.d.ts.map +1 -1
  136. package/lib/cjs/principal/principal.js +10 -6
  137. package/lib/cjs/principal/principal.js.map +1 -1
  138. package/lib/esm/agent/actor.d.ts +15 -1
  139. package/lib/esm/agent/actor.d.ts.map +1 -1
  140. package/lib/esm/agent/actor.js +40 -94
  141. package/lib/esm/agent/actor.js.map +1 -1
  142. package/lib/esm/agent/agent/api.d.ts +29 -6
  143. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  144. package/lib/esm/agent/agent/api.js.map +1 -1
  145. package/lib/esm/agent/agent/http/expiry.d.ts +36 -0
  146. package/lib/esm/agent/agent/http/expiry.d.ts.map +1 -0
  147. package/lib/esm/agent/agent/http/expiry.js +88 -0
  148. package/lib/esm/agent/agent/http/expiry.js.map +1 -0
  149. package/lib/esm/agent/agent/http/index.d.ts +28 -2
  150. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  151. package/lib/esm/agent/agent/http/index.js +185 -93
  152. package/lib/esm/agent/agent/http/index.js.map +1 -1
  153. package/lib/esm/agent/agent/http/transforms.d.ts +1 -35
  154. package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -1
  155. package/lib/esm/agent/agent/http/transforms.js +2 -87
  156. package/lib/esm/agent/agent/http/transforms.js.map +1 -1
  157. package/lib/esm/agent/agent/http/types.d.ts +1 -1
  158. package/lib/esm/agent/agent/http/types.d.ts.map +1 -1
  159. package/lib/esm/agent/auth.d.ts +1 -1
  160. package/lib/esm/agent/auth.d.ts.map +1 -1
  161. package/lib/esm/agent/canister-env/index.d.ts +2 -2
  162. package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
  163. package/lib/esm/agent/canisterStatus/index.d.ts +5 -4
  164. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  165. package/lib/esm/agent/canisterStatus/index.js +6 -10
  166. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  167. package/lib/esm/agent/cbor.js +1 -1
  168. package/lib/esm/agent/cbor.js.map +1 -1
  169. package/lib/esm/agent/certificate.d.ts +4 -4
  170. package/lib/esm/agent/certificate.d.ts.map +1 -1
  171. package/lib/esm/agent/certificate.js +6 -10
  172. package/lib/esm/agent/certificate.js.map +1 -1
  173. package/lib/esm/agent/constants.d.ts +4 -4
  174. package/lib/esm/agent/constants.d.ts.map +1 -1
  175. package/lib/esm/agent/der.d.ts.map +1 -1
  176. package/lib/esm/agent/der.js +26 -21
  177. package/lib/esm/agent/der.js.map +1 -1
  178. package/lib/esm/agent/errors.d.ts +45 -19
  179. package/lib/esm/agent/errors.d.ts.map +1 -1
  180. package/lib/esm/agent/errors.js +54 -16
  181. package/lib/esm/agent/errors.js.map +1 -1
  182. package/lib/esm/agent/index.d.ts +1 -0
  183. package/lib/esm/agent/index.d.ts.map +1 -1
  184. package/lib/esm/agent/index.js.map +1 -1
  185. package/lib/esm/agent/observable.d.ts +1 -1
  186. package/lib/esm/agent/observable.d.ts.map +1 -1
  187. package/lib/esm/agent/polling/backoff.d.ts +7 -7
  188. package/lib/esm/agent/polling/backoff.d.ts.map +1 -1
  189. package/lib/esm/agent/polling/backoff.js +3 -5
  190. package/lib/esm/agent/polling/backoff.js.map +1 -1
  191. package/lib/esm/agent/polling/index.d.ts +11 -10
  192. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  193. package/lib/esm/agent/polling/index.js +9 -7
  194. package/lib/esm/agent/polling/index.js.map +1 -1
  195. package/lib/esm/agent/polling/strategy.d.ts +2 -5
  196. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  197. package/lib/esm/agent/polling/strategy.js.map +1 -1
  198. package/lib/esm/agent/polling/types.d.ts +18 -0
  199. package/lib/esm/agent/polling/types.d.ts.map +1 -0
  200. package/lib/esm/agent/polling/types.js +2 -0
  201. package/lib/esm/agent/polling/types.js.map +1 -0
  202. package/lib/esm/agent/public_key.d.ts +1 -1
  203. package/lib/esm/agent/public_key.d.ts.map +1 -1
  204. package/lib/esm/agent/request_id.d.ts.map +1 -1
  205. package/lib/esm/agent/request_id.js +7 -7
  206. package/lib/esm/agent/request_id.js.map +1 -1
  207. package/lib/esm/agent/subnetStatus/index.d.ts +5 -4
  208. package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
  209. package/lib/esm/agent/subnetStatus/index.js +6 -10
  210. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  211. package/lib/esm/agent/utils/buffer.d.ts.map +1 -1
  212. package/lib/esm/agent/utils/buffer.js +4 -2
  213. package/lib/esm/agent/utils/buffer.js.map +1 -1
  214. package/lib/esm/agent/utils/certificateReject.d.ts +11 -0
  215. package/lib/esm/agent/utils/certificateReject.d.ts.map +1 -0
  216. package/lib/esm/agent/utils/certificateReject.js +16 -0
  217. package/lib/esm/agent/utils/certificateReject.js.map +1 -0
  218. package/lib/esm/agent/utils/createExpirableStore.d.ts +23 -0
  219. package/lib/esm/agent/utils/createExpirableStore.d.ts.map +1 -0
  220. package/lib/esm/agent/utils/createExpirableStore.js +15 -0
  221. package/lib/esm/agent/utils/createExpirableStore.js.map +1 -0
  222. package/lib/esm/agent/utils/expirableStore.d.ts +39 -0
  223. package/lib/esm/agent/utils/expirableStore.d.ts.map +1 -0
  224. package/lib/esm/agent/utils/expirableStore.js +11 -0
  225. package/lib/esm/agent/utils/expirableStore.js.map +1 -0
  226. package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts +14 -0
  227. package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
  228. package/lib/esm/agent/utils/inMemoryExpirableStore.js +42 -0
  229. package/lib/esm/agent/utils/inMemoryExpirableStore.js.map +1 -0
  230. package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts +28 -0
  231. package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
  232. package/lib/esm/agent/utils/indexedDBExpirableStore.js +135 -0
  233. package/lib/esm/agent/utils/indexedDBExpirableStore.js.map +1 -0
  234. package/lib/esm/agent/utils/readState.d.ts +3 -3
  235. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  236. package/lib/esm/candid/candid-core.d.ts +1 -1
  237. package/lib/esm/candid/candid-core.d.ts.map +1 -1
  238. package/lib/esm/candid/candid-core.js +7 -9
  239. package/lib/esm/candid/candid-core.js.map +1 -1
  240. package/lib/esm/candid/candid-ui.d.ts.map +1 -1
  241. package/lib/esm/candid/candid-ui.js +7 -23
  242. package/lib/esm/candid/candid-ui.js.map +1 -1
  243. package/lib/esm/candid/idl.d.ts +14 -1
  244. package/lib/esm/candid/idl.d.ts.map +1 -1
  245. package/lib/esm/candid/idl.js +162 -124
  246. package/lib/esm/candid/idl.js.map +1 -1
  247. package/lib/esm/candid/utils/leb128.d.ts.map +1 -1
  248. package/lib/esm/candid/utils/leb128.js +1 -3
  249. package/lib/esm/candid/utils/leb128.js.map +1 -1
  250. package/lib/esm/identity/identity/delegation.d.ts +4 -3
  251. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  252. package/lib/esm/identity/identity/delegation.js +3 -3
  253. package/lib/esm/identity/identity/delegation.js.map +1 -1
  254. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  255. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  256. package/lib/esm/identity/identity/ecdsa.js +2 -4
  257. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  258. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  259. package/lib/esm/identity/identity/ed25519.js +9 -10
  260. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  261. package/lib/esm/identity/identity/partial.d.ts +1 -1
  262. package/lib/esm/identity/identity/partial.d.ts.map +1 -1
  263. package/lib/esm/identity/identity/webauthn.js +4 -5
  264. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  265. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  266. package/lib/esm/identity/secp256k1/der.js +27 -22
  267. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  268. package/lib/esm/identity/secp256k1/pem.js +1 -1
  269. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  270. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  271. package/lib/esm/identity/secp256k1/secp256k1.js +6 -6
  272. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  273. package/lib/esm/principal/principal.d.ts +2 -2
  274. package/lib/esm/principal/principal.d.ts.map +1 -1
  275. package/lib/esm/principal/principal.js +10 -6
  276. package/lib/esm/principal/principal.js.map +1 -1
  277. package/package.json +14 -13
  278. package/src/agent/actor.ts +74 -126
  279. package/src/agent/agent/api.ts +36 -6
  280. package/src/agent/agent/http/expiry.ts +107 -0
  281. package/src/agent/agent/http/index.ts +262 -104
  282. package/src/agent/agent/http/transforms.ts +2 -106
  283. package/src/agent/agent/http/types.ts +1 -1
  284. package/src/agent/auth.ts +1 -1
  285. package/src/agent/canister-env/index.ts +2 -2
  286. package/src/agent/canisterStatus/index.ts +10 -12
  287. package/src/agent/cbor.ts +1 -1
  288. package/src/agent/certificate.ts +11 -13
  289. package/src/agent/der.ts +40 -19
  290. package/src/agent/errors.ts +85 -34
  291. package/src/agent/fetch_candid.ts +1 -1
  292. package/src/agent/index.ts +1 -0
  293. package/src/agent/observable.ts +1 -1
  294. package/src/agent/polling/backoff.ts +9 -10
  295. package/src/agent/polling/index.ts +14 -26
  296. package/src/agent/polling/strategy.ts +5 -9
  297. package/src/agent/polling/types.ts +28 -0
  298. package/src/agent/public_key.ts +1 -1
  299. package/src/agent/request_id.ts +15 -8
  300. package/src/agent/subnetStatus/index.ts +10 -12
  301. package/src/agent/utils/buffer.ts +6 -2
  302. package/src/agent/utils/certificateReject.ts +25 -0
  303. package/src/agent/utils/createExpirableStore.ts +31 -0
  304. package/src/agent/utils/expirableStore.ts +47 -0
  305. package/src/agent/utils/inMemoryExpirableStore.ts +49 -0
  306. package/src/agent/utils/indexedDBExpirableStore.ts +191 -0
  307. package/src/agent/utils/random.ts +2 -2
  308. package/src/agent/utils/readState.ts +3 -3
  309. package/src/candid/candid-core.ts +9 -11
  310. package/src/candid/candid-ui.ts +8 -16
  311. package/src/candid/idl.ts +210 -112
  312. package/src/candid/utils/leb128.ts +1 -2
  313. package/src/identity/identity/delegation.ts +5 -5
  314. package/src/identity/identity/ecdsa.ts +12 -8
  315. package/src/identity/identity/ed25519.ts +16 -9
  316. package/src/identity/identity/partial.ts +1 -1
  317. package/src/identity/identity/webauthn.ts +21 -21
  318. package/src/identity/secp256k1/der.ts +41 -20
  319. package/src/identity/secp256k1/pem.ts +1 -1
  320. package/src/identity/secp256k1/secp256k1.ts +12 -6
  321. package/src/principal/principal.ts +18 -8
  322. package/LICENSE +0 -201
  323. package/lib/cjs/agent/utils/expirableMap.d.ts +0 -80
  324. package/lib/cjs/agent/utils/expirableMap.d.ts.map +0 -1
  325. package/lib/cjs/agent/utils/expirableMap.js +0 -148
  326. package/lib/cjs/agent/utils/expirableMap.js.map +0 -1
  327. package/lib/esm/agent/utils/expirableMap.d.ts +0 -80
  328. package/lib/esm/agent/utils/expirableMap.d.ts.map +0 -1
  329. package/lib/esm/agent/utils/expirableMap.js +0 -144
  330. package/lib/esm/agent/utils/expirableMap.js.map +0 -1
  331. package/src/agent/utils/expirableMap.ts +0 -163
@@ -1,31 +1,43 @@
1
1
  import { Principal } from '../../../principal/index.js';
2
- import { HashTreeDecodeErrorCode, CreateHttpAgentErrorCode, ExternalError, HttpDefaultFetchErrorCode, IdentityInvalidErrorCode, IngressExpiryInvalidErrorCode, InputError, LookupErrorCode, MalformedPublicKeyErrorCode, MalformedSignatureErrorCode, MissingRootKeyErrorCode, MissingSignatureErrorCode, ProtocolError, QuerySignatureVerificationFailedErrorCode, TimeoutWaitingForResponseErrorCode, TrustError, UnexpectedErrorCode, UnknownError, HttpErrorCode, HttpV4ApiNotSupportedErrorCode, TransportError, HttpFetchErrorCode, AgentError, MalformedLookupFoundValueErrorCode, CertificateOutdatedErrorCode, CertificateNotAuthorizedErrorCode, UNREACHABLE_ERROR, } from "../../errors.js";
2
+ import { HashTreeDecodeErrorCode, CreateHttpAgentErrorCode, MissingFetchErrorCode, ExcessiveSignaturesErrorCode, ExternalError, IdentityInvalidErrorCode, IngressExpiryInvalidErrorCode, InputError, LookupErrorCode, MalformedPublicKeyErrorCode, MalformedSignatureErrorCode, MissingRootKeyErrorCode, ProtocolError, QuerySignatureVerificationFailedErrorCode, RejectError, TimeoutWaitingForResponseErrorCode, TrustError, UncertifiedRejectUpdateErrorCode, UnexpectedErrorCode, UnknownError, HttpErrorCode, HttpV4ApiNotSupportedErrorCode, TransportError, HttpFetchErrorCode, AgentError, MalformedLookupFoundValueErrorCode, CertificateOutdatedErrorCode, CertificateNotAuthorizedErrorCode, UNREACHABLE_ERROR, } from "../../errors.js";
3
3
  import { AnonymousIdentity } from "../../auth.js";
4
4
  import * as cbor from "../../cbor.js";
5
5
  import { hashOfMap, requestIdOf } from "../../request_id.js";
6
- import { QueryResponseStatus, } from "../api.js";
6
+ import { QueryResponseStatus, isV4ResponseBody, isV2ResponseBody, } from "../api.js";
7
7
  import { Expiry, httpHeadersTransform, makeNonceTransform } from "./transforms.js";
8
8
  import { Endpoint, makeNonce, ReadRequestType, SubmitRequestType, } from "./types.js";
9
9
  import { request as canisterStatusRequest } from "../../canisterStatus/index.js";
10
10
  import { request as subnetStatusRequest } from "../../subnetStatus/index.js";
11
11
  import { lookupNodeKeysFromCertificate } from "../../utils/readState.js";
12
- import { Certificate, check_canister_ranges, getSubnetIdFromCertificate, lookup_path, LookupPathStatus, } from "../../certificate.js";
12
+ import { Certificate, check_canister_ranges, getSubnetIdFromCertificate, lookup_path, lookupResultToBuffer, LookupPathStatus, } from "../../certificate.js";
13
+ import { readCertifiedReject } from "../../utils/certificateReject.js";
13
14
  import { ed25519 } from '@noble/curves/ed25519';
14
- import { ExpirableMap } from "../../utils/expirableMap.js";
15
+ import { createExpirableStore } from "../../utils/createExpirableStore.js";
15
16
  import { Ed25519PublicKey } from "../../public_key.js";
16
17
  import { ObservableLog } from "../../observable.js";
17
18
  import { ExponentialBackoff, } from "../../polling/backoff.js";
18
19
  import { decodeTime } from "../../utils/leb.js";
20
+ import { pollForResponse } from "../../polling/index.js";
19
21
  import { concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
20
22
  import { uint8Equals, uint8FromBufLike } from "../../utils/buffer.js";
21
23
  import { IC_RESPONSE_DOMAIN_SEPARATOR } from "../../constants.js";
24
+ /**
25
+ * Possible values for the request status in the IC state tree.
26
+ * @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-request-status
27
+ */
22
28
  export var RequestStatusResponseStatus;
23
29
  (function (RequestStatusResponseStatus) {
30
+ /** The call has made it past the endpoint into the IC's state. */
24
31
  RequestStatusResponseStatus["Received"] = "received";
32
+ /** The initial effect of the call has happened or will happen. */
25
33
  RequestStatusResponseStatus["Processing"] = "processing";
34
+ /** The call completed successfully; the reply is available at `/request_status/<id>/reply`. */
26
35
  RequestStatusResponseStatus["Replied"] = "replied";
36
+ /** The call failed; reject code and message are available at `/request_status/<id>/reject_code` and `/request_status/<id>/reject_message`. */
27
37
  RequestStatusResponseStatus["Rejected"] = "rejected";
38
+ /** The request status path is absent from the state tree, the request is unknown to the IC (never received or pruned after expiry). */
28
39
  RequestStatusResponseStatus["Unknown"] = "unknown";
40
+ /** The IC has pruned the response data but remembers the request to prevent replay attacks. */
29
41
  RequestStatusResponseStatus["Done"] = "done";
30
42
  })(RequestStatusResponseStatus || (RequestStatusResponseStatus = {}));
31
43
  const MINUTE_TO_MSECS = 60 * 1_000;
@@ -47,41 +59,11 @@ const ICP_API_SUB_DOMAIN = '.icp-api.io';
47
59
  const HTTP_STATUS_OK = 200;
48
60
  const HTTP_STATUS_ACCEPTED = 202;
49
61
  const HTTP_STATUS_NOT_FOUND = 404;
50
- function getDefaultFetch() {
51
- let defaultFetch;
52
- if (typeof window !== 'undefined') {
53
- // Browser context
54
- if (window.fetch) {
55
- defaultFetch = window.fetch.bind(window);
56
- }
57
- else {
58
- throw ExternalError.fromCode(new HttpDefaultFetchErrorCode('Fetch implementation was not available. You appear to be in a browser context, but window.fetch was not present.'));
59
- }
60
- }
61
- else if (typeof global !== 'undefined') {
62
- // Node context
63
- if (global.fetch) {
64
- defaultFetch = global.fetch.bind(global);
65
- }
66
- else {
67
- throw ExternalError.fromCode(new HttpDefaultFetchErrorCode('Fetch implementation was not available. You appear to be in a Node.js context, but global.fetch was not available.'));
68
- }
69
- }
70
- else if (typeof self !== 'undefined') {
71
- if (self.fetch) {
72
- defaultFetch = self.fetch.bind(self);
73
- }
74
- }
75
- if (defaultFetch) {
76
- return defaultFetch;
77
- }
78
- throw ExternalError.fromCode(new HttpDefaultFetchErrorCode('Fetch implementation was not available. Please provide fetch to the HttpAgent constructor, or ensure it is available in the window or global context.'));
79
- }
80
62
  function determineHost(configuredHost) {
81
63
  let host;
82
64
  if (configuredHost !== undefined) {
83
65
  if (!configuredHost.match(/^[a-z]+:/) && typeof window !== 'undefined') {
84
- host = new URL(window.location.protocol + '//' + configuredHost);
66
+ host = new URL(`${window.location.protocol}//${configuredHost}`);
85
67
  }
86
68
  else {
87
69
  host = new URL(configuredHost);
@@ -104,7 +86,7 @@ function determineHost(configuredHost) {
104
86
  }
105
87
  if (location && knownHost) {
106
88
  // If the user is on a boundary-node provided host, we can use the same host for the agent
107
- host = new URL(`${location.protocol}//${knownHost}${location.port ? ':' + location.port : ''}`);
89
+ host = new URL(`${location.protocol}//${knownHost}${location.port ? `:${location.port}` : ''}`);
108
90
  }
109
91
  else {
110
92
  host = new URL('https://icp-api.io');
@@ -137,12 +119,15 @@ export class HttpAgent {
137
119
  #retryTimes; // Retry requests N times before erroring by default
138
120
  #backoffStrategy;
139
121
  #maxIngressExpiryInMinutes;
122
+ #subnetNodeKeyExpirableStore;
140
123
  get #maxIngressExpiryInMs() {
141
124
  return this.#maxIngressExpiryInMinutes * MINUTE_TO_MSECS;
142
125
  }
143
126
  #queryPipeline;
144
127
  #updatePipeline;
145
- #subnetKeys;
128
+ // Tracks in-flight fetchSubnetKeys promises to deduplicate parallel requests
129
+ // for the same canister, preventing redundant read_state round-trips.
130
+ #subnetKeysFetching;
146
131
  #verifyQuerySignatures;
147
132
  /**
148
133
  * @param options - Options for the HttpAgent
@@ -161,9 +146,9 @@ export class HttpAgent {
161
146
  this.log = new ObservableLog();
162
147
  this.#queryPipeline = [];
163
148
  this.#updatePipeline = [];
164
- this.#subnetKeys = new ExpirableMap({
165
- expirationTime: 5 * MINUTE_TO_MSECS,
166
- });
149
+ // Tracks in-flight fetchSubnetKeys promises to deduplicate parallel requests
150
+ // for the same canister, preventing redundant read_state round-trips.
151
+ this.#subnetKeysFetching = new Map();
167
152
  this.#verifyQuerySignatures = true;
168
153
  /**
169
154
  * See https://internetcomputer.org/docs/current/references/ic-interface-spec/#http-query for details on validation
@@ -177,6 +162,10 @@ export class HttpAgent {
177
162
  return queryResponse;
178
163
  }
179
164
  const { status, signatures = [], requestId } = queryResponse;
165
+ const maxSignatures = subnetNodeKeys.size;
166
+ if (signatures.length > maxSignatures) {
167
+ throw ProtocolError.fromCode(new ExcessiveSignaturesErrorCode(signatures.length, maxSignatures));
168
+ }
180
169
  for (const sig of signatures) {
181
170
  const { timestamp, identity } = sig;
182
171
  const nodeId = Principal.fromUint8Array(identity).toText();
@@ -185,8 +174,8 @@ export class HttpAgent {
185
174
  if (status === QueryResponseStatus.Replied) {
186
175
  const { reply } = queryResponse;
187
176
  hash = hashOfMap({
188
- status: status,
189
- reply: reply,
177
+ status,
178
+ reply,
190
179
  timestamp: BigInt(timestamp),
191
180
  request_id: requestId,
192
181
  });
@@ -194,10 +183,10 @@ export class HttpAgent {
194
183
  else if (status === QueryResponseStatus.Rejected) {
195
184
  const { reject_code, reject_message, error_code } = queryResponse;
196
185
  hash = hashOfMap({
197
- status: status,
198
- reject_code: reject_code,
199
- reject_message: reject_message,
200
- error_code: error_code,
186
+ status,
187
+ reject_code,
188
+ reject_message,
189
+ error_code,
201
190
  timestamp: BigInt(timestamp),
202
191
  request_id: requestId,
203
192
  });
@@ -219,7 +208,16 @@ export class HttpAgent {
219
208
  return queryResponse;
220
209
  };
221
210
  this.config = options;
222
- this.#fetch = options.fetch || getDefaultFetch() || fetch.bind(global);
211
+ if (options.fetch) {
212
+ this.#fetch = options.fetch;
213
+ }
214
+ else {
215
+ const globalFetch = globalThis.fetch;
216
+ if (typeof globalFetch !== 'function') {
217
+ throw InputError.fromCode(new MissingFetchErrorCode());
218
+ }
219
+ this.#fetch = globalFetch.bind(globalThis);
220
+ }
223
221
  this.#fetchOptions = options.fetchOptions;
224
222
  this.#callOptions = options.callOptions;
225
223
  this.#shouldFetchRootKey = options.shouldFetchRootKey ?? false;
@@ -236,9 +234,26 @@ export class HttpAgent {
236
234
  }
237
235
  const host = determineHost(options.host);
238
236
  this.host = new URL(host);
237
+ // Rewrite to avoid redirects and normalize the host before using it for namespacing caches
238
+ if (this.host.hostname.endsWith(IC0_SUB_DOMAIN)) {
239
+ this.host.hostname = IC0_DOMAIN;
240
+ }
241
+ else if (this.host.hostname.endsWith(ICP0_SUB_DOMAIN)) {
242
+ this.host.hostname = ICP0_DOMAIN;
243
+ }
244
+ else if (this.host.hostname.endsWith(ICP_API_SUB_DOMAIN)) {
245
+ this.host.hostname = ICP_API_DOMAIN;
246
+ }
239
247
  if (options.verifyQuerySignatures !== undefined) {
240
248
  this.#verifyQuerySignatures = options.verifyQuerySignatures;
241
249
  }
250
+ this.#subnetNodeKeyExpirableStore =
251
+ options.subnetNodeKeyExpirableStore ??
252
+ createExpirableStore({
253
+ dbName: `icp-sdk-${this.host.host}`,
254
+ storeName: 'subnetNodeKeys',
255
+ expirationTime: 5 * MINUTE_TO_MSECS,
256
+ });
242
257
  // Default is 3
243
258
  this.#retryTimes = options.retryTimes ?? 3;
244
259
  // Delay strategy for retries. Default is exponential backoff
@@ -246,19 +261,9 @@ export class HttpAgent {
246
261
  maxIterations: this.#retryTimes,
247
262
  });
248
263
  this.#backoffStrategy = options.backoffStrategy || defaultBackoffFactory;
249
- // Rewrite to avoid redirects
250
- if (this.host.hostname.endsWith(IC0_SUB_DOMAIN)) {
251
- this.host.hostname = IC0_DOMAIN;
252
- }
253
- else if (this.host.hostname.endsWith(ICP0_SUB_DOMAIN)) {
254
- this.host.hostname = ICP0_DOMAIN;
255
- }
256
- else if (this.host.hostname.endsWith(ICP_API_SUB_DOMAIN)) {
257
- this.host.hostname = ICP_API_DOMAIN;
258
- }
259
264
  if (options.credentials) {
260
265
  const { name, password } = options.credentials;
261
- this.#credentials = `${name}${password ? ':' + password : ''}`;
266
+ this.#credentials = `${name}${password ? `:${password}` : ''}`;
262
267
  }
263
268
  this.#identity = Promise.resolve(options.identity || new AnonymousIdentity());
264
269
  if (options.ingressExpiryInMinutes && options.ingressExpiryInMinutes > 5) {
@@ -282,6 +287,7 @@ export class HttpAgent {
282
287
  console.warn(log.message);
283
288
  }
284
289
  else {
290
+ // eslint-disable-next-line no-console -- logToConsole opt-in
285
291
  console.log(log.message);
286
292
  }
287
293
  });
@@ -373,7 +379,7 @@ export class HttpAgent {
373
379
  method: 'POST',
374
380
  headers: {
375
381
  'Content-Type': 'application/cbor',
376
- ...(this.#credentials ? { Authorization: 'Basic ' + btoa(this.#credentials) } : {}),
382
+ ...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
377
383
  },
378
384
  },
379
385
  endpoint: Endpoint.Call,
@@ -456,22 +462,20 @@ export class HttpAgent {
456
462
  callSync: false,
457
463
  }, identity);
458
464
  }
459
- else if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
465
+ if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
460
466
  // if there is an ingress expiry error and the time has not been synced yet,
461
467
  // sync time with the network and try again
462
468
  await this.syncTime(canister);
463
469
  return this.call(canister, options, identity);
464
470
  }
465
- else {
466
- // override the error code to include the request details
467
- error.code.requestContext = {
468
- requestId,
469
- senderPubKey: transformedRequest.body.sender_pubkey,
470
- senderSignature: transformedRequest.body.sender_sig,
471
- ingressExpiry: transformedRequest.body.content.ingress_expiry,
472
- };
473
- callError = error;
474
- }
471
+ // override the error code to include the request details
472
+ error.code.requestContext = {
473
+ requestId,
474
+ senderPubKey: transformedRequest.body.sender_pubkey,
475
+ senderSignature: transformedRequest.body.sender_sig,
476
+ ingressExpiry: transformedRequest.body.content.ingress_expiry,
477
+ };
478
+ callError = error;
475
479
  }
476
480
  else {
477
481
  callError = UnknownError.fromCode(new UnexpectedErrorCode(error));
@@ -480,6 +484,89 @@ export class HttpAgent {
480
484
  throw callError;
481
485
  }
482
486
  }
487
+ /**
488
+ * Executes an update call to a canister and returns the certified result.
489
+ * @param canisterId The canister to call.
490
+ * @param fields The call options (method name, arg, effective canister ID, optional nonce).
491
+ * @param pollingOptions Optional polling configuration.
492
+ * @returns The certified result including the certificate, reply bytes, and raw certificate bytes.
493
+ */
494
+ async update(canisterId, fields, pollingOptions = {}) {
495
+ const effectiveCanisterId = Principal.from(fields.effectiveCanisterId);
496
+ const { requestId, response, requestDetails } = await this.call(canisterId, fields);
497
+ const { body, ...httpDetails } = response;
498
+ if (isV4ResponseBody(body)) {
499
+ return this.#handleV4SyncResponse(body, response, requestId, effectiveCanisterId, fields.methodName, httpDetails, requestDetails, pollingOptions);
500
+ }
501
+ if (isV2ResponseBody(body)) {
502
+ this.#handleV2Rejection(body, requestId, effectiveCanisterId, fields.methodName, httpDetails);
503
+ }
504
+ if (response.status === HTTP_STATUS_ACCEPTED) {
505
+ const pollResult = await pollForResponse(this, effectiveCanisterId, requestId, pollingOptions);
506
+ return { ...pollResult, requestDetails, callResponse: response };
507
+ }
508
+ const unexpectedErrorCode = new UnexpectedErrorCode(`Unexpected response from call: status ${response.status} with unrecognized body`);
509
+ unexpectedErrorCode.callContext = {
510
+ canisterId: effectiveCanisterId,
511
+ methodName: fields.methodName,
512
+ httpDetails,
513
+ };
514
+ throw UnknownError.fromCode(unexpectedErrorCode);
515
+ }
516
+ async #handleV4SyncResponse(body, response, requestId, effectiveCanisterId, methodName, httpDetails, requestDetails, pollingOptions) {
517
+ if (this.rootKey == null) {
518
+ throw ExternalError.fromCode(new MissingRootKeyErrorCode());
519
+ }
520
+ const rawCertificate = body.certificate;
521
+ const certificate = await Certificate.create({
522
+ certificate: rawCertificate,
523
+ rootKey: this.rootKey,
524
+ principal: { canisterId: effectiveCanisterId },
525
+ blsVerify: pollingOptions.blsVerify,
526
+ agent: this,
527
+ });
528
+ const path = [utf8ToBytes('request_status'), requestId];
529
+ const maybeBuf = lookupResultToBuffer(certificate.lookup_path([...path, utf8ToBytes('status')]));
530
+ const status = maybeBuf ? new TextDecoder().decode(maybeBuf) : undefined;
531
+ switch (status) {
532
+ case RequestStatusResponseStatus.Replied:
533
+ return {
534
+ reply: lookupResultToBuffer(certificate.lookup_path([...path, 'reply'])),
535
+ certificate,
536
+ rawCertificate,
537
+ requestDetails,
538
+ callResponse: response,
539
+ };
540
+ case RequestStatusResponseStatus.Rejected: {
541
+ const error = readCertifiedReject(certificate, path, requestId);
542
+ error.callContext = {
543
+ canisterId: effectiveCanisterId,
544
+ methodName,
545
+ httpDetails,
546
+ };
547
+ throw RejectError.fromCode(error);
548
+ }
549
+ default: {
550
+ const unexpectedErrorCode = new UnexpectedErrorCode(`Unexpected request status in v4 sync response: "${status}"`);
551
+ unexpectedErrorCode.callContext = {
552
+ canisterId: effectiveCanisterId,
553
+ methodName,
554
+ httpDetails,
555
+ };
556
+ throw UnknownError.fromCode(unexpectedErrorCode);
557
+ }
558
+ }
559
+ }
560
+ #handleV2Rejection(body, requestId, effectiveCanisterId, methodName, httpDetails) {
561
+ const { reject_code, reject_message, error_code } = body;
562
+ const errorCode = new UncertifiedRejectUpdateErrorCode(requestId, reject_code, reject_message, error_code);
563
+ errorCode.callContext = {
564
+ canisterId: effectiveCanisterId,
565
+ methodName,
566
+ httpDetails,
567
+ };
568
+ throw RejectError.fromCode(errorCode);
569
+ }
483
570
  async #requestAndRetryQuery(args) {
484
571
  const { ecid, transformedRequest, body, requestId, backoff, tries } = args;
485
572
  const delay = tries === 0 ? 0 : backoff.next();
@@ -654,7 +741,7 @@ export class HttpAgent {
654
741
  method: 'POST',
655
742
  headers: {
656
743
  'Content-Type': 'application/cbor',
657
- ...(this.#credentials ? { Authorization: 'Basic ' + btoa(this.#credentials) } : {}),
744
+ ...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
658
745
  },
659
746
  },
660
747
  endpoint: Endpoint.Query,
@@ -680,18 +767,6 @@ export class HttpAgent {
680
767
  ...query,
681
768
  };
682
769
  };
683
- const getSubnetNodeKeys = async () => {
684
- const cachedSubnetNodeKeys = this.#subnetKeys.get(ecid.toString());
685
- if (cachedSubnetNodeKeys) {
686
- return cachedSubnetNodeKeys;
687
- }
688
- await this.fetchSubnetKeys(ecid.toString());
689
- const subnetNodeKeys = this.#subnetKeys.get(ecid.toString());
690
- if (!subnetNodeKeys) {
691
- throw TrustError.fromCode(new MissingSignatureErrorCode());
692
- }
693
- return subnetNodeKeys;
694
- };
695
770
  try {
696
771
  if (!this.#verifyQuerySignatures) {
697
772
  // Skip verification if the user has disabled it
@@ -700,7 +775,7 @@ export class HttpAgent {
700
775
  // Make query and fetch subnet keys in parallel
701
776
  const [queryWithDetails, subnetNodeKeys] = await Promise.all([
702
777
  makeQuery(),
703
- getSubnetNodeKeys(),
778
+ this.fetchSubnetKeys(ecid),
704
779
  ]);
705
780
  try {
706
781
  return this.#verifyQueryResponse(queryWithDetails, subnetNodeKeys);
@@ -708,8 +783,8 @@ export class HttpAgent {
708
783
  catch {
709
784
  // In case the node signatures have changed, refresh the subnet keys and try again
710
785
  this.log.warn('Query response verification failed. Retrying with fresh subnet keys.');
711
- this.#subnetKeys.delete(ecid.toString());
712
- const updatedSubnetNodeKeys = await getSubnetNodeKeys();
786
+ await this.#subnetNodeKeyExpirableStore.delete(ecid.toString());
787
+ const updatedSubnetNodeKeys = await this.fetchSubnetKeys(ecid);
713
788
  return this.#verifyQueryResponse(queryWithDetails, updatedSubnetNodeKeys);
714
789
  }
715
790
  }
@@ -757,7 +832,7 @@ export class HttpAgent {
757
832
  method: 'POST',
758
833
  headers: {
759
834
  'Content-Type': 'application/cbor',
760
- ...(this.#credentials ? { Authorization: 'Basic ' + btoa(this.#credentials) } : {}),
835
+ ...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
761
836
  },
762
837
  },
763
838
  endpoint: Endpoint.ReadState,
@@ -888,9 +963,7 @@ export class HttpAgent {
888
963
  this.log.print('Time from response in milliseconds:', date.getTime());
889
964
  return date.getTime();
890
965
  }
891
- else {
892
- this.log.warn('No certificate found in response');
893
- }
966
+ this.log.warn('No certificate found in response');
894
967
  return 0;
895
968
  }
896
969
  /**
@@ -999,7 +1072,7 @@ export class HttpAgent {
999
1072
  async status() {
1000
1073
  const headers = this.#credentials
1001
1074
  ? {
1002
- Authorization: 'Basic ' + btoa(this.#credentials),
1075
+ Authorization: `Basic ${btoa(this.#credentials)}`,
1003
1076
  }
1004
1077
  : {};
1005
1078
  const url = new URL(`/api/v2/status`, this.host);
@@ -1034,7 +1107,7 @@ export class HttpAgent {
1034
1107
  if (this.rootKey) {
1035
1108
  return;
1036
1109
  }
1037
- else if (this.rootKey === null &&
1110
+ if (this.rootKey === null &&
1038
1111
  this.host.toString() !== 'https://icp-api.io' &&
1039
1112
  this.#shouldFetchRootKey) {
1040
1113
  await this.fetchRootKey();
@@ -1056,6 +1129,25 @@ export class HttpAgent {
1056
1129
  }
1057
1130
  async fetchSubnetKeys(canisterId) {
1058
1131
  const effectiveCanisterId = Principal.from(canisterId);
1132
+ // Return cached result if available within the TTL.
1133
+ const cached = await this.#subnetNodeKeyExpirableStore.get(effectiveCanisterId.toText());
1134
+ if (cached) {
1135
+ return cached;
1136
+ }
1137
+ // Deduplicate parallel requests for the same canister so that concurrent
1138
+ // query calls share a single read_state round-trip instead of each
1139
+ // issuing their own.
1140
+ const inflight = this.#subnetKeysFetching.get(effectiveCanisterId.toText());
1141
+ if (inflight) {
1142
+ return inflight;
1143
+ }
1144
+ const fetchPromise = this.#doFetchSubnetKeys(effectiveCanisterId).finally(() => {
1145
+ this.#subnetKeysFetching.delete(effectiveCanisterId.toText());
1146
+ });
1147
+ this.#subnetKeysFetching.set(effectiveCanisterId.toText(), fetchPromise);
1148
+ return fetchPromise;
1149
+ }
1150
+ async #doFetchSubnetKeys(effectiveCanisterId) {
1059
1151
  await this.#asyncGuard(effectiveCanisterId);
1060
1152
  const rootKey = this.rootKey;
1061
1153
  const canisterReadState = await this.readState(effectiveCanisterId, {
@@ -1080,7 +1172,7 @@ export class HttpAgent {
1080
1172
  }
1081
1173
  const subnetId = getSubnetIdFromCertificate(canisterCertificate.cert, rootKey);
1082
1174
  const nodeKeys = lookupNodeKeysFromCertificate(canisterCertificate.cert, subnetId);
1083
- this.#subnetKeys.set(effectiveCanisterId.toText(), nodeKeys);
1175
+ await this.#subnetNodeKeyExpirableStore.set(effectiveCanisterId.toText(), nodeKeys);
1084
1176
  return nodeKeys;
1085
1177
  }
1086
1178
  /**