@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,10 +1,11 @@
1
- import { type JsonObject } from '#candid';
1
+ import type { JsonObject } from '#candid';
2
2
  import { Principal } from '#principal';
3
3
  import {
4
4
  HashTreeDecodeErrorCode,
5
5
  CreateHttpAgentErrorCode,
6
+ MissingFetchErrorCode,
7
+ ExcessiveSignaturesErrorCode,
6
8
  ExternalError,
7
- HttpDefaultFetchErrorCode,
8
9
  IdentityInvalidErrorCode,
9
10
  IngressExpiryInvalidErrorCode,
10
11
  InputError,
@@ -12,11 +13,12 @@ import {
12
13
  MalformedPublicKeyErrorCode,
13
14
  MalformedSignatureErrorCode,
14
15
  MissingRootKeyErrorCode,
15
- MissingSignatureErrorCode,
16
16
  ProtocolError,
17
17
  QuerySignatureVerificationFailedErrorCode,
18
+ RejectError,
18
19
  TimeoutWaitingForResponseErrorCode,
19
20
  TrustError,
21
+ UncertifiedRejectUpdateErrorCode,
20
22
  UnexpectedErrorCode,
21
23
  UnknownError,
22
24
  HttpErrorCode,
@@ -36,11 +38,18 @@ import {
36
38
  QueryResponseStatus,
37
39
  type Agent,
38
40
  type ApiQueryResponse,
41
+ type UpdateResult,
42
+ type HttpDetailsResponse,
39
43
  type QueryFields,
40
44
  type QueryResponse,
41
45
  type ReadStateOptions,
42
46
  type ReadStateResponse,
43
47
  type SubmitResponse,
48
+ type CallOptions,
49
+ type v2ResponseBody,
50
+ type v4ResponseBody,
51
+ isV4ResponseBody,
52
+ isV2ResponseBody,
44
53
  } from '../api.ts';
45
54
  import { Expiry, httpHeadersTransform, makeNonceTransform } from './transforms.ts';
46
55
  import {
@@ -66,10 +75,13 @@ import {
66
75
  getSubnetIdFromCertificate,
67
76
  type HashTree,
68
77
  lookup_path,
78
+ lookupResultToBuffer,
69
79
  LookupPathStatus,
70
80
  } from '../../certificate.ts';
81
+ import { readCertifiedReject } from '../../utils/certificateReject.ts';
71
82
  import { ed25519 } from '@noble/curves/ed25519';
72
- import { ExpirableMap } from '../../utils/expirableMap.ts';
83
+ import type { ExpirableStore } from '../../utils/expirableStore.ts';
84
+ import { createExpirableStore } from '../../utils/createExpirableStore.ts';
73
85
  import { Ed25519PublicKey } from '../../public_key.ts';
74
86
  import { ObservableLog } from '../../observable.ts';
75
87
  import {
@@ -78,16 +90,27 @@ import {
78
90
  ExponentialBackoff,
79
91
  } from '../../polling/backoff.ts';
80
92
  import { decodeTime } from '../../utils/leb.ts';
93
+ import { pollForResponse, type PollingOptions } from '../../polling/index.ts';
81
94
  import { concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
82
95
  import { uint8Equals, uint8FromBufLike } from '../../utils/buffer.ts';
83
96
  import { IC_RESPONSE_DOMAIN_SEPARATOR } from '../../constants.ts';
84
97
 
98
+ /**
99
+ * Possible values for the request status in the IC state tree.
100
+ * @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-request-status
101
+ */
85
102
  export enum RequestStatusResponseStatus {
103
+ /** The call has made it past the endpoint into the IC's state. */
86
104
  Received = 'received',
105
+ /** The initial effect of the call has happened or will happen. */
87
106
  Processing = 'processing',
107
+ /** The call completed successfully; the reply is available at `/request_status/<id>/reply`. */
88
108
  Replied = 'replied',
109
+ /** The call failed; reject code and message are available at `/request_status/<id>/reject_code` and `/request_status/<id>/reject_message`. */
89
110
  Rejected = 'rejected',
111
+ /** The request status path is absent from the state tree, the request is unknown to the IC (never received or pruned after expiry). */
90
112
  Unknown = 'unknown',
113
+ /** The IC has pruned the response data but remembers the request to prevent replay attacks. */
91
114
  Done = 'done',
92
115
  }
93
116
 
@@ -173,6 +196,12 @@ export interface HttpAgentOptions {
173
196
  * @default true
174
197
  */
175
198
  verifyQuerySignatures?: boolean;
199
+ /**
200
+ * Custom store for caching subnet node keys.
201
+ * Allows sharing the cache across multiple HttpAgent instances.
202
+ * Defaults to IndexedDB in browser environments, in-memory otherwise.
203
+ */
204
+ subnetNodeKeyExpirableStore?: ExpirableStore<SubnetNodeKeys>;
176
205
  /**
177
206
  * Whether to log to the console. Defaults to false.
178
207
  */
@@ -194,52 +223,11 @@ export interface HttpAgentOptions {
194
223
  shouldSyncTime?: boolean;
195
224
  }
196
225
 
197
- function getDefaultFetch(): typeof fetch {
198
- let defaultFetch;
199
-
200
- if (typeof window !== 'undefined') {
201
- // Browser context
202
- if (window.fetch) {
203
- defaultFetch = window.fetch.bind(window);
204
- } else {
205
- throw ExternalError.fromCode(
206
- new HttpDefaultFetchErrorCode(
207
- 'Fetch implementation was not available. You appear to be in a browser context, but window.fetch was not present.',
208
- ),
209
- );
210
- }
211
- } else if (typeof global !== 'undefined') {
212
- // Node context
213
- if (global.fetch) {
214
- defaultFetch = global.fetch.bind(global);
215
- } else {
216
- throw ExternalError.fromCode(
217
- new HttpDefaultFetchErrorCode(
218
- 'Fetch implementation was not available. You appear to be in a Node.js context, but global.fetch was not available.',
219
- ),
220
- );
221
- }
222
- } else if (typeof self !== 'undefined') {
223
- if (self.fetch) {
224
- defaultFetch = self.fetch.bind(self);
225
- }
226
- }
227
-
228
- if (defaultFetch) {
229
- return defaultFetch;
230
- }
231
- throw ExternalError.fromCode(
232
- new HttpDefaultFetchErrorCode(
233
- 'Fetch implementation was not available. Please provide fetch to the HttpAgent constructor, or ensure it is available in the window or global context.',
234
- ),
235
- );
236
- }
237
-
238
226
  function determineHost(configuredHost: string | undefined): string {
239
227
  let host: URL;
240
228
  if (configuredHost !== undefined) {
241
229
  if (!configuredHost.match(/^[a-z]+:/) && typeof window !== 'undefined') {
242
- host = new URL(window.location.protocol + '//' + configuredHost);
230
+ host = new URL(`${window.location.protocol}//${configuredHost}`);
243
231
  } else {
244
232
  host = new URL(configuredHost);
245
233
  }
@@ -261,7 +249,7 @@ function determineHost(configuredHost: string | undefined): string {
261
249
  if (location && knownHost) {
262
250
  // If the user is on a boundary-node provided host, we can use the same host for the agent
263
251
  host = new URL(
264
- `${location.protocol}//${knownHost}${location.port ? ':' + location.port : ''}`,
252
+ `${location.protocol}//${knownHost}${location.port ? `:${location.port}` : ''}`,
265
253
  );
266
254
  } else {
267
255
  host = new URL('https://icp-api.io');
@@ -311,6 +299,7 @@ export class HttpAgent implements Agent {
311
299
  readonly #retryTimes; // Retry requests N times before erroring by default
312
300
  #backoffStrategy: BackoffStrategyFactory;
313
301
  readonly #maxIngressExpiryInMinutes: number;
302
+ readonly #subnetNodeKeyExpirableStore: ExpirableStore<SubnetNodeKeys>;
314
303
  get #maxIngressExpiryInMs(): number {
315
304
  return this.#maxIngressExpiryInMinutes * MINUTE_TO_MSECS;
316
305
  }
@@ -324,9 +313,9 @@ export class HttpAgent implements Agent {
324
313
  #queryPipeline: HttpAgentRequestTransformFn[] = [];
325
314
  #updatePipeline: HttpAgentRequestTransformFn[] = [];
326
315
 
327
- #subnetKeys: ExpirableMap<string, SubnetNodeKeys> = new ExpirableMap({
328
- expirationTime: 5 * MINUTE_TO_MSECS,
329
- });
316
+ // Tracks in-flight fetchSubnetKeys promises to deduplicate parallel requests
317
+ // for the same canister, preventing redundant read_state round-trips.
318
+ #subnetKeysFetching: Map<string, Promise<SubnetNodeKeys>> = new Map();
330
319
  #verifyQuerySignatures = true;
331
320
 
332
321
  /**
@@ -335,7 +324,15 @@ export class HttpAgent implements Agent {
335
324
  */
336
325
  constructor(options: HttpAgentOptions = {}) {
337
326
  this.config = options;
338
- this.#fetch = options.fetch || getDefaultFetch() || fetch.bind(global);
327
+ if (options.fetch) {
328
+ this.#fetch = options.fetch;
329
+ } else {
330
+ const globalFetch = (globalThis as Record<string, unknown>).fetch;
331
+ if (typeof globalFetch !== 'function') {
332
+ throw InputError.fromCode(new MissingFetchErrorCode());
333
+ }
334
+ this.#fetch = (globalFetch as typeof fetch).bind(globalThis);
335
+ }
339
336
  this.#fetchOptions = options.fetchOptions;
340
337
  this.#callOptions = options.callOptions;
341
338
  this.#shouldFetchRootKey = options.shouldFetchRootKey ?? false;
@@ -352,10 +349,25 @@ export class HttpAgent implements Agent {
352
349
 
353
350
  const host = determineHost(options.host);
354
351
  this.host = new URL(host);
352
+ // Rewrite to avoid redirects and normalize the host before using it for namespacing caches
353
+ if (this.host.hostname.endsWith(IC0_SUB_DOMAIN)) {
354
+ this.host.hostname = IC0_DOMAIN;
355
+ } else if (this.host.hostname.endsWith(ICP0_SUB_DOMAIN)) {
356
+ this.host.hostname = ICP0_DOMAIN;
357
+ } else if (this.host.hostname.endsWith(ICP_API_SUB_DOMAIN)) {
358
+ this.host.hostname = ICP_API_DOMAIN;
359
+ }
355
360
 
356
361
  if (options.verifyQuerySignatures !== undefined) {
357
362
  this.#verifyQuerySignatures = options.verifyQuerySignatures;
358
363
  }
364
+ this.#subnetNodeKeyExpirableStore =
365
+ options.subnetNodeKeyExpirableStore ??
366
+ createExpirableStore<SubnetNodeKeys>({
367
+ dbName: `icp-sdk-${this.host.host}`,
368
+ storeName: 'subnetNodeKeys',
369
+ expirationTime: 5 * MINUTE_TO_MSECS,
370
+ });
359
371
  // Default is 3
360
372
  this.#retryTimes = options.retryTimes ?? 3;
361
373
  // Delay strategy for retries. Default is exponential backoff
@@ -364,18 +376,10 @@ export class HttpAgent implements Agent {
364
376
  maxIterations: this.#retryTimes,
365
377
  });
366
378
  this.#backoffStrategy = options.backoffStrategy || defaultBackoffFactory;
367
- // Rewrite to avoid redirects
368
- if (this.host.hostname.endsWith(IC0_SUB_DOMAIN)) {
369
- this.host.hostname = IC0_DOMAIN;
370
- } else if (this.host.hostname.endsWith(ICP0_SUB_DOMAIN)) {
371
- this.host.hostname = ICP0_DOMAIN;
372
- } else if (this.host.hostname.endsWith(ICP_API_SUB_DOMAIN)) {
373
- this.host.hostname = ICP_API_DOMAIN;
374
- }
375
379
 
376
380
  if (options.credentials) {
377
381
  const { name, password } = options.credentials;
378
- this.#credentials = `${name}${password ? ':' + password : ''}`;
382
+ this.#credentials = `${name}${password ? `:${password}` : ''}`;
379
383
  }
380
384
  this.#identity = Promise.resolve(options.identity || new AnonymousIdentity());
381
385
 
@@ -410,6 +414,7 @@ export class HttpAgent implements Agent {
410
414
  } else if (log.level === 'warn') {
411
415
  console.warn(log.message);
412
416
  } else {
417
+ // eslint-disable-next-line no-console -- logToConsole opt-in
413
418
  console.log(log.message);
414
419
  }
415
420
  });
@@ -537,7 +542,7 @@ export class HttpAgent implements Agent {
537
542
  method: 'POST',
538
543
  headers: {
539
544
  'Content-Type': 'application/cbor',
540
- ...(this.#credentials ? { Authorization: 'Basic ' + btoa(this.#credentials) } : {}),
545
+ ...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
541
546
  },
542
547
  },
543
548
  endpoint: Endpoint.Call,
@@ -575,7 +580,7 @@ export class HttpAgent implements Agent {
575
580
  // Apply transform for identity.
576
581
  transformedRequest = (await id.transformRequest(transformedRequest)) as HttpAgentSubmitRequest;
577
582
 
578
- const body = cbor.encode(transformedRequest.body);
583
+ const body = cbor.encode(transformedRequest.body) as Uint8Array<ArrayBuffer>;
579
584
  const backoff = this.#backoffStrategy();
580
585
  const requestId = requestIdOf(submit);
581
586
  try {
@@ -634,21 +639,21 @@ export class HttpAgent implements Agent {
634
639
  },
635
640
  identity,
636
641
  );
637
- } else if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
642
+ }
643
+ if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
638
644
  // if there is an ingress expiry error and the time has not been synced yet,
639
645
  // sync time with the network and try again
640
646
  await this.syncTime(canister);
641
647
  return this.call(canister, options, identity);
642
- } else {
643
- // override the error code to include the request details
644
- error.code.requestContext = {
645
- requestId,
646
- senderPubKey: transformedRequest.body.sender_pubkey,
647
- senderSignature: transformedRequest.body.sender_sig,
648
- ingressExpiry: transformedRequest.body.content.ingress_expiry,
649
- };
650
- callError = error;
651
648
  }
649
+ // override the error code to include the request details
650
+ error.code.requestContext = {
651
+ requestId,
652
+ senderPubKey: transformedRequest.body.sender_pubkey,
653
+ senderSignature: transformedRequest.body.sender_sig,
654
+ ingressExpiry: transformedRequest.body.content.ingress_expiry,
655
+ };
656
+ callError = error;
652
657
  } else {
653
658
  callError = UnknownError.fromCode(new UnexpectedErrorCode(error));
654
659
  }
@@ -657,10 +662,146 @@ export class HttpAgent implements Agent {
657
662
  }
658
663
  }
659
664
 
665
+ /**
666
+ * Executes an update call to a canister and returns the certified result.
667
+ * @param canisterId The canister to call.
668
+ * @param fields The call options (method name, arg, effective canister ID, optional nonce).
669
+ * @param pollingOptions Optional polling configuration.
670
+ * @returns The certified result including the certificate, reply bytes, and raw certificate bytes.
671
+ */
672
+ public async update(
673
+ canisterId: Principal | string,
674
+ fields: CallOptions,
675
+ pollingOptions: PollingOptions = {},
676
+ ): Promise<UpdateResult> {
677
+ const effectiveCanisterId = Principal.from(fields.effectiveCanisterId);
678
+ const { requestId, response, requestDetails } = await this.call(canisterId, fields);
679
+ const { body, ...httpDetails } = response;
680
+
681
+ if (isV4ResponseBody(body)) {
682
+ return this.#handleV4SyncResponse(
683
+ body,
684
+ response,
685
+ requestId,
686
+ effectiveCanisterId,
687
+ fields.methodName,
688
+ httpDetails,
689
+ requestDetails,
690
+ pollingOptions,
691
+ );
692
+ }
693
+
694
+ if (isV2ResponseBody(body)) {
695
+ this.#handleV2Rejection(body, requestId, effectiveCanisterId, fields.methodName, httpDetails);
696
+ }
697
+
698
+ if (response.status === HTTP_STATUS_ACCEPTED) {
699
+ const pollResult = await pollForResponse(
700
+ this,
701
+ effectiveCanisterId,
702
+ requestId,
703
+ pollingOptions,
704
+ );
705
+ return { ...pollResult, requestDetails, callResponse: response };
706
+ }
707
+
708
+ const unexpectedErrorCode = new UnexpectedErrorCode(
709
+ `Unexpected response from call: status ${response.status} with unrecognized body`,
710
+ );
711
+ unexpectedErrorCode.callContext = {
712
+ canisterId: effectiveCanisterId,
713
+ methodName: fields.methodName,
714
+ httpDetails,
715
+ };
716
+ throw UnknownError.fromCode(unexpectedErrorCode);
717
+ }
718
+
719
+ async #handleV4SyncResponse(
720
+ body: v4ResponseBody,
721
+ response: SubmitResponse['response'],
722
+ requestId: RequestId,
723
+ effectiveCanisterId: Principal,
724
+ methodName: string,
725
+ httpDetails: HttpDetailsResponse,
726
+ requestDetails: UpdateResult['requestDetails'],
727
+ pollingOptions: PollingOptions,
728
+ ): Promise<UpdateResult> {
729
+ if (this.rootKey == null) {
730
+ throw ExternalError.fromCode(new MissingRootKeyErrorCode());
731
+ }
732
+ const rawCertificate = body.certificate;
733
+ const certificate = await Certificate.create({
734
+ certificate: rawCertificate,
735
+ rootKey: this.rootKey,
736
+ principal: { canisterId: effectiveCanisterId },
737
+ blsVerify: pollingOptions.blsVerify,
738
+ agent: this,
739
+ });
740
+
741
+ const path = [utf8ToBytes('request_status'), requestId];
742
+ const maybeBuf = lookupResultToBuffer(
743
+ certificate.lookup_path([...path, utf8ToBytes('status')]),
744
+ );
745
+ const status = maybeBuf ? new TextDecoder().decode(maybeBuf) : undefined;
746
+
747
+ switch (status) {
748
+ case RequestStatusResponseStatus.Replied:
749
+ return {
750
+ reply: lookupResultToBuffer(certificate.lookup_path([...path, 'reply']))!,
751
+ certificate,
752
+ rawCertificate,
753
+ requestDetails,
754
+ callResponse: response,
755
+ };
756
+ case RequestStatusResponseStatus.Rejected: {
757
+ const error = readCertifiedReject(certificate, path, requestId);
758
+ error.callContext = {
759
+ canisterId: effectiveCanisterId,
760
+ methodName,
761
+ httpDetails,
762
+ };
763
+ throw RejectError.fromCode(error);
764
+ }
765
+ default: {
766
+ const unexpectedErrorCode = new UnexpectedErrorCode(
767
+ `Unexpected request status in v4 sync response: "${status}"`,
768
+ );
769
+ unexpectedErrorCode.callContext = {
770
+ canisterId: effectiveCanisterId,
771
+ methodName,
772
+ httpDetails,
773
+ };
774
+ throw UnknownError.fromCode(unexpectedErrorCode);
775
+ }
776
+ }
777
+ }
778
+
779
+ #handleV2Rejection(
780
+ body: v2ResponseBody,
781
+ requestId: RequestId,
782
+ effectiveCanisterId: Principal,
783
+ methodName: string,
784
+ httpDetails: HttpDetailsResponse,
785
+ ): never {
786
+ const { reject_code, reject_message, error_code } = body;
787
+ const errorCode = new UncertifiedRejectUpdateErrorCode(
788
+ requestId,
789
+ reject_code,
790
+ reject_message,
791
+ error_code,
792
+ );
793
+ errorCode.callContext = {
794
+ canisterId: effectiveCanisterId,
795
+ methodName,
796
+ httpDetails,
797
+ };
798
+ throw RejectError.fromCode(errorCode);
799
+ }
800
+
660
801
  async #requestAndRetryQuery(args: {
661
802
  ecid: Principal;
662
803
  transformedRequest: HttpAgentRequest;
663
- body: Uint8Array;
804
+ body: Uint8Array<ArrayBuffer>;
664
805
  requestId: RequestId;
665
806
  backoff: BackoffStrategy;
666
807
  tries: number;
@@ -820,7 +961,7 @@ export class HttpAgent implements Agent {
820
961
  }
821
962
 
822
963
  let response: Response;
823
- let responseBodyBytes = new Uint8Array();
964
+ let responseBodyBytes: Uint8Array = new Uint8Array();
824
965
  try {
825
966
  response = await requestFn();
826
967
  // According to the spec, only 200 responses have a non-empty body
@@ -917,7 +1058,7 @@ export class HttpAgent implements Agent {
917
1058
  method: 'POST',
918
1059
  headers: {
919
1060
  'Content-Type': 'application/cbor',
920
- ...(this.#credentials ? { Authorization: 'Basic ' + btoa(this.#credentials) } : {}),
1061
+ ...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
921
1062
  },
922
1063
  },
923
1064
  endpoint: Endpoint.Query,
@@ -927,7 +1068,7 @@ export class HttpAgent implements Agent {
927
1068
  // Apply transform for identity.
928
1069
  transformedRequest = (await id.transformRequest(transformedRequest)) as HttpAgentRequest;
929
1070
 
930
- const body = cbor.encode(transformedRequest.body);
1071
+ const body = cbor.encode(transformedRequest.body) as Uint8Array<ArrayBuffer>;
931
1072
 
932
1073
  const args = {
933
1074
  canister: canister.toText(),
@@ -948,19 +1089,6 @@ export class HttpAgent implements Agent {
948
1089
  };
949
1090
  };
950
1091
 
951
- const getSubnetNodeKeys = async (): Promise<SubnetNodeKeys> => {
952
- const cachedSubnetNodeKeys = this.#subnetKeys.get(ecid.toString());
953
- if (cachedSubnetNodeKeys) {
954
- return cachedSubnetNodeKeys;
955
- }
956
- await this.fetchSubnetKeys(ecid.toString());
957
- const subnetNodeKeys = this.#subnetKeys.get(ecid.toString());
958
- if (!subnetNodeKeys) {
959
- throw TrustError.fromCode(new MissingSignatureErrorCode());
960
- }
961
- return subnetNodeKeys;
962
- };
963
-
964
1092
  try {
965
1093
  if (!this.#verifyQuerySignatures) {
966
1094
  // Skip verification if the user has disabled it
@@ -970,7 +1098,7 @@ export class HttpAgent implements Agent {
970
1098
  // Make query and fetch subnet keys in parallel
971
1099
  const [queryWithDetails, subnetNodeKeys] = await Promise.all([
972
1100
  makeQuery(),
973
- getSubnetNodeKeys(),
1101
+ this.fetchSubnetKeys(ecid),
974
1102
  ]);
975
1103
 
976
1104
  try {
@@ -978,8 +1106,8 @@ export class HttpAgent implements Agent {
978
1106
  } catch {
979
1107
  // In case the node signatures have changed, refresh the subnet keys and try again
980
1108
  this.log.warn('Query response verification failed. Retrying with fresh subnet keys.');
981
- this.#subnetKeys.delete(ecid.toString());
982
- const updatedSubnetNodeKeys = await getSubnetNodeKeys();
1109
+ await this.#subnetNodeKeyExpirableStore.delete(ecid.toString());
1110
+ const updatedSubnetNodeKeys = await this.fetchSubnetKeys(ecid);
983
1111
  return this.#verifyQueryResponse(queryWithDetails, updatedSubnetNodeKeys);
984
1112
  }
985
1113
  } catch (error) {
@@ -1023,6 +1151,13 @@ export class HttpAgent implements Agent {
1023
1151
  }
1024
1152
  const { status, signatures = [], requestId } = queryResponse;
1025
1153
 
1154
+ const maxSignatures = subnetNodeKeys.size;
1155
+ if (signatures.length > maxSignatures) {
1156
+ throw ProtocolError.fromCode(
1157
+ new ExcessiveSignaturesErrorCode(signatures.length, maxSignatures),
1158
+ );
1159
+ }
1160
+
1026
1161
  for (const sig of signatures) {
1027
1162
  const { timestamp, identity } = sig;
1028
1163
  const nodeId = Principal.fromUint8Array(identity).toText();
@@ -1032,18 +1167,18 @@ export class HttpAgent implements Agent {
1032
1167
  if (status === QueryResponseStatus.Replied) {
1033
1168
  const { reply } = queryResponse;
1034
1169
  hash = hashOfMap({
1035
- status: status,
1036
- reply: reply,
1170
+ status,
1171
+ reply,
1037
1172
  timestamp: BigInt(timestamp),
1038
1173
  request_id: requestId,
1039
1174
  });
1040
1175
  } else if (status === QueryResponseStatus.Rejected) {
1041
1176
  const { reject_code, reject_message, error_code } = queryResponse;
1042
1177
  hash = hashOfMap({
1043
- status: status,
1044
- reject_code: reject_code,
1045
- reject_message: reject_message,
1046
- error_code: error_code,
1178
+ status,
1179
+ reject_code,
1180
+ reject_message,
1181
+ error_code,
1047
1182
  timestamp: BigInt(timestamp),
1048
1183
  request_id: requestId,
1049
1184
  });
@@ -1083,7 +1218,7 @@ export class HttpAgent implements Agent {
1083
1218
  method: 'POST',
1084
1219
  headers: {
1085
1220
  'Content-Type': 'application/cbor',
1086
- ...(this.#credentials ? { Authorization: 'Basic ' + btoa(this.#credentials) } : {}),
1221
+ ...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
1087
1222
  },
1088
1223
  },
1089
1224
  endpoint: Endpoint.ReadState,
@@ -1251,9 +1386,9 @@ export class HttpAgent implements Agent {
1251
1386
  this.log.print('Time from response:', date);
1252
1387
  this.log.print('Time from response in milliseconds:', date.getTime());
1253
1388
  return date.getTime();
1254
- } else {
1255
- this.log.warn('No certificate found in response');
1256
1389
  }
1390
+ this.log.warn('No certificate found in response');
1391
+
1257
1392
  return 0;
1258
1393
  }
1259
1394
 
@@ -1384,7 +1519,7 @@ export class HttpAgent implements Agent {
1384
1519
  public async status(): Promise<JsonObject> {
1385
1520
  const headers: Record<string, string> = this.#credentials
1386
1521
  ? {
1387
- Authorization: 'Basic ' + btoa(this.#credentials),
1522
+ Authorization: `Basic ${btoa(this.#credentials)}`,
1388
1523
  }
1389
1524
  : {};
1390
1525
 
@@ -1424,7 +1559,8 @@ export class HttpAgent implements Agent {
1424
1559
  async #rootKeyGuard(): Promise<void> {
1425
1560
  if (this.rootKey) {
1426
1561
  return;
1427
- } else if (
1562
+ }
1563
+ if (
1428
1564
  this.rootKey === null &&
1429
1565
  this.host.toString() !== 'https://icp-api.io' &&
1430
1566
  this.#shouldFetchRootKey
@@ -1451,6 +1587,28 @@ export class HttpAgent implements Agent {
1451
1587
 
1452
1588
  public async fetchSubnetKeys(canisterId: Principal | string): Promise<SubnetNodeKeys> {
1453
1589
  const effectiveCanisterId: Principal = Principal.from(canisterId);
1590
+
1591
+ // Return cached result if available within the TTL.
1592
+ const cached = await this.#subnetNodeKeyExpirableStore.get(effectiveCanisterId.toText());
1593
+ if (cached) {
1594
+ return cached;
1595
+ }
1596
+
1597
+ // Deduplicate parallel requests for the same canister so that concurrent
1598
+ // query calls share a single read_state round-trip instead of each
1599
+ // issuing their own.
1600
+ const inflight = this.#subnetKeysFetching.get(effectiveCanisterId.toText());
1601
+ if (inflight) {
1602
+ return inflight;
1603
+ }
1604
+ const fetchPromise = this.#doFetchSubnetKeys(effectiveCanisterId).finally(() => {
1605
+ this.#subnetKeysFetching.delete(effectiveCanisterId.toText());
1606
+ });
1607
+ this.#subnetKeysFetching.set(effectiveCanisterId.toText(), fetchPromise);
1608
+ return fetchPromise;
1609
+ }
1610
+
1611
+ async #doFetchSubnetKeys(effectiveCanisterId: Principal): Promise<SubnetNodeKeys> {
1454
1612
  await this.#asyncGuard(effectiveCanisterId);
1455
1613
 
1456
1614
  const rootKey = this.rootKey!;
@@ -1480,7 +1638,7 @@ export class HttpAgent implements Agent {
1480
1638
 
1481
1639
  const subnetId = getSubnetIdFromCertificate(canisterCertificate.cert, rootKey);
1482
1640
  const nodeKeys = lookupNodeKeysFromCertificate(canisterCertificate.cert, subnetId);
1483
- this.#subnetKeys.set(effectiveCanisterId.toText(), nodeKeys);
1641
+ await this.#subnetNodeKeyExpirableStore.set(effectiveCanisterId.toText(), nodeKeys);
1484
1642
 
1485
1643
  return nodeKeys;
1486
1644
  }