@icp-sdk/core 5.0.0-beta.2 → 5.1.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 (286) 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 +11 -2
  13. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  14. package/lib/cjs/agent/agent/http/index.js +142 -73
  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 +27 -18
  40. package/lib/cjs/agent/errors.d.ts.map +1 -1
  41. package/lib/cjs/agent/errors.js +18 -15
  42. package/lib/cjs/agent/errors.js.map +1 -1
  43. package/lib/cjs/agent/observable.d.ts +1 -1
  44. package/lib/cjs/agent/observable.d.ts.map +1 -1
  45. package/lib/cjs/agent/polling/backoff.d.ts +7 -7
  46. package/lib/cjs/agent/polling/backoff.d.ts.map +1 -1
  47. package/lib/cjs/agent/polling/backoff.js +3 -5
  48. package/lib/cjs/agent/polling/backoff.js.map +1 -1
  49. package/lib/cjs/agent/polling/index.d.ts +11 -10
  50. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  51. package/lib/cjs/agent/polling/index.js +8 -6
  52. package/lib/cjs/agent/polling/index.js.map +1 -1
  53. package/lib/cjs/agent/polling/strategy.d.ts +2 -5
  54. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  55. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  56. package/lib/cjs/agent/polling/types.d.ts +18 -0
  57. package/lib/cjs/agent/polling/types.d.ts.map +1 -0
  58. package/lib/cjs/agent/polling/types.js +3 -0
  59. package/lib/cjs/agent/polling/types.js.map +1 -0
  60. package/lib/cjs/agent/public_key.d.ts +1 -1
  61. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  62. package/lib/cjs/agent/request_id.d.ts.map +1 -1
  63. package/lib/cjs/agent/request_id.js +7 -7
  64. package/lib/cjs/agent/request_id.js.map +1 -1
  65. package/lib/cjs/agent/subnetStatus/index.d.ts +5 -4
  66. package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
  67. package/lib/cjs/agent/subnetStatus/index.js +6 -10
  68. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  69. package/lib/cjs/agent/utils/buffer.d.ts.map +1 -1
  70. package/lib/cjs/agent/utils/buffer.js +4 -2
  71. package/lib/cjs/agent/utils/buffer.js.map +1 -1
  72. package/lib/cjs/agent/utils/certificateReject.d.ts +11 -0
  73. package/lib/cjs/agent/utils/certificateReject.d.ts.map +1 -0
  74. package/lib/cjs/agent/utils/certificateReject.js +19 -0
  75. package/lib/cjs/agent/utils/certificateReject.js.map +1 -0
  76. package/lib/cjs/agent/utils/expirableMap.d.ts +4 -4
  77. package/lib/cjs/agent/utils/expirableMap.d.ts.map +1 -1
  78. package/lib/cjs/agent/utils/expirableMap.js +2 -2
  79. package/lib/cjs/agent/utils/readState.d.ts +3 -3
  80. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  81. package/lib/cjs/candid/candid-core.d.ts +1 -1
  82. package/lib/cjs/candid/candid-core.d.ts.map +1 -1
  83. package/lib/cjs/candid/candid-core.js +7 -9
  84. package/lib/cjs/candid/candid-core.js.map +1 -1
  85. package/lib/cjs/candid/candid-ui.d.ts.map +1 -1
  86. package/lib/cjs/candid/candid-ui.js +8 -24
  87. package/lib/cjs/candid/candid-ui.js.map +1 -1
  88. package/lib/cjs/candid/idl.d.ts +14 -1
  89. package/lib/cjs/candid/idl.d.ts.map +1 -1
  90. package/lib/cjs/candid/idl.js +164 -125
  91. package/lib/cjs/candid/idl.js.map +1 -1
  92. package/lib/cjs/candid/utils/leb128.d.ts.map +1 -1
  93. package/lib/cjs/candid/utils/leb128.js +1 -3
  94. package/lib/cjs/candid/utils/leb128.js.map +1 -1
  95. package/lib/cjs/identity/identity/delegation.d.ts +4 -3
  96. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  97. package/lib/cjs/identity/identity/delegation.js +3 -3
  98. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  99. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  100. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  101. package/lib/cjs/identity/identity/ecdsa.js +2 -4
  102. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  103. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  104. package/lib/cjs/identity/identity/ed25519.js +9 -10
  105. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  106. package/lib/cjs/identity/identity/partial.d.ts +1 -1
  107. package/lib/cjs/identity/identity/partial.d.ts.map +1 -1
  108. package/lib/cjs/identity/identity/webauthn.js +4 -5
  109. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  110. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  111. package/lib/cjs/identity/secp256k1/der.js +27 -22
  112. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  113. package/lib/cjs/identity/secp256k1/pem.js +1 -1
  114. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  115. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  116. package/lib/cjs/identity/secp256k1/secp256k1.js +6 -6
  117. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  118. package/lib/cjs/principal/principal.d.ts +2 -2
  119. package/lib/cjs/principal/principal.d.ts.map +1 -1
  120. package/lib/cjs/principal/principal.js +10 -6
  121. package/lib/cjs/principal/principal.js.map +1 -1
  122. package/lib/esm/agent/actor.d.ts +15 -1
  123. package/lib/esm/agent/actor.d.ts.map +1 -1
  124. package/lib/esm/agent/actor.js +40 -94
  125. package/lib/esm/agent/actor.js.map +1 -1
  126. package/lib/esm/agent/agent/api.d.ts +29 -6
  127. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  128. package/lib/esm/agent/agent/api.js.map +1 -1
  129. package/lib/esm/agent/agent/http/expiry.d.ts +36 -0
  130. package/lib/esm/agent/agent/http/expiry.d.ts.map +1 -0
  131. package/lib/esm/agent/agent/http/expiry.js +88 -0
  132. package/lib/esm/agent/agent/http/expiry.js.map +1 -0
  133. package/lib/esm/agent/agent/http/index.d.ts +11 -2
  134. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  135. package/lib/esm/agent/agent/http/index.js +145 -76
  136. package/lib/esm/agent/agent/http/index.js.map +1 -1
  137. package/lib/esm/agent/agent/http/transforms.d.ts +1 -35
  138. package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -1
  139. package/lib/esm/agent/agent/http/transforms.js +2 -87
  140. package/lib/esm/agent/agent/http/transforms.js.map +1 -1
  141. package/lib/esm/agent/agent/http/types.d.ts +1 -1
  142. package/lib/esm/agent/agent/http/types.d.ts.map +1 -1
  143. package/lib/esm/agent/auth.d.ts +1 -1
  144. package/lib/esm/agent/auth.d.ts.map +1 -1
  145. package/lib/esm/agent/canister-env/index.d.ts +2 -2
  146. package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
  147. package/lib/esm/agent/canisterStatus/index.d.ts +5 -4
  148. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  149. package/lib/esm/agent/canisterStatus/index.js +6 -10
  150. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  151. package/lib/esm/agent/cbor.js +1 -1
  152. package/lib/esm/agent/cbor.js.map +1 -1
  153. package/lib/esm/agent/certificate.d.ts +4 -4
  154. package/lib/esm/agent/certificate.d.ts.map +1 -1
  155. package/lib/esm/agent/certificate.js +6 -10
  156. package/lib/esm/agent/certificate.js.map +1 -1
  157. package/lib/esm/agent/constants.d.ts +4 -4
  158. package/lib/esm/agent/constants.d.ts.map +1 -1
  159. package/lib/esm/agent/der.d.ts.map +1 -1
  160. package/lib/esm/agent/der.js +26 -21
  161. package/lib/esm/agent/der.js.map +1 -1
  162. package/lib/esm/agent/errors.d.ts +27 -18
  163. package/lib/esm/agent/errors.d.ts.map +1 -1
  164. package/lib/esm/agent/errors.js +16 -13
  165. package/lib/esm/agent/errors.js.map +1 -1
  166. package/lib/esm/agent/observable.d.ts +1 -1
  167. package/lib/esm/agent/observable.d.ts.map +1 -1
  168. package/lib/esm/agent/polling/backoff.d.ts +7 -7
  169. package/lib/esm/agent/polling/backoff.d.ts.map +1 -1
  170. package/lib/esm/agent/polling/backoff.js +3 -5
  171. package/lib/esm/agent/polling/backoff.js.map +1 -1
  172. package/lib/esm/agent/polling/index.d.ts +11 -10
  173. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  174. package/lib/esm/agent/polling/index.js +9 -7
  175. package/lib/esm/agent/polling/index.js.map +1 -1
  176. package/lib/esm/agent/polling/strategy.d.ts +2 -5
  177. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  178. package/lib/esm/agent/polling/strategy.js.map +1 -1
  179. package/lib/esm/agent/polling/types.d.ts +18 -0
  180. package/lib/esm/agent/polling/types.d.ts.map +1 -0
  181. package/lib/esm/agent/polling/types.js +2 -0
  182. package/lib/esm/agent/polling/types.js.map +1 -0
  183. package/lib/esm/agent/public_key.d.ts +1 -1
  184. package/lib/esm/agent/public_key.d.ts.map +1 -1
  185. package/lib/esm/agent/request_id.d.ts.map +1 -1
  186. package/lib/esm/agent/request_id.js +7 -7
  187. package/lib/esm/agent/request_id.js.map +1 -1
  188. package/lib/esm/agent/subnetStatus/index.d.ts +5 -4
  189. package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
  190. package/lib/esm/agent/subnetStatus/index.js +6 -10
  191. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  192. package/lib/esm/agent/utils/buffer.d.ts.map +1 -1
  193. package/lib/esm/agent/utils/buffer.js +4 -2
  194. package/lib/esm/agent/utils/buffer.js.map +1 -1
  195. package/lib/esm/agent/utils/certificateReject.d.ts +11 -0
  196. package/lib/esm/agent/utils/certificateReject.d.ts.map +1 -0
  197. package/lib/esm/agent/utils/certificateReject.js +16 -0
  198. package/lib/esm/agent/utils/certificateReject.js.map +1 -0
  199. package/lib/esm/agent/utils/expirableMap.d.ts +4 -4
  200. package/lib/esm/agent/utils/expirableMap.d.ts.map +1 -1
  201. package/lib/esm/agent/utils/expirableMap.js +2 -2
  202. package/lib/esm/agent/utils/readState.d.ts +3 -3
  203. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  204. package/lib/esm/candid/candid-core.d.ts +1 -1
  205. package/lib/esm/candid/candid-core.d.ts.map +1 -1
  206. package/lib/esm/candid/candid-core.js +7 -9
  207. package/lib/esm/candid/candid-core.js.map +1 -1
  208. package/lib/esm/candid/candid-ui.d.ts.map +1 -1
  209. package/lib/esm/candid/candid-ui.js +7 -23
  210. package/lib/esm/candid/candid-ui.js.map +1 -1
  211. package/lib/esm/candid/idl.d.ts +14 -1
  212. package/lib/esm/candid/idl.d.ts.map +1 -1
  213. package/lib/esm/candid/idl.js +162 -124
  214. package/lib/esm/candid/idl.js.map +1 -1
  215. package/lib/esm/candid/utils/leb128.d.ts.map +1 -1
  216. package/lib/esm/candid/utils/leb128.js +1 -3
  217. package/lib/esm/candid/utils/leb128.js.map +1 -1
  218. package/lib/esm/identity/identity/delegation.d.ts +4 -3
  219. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  220. package/lib/esm/identity/identity/delegation.js +3 -3
  221. package/lib/esm/identity/identity/delegation.js.map +1 -1
  222. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  223. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  224. package/lib/esm/identity/identity/ecdsa.js +2 -4
  225. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  226. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  227. package/lib/esm/identity/identity/ed25519.js +9 -10
  228. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  229. package/lib/esm/identity/identity/partial.d.ts +1 -1
  230. package/lib/esm/identity/identity/partial.d.ts.map +1 -1
  231. package/lib/esm/identity/identity/webauthn.js +4 -5
  232. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  233. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  234. package/lib/esm/identity/secp256k1/der.js +27 -22
  235. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  236. package/lib/esm/identity/secp256k1/pem.js +1 -1
  237. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  238. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  239. package/lib/esm/identity/secp256k1/secp256k1.js +6 -6
  240. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  241. package/lib/esm/principal/principal.d.ts +2 -2
  242. package/lib/esm/principal/principal.d.ts.map +1 -1
  243. package/lib/esm/principal/principal.js +10 -6
  244. package/lib/esm/principal/principal.js.map +1 -1
  245. package/package.json +13 -13
  246. package/src/agent/actor.ts +74 -126
  247. package/src/agent/agent/api.ts +36 -6
  248. package/src/agent/agent/http/expiry.ts +107 -0
  249. package/src/agent/agent/http/index.ts +217 -90
  250. package/src/agent/agent/http/transforms.ts +2 -106
  251. package/src/agent/agent/http/types.ts +1 -1
  252. package/src/agent/auth.ts +1 -1
  253. package/src/agent/canister-env/index.ts +2 -2
  254. package/src/agent/canisterStatus/index.ts +10 -12
  255. package/src/agent/cbor.ts +1 -1
  256. package/src/agent/certificate.ts +11 -13
  257. package/src/agent/der.ts +40 -19
  258. package/src/agent/errors.ts +41 -31
  259. package/src/agent/fetch_candid.ts +1 -1
  260. package/src/agent/observable.ts +1 -1
  261. package/src/agent/polling/backoff.ts +9 -10
  262. package/src/agent/polling/index.ts +14 -26
  263. package/src/agent/polling/strategy.ts +5 -9
  264. package/src/agent/polling/types.ts +28 -0
  265. package/src/agent/public_key.ts +1 -1
  266. package/src/agent/request_id.ts +15 -8
  267. package/src/agent/subnetStatus/index.ts +10 -12
  268. package/src/agent/utils/buffer.ts +6 -2
  269. package/src/agent/utils/certificateReject.ts +25 -0
  270. package/src/agent/utils/expirableMap.ts +4 -4
  271. package/src/agent/utils/random.ts +2 -2
  272. package/src/agent/utils/readState.ts +3 -3
  273. package/src/candid/candid-core.ts +9 -11
  274. package/src/candid/candid-ui.ts +8 -16
  275. package/src/candid/idl.ts +210 -112
  276. package/src/candid/utils/leb128.ts +1 -2
  277. package/src/identity/identity/delegation.ts +5 -5
  278. package/src/identity/identity/ecdsa.ts +12 -8
  279. package/src/identity/identity/ed25519.ts +16 -9
  280. package/src/identity/identity/partial.ts +1 -1
  281. package/src/identity/identity/webauthn.ts +21 -21
  282. package/src/identity/secp256k1/der.ts +41 -20
  283. package/src/identity/secp256k1/pem.ts +1 -1
  284. package/src/identity/secp256k1/secp256k1.ts +12 -6
  285. package/src/principal/principal.ts +18 -8
  286. package/LICENSE +0 -201
