@icp-sdk/core 4.2.3 → 5.0.0-beta.1

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 (474) 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 +37 -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 +51 -0
  12. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -0
  13. package/lib/cjs/agent/agent/http/index.d.ts +190 -0
  14. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -0
  15. package/lib/cjs/agent/agent/http/index.js +1159 -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 +61 -0
  34. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -0
  35. package/lib/cjs/agent/canisterStatus/index.js +239 -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 +286 -0
  42. package/lib/cjs/agent/certificate.d.ts.map +1 -0
  43. package/lib/cjs/agent/certificate.js +762 -0
  44. package/lib/cjs/agent/certificate.js.map +1 -0
  45. package/lib/cjs/agent/constants.d.ts +18 -0
  46. package/lib/cjs/agent/constants.d.ts.map +1 -0
  47. package/lib/cjs/agent/constants.js +22 -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 +386 -0
  54. package/lib/cjs/agent/errors.d.ts.map +1 -0
  55. package/lib/cjs/agent/errors.js +745 -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 +125 -1
  62. package/lib/cjs/agent/index.d.ts.map +1 -1
  63. package/lib/cjs/agent/index.js +142 -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/subnetStatus/index.d.ts +70 -0
  90. package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -0
  91. package/lib/cjs/agent/subnetStatus/index.js +224 -0
  92. package/lib/cjs/agent/subnetStatus/index.js.map +1 -0
  93. package/lib/cjs/agent/utils/bls.d.ts +10 -0
  94. package/lib/cjs/agent/utils/bls.d.ts.map +1 -0
  95. package/lib/cjs/agent/utils/bls.js +20 -0
  96. package/lib/cjs/agent/utils/bls.js.map +1 -0
  97. package/lib/cjs/agent/utils/buffer.d.ts +22 -0
  98. package/lib/cjs/agent/utils/buffer.d.ts.map +1 -0
  99. package/lib/cjs/agent/utils/buffer.js +55 -0
  100. package/lib/cjs/agent/utils/buffer.js.map +1 -0
  101. package/lib/cjs/agent/utils/expirableMap.d.ts +80 -0
  102. package/lib/cjs/agent/utils/expirableMap.d.ts.map +1 -0
  103. package/lib/cjs/agent/utils/expirableMap.js +148 -0
  104. package/lib/cjs/agent/utils/expirableMap.js.map +1 -0
  105. package/lib/cjs/agent/utils/leb.d.ts +3 -0
  106. package/lib/cjs/agent/utils/leb.d.ts.map +1 -0
  107. package/lib/cjs/agent/utils/leb.js +17 -0
  108. package/lib/cjs/agent/utils/leb.js.map +1 -0
  109. package/lib/cjs/agent/utils/random.d.ts +6 -0
  110. package/lib/cjs/agent/utils/random.d.ts.map +1 -0
  111. package/lib/cjs/agent/utils/random.js +29 -0
  112. package/lib/cjs/agent/utils/random.js.map +1 -0
  113. package/lib/cjs/agent/utils/readState.d.ts +84 -0
  114. package/lib/cjs/agent/utils/readState.d.ts.map +1 -0
  115. package/lib/cjs/agent/utils/readState.js +149 -0
  116. package/lib/cjs/agent/utils/readState.js.map +1 -0
  117. package/lib/cjs/candid/candid-core.d.ts +72 -0
  118. package/lib/cjs/candid/candid-core.d.ts.map +1 -0
  119. package/lib/cjs/candid/candid-core.js +251 -0
  120. package/lib/cjs/candid/candid-core.js.map +1 -0
  121. package/lib/cjs/candid/candid-ui.d.ts +35 -0
  122. package/lib/cjs/candid/candid-ui.d.ts.map +1 -0
  123. package/lib/cjs/candid/candid-ui.js +317 -0
  124. package/lib/cjs/candid/candid-ui.js.map +1 -0
  125. package/lib/cjs/candid/idl.d.ts +579 -0
  126. package/lib/cjs/candid/idl.d.ts.map +1 -0
  127. package/lib/cjs/candid/idl.js +2228 -0
  128. package/lib/cjs/candid/idl.js.map +1 -0
  129. package/lib/cjs/candid/index.d.ts +30 -1
  130. package/lib/cjs/candid/index.d.ts.map +1 -1
  131. package/lib/cjs/candid/index.js +52 -1
  132. package/lib/cjs/candid/index.js.map +1 -1
  133. package/lib/cjs/candid/types.d.ts +6 -0
  134. package/lib/cjs/candid/types.d.ts.map +1 -0
  135. package/lib/cjs/candid/types.js +4 -0
  136. package/lib/cjs/candid/types.js.map +1 -0
  137. package/lib/cjs/candid/utils/bigint-math.d.ts +14 -0
  138. package/lib/cjs/candid/utils/bigint-math.d.ts.map +1 -0
  139. package/lib/cjs/candid/utils/bigint-math.js +30 -0
  140. package/lib/cjs/candid/utils/bigint-math.js.map +1 -0
  141. package/lib/cjs/candid/utils/buffer.d.ts +86 -0
  142. package/lib/cjs/candid/utils/buffer.d.ts.map +1 -0
  143. package/lib/cjs/candid/utils/buffer.js +191 -0
  144. package/lib/cjs/candid/utils/buffer.js.map +1 -0
  145. package/lib/cjs/candid/utils/hash.d.ts +7 -0
  146. package/lib/cjs/candid/utils/hash.d.ts.map +1 -0
  147. package/lib/cjs/candid/utils/hash.js +33 -0
  148. package/lib/cjs/candid/utils/hash.js.map +1 -0
  149. package/lib/cjs/candid/utils/leb128.d.ts +65 -0
  150. package/lib/cjs/candid/utils/leb128.d.ts.map +1 -0
  151. package/lib/cjs/candid/utils/leb128.js +228 -0
  152. package/lib/cjs/candid/utils/leb128.js.map +1 -0
  153. package/lib/cjs/identity/identity/delegation.d.ts +164 -0
  154. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -0
  155. package/lib/cjs/identity/identity/delegation.js +302 -0
  156. package/lib/cjs/identity/identity/delegation.js.map +1 -0
  157. package/lib/cjs/identity/identity/ecdsa.d.ts +62 -0
  158. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -0
  159. package/lib/cjs/identity/identity/ecdsa.js +117 -0
  160. package/lib/cjs/identity/identity/ecdsa.js.map +1 -0
  161. package/lib/cjs/identity/identity/ed25519.d.ts +67 -0
  162. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -0
  163. package/lib/cjs/identity/identity/ed25519.js +200 -0
  164. package/lib/cjs/identity/identity/ed25519.js.map +1 -0
  165. package/lib/cjs/identity/identity/partial.d.ts +34 -0
  166. package/lib/cjs/identity/identity/partial.d.ts.map +1 -0
  167. package/lib/cjs/identity/identity/partial.js +54 -0
  168. package/lib/cjs/identity/identity/partial.js.map +1 -0
  169. package/lib/cjs/identity/identity/webauthn.d.ts +51 -0
  170. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -0
  171. package/lib/cjs/identity/identity/webauthn.js +197 -0
  172. package/lib/cjs/identity/identity/webauthn.js.map +1 -0
  173. package/lib/cjs/identity/index.d.ts +38 -1
  174. package/lib/cjs/identity/index.d.ts.map +1 -1
  175. package/lib/cjs/identity/index.js +44 -1
  176. package/lib/cjs/identity/index.js.map +1 -1
  177. package/lib/cjs/identity/secp256k1/der.d.ts +33 -0
  178. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -0
  179. package/lib/cjs/identity/secp256k1/der.js +165 -0
  180. package/lib/cjs/identity/secp256k1/der.js.map +1 -0
  181. package/lib/cjs/identity/secp256k1/index.d.ts +51 -1
  182. package/lib/cjs/identity/secp256k1/index.d.ts.map +1 -1
  183. package/lib/cjs/identity/secp256k1/index.js +51 -1
  184. package/lib/cjs/identity/secp256k1/index.js.map +1 -1
  185. package/lib/cjs/identity/secp256k1/pem.d.ts +7 -0
  186. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -0
  187. package/lib/cjs/identity/secp256k1/pem.js +75 -0
  188. package/lib/cjs/identity/secp256k1/pem.js.map +1 -0
  189. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +87 -0
  190. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -0
  191. package/lib/cjs/identity/secp256k1/secp256k1.js +260 -0
  192. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -0
  193. package/lib/cjs/principal/index.d.ts +18 -1
  194. package/lib/cjs/principal/index.d.ts.map +1 -1
  195. package/lib/cjs/principal/index.js +22 -1
  196. package/lib/cjs/principal/index.js.map +1 -1
  197. package/lib/cjs/principal/principal.d.ts +50 -0
  198. package/lib/cjs/principal/principal.d.ts.map +1 -0
  199. package/lib/cjs/principal/principal.js +148 -0
  200. package/lib/cjs/principal/principal.js.map +1 -0
  201. package/lib/cjs/principal/utils/base32.d.ts +10 -0
  202. package/lib/cjs/principal/utils/base32.d.ts.map +1 -0
  203. package/lib/cjs/principal/utils/base32.js +90 -0
  204. package/lib/cjs/principal/utils/base32.js.map +1 -0
  205. package/lib/cjs/principal/utils/getCrc.d.ts +6 -0
  206. package/lib/cjs/principal/utils/getCrc.d.ts.map +1 -0
  207. package/lib/cjs/principal/utils/getCrc.js +53 -0
  208. package/lib/cjs/principal/utils/getCrc.js.map +1 -0
  209. package/lib/esm/agent/actor.d.ts +203 -0
  210. package/lib/esm/agent/actor.d.ts.map +1 -0
  211. package/lib/esm/agent/actor.js +337 -0
  212. package/lib/esm/agent/actor.js.map +1 -0
  213. package/lib/esm/agent/agent/api.d.ts +216 -0
  214. package/lib/esm/agent/agent/api.d.ts.map +1 -0
  215. package/lib/esm/agent/agent/api.js +34 -0
  216. package/lib/esm/agent/agent/api.js.map +1 -0
  217. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts +37 -0
  218. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -0
  219. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js +48 -0
  220. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -0
  221. package/lib/esm/agent/agent/http/index.d.ts +190 -0
  222. package/lib/esm/agent/agent/http/index.d.ts.map +1 -0
  223. package/lib/esm/agent/agent/http/index.js +1117 -0
  224. package/lib/esm/agent/agent/http/index.js.map +1 -0
  225. package/lib/esm/agent/agent/http/transforms.d.ts +54 -0
  226. package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -0
  227. package/lib/esm/agent/agent/http/transforms.js +128 -0
  228. package/lib/esm/agent/agent/http/transforms.js.map +1 -0
  229. package/lib/esm/agent/agent/http/types.d.ts +81 -0
  230. package/lib/esm/agent/agent/http/types.d.ts.map +1 -0
  231. package/lib/esm/agent/agent/http/types.js +39 -0
  232. package/lib/esm/agent/agent/http/types.js.map +1 -0
  233. package/lib/esm/agent/agent/index.d.ts +3 -0
  234. package/lib/esm/agent/agent/index.d.ts.map +1 -0
  235. package/lib/esm/agent/agent/index.js +3 -0
  236. package/lib/esm/agent/agent/index.js.map +1 -0
  237. package/lib/esm/agent/auth.d.ts +91 -0
  238. package/lib/esm/agent/auth.d.ts.map +1 -0
  239. package/lib/esm/agent/auth.js +59 -0
  240. package/lib/esm/agent/auth.js.map +1 -0
  241. package/lib/esm/agent/canister-env/index.d.ts +111 -1
  242. package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
  243. package/lib/esm/agent/canister-env/index.js +111 -1
  244. package/lib/esm/agent/canister-env/index.js.map +1 -1
  245. package/lib/esm/agent/canisterStatus/index.d.ts +61 -0
  246. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -0
  247. package/lib/esm/agent/canisterStatus/index.js +199 -0
  248. package/lib/esm/agent/canisterStatus/index.js.map +1 -0
  249. package/lib/esm/agent/cbor.d.ts +26 -0
  250. package/lib/esm/agent/cbor.d.ts.map +1 -0
  251. package/lib/esm/agent/cbor.js +54 -0
  252. package/lib/esm/agent/cbor.js.map +1 -0
  253. package/lib/esm/agent/certificate.d.ts +286 -0
  254. package/lib/esm/agent/certificate.d.ts.map +1 -0
  255. package/lib/esm/agent/certificate.js +712 -0
  256. package/lib/esm/agent/certificate.js.map +1 -0
  257. package/lib/esm/agent/constants.d.ts +18 -0
  258. package/lib/esm/agent/constants.d.ts.map +1 -0
  259. package/lib/esm/agent/constants.js +19 -0
  260. package/lib/esm/agent/constants.js.map +1 -0
  261. package/lib/esm/agent/der.d.ts +34 -0
  262. package/lib/esm/agent/der.d.ts.map +1 -0
  263. package/lib/esm/agent/der.js +166 -0
  264. package/lib/esm/agent/der.js.map +1 -0
  265. package/lib/esm/agent/errors.d.ts +386 -0
  266. package/lib/esm/agent/errors.d.ts.map +1 -0
  267. package/lib/esm/agent/errors.js +689 -0
  268. package/lib/esm/agent/errors.js.map +1 -0
  269. package/lib/esm/agent/fetch_candid.d.ts +9 -0
  270. package/lib/esm/agent/fetch_candid.d.ts.map +1 -0
  271. package/lib/esm/agent/fetch_candid.js +33 -0
  272. package/lib/esm/agent/fetch_candid.js.map +1 -0
  273. package/lib/esm/agent/index.d.ts +125 -1
  274. package/lib/esm/agent/index.d.ts.map +1 -1
  275. package/lib/esm/agent/index.js +125 -1
  276. package/lib/esm/agent/index.js.map +1 -1
  277. package/lib/esm/agent/observable.d.ts +24 -0
  278. package/lib/esm/agent/observable.d.ts.map +1 -0
  279. package/lib/esm/agent/observable.js +29 -0
  280. package/lib/esm/agent/observable.js.map +1 -0
  281. package/lib/esm/agent/polling/backoff.d.ts +50 -0
  282. package/lib/esm/agent/polling/backoff.d.ts.map +1 -0
  283. package/lib/esm/agent/polling/backoff.js +85 -0
  284. package/lib/esm/agent/polling/backoff.js.map +1 -0
  285. package/lib/esm/agent/polling/index.d.ts +63 -0
  286. package/lib/esm/agent/polling/index.d.ts.map +1 -0
  287. package/lib/esm/agent/polling/index.js +144 -0
  288. package/lib/esm/agent/polling/index.js.map +1 -0
  289. package/lib/esm/agent/polling/strategy.d.ts +52 -0
  290. package/lib/esm/agent/polling/strategy.d.ts.map +1 -0
  291. package/lib/esm/agent/polling/strategy.js +94 -0
  292. package/lib/esm/agent/polling/strategy.js.map +1 -0
  293. package/lib/esm/agent/public_key.d.ts +16 -0
  294. package/lib/esm/agent/public_key.d.ts.map +1 -0
  295. package/lib/esm/agent/public_key.js +48 -0
  296. package/lib/esm/agent/public_key.js.map +1 -0
  297. package/lib/esm/agent/request_id.d.ts +24 -0
  298. package/lib/esm/agent/request_id.d.ts.map +1 -0
  299. package/lib/esm/agent/request_id.js +83 -0
  300. package/lib/esm/agent/request_id.js.map +1 -0
  301. package/lib/esm/agent/subnetStatus/index.d.ts +70 -0
  302. package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -0
  303. package/lib/esm/agent/subnetStatus/index.js +183 -0
  304. package/lib/esm/agent/subnetStatus/index.js.map +1 -0
  305. package/lib/esm/agent/utils/bls.d.ts +10 -0
  306. package/lib/esm/agent/utils/bls.d.ts.map +1 -0
  307. package/lib/esm/agent/utils/bls.js +17 -0
  308. package/lib/esm/agent/utils/bls.js.map +1 -0
  309. package/lib/esm/agent/utils/buffer.d.ts +22 -0
  310. package/lib/esm/agent/utils/buffer.d.ts.map +1 -0
  311. package/lib/esm/agent/utils/buffer.js +50 -0
  312. package/lib/esm/agent/utils/buffer.js.map +1 -0
  313. package/lib/esm/agent/utils/expirableMap.d.ts +80 -0
  314. package/lib/esm/agent/utils/expirableMap.d.ts.map +1 -0
  315. package/lib/esm/agent/utils/expirableMap.js +144 -0
  316. package/lib/esm/agent/utils/expirableMap.js.map +1 -0
  317. package/lib/esm/agent/utils/leb.d.ts +3 -0
  318. package/lib/esm/agent/utils/leb.d.ts.map +1 -0
  319. package/lib/esm/agent/utils/leb.js +12 -0
  320. package/lib/esm/agent/utils/leb.js.map +1 -0
  321. package/lib/esm/agent/utils/random.d.ts +6 -0
  322. package/lib/esm/agent/utils/random.d.ts.map +1 -0
  323. package/lib/esm/agent/utils/random.js +25 -0
  324. package/lib/esm/agent/utils/random.js.map +1 -0
  325. package/lib/esm/agent/utils/readState.d.ts +84 -0
  326. package/lib/esm/agent/utils/readState.d.ts.map +1 -0
  327. package/lib/esm/agent/utils/readState.js +107 -0
  328. package/lib/esm/agent/utils/readState.js.map +1 -0
  329. package/lib/esm/candid/candid-core.d.ts +72 -0
  330. package/lib/esm/candid/candid-core.d.ts.map +1 -0
  331. package/lib/esm/candid/candid-core.js +241 -0
  332. package/lib/esm/candid/candid-core.js.map +1 -0
  333. package/lib/esm/candid/candid-ui.d.ts +35 -0
  334. package/lib/esm/candid/candid-ui.d.ts.map +1 -0
  335. package/lib/esm/candid/candid-ui.js +272 -0
  336. package/lib/esm/candid/candid-ui.js.map +1 -0
  337. package/lib/esm/candid/idl.d.ts +579 -0
  338. package/lib/esm/candid/idl.d.ts.map +1 -0
  339. package/lib/esm/candid/idl.js +2189 -0
  340. package/lib/esm/candid/idl.js.map +1 -0
  341. package/lib/esm/candid/index.d.ts +30 -1
  342. package/lib/esm/candid/index.d.ts.map +1 -1
  343. package/lib/esm/candid/index.js +29 -1
  344. package/lib/esm/candid/index.js.map +1 -1
  345. package/lib/esm/candid/types.d.ts +6 -0
  346. package/lib/esm/candid/types.d.ts.map +1 -0
  347. package/lib/esm/candid/types.js +3 -0
  348. package/lib/esm/candid/types.js.map +1 -0
  349. package/lib/esm/candid/utils/bigint-math.d.ts +14 -0
  350. package/lib/esm/candid/utils/bigint-math.d.ts.map +1 -0
  351. package/lib/esm/candid/utils/bigint-math.js +26 -0
  352. package/lib/esm/candid/utils/bigint-math.js.map +1 -0
  353. package/lib/esm/candid/utils/buffer.d.ts +86 -0
  354. package/lib/esm/candid/utils/buffer.d.ts.map +1 -0
  355. package/lib/esm/candid/utils/buffer.js +182 -0
  356. package/lib/esm/candid/utils/buffer.js.map +1 -0
  357. package/lib/esm/candid/utils/hash.d.ts +7 -0
  358. package/lib/esm/candid/utils/hash.d.ts.map +1 -0
  359. package/lib/esm/candid/utils/hash.js +30 -0
  360. package/lib/esm/candid/utils/hash.js.map +1 -0
  361. package/lib/esm/candid/utils/leb128.d.ts +65 -0
  362. package/lib/esm/candid/utils/leb128.d.ts.map +1 -0
  363. package/lib/esm/candid/utils/leb128.js +216 -0
  364. package/lib/esm/candid/utils/leb128.js.map +1 -0
  365. package/lib/esm/identity/identity/delegation.d.ts +164 -0
  366. package/lib/esm/identity/identity/delegation.d.ts.map +1 -0
  367. package/lib/esm/identity/identity/delegation.js +294 -0
  368. package/lib/esm/identity/identity/delegation.js.map +1 -0
  369. package/lib/esm/identity/identity/ecdsa.d.ts +62 -0
  370. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -0
  371. package/lib/esm/identity/identity/ecdsa.js +112 -0
  372. package/lib/esm/identity/identity/ecdsa.js.map +1 -0
  373. package/lib/esm/identity/identity/ed25519.d.ts +67 -0
  374. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -0
  375. package/lib/esm/identity/identity/ed25519.js +195 -0
  376. package/lib/esm/identity/identity/ed25519.js.map +1 -0
  377. package/lib/esm/identity/identity/partial.d.ts +34 -0
  378. package/lib/esm/identity/identity/partial.d.ts.map +1 -0
  379. package/lib/esm/identity/identity/partial.js +50 -0
  380. package/lib/esm/identity/identity/partial.js.map +1 -0
  381. package/lib/esm/identity/identity/webauthn.d.ts +51 -0
  382. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -0
  383. package/lib/esm/identity/identity/webauthn.js +192 -0
  384. package/lib/esm/identity/identity/webauthn.js.map +1 -0
  385. package/lib/esm/identity/index.d.ts +38 -1
  386. package/lib/esm/identity/index.d.ts.map +1 -1
  387. package/lib/esm/identity/index.js +38 -1
  388. package/lib/esm/identity/index.js.map +1 -1
  389. package/lib/esm/identity/secp256k1/der.d.ts +33 -0
  390. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -0
  391. package/lib/esm/identity/secp256k1/der.js +156 -0
  392. package/lib/esm/identity/secp256k1/der.js.map +1 -0
  393. package/lib/esm/identity/secp256k1/index.d.ts +51 -1
  394. package/lib/esm/identity/secp256k1/index.d.ts.map +1 -1
  395. package/lib/esm/identity/secp256k1/index.js +51 -1
  396. package/lib/esm/identity/secp256k1/index.js.map +1 -1
  397. package/lib/esm/identity/secp256k1/pem.d.ts +7 -0
  398. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -0
  399. package/lib/esm/identity/secp256k1/pem.js +39 -0
  400. package/lib/esm/identity/secp256k1/pem.js.map +1 -0
  401. package/lib/esm/identity/secp256k1/secp256k1.d.ts +87 -0
  402. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -0
  403. package/lib/esm/identity/secp256k1/secp256k1.js +222 -0
  404. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -0
  405. package/lib/esm/principal/index.d.ts +18 -1
  406. package/lib/esm/principal/index.d.ts.map +1 -1
  407. package/lib/esm/principal/index.js +18 -1
  408. package/lib/esm/principal/index.js.map +1 -1
  409. package/lib/esm/principal/principal.d.ts +50 -0
  410. package/lib/esm/principal/principal.d.ts.map +1 -0
  411. package/lib/esm/principal/principal.js +144 -0
  412. package/lib/esm/principal/principal.js.map +1 -0
  413. package/lib/esm/principal/utils/base32.d.ts +10 -0
  414. package/lib/esm/principal/utils/base32.d.ts.map +1 -0
  415. package/lib/esm/principal/utils/base32.js +86 -0
  416. package/lib/esm/principal/utils/base32.js.map +1 -0
  417. package/lib/esm/principal/utils/getCrc.d.ts +6 -0
  418. package/lib/esm/principal/utils/getCrc.d.ts.map +1 -0
  419. package/lib/esm/principal/utils/getCrc.js +50 -0
  420. package/lib/esm/principal/utils/getCrc.js.map +1 -0
  421. package/package.json +17 -11
  422. package/src/agent/actor.ts +576 -0
  423. package/src/agent/agent/api.ts +262 -0
  424. package/src/agent/agent/http/index.ts +1518 -0
  425. package/src/agent/agent/http/transforms.ts +158 -0
  426. package/src/agent/agent/http/types.ts +126 -0
  427. package/src/agent/agent/index.ts +2 -0
  428. package/src/agent/auth.ts +147 -0
  429. package/src/agent/canister-env/index.ts +192 -1
  430. package/src/agent/canisterStatus/index.ts +267 -0
  431. package/src/agent/cbor.ts +63 -0
  432. package/src/agent/certificate.ts +1060 -0
  433. package/src/agent/constants.ts +24 -0
  434. package/src/agent/der.ts +170 -0
  435. package/src/agent/errors.ts +881 -0
  436. package/src/agent/fetch_candid.ts +37 -0
  437. package/src/agent/index.ts +131 -1
  438. package/src/agent/observable.ts +49 -0
  439. package/src/agent/polling/backoff.ts +130 -0
  440. package/src/agent/polling/index.ts +250 -0
  441. package/src/agent/polling/strategy.ts +144 -0
  442. package/src/agent/public_key.ts +65 -0
  443. package/src/agent/request_id.ts +92 -0
  444. package/src/agent/subnetStatus/index.ts +270 -0
  445. package/src/agent/utils/bls.ts +18 -0
  446. package/src/agent/utils/buffer.ts +60 -0
  447. package/src/agent/utils/expirableMap.ts +163 -0
  448. package/src/agent/utils/leb.ts +14 -0
  449. package/src/agent/utils/random.ts +30 -0
  450. package/src/agent/utils/readState.ts +180 -0
  451. package/src/candid/candid-core.ts +276 -0
  452. package/src/candid/candid-ui.ts +289 -0
  453. package/src/candid/idl.ts +2556 -0
  454. package/src/candid/index.ts +35 -1
  455. package/src/candid/types.ts +7 -0
  456. package/src/candid/utils/bigint-math.ts +26 -0
  457. package/src/candid/utils/buffer.ts +221 -0
  458. package/src/candid/utils/hash.ts +31 -0
  459. package/src/candid/utils/leb128.ts +234 -0
  460. package/src/identity/identity/delegation.ts +411 -0
  461. package/src/identity/identity/ecdsa.ts +158 -0
  462. package/src/identity/identity/ed25519.ts +239 -0
  463. package/src/identity/identity/partial.ts +60 -0
  464. package/src/identity/identity/webauthn.ts +265 -0
  465. package/src/identity/index.ts +39 -1
  466. package/src/identity/secp256k1/der.ts +156 -0
  467. package/src/identity/secp256k1/index.ts +52 -1
  468. package/src/identity/secp256k1/pem.ts +40 -0
  469. package/src/identity/secp256k1/secp256k1.ts +270 -0
  470. package/src/package.json +4 -0
  471. package/src/principal/index.ts +19 -1
  472. package/src/principal/principal.ts +171 -0
  473. package/src/principal/utils/base32.ts +101 -0
  474. package/src/principal/utils/getCrc.ts +52 -0
