@icp-sdk/core 4.2.3 → 5.0.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 (456) hide show
  1. package/lib/cjs/agent/actor.d.ts +203 -0
  2. package/lib/cjs/agent/actor.d.ts.map +1 -0
  3. package/lib/cjs/agent/actor.js +341 -0
  4. package/lib/cjs/agent/actor.js.map +1 -0
  5. package/lib/cjs/agent/agent/api.d.ts +216 -0
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -0
  7. package/lib/cjs/agent/agent/api.js +39 -0
  8. package/lib/cjs/agent/agent/api.js.map +1 -0
  9. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts +7 -0
  10. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -0
  11. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js +19 -0
  12. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -0
  13. package/lib/cjs/agent/agent/http/index.d.ts +171 -0
  14. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -0
  15. package/lib/cjs/agent/agent/http/index.js +1079 -0
  16. package/lib/cjs/agent/agent/http/index.js.map +1 -0
  17. package/lib/cjs/agent/agent/http/transforms.d.ts +54 -0
  18. package/lib/cjs/agent/agent/http/transforms.d.ts.map +1 -0
  19. package/lib/cjs/agent/agent/http/transforms.js +135 -0
  20. package/lib/cjs/agent/agent/http/transforms.js.map +1 -0
  21. package/lib/cjs/agent/agent/http/types.d.ts +81 -0
  22. package/lib/cjs/agent/agent/http/types.d.ts.map +1 -0
  23. package/lib/cjs/agent/agent/http/types.js +43 -0
  24. package/lib/cjs/agent/agent/http/types.js.map +1 -0
  25. package/lib/cjs/agent/agent/index.d.ts +3 -0
  26. package/lib/cjs/agent/agent/index.d.ts.map +1 -0
  27. package/lib/cjs/agent/agent/index.js +19 -0
  28. package/lib/cjs/agent/agent/index.js.map +1 -0
  29. package/lib/cjs/agent/auth.d.ts +91 -0
  30. package/lib/cjs/agent/auth.d.ts.map +1 -0
  31. package/lib/cjs/agent/auth.js +65 -0
  32. package/lib/cjs/agent/auth.js.map +1 -0
  33. package/lib/cjs/agent/canisterStatus/index.d.ts +93 -0
  34. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -0
  35. package/lib/cjs/agent/canisterStatus/index.js +301 -0
  36. package/lib/cjs/agent/canisterStatus/index.js.map +1 -0
  37. package/lib/cjs/agent/cbor.d.ts +26 -0
  38. package/lib/cjs/agent/cbor.d.ts.map +1 -0
  39. package/lib/cjs/agent/cbor.js +93 -0
  40. package/lib/cjs/agent/cbor.js.map +1 -0
  41. package/lib/cjs/agent/certificate.d.ts +228 -0
  42. package/lib/cjs/agent/certificate.d.ts.map +1 -0
  43. package/lib/cjs/agent/certificate.js +591 -0
  44. package/lib/cjs/agent/certificate.js.map +1 -0
  45. package/lib/cjs/agent/constants.d.ts +14 -0
  46. package/lib/cjs/agent/constants.d.ts.map +1 -0
  47. package/lib/cjs/agent/constants.js +18 -0
  48. package/lib/cjs/agent/constants.js.map +1 -0
  49. package/lib/cjs/agent/der.d.ts +34 -0
  50. package/lib/cjs/agent/der.d.ts.map +1 -0
  51. package/lib/cjs/agent/der.js +175 -0
  52. package/lib/cjs/agent/der.js.map +1 -0
  53. package/lib/cjs/agent/errors.d.ts +380 -0
  54. package/lib/cjs/agent/errors.d.ts.map +1 -0
  55. package/lib/cjs/agent/errors.js +733 -0
  56. package/lib/cjs/agent/errors.js.map +1 -0
  57. package/lib/cjs/agent/fetch_candid.d.ts +9 -0
  58. package/lib/cjs/agent/fetch_candid.d.ts.map +1 -0
  59. package/lib/cjs/agent/fetch_candid.js +69 -0
  60. package/lib/cjs/agent/fetch_candid.js.map +1 -0
  61. package/lib/cjs/agent/index.d.ts +31 -1
  62. package/lib/cjs/agent/index.d.ts.map +1 -1
  63. package/lib/cjs/agent/index.js +59 -1
  64. package/lib/cjs/agent/index.js.map +1 -1
  65. package/lib/cjs/agent/observable.d.ts +24 -0
  66. package/lib/cjs/agent/observable.d.ts.map +1 -0
  67. package/lib/cjs/agent/observable.js +34 -0
  68. package/lib/cjs/agent/observable.js.map +1 -0
  69. package/lib/cjs/agent/polling/backoff.d.ts +50 -0
  70. package/lib/cjs/agent/polling/backoff.d.ts.map +1 -0
  71. package/lib/cjs/agent/polling/backoff.js +90 -0
  72. package/lib/cjs/agent/polling/backoff.js.map +1 -0
  73. package/lib/cjs/agent/polling/index.d.ts +63 -0
  74. package/lib/cjs/agent/polling/index.d.ts.map +1 -0
  75. package/lib/cjs/agent/polling/index.js +183 -0
  76. package/lib/cjs/agent/polling/index.js.map +1 -0
  77. package/lib/cjs/agent/polling/strategy.d.ts +52 -0
  78. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -0
  79. package/lib/cjs/agent/polling/strategy.js +104 -0
  80. package/lib/cjs/agent/polling/strategy.js.map +1 -0
  81. package/lib/cjs/agent/public_key.d.ts +16 -0
  82. package/lib/cjs/agent/public_key.d.ts.map +1 -0
  83. package/lib/cjs/agent/public_key.js +52 -0
  84. package/lib/cjs/agent/public_key.js.map +1 -0
  85. package/lib/cjs/agent/request_id.d.ts +24 -0
  86. package/lib/cjs/agent/request_id.d.ts.map +1 -0
  87. package/lib/cjs/agent/request_id.js +88 -0
  88. package/lib/cjs/agent/request_id.js.map +1 -0
  89. package/lib/cjs/agent/utils/bls.d.ts +10 -0
  90. package/lib/cjs/agent/utils/bls.d.ts.map +1 -0
  91. package/lib/cjs/agent/utils/bls.js +20 -0
  92. package/lib/cjs/agent/utils/bls.js.map +1 -0
  93. package/lib/cjs/agent/utils/buffer.d.ts +22 -0
  94. package/lib/cjs/agent/utils/buffer.d.ts.map +1 -0
  95. package/lib/cjs/agent/utils/buffer.js +55 -0
  96. package/lib/cjs/agent/utils/buffer.js.map +1 -0
  97. package/lib/cjs/agent/utils/expirableMap.d.ts +80 -0
  98. package/lib/cjs/agent/utils/expirableMap.d.ts.map +1 -0
  99. package/lib/cjs/agent/utils/expirableMap.js +148 -0
  100. package/lib/cjs/agent/utils/expirableMap.js.map +1 -0
  101. package/lib/cjs/agent/utils/leb.d.ts +3 -0
  102. package/lib/cjs/agent/utils/leb.d.ts.map +1 -0
  103. package/lib/cjs/agent/utils/leb.js +17 -0
  104. package/lib/cjs/agent/utils/leb.js.map +1 -0
  105. package/lib/cjs/agent/utils/random.d.ts +6 -0
  106. package/lib/cjs/agent/utils/random.d.ts.map +1 -0
  107. package/lib/cjs/agent/utils/random.js +29 -0
  108. package/lib/cjs/agent/utils/random.js.map +1 -0
  109. package/lib/cjs/candid/candid-core.d.ts +72 -0
  110. package/lib/cjs/candid/candid-core.d.ts.map +1 -0
  111. package/lib/cjs/candid/candid-core.js +251 -0
  112. package/lib/cjs/candid/candid-core.js.map +1 -0
  113. package/lib/cjs/candid/candid-ui.d.ts +35 -0
  114. package/lib/cjs/candid/candid-ui.d.ts.map +1 -0
  115. package/lib/cjs/candid/candid-ui.js +317 -0
  116. package/lib/cjs/candid/candid-ui.js.map +1 -0
  117. package/lib/cjs/candid/idl.d.ts +579 -0
  118. package/lib/cjs/candid/idl.d.ts.map +1 -0
  119. package/lib/cjs/candid/idl.js +2228 -0
  120. package/lib/cjs/candid/idl.js.map +1 -0
  121. package/lib/cjs/candid/index.d.ts +11 -1
  122. package/lib/cjs/candid/index.d.ts.map +1 -1
  123. package/lib/cjs/candid/index.js +33 -1
  124. package/lib/cjs/candid/index.js.map +1 -1
  125. package/lib/cjs/candid/types.d.ts +6 -0
  126. package/lib/cjs/candid/types.d.ts.map +1 -0
  127. package/lib/cjs/candid/types.js +4 -0
  128. package/lib/cjs/candid/types.js.map +1 -0
  129. package/lib/cjs/candid/utils/bigint-math.d.ts +14 -0
  130. package/lib/cjs/candid/utils/bigint-math.d.ts.map +1 -0
  131. package/lib/cjs/candid/utils/bigint-math.js +30 -0
  132. package/lib/cjs/candid/utils/bigint-math.js.map +1 -0
  133. package/lib/cjs/candid/utils/buffer.d.ts +86 -0
  134. package/lib/cjs/candid/utils/buffer.d.ts.map +1 -0
  135. package/lib/cjs/candid/utils/buffer.js +191 -0
  136. package/lib/cjs/candid/utils/buffer.js.map +1 -0
  137. package/lib/cjs/candid/utils/hash.d.ts +7 -0
  138. package/lib/cjs/candid/utils/hash.d.ts.map +1 -0
  139. package/lib/cjs/candid/utils/hash.js +33 -0
  140. package/lib/cjs/candid/utils/hash.js.map +1 -0
  141. package/lib/cjs/candid/utils/leb128.d.ts +65 -0
  142. package/lib/cjs/candid/utils/leb128.d.ts.map +1 -0
  143. package/lib/cjs/candid/utils/leb128.js +228 -0
  144. package/lib/cjs/candid/utils/leb128.js.map +1 -0
  145. package/lib/cjs/identity/identity/delegation.d.ts +164 -0
  146. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -0
  147. package/lib/cjs/identity/identity/delegation.js +302 -0
  148. package/lib/cjs/identity/identity/delegation.js.map +1 -0
  149. package/lib/cjs/identity/identity/ecdsa.d.ts +62 -0
  150. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -0
  151. package/lib/cjs/identity/identity/ecdsa.js +117 -0
  152. package/lib/cjs/identity/identity/ecdsa.js.map +1 -0
  153. package/lib/cjs/identity/identity/ed25519.d.ts +67 -0
  154. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -0
  155. package/lib/cjs/identity/identity/ed25519.js +200 -0
  156. package/lib/cjs/identity/identity/ed25519.js.map +1 -0
  157. package/lib/cjs/identity/identity/partial.d.ts +34 -0
  158. package/lib/cjs/identity/identity/partial.d.ts.map +1 -0
  159. package/lib/cjs/identity/identity/partial.js +54 -0
  160. package/lib/cjs/identity/identity/partial.js.map +1 -0
  161. package/lib/cjs/identity/identity/webauthn.d.ts +51 -0
  162. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -0
  163. package/lib/cjs/identity/identity/webauthn.js +197 -0
  164. package/lib/cjs/identity/identity/webauthn.js.map +1 -0
  165. package/lib/cjs/identity/index.d.ts +15 -1
  166. package/lib/cjs/identity/index.d.ts.map +1 -1
  167. package/lib/cjs/identity/index.js +24 -1
  168. package/lib/cjs/identity/index.js.map +1 -1
  169. package/lib/cjs/identity/secp256k1/der.d.ts +33 -0
  170. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -0
  171. package/lib/cjs/identity/secp256k1/der.js +165 -0
  172. package/lib/cjs/identity/secp256k1/der.js.map +1 -0
  173. package/lib/cjs/identity/secp256k1/index.d.ts +4 -1
  174. package/lib/cjs/identity/secp256k1/index.d.ts.map +1 -1
  175. package/lib/cjs/identity/secp256k1/index.js +4 -1
  176. package/lib/cjs/identity/secp256k1/index.js.map +1 -1
  177. package/lib/cjs/identity/secp256k1/pem.d.ts +7 -0
  178. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -0
  179. package/lib/cjs/identity/secp256k1/pem.js +75 -0
  180. package/lib/cjs/identity/secp256k1/pem.js.map +1 -0
  181. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +87 -0
  182. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -0
  183. package/lib/cjs/identity/secp256k1/secp256k1.js +260 -0
  184. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -0
  185. package/lib/cjs/principal/index.d.ts +3 -1
  186. package/lib/cjs/principal/index.d.ts.map +1 -1
  187. package/lib/cjs/principal/index.js +7 -1
  188. package/lib/cjs/principal/index.js.map +1 -1
  189. package/lib/cjs/principal/principal.d.ts +50 -0
  190. package/lib/cjs/principal/principal.d.ts.map +1 -0
  191. package/lib/cjs/principal/principal.js +148 -0
  192. package/lib/cjs/principal/principal.js.map +1 -0
  193. package/lib/cjs/principal/utils/base32.d.ts +10 -0
  194. package/lib/cjs/principal/utils/base32.d.ts.map +1 -0
  195. package/lib/cjs/principal/utils/base32.js +90 -0
  196. package/lib/cjs/principal/utils/base32.js.map +1 -0
  197. package/lib/cjs/principal/utils/getCrc.d.ts +6 -0
  198. package/lib/cjs/principal/utils/getCrc.d.ts.map +1 -0
  199. package/lib/cjs/principal/utils/getCrc.js +53 -0
  200. package/lib/cjs/principal/utils/getCrc.js.map +1 -0
  201. package/lib/esm/agent/actor.d.ts +203 -0
  202. package/lib/esm/agent/actor.d.ts.map +1 -0
  203. package/lib/esm/agent/actor.js +337 -0
  204. package/lib/esm/agent/actor.js.map +1 -0
  205. package/lib/esm/agent/agent/api.d.ts +216 -0
  206. package/lib/esm/agent/agent/api.d.ts.map +1 -0
  207. package/lib/esm/agent/agent/api.js +34 -0
  208. package/lib/esm/agent/agent/api.js.map +1 -0
  209. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts +7 -0
  210. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -0
  211. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js +16 -0
  212. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -0
  213. package/lib/esm/agent/agent/http/index.d.ts +171 -0
  214. package/lib/esm/agent/agent/http/index.d.ts.map +1 -0
  215. package/lib/esm/agent/agent/http/index.js +1037 -0
  216. package/lib/esm/agent/agent/http/index.js.map +1 -0
  217. package/lib/esm/agent/agent/http/transforms.d.ts +54 -0
  218. package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -0
  219. package/lib/esm/agent/agent/http/transforms.js +128 -0
  220. package/lib/esm/agent/agent/http/transforms.js.map +1 -0
  221. package/lib/esm/agent/agent/http/types.d.ts +81 -0
  222. package/lib/esm/agent/agent/http/types.d.ts.map +1 -0
  223. package/lib/esm/agent/agent/http/types.js +39 -0
  224. package/lib/esm/agent/agent/http/types.js.map +1 -0
  225. package/lib/esm/agent/agent/index.d.ts +3 -0
  226. package/lib/esm/agent/agent/index.d.ts.map +1 -0
  227. package/lib/esm/agent/agent/index.js +3 -0
  228. package/lib/esm/agent/agent/index.js.map +1 -0
  229. package/lib/esm/agent/auth.d.ts +91 -0
  230. package/lib/esm/agent/auth.d.ts.map +1 -0
  231. package/lib/esm/agent/auth.js +59 -0
  232. package/lib/esm/agent/auth.js.map +1 -0
  233. package/lib/esm/agent/canister-env/index.d.ts +110 -1
  234. package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
  235. package/lib/esm/agent/canister-env/index.js +110 -1
  236. package/lib/esm/agent/canister-env/index.js.map +1 -1
  237. package/lib/esm/agent/canisterStatus/index.d.ts +93 -0
  238. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -0
  239. package/lib/esm/agent/canisterStatus/index.js +261 -0
  240. package/lib/esm/agent/canisterStatus/index.js.map +1 -0
  241. package/lib/esm/agent/cbor.d.ts +26 -0
  242. package/lib/esm/agent/cbor.d.ts.map +1 -0
  243. package/lib/esm/agent/cbor.js +54 -0
  244. package/lib/esm/agent/cbor.js.map +1 -0
  245. package/lib/esm/agent/certificate.d.ts +228 -0
  246. package/lib/esm/agent/certificate.d.ts.map +1 -0
  247. package/lib/esm/agent/certificate.js +545 -0
  248. package/lib/esm/agent/certificate.js.map +1 -0
  249. package/lib/esm/agent/constants.d.ts +14 -0
  250. package/lib/esm/agent/constants.d.ts.map +1 -0
  251. package/lib/esm/agent/constants.js +15 -0
  252. package/lib/esm/agent/constants.js.map +1 -0
  253. package/lib/esm/agent/der.d.ts +34 -0
  254. package/lib/esm/agent/der.d.ts.map +1 -0
  255. package/lib/esm/agent/der.js +166 -0
  256. package/lib/esm/agent/der.js.map +1 -0
  257. package/lib/esm/agent/errors.d.ts +380 -0
  258. package/lib/esm/agent/errors.d.ts.map +1 -0
  259. package/lib/esm/agent/errors.js +678 -0
  260. package/lib/esm/agent/errors.js.map +1 -0
  261. package/lib/esm/agent/fetch_candid.d.ts +9 -0
  262. package/lib/esm/agent/fetch_candid.d.ts.map +1 -0
  263. package/lib/esm/agent/fetch_candid.js +33 -0
  264. package/lib/esm/agent/fetch_candid.js.map +1 -0
  265. package/lib/esm/agent/index.d.ts +31 -1
  266. package/lib/esm/agent/index.d.ts.map +1 -1
  267. package/lib/esm/agent/index.js +31 -1
  268. package/lib/esm/agent/index.js.map +1 -1
  269. package/lib/esm/agent/observable.d.ts +24 -0
  270. package/lib/esm/agent/observable.d.ts.map +1 -0
  271. package/lib/esm/agent/observable.js +29 -0
  272. package/lib/esm/agent/observable.js.map +1 -0
  273. package/lib/esm/agent/polling/backoff.d.ts +50 -0
  274. package/lib/esm/agent/polling/backoff.d.ts.map +1 -0
  275. package/lib/esm/agent/polling/backoff.js +85 -0
  276. package/lib/esm/agent/polling/backoff.js.map +1 -0
  277. package/lib/esm/agent/polling/index.d.ts +63 -0
  278. package/lib/esm/agent/polling/index.d.ts.map +1 -0
  279. package/lib/esm/agent/polling/index.js +144 -0
  280. package/lib/esm/agent/polling/index.js.map +1 -0
  281. package/lib/esm/agent/polling/strategy.d.ts +52 -0
  282. package/lib/esm/agent/polling/strategy.d.ts.map +1 -0
  283. package/lib/esm/agent/polling/strategy.js +94 -0
  284. package/lib/esm/agent/polling/strategy.js.map +1 -0
  285. package/lib/esm/agent/public_key.d.ts +16 -0
  286. package/lib/esm/agent/public_key.d.ts.map +1 -0
  287. package/lib/esm/agent/public_key.js +48 -0
  288. package/lib/esm/agent/public_key.js.map +1 -0
  289. package/lib/esm/agent/request_id.d.ts +24 -0
  290. package/lib/esm/agent/request_id.d.ts.map +1 -0
  291. package/lib/esm/agent/request_id.js +83 -0
  292. package/lib/esm/agent/request_id.js.map +1 -0
  293. package/lib/esm/agent/utils/bls.d.ts +10 -0
  294. package/lib/esm/agent/utils/bls.d.ts.map +1 -0
  295. package/lib/esm/agent/utils/bls.js +17 -0
  296. package/lib/esm/agent/utils/bls.js.map +1 -0
  297. package/lib/esm/agent/utils/buffer.d.ts +22 -0
  298. package/lib/esm/agent/utils/buffer.d.ts.map +1 -0
  299. package/lib/esm/agent/utils/buffer.js +50 -0
  300. package/lib/esm/agent/utils/buffer.js.map +1 -0
  301. package/lib/esm/agent/utils/expirableMap.d.ts +80 -0
  302. package/lib/esm/agent/utils/expirableMap.d.ts.map +1 -0
  303. package/lib/esm/agent/utils/expirableMap.js +144 -0
  304. package/lib/esm/agent/utils/expirableMap.js.map +1 -0
  305. package/lib/esm/agent/utils/leb.d.ts +3 -0
  306. package/lib/esm/agent/utils/leb.d.ts.map +1 -0
  307. package/lib/esm/agent/utils/leb.js +12 -0
  308. package/lib/esm/agent/utils/leb.js.map +1 -0
  309. package/lib/esm/agent/utils/random.d.ts +6 -0
  310. package/lib/esm/agent/utils/random.d.ts.map +1 -0
  311. package/lib/esm/agent/utils/random.js +25 -0
  312. package/lib/esm/agent/utils/random.js.map +1 -0
  313. package/lib/esm/candid/candid-core.d.ts +72 -0
  314. package/lib/esm/candid/candid-core.d.ts.map +1 -0
  315. package/lib/esm/candid/candid-core.js +241 -0
  316. package/lib/esm/candid/candid-core.js.map +1 -0
  317. package/lib/esm/candid/candid-ui.d.ts +35 -0
  318. package/lib/esm/candid/candid-ui.d.ts.map +1 -0
  319. package/lib/esm/candid/candid-ui.js +272 -0
  320. package/lib/esm/candid/candid-ui.js.map +1 -0
  321. package/lib/esm/candid/idl.d.ts +579 -0
  322. package/lib/esm/candid/idl.d.ts.map +1 -0
  323. package/lib/esm/candid/idl.js +2189 -0
  324. package/lib/esm/candid/idl.js.map +1 -0
  325. package/lib/esm/candid/index.d.ts +11 -1
  326. package/lib/esm/candid/index.d.ts.map +1 -1
  327. package/lib/esm/candid/index.js +10 -1
  328. package/lib/esm/candid/index.js.map +1 -1
  329. package/lib/esm/candid/types.d.ts +6 -0
  330. package/lib/esm/candid/types.d.ts.map +1 -0
  331. package/lib/esm/candid/types.js +3 -0
  332. package/lib/esm/candid/types.js.map +1 -0
  333. package/lib/esm/candid/utils/bigint-math.d.ts +14 -0
  334. package/lib/esm/candid/utils/bigint-math.d.ts.map +1 -0
  335. package/lib/esm/candid/utils/bigint-math.js +26 -0
  336. package/lib/esm/candid/utils/bigint-math.js.map +1 -0
  337. package/lib/esm/candid/utils/buffer.d.ts +86 -0
  338. package/lib/esm/candid/utils/buffer.d.ts.map +1 -0
  339. package/lib/esm/candid/utils/buffer.js +182 -0
  340. package/lib/esm/candid/utils/buffer.js.map +1 -0
  341. package/lib/esm/candid/utils/hash.d.ts +7 -0
  342. package/lib/esm/candid/utils/hash.d.ts.map +1 -0
  343. package/lib/esm/candid/utils/hash.js +30 -0
  344. package/lib/esm/candid/utils/hash.js.map +1 -0
  345. package/lib/esm/candid/utils/leb128.d.ts +65 -0
  346. package/lib/esm/candid/utils/leb128.d.ts.map +1 -0
  347. package/lib/esm/candid/utils/leb128.js +216 -0
  348. package/lib/esm/candid/utils/leb128.js.map +1 -0
  349. package/lib/esm/identity/identity/delegation.d.ts +164 -0
  350. package/lib/esm/identity/identity/delegation.d.ts.map +1 -0
  351. package/lib/esm/identity/identity/delegation.js +294 -0
  352. package/lib/esm/identity/identity/delegation.js.map +1 -0
  353. package/lib/esm/identity/identity/ecdsa.d.ts +62 -0
  354. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -0
  355. package/lib/esm/identity/identity/ecdsa.js +112 -0
  356. package/lib/esm/identity/identity/ecdsa.js.map +1 -0
  357. package/lib/esm/identity/identity/ed25519.d.ts +67 -0
  358. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -0
  359. package/lib/esm/identity/identity/ed25519.js +195 -0
  360. package/lib/esm/identity/identity/ed25519.js.map +1 -0
  361. package/lib/esm/identity/identity/partial.d.ts +34 -0
  362. package/lib/esm/identity/identity/partial.d.ts.map +1 -0
  363. package/lib/esm/identity/identity/partial.js +50 -0
  364. package/lib/esm/identity/identity/partial.js.map +1 -0
  365. package/lib/esm/identity/identity/webauthn.d.ts +51 -0
  366. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -0
  367. package/lib/esm/identity/identity/webauthn.js +192 -0
  368. package/lib/esm/identity/identity/webauthn.js.map +1 -0
  369. package/lib/esm/identity/index.d.ts +15 -1
  370. package/lib/esm/identity/index.d.ts.map +1 -1
  371. package/lib/esm/identity/index.js +17 -1
  372. package/lib/esm/identity/index.js.map +1 -1
  373. package/lib/esm/identity/secp256k1/der.d.ts +33 -0
  374. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -0
  375. package/lib/esm/identity/secp256k1/der.js +156 -0
  376. package/lib/esm/identity/secp256k1/der.js.map +1 -0
  377. package/lib/esm/identity/secp256k1/index.d.ts +4 -1
  378. package/lib/esm/identity/secp256k1/index.d.ts.map +1 -1
  379. package/lib/esm/identity/secp256k1/index.js +4 -1
  380. package/lib/esm/identity/secp256k1/index.js.map +1 -1
  381. package/lib/esm/identity/secp256k1/pem.d.ts +7 -0
  382. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -0
  383. package/lib/esm/identity/secp256k1/pem.js +39 -0
  384. package/lib/esm/identity/secp256k1/pem.js.map +1 -0
  385. package/lib/esm/identity/secp256k1/secp256k1.d.ts +87 -0
  386. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -0
  387. package/lib/esm/identity/secp256k1/secp256k1.js +222 -0
  388. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -0
  389. package/lib/esm/principal/index.d.ts +3 -1
  390. package/lib/esm/principal/index.d.ts.map +1 -1
  391. package/lib/esm/principal/index.js +3 -1
  392. package/lib/esm/principal/index.js.map +1 -1
  393. package/lib/esm/principal/principal.d.ts +50 -0
  394. package/lib/esm/principal/principal.d.ts.map +1 -0
  395. package/lib/esm/principal/principal.js +144 -0
  396. package/lib/esm/principal/principal.js.map +1 -0
  397. package/lib/esm/principal/utils/base32.d.ts +10 -0
  398. package/lib/esm/principal/utils/base32.d.ts.map +1 -0
  399. package/lib/esm/principal/utils/base32.js +86 -0
  400. package/lib/esm/principal/utils/base32.js.map +1 -0
  401. package/lib/esm/principal/utils/getCrc.d.ts +6 -0
  402. package/lib/esm/principal/utils/getCrc.d.ts.map +1 -0
  403. package/lib/esm/principal/utils/getCrc.js +50 -0
  404. package/lib/esm/principal/utils/getCrc.js.map +1 -0
  405. package/package.json +17 -11
  406. package/src/agent/actor.ts +576 -0
  407. package/src/agent/agent/api.ts +262 -0
  408. package/src/agent/agent/http/index.ts +1405 -0
  409. package/src/agent/agent/http/transforms.ts +158 -0
  410. package/src/agent/agent/http/types.ts +126 -0
  411. package/src/agent/agent/index.ts +2 -0
  412. package/src/agent/auth.ts +147 -0
  413. package/src/agent/canister-env/index.ts +191 -1
  414. package/src/agent/canisterStatus/index.ts +412 -0
  415. package/src/agent/cbor.ts +63 -0
  416. package/src/agent/certificate.ts +841 -0
  417. package/src/agent/constants.ts +19 -0
  418. package/src/agent/der.ts +170 -0
  419. package/src/agent/errors.ts +868 -0
  420. package/src/agent/fetch_candid.ts +37 -0
  421. package/src/agent/index.ts +37 -1
  422. package/src/agent/observable.ts +49 -0
  423. package/src/agent/polling/backoff.ts +130 -0
  424. package/src/agent/polling/index.ts +250 -0
  425. package/src/agent/polling/strategy.ts +144 -0
  426. package/src/agent/public_key.ts +65 -0
  427. package/src/agent/request_id.ts +92 -0
  428. package/src/agent/utils/bls.ts +18 -0
  429. package/src/agent/utils/buffer.ts +60 -0
  430. package/src/agent/utils/expirableMap.ts +163 -0
  431. package/src/agent/utils/leb.ts +14 -0
  432. package/src/agent/utils/random.ts +30 -0
  433. package/src/candid/candid-core.ts +276 -0
  434. package/src/candid/candid-ui.ts +289 -0
  435. package/src/candid/idl.ts +2556 -0
  436. package/src/candid/index.ts +16 -1
  437. package/src/candid/types.ts +7 -0
  438. package/src/candid/utils/bigint-math.ts +26 -0
  439. package/src/candid/utils/buffer.ts +221 -0
  440. package/src/candid/utils/hash.ts +31 -0
  441. package/src/candid/utils/leb128.ts +234 -0
  442. package/src/identity/identity/delegation.ts +411 -0
  443. package/src/identity/identity/ecdsa.ts +158 -0
  444. package/src/identity/identity/ed25519.ts +239 -0
  445. package/src/identity/identity/partial.ts +60 -0
  446. package/src/identity/identity/webauthn.ts +265 -0
  447. package/src/identity/index.ts +19 -1
  448. package/src/identity/secp256k1/der.ts +156 -0
  449. package/src/identity/secp256k1/index.ts +5 -1
  450. package/src/identity/secp256k1/pem.ts +40 -0
  451. package/src/identity/secp256k1/secp256k1.ts +270 -0
  452. package/src/package.json +4 -0
  453. package/src/principal/index.ts +3 -1
  454. package/src/principal/principal.ts +171 -0
  455. package/src/principal/utils/base32.ts +101 -0
  456. package/src/principal/utils/getCrc.ts +52 -0
@@ -0,0 +1,841 @@
1
+ import * as cbor from './cbor.ts';
2
+ import {
3
+ CertificateHasTooManyDelegationsErrorCode,
4
+ CertificateNotAuthorizedErrorCode,
5
+ CertificateTimeErrorCode,
6
+ CertificateVerificationErrorCode,
7
+ DerKeyLengthMismatchErrorCode,
8
+ DerPrefixMismatchErrorCode,
9
+ ProtocolError,
10
+ LookupErrorCode,
11
+ TrustError,
12
+ UnknownError,
13
+ HashTreeDecodeErrorCode,
14
+ UNREACHABLE_ERROR,
15
+ MalformedLookupFoundValueErrorCode,
16
+ MissingLookupValueErrorCode,
17
+ UnexpectedErrorCode,
18
+ } from './errors.ts';
19
+ import { Principal } from '#principal';
20
+ import * as bls from './utils/bls.ts';
21
+ import { decodeTime } from './utils/leb.ts';
22
+ import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
23
+ import { uint8Equals } from './utils/buffer.ts';
24
+ import { sha256 } from '@noble/hashes/sha2';
25
+ import type { HttpAgent } from './agent/http/index.ts';
26
+ import type { Agent } from './agent/api.ts';
27
+
28
+ const MINUTES_TO_MSEC = 60 * 1000;
29
+ const HOURS_TO_MINUTES = 60;
30
+ const DAYS_TO_HOURS = 24;
31
+ const DAYS_TO_MINUTES = DAYS_TO_HOURS * HOURS_TO_MINUTES;
32
+
33
+ const DEFAULT_CERTIFICATE_MAX_AGE_IN_MINUTES = 5;
34
+ const DEFAULT_CERTIFICATE_MAX_MINUTES_IN_FUTURE = 5;
35
+ // For now, we don't want to set a strict timeout on the certificate delegation freshness,
36
+ // so we set the max age really far in the past.
37
+ const DEFAULT_CERTIFICATE_DELEGATION_MAX_AGE_IN_MINUTES = 30 * DAYS_TO_MINUTES;
38
+
39
+ export interface Cert {
40
+ tree: HashTree;
41
+ signature: Uint8Array;
42
+ delegation?: Delegation;
43
+ }
44
+
45
+ export enum NodeType {
46
+ Empty = 0,
47
+ Fork = 1,
48
+ Labeled = 2,
49
+ Leaf = 3,
50
+ Pruned = 4,
51
+ }
52
+
53
+ export type NodePath = Array<Uint8Array | string>;
54
+ export type NodeLabel = Uint8Array & { __nodeLabel__: void };
55
+ export type NodeValue = Uint8Array & { __nodeValue__: void };
56
+ export type NodeHash = Uint8Array & { __nodeHash__: void };
57
+
58
+ export type EmptyHashTree = [NodeType.Empty];
59
+ export type ForkHashTree = [NodeType.Fork, HashTree, HashTree];
60
+ export type LabeledHashTree = [NodeType.Labeled, NodeLabel, HashTree];
61
+ export type LeafHashTree = [NodeType.Leaf, NodeValue];
62
+ export type PrunedHashTree = [NodeType.Pruned, NodeHash];
63
+
64
+ export type HashTree =
65
+ | EmptyHashTree
66
+ | ForkHashTree
67
+ | LabeledHashTree
68
+ | LeafHashTree
69
+ | PrunedHashTree;
70
+
71
+ /**
72
+ * Make a human readable string out of a hash tree.
73
+ * @param tree The hash tree to convert to a string
74
+ */
75
+ export function hashTreeToString(tree: HashTree): string {
76
+ const indent = (s: string) =>
77
+ s
78
+ .split('\n')
79
+ .map(x => ' ' + x)
80
+ .join('\n');
81
+ function labelToString(label: Uint8Array): string {
82
+ const decoder = new TextDecoder(undefined, { fatal: true });
83
+ try {
84
+ return JSON.stringify(decoder.decode(label));
85
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
86
+ } catch (e) {
87
+ return `data(...${label.byteLength} bytes)`;
88
+ }
89
+ }
90
+
91
+ switch (tree[0]) {
92
+ case NodeType.Empty:
93
+ return '()';
94
+ case NodeType.Fork: {
95
+ if (tree[1] instanceof Array && tree[2] instanceof Uint8Array) {
96
+ const left = hashTreeToString(tree[1]);
97
+ const right = hashTreeToString(tree[2]);
98
+ return `sub(\n left:\n${indent(left)}\n---\n right:\n${indent(right)}\n)`;
99
+ } else {
100
+ throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid tree structure for fork'));
101
+ }
102
+ }
103
+ case NodeType.Labeled: {
104
+ if (tree[1] instanceof Uint8Array && tree[2] instanceof Uint8Array) {
105
+ const label = labelToString(tree[1]);
106
+ const sub = hashTreeToString(tree[2]);
107
+ return `label(\n label:\n${indent(label)}\n sub:\n${indent(sub)}\n)`;
108
+ } else {
109
+ throw UnknownError.fromCode(
110
+ new HashTreeDecodeErrorCode('Invalid tree structure for labeled'),
111
+ );
112
+ }
113
+ }
114
+ case NodeType.Leaf: {
115
+ if (!tree[1]) {
116
+ throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid tree structure for leaf'));
117
+ } else if (Array.isArray(tree[1])) {
118
+ return JSON.stringify(tree[1]);
119
+ }
120
+ return `leaf(...${tree[1].byteLength} bytes)`;
121
+ }
122
+ case NodeType.Pruned: {
123
+ if (!tree[1]) {
124
+ throw UnknownError.fromCode(
125
+ new HashTreeDecodeErrorCode('Invalid tree structure for pruned'),
126
+ );
127
+ } else if (Array.isArray(tree[1])) {
128
+ return JSON.stringify(tree[1]);
129
+ }
130
+
131
+ return `pruned(${bytesToHex(new Uint8Array(tree[1]))}`;
132
+ }
133
+ default: {
134
+ return `unknown(${JSON.stringify(tree[0])})`;
135
+ }
136
+ }
137
+ }
138
+
139
+ interface Delegation extends Record<string, unknown> {
140
+ subnet_id: Uint8Array;
141
+ certificate: Uint8Array;
142
+ }
143
+
144
+ function isBufferGreaterThan(a: Uint8Array, b: Uint8Array): boolean {
145
+ for (let i = 0; i < a.length; i++) {
146
+ if (a[i] > b[i]) {
147
+ return true;
148
+ }
149
+ }
150
+ return false;
151
+ }
152
+
153
+ type VerifyFunc = (pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) => Promise<boolean> | boolean;
154
+
155
+ export interface CreateCertificateOptions {
156
+ /**
157
+ * The bytes encoding the certificate to be verified
158
+ */
159
+ certificate: Uint8Array;
160
+ /**
161
+ * The root key against which to verify the certificate
162
+ * (normally, the root key of the IC main network)
163
+ */
164
+ rootKey: Uint8Array;
165
+ /**
166
+ * The effective canister ID of the request when verifying a response, or
167
+ * the signing canister ID when verifying a certified variable.
168
+ */
169
+ canisterId: Principal;
170
+ /**
171
+ * BLS Verification strategy. Default strategy uses bls12_381 from @noble/curves
172
+ */
173
+ blsVerify?: VerifyFunc;
174
+
175
+ /**
176
+ * The maximum age of the certificate in minutes. Default is 5 minutes.
177
+ * This is used to verify the time the certificate was signed, particularly for validating Delegation certificates, which can live for longer than the default window of +/- 5 minutes. If the certificate is
178
+ * older than the specified age, it will fail verification.
179
+ * @default 5
180
+ */
181
+ maxAgeInMinutes?: number;
182
+
183
+ /**
184
+ * Overrides the maxAgeInMinutes setting and skips comparing the client's time against the certificate. Used for scenarios where the machine's clock is known to be out of sync, or for inspecting expired certificates.
185
+ * @default false
186
+ */
187
+ disableTimeVerification?: boolean;
188
+
189
+ /**
190
+ * The agent used to sync time with the IC network, if the certificate fails the freshness check.
191
+ * If the agent does not implement the {@link HttpAgent.getTimeDiffMsecs}, {@link HttpAgent.hasSyncedTime} and {@link HttpAgent.syncTime} methods,
192
+ * time will not be synced in case of a freshness check failure.
193
+ * @default undefined
194
+ */
195
+ agent?: Agent;
196
+ }
197
+
198
+ export class Certificate {
199
+ public cert: Cert;
200
+ #disableTimeVerification: boolean = false;
201
+ #agent: Pick<HttpAgent, 'getTimeDiffMsecs' | 'hasSyncedTime' | 'syncTime'> | undefined =
202
+ undefined;
203
+
204
+ /**
205
+ * Create a new instance of a certificate, automatically verifying it.
206
+ * @param {CreateCertificateOptions} options {@link CreateCertificateOptions}
207
+ * @throws if the verification of the certificate fails
208
+ */
209
+ public static async create(options: CreateCertificateOptions): Promise<Certificate> {
210
+ const cert = Certificate.createUnverified(options);
211
+
212
+ await cert.verify();
213
+ return cert;
214
+ }
215
+
216
+ private static createUnverified(options: CreateCertificateOptions): Certificate {
217
+ return new Certificate(
218
+ options.certificate,
219
+ options.rootKey,
220
+ options.canisterId,
221
+ options.blsVerify ?? bls.blsVerify,
222
+ options.maxAgeInMinutes,
223
+ options.disableTimeVerification,
224
+ options.agent,
225
+ );
226
+ }
227
+
228
+ private constructor(
229
+ certificate: Uint8Array,
230
+ private _rootKey: Uint8Array,
231
+ private _canisterId: Principal,
232
+ private _blsVerify: VerifyFunc,
233
+ private _maxAgeInMinutes: number = DEFAULT_CERTIFICATE_MAX_AGE_IN_MINUTES,
234
+ disableTimeVerification: boolean = false,
235
+ agent?: Agent,
236
+ ) {
237
+ this.#disableTimeVerification = disableTimeVerification;
238
+ this.cert = cbor.decode(certificate);
239
+
240
+ if (agent && 'getTimeDiffMsecs' in agent && 'hasSyncedTime' in agent && 'syncTime' in agent) {
241
+ this.#agent = agent as Pick<HttpAgent, 'getTimeDiffMsecs' | 'hasSyncedTime' | 'syncTime'>;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Lookup a path in the certificate tree, using {@link lookup_path}.
247
+ * @param path The path to lookup.
248
+ * @returns The result of the lookup.
249
+ */
250
+ public lookup_path(path: NodePath): LookupResult {
251
+ return lookup_path(path, this.cert.tree);
252
+ }
253
+
254
+ /**
255
+ * Lookup a subtree in the certificate tree, using {@link lookup_subtree}.
256
+ * @param path The path to lookup.
257
+ * @returns The result of the lookup.
258
+ */
259
+ public lookup_subtree(path: NodePath): SubtreeLookupResult {
260
+ return lookup_subtree(path, this.cert.tree);
261
+ }
262
+
263
+ private async verify(): Promise<void> {
264
+ const rootHash = await reconstruct(this.cert.tree);
265
+ const derKey = await this._checkDelegationAndGetKey(this.cert.delegation);
266
+ const sig = this.cert.signature;
267
+ const key = extractDER(derKey);
268
+ const msg = concatBytes(domain_sep('ic-state-root'), rootHash);
269
+
270
+ const lookupTime = lookupResultToBuffer(this.lookup_path(['time']));
271
+ if (!lookupTime) {
272
+ // Should never happen - time is always present in IC certificates
273
+ throw ProtocolError.fromCode(
274
+ new CertificateVerificationErrorCode('Certificate does not contain a time'),
275
+ );
276
+ }
277
+
278
+ // Certificate time verification checks
279
+ if (!this.#disableTimeVerification) {
280
+ const timeDiffMsecs = this.#agent?.getTimeDiffMsecs() ?? 0;
281
+ const maxAgeInMsec = this._maxAgeInMinutes * MINUTES_TO_MSEC;
282
+ const now = new Date();
283
+ const adjustedNow = now.getTime() + timeDiffMsecs;
284
+ const earliestCertificateTime = adjustedNow - maxAgeInMsec;
285
+ const latestCertificateTime =
286
+ adjustedNow + DEFAULT_CERTIFICATE_MAX_MINUTES_IN_FUTURE * MINUTES_TO_MSEC;
287
+
288
+ const certTime = decodeTime(lookupTime);
289
+
290
+ const isCertificateTimePast = certTime.getTime() < earliestCertificateTime;
291
+ const isCertificateTimeFuture = certTime.getTime() > latestCertificateTime;
292
+
293
+ if (
294
+ (isCertificateTimePast || isCertificateTimeFuture) &&
295
+ this.#agent &&
296
+ !this.#agent.hasSyncedTime()
297
+ ) {
298
+ await this.#agent.syncTime(this._canisterId);
299
+ return await this.verify();
300
+ }
301
+
302
+ if (isCertificateTimePast) {
303
+ throw TrustError.fromCode(
304
+ new CertificateTimeErrorCode(this._maxAgeInMinutes, certTime, now, timeDiffMsecs, 'past'),
305
+ );
306
+ } else if (isCertificateTimeFuture) {
307
+ if (this.#agent?.hasSyncedTime()) {
308
+ // This case should never happen, and it signals a bug in either the replica or the local system.
309
+ throw UnknownError.fromCode(
310
+ new UnexpectedErrorCode(
311
+ 'System time has been synced with the IC network, but certificate is still too far in the future.',
312
+ ),
313
+ );
314
+ }
315
+ throw TrustError.fromCode(
316
+ new CertificateTimeErrorCode(5, certTime, now, timeDiffMsecs, 'future'),
317
+ );
318
+ }
319
+ }
320
+
321
+ try {
322
+ const sigVer = await this._blsVerify(key, sig, msg);
323
+ if (!sigVer) {
324
+ throw TrustError.fromCode(new CertificateVerificationErrorCode('Invalid signature'));
325
+ }
326
+ } catch (err) {
327
+ throw TrustError.fromCode(
328
+ new CertificateVerificationErrorCode('Signature verification failed', err),
329
+ );
330
+ }
331
+ }
332
+
333
+ private async _checkDelegationAndGetKey(d: Delegation | undefined): Promise<Uint8Array> {
334
+ if (!d) {
335
+ return this._rootKey;
336
+ }
337
+
338
+ const cert = Certificate.createUnverified({
339
+ certificate: d.certificate,
340
+ rootKey: this._rootKey,
341
+ canisterId: this._canisterId,
342
+ blsVerify: this._blsVerify,
343
+ disableTimeVerification: this.#disableTimeVerification,
344
+ maxAgeInMinutes: DEFAULT_CERTIFICATE_DELEGATION_MAX_AGE_IN_MINUTES,
345
+ agent: this.#agent as HttpAgent,
346
+ });
347
+
348
+ if (cert.cert.delegation) {
349
+ throw ProtocolError.fromCode(new CertificateHasTooManyDelegationsErrorCode());
350
+ }
351
+
352
+ await cert.verify();
353
+
354
+ const subnetIdBytes = d.subnet_id;
355
+ const subnetId = Principal.fromUint8Array(subnetIdBytes);
356
+
357
+ const canisterInRange = check_canister_ranges({
358
+ canisterId: this._canisterId,
359
+ subnetId,
360
+ tree: cert.cert.tree,
361
+ });
362
+ if (!canisterInRange) {
363
+ throw TrustError.fromCode(new CertificateNotAuthorizedErrorCode(this._canisterId, subnetId));
364
+ }
365
+
366
+ const publicKeyLookup = lookupResultToBuffer(
367
+ cert.lookup_path(['subnet', subnetIdBytes, 'public_key']),
368
+ );
369
+ if (!publicKeyLookup) {
370
+ throw TrustError.fromCode(
371
+ new MissingLookupValueErrorCode(
372
+ `Could not find subnet key for subnet ID ${subnetId.toText()}`,
373
+ ),
374
+ );
375
+ }
376
+ return publicKeyLookup;
377
+ }
378
+ }
379
+
380
+ const DER_PREFIX = hexToBytes(
381
+ '308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c05030201036100',
382
+ );
383
+ const KEY_LENGTH = 96;
384
+
385
+ function extractDER(buf: Uint8Array): Uint8Array {
386
+ const expectedLength = DER_PREFIX.byteLength + KEY_LENGTH;
387
+ if (buf.byteLength !== expectedLength) {
388
+ throw ProtocolError.fromCode(new DerKeyLengthMismatchErrorCode(expectedLength, buf.byteLength));
389
+ }
390
+ const prefix = buf.slice(0, DER_PREFIX.byteLength);
391
+ if (!uint8Equals(prefix, DER_PREFIX)) {
392
+ throw ProtocolError.fromCode(new DerPrefixMismatchErrorCode(DER_PREFIX, prefix));
393
+ }
394
+
395
+ return buf.slice(DER_PREFIX.byteLength);
396
+ }
397
+
398
+ /**
399
+ * Utility function to constrain the type of a lookup result
400
+ * @param result the result of a lookup
401
+ * @returns {Uint8Array | undefined} the value if the lookup was found, `undefined` otherwise
402
+ */
403
+ export function lookupResultToBuffer(result: LookupResult): Uint8Array | undefined {
404
+ if (result.status !== LookupPathStatus.Found) {
405
+ return undefined;
406
+ }
407
+
408
+ if (result.value instanceof Uint8Array) {
409
+ return result.value;
410
+ }
411
+
412
+ return undefined;
413
+ }
414
+
415
+ /**
416
+ * @param t The hash tree to reconstruct
417
+ */
418
+ export async function reconstruct(t: HashTree): Promise<Uint8Array> {
419
+ switch (t[0]) {
420
+ case NodeType.Empty:
421
+ return sha256(domain_sep('ic-hashtree-empty'));
422
+ case NodeType.Pruned:
423
+ return t[1];
424
+ case NodeType.Leaf:
425
+ return sha256(concatBytes(domain_sep('ic-hashtree-leaf'), t[1]));
426
+ case NodeType.Labeled:
427
+ return sha256(concatBytes(domain_sep('ic-hashtree-labeled'), t[1], await reconstruct(t[2])));
428
+ case NodeType.Fork:
429
+ return sha256(
430
+ concatBytes(
431
+ domain_sep('ic-hashtree-fork'),
432
+ await reconstruct(t[1]),
433
+ await reconstruct(t[2]),
434
+ ),
435
+ );
436
+ default:
437
+ throw UNREACHABLE_ERROR;
438
+ }
439
+ }
440
+
441
+ /**
442
+ * Creates a domain separator for hashing by encoding the input string
443
+ * with its length as a prefix.
444
+ * @param s - The input string to encode.
445
+ * @returns A Uint8Array containing the encoded domain separator.
446
+ */
447
+ export function domain_sep(s: string): Uint8Array {
448
+ const len = new Uint8Array([s.length]);
449
+ const str = new TextEncoder().encode(s);
450
+ return concatBytes(len, str);
451
+ }
452
+
453
+ function pathToLabel(path: NodePath): NodeLabel {
454
+ return (typeof path[0] === 'string' ? utf8ToBytes(path[0]) : path[0]) as NodeLabel;
455
+ }
456
+
457
+ export enum LookupPathStatus {
458
+ Unknown = 'Unknown',
459
+ Absent = 'Absent',
460
+ Found = 'Found',
461
+ Error = 'Error',
462
+ }
463
+
464
+ export interface LookupPathResultAbsent {
465
+ status: LookupPathStatus.Absent;
466
+ }
467
+
468
+ export interface LookupPathResultUnknown {
469
+ status: LookupPathStatus.Unknown;
470
+ }
471
+
472
+ export interface LookupPathResultFound {
473
+ status: LookupPathStatus.Found;
474
+ value: Uint8Array;
475
+ }
476
+
477
+ export interface LookupPathResultError {
478
+ status: LookupPathStatus.Error;
479
+ }
480
+
481
+ export type LookupResult =
482
+ | LookupPathResultAbsent
483
+ | LookupPathResultUnknown
484
+ | LookupPathResultFound
485
+ | LookupPathResultError;
486
+
487
+ export enum LookupSubtreeStatus {
488
+ Absent = 'Absent',
489
+ Unknown = 'Unknown',
490
+ Found = 'Found',
491
+ }
492
+
493
+ export interface LookupSubtreeResultAbsent {
494
+ status: LookupSubtreeStatus.Absent;
495
+ }
496
+
497
+ export interface LookupSubtreeResultUnknown {
498
+ status: LookupSubtreeStatus.Unknown;
499
+ }
500
+
501
+ export interface LookupSubtreeResultFound {
502
+ status: LookupSubtreeStatus.Found;
503
+ value: HashTree;
504
+ }
505
+
506
+ export type SubtreeLookupResult =
507
+ | LookupSubtreeResultAbsent
508
+ | LookupSubtreeResultUnknown
509
+ | LookupSubtreeResultFound;
510
+
511
+ export enum LookupLabelStatus {
512
+ Absent = 'Absent',
513
+ Unknown = 'Unknown',
514
+ Found = 'Found',
515
+ Less = 'Less',
516
+ Greater = 'Greater',
517
+ }
518
+
519
+ export interface LookupLabelResultAbsent {
520
+ status: LookupLabelStatus.Absent;
521
+ }
522
+
523
+ export interface LookupLabelResultUnknown {
524
+ status: LookupLabelStatus.Unknown;
525
+ }
526
+
527
+ export interface LookupLabelResultFound {
528
+ status: LookupLabelStatus.Found;
529
+ value: HashTree;
530
+ }
531
+
532
+ export interface LookupLabelResultGreater {
533
+ status: LookupLabelStatus.Greater;
534
+ }
535
+
536
+ export interface LookupLabelResultLess {
537
+ status: LookupLabelStatus.Less;
538
+ }
539
+
540
+ export type LabelLookupResult =
541
+ | LookupLabelResultAbsent
542
+ | LookupLabelResultUnknown
543
+ | LookupLabelResultFound
544
+ | LookupLabelResultGreater
545
+ | LookupLabelResultLess;
546
+
547
+ /**
548
+ * Lookup a path in a tree. If the path is a subtree, use {@link lookup_subtree} instead.
549
+ * @param path the path to look up
550
+ * @param tree the tree to search
551
+ * @returns {LookupResult} the result of the lookup
552
+ */
553
+ export function lookup_path(path: NodePath, tree: HashTree): LookupResult {
554
+ if (path.length === 0) {
555
+ switch (tree[0]) {
556
+ case NodeType.Empty: {
557
+ return {
558
+ status: LookupPathStatus.Absent,
559
+ };
560
+ }
561
+
562
+ case NodeType.Leaf: {
563
+ if (!tree[1]) {
564
+ throw UnknownError.fromCode(
565
+ new HashTreeDecodeErrorCode('Invalid tree structure for leaf'),
566
+ );
567
+ }
568
+
569
+ if (tree[1] instanceof Uint8Array) {
570
+ return {
571
+ status: LookupPathStatus.Found,
572
+ value: tree[1].slice(tree[1].byteOffset, tree[1].byteLength + tree[1].byteOffset),
573
+ };
574
+ }
575
+
576
+ throw UNREACHABLE_ERROR;
577
+ }
578
+
579
+ case NodeType.Pruned: {
580
+ return {
581
+ status: LookupPathStatus.Unknown,
582
+ };
583
+ }
584
+
585
+ case NodeType.Labeled:
586
+ case NodeType.Fork: {
587
+ return {
588
+ status: LookupPathStatus.Error,
589
+ };
590
+ }
591
+
592
+ default: {
593
+ throw UNREACHABLE_ERROR;
594
+ }
595
+ }
596
+ }
597
+
598
+ const label = pathToLabel(path);
599
+ const lookupResult = find_label(label, tree);
600
+
601
+ switch (lookupResult.status) {
602
+ case LookupLabelStatus.Found: {
603
+ return lookup_path(path.slice(1), lookupResult.value);
604
+ }
605
+
606
+ case LookupLabelStatus.Absent:
607
+ case LookupLabelStatus.Greater:
608
+ case LookupLabelStatus.Less: {
609
+ return {
610
+ status: LookupPathStatus.Absent,
611
+ };
612
+ }
613
+
614
+ case LookupLabelStatus.Unknown: {
615
+ return {
616
+ status: LookupPathStatus.Unknown,
617
+ };
618
+ }
619
+
620
+ default: {
621
+ throw UNREACHABLE_ERROR;
622
+ }
623
+ }
624
+ }
625
+
626
+ /**
627
+ * Lookup a subtree in a tree.
628
+ * @param path the path to look up
629
+ * @param tree the tree to search
630
+ * @returns {SubtreeLookupResult} the result of the lookup
631
+ */
632
+ export function lookup_subtree(path: NodePath, tree: HashTree): SubtreeLookupResult {
633
+ if (path.length === 0) {
634
+ return {
635
+ status: LookupSubtreeStatus.Found,
636
+ value: tree,
637
+ };
638
+ }
639
+
640
+ const label = pathToLabel(path);
641
+ const lookupResult = find_label(label, tree);
642
+
643
+ switch (lookupResult.status) {
644
+ case LookupLabelStatus.Found: {
645
+ return lookup_subtree(path.slice(1), lookupResult.value);
646
+ }
647
+
648
+ case LookupLabelStatus.Unknown: {
649
+ return {
650
+ status: LookupSubtreeStatus.Unknown,
651
+ };
652
+ }
653
+
654
+ case LookupLabelStatus.Absent:
655
+ case LookupLabelStatus.Greater:
656
+ case LookupLabelStatus.Less: {
657
+ return {
658
+ status: LookupSubtreeStatus.Absent,
659
+ };
660
+ }
661
+
662
+ default: {
663
+ throw UNREACHABLE_ERROR;
664
+ }
665
+ }
666
+ }
667
+
668
+ /**
669
+ * If the tree is a fork, flatten it into an array of trees
670
+ * @param {HashTree} t the tree to flatten
671
+ * @returns {HashTree[]} the flattened tree
672
+ */
673
+ export function flatten_forks(t: HashTree): Array<LabeledHashTree | LeafHashTree | PrunedHashTree> {
674
+ switch (t[0]) {
675
+ case NodeType.Empty:
676
+ return [];
677
+ case NodeType.Fork:
678
+ return flatten_forks(t[1]).concat(flatten_forks(t[2]));
679
+ default:
680
+ return [t];
681
+ }
682
+ }
683
+
684
+ /**
685
+ * Find a label in a tree
686
+ * @param label the label to find
687
+ * @param tree the tree to search
688
+ * @returns {LabelLookupResult} the result of the label lookup
689
+ */
690
+ export function find_label(label: NodeLabel, tree: HashTree): LabelLookupResult {
691
+ switch (tree[0]) {
692
+ // if we have a labelled node, compare the node's label to the one we are
693
+ // looking for
694
+ case NodeType.Labeled:
695
+ // if the label we're searching for is greater than this node's label,
696
+ // we need to keep searching
697
+ if (isBufferGreaterThan(label, tree[1])) {
698
+ return {
699
+ status: LookupLabelStatus.Greater,
700
+ };
701
+ }
702
+
703
+ // if the label we're searching for is equal this node's label, we can
704
+ // stop searching and return the found node
705
+ if (uint8Equals(label, tree[1])) {
706
+ return {
707
+ status: LookupLabelStatus.Found,
708
+ value: tree[2],
709
+ };
710
+ }
711
+
712
+ // if the label we're searching for is not greater than or equal to this
713
+ // node's label, then it's less than this node's label, and we can stop
714
+ // searching because we've looked too far
715
+ return {
716
+ status: LookupLabelStatus.Less,
717
+ };
718
+
719
+ // if we have a fork node, we need to search both sides, starting with the left
720
+ case NodeType.Fork: {
721
+ // search in the left node
722
+ const leftLookupResult = find_label(label, tree[1]);
723
+
724
+ switch (leftLookupResult.status) {
725
+ // if the label we're searching for is greater than the left node lookup,
726
+ // we need to search the right node
727
+ case LookupLabelStatus.Greater: {
728
+ const rightLookupResult = find_label(label, tree[2]);
729
+
730
+ // if the label we're searching for is less than the right node lookup,
731
+ // then we can stop searching and say that the label is provably Absent
732
+ if (rightLookupResult.status === LookupLabelStatus.Less) {
733
+ return {
734
+ status: LookupLabelStatus.Absent,
735
+ };
736
+ }
737
+
738
+ // if the label we're searching for is less than or equal to the right
739
+ // node lookup, then we let the caller handle it
740
+ return rightLookupResult;
741
+ }
742
+
743
+ // if the left node returns an uncertain result, we need to search the
744
+ // right node
745
+ case LookupLabelStatus.Unknown: {
746
+ const rightLookupResult = find_label(label, tree[2]);
747
+
748
+ // if the label we're searching for is less than the right node lookup,
749
+ // then we also need to return an uncertain result
750
+ if (rightLookupResult.status === LookupLabelStatus.Less) {
751
+ return {
752
+ status: LookupLabelStatus.Unknown,
753
+ };
754
+ }
755
+
756
+ // if the label we're searching for is less than or equal to the right
757
+ // node lookup, then we let the caller handle it
758
+ return rightLookupResult;
759
+ }
760
+
761
+ // if the label we're searching for is not greater than the left node
762
+ // lookup, or the result is not uncertain, we stop searching and return
763
+ // whatever the result of the left node lookup was, which can be either
764
+ // Found or Absent
765
+ default: {
766
+ return leftLookupResult;
767
+ }
768
+ }
769
+ }
770
+
771
+ // if we encounter a Pruned node, we can't know for certain if the label
772
+ // we're searching for is present or not
773
+ case NodeType.Pruned:
774
+ return {
775
+ status: LookupLabelStatus.Unknown,
776
+ };
777
+
778
+ // if the current node is Empty, or a Leaf, we can stop searching because
779
+ // we know for sure that the label we're searching for is not present
780
+ default:
781
+ return {
782
+ status: LookupLabelStatus.Absent,
783
+ };
784
+ }
785
+ }
786
+
787
+ type CanisterRanges = Array<[Principal, Principal]>;
788
+
789
+ /**
790
+ * Check if a canister ID falls within the canister ranges of a given subnet
791
+ * @param params the parameters with which to check the canister ranges
792
+ * @param params.canisterId the canister ID to check
793
+ * @param params.subnetId the subnet ID from which to check the canister ranges
794
+ * @param params.tree the hash tree in which to lookup the subnet's canister ranges
795
+ * @returns {boolean} `true` if the canister is in the range, `false` otherwise
796
+ */
797
+ export function check_canister_ranges(params: {
798
+ canisterId: Principal;
799
+ subnetId: Principal;
800
+ tree: HashTree;
801
+ }): boolean {
802
+ const { canisterId, subnetId, tree } = params;
803
+
804
+ const rangesLookupValue = lookupCanisterRanges(subnetId, tree);
805
+ const ranges = decodeCanisterRanges(rangesLookupValue);
806
+
807
+ const canisterInRange = ranges.some(r => r[0].ltEq(canisterId) && r[1].gtEq(canisterId));
808
+ return canisterInRange;
809
+ }
810
+
811
+ function lookupCanisterRanges(subnetId: Principal, tree: HashTree): Uint8Array {
812
+ const rangeLookup = lookup_path(['subnet', subnetId.toUint8Array(), 'canister_ranges'], tree);
813
+
814
+ if (rangeLookup.status !== LookupPathStatus.Found) {
815
+ throw ProtocolError.fromCode(
816
+ new LookupErrorCode(
817
+ `Could not find canister ranges for subnet ${subnetId.toText()}`,
818
+ rangeLookup.status,
819
+ ),
820
+ );
821
+ }
822
+
823
+ if (!(rangeLookup.value instanceof Uint8Array)) {
824
+ throw ProtocolError.fromCode(
825
+ new MalformedLookupFoundValueErrorCode(
826
+ `Could not find canister ranges for subnet ${subnetId.toText()}`,
827
+ ),
828
+ );
829
+ }
830
+
831
+ return rangeLookup.value;
832
+ }
833
+
834
+ function decodeCanisterRanges(lookupValue: Uint8Array): CanisterRanges {
835
+ const ranges_arr = cbor.decode<Array<[Uint8Array, Uint8Array]>>(lookupValue);
836
+ const ranges: CanisterRanges = ranges_arr.map(v => [
837
+ Principal.fromUint8Array(v[0]),
838
+ Principal.fromUint8Array(v[1]),
839
+ ]);
840
+ return ranges;
841
+ }