@icp-sdk/core 4.2.2 → 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,411 @@
1
+ import {
2
+ type DerEncodedPublicKey,
3
+ type HttpAgentRequest,
4
+ type PublicKey,
5
+ requestIdOf,
6
+ type Signature,
7
+ SignIdentity,
8
+ IC_REQUEST_DOMAIN_SEPARATOR,
9
+ IC_REQUEST_AUTH_DELEGATION_DOMAIN_SEPARATOR,
10
+ ToCborValue,
11
+ } from '#agent';
12
+ import { Principal } from '#principal';
13
+ import { PartialIdentity } from './partial.ts';
14
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
15
+
16
+ /**
17
+ * Safe wrapper around bytesToHex that handles ArrayBuffer/Uint8Array type conversion.
18
+ * Required because @noble/hashes v1.8+ strictly expects Uint8Array inputs.
19
+ * @param data The binary data to convert to hexadecimal string (ArrayBuffer, Uint8Array, or ArrayLike<number>)
20
+ */
21
+ function safeBytesToHex(data: ArrayBuffer | Uint8Array | ArrayLike<number>): string {
22
+ if (data instanceof Uint8Array) {
23
+ return bytesToHex(data);
24
+ }
25
+ return bytesToHex(new Uint8Array(data));
26
+ }
27
+
28
+ function _parseBlob(value: unknown): Uint8Array {
29
+ if (typeof value !== 'string' || value.length < 64) {
30
+ throw new Error('Invalid public key.');
31
+ }
32
+
33
+ return hexToBytes(value);
34
+ }
35
+
36
+ /**
37
+ * A single delegation object that is signed by a private key. This is constructed by
38
+ * `DelegationChain.create()`.
39
+ *
40
+ * {@see DelegationChain}
41
+ */
42
+ export class Delegation implements ToCborValue {
43
+ constructor(
44
+ public readonly pubkey: Uint8Array,
45
+ public readonly expiration: bigint,
46
+ public readonly targets?: Principal[],
47
+ ) {}
48
+
49
+ public toCborValue() {
50
+ return {
51
+ pubkey: this.pubkey,
52
+ expiration: this.expiration,
53
+ ...(this.targets && {
54
+ targets: this.targets,
55
+ }),
56
+ };
57
+ }
58
+
59
+ public toJSON(): JsonnableDelegation {
60
+ // every string should be hex and once-de-hexed,
61
+ // discoverable what it is (e.g. de-hex to get JSON with a 'type' property, or de-hex to DER
62
+ // with an OID). After de-hex, if it's not obvious what it is, it's an ArrayBuffer.
63
+ return {
64
+ expiration: this.expiration.toString(16),
65
+ pubkey: safeBytesToHex(this.pubkey),
66
+ ...(this.targets && { targets: this.targets.map(p => p.toHex()) }),
67
+ };
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Type of ReturnType<Delegation.toJSON>.
73
+ * The goal here is to stringify all non-JSON-compatible types to some bytes representation we can
74
+ * stringify as hex.
75
+ * (Hex shouldn't be ambiguous ever, because you can encode as DER with semantic OIDs).
76
+ */
77
+ interface JsonnableDelegation {
78
+ // A BigInt of Nanoseconds since epoch as hex
79
+ expiration: string;
80
+ // Hexadecimal representation of the DER public key.
81
+ pubkey: string;
82
+ // Array of strings, where each string is hex of principal blob (*NOT* textual representation).
83
+ targets?: string[];
84
+ }
85
+
86
+ /**
87
+ * A signed delegation, which lends its identity to the public key in the delegation
88
+ * object. This is constructed by `DelegationChain.create()`.
89
+ *
90
+ * {@see DelegationChain}
91
+ */
92
+ export interface SignedDelegation {
93
+ delegation: Delegation;
94
+ signature: Signature;
95
+ }
96
+
97
+ /**
98
+ * Sign a single delegation object for a period of time.
99
+ * @param from The identity that lends its delegation.
100
+ * @param to The identity that receives the delegation.
101
+ * @param expiration An expiration date for this delegation.
102
+ * @param targets Limit this delegation to the target principals.
103
+ */
104
+ async function _createSingleDelegation(
105
+ from: SignIdentity,
106
+ to: PublicKey,
107
+ expiration: Date,
108
+ targets?: Principal[],
109
+ ): Promise<SignedDelegation> {
110
+ const delegation: Delegation = new Delegation(
111
+ to.toDer(),
112
+ BigInt(+expiration) * BigInt(1000000), // In nanoseconds.
113
+ targets,
114
+ );
115
+ // The signature is calculated by signing the concatenation of the domain separator
116
+ // and the message.
117
+ // Note: To ensure Safari treats this as a user gesture, ensure to not use async methods
118
+ // besides the actualy webauthn functionality (such as `sign`). Safari will de-register
119
+ // a user gesture if you await an async call thats not fetch, xhr, or setTimeout.
120
+ const challenge = new Uint8Array([
121
+ ...IC_REQUEST_AUTH_DELEGATION_DOMAIN_SEPARATOR,
122
+ ...new Uint8Array(requestIdOf({ ...delegation })),
123
+ ]);
124
+ const signature = await from.sign(challenge);
125
+
126
+ return {
127
+ delegation,
128
+ signature,
129
+ };
130
+ }
131
+
132
+ export interface JsonnableDelegationChain {
133
+ publicKey: string;
134
+ delegations: Array<{
135
+ signature: string;
136
+ delegation: {
137
+ pubkey: string;
138
+ expiration: string;
139
+ targets?: string[];
140
+ };
141
+ }>;
142
+ }
143
+
144
+ /**
145
+ * A chain of delegations. This is JSON Serializable.
146
+ * This is the object to serialize and pass to a DelegationIdentity. It does not keep any
147
+ * private keys.
148
+ */
149
+ export class DelegationChain {
150
+ /**
151
+ * Create a delegation chain between two (or more) keys. By default, the expiration time
152
+ * will be very short (15 minutes).
153
+ *
154
+ * To build a chain of more than 2 identities, this function needs to be called multiple times,
155
+ * passing the previous delegation chain into the options argument. For example:
156
+ * @example
157
+ * const rootKey = createKey();
158
+ * const middleKey = createKey();
159
+ * const bottomeKey = createKey();
160
+ *
161
+ * const rootToMiddle = await DelegationChain.create(
162
+ * root, middle.getPublicKey(), Date.parse('2100-01-01'),
163
+ * );
164
+ * const middleToBottom = await DelegationChain.create(
165
+ * middle, bottom.getPublicKey(), Date.parse('2100-01-01'), { previous: rootToMiddle },
166
+ * );
167
+ *
168
+ * // We can now use a delegation identity that uses the delegation above:
169
+ * const identity = DelegationIdentity.fromDelegation(bottomKey, middleToBottom);
170
+ * @param from The identity that will delegate.
171
+ * @param to The identity that gets delegated. It can now sign messages as if it was the
172
+ * identity above.
173
+ * @param expiration The length the delegation is valid. By default, 15 minutes from calling
174
+ * this function.
175
+ * @param options A set of options for this delegation. expiration and previous
176
+ * @param options.previous - Another DelegationChain that this chain should start with.
177
+ * @param options.targets - targets that scope the delegation (e.g. Canister Principals)
178
+ */
179
+ public static async create(
180
+ from: SignIdentity,
181
+ to: PublicKey,
182
+ expiration: Date = new Date(Date.now() + 15 * 60 * 1000),
183
+ options: {
184
+ previous?: DelegationChain;
185
+ targets?: Principal[];
186
+ } = {},
187
+ ): Promise<DelegationChain> {
188
+ const delegation = await _createSingleDelegation(from, to, expiration, options.targets);
189
+ return new DelegationChain(
190
+ [...(options.previous?.delegations || []), delegation],
191
+ options.previous?.publicKey || from.getPublicKey().toDer(),
192
+ );
193
+ }
194
+
195
+ /**
196
+ * Creates a DelegationChain object from a JSON string.
197
+ * @param json The JSON string to parse.
198
+ */
199
+ public static fromJSON(json: string | JsonnableDelegationChain): DelegationChain {
200
+ const { publicKey, delegations } = typeof json === 'string' ? JSON.parse(json) : json;
201
+ if (!Array.isArray(delegations)) {
202
+ throw new Error('Invalid delegations.');
203
+ }
204
+
205
+ const parsedDelegations: SignedDelegation[] = delegations.map(signedDelegation => {
206
+ const { delegation, signature } = signedDelegation;
207
+ const { pubkey, expiration, targets } = delegation;
208
+ if (targets !== undefined && !Array.isArray(targets)) {
209
+ throw new Error('Invalid targets.');
210
+ }
211
+
212
+ return {
213
+ delegation: new Delegation(
214
+ _parseBlob(pubkey),
215
+ BigInt('0x' + expiration), // expiration in JSON is an hexa string (See toJSON() below).
216
+ targets &&
217
+ targets.map((t: unknown) => {
218
+ if (typeof t !== 'string') {
219
+ throw new Error('Invalid target.');
220
+ }
221
+ return Principal.fromHex(t);
222
+ }),
223
+ ),
224
+ signature: _parseBlob(signature) as Signature,
225
+ };
226
+ });
227
+
228
+ return new this(parsedDelegations, _parseBlob(publicKey) as DerEncodedPublicKey);
229
+ }
230
+
231
+ /**
232
+ * Creates a DelegationChain object from a list of delegations and a DER-encoded public key.
233
+ * @param delegations The list of delegations.
234
+ * @param publicKey The DER-encoded public key of the key-pair signing the first delegation.
235
+ */
236
+ public static fromDelegations(
237
+ delegations: SignedDelegation[],
238
+ publicKey: DerEncodedPublicKey,
239
+ ): DelegationChain {
240
+ return new this(delegations, publicKey);
241
+ }
242
+
243
+ protected constructor(
244
+ public readonly delegations: SignedDelegation[],
245
+ public readonly publicKey: DerEncodedPublicKey,
246
+ ) {}
247
+
248
+ public toJSON(): JsonnableDelegationChain {
249
+ return {
250
+ delegations: this.delegations.map(signedDelegation => {
251
+ const { delegation, signature } = signedDelegation;
252
+ const { targets } = delegation;
253
+ return {
254
+ delegation: {
255
+ expiration: delegation.expiration.toString(16),
256
+ pubkey: safeBytesToHex(delegation.pubkey),
257
+ ...(targets && {
258
+ targets: targets.map(t => t.toHex()),
259
+ }),
260
+ },
261
+ signature: safeBytesToHex(signature),
262
+ };
263
+ }),
264
+ publicKey: safeBytesToHex(this.publicKey),
265
+ };
266
+ }
267
+ }
268
+
269
+ /**
270
+ * An Identity that adds delegation to a request. Everywhere in this class, the name
271
+ * innerKey refers to the SignIdentity that is being used to sign the requests, while
272
+ * originalKey is the identity that is being borrowed. More identities can be used
273
+ * in the middle to delegate.
274
+ */
275
+ export class DelegationIdentity extends SignIdentity {
276
+ /**
277
+ * Create a delegation without having access to delegateKey.
278
+ * @param key The key used to sign the requests.
279
+ * @param delegation A delegation object created using `createDelegation`.
280
+ */
281
+ public static fromDelegation(
282
+ key: Pick<SignIdentity, 'sign'>,
283
+ delegation: DelegationChain,
284
+ ): DelegationIdentity {
285
+ return new this(key, delegation);
286
+ }
287
+
288
+ protected constructor(
289
+ private _inner: Pick<SignIdentity, 'sign'>,
290
+ private _delegation: DelegationChain,
291
+ ) {
292
+ super();
293
+ }
294
+
295
+ public getDelegation(): DelegationChain {
296
+ return this._delegation;
297
+ }
298
+
299
+ public getPublicKey(): PublicKey {
300
+ return {
301
+ derKey: this._delegation.publicKey,
302
+ toDer: () => this._delegation.publicKey,
303
+ };
304
+ }
305
+ public sign(blob: Uint8Array): Promise<Signature> {
306
+ return this._inner.sign(blob);
307
+ }
308
+
309
+ public async transformRequest(request: HttpAgentRequest): Promise<unknown> {
310
+ const { body, ...fields } = request;
311
+ const requestId = await requestIdOf(body);
312
+ return {
313
+ ...fields,
314
+ body: {
315
+ content: body,
316
+ sender_sig: await this.sign(
317
+ new Uint8Array([...IC_REQUEST_DOMAIN_SEPARATOR, ...new Uint8Array(requestId)]),
318
+ ),
319
+ sender_delegation: this._delegation.delegations,
320
+ sender_pubkey: this._delegation.publicKey,
321
+ },
322
+ };
323
+ }
324
+ }
325
+
326
+ /**
327
+ * A partial delegated identity, representing a delegation chain and the public key that it targets
328
+ */
329
+ export class PartialDelegationIdentity extends PartialIdentity {
330
+ #delegation: DelegationChain;
331
+
332
+ /**
333
+ * The Delegation Chain of this identity.
334
+ */
335
+ get delegation(): DelegationChain {
336
+ return this.#delegation;
337
+ }
338
+
339
+ private constructor(inner: PublicKey, delegation: DelegationChain) {
340
+ super(inner);
341
+ this.#delegation = delegation;
342
+ }
343
+
344
+ /**
345
+ * Create a {@link PartialDelegationIdentity} from a {@link PublicKey} and a {@link DelegationChain}.
346
+ * @param key The {@link PublicKey} to delegate to.
347
+ * @param delegation a {@link DelegationChain} targeting the inner key.
348
+ */
349
+ public static fromDelegation(key: PublicKey, delegation: DelegationChain) {
350
+ return new PartialDelegationIdentity(key, delegation);
351
+ }
352
+ }
353
+
354
+ /**
355
+ * List of things to check for a delegation chain validity.
356
+ */
357
+ export interface DelegationValidChecks {
358
+ /**
359
+ * Check that the scope is amongst the scopes that this delegation has access to.
360
+ */
361
+ scope?: Principal | string | Array<Principal | string>;
362
+ }
363
+
364
+ /**
365
+ * Analyze a DelegationChain and validate that it's valid, ie. not expired and apply to the
366
+ * scope.
367
+ * @param chain The chain to validate.
368
+ * @param checks Various checks to validate on the chain.
369
+ */
370
+ export function isDelegationValid(chain: DelegationChain, checks?: DelegationValidChecks): boolean {
371
+ // Verify that the no delegation is expired. If any are in the chain, returns false.
372
+ for (const { delegation } of chain.delegations) {
373
+ // prettier-ignore
374
+ if (+new Date(Number(delegation.expiration / BigInt(1000000))) <= +Date.now()) {
375
+ return false;
376
+ }
377
+ }
378
+
379
+ // Check the scopes.
380
+ const scopes: Principal[] = [];
381
+ const maybeScope = checks?.scope;
382
+ if (maybeScope) {
383
+ if (Array.isArray(maybeScope)) {
384
+ scopes.push(...maybeScope.map(s => (typeof s === 'string' ? Principal.fromText(s) : s)));
385
+ } else {
386
+ scopes.push(typeof maybeScope === 'string' ? Principal.fromText(maybeScope) : maybeScope);
387
+ }
388
+ }
389
+
390
+ for (const s of scopes) {
391
+ const scope = s.toText();
392
+ for (const { delegation } of chain.delegations) {
393
+ if (delegation.targets === undefined) {
394
+ continue;
395
+ }
396
+
397
+ let none = true;
398
+ for (const target of delegation.targets) {
399
+ if (target.toText() === scope) {
400
+ none = false;
401
+ break;
402
+ }
403
+ }
404
+ if (none) {
405
+ return false;
406
+ }
407
+ }
408
+ }
409
+
410
+ return true;
411
+ }
@@ -0,0 +1,158 @@
1
+ import { type DerEncodedPublicKey, type PublicKey, type Signature, SignIdentity } from '#agent';
2
+ import { uint8FromBufLike } from '#candid';
3
+
4
+ /**
5
+ * Options used in a {@link ECDSAKeyIdentity}
6
+ */
7
+ export type CryptoKeyOptions = {
8
+ extractable?: boolean;
9
+ keyUsages?: KeyUsage[];
10
+ subtleCrypto?: SubtleCrypto;
11
+ };
12
+
13
+ export class CryptoError extends Error {
14
+ constructor(public readonly message: string) {
15
+ super(message);
16
+ Object.setPrototypeOf(this, CryptoError.prototype);
17
+ }
18
+ }
19
+
20
+ export interface DerCryptoKey extends CryptoKey {
21
+ toDer: () => DerEncodedPublicKey;
22
+ }
23
+
24
+ /**
25
+ * Utility method to ensure that a subtleCrypto implementation is provided or is available in the global context
26
+ * @param subtleCrypto SubtleCrypto implementation
27
+ * @returns subleCrypto
28
+ */
29
+ function _getEffectiveCrypto(subtleCrypto: CryptoKeyOptions['subtleCrypto']): SubtleCrypto {
30
+ if (typeof global !== 'undefined' && global['crypto'] && global['crypto']['subtle']) {
31
+ return global['crypto']['subtle'];
32
+ }
33
+ if (subtleCrypto) {
34
+ return subtleCrypto;
35
+ } else if (typeof crypto !== 'undefined' && crypto['subtle']) {
36
+ return crypto.subtle;
37
+ } else {
38
+ throw new CryptoError(
39
+ 'Global crypto was not available and none was provided. Please inlcude a SubtleCrypto implementation. See https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto',
40
+ );
41
+ }
42
+ }
43
+
44
+ /**
45
+ * An identity interface that wraps an ECDSA keypair using the P-256 named curve. Supports DER-encoding and decoding for agent calls
46
+ */
47
+ export class ECDSAKeyIdentity extends SignIdentity {
48
+ /**
49
+ * Generates a randomly generated identity for use in calls to the Internet Computer.
50
+ * @param {CryptoKeyOptions} options optional settings
51
+ * @param {CryptoKeyOptions['extractable']} options.extractable - whether the key should allow itself to be used. Set to false for maximum security.
52
+ * @param {CryptoKeyOptions['keyUsages']} options.keyUsages - a list of key usages that the key can be used for
53
+ * @param {CryptoKeyOptions['subtleCrypto']} options.subtleCrypto interface
54
+ * @returns a {@link ECDSAKeyIdentity}
55
+ */
56
+ public static async generate(options?: CryptoKeyOptions): Promise<ECDSAKeyIdentity> {
57
+ const { extractable = false, keyUsages = ['sign', 'verify'], subtleCrypto } = options ?? {};
58
+ const effectiveCrypto = _getEffectiveCrypto(subtleCrypto);
59
+ const keyPair = await effectiveCrypto.generateKey(
60
+ {
61
+ name: 'ECDSA',
62
+ namedCurve: 'P-256',
63
+ },
64
+ extractable,
65
+ keyUsages,
66
+ );
67
+ const derKey: DerEncodedPublicKey = uint8FromBufLike(
68
+ await effectiveCrypto.exportKey('spki', keyPair.publicKey),
69
+ );
70
+
71
+ Object.assign(derKey, {
72
+ __derEncodedPublicKey__: undefined,
73
+ });
74
+
75
+ return new this(keyPair, derKey, effectiveCrypto);
76
+ }
77
+
78
+ /**
79
+ * generates an identity from a public and private key. Please ensure that you are generating these keys securely and protect the user's private key
80
+ * @param keyPair a CryptoKeyPair
81
+ * @param subtleCrypto - a SubtleCrypto interface in case one is not available globally
82
+ * @returns an {@link ECDSAKeyIdentity}
83
+ */
84
+ public static async fromKeyPair(
85
+ keyPair: CryptoKeyPair | { privateKey: CryptoKey; publicKey: CryptoKey },
86
+ subtleCrypto?: SubtleCrypto,
87
+ ): Promise<ECDSAKeyIdentity> {
88
+ const effectiveCrypto = _getEffectiveCrypto(subtleCrypto);
89
+ const derKey: DerEncodedPublicKey = uint8FromBufLike(
90
+ await effectiveCrypto.exportKey('spki', keyPair.publicKey),
91
+ );
92
+ Object.assign(derKey, {
93
+ __derEncodedPublicKey__: undefined,
94
+ });
95
+ return new ECDSAKeyIdentity(keyPair, derKey, effectiveCrypto);
96
+ }
97
+
98
+ protected _derKey: DerEncodedPublicKey;
99
+ protected _keyPair: CryptoKeyPair;
100
+ protected _subtleCrypto: SubtleCrypto;
101
+
102
+ // `fromKeyPair` and `generate` should be used for instantiation, not this constructor.
103
+ protected constructor(
104
+ keyPair: CryptoKeyPair,
105
+ derKey: DerEncodedPublicKey,
106
+ subtleCrypto: SubtleCrypto,
107
+ ) {
108
+ super();
109
+ this._keyPair = keyPair;
110
+ this._derKey = derKey;
111
+ this._subtleCrypto = subtleCrypto;
112
+ }
113
+
114
+ /**
115
+ * Return the internally-used key pair.
116
+ * @returns a CryptoKeyPair
117
+ */
118
+ public getKeyPair(): CryptoKeyPair {
119
+ return this._keyPair;
120
+ }
121
+
122
+ /**
123
+ * Return the public key.
124
+ * @returns an {@link PublicKey & DerCryptoKey}
125
+ */
126
+ public getPublicKey(): PublicKey & DerCryptoKey {
127
+ const derKey = this._derKey;
128
+ const key: DerCryptoKey = Object.create(this._keyPair.publicKey);
129
+ key.toDer = function () {
130
+ return derKey;
131
+ };
132
+
133
+ return key;
134
+ }
135
+
136
+ /**
137
+ * Signs a blob of data, with this identity's private key.
138
+ * @param {Uint8Array} challenge - challenge to sign with this identity's secretKey, producing a signature
139
+ * @returns {Promise<Signature>} signature
140
+ */
141
+ public async sign(challenge: Uint8Array): Promise<Signature> {
142
+ const params: EcdsaParams = {
143
+ name: 'ECDSA',
144
+ hash: { name: 'SHA-256' },
145
+ };
146
+ const signature = uint8FromBufLike(
147
+ await this._subtleCrypto.sign(params, this._keyPair.privateKey, challenge),
148
+ );
149
+
150
+ Object.assign(signature, {
151
+ __signature__: undefined,
152
+ });
153
+
154
+ return signature as Signature;
155
+ }
156
+ }
157
+
158
+ export default ECDSAKeyIdentity;