@@ -0,0 +1,163 @@
1
+ export type ExpirableMapOptions<K, V> = {
2
+ source?: Iterable<[K, V]>;
3
+ expirationTime?: number;
4
+ };
5
+
6
+ /**
7
+ * A map that expires entries after a given time.
8
+ * Defaults to 10 minutes.
9
+ */
10
+ export class ExpirableMap<K, V> implements Map<K, V> {
11
+ // Internals
12
+ #inner: Map<K, { value: V; timestamp: number }>;
13
+ #expirationTime: number;
14
+
15
+ [Symbol.iterator]: () => MapIterator<[K, V]> = this.entries.bind(this);
16
+ [Symbol.toStringTag] = 'ExpirableMap';
17
+
18
+ /**
19
+ * Create a new ExpirableMap.
20
+ * @param {ExpirableMapOptions<any, any>} options - options for the map.
21
+ * @param {Iterable<[any, any]>} options.source - an optional source of entries to initialize the map with.
22
+ * @param {number} options.expirationTime - the time in milliseconds after which entries will expire.
23
+ */
24
+ constructor(options: ExpirableMapOptions<K, V> = {}) {
25
+ const { source = [], expirationTime = 10 * 60 * 1000 } = options;
26
+ const currentTime = Date.now();
27
+ this.#inner = new Map(
28
+ [...source].map(([key, value]) => [key, { value, timestamp: currentTime }]),
29
+ );
30
+ this.#expirationTime = expirationTime;
31
+ }
32
+
33
+ /**
34
+ * Prune removes all expired entries.
35
+ */
36
+ prune() {
37
+ const currentTime = Date.now();
38
+ for (const [key, entry] of this.#inner.entries()) {
39
+ if (currentTime - entry.timestamp > this.#expirationTime) {
40
+ this.#inner.delete(key);
41
+ }
42
+ }
43
+ return this;
44
+ }
45
+
46
+ // Implementing the Map interface
47
+
48
+ /**
49
+ * Set the value for the given key. Prunes expired entries.
50
+ * @param key for the entry
51
+ * @param value of the entry
52
+ * @returns this
53
+ */
54
+ set(key: K, value: V) {
55
+ this.prune();
56
+ const entry = {
57
+ value,
58
+ timestamp: Date.now(),
59
+ };
60
+ this.#inner.set(key, entry);
61
+
62
+ return this;
63
+ }
64
+
65
+ /**
66
+ * Get the value associated with the key, if it exists and has not expired.
67
+ * @param key K
68
+ * @returns the value associated with the key, or undefined if the key is not present or has expired.
69
+ */
70
+ get(key: K) {
71
+ const entry = this.#inner.get(key);
72
+ if (entry === undefined) {
73
+ return undefined;
74
+ }
75
+ if (Date.now() - entry.timestamp > this.#expirationTime) {
76
+ this.#inner.delete(key);
77
+ return undefined;
78
+ }
79
+ return entry.value;
80
+ }
81
+
82
+ /**
83
+ * Clear all entries.
84
+ */
85
+ clear() {
86
+ this.#inner.clear();
87
+ }
88
+
89
+ /**
90
+ * Entries returns the entries of the map, without the expiration time.
91
+ * @returns an iterator over the entries of the map.
92
+ */
93
+ entries(): MapIterator<[K, V]> {
94
+ const iterator = this.#inner.entries();
95
+ const generator = function* () {
96
+ for (const [key, value] of iterator) {
97
+ yield [key, value.value] as [K, V];
98
+ }
99
+ return undefined;
100
+ };
101
+ return generator();
102
+ }
103
+
104
+ /**
105
+ * Values returns the values of the map, without the expiration time.
106
+ * @returns an iterator over the values of the map.
107
+ */
108
+ values(): MapIterator<V> {
109
+ const iterator = this.#inner.values();
110
+ const generator = function* () {
111
+ for (const value of iterator) {
112
+ yield value.value;
113
+ }
114
+ return undefined;
115
+ };
116
+ return generator();
117
+ }
118
+
119
+ /**
120
+ * Keys returns the keys of the map
121
+ * @returns an iterator over the keys of the map.
122
+ */
123
+ keys(): MapIterator<K> {
124
+ return this.#inner.keys();
125
+ }
126
+
127
+ /**
128
+ * forEach calls the callbackfn on each entry of the map.
129
+ * @param callbackfn to call on each entry
130
+ * @param thisArg to use as this when calling the callbackfn
131
+ */
132
+ forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: ExpirableMap<K, V>) {
133
+ for (const [key, value] of this.#inner.entries()) {
134
+ callbackfn.call(thisArg, value.value, key, this);
135
+ }
136
+ }
137
+
138
+ /**
139
+ * has returns true if the key exists and has not expired.
140
+ * @param key K
141
+ * @returns true if the key exists and has not expired.
142
+ */
143
+ has(key: K): boolean {
144
+ return this.#inner.has(key);
145
+ }
146
+
147
+ /**
148
+ * delete the entry for the given key.
149
+ * @param key K
150
+ * @returns true if the key existed and has been deleted.
151
+ */
152
+ delete(key: K) {
153
+ return this.#inner.delete(key);
154
+ }
155
+
156
+ /**
157
+ * get size of the map.
158
+ * @returns the size of the map.
159
+ */
160
+ get size() {
161
+ return this.#inner.size;
162
+ }
163
+ }
@@ -0,0 +1,14 @@
1
+ import { PipeArrayBuffer, lebDecode } from '#candid';
2
+
3
+ const MILLISECOND_TO_NANOSECONDS = BigInt(1_000_000);
4
+
5
+ export const decodeLeb128 = (buf: Uint8Array): bigint => {
6
+ return lebDecode(new PipeArrayBuffer(buf));
7
+ };
8
+
9
+ // time is a LEB128-encoded Nat
10
+ export const decodeTime = (buf: Uint8Array): Date => {
11
+ const timestampNs = decodeLeb128(buf);
12
+ const timestampMs = timestampNs / MILLISECOND_TO_NANOSECONDS;
13
+ return new Date(Number(timestampMs));
14
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Generates a random unsigned 32-bit integer between 0 and 0xffffffff
3
+ * @returns {number} a random number
4
+ */
5
+ export const randomNumber = (): number => {
6
+ // determine whether browser crypto is available
7
+ if (typeof window !== 'undefined' && !!window.crypto && !!window.crypto.getRandomValues) {
8
+ const array = new Uint32Array(1);
9
+ window.crypto.getRandomValues(array);
10
+ return array[0];
11
+ }
12
+ // A second check for webcrypto, in case it is loaded under global instead of window
13
+ if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
14
+ const array = new Uint32Array(1);
15
+ crypto.getRandomValues(array);
16
+ return array[0];
17
+ }
18
+
19
+ type nodeCrypto = {
20
+ randomInt: (min: number, max: number) => number;
21
+ };
22
+
23
+ // determine whether node crypto is available
24
+ if (typeof crypto !== 'undefined' && (crypto as unknown as nodeCrypto).randomInt) {
25
+ return (crypto as unknown as nodeCrypto).randomInt(0, 0xffffffff);
26
+ }
27
+
28
+ // fall back to Math.random
29
+ return Math.floor(Math.random() * 0xffffffff);
30
+ };
@@ -0,0 +1,180 @@
1
+ import { Principal } from '#principal';
2
+ import * as cbor from '../cbor.ts';
3
+ import { decodeLeb128 } from '../utils/leb.ts';
4
+ import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils';
5
+ import { type DerEncodedPublicKey } from '../auth.ts';
6
+ import {
7
+ type Cert,
8
+ flatten_forks,
9
+ LookupPathStatus,
10
+ lookup_path,
11
+ lookup_subtree,
12
+ type LabeledHashTree,
13
+ LookupSubtreeStatus,
14
+ } from '../certificate.ts';
15
+ import {
16
+ LookupErrorCode,
17
+ DerKeyLengthMismatchErrorCode,
18
+ ProtocolError,
19
+ UnknownError,
20
+ HashTreeDecodeErrorCode,
21
+ } from '../errors.ts';
22
+
23
+ /**
24
+ * The root subnet ID for IC mainnet
25
+ */
26
+ export const IC_ROOT_SUBNET_ID = Principal.fromText(
27
+ 'tdb26-jop6k-aogll-7ltgs-eruif-6kk7m-qpktf-gdiqx-mxtrf-vb5e6-eqe',
28
+ );
29
+
30
+ export type SubnetNodeKeys = Map<string, DerEncodedPublicKey>;
31
+
32
+ /**
33
+ * Represents the useful information about a subnet
34
+ */
35
+ export type BaseSubnetStatus = {
36
+ /**
37
+ * The subnet ID
38
+ */
39
+ subnetId: string;
40
+ /**
41
+ * The node keys of the subnet
42
+ */
43
+ nodeKeys: SubnetNodeKeys;
44
+ /**
45
+ * Not supported
46
+ */
47
+ metrics?: never;
48
+ };
49
+
50
+ /**
51
+ * Base types of an entry on the status map.
52
+ * An entry of null indicates that the request failed, due to lack of permissions or the result being missing.
53
+ */
54
+ export type BaseStatus = string | Uint8Array | Date | Uint8Array[] | Principal[] | bigint | null;
55
+
56
+ /**
57
+ * Decode strategy for custom paths
58
+ */
59
+ export type DecodeStrategy = 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw';
60
+
61
+ /**
62
+ * Interface to define a custom path. Nested paths will be represented as individual buffers, and can be created from text using TextEncoder.
63
+ * @param {string} key the key to use to access the returned value in the status map
64
+ * @param {Uint8Array[]} path the path to the desired value, represented as an array of buffers
65
+ * @param {string} decodeStrategy the strategy to use to decode the returned value
66
+ */
67
+ export class CustomPath implements CustomPath {
68
+ public key: string;
69
+ public path: Uint8Array[] | string;
70
+ public decodeStrategy: DecodeStrategy;
71
+ constructor(key: string, path: Uint8Array[] | string, decodeStrategy: DecodeStrategy) {
72
+ this.key = key;
73
+ this.path = path;
74
+ this.decodeStrategy = decodeStrategy;
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Decode a value based on the specified strategy
80
+ * @param data the raw data to decode
81
+ * @param strategy the decode strategy to use
82
+ * @returns the decoded value
83
+ */
84
+ export function decodeValue(data: Uint8Array, strategy: DecodeStrategy): BaseStatus {
85
+ switch (strategy) {
86
+ case 'raw':
87
+ return data;
88
+ case 'leb128':
89
+ return decodeLeb128(data);
90
+ case 'cbor':
91
+ return cbor.decode(data);
92
+ case 'hex':
93
+ return bytesToHex(data);
94
+ case 'utf-8':
95
+ return new TextDecoder().decode(data);
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Decode controllers from CBOR-encoded buffer
101
+ * @param buf the CBOR-encoded buffer to decode
102
+ * @returns an array of principal IDs
103
+ */
104
+ export function decodeControllers(buf: Uint8Array): Principal[] {
105
+ const controllersRaw = cbor.decode<Uint8Array[]>(buf);
106
+ return controllersRaw.map(buf => {
107
+ return Principal.fromUint8Array(buf);
108
+ });
109
+ }
110
+
111
+ /**
112
+ * Encode a metadata path for canister metadata queries
113
+ * @param metaPath the metadata path (string or Uint8Array)
114
+ * @param canisterUint8Array the canister ID as Uint8Array
115
+ * @returns the encoded path
116
+ */
117
+ export function encodeMetadataPath(
118
+ metaPath: string | Uint8Array,
119
+ canisterUint8Array: Uint8Array,
120
+ ): Uint8Array[] {
121
+ const encoded = typeof metaPath === 'string' ? utf8ToBytes(metaPath) : metaPath;
122
+ return [utf8ToBytes('canister'), canisterUint8Array, utf8ToBytes('metadata'), encoded];
123
+ }
124
+
125
+ /**
126
+ * Check if a path object has custom path signature (has 'key' and 'path' properties)
127
+ * @param path the path to check
128
+ * @returns true if the path has custom path signature, false otherwise
129
+ */
130
+ export function isCustomPath<T>(path: T): path is T & { key: string; path: unknown } {
131
+ return typeof path === 'object' && path !== null && 'key' in path && 'path' in path;
132
+ }
133
+
134
+ /**
135
+ * Lookup node keys from a certificate for a given subnet
136
+ * This can be used for both canister and subnet status queries
137
+ * @param certificate the certificate to fetch node keys from
138
+ * @param subnetId the subnet ID to fetch node keys for
139
+ * @returns a map of node IDs to public keys
140
+ */
141
+ export function lookupNodeKeysFromCertificate(
142
+ certificate: Cert,
143
+ subnetId: Principal,
144
+ ): SubnetNodeKeys {
145
+ const subnetLookupResult = lookup_subtree(
146
+ ['subnet', subnetId.toUint8Array(), 'node'],
147
+ certificate.tree,
148
+ );
149
+ if (subnetLookupResult.status !== LookupSubtreeStatus.Found) {
150
+ throw ProtocolError.fromCode(new LookupErrorCode('Node not found', subnetLookupResult.status));
151
+ }
152
+ if (subnetLookupResult.value instanceof Uint8Array) {
153
+ throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid node tree'));
154
+ }
155
+
156
+ // The forks are all labeled trees with the <node_id> label
157
+ const nodeForks = flatten_forks(subnetLookupResult.value) as Array<LabeledHashTree>;
158
+ const nodeKeys = new Map<string, DerEncodedPublicKey>();
159
+
160
+ nodeForks.forEach(fork => {
161
+ const node_id = Principal.from(fork[1]).toText();
162
+ const publicKeyLookupResult = lookup_path(['public_key'], fork[2]);
163
+ if (publicKeyLookupResult.status !== LookupPathStatus.Found) {
164
+ throw ProtocolError.fromCode(
165
+ new LookupErrorCode('Public key not found', publicKeyLookupResult.status),
166
+ );
167
+ }
168
+
169
+ const derEncodedPublicKey = publicKeyLookupResult.value;
170
+ if (derEncodedPublicKey.byteLength !== 44) {
171
+ throw ProtocolError.fromCode(
172
+ new DerKeyLengthMismatchErrorCode(44, derEncodedPublicKey.byteLength),
173
+ );
174
+ } else {
175
+ nodeKeys.set(node_id, derEncodedPublicKey as DerEncodedPublicKey);
176
+ }
177
+ });
178
+
179
+ return nodeKeys;
180
+ }
@@ -0,0 +1,276 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ /* eslint-disable @typescript-eslint/no-this-alias */
3
+ /* eslint-disable @typescript-eslint/no-explicit-any */
4
+ import * as IDL from './idl.ts';
5
+
6
+ export interface ParseConfig {
7
+ random?: boolean;
8
+ }
9
+
10
+ export interface UIConfig {
11
+ input?: HTMLElement;
12
+ form?: InputForm;
13
+ parse(t: IDL.Type, config: ParseConfig, v: string): any;
14
+ }
15
+
16
+ export interface FormConfig {
17
+ open?: HTMLElement;
18
+ event?: string;
19
+ labelMap?: Record<string, string>;
20
+ container?: HTMLElement;
21
+ render(t: IDL.Type): InputBox;
22
+ }
23
+
24
+ export class InputBox {
25
+ public status: HTMLElement;
26
+ public label: string | null = null;
27
+ public value: any = undefined;
28
+
29
+ constructor(
30
+ public idl: IDL.Type,
31
+ public ui: UIConfig,
32
+ ) {
33
+ const status = document.createElement('span');
34
+ status.className = 'status';
35
+ this.status = status;
36
+
37
+ if (ui.input) {
38
+ ui.input.addEventListener('blur', () => {
39
+ if ((ui.input as HTMLInputElement).value === '') {
40
+ return;
41
+ }
42
+ this.parse();
43
+ });
44
+ ui.input.addEventListener('input', () => {
45
+ status.style.display = 'none';
46
+ ui.input!.classList.remove('reject');
47
+ });
48
+ }
49
+ }
50
+ public isRejected(): boolean {
51
+ return this.value === undefined;
52
+ }
53
+
54
+ public parse(config: ParseConfig = {}): any {
55
+ if (this.ui.form) {
56
+ const value = this.ui.form.parse(config);
57
+ this.value = value;
58
+ return value;
59
+ }
60
+
61
+ if (this.ui.input) {
62
+ const input = this.ui.input as HTMLInputElement;
63
+ try {
64
+ const value = this.ui.parse(this.idl, config, input.value);
65
+ if (!this.idl.covariant(value)) {
66
+ throw new Error(`${input.value} is not of type ${this.idl.display()}`);
67
+ }
68
+ this.status.style.display = 'none';
69
+ this.value = value;
70
+ return value;
71
+ } catch (err) {
72
+ input.classList.add('reject');
73
+ this.status.style.display = 'block';
74
+ this.status.innerHTML = 'InputError: ' + (err as Error).message;
75
+ this.value = undefined;
76
+ return undefined;
77
+ }
78
+ }
79
+ return null;
80
+ }
81
+ public render(dom: HTMLElement): void {
82
+ const container = document.createElement('span');
83
+ if (this.label) {
84
+ const label = document.createElement('label');
85
+ label.innerText = this.label;
86
+ container.appendChild(label);
87
+ }
88
+ if (this.ui.input) {
89
+ container.appendChild(this.ui.input);
90
+ container.appendChild(this.status);
91
+ }
92
+
93
+ if (this.ui.form) {
94
+ this.ui.form.render(container);
95
+ }
96
+ dom.appendChild(container);
97
+ }
98
+ }
99
+
100
+ export abstract class InputForm {
101
+ public form: InputBox[] = [];
102
+ constructor(public ui: FormConfig) {}
103
+
104
+ public abstract parse(config: ParseConfig): any;
105
+ public abstract generateForm(): any;
106
+ public renderForm(dom: HTMLElement): void {
107
+ if (this.ui.container) {
108
+ this.form.forEach(e => e.render(this.ui.container!));
109
+ dom.appendChild(this.ui.container);
110
+ } else {
111
+ this.form.forEach(e => e.render(dom));
112
+ }
113
+ }
114
+ public render(dom: HTMLElement): void {
115
+ if (this.ui.open && this.ui.event) {
116
+ dom.appendChild(this.ui.open);
117
+ const form = this;
118
+
119
+ form.ui.open!.addEventListener(form.ui.event!, () => {
120
+ // Remove old form
121
+ if (form.ui.container) {
122
+ form.ui.container.innerHTML = '';
123
+ } else {
124
+ const oldContainer = form.ui.open!.nextElementSibling;
125
+ if (oldContainer) {
126
+ oldContainer.parentNode!.removeChild(oldContainer);
127
+ }
128
+ }
129
+ // Render form
130
+ form.generateForm();
131
+ form.renderForm(dom);
132
+ });
133
+ } else {
134
+ this.generateForm();
135
+ this.renderForm(dom);
136
+ }
137
+ }
138
+ }
139
+
140
+ export class RecordForm extends InputForm {
141
+ constructor(
142
+ public fields: Array<[string, IDL.Type]>,
143
+ public ui: FormConfig,
144
+ ) {
145
+ super(ui);
146
+ }
147
+ public generateForm(): void {
148
+ this.form = this.fields.map(([key, type]) => {
149
+ const input = this.ui.render(type);
150
+ // eslint-disable-next-line
151
+ if (this.ui.labelMap && this.ui.labelMap.hasOwnProperty(key)) {
152
+ input.label = this.ui.labelMap[key] + ' ';
153
+ } else {
154
+ input.label = key + ' ';
155
+ }
156
+ return input;
157
+ });
158
+ }
159
+ public parse(config: ParseConfig): Record<string, any> | undefined {
160
+ const v: Record<string, any> = {};
161
+ this.fields.forEach(([key, _], i) => {
162
+ const value = this.form[i].parse(config);
163
+ v[key] = value;
164
+ });
165
+ if (this.form.some(input => input.isRejected())) {
166
+ return undefined;
167
+ }
168
+ return v;
169
+ }
170
+ }
171
+
172
+ export class TupleForm extends InputForm {
173
+ constructor(
174
+ public components: IDL.Type[],
175
+ public ui: FormConfig,
176
+ ) {
177
+ super(ui);
178
+ }
179
+ public generateForm(): void {
180
+ this.form = this.components.map(type => {
181
+ const input = this.ui.render(type);
182
+ return input;
183
+ });
184
+ }
185
+ public parse(config: ParseConfig): any[] | undefined {
186
+ const v: any[] = [];
187
+ this.components.forEach((_, i) => {
188
+ const value = this.form[i].parse(config);
189
+ v.push(value);
190
+ });
191
+ if (this.form.some(input => input.isRejected())) {
192
+ return undefined;
193
+ }
194
+ return v;
195
+ }
196
+ }
197
+
198
+ export class VariantForm extends InputForm {
199
+ constructor(
200
+ public fields: Array<[string, IDL.Type]>,
201
+ public ui: FormConfig,
202
+ ) {
203
+ super(ui);
204
+ }
205
+ public generateForm(): void {
206
+ const index = (this.ui.open as HTMLSelectElement).selectedIndex;
207
+ const [_, type] = this.fields[index];
208
+ const variant = this.ui.render(type);
209
+ this.form = [variant];
210
+ }
211
+ public parse(config: ParseConfig): Record<string, any> | undefined {
212
+ const select = this.ui.open as HTMLSelectElement;
213
+ const selected = select.options[select.selectedIndex].value;
214
+ const value = this.form[0].parse(config);
215
+ if (value === undefined) {
216
+ return undefined;
217
+ }
218
+ const v: Record<string, any> = {};
219
+ v[selected] = value;
220
+ return v;
221
+ }
222
+ }
223
+
224
+ export class OptionForm extends InputForm {
225
+ constructor(
226
+ public ty: IDL.Type,
227
+ public ui: FormConfig,
228
+ ) {
229
+ super(ui);
230
+ }
231
+ public generateForm(): void {
232
+ if ((this.ui.open as HTMLInputElement).checked) {
233
+ const opt = this.ui.render(this.ty);
234
+ this.form = [opt];
235
+ } else {
236
+ this.form = [];
237
+ }
238
+ }
239
+ public parse<T>(config: ParseConfig): [T] | [] | undefined {
240
+ if (this.form.length === 0) {
241
+ return [];
242
+ } else {
243
+ const value = this.form[0].parse(config);
244
+ if (value === undefined) {
245
+ return undefined;
246
+ }
247
+ return [value];
248
+ }
249
+ }
250
+ }
251
+
252
+ export class VecForm extends InputForm {
253
+ constructor(
254
+ public ty: IDL.Type,
255
+ public ui: FormConfig,
256
+ ) {
257
+ super(ui);
258
+ }
259
+ public generateForm(): void {
260
+ const len = +(this.ui.open as HTMLInputElement).value;
261
+ this.form = [];
262
+ for (let i = 0; i < len; i++) {
263
+ const t = this.ui.render(this.ty);
264
+ this.form.push(t);
265
+ }
266
+ }
267
+ public parse<T>(config: ParseConfig): T[] | undefined {
268
+ const value = this.form.map(input => {
269
+ return input.parse(config);
270
+ });
271
+ if (this.form.some(input => input.isRejected())) {
272
+ return undefined;
273
+ }
274
+ return value;
275
+ }
276
+ }