@@ -9,15 +9,15 @@ import {
9
9
  InputError,
10
10
  CertificateTimeErrorCode,
11
11
  } from '../errors.ts';
12
- import { HttpAgent } from '../agent/http/index.ts';
12
+ import type { HttpAgent } from '../agent/http/index.ts';
13
13
  import { type Cert, Certificate, lookupResultToBuffer } from '../certificate.ts';
14
14
  import * as cbor from '../cbor.ts';
15
15
  import { decodeTime } from '../utils/leb.ts';
16
16
  import { utf8ToBytes, bytesToHex } from '@noble/hashes/utils';
17
+ import type { CustomPath } from '../utils/readState.ts';
17
18
  import {
18
19
  type BaseSubnetStatus,
19
20
  type BaseStatus,
20
- CustomPath,
21
21
  decodeValue,
22
22
  decodeControllers,
23
23
  encodeMetadataPath,
@@ -39,7 +39,7 @@ export type Path = 'time' | 'controllers' | 'subnet' | 'module_hash' | 'candid'
39
39
 
40
40
  export type StatusMap = Map<Path | string, Status>;
41
41
 
42
- export type CanisterStatusOptions = {
42
+ export interface CanisterStatusOptions {
43
43
  /**
44
44
  * The effective canister ID to use in the underlying {@link HttpAgent.readState} call.
45
45
  */
@@ -58,7 +58,7 @@ export type CanisterStatusOptions = {
58
58
  * @default false
59
59
  */
60
60
  disableCertificateTimeVerification?: boolean;
61
- };
61
+ }
62
62
 
63
63
  /**
64
64
  * Requests information from a canister's `read_state` endpoint.
@@ -115,12 +115,11 @@ export const request = async (options: CanisterStatusOptions): Promise<StatusMap
115
115
  path,
116
116
  data,
117
117
  };
118
- } else {
119
- return {
120
- path,
121
- data: lookupResultToBuffer(cert.lookup_path(encodedPath)),
122
- };
123
118
  }
119
+ return {
120
+ path,
121
+ data: lookupResultToBuffer(cert.lookup_path(encodedPath)),
122
+ };
124
123
  };
125
124
 
126
125
  // must pass in the rootKey if we have no delegation
@@ -247,10 +246,9 @@ export const encodePath = (path: Path, canisterId: Principal): Uint8Array[] => {
247
246
  // For simplified metadata queries
248
247
  if (typeof path['path'] === 'string' || path['path'] instanceof Uint8Array) {
249
248
  return encodeMetadataPath(path.path, canisterUint8Array);
250
- } else {
251
- // For non-metadata, return the provided custom path
252
- return path['path'];
253
249
  }
250
+ // For non-metadata, return the provided custom path
251
+ return path['path'];
254
252
  }
255
253
  }
256
254
  }
package/src/agent/cbor.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Principal } from '#principal';
2
2
  import * as cbor from '@dfinity/cbor';
3
3
  import { CborDecodeErrorCode, CborEncodeErrorCode, InputError } from './errors.ts';
4
- import { Expiry } from './agent/index.ts';
4
+ import { Expiry } from './agent/http/expiry.ts';
5
5
 
6
6
  /**
7
7
  * Used to extend classes that need to provide a custom value for the CBOR encoding process.
@@ -78,7 +78,7 @@ export function hashTreeToString(tree: HashTree): string {
78
78
  const indent = (s: string) =>
79
79
  s
80
80
  .split('\n')
81
- .map(x => ' ' + x)
81
+ .map(x => ` ${x}`)
82
82
  .join('\n');
83
83
  function labelToString(label: Uint8Array): string {
84
84
  const decoder = new TextDecoder(undefined, { fatal: true });
@@ -94,24 +94,22 @@ export function hashTreeToString(tree: HashTree): string {
94
94
  case NodeType.Empty:
95
95
  return '()';
96
96
  case NodeType.Fork: {
97
- if (tree[1] instanceof Array && tree[2] instanceof Uint8Array) {
97
+ if (tree[1] instanceof Array && tree[2] instanceof Array) {
98
98
  const left = hashTreeToString(tree[1]);
99
99
  const right = hashTreeToString(tree[2]);
100
100
  return `sub(\n left:\n${indent(left)}\n---\n right:\n${indent(right)}\n)`;
101
- } else {
102
- throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid tree structure for fork'));
103
101
  }
102
+ throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid tree structure for fork'));
104
103
  }
105
104
  case NodeType.Labeled: {
106
- if (tree[1] instanceof Uint8Array && tree[2] instanceof Uint8Array) {
105
+ if (tree[1] instanceof Uint8Array && tree[2] instanceof Array) {
107
106
  const label = labelToString(tree[1]);
108
107
  const sub = hashTreeToString(tree[2]);
109
108
  return `label(\n label:\n${indent(label)}\n sub:\n${indent(sub)}\n)`;
110
- } else {
111
- throw UnknownError.fromCode(
112
- new HashTreeDecodeErrorCode('Invalid tree structure for labeled'),
113
- );
114
109
  }
110
+ throw UnknownError.fromCode(
111
+ new HashTreeDecodeErrorCode('Invalid tree structure for labeled'),
112
+ );
115
113
  }
116
114
  case NodeType.Leaf: {
117
115
  if (!tree[1]) {
@@ -184,7 +182,7 @@ export interface CreateCertificateOptions {
184
182
  */
185
183
  principal: CertificatePrincipal;
186
184
  /**
187
- * BLS Verification strategy. Default strategy uses bls12_381 from @noble/curves
185
+ * BLS Verification strategy. Default strategy uses bls12_381 from `@noble/curves`
188
186
  */
189
187
  blsVerify?: VerifyFunc;
190
188
 
@@ -221,7 +219,7 @@ export class Certificate {
221
219
  /**
222
220
  * Create a new instance of a certificate, automatically verifying it.
223
221
  * @param {CreateCertificateOptions} options {@link CreateCertificateOptions}
224
- * @throws if the verification of the certificate fails
222
+ * @throws {Error} if the verification of the certificate fails
225
223
  */
226
224
  public static async create(options: CreateCertificateOptions): Promise<Certificate> {
227
225
  const cert = Certificate.createUnverified(options);
@@ -875,11 +873,11 @@ function list_paths(path: Array<NodeLabel>, tree: HashTree): Array<Array<NodeLab
875
873
  }
876
874
  }
877
875
 
878
- export type CheckCanisterRangesParams = {
876
+ export interface CheckCanisterRangesParams {
879
877
  canisterId: Principal;
880
878
  subnetId: Principal;
881
879
  tree: HashTree;
882
- };
880
+ }
883
881
 
884
882
  /**
885
883
  * Canister ranges in the form of an array of [start, end] principal tuples,
package/src/agent/der.ts CHANGED
@@ -9,57 +9,78 @@ import { uint8Equals } from './utils/buffer.ts';
9
9
  export const encodeLenBytes = (len: number): number => {
10
10
  if (len <= 0x7f) {
11
11
  return 1;
12
- } else if (len <= 0xff) {
12
+ }
13
+ if (len <= 0xff) {
13
14
  return 2;
14
- } else if (len <= 0xffff) {
15
+ }
16
+ if (len <= 0xffff) {
15
17
  return 3;
16
- } else if (len <= 0xffffff) {
18
+ }
19
+ if (len <= 0xffffff) {
17
20
  return 4;
18
- } else {
19
- throw InputError.fromCode(new DerEncodeErrorCode('Length too long (> 4 bytes)'));
20
21
  }
22
+ throw InputError.fromCode(new DerEncodeErrorCode('Length too long (> 4 bytes)'));
21
23
  };
22
24
 
23
25
  export const encodeLen = (buf: Uint8Array, offset: number, len: number): number => {
24
26
  if (len <= 0x7f) {
25
27
  buf[offset] = len;
26
28
  return 1;
27
- } else if (len <= 0xff) {
29
+ }
30
+ if (len <= 0xff) {
28
31
  buf[offset] = 0x81;
29
32
  buf[offset + 1] = len;
30
33
  return 2;
31
- } else if (len <= 0xffff) {
34
+ }
35
+ if (len <= 0xffff) {
32
36
  buf[offset] = 0x82;
33
37
  buf[offset + 1] = len >> 8;
34
38
  buf[offset + 2] = len;
35
39
  return 3;
36
- } else if (len <= 0xffffff) {
40
+ }
41
+ if (len <= 0xffffff) {
37
42
  buf[offset] = 0x83;
38
43
  buf[offset + 1] = len >> 16;
39
44
  buf[offset + 2] = len >> 8;
40
45
  buf[offset + 3] = len;
41
46
  return 4;
42
- } else {
43
- throw InputError.fromCode(new DerEncodeErrorCode('Length too long (> 4 bytes)'));
44
47
  }
48
+ throw InputError.fromCode(new DerEncodeErrorCode('Length too long (> 4 bytes)'));
45
49
  };
46
50
 
47
51
  export const decodeLenBytes = (buf: Uint8Array, offset: number): number => {
48
- if (buf[offset] < 0x80) return 1;
49
- if (buf[offset] === 0x80) throw InputError.fromCode(new DerDecodeErrorCode('Invalid length 0'));
50
- if (buf[offset] === 0x81) return 2;
51
- if (buf[offset] === 0x82) return 3;
52
- if (buf[offset] === 0x83) return 4;
52
+ if (buf[offset] < 0x80) {
53
+ return 1;
54
+ }
55
+ if (buf[offset] === 0x80) {
56
+ throw InputError.fromCode(new DerDecodeErrorCode('Invalid length 0'));
57
+ }
58
+ if (buf[offset] === 0x81) {
59
+ return 2;
60
+ }
61
+ if (buf[offset] === 0x82) {
62
+ return 3;
63
+ }
64
+ if (buf[offset] === 0x83) {
65
+ return 4;
66
+ }
53
67
  throw InputError.fromCode(new DerDecodeErrorCode('Length too long (> 4 bytes)'));
54
68
  };
55
69
 
56
70
  export const decodeLen = (buf: Uint8Array, offset: number): number => {
57
71
  const lenBytes = decodeLenBytes(buf, offset);
58
- if (lenBytes === 1) return buf[offset];
59
- else if (lenBytes === 2) return buf[offset + 1];
60
- else if (lenBytes === 3) return (buf[offset + 1] << 8) + buf[offset + 2];
61
- else if (lenBytes === 4)
72
+ if (lenBytes === 1) {
73
+ return buf[offset];
74
+ }
75
+ if (lenBytes === 2) {
76
+ return buf[offset + 1];
77
+ }
78
+ if (lenBytes === 3) {
79
+ return (buf[offset + 1] << 8) + buf[offset + 2];
80
+ }
81
+ if (lenBytes === 4) {
62
82
  return (buf[offset + 1] << 16) + (buf[offset + 2] << 8) + buf[offset + 3];
83
+ }
63
84
  throw InputError.fromCode(new DerDecodeErrorCode('Length too long (> 4 bytes)'));
64
85
  };
65
86
 
@@ -1,14 +1,10 @@
1
- import { Principal } from '#principal';
2
- import {
3
- type HttpDetailsResponse,
4
- type NodeSignature,
5
- type ReplicaRejectCode,
6
- } from './agent/api.ts';
7
- import { type RequestId } from './request_id.ts';
8
- import { type RequestStatusResponseStatus } from './agent/http/index.ts';
9
- import { type Expiry } from './agent/http/transforms.ts';
10
- import { type HttpHeaderField } from './agent/http/types.ts';
11
- import { LookupPathStatus, LookupSubtreeStatus } from './certificate.ts';
1
+ import type { Principal } from '#principal';
2
+ import type { HttpDetailsResponse, NodeSignature, ReplicaRejectCode } from './agent/api.ts';
3
+ import type { RequestId } from './request_id.ts';
4
+ import type { RequestStatusResponseStatus } from './agent/http/index.ts';
5
+ import type { Expiry } from './agent/http/expiry.ts';
6
+ import type { HttpHeaderField } from './agent/http/types.ts';
7
+ import type { LookupPathStatus, LookupSubtreeStatus } from './certificate.ts';
12
8
  import { bytesToHex } from '@noble/hashes/utils';
13
9
 
14
10
  export enum ErrorKindEnum {
@@ -22,22 +18,31 @@ export enum ErrorKindEnum {
22
18
  Unknown = 'Unknown',
23
19
  }
24
20
 
25
- export type RequestContext = {
21
+ export interface RequestContext {
26
22
  requestId?: RequestId;
27
23
  senderPubKey: Uint8Array;
28
24
  senderSignature: Uint8Array;
29
25
  ingressExpiry: Expiry;
30
- };
26
+ }
31
27
 
32
- export type CallContext = {
28
+ /**
29
+ * Call context for errors that arise during polling.
30
+ */
31
+ export interface PollingCallContext {
33
32
  canisterId: Principal;
34
33
  methodName: string;
34
+ }
35
+
36
+ /**
37
+ * Call context for errors that arise from a direct HTTP call response.
38
+ */
39
+ export interface CallContext extends PollingCallContext {
35
40
  httpDetails: HttpDetailsResponse;
36
- };
41
+ }
37
42
 
38
43
  abstract class ErrorCode {
39
44
  public requestContext?: RequestContext;
40
- public callContext?: CallContext;
45
+ public callContext?: CallContext | PollingCallContext;
41
46
 
42
47
  constructor(public readonly isCertified: boolean = false) {}
43
48
 
@@ -57,8 +62,10 @@ abstract class ErrorCode {
57
62
  errorMessage +=
58
63
  `\nCall context:\n` +
59
64
  ` Canister ID: ${this.callContext.canisterId.toText()}\n` +
60
- ` Method name: ${this.callContext.methodName}\n` +
61
- ` HTTP details: ${JSON.stringify(this.callContext.httpDetails, null, 2)}`;
65
+ ` Method name: ${this.callContext.methodName}`;
66
+ if ('httpDetails' in this.callContext) {
67
+ errorMessage += `\n HTTP details: ${JSON.stringify(this.callContext.httpDetails, null, 2)}`;
68
+ }
62
69
  }
63
70
  return errorMessage;
64
71
  }
@@ -598,19 +605,6 @@ export class HashValueErrorCode extends ErrorCode {
598
605
  }
599
606
  }
600
607
 
601
- export class HttpDefaultFetchErrorCode extends ErrorCode {
602
- public name = 'HttpDefaultFetchErrorCode';
603
-
604
- constructor(public readonly error: string) {
605
- super();
606
- Object.setPrototypeOf(this, HttpDefaultFetchErrorCode.prototype);
607
- }
608
-
609
- public toErrorMessage(): string {
610
- return this.error;
611
- }
612
- }
613
-
614
608
  export class IdentityInvalidErrorCode extends ErrorCode {
615
609
  public name = 'IdentityInvalidErrorCode';
616
610
 
@@ -653,6 +647,22 @@ export class CreateHttpAgentErrorCode extends ErrorCode {
653
647
  }
654
648
  }
655
649
 
650
+ export class ExcessiveSignaturesErrorCode extends ErrorCode {
651
+ public name = 'ExcessiveSignaturesErrorCode';
652
+
653
+ constructor(
654
+ public readonly signatureCount: number,
655
+ public readonly maxExpected: number,
656
+ ) {
657
+ super();
658
+ Object.setPrototypeOf(this, ExcessiveSignaturesErrorCode.prototype);
659
+ }
660
+
661
+ public toErrorMessage(): string {
662
+ return `Query response contains ${this.signatureCount} signatures, which exceeds the subnet size of ${this.maxExpected}. This suggests a malformed or malicious response.`;
663
+ }
664
+ }
665
+
656
666
  export class MalformedSignatureErrorCode extends ErrorCode {
657
667
  public name = 'MalformedSignatureErrorCode';
658
668
 
@@ -1,6 +1,6 @@
1
1
  import { Principal } from '#principal';
2
2
  import * as CanisterStatus from './canisterStatus/index.ts';
3
- import { IDL } from '#candid';
3
+ import type { IDL } from '#candid';
4
4
  import { HttpAgent } from './agent/http/index.ts';
5
5
  import { Actor, type ActorSubclass } from './actor.ts';
6
6
 
@@ -1,4 +1,4 @@
1
- import { AgentError } from './errors.ts';
1
+ import type { AgentError } from './errors.ts';
2
2
 
3
3
  export type ObserveFunction<T> = (data: T, ...rest: unknown[]) => void;
4
4
 
@@ -5,23 +5,23 @@ const MAX_INTERVAL_MSEC = 60_000;
5
5
  const MAX_ELAPSED_TIME_MSEC = 900_000;
6
6
  const MAX_ITERATIONS = 10;
7
7
 
8
- export type BackoffStrategy = {
8
+ export interface BackoffStrategy {
9
9
  next: () => number | null;
10
10
  currentInterval?: number;
11
11
  count?: number;
12
12
  ellapsedTimeInMsec?: number;
13
- };
13
+ }
14
14
 
15
- export type BackoffStrategyArgs = {
15
+ export interface BackoffStrategyArgs {
16
16
  maxIterations?: number;
17
17
  maxElapsedTime?: number;
18
- };
18
+ }
19
19
 
20
20
  export type BackoffStrategyFactory = (args?: BackoffStrategyArgs) => BackoffStrategy;
21
21
 
22
22
  // export type BackoffStrategyGenerator = Generator<number, void, unknown>;
23
23
 
24
- export type ExponentialBackoffOptions = {
24
+ export interface ExponentialBackoffOptions {
25
25
  initialInterval?: number;
26
26
  randomizationFactor?: number;
27
27
  multiplier?: number;
@@ -29,7 +29,7 @@ export type ExponentialBackoffOptions = {
29
29
  maxElapsedTime?: number;
30
30
  maxIterations?: number;
31
31
  date?: DateConstructor;
32
- };
32
+ }
33
33
 
34
34
  /**
35
35
  * Exponential backoff strategy.
@@ -105,17 +105,16 @@ export class ExponentialBackoff {
105
105
  public next() {
106
106
  if (this.ellapsedTimeInMsec >= this.#maxElapsedTime || this.#count >= this.#maxIterations) {
107
107
  return null;
108
- } else {
109
- this.incrementCurrentInterval();
110
- return this.randomValueFromInterval;
111
108
  }
109
+ this.incrementCurrentInterval();
110
+ return this.randomValueFromInterval;
112
111
  }
113
112
  }
114
113
  /**
115
114
  * Utility function to create an exponential backoff iterator.
116
115
  * @param options - for the exponential backoff
117
116
  * @returns an iterator that yields the next delay in the exponential backoff
118
- * @yields the next delay in the exponential backoff
117
+ * @yields {number} the next delay in the exponential backoff
119
118
  */
120
119
  export function* exponentialBackoff(
121
120
  options: ExponentialBackoffOptions = ExponentialBackoff.default,
@@ -1,13 +1,13 @@
1
- import { type RequestId } from '../request_id.ts';
1
+ import type { RequestId } from '../request_id.ts';
2
2
  import {
3
3
  type CreateCertificateOptions,
4
4
  Certificate,
5
5
  lookupResultToBuffer,
6
6
  } from '../certificate.ts';
7
- import { type Agent, type ReadStateResponse } from '../agent/api.ts';
8
- import { Principal } from '#principal';
7
+ import { readCertifiedReject } from '../utils/certificateReject.ts';
8
+ import type { Agent, ReadStateResponse } from '../agent/api.ts';
9
+ import type { Principal } from '#principal';
9
10
  import {
10
- CertifiedRejectErrorCode,
11
11
  ExternalError,
12
12
  InputError,
13
13
  InvalidReadStateRequestErrorCode,
@@ -20,16 +20,12 @@ import {
20
20
 
21
21
  export * as strategy from './strategy.ts';
22
22
  import { defaultStrategy } from './strategy.ts';
23
+ import type { PollStrategy, PollForResponseResult } from './types.ts';
23
24
  import { ReadRequestType, type ReadStateRequest } from '../agent/http/types.ts';
24
- import { RequestStatusResponseStatus } from '../agent/index.ts';
25
+ import { RequestStatusResponseStatus } from '../agent/http/index.ts';
25
26
  import { utf8ToBytes } from '@noble/hashes/utils';
26
27
  export { defaultStrategy } from './strategy.ts';
27
-
28
- export type PollStrategy = (
29
- canisterId: Principal,
30
- requestId: RequestId,
31
- status: RequestStatusResponseStatus,
32
- ) => Promise<void>;
28
+ export type { PollStrategy, PollForResponseResult } from './types.ts';
33
29
 
34
30
  interface SignedReadStateRequestWithExpiry extends ReadStateRequest {
35
31
  body: {
@@ -122,16 +118,17 @@ function isSignedReadStateRequestWithExpiry(
122
118
  * @param canisterId The effective canister ID.
123
119
  * @param requestId The Request ID to poll status for.
124
120
  * @param options polling options to control behavior
121
+ * @returns The certificate, reply bytes, and raw certificate bytes for the request.
122
+ * @throws {ExternalError} If the agent's root key is not available.
123
+ * @throws {RejectError} If the request was rejected by the canister.
124
+ * @throws {UnknownError} If the request reached `done` status without a reply.
125
125
  */
126
126
  export async function pollForResponse(
127
127
  agent: Agent,
128
128
  canisterId: Principal,
129
129
  requestId: RequestId,
130
130
  options: PollingOptions = {},
131
- ): Promise<{
132
- certificate: Certificate;
133
- reply: Uint8Array;
134
- }> {
131
+ ): Promise<PollForResponseResult> {
135
132
  const path = [utf8ToBytes('request_status'), requestId];
136
133
 
137
134
  let state: ReadStateResponse;
@@ -175,6 +172,7 @@ export async function pollForResponse(
175
172
  return {
176
173
  reply: lookupResultToBuffer(cert.lookup_path([...path, 'reply']))!,
177
174
  certificate: cert,
175
+ rawCertificate: state.certificate,
178
176
  };
179
177
  }
180
178
 
@@ -194,17 +192,7 @@ export async function pollForResponse(
194
192
  }
195
193
 
196
194
  case RequestStatusResponseStatus.Rejected: {
197
- const rejectCode = new Uint8Array(
198
- lookupResultToBuffer(cert.lookup_path([...path, 'reject_code']))!,
199
- )[0];
200
- const rejectMessage = new TextDecoder().decode(
201
- lookupResultToBuffer(cert.lookup_path([...path, 'reject_message']))!,
202
- );
203
- const errorCodeBuf = lookupResultToBuffer(cert.lookup_path([...path, 'error_code']));
204
- const errorCode = errorCodeBuf ? new TextDecoder().decode(errorCodeBuf) : undefined;
205
- throw RejectError.fromCode(
206
- new CertifiedRejectErrorCode(requestId, rejectCode, rejectMessage, errorCode),
207
- );
195
+ throw RejectError.fromCode(readCertifiedReject(cert, path, requestId));
208
196
  }
209
197
 
210
198
  case RequestStatusResponseStatus.Done:
@@ -1,14 +1,10 @@
1
- import { Principal } from '#principal';
2
- import { RequestStatusResponseStatus } from '../agent/index.ts';
3
- import { type PollStrategy } from './index.ts';
4
- import { type RequestId } from '../request_id.ts';
1
+ import type { Principal } from '#principal';
2
+ import type { RequestStatusResponseStatus } from '../agent/http/index.ts';
3
+ import type { RequestId } from '../request_id.ts';
5
4
  import { ProtocolError, TimeoutWaitingForResponseErrorCode } from '../errors.ts';
6
5
 
7
- export type Predicate<T> = (
8
- canisterId: Principal,
9
- requestId: RequestId,
10
- status: RequestStatusResponseStatus,
11
- ) => Promise<T>;
6
+ import type { PollStrategy, Predicate } from './types.ts';
7
+ export type { PollStrategy, Predicate } from './types.ts';
12
8
 
13
9
  const FIVE_MINUTES_IN_MSEC = 5 * 60 * 1000;
14
10
 
@@ -0,0 +1,28 @@
1
+ import type { Certificate } from '../certificate.ts';
2
+ import type { Principal } from '#principal';
3
+ import type { RequestStatusResponseStatus } from '../agent/http/index.ts';
4
+ import type { RequestId } from '../request_id.ts';
5
+
6
+ export type PollStrategy = (
7
+ canisterId: Principal,
8
+ requestId: RequestId,
9
+ status: RequestStatusResponseStatus,
10
+ ) => Promise<void>;
11
+
12
+ export type Predicate<T> = (
13
+ canisterId: Principal,
14
+ requestId: RequestId,
15
+ status: RequestStatusResponseStatus,
16
+ ) => Promise<T>;
17
+
18
+ /**
19
+ * The result of polling for a response, including the certificate, reply bytes, and raw certificate bytes.
20
+ */
21
+ export interface PollForResponseResult {
22
+ /** The certificate for the request, which can be used to verify the reply. */
23
+ certificate: Certificate;
24
+ /** The reply bytes for the request. */
25
+ reply: Uint8Array;
26
+ /** The raw certificate bytes for the request. */
27
+ rawCertificate: Uint8Array;
28
+ }
@@ -1,4 +1,4 @@
1
- import { type DerEncodedPublicKey, type PublicKey } from './auth.ts';
1
+ import type { DerEncodedPublicKey, PublicKey } from './auth.ts';
2
2
  import { ED25519_OID, unwrapDER, wrapDER } from './der.ts';
3
3
  import { DerDecodeErrorCode, InputError } from './errors.ts';
4
4
 
@@ -1,5 +1,5 @@
1
1
  import { lebEncode, compare } from '#candid';
2
- import { Principal } from '#principal';
2
+ import type { Principal } from '#principal';
3
3
  import { HashValueErrorCode, InputError } from './errors.ts';
4
4
  import { uint8FromBufLike } from './utils/buffer.ts';
5
5
  import { concatBytes } from '@noble/hashes/utils';
@@ -19,16 +19,21 @@ interface ToHashable {
19
19
  export function hashValue(value: unknown): Uint8Array {
20
20
  if (typeof value === 'string') {
21
21
  return hashString(value);
22
- } else if (typeof value === 'number') {
22
+ }
23
+ if (typeof value === 'number') {
23
24
  return sha256(lebEncode(value));
24
- } else if (value instanceof Uint8Array || ArrayBuffer.isView(value)) {
25
+ }
26
+ if (value instanceof Uint8Array || ArrayBuffer.isView(value)) {
25
27
  return sha256(uint8FromBufLike(value));
26
- } else if (Array.isArray(value)) {
28
+ }
29
+ if (Array.isArray(value)) {
27
30
  const vals = value.map(hashValue);
28
31
  return sha256(concatBytes(...vals));
29
- } else if (value && typeof value === 'object' && (value as Principal)._isPrincipal) {
32
+ }
33
+ if (value && typeof value === 'object' && (value as Principal)._isPrincipal) {
30
34
  return sha256((value as Principal).toUint8Array());
31
- } else if (
35
+ }
36
+ if (
32
37
  typeof value === 'object' &&
33
38
  value !== null &&
34
39
  typeof (value as ToHashable).toHash === 'function'
@@ -38,9 +43,11 @@ export function hashValue(value: unknown): Uint8Array {
38
43
  // the flow to be synchronous to ensure Safari touch id works.
39
44
  // } else if (value instanceof Promise) {
40
45
  // return value.then(x => hashValue(x));
41
- } else if (typeof value === 'object') {
46
+ }
47
+ if (typeof value === 'object') {
42
48
  return hashOfMap(value as Record<string, unknown>);
43
- } else if (typeof value === 'bigint') {
49
+ }
50
+ if (typeof value === 'bigint') {
44
51
  // Do this check much later than the other bigint check because this one is much less
45
52
  // type-safe.
46
53
  // So we want to try all the high-assurance type guards before this 'probable' one.