@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,156 @@
1
+ import { uint8Equals } from '#candid';
2
+
3
+ export const encodeLenBytes = (len: number): number => {
4
+ if (len <= 0x7f) {
5
+ return 1;
6
+ } else if (len <= 0xff) {
7
+ return 2;
8
+ } else if (len <= 0xffff) {
9
+ return 3;
10
+ } else if (len <= 0xffffff) {
11
+ return 4;
12
+ } else {
13
+ throw new Error('Length too long (> 4 bytes)');
14
+ }
15
+ };
16
+
17
+ export const encodeLen = (buf: Uint8Array, offset: number, len: number): number => {
18
+ if (len <= 0x7f) {
19
+ buf[offset] = len;
20
+ return 1;
21
+ } else if (len <= 0xff) {
22
+ buf[offset] = 0x81;
23
+ buf[offset + 1] = len;
24
+ return 2;
25
+ } else if (len <= 0xffff) {
26
+ buf[offset] = 0x82;
27
+ buf[offset + 1] = len >> 8;
28
+ buf[offset + 2] = len;
29
+ return 3;
30
+ } else if (len <= 0xffffff) {
31
+ buf[offset] = 0x83;
32
+ buf[offset + 1] = len >> 16;
33
+ buf[offset + 2] = len >> 8;
34
+ buf[offset + 3] = len;
35
+ return 4;
36
+ } else {
37
+ throw new Error('Length too long (> 4 bytes)');
38
+ }
39
+ };
40
+
41
+ export const decodeLenBytes = (buf: Uint8Array, offset: number): number => {
42
+ if (buf[offset] < 0x80) return 1;
43
+ if (buf[offset] === 0x80) throw new Error('Invalid length 0');
44
+ if (buf[offset] === 0x81) return 2;
45
+ if (buf[offset] === 0x82) return 3;
46
+ if (buf[offset] === 0x83) return 4;
47
+ throw new Error('Length too long (> 4 bytes)');
48
+ };
49
+
50
+ export const decodeLen = (buf: Uint8Array, offset: number): number => {
51
+ const lenBytes = decodeLenBytes(buf, offset);
52
+ if (lenBytes === 1) return buf[offset];
53
+ else if (lenBytes === 2) return buf[offset + 1];
54
+ else if (lenBytes === 3) return (buf[offset + 1] << 8) + buf[offset + 2];
55
+ else if (lenBytes === 4)
56
+ return (buf[offset + 1] << 16) + (buf[offset + 2] << 8) + buf[offset + 3];
57
+ throw new Error('Length too long (> 4 bytes)');
58
+ };
59
+
60
+ /**
61
+ * A DER encoded `SEQUENCE(OID)` for DER-encoded-COSE
62
+ */
63
+ export const DER_COSE_OID = Uint8Array.from([
64
+ ...[0x30, 0x0c], // SEQUENCE
65
+ ...[0x06, 0x0a], // OID with 10 bytes
66
+ ...[0x2b, 0x06, 0x01, 0x04, 0x01, 0x83, 0xb8, 0x43, 0x01, 0x01], // DER encoded COSE
67
+ ]);
68
+
69
+ /**
70
+ * A DER encoded `SEQUENCE(OID)` for the Ed25519 algorithm
71
+ */
72
+ export const ED25519_OID = Uint8Array.from([
73
+ ...[0x30, 0x05], // SEQUENCE
74
+ ...[0x06, 0x03], // OID with 3 bytes
75
+ ...[0x2b, 0x65, 0x70], // id-Ed25519 OID
76
+ ]);
77
+
78
+ /**
79
+ * A DER encoded `SEQUENCE(OID)` for secp256k1 with the ECDSA algorithm
80
+ */
81
+ export const SECP256K1_OID = Uint8Array.from([
82
+ ...[0x30, 0x10], // SEQUENCE
83
+ ...[0x06, 0x07], // OID with 7 bytes
84
+ ...[0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01], // OID ECDSA
85
+ ...[0x06, 0x05], // OID with 5 bytes
86
+ ...[0x2b, 0x81, 0x04, 0x00, 0x0a], // OID secp256k1
87
+ ]);
88
+
89
+ /**
90
+ * Wraps the given `payload` in a DER encoding tagged with the given encoded `oid` like so:
91
+ * `SEQUENCE(oid, BITSTRING(payload))`
92
+ * @param payload The payload to encode as the bit string
93
+ * @param oid The DER encoded (and SEQUENCE wrapped!) OID to tag the payload with
94
+ */
95
+ export function wrapDER(payload: Uint8Array, oid: Uint8Array): Uint8Array {
96
+ // The Bit String header needs to include the unused bit count byte in its length
97
+ const bitStringHeaderLength = 2 + encodeLenBytes(payload.byteLength + 1);
98
+ const len = oid.byteLength + bitStringHeaderLength + payload.byteLength;
99
+ let offset = 0;
100
+ const buf = new Uint8Array(1 + encodeLenBytes(len) + len);
101
+ // Sequence
102
+ buf[offset++] = 0x30;
103
+ // Sequence Length
104
+ offset += encodeLen(buf, offset, len);
105
+
106
+ // OID
107
+ buf.set(oid, offset);
108
+ offset += oid.byteLength;
109
+
110
+ // Bit String Header
111
+ buf[offset++] = 0x03;
112
+ offset += encodeLen(buf, offset, payload.byteLength + 1);
113
+ // 0 padding
114
+ buf[offset++] = 0x00;
115
+ buf.set(new Uint8Array(payload), offset);
116
+
117
+ return buf;
118
+ }
119
+
120
+ /**
121
+ * Extracts a payload from the given `derEncoded` data, and checks that it was tagged with the given `oid`.
122
+ *
123
+ * `derEncoded = SEQUENCE(oid, BITSTRING(payload))`
124
+ * @param derEncoded The DER encoded and tagged data
125
+ * @param oid The DER encoded (and SEQUENCE wrapped!) expected OID
126
+ * @returns The unwrapped payload
127
+ */
128
+ export const unwrapDER = (derEncoded: Uint8Array, oid: Uint8Array): Uint8Array => {
129
+ let offset = 0;
130
+ const expect = (n: number, msg: string) => {
131
+ if (buf[offset++] !== n) {
132
+ throw new Error('Expected: ' + msg);
133
+ }
134
+ };
135
+
136
+ const buf = new Uint8Array(derEncoded);
137
+ expect(0x30, 'sequence');
138
+ offset += decodeLenBytes(buf, offset);
139
+
140
+ if (!uint8Equals(buf.slice(offset, offset + oid.byteLength), oid)) {
141
+ throw new Error('Not the expected OID.');
142
+ }
143
+ offset += oid.byteLength;
144
+
145
+ expect(0x03, 'bit string');
146
+ const payloadLen = decodeLen(buf, offset) - 1; // Subtracting 1 to account for the 0 padding
147
+ offset += decodeLenBytes(buf, offset);
148
+ expect(0x00, '0 padding');
149
+ const result = buf.slice(offset);
150
+ if (payloadLen !== result.length) {
151
+ throw new Error(
152
+ `DER payload mismatch: Expected length ${payloadLen} actual length ${result.length}`,
153
+ );
154
+ }
155
+ return result;
156
+ };
@@ -1 +1,5 @@
1
- export * from '@dfinity/identity-secp256k1';
1
+ /**
2
+ * @module api
3
+ */
4
+
5
+ export * from './secp256k1.ts';
@@ -0,0 +1,40 @@
1
+ import * as asn1js from 'asn1js';
2
+
3
+ const HEADER = `-----BEGIN EC PRIVATE KEY-----`;
4
+ const FOOTER = `-----END EC PRIVATE KEY-----`;
5
+
6
+ /**
7
+ * Parse a PEM-encoded key into a Uint8Array
8
+ * @param pem - the PEM-encoded key
9
+ * @returns secret key as a Uint8Array
10
+ */
11
+ export function pemToSecretKey(pem: string): Uint8Array {
12
+ const lines = pem.trim().split('\n');
13
+ const header = lines[0].trim();
14
+ const footer = lines[lines.length - 1].trim();
15
+ if (lines.length < 3) {
16
+ throw new Error('Invalid PEM format');
17
+ }
18
+ if (!header.startsWith(HEADER)) {
19
+ throw new Error('Invalid PEM header');
20
+ }
21
+ if (!footer.startsWith(FOOTER)) {
22
+ throw new Error('Invalid PEM footer');
23
+ }
24
+ const base64Data = lines.slice(1, -1).join('').replace(/\r?\n/g, '');
25
+ const rawKey = Uint8Array.from(atob(base64Data), c => c.charCodeAt(0));
26
+
27
+ // Parse the key as ASN.1 object
28
+ try {
29
+ const asn1 = asn1js.fromBER(rawKey);
30
+ // OID for secp256k1
31
+ if (!asn1.result.toString().includes(`OBJECT IDENTIFIER : 1.3.132.0.10`)) {
32
+ throw new Error('Invalid curve, must be secp256k1');
33
+ }
34
+ // Returns the 32-byte raw private key at the appropriate offset
35
+ return rawKey.slice(7, 39);
36
+ } catch (error) {
37
+ console.error(error);
38
+ throw new Error('Encountered error while parsing PEM key');
39
+ }
40
+ }
@@ -0,0 +1,270 @@
1
+ import {
2
+ type DerEncodedPublicKey,
3
+ type KeyPair,
4
+ type Signature,
5
+ type PublicKey,
6
+ SignIdentity,
7
+ } from '#agent';
8
+ import { secp256k1 } from '@noble/curves/secp256k1';
9
+ import { sha256 } from '@noble/hashes/sha2';
10
+ import { bytesToHex, hexToBytes, randomBytes } from '@noble/hashes/utils';
11
+ import * as bip39 from '@scure/bip39';
12
+ import { HDKey } from '@scure/bip32';
13
+ import { SECP256K1_OID, unwrapDER, wrapDER } from './der.ts';
14
+ import { pemToSecretKey } from './pem.ts';
15
+ import { uint8FromBufLike } from '#candid';
16
+
17
+ declare type PublicKeyHex = string;
18
+ declare type SecretKeyHex = string;
19
+ export declare type JsonableSecp256k1Identity = [PublicKeyHex, SecretKeyHex];
20
+
21
+ declare type KeyLike = PublicKey | DerEncodedPublicKey | ArrayBuffer | ArrayBufferView | Uint8Array;
22
+
23
+ function isObject(value: unknown) {
24
+ return value !== null && typeof value === 'object';
25
+ }
26
+
27
+ export class Secp256k1PublicKey implements PublicKey {
28
+ public static fromRaw(rawKey: Uint8Array): Secp256k1PublicKey {
29
+ return new Secp256k1PublicKey(rawKey);
30
+ }
31
+
32
+ public static fromDer(derKey: DerEncodedPublicKey): Secp256k1PublicKey {
33
+ return new Secp256k1PublicKey(this.derDecode(derKey));
34
+ }
35
+
36
+ /**
37
+ * Construct Secp256k1PublicKey from an existing PublicKey
38
+ * @param {unknown} maybeKey - existing PublicKey, ArrayBuffer, DerEncodedPublicKey, or hex string
39
+ * @returns {Secp256k1PublicKey} Instance of Secp256k1PublicKey
40
+ */
41
+ public static from(maybeKey: unknown): Secp256k1PublicKey {
42
+ if (typeof maybeKey === 'string') {
43
+ const key = hexToBytes(maybeKey);
44
+ return this.fromRaw(key);
45
+ } else if (isObject(maybeKey)) {
46
+ const key = maybeKey as KeyLike;
47
+ if (isObject(key) && Object.hasOwnProperty.call(key, '__derEncodedPublicKey__')) {
48
+ return this.fromDer(key as DerEncodedPublicKey);
49
+ } else if (ArrayBuffer.isView(key)) {
50
+ const view = key as ArrayBufferView;
51
+ return this.fromRaw(uint8FromBufLike(view.buffer));
52
+ } else if (key instanceof ArrayBuffer) {
53
+ return this.fromRaw(uint8FromBufLike(key));
54
+ } else if ('rawKey' in key && key['rawKey'] !== undefined) {
55
+ return this.fromRaw(key.rawKey);
56
+ } else if ('derKey' in key) {
57
+ return this.fromDer(key.derKey as DerEncodedPublicKey);
58
+ } else if ('toDer' in key) {
59
+ return this.fromDer(key.toDer());
60
+ }
61
+ }
62
+ throw new Error('Cannot construct Secp256k1PublicKey from the provided key.');
63
+ }
64
+
65
+ private static derEncode(publicKey: Uint8Array): DerEncodedPublicKey {
66
+ const key = uint8FromBufLike(wrapDER(publicKey, SECP256K1_OID).buffer) as DerEncodedPublicKey;
67
+ key.__derEncodedPublicKey__ = undefined;
68
+ return key;
69
+ }
70
+
71
+ private static derDecode(key: DerEncodedPublicKey): Uint8Array {
72
+ return unwrapDER(key, SECP256K1_OID);
73
+ }
74
+
75
+ #rawKey: Uint8Array;
76
+
77
+ public get rawKey(): Uint8Array {
78
+ return this.#rawKey;
79
+ }
80
+
81
+ #derKey: DerEncodedPublicKey;
82
+
83
+ public get derKey(): DerEncodedPublicKey {
84
+ return this.#derKey;
85
+ }
86
+
87
+ // `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
88
+ private constructor(key: Uint8Array) {
89
+ this.#rawKey = key;
90
+ this.#derKey = Secp256k1PublicKey.derEncode(key);
91
+ }
92
+
93
+ public toDer(): DerEncodedPublicKey {
94
+ return this.derKey;
95
+ }
96
+
97
+ public toRaw(): Uint8Array {
98
+ return this.rawKey;
99
+ }
100
+ }
101
+
102
+ export class Secp256k1KeyIdentity extends SignIdentity {
103
+ /**
104
+ * Generates an identity. If a seed is provided, the keys are generated from the
105
+ * seed according to BIP 0032. Otherwise, the key pair is randomly generated.
106
+ * This method throws an error in case the seed is not 32 bytes long or invalid
107
+ * for use as a private key.
108
+ * @param {Uint8Array} seed the optional seed
109
+ * @returns {Secp256k1KeyIdentity} Secp256k1KeyIdentity
110
+ */
111
+ public static generate(seed?: Uint8Array): Secp256k1KeyIdentity {
112
+ if (seed && seed.byteLength !== 32) {
113
+ throw new Error('Secp256k1 Seed needs to be 32 bytes long.');
114
+ }
115
+ let privateKey: Uint8Array;
116
+
117
+ if (seed) {
118
+ // private key from seed according to https://en.bitcoin.it/wiki/BIP_0032
119
+ // master key generation:
120
+ privateKey = seed;
121
+ if (!secp256k1.utils.isValidPrivateKey(privateKey)) {
122
+ throw new Error('The seed is invalid.');
123
+ }
124
+ } else {
125
+ privateKey = randomBytes(32);
126
+ while (!secp256k1.utils.isValidPrivateKey(privateKey)) {
127
+ privateKey = randomBytes(32);
128
+ }
129
+ }
130
+
131
+ const publicKeyRaw = secp256k1.getPublicKey(privateKey, false);
132
+
133
+ const publicKey = Secp256k1PublicKey.fromRaw(publicKeyRaw);
134
+ return new this(publicKey, privateKey);
135
+ }
136
+
137
+ public static fromParsedJson(obj: JsonableSecp256k1Identity): Secp256k1KeyIdentity {
138
+ const [publicKeyRaw, privateKeyRaw] = obj;
139
+ return new Secp256k1KeyIdentity(
140
+ Secp256k1PublicKey.fromRaw(hexToBytes(publicKeyRaw)),
141
+ hexToBytes(privateKeyRaw),
142
+ );
143
+ }
144
+
145
+ public static fromJSON(json: string): Secp256k1KeyIdentity {
146
+ const parsed = JSON.parse(json);
147
+ if (Array.isArray(parsed)) {
148
+ if (typeof parsed[0] === 'string' && typeof parsed[1] === 'string') {
149
+ return this.fromParsedJson([parsed[0], parsed[1]]);
150
+ }
151
+ throw new Error('Deserialization error: JSON must have at least 2 items.');
152
+ }
153
+ throw new Error(`Deserialization error: Invalid JSON type for string: ${JSON.stringify(json)}`);
154
+ }
155
+
156
+ /**
157
+ * 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
158
+ * @param {Uint8Array} publicKey - Uint8Array
159
+ * @param {Uint8Array} privateKey - Uint8Array
160
+ * @returns {Secp256k1KeyIdentity} Secp256k1KeyIdentity
161
+ */
162
+ public static fromKeyPair(publicKey: Uint8Array, privateKey: Uint8Array): Secp256k1KeyIdentity {
163
+ return new Secp256k1KeyIdentity(Secp256k1PublicKey.fromRaw(publicKey), privateKey);
164
+ }
165
+
166
+ /**
167
+ * generates an identity from an existing secret key, and is the correct method to generate an identity from a seed phrase. Please ensure you protect the user's private key.
168
+ * @param {Uint8Array} secretKey - Uint8Array
169
+ * @returns {Secp256k1KeyIdentity} - Secp256k1KeyIdentity
170
+ */
171
+ public static fromSecretKey(secretKey: Uint8Array): Secp256k1KeyIdentity {
172
+ const publicKey = secp256k1.getPublicKey(secretKey, false);
173
+ const identity = Secp256k1KeyIdentity.fromKeyPair(publicKey, secretKey);
174
+ return identity;
175
+ }
176
+
177
+ /**
178
+ * Generates an identity from a seed phrase. Use carefully - seed phrases should only be used in secure contexts, and you should avoid having users copying and pasting seed phrases as much as possible.
179
+ * @param {string | string[]} seedPhrase - either an array of words or a string of words separated by spaces.
180
+ * @param password - optional password to be used by bip39
181
+ * @returns Secp256k1KeyIdentity
182
+ */
183
+ public static fromSeedPhrase(
184
+ seedPhrase: string | string[],
185
+ password?: string | undefined,
186
+ ): Secp256k1KeyIdentity {
187
+ // Convert to string for convenience
188
+ const phrase = Array.isArray(seedPhrase) ? seedPhrase.join(' ') : seedPhrase;
189
+ // Warn if provided phrase is not conventional
190
+ if (phrase.split(' ').length < 12 || phrase.split(' ').length > 24) {
191
+ console.warn(
192
+ 'Warning - an unusually formatted seed phrase has been provided. Decoding may not work as expected',
193
+ );
194
+ }
195
+
196
+ const seed = bip39.mnemonicToSeedSync(phrase, password);
197
+ // Ensure the seed is 64 bytes long
198
+ if (seed.byteLength !== 64) {
199
+ throw new Error('Derived seed must be 64 bytes long.');
200
+ }
201
+ const root = HDKey.fromMasterSeed(seed);
202
+ const addrnode = root.derive("m/44'/223'/0'/0/0");
203
+
204
+ if (!addrnode.privateKey) {
205
+ throw new Error('Failed to derive private key from seed phrase');
206
+ }
207
+
208
+ return Secp256k1KeyIdentity.fromSecretKey(addrnode.privateKey);
209
+ }
210
+
211
+ /**
212
+ * Utility method to create a Secp256k1KeyIdentity from a PEM-encoded key.
213
+ * @param pemKey - PEM-encoded key as a string
214
+ * @returns - Secp256k1KeyIdentity
215
+ */
216
+ public static fromPem(pemKey: string): Secp256k1KeyIdentity {
217
+ const secretKey = pemToSecretKey(pemKey);
218
+ return this.fromSecretKey(secretKey);
219
+ }
220
+
221
+ _publicKey: Secp256k1PublicKey;
222
+
223
+ protected constructor(
224
+ publicKey: Secp256k1PublicKey,
225
+ protected _privateKey: Uint8Array,
226
+ ) {
227
+ super();
228
+ this._publicKey = publicKey;
229
+ }
230
+
231
+ /**
232
+ * Serialize this key to JSON-serializable object.
233
+ * @returns {JsonableSecp256k1Identity} JsonableSecp256k1Identity
234
+ */
235
+ public toJSON(): JsonableSecp256k1Identity {
236
+ return [bytesToHex(this._publicKey.toRaw()), bytesToHex(this._privateKey)];
237
+ }
238
+
239
+ /**
240
+ * Return a copy of the key pair.
241
+ * @returns {KeyPair} KeyPair
242
+ */
243
+ public getKeyPair(): KeyPair {
244
+ return {
245
+ secretKey: this._privateKey,
246
+ publicKey: this._publicKey,
247
+ };
248
+ }
249
+
250
+ /**
251
+ * Return the public key.
252
+ * @returns {Required<PublicKey>} Required<PublicKey>
253
+ */
254
+ public getPublicKey(): Required<PublicKey> {
255
+ return this._publicKey;
256
+ }
257
+
258
+ /**
259
+ * Signs a blob of data, with this identity's private key.
260
+ * @param {Uint8Array} data - bytes to hash and sign with this identity's secretKey, producing a signature
261
+ * @returns {Promise<Signature>} signature
262
+ */
263
+ public async sign(data: Uint8Array): Promise<Signature> {
264
+ const challenge = sha256(data);
265
+ const signature = secp256k1.sign(challenge, this._privateKey).toCompactRawBytes();
266
+ return signature as Signature;
267
+ }
268
+ }
269
+
270
+ export default Secp256k1KeyIdentity;
@@ -0,0 +1,4 @@
1
+ {
2
+ "type": "module",
3
+ "sideEffects": false
4
+ }
@@ -1 +1,3 @@
1
- export * from '@dfinity/principal';
1
+ export * from './principal.ts';
2
+ export { getCrc32 } from './utils/getCrc.ts';
3
+ export { base32Encode, base32Decode } from './utils/base32.ts';
@@ -0,0 +1,171 @@
1
+ import { base32Decode, base32Encode } from './utils/base32.ts';
2
+ import { getCrc32 } from './utils/getCrc.ts';
3
+ import { sha224 } from '@noble/hashes/sha2';
4
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
5
+
6
+ export const JSON_KEY_PRINCIPAL = '__principal__';
7
+ const SELF_AUTHENTICATING_SUFFIX = 2;
8
+ const ANONYMOUS_SUFFIX = 4;
9
+
10
+ const MANAGEMENT_CANISTER_PRINCIPAL_TEXT_STR = 'aaaaa-aa';
11
+
12
+ export type JsonnablePrincipal = {
13
+ [JSON_KEY_PRINCIPAL]: string;
14
+ };
15
+
16
+ export class Principal {
17
+ public static anonymous(): Principal {
18
+ return new this(new Uint8Array([ANONYMOUS_SUFFIX]));
19
+ }
20
+
21
+ /**
22
+ * Utility method, returning the principal representing the management canister, decoded from the hex string `'aaaaa-aa'`
23
+ * @returns {Principal} principal of the management canister
24
+ */
25
+ public static managementCanister(): Principal {
26
+ return this.fromText(MANAGEMENT_CANISTER_PRINCIPAL_TEXT_STR);
27
+ }
28
+
29
+ public static selfAuthenticating(publicKey: Uint8Array): Principal {
30
+ const sha = sha224(publicKey);
31
+ return new this(new Uint8Array([...sha, SELF_AUTHENTICATING_SUFFIX]));
32
+ }
33
+
34
+ public static from(other: unknown): Principal {
35
+ if (typeof other === 'string') {
36
+ return Principal.fromText(other);
37
+ } else if (Object.getPrototypeOf(other) === Uint8Array.prototype) {
38
+ return new Principal(other as Uint8Array);
39
+ } else if (Principal.isPrincipal(other)) {
40
+ return new Principal(other._arr);
41
+ }
42
+
43
+ throw new Error(`Impossible to convert ${JSON.stringify(other)} to Principal.`);
44
+ }
45
+
46
+ public static fromHex(hex: string): Principal {
47
+ return new this(hexToBytes(hex));
48
+ }
49
+
50
+ public static fromText(text: string): Principal {
51
+ let maybePrincipal = text;
52
+ // If formatted as JSON string, parse it first
53
+ if (text.includes(JSON_KEY_PRINCIPAL)) {
54
+ const obj = JSON.parse(text);
55
+ if (JSON_KEY_PRINCIPAL in obj) {
56
+ maybePrincipal = obj[JSON_KEY_PRINCIPAL];
57
+ }
58
+ }
59
+
60
+ const canisterIdNoDash = maybePrincipal.toLowerCase().replace(/-/g, '');
61
+
62
+ let arr = base32Decode(canisterIdNoDash);
63
+ arr = arr.slice(4, arr.length);
64
+
65
+ const principal = new this(arr);
66
+ if (principal.toText() !== maybePrincipal) {
67
+ throw new Error(
68
+ `Principal "${principal.toText()}" does not have a valid checksum (original value "${maybePrincipal}" may not be a valid Principal ID).`,
69
+ );
70
+ }
71
+
72
+ return principal;
73
+ }
74
+
75
+ public static fromUint8Array(arr: Uint8Array): Principal {
76
+ return new this(arr);
77
+ }
78
+
79
+ public static isPrincipal(other: unknown): other is Principal {
80
+ return (
81
+ other instanceof Principal ||
82
+ (typeof other === 'object' &&
83
+ other !== null &&
84
+ '_isPrincipal' in other &&
85
+ (other as { _isPrincipal: boolean })['_isPrincipal'] === true &&
86
+ '_arr' in other &&
87
+ (other as { _arr: Uint8Array })['_arr'] instanceof Uint8Array)
88
+ );
89
+ }
90
+
91
+ public readonly _isPrincipal = true;
92
+
93
+ protected constructor(private _arr: Uint8Array) {}
94
+
95
+ public isAnonymous(): boolean {
96
+ return this._arr.byteLength === 1 && this._arr[0] === ANONYMOUS_SUFFIX;
97
+ }
98
+
99
+ public toUint8Array(): Uint8Array {
100
+ return this._arr;
101
+ }
102
+
103
+ public toHex(): string {
104
+ return bytesToHex(this._arr).toUpperCase();
105
+ }
106
+
107
+ public toText(): string {
108
+ const checksumArrayBuf = new ArrayBuffer(4);
109
+ const view = new DataView(checksumArrayBuf);
110
+ view.setUint32(0, getCrc32(this._arr));
111
+ const checksum = new Uint8Array(checksumArrayBuf);
112
+
113
+ const array = new Uint8Array([...checksum, ...this._arr]);
114
+
115
+ const result = base32Encode(array);
116
+ const matches = result.match(/.{1,5}/g);
117
+ if (!matches) {
118
+ // This should only happen if there's no character, which is unreachable.
119
+ throw new Error();
120
+ }
121
+ return matches.join('-');
122
+ }
123
+
124
+ public toString(): string {
125
+ return this.toText();
126
+ }
127
+
128
+ /**
129
+ * Serializes to JSON
130
+ * @returns {JsonnablePrincipal} a JSON object with a single key, {@link JSON_KEY_PRINCIPAL}, whose value is the principal as a string
131
+ */
132
+ public toJSON(): JsonnablePrincipal {
133
+ return { [JSON_KEY_PRINCIPAL]: this.toText() };
134
+ }
135
+
136
+ /**
137
+ * Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification
138
+ * @param {Principal} other - a {@link Principal} to compare
139
+ * @returns {'lt' | 'eq' | 'gt'} `'lt' | 'eq' | 'gt'` a string, representing less than, equal to, or greater than
140
+ */
141
+ public compareTo(other: Principal): 'lt' | 'eq' | 'gt' {
142
+ for (let i = 0; i < Math.min(this._arr.length, other._arr.length); i++) {
143
+ if (this._arr[i] < other._arr[i]) return 'lt';
144
+ else if (this._arr[i] > other._arr[i]) return 'gt';
145
+ }
146
+ // Here, at least one principal is a prefix of the other principal (they could be the same)
147
+ if (this._arr.length < other._arr.length) return 'lt';
148
+ if (this._arr.length > other._arr.length) return 'gt';
149
+ return 'eq';
150
+ }
151
+
152
+ /**
153
+ * Utility method checking whether a provided Principal is less than or equal to the current one using the {@link Principal.compareTo} method
154
+ * @param other a {@link Principal} to compare
155
+ * @returns {boolean} boolean
156
+ */
157
+ public ltEq(other: Principal): boolean {
158
+ const cmp = this.compareTo(other);
159
+ return cmp == 'lt' || cmp == 'eq';
160
+ }
161
+
162
+ /**
163
+ * Utility method checking whether a provided Principal is greater than or equal to the current one using the {@link Principal.compareTo} method
164
+ * @param other a {@link Principal} to compare
165
+ * @returns {boolean} boolean
166
+ */
167
+ public gtEq(other: Principal): boolean {
168
+ const cmp = this.compareTo(other);
169
+ return cmp == 'gt' || cmp == 'eq';
170
+ }
171
+ }