@icp-sdk/core 5.3.1 → 6.0.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 (259) hide show
  1. package/lib/cjs/agent/actor.d.ts +7 -3
  2. package/lib/cjs/agent/actor.d.ts.map +1 -1
  3. package/lib/cjs/agent/actor.js +18 -5
  4. package/lib/cjs/agent/actor.js.map +1 -1
  5. package/lib/cjs/agent/agent/api.d.ts +35 -6
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/api.js.map +1 -1
  8. package/lib/cjs/agent/agent/http/index.d.ts +11 -25
  9. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  10. package/lib/cjs/agent/agent/http/index.js +198 -173
  11. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  12. package/lib/cjs/agent/auth.d.ts +6 -5
  13. package/lib/cjs/agent/auth.d.ts.map +1 -1
  14. package/lib/cjs/agent/auth.js +3 -3
  15. package/lib/cjs/agent/auth.js.map +1 -1
  16. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  17. package/lib/cjs/agent/canisterStatus/index.js +10 -10
  18. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  19. package/lib/cjs/agent/cbor.d.ts +2 -1
  20. package/lib/cjs/agent/cbor.d.ts.map +1 -1
  21. package/lib/cjs/agent/cbor.js +4 -1
  22. package/lib/cjs/agent/cbor.js.map +1 -1
  23. package/lib/cjs/agent/certificate.d.ts +5 -4
  24. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  25. package/lib/cjs/agent/certificate.js +14 -18
  26. package/lib/cjs/agent/certificate.js.map +1 -1
  27. package/lib/cjs/agent/der.d.ts +3 -2
  28. package/lib/cjs/agent/der.d.ts.map +1 -1
  29. package/lib/cjs/agent/der.js.map +1 -1
  30. package/lib/cjs/agent/errors.d.ts +7 -1
  31. package/lib/cjs/agent/errors.d.ts.map +1 -1
  32. package/lib/cjs/agent/errors.js +34 -16
  33. package/lib/cjs/agent/errors.js.map +1 -1
  34. package/lib/cjs/agent/polling/index.d.ts +3 -4
  35. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  36. package/lib/cjs/agent/polling/index.js +17 -10
  37. package/lib/cjs/agent/polling/index.js.map +1 -1
  38. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  39. package/lib/cjs/agent/polling/strategy.js +6 -6
  40. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  41. package/lib/cjs/agent/polling/types.d.ts +3 -4
  42. package/lib/cjs/agent/polling/types.d.ts.map +1 -1
  43. package/lib/cjs/agent/public_key.d.ts +3 -2
  44. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  45. package/lib/cjs/agent/public_key.js +2 -2
  46. package/lib/cjs/agent/public_key.js.map +1 -1
  47. package/lib/cjs/agent/request_id.js +10 -10
  48. package/lib/cjs/agent/request_id.js.map +1 -1
  49. package/lib/cjs/agent/subnetStatus/index.js +7 -7
  50. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  51. package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
  52. package/lib/cjs/agent/utils/bls.js +7 -6
  53. package/lib/cjs/agent/utils/bls.js.map +1 -1
  54. package/lib/cjs/agent/utils/readState.d.ts +2 -2
  55. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  56. package/lib/cjs/agent/utils/readState.js +4 -4
  57. package/lib/cjs/agent/utils/readState.js.map +1 -1
  58. package/lib/cjs/agent/utils/target.d.ts +15 -0
  59. package/lib/cjs/agent/utils/target.d.ts.map +1 -0
  60. package/lib/cjs/agent/utils/target.js +27 -0
  61. package/lib/cjs/agent/utils/target.js.map +1 -0
  62. package/lib/cjs/candid/utils/buffer.d.ts +2 -1
  63. package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
  64. package/lib/cjs/candid/utils/buffer.js +5 -2
  65. package/lib/cjs/candid/utils/buffer.js.map +1 -1
  66. package/lib/cjs/identity/identity/attributes.d.ts +6 -2
  67. package/lib/cjs/identity/identity/attributes.d.ts.map +1 -1
  68. package/lib/cjs/identity/identity/attributes.js +9 -1
  69. package/lib/cjs/identity/identity/attributes.js.map +1 -1
  70. package/lib/cjs/identity/identity/delegation.d.ts +10 -3
  71. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  72. package/lib/cjs/identity/identity/delegation.js +25 -12
  73. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  74. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  75. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  76. package/lib/cjs/identity/identity/ecdsa.js +3 -2
  77. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  78. package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
  79. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  80. package/lib/cjs/identity/identity/ed25519.js +18 -15
  81. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  82. package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
  83. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
  84. package/lib/cjs/identity/identity/webauthn.js +12 -11
  85. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  86. package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
  87. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  88. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  89. package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
  90. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
  91. package/lib/cjs/identity/secp256k1/pem.js +2 -2
  92. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  93. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
  94. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  95. package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
  96. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  97. package/lib/cjs/principal/principal.js +5 -5
  98. package/lib/cjs/principal/principal.js.map +1 -1
  99. package/lib/esm/agent/actor.d.ts +7 -3
  100. package/lib/esm/agent/actor.d.ts.map +1 -1
  101. package/lib/esm/agent/actor.js +18 -5
  102. package/lib/esm/agent/actor.js.map +1 -1
  103. package/lib/esm/agent/agent/api.d.ts +35 -6
  104. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  105. package/lib/esm/agent/agent/api.js.map +1 -1
  106. package/lib/esm/agent/agent/http/index.d.ts +11 -25
  107. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  108. package/lib/esm/agent/agent/http/index.js +191 -166
  109. package/lib/esm/agent/agent/http/index.js.map +1 -1
  110. package/lib/esm/agent/auth.d.ts +6 -5
  111. package/lib/esm/agent/auth.d.ts.map +1 -1
  112. package/lib/esm/agent/auth.js +1 -1
  113. package/lib/esm/agent/auth.js.map +1 -1
  114. package/lib/esm/agent/canister-env/index.js +1 -1
  115. package/lib/esm/agent/canister-env/index.js.map +1 -1
  116. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  117. package/lib/esm/agent/canisterStatus/index.js +2 -2
  118. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  119. package/lib/esm/agent/cbor.d.ts +2 -1
  120. package/lib/esm/agent/cbor.d.ts.map +1 -1
  121. package/lib/esm/agent/cbor.js +4 -1
  122. package/lib/esm/agent/cbor.js.map +1 -1
  123. package/lib/esm/agent/certificate.d.ts +5 -4
  124. package/lib/esm/agent/certificate.d.ts.map +1 -1
  125. package/lib/esm/agent/certificate.js +5 -9
  126. package/lib/esm/agent/certificate.js.map +1 -1
  127. package/lib/esm/agent/der.d.ts +3 -2
  128. package/lib/esm/agent/der.d.ts.map +1 -1
  129. package/lib/esm/agent/der.js.map +1 -1
  130. package/lib/esm/agent/errors.d.ts +7 -1
  131. package/lib/esm/agent/errors.d.ts.map +1 -1
  132. package/lib/esm/agent/errors.js +18 -1
  133. package/lib/esm/agent/errors.js.map +1 -1
  134. package/lib/esm/agent/polling/index.d.ts +3 -4
  135. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  136. package/lib/esm/agent/polling/index.js +15 -8
  137. package/lib/esm/agent/polling/index.js.map +1 -1
  138. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  139. package/lib/esm/agent/polling/strategy.js +6 -6
  140. package/lib/esm/agent/polling/strategy.js.map +1 -1
  141. package/lib/esm/agent/polling/types.d.ts +3 -4
  142. package/lib/esm/agent/polling/types.d.ts.map +1 -1
  143. package/lib/esm/agent/public_key.d.ts +3 -2
  144. package/lib/esm/agent/public_key.d.ts.map +1 -1
  145. package/lib/esm/agent/public_key.js +2 -2
  146. package/lib/esm/agent/public_key.js.map +1 -1
  147. package/lib/esm/agent/request_id.js +2 -2
  148. package/lib/esm/agent/request_id.js.map +1 -1
  149. package/lib/esm/agent/subnetStatus/index.js +1 -1
  150. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  151. package/lib/esm/agent/utils/bls.d.ts.map +1 -1
  152. package/lib/esm/agent/utils/bls.js +7 -6
  153. package/lib/esm/agent/utils/bls.js.map +1 -1
  154. package/lib/esm/agent/utils/readState.d.ts +2 -2
  155. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  156. package/lib/esm/agent/utils/readState.js +1 -1
  157. package/lib/esm/agent/utils/readState.js.map +1 -1
  158. package/lib/esm/agent/utils/target.d.ts +15 -0
  159. package/lib/esm/agent/utils/target.d.ts.map +1 -0
  160. package/lib/esm/agent/utils/target.js +23 -0
  161. package/lib/esm/agent/utils/target.js.map +1 -0
  162. package/lib/esm/candid/utils/buffer.d.ts +2 -1
  163. package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
  164. package/lib/esm/candid/utils/buffer.js +5 -2
  165. package/lib/esm/candid/utils/buffer.js.map +1 -1
  166. package/lib/esm/identity/identity/attributes.d.ts +6 -2
  167. package/lib/esm/identity/identity/attributes.d.ts.map +1 -1
  168. package/lib/esm/identity/identity/attributes.js +9 -1
  169. package/lib/esm/identity/identity/attributes.js.map +1 -1
  170. package/lib/esm/identity/identity/delegation.d.ts +10 -3
  171. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  172. package/lib/esm/identity/identity/delegation.js +22 -9
  173. package/lib/esm/identity/identity/delegation.js.map +1 -1
  174. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  175. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  176. package/lib/esm/identity/identity/ecdsa.js +3 -2
  177. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  178. package/lib/esm/identity/identity/ed25519.d.ts +3 -2
  179. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  180. package/lib/esm/identity/identity/ed25519.js +10 -7
  181. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  182. package/lib/esm/identity/identity/webauthn.d.ts +4 -3
  183. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
  184. package/lib/esm/identity/identity/webauthn.js +8 -7
  185. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  186. package/lib/esm/identity/secp256k1/der.d.ts +2 -1
  187. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  188. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  189. package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
  190. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
  191. package/lib/esm/identity/secp256k1/pem.js +2 -2
  192. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  193. package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
  194. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  195. package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
  196. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  197. package/lib/esm/principal/principal.js +2 -2
  198. package/lib/esm/principal/principal.js.map +1 -1
  199. package/package.json +20 -10
  200. package/src/agent/actor.ts +0 -524
  201. package/src/agent/agent/api.ts +0 -292
  202. package/src/agent/agent/http/expiry.ts +0 -107
  203. package/src/agent/agent/http/index.ts +0 -1727
  204. package/src/agent/agent/http/transforms.ts +0 -54
  205. package/src/agent/agent/http/types.ts +0 -145
  206. package/src/agent/agent/index.ts +0 -4
  207. package/src/agent/auth.ts +0 -147
  208. package/src/agent/canister-env/index.ts +0 -192
  209. package/src/agent/canisterStatus/index.ts +0 -260
  210. package/src/agent/cbor.ts +0 -63
  211. package/src/agent/certificate.ts +0 -1058
  212. package/src/agent/constants.ts +0 -24
  213. package/src/agent/der.ts +0 -191
  214. package/src/agent/errors.ts +0 -962
  215. package/src/agent/fetch_candid.ts +0 -37
  216. package/src/agent/index.ts +0 -130
  217. package/src/agent/observable.ts +0 -49
  218. package/src/agent/polling/backoff.ts +0 -129
  219. package/src/agent/polling/index.ts +0 -238
  220. package/src/agent/polling/strategy.ts +0 -140
  221. package/src/agent/polling/types.ts +0 -28
  222. package/src/agent/public_key.ts +0 -65
  223. package/src/agent/request_id.ts +0 -99
  224. package/src/agent/subnetStatus/index.ts +0 -263
  225. package/src/agent/utils/bls.ts +0 -18
  226. package/src/agent/utils/buffer.ts +0 -64
  227. package/src/agent/utils/certificateReject.ts +0 -25
  228. package/src/agent/utils/createExpirableStore.ts +0 -31
  229. package/src/agent/utils/expirableStore.ts +0 -47
  230. package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
  231. package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
  232. package/src/agent/utils/leb.ts +0 -14
  233. package/src/agent/utils/random.ts +0 -30
  234. package/src/agent/utils/readState.ts +0 -180
  235. package/src/candid/candid-core.ts +0 -274
  236. package/src/candid/candid-ui.ts +0 -281
  237. package/src/candid/idl.ts +0 -2654
  238. package/src/candid/index.ts +0 -35
  239. package/src/candid/types.ts +0 -7
  240. package/src/candid/utils/bigint-math.ts +0 -26
  241. package/src/candid/utils/buffer.ts +0 -221
  242. package/src/candid/utils/hash.ts +0 -31
  243. package/src/candid/utils/leb128.ts +0 -233
  244. package/src/identity/identity/attributes.ts +0 -84
  245. package/src/identity/identity/delegation.ts +0 -411
  246. package/src/identity/identity/ecdsa.ts +0 -162
  247. package/src/identity/identity/ed25519.ts +0 -246
  248. package/src/identity/identity/partial.ts +0 -60
  249. package/src/identity/identity/webauthn.ts +0 -265
  250. package/src/identity/index.ts +0 -44
  251. package/src/identity/secp256k1/der.ts +0 -177
  252. package/src/identity/secp256k1/index.ts +0 -52
  253. package/src/identity/secp256k1/pem.ts +0 -39
  254. package/src/identity/secp256k1/secp256k1.ts +0 -276
  255. package/src/index.ts +0 -3
  256. package/src/principal/index.ts +0 -19
  257. package/src/principal/principal.ts +0 -181
  258. package/src/principal/utils/base32.ts +0 -101
  259. package/src/principal/utils/getCrc.ts +0 -52
@@ -1,37 +0,0 @@
1
- import { Principal } from '#principal';
2
- import * as CanisterStatus from './canisterStatus/index.ts';
3
- import type { IDL } from '#candid';
4
- import { HttpAgent } from './agent/http/index.ts';
5
- import { Actor, type ActorSubclass } from './actor.ts';
6
-
7
- /**
8
- * Retrieves the Candid interface for the specified canister.
9
- * @param canisterId A string corresponding to the canister ID
10
- * @param agent The agent to use for the request (usually an `HttpAgent`)
11
- * @returns Candid source code
12
- */
13
- export async function fetchCandid(canisterId: string, agent?: HttpAgent): Promise<string> {
14
- if (!agent) {
15
- // Create an anonymous `HttpAgent` (adapted from Candid UI)
16
- agent = await HttpAgent.create();
17
- }
18
-
19
- // Attempt to use canister metadata
20
- const status = await CanisterStatus.request({
21
- agent,
22
- canisterId: Principal.fromText(canisterId),
23
- paths: ['candid'],
24
- });
25
- const candid = status.get('candid') as string | undefined;
26
- if (candid) {
27
- return candid;
28
- }
29
-
30
- // Use `__get_candid_interface_tmp_hack` for canisters without Candid metadata
31
- const tmpHackInterface: IDL.InterfaceFactory = ({ IDL }) =>
32
- IDL.Service({
33
- __get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ['query']),
34
- });
35
- const actor: ActorSubclass = Actor.createActor(tmpHackInterface, { agent, canisterId });
36
- return (await actor.__get_candid_interface_tmp_hack()) as string;
37
- }
@@ -1,130 +0,0 @@
1
- /**
2
- * JavaScript and TypeScript module to interact with the [Internet Computer](https://internetcomputer.org/) for Node.js and browser applications.
3
- *
4
- * ## Usage
5
- *
6
- * ```ts
7
- * import { HttpAgent } from '@icp-sdk/core/agent';
8
- *
9
- * const agent = await HttpAgent.create({
10
- * host: 'https://icp-api.io',
11
- * });
12
- *
13
- * agent.call(..) // Send an update call to the canister
14
- * agent.query(..) // Send a query call to the canister
15
- * agent.readState(..) // Read the state of the canister
16
- * ```
17
- *
18
- * ### Using an Agent
19
- *
20
- * The agent is a low-level interface that the Actor uses to encode and decode messages to the Internet Computer. It provides `call`, `query` and `readState` methods to the Actor, as well as a few additional utilities. For the most part, calls through the agent are intended to be structured through an Actor, configured with a canister interface that can be automatically generated from a [Candid](https://github.com/dfinity/candid) interface.
21
- *
22
- * ### Initializing an Actor
23
- *
24
- * The most common use for the agent is to create an actor. This is done by calling the `Actor.createActor` constructor:
25
- *
26
- * ```ts
27
- * Actor.createActor(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<T>
28
- * ```
29
- *
30
- * The `interfaceFactory` is a function that returns a runtime interface that the Actor uses to strucure calls to a canister. The interfaceFactory can be written manually, but it is recommended to use the `dfx generate` command to generate the interface for your project, or to use the `didc` tool to generate the interface for your project.
31
- *
32
- * Actors can also be initialized to include the boundary node http headers, This is done by calling the `Actor.createActor` constructor:
33
- *
34
- * ```ts
35
- * Actor.createActorWithHttpDetails(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<ActorMethodMappedWithHttpDetails<T>>
36
- * ```
37
- *
38
- * ### Inspecting an actor's agent
39
- *
40
- * Use the `Actor.agentOf` method to get the agent of an actor:
41
- *
42
- * ```ts
43
- * const defaultAgent = Actor.agentOf(defaultActor);
44
- * ```
45
- *
46
- * This is useful if you need to replace or invalidate the identity used by an actor's agent.
47
- *
48
- * For example, if you want to replace the identity of an actor's agent with a newly authenticated identity from [Internet Identity](https://identity.internetcomputer.org), you can do so by calling the `Actor.replaceAgent` method:
49
- *
50
- * ```ts
51
- * defaultAgent.replaceIdentity(await authClient.getIdentity());
52
- * ```
53
- *
54
- * ### Tips for using fetch
55
- *
56
- * The agent uses the browser `fetch` API to make calls to the Internet Computer. If you are not using the agent in the browser, you can pass a custom `fetch` implementation to the agent's constructor. This is useful if you want to use a custom fetch implementation, such as one that adds authentication headers to the request. We recommend using the [isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch) package to provide a consistent fetch API across Node.js and the browser. You will also need to provide a `host` option to the agent's constructor, as the agent will not be able to determine the host from the global context.
57
- *
58
- * For example,
59
- *
60
- * ```ts
61
- * import fetch from 'isomorphic-fetch';
62
- * import { HttpAgent } from '@icp-sdk/core/agent';
63
- *
64
- * const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://icp-api.io';
65
- *
66
- * const agent = new HttpAgent({ fetch, host });
67
- * ```
68
- *
69
- * You can also pass `fetchOptions` to the agent's constructor, which will be passed to the `fetch` implementation. This is useful if you want to pass additional options to the `fetch` implementation, such as a custom header.
70
- *
71
- * For example,
72
- *
73
- * ```ts
74
- * import fetch from 'isomorphic-fetch';
75
- * import { HttpAgent } from '@icp-sdk/core/agent';
76
- *
77
- * const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://ic0.app';
78
- *
79
- * const fetchOptions: RequestInit = {
80
- * headers: {
81
- * 'X-Custom-Header': 'value',
82
- * },
83
- * };
84
- *
85
- * const agent = new HttpAgent({ fetch, host, fetchOptions });
86
- * ```
87
- *
88
- * <!-- split here -->
89
- * @module libs/agent/api
90
- */
91
-
92
- export * from './actor.ts';
93
- export * from './agent/index.ts';
94
- export * from './auth.ts';
95
- export * from './certificate.ts';
96
- export {
97
- IC_REQUEST_DOMAIN_SEPARATOR,
98
- IC_RESPONSE_DOMAIN_SEPARATOR,
99
- IC_REQUEST_AUTH_DELEGATION_DOMAIN_SEPARATOR,
100
- IC_STATE_ROOT_DOMAIN_SEPARATOR,
101
- } from './constants.ts';
102
- export * from './der.ts';
103
- export * from './errors.ts';
104
- export * from './fetch_candid.ts';
105
- export * from './observable.ts';
106
- export * from './public_key.ts';
107
- export * from './request_id.ts';
108
- export * from './utils/bls.ts';
109
- export * from './utils/buffer.ts';
110
- export * from './utils/random.ts';
111
- export { type ExpirableStore } from './utils/expirableStore.ts';
112
- export * as polling from './polling/index.ts';
113
- import * as CanisterStatus from './canisterStatus/index.ts';
114
- import * as SubnetStatus from './subnetStatus/index.ts';
115
- /**
116
- * The CanisterStatus utility is used to request structured data directly from the IC public API. This data can be accessed using agent.readState, but CanisterStatus provides a helpful abstraction with some known paths.
117
- *
118
- * You can request a canisters Controllers, ModuleHash, Candid interface, Subnet, or Time, or provide a custom path {@link CanisterStatus.CustomPath} and pass arbitrary buffers for valid paths identified in https://internetcomputer.org/docs/current/references/ic-interface-spec.
119
- *
120
- * The primary method for this namespace is {@link CanisterStatus.request}
121
- */
122
- export { CanisterStatus };
123
- /**
124
- * The SubnetStatus utility is used to request structured data directly from the IC public API. This data can be accessed using agent.readSubnetState, but SubnetStatus provides a helpful abstraction with some known paths.
125
- *
126
- * The primary method for this namespace is {@link SubnetStatus.request}
127
- */
128
- export { SubnetStatus };
129
- export { Cbor, ToCborValue } from './cbor.ts';
130
- export * from './polling/index.ts';
@@ -1,49 +0,0 @@
1
- import type { AgentError } from './errors.ts';
2
-
3
- export type ObserveFunction<T> = (data: T, ...rest: unknown[]) => void;
4
-
5
- export class Observable<T> {
6
- observers: ObserveFunction<T>[];
7
-
8
- constructor() {
9
- this.observers = [];
10
- }
11
-
12
- subscribe(func: ObserveFunction<T>) {
13
- this.observers.push(func);
14
- }
15
-
16
- unsubscribe(func: ObserveFunction<T>) {
17
- this.observers = this.observers.filter(observer => observer !== func);
18
- }
19
-
20
- notify(data: T, ...rest: unknown[]) {
21
- this.observers.forEach(observer => observer(data, ...rest));
22
- }
23
- }
24
-
25
- export type AgentLog =
26
- | {
27
- message: string;
28
- level: 'warn' | 'info';
29
- }
30
- | {
31
- message: string;
32
- level: 'error';
33
- error: AgentError;
34
- };
35
-
36
- export class ObservableLog extends Observable<AgentLog> {
37
- constructor() {
38
- super();
39
- }
40
- print(message: string, ...rest: unknown[]) {
41
- this.notify({ message, level: 'info' }, ...rest);
42
- }
43
- warn(message: string, ...rest: unknown[]) {
44
- this.notify({ message, level: 'warn' }, ...rest);
45
- }
46
- error(message: string, error: AgentError, ...rest: unknown[]) {
47
- this.notify({ message, level: 'error', error }, ...rest);
48
- }
49
- }
@@ -1,129 +0,0 @@
1
- const RANDOMIZATION_FACTOR = 0.5;
2
- const MULTIPLIER = 1.5;
3
- const INITIAL_INTERVAL_MSEC = 500;
4
- const MAX_INTERVAL_MSEC = 60_000;
5
- const MAX_ELAPSED_TIME_MSEC = 900_000;
6
- const MAX_ITERATIONS = 10;
7
-
8
- export interface BackoffStrategy {
9
- next: () => number | null;
10
- currentInterval?: number;
11
- count?: number;
12
- ellapsedTimeInMsec?: number;
13
- }
14
-
15
- export interface BackoffStrategyArgs {
16
- maxIterations?: number;
17
- maxElapsedTime?: number;
18
- }
19
-
20
- export type BackoffStrategyFactory = (args?: BackoffStrategyArgs) => BackoffStrategy;
21
-
22
- // export type BackoffStrategyGenerator = Generator<number, void, unknown>;
23
-
24
- export interface ExponentialBackoffOptions {
25
- initialInterval?: number;
26
- randomizationFactor?: number;
27
- multiplier?: number;
28
- maxInterval?: number;
29
- maxElapsedTime?: number;
30
- maxIterations?: number;
31
- date?: DateConstructor;
32
- }
33
-
34
- /**
35
- * Exponential backoff strategy.
36
- */
37
- export class ExponentialBackoff {
38
- #currentInterval: number;
39
- #randomizationFactor: number;
40
- #multiplier: number;
41
- #maxInterval: number;
42
- #startTime: number;
43
- #maxElapsedTime: number;
44
- #maxIterations: number;
45
- #date: DateConstructor;
46
- #count = 0;
47
-
48
- static default = {
49
- initialInterval: INITIAL_INTERVAL_MSEC,
50
- randomizationFactor: RANDOMIZATION_FACTOR,
51
- multiplier: MULTIPLIER,
52
- maxInterval: MAX_INTERVAL_MSEC,
53
- // 1 minute
54
- maxElapsedTime: MAX_ELAPSED_TIME_MSEC,
55
- maxIterations: MAX_ITERATIONS,
56
- date: Date,
57
- };
58
-
59
- constructor(options: ExponentialBackoffOptions = ExponentialBackoff.default) {
60
- const {
61
- initialInterval = INITIAL_INTERVAL_MSEC,
62
- randomizationFactor = RANDOMIZATION_FACTOR,
63
- multiplier = MULTIPLIER,
64
- maxInterval = MAX_INTERVAL_MSEC,
65
- maxElapsedTime = MAX_ELAPSED_TIME_MSEC,
66
- maxIterations = MAX_ITERATIONS,
67
- date = Date,
68
- } = options;
69
- this.#currentInterval = initialInterval;
70
- this.#randomizationFactor = randomizationFactor;
71
- this.#multiplier = multiplier;
72
- this.#maxInterval = maxInterval;
73
- this.#date = date;
74
- this.#startTime = date.now();
75
- this.#maxElapsedTime = maxElapsedTime;
76
- this.#maxIterations = maxIterations;
77
- }
78
-
79
- get ellapsedTimeInMsec() {
80
- return this.#date.now() - this.#startTime;
81
- }
82
-
83
- get currentInterval() {
84
- return this.#currentInterval;
85
- }
86
-
87
- get count() {
88
- return this.#count;
89
- }
90
-
91
- get randomValueFromInterval() {
92
- const delta = this.#randomizationFactor * this.#currentInterval;
93
- const min = this.#currentInterval - delta;
94
- const max = this.#currentInterval + delta;
95
- return Math.random() * (max - min) + min;
96
- }
97
-
98
- public incrementCurrentInterval() {
99
- this.#currentInterval = Math.min(this.#currentInterval * this.#multiplier, this.#maxInterval);
100
- this.#count++;
101
-
102
- return this.#currentInterval;
103
- }
104
-
105
- public next() {
106
- if (this.ellapsedTimeInMsec >= this.#maxElapsedTime || this.#count >= this.#maxIterations) {
107
- return null;
108
- }
109
- this.incrementCurrentInterval();
110
- return this.randomValueFromInterval;
111
- }
112
- }
113
- /**
114
- * Utility function to create an exponential backoff iterator.
115
- * @param options - for the exponential backoff
116
- * @returns an iterator that yields the next delay in the exponential backoff
117
- * @yields {number} the next delay in the exponential backoff
118
- */
119
- export function* exponentialBackoff(
120
- options: ExponentialBackoffOptions = ExponentialBackoff.default,
121
- ) {
122
- const backoff = new ExponentialBackoff(options);
123
-
124
- let next = backoff.next();
125
- while (next) {
126
- yield next;
127
- next = backoff.next();
128
- }
129
- }
@@ -1,238 +0,0 @@
1
- import type { RequestId } from '../request_id.ts';
2
- import {
3
- type CreateCertificateOptions,
4
- Certificate,
5
- lookupResultToBuffer,
6
- } from '../certificate.ts';
7
- import { readCertifiedReject } from '../utils/certificateReject.ts';
8
- import type { Agent, ReadStateResponse } from '../agent/api.ts';
9
- import type { Principal } from '#principal';
10
- import {
11
- ExternalError,
12
- InputError,
13
- InvalidReadStateRequestErrorCode,
14
- MissingRootKeyErrorCode,
15
- RejectError,
16
- RequestStatusDoneNoReplyErrorCode,
17
- UnknownError,
18
- UNREACHABLE_ERROR,
19
- } from '../errors.ts';
20
-
21
- export * as strategy from './strategy.ts';
22
- import { defaultStrategy } from './strategy.ts';
23
- import type { PollStrategy, PollForResponseResult } from './types.ts';
24
- import { ReadRequestType, type ReadStateRequest } from '../agent/http/types.ts';
25
- import { RequestStatusResponseStatus } from '../agent/http/types.ts';
26
- import { utf8ToBytes } from '@noble/hashes/utils';
27
- export { defaultStrategy } from './strategy.ts';
28
- export type { PollStrategy, PollForResponseResult } from './types.ts';
29
-
30
- interface SignedReadStateRequestWithExpiry extends ReadStateRequest {
31
- body: {
32
- content: Pick<ReadStateRequest, 'request_type' | 'ingress_expiry'>;
33
- };
34
- }
35
-
36
- /**
37
- * Options for controlling polling behavior
38
- */
39
- export interface PollingOptions {
40
- /**
41
- * A polling strategy that dictates how much and often we should poll the
42
- * read_state endpoint to get the result of an update call.
43
- * @default {@link defaultStrategy}
44
- */
45
- strategy?: PollStrategy;
46
-
47
- /**
48
- * Whether to reuse the same signed request for polling or create a new unsigned request each time.
49
- * @default false
50
- */
51
- preSignReadStateRequest?: boolean;
52
-
53
- /**
54
- * Optional replacement function that verifies the BLS signature of a certificate.
55
- */
56
- blsVerify?: CreateCertificateOptions['blsVerify'];
57
-
58
- /**
59
- * The request to use for polling. If not provided, a new request will be created.
60
- * This is only used if `preSignReadStateRequest` is set to false.
61
- */
62
- request?: ReadStateRequest;
63
- }
64
-
65
- export const DEFAULT_POLLING_OPTIONS: PollingOptions = {
66
- preSignReadStateRequest: false,
67
- };
68
-
69
- /**
70
- * Check if an object has a property
71
- * @param value the object that might have the property
72
- * @param property the key of property we're looking for
73
- */
74
- function hasProperty<O extends object, P extends string>(
75
- value: O,
76
- property: P,
77
- ): value is O & Record<P, unknown> {
78
- return Object.prototype.hasOwnProperty.call(value, property);
79
- }
80
-
81
- function isObjectWithProperty<O extends object, P extends string>(
82
- value: unknown,
83
- property: P,
84
- ): value is O & Record<P, unknown> {
85
- return value !== null && typeof value === 'object' && hasProperty(value, property);
86
- }
87
-
88
- function hasFunction<O extends object, P extends string>(
89
- value: O,
90
- property: P,
91
- ): value is O & Record<P, (...args: unknown[]) => unknown> {
92
- return hasProperty(value, property) && typeof value[property] === 'function';
93
- }
94
-
95
- /**
96
- * Check if value is a signed read state request with expiry
97
- * @param value to check
98
- */
99
- function isSignedReadStateRequestWithExpiry(
100
- value: unknown,
101
- ): value is SignedReadStateRequestWithExpiry {
102
- return (
103
- isObjectWithProperty(value, 'body') &&
104
- isObjectWithProperty(value.body, 'content') &&
105
- (value.body.content as { request_type: ReadRequestType }).request_type ===
106
- ReadRequestType.ReadState &&
107
- isObjectWithProperty(value.body.content, 'ingress_expiry') &&
108
- typeof value.body.content.ingress_expiry === 'object' &&
109
- value.body.content.ingress_expiry !== null &&
110
- hasFunction(value.body.content.ingress_expiry, 'toHash')
111
- );
112
- }
113
-
114
- /**
115
- * Polls the IC to check the status of the given request then
116
- * returns the response bytes once the request has been processed.
117
- * @param agent The agent to use to poll read_state.
118
- * @param canisterId The effective canister ID.
119
- * @param requestId The Request ID to poll status for.
120
- * @param options polling options to control behavior
121
- * @returns The certificate, reply bytes, and raw certificate bytes for the request.
122
- * @throws {ExternalError} If the agent's root key is not available.
123
- * @throws {RejectError} If the request was rejected by the canister.
124
- * @throws {UnknownError} If the request reached `done` status without a reply.
125
- */
126
- export async function pollForResponse(
127
- agent: Agent,
128
- canisterId: Principal,
129
- requestId: RequestId,
130
- options: PollingOptions = {},
131
- ): Promise<PollForResponseResult> {
132
- const path = [utf8ToBytes('request_status'), requestId];
133
-
134
- let state: ReadStateResponse;
135
- let currentRequest: ReadStateRequest | undefined;
136
- const preSignReadStateRequest = options.preSignReadStateRequest ?? false;
137
- if (preSignReadStateRequest) {
138
- // If preSignReadStateRequest is true, we need to create a new request
139
- currentRequest = await constructRequest({
140
- paths: [path],
141
- agent,
142
- pollingOptions: options,
143
- });
144
- state = await agent.readState(canisterId, { paths: [path] }, undefined, currentRequest);
145
- } else {
146
- // If preSignReadStateRequest is false, we use the default strategy and sign the request each time
147
- state = await agent.readState(canisterId, { paths: [path] });
148
- }
149
-
150
- if (agent.rootKey == null) {
151
- throw ExternalError.fromCode(new MissingRootKeyErrorCode());
152
- }
153
- const cert = await Certificate.create({
154
- certificate: state.certificate,
155
- rootKey: agent.rootKey,
156
- principal: { canisterId },
157
- blsVerify: options.blsVerify,
158
- agent,
159
- });
160
-
161
- const maybeBuf = lookupResultToBuffer(cert.lookup_path([...path, utf8ToBytes('status')]));
162
- let status;
163
- if (typeof maybeBuf === 'undefined') {
164
- // Missing requestId means we need to wait
165
- status = RequestStatusResponseStatus.Unknown;
166
- } else {
167
- status = new TextDecoder().decode(maybeBuf);
168
- }
169
-
170
- switch (status) {
171
- case RequestStatusResponseStatus.Replied: {
172
- return {
173
- reply: lookupResultToBuffer(cert.lookup_path([...path, 'reply']))!,
174
- certificate: cert,
175
- rawCertificate: state.certificate,
176
- };
177
- }
178
-
179
- case RequestStatusResponseStatus.Received:
180
- case RequestStatusResponseStatus.Unknown:
181
- case RequestStatusResponseStatus.Processing: {
182
- // Execute the polling strategy, then retry.
183
- const strategy = options.strategy ?? defaultStrategy();
184
- await strategy(canisterId, requestId, status);
185
-
186
- return pollForResponse(agent, canisterId, requestId, {
187
- ...options,
188
- // Pass over either the strategy already provided or the new one created above
189
- strategy,
190
- request: currentRequest,
191
- });
192
- }
193
-
194
- case RequestStatusResponseStatus.Rejected: {
195
- throw RejectError.fromCode(readCertifiedReject(cert, path, requestId));
196
- }
197
-
198
- case RequestStatusResponseStatus.Done:
199
- // This is _technically_ not an error, but we still didn't see the `Replied` status so
200
- // we don't know the result and cannot decode it.
201
- throw UnknownError.fromCode(new RequestStatusDoneNoReplyErrorCode(requestId));
202
- }
203
- throw UNREACHABLE_ERROR;
204
- }
205
-
206
- // Determine if we should reuse the read state request or create a new one
207
- // based on the options provided.
208
-
209
- /**
210
- * Constructs a read state request for the given paths.
211
- * If the request is already signed and has an expiry, it will be returned as is.
212
- * Otherwise, a new request will be created.
213
- * @param options The options to use for creating the request.
214
- * @param options.paths The paths to read from.
215
- * @param options.agent The agent to use to create the request.
216
- * @param options.pollingOptions The options to use for creating the request.
217
- * @returns The read state request.
218
- */
219
- export async function constructRequest(options: {
220
- paths: Uint8Array[][];
221
- agent: Agent;
222
- pollingOptions: PollingOptions;
223
- }): Promise<ReadStateRequest> {
224
- const { paths, agent, pollingOptions } = options;
225
- if (pollingOptions.request && isSignedReadStateRequestWithExpiry(pollingOptions.request)) {
226
- return pollingOptions.request;
227
- }
228
- const request = await agent.createReadStateRequest?.(
229
- {
230
- paths,
231
- },
232
- undefined,
233
- );
234
- if (!isSignedReadStateRequestWithExpiry(request)) {
235
- throw InputError.fromCode(new InvalidReadStateRequestErrorCode(request));
236
- }
237
- return request;
238
- }