@icp-sdk/core 5.4.0 → 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 (251) 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 +19 -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 +8 -11
  9. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  10. package/lib/cjs/agent/agent/http/index.js +190 -166
  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/delegation.d.ts +10 -3
  67. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  68. package/lib/cjs/identity/identity/delegation.js +25 -12
  69. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  70. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  71. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  72. package/lib/cjs/identity/identity/ecdsa.js +3 -2
  73. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  74. package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
  75. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  76. package/lib/cjs/identity/identity/ed25519.js +18 -15
  77. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  78. package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
  79. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
  80. package/lib/cjs/identity/identity/webauthn.js +12 -11
  81. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  82. package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
  83. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  84. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  85. package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
  86. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
  87. package/lib/cjs/identity/secp256k1/pem.js +2 -2
  88. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  89. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
  90. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  91. package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
  92. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  93. package/lib/cjs/principal/principal.js +5 -5
  94. package/lib/cjs/principal/principal.js.map +1 -1
  95. package/lib/esm/agent/actor.d.ts +7 -3
  96. package/lib/esm/agent/actor.d.ts.map +1 -1
  97. package/lib/esm/agent/actor.js +18 -5
  98. package/lib/esm/agent/actor.js.map +1 -1
  99. package/lib/esm/agent/agent/api.d.ts +19 -6
  100. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  101. package/lib/esm/agent/agent/api.js.map +1 -1
  102. package/lib/esm/agent/agent/http/index.d.ts +8 -11
  103. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  104. package/lib/esm/agent/agent/http/index.js +183 -159
  105. package/lib/esm/agent/agent/http/index.js.map +1 -1
  106. package/lib/esm/agent/auth.d.ts +6 -5
  107. package/lib/esm/agent/auth.d.ts.map +1 -1
  108. package/lib/esm/agent/auth.js +1 -1
  109. package/lib/esm/agent/auth.js.map +1 -1
  110. package/lib/esm/agent/canister-env/index.js +1 -1
  111. package/lib/esm/agent/canister-env/index.js.map +1 -1
  112. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  113. package/lib/esm/agent/canisterStatus/index.js +2 -2
  114. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  115. package/lib/esm/agent/cbor.d.ts +2 -1
  116. package/lib/esm/agent/cbor.d.ts.map +1 -1
  117. package/lib/esm/agent/cbor.js +4 -1
  118. package/lib/esm/agent/cbor.js.map +1 -1
  119. package/lib/esm/agent/certificate.d.ts +5 -4
  120. package/lib/esm/agent/certificate.d.ts.map +1 -1
  121. package/lib/esm/agent/certificate.js +5 -9
  122. package/lib/esm/agent/certificate.js.map +1 -1
  123. package/lib/esm/agent/der.d.ts +3 -2
  124. package/lib/esm/agent/der.d.ts.map +1 -1
  125. package/lib/esm/agent/der.js.map +1 -1
  126. package/lib/esm/agent/errors.d.ts +7 -1
  127. package/lib/esm/agent/errors.d.ts.map +1 -1
  128. package/lib/esm/agent/errors.js +18 -1
  129. package/lib/esm/agent/errors.js.map +1 -1
  130. package/lib/esm/agent/polling/index.d.ts +3 -4
  131. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  132. package/lib/esm/agent/polling/index.js +15 -8
  133. package/lib/esm/agent/polling/index.js.map +1 -1
  134. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  135. package/lib/esm/agent/polling/strategy.js +6 -6
  136. package/lib/esm/agent/polling/strategy.js.map +1 -1
  137. package/lib/esm/agent/polling/types.d.ts +3 -4
  138. package/lib/esm/agent/polling/types.d.ts.map +1 -1
  139. package/lib/esm/agent/public_key.d.ts +3 -2
  140. package/lib/esm/agent/public_key.d.ts.map +1 -1
  141. package/lib/esm/agent/public_key.js +2 -2
  142. package/lib/esm/agent/public_key.js.map +1 -1
  143. package/lib/esm/agent/request_id.js +2 -2
  144. package/lib/esm/agent/request_id.js.map +1 -1
  145. package/lib/esm/agent/subnetStatus/index.js +1 -1
  146. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  147. package/lib/esm/agent/utils/bls.d.ts.map +1 -1
  148. package/lib/esm/agent/utils/bls.js +7 -6
  149. package/lib/esm/agent/utils/bls.js.map +1 -1
  150. package/lib/esm/agent/utils/readState.d.ts +2 -2
  151. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  152. package/lib/esm/agent/utils/readState.js +1 -1
  153. package/lib/esm/agent/utils/readState.js.map +1 -1
  154. package/lib/esm/agent/utils/target.d.ts +15 -0
  155. package/lib/esm/agent/utils/target.d.ts.map +1 -0
  156. package/lib/esm/agent/utils/target.js +23 -0
  157. package/lib/esm/agent/utils/target.js.map +1 -0
  158. package/lib/esm/candid/utils/buffer.d.ts +2 -1
  159. package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
  160. package/lib/esm/candid/utils/buffer.js +5 -2
  161. package/lib/esm/candid/utils/buffer.js.map +1 -1
  162. package/lib/esm/identity/identity/delegation.d.ts +10 -3
  163. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  164. package/lib/esm/identity/identity/delegation.js +22 -9
  165. package/lib/esm/identity/identity/delegation.js.map +1 -1
  166. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  167. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  168. package/lib/esm/identity/identity/ecdsa.js +3 -2
  169. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  170. package/lib/esm/identity/identity/ed25519.d.ts +3 -2
  171. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  172. package/lib/esm/identity/identity/ed25519.js +10 -7
  173. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  174. package/lib/esm/identity/identity/webauthn.d.ts +4 -3
  175. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
  176. package/lib/esm/identity/identity/webauthn.js +8 -7
  177. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  178. package/lib/esm/identity/secp256k1/der.d.ts +2 -1
  179. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  180. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  181. package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
  182. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
  183. package/lib/esm/identity/secp256k1/pem.js +2 -2
  184. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  185. package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
  186. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  187. package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
  188. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  189. package/lib/esm/principal/principal.js +2 -2
  190. package/lib/esm/principal/principal.js.map +1 -1
  191. package/package.json +20 -10
  192. package/src/agent/actor.ts +0 -524
  193. package/src/agent/agent/api.ts +0 -310
  194. package/src/agent/agent/http/expiry.ts +0 -107
  195. package/src/agent/agent/http/index.ts +0 -1724
  196. package/src/agent/agent/http/transforms.ts +0 -54
  197. package/src/agent/agent/http/types.ts +0 -145
  198. package/src/agent/agent/index.ts +0 -4
  199. package/src/agent/auth.ts +0 -147
  200. package/src/agent/canister-env/index.ts +0 -192
  201. package/src/agent/canisterStatus/index.ts +0 -260
  202. package/src/agent/cbor.ts +0 -63
  203. package/src/agent/certificate.ts +0 -1058
  204. package/src/agent/constants.ts +0 -24
  205. package/src/agent/der.ts +0 -191
  206. package/src/agent/errors.ts +0 -962
  207. package/src/agent/fetch_candid.ts +0 -37
  208. package/src/agent/index.ts +0 -130
  209. package/src/agent/observable.ts +0 -49
  210. package/src/agent/polling/backoff.ts +0 -129
  211. package/src/agent/polling/index.ts +0 -238
  212. package/src/agent/polling/strategy.ts +0 -140
  213. package/src/agent/polling/types.ts +0 -28
  214. package/src/agent/public_key.ts +0 -65
  215. package/src/agent/request_id.ts +0 -99
  216. package/src/agent/subnetStatus/index.ts +0 -263
  217. package/src/agent/utils/bls.ts +0 -18
  218. package/src/agent/utils/buffer.ts +0 -64
  219. package/src/agent/utils/certificateReject.ts +0 -25
  220. package/src/agent/utils/createExpirableStore.ts +0 -31
  221. package/src/agent/utils/expirableStore.ts +0 -47
  222. package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
  223. package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
  224. package/src/agent/utils/leb.ts +0 -14
  225. package/src/agent/utils/random.ts +0 -30
  226. package/src/agent/utils/readState.ts +0 -180
  227. package/src/candid/candid-core.ts +0 -274
  228. package/src/candid/candid-ui.ts +0 -281
  229. package/src/candid/idl.ts +0 -2654
  230. package/src/candid/index.ts +0 -35
  231. package/src/candid/types.ts +0 -7
  232. package/src/candid/utils/bigint-math.ts +0 -26
  233. package/src/candid/utils/buffer.ts +0 -221
  234. package/src/candid/utils/hash.ts +0 -31
  235. package/src/candid/utils/leb128.ts +0 -233
  236. package/src/identity/identity/attributes.ts +0 -91
  237. package/src/identity/identity/delegation.ts +0 -411
  238. package/src/identity/identity/ecdsa.ts +0 -162
  239. package/src/identity/identity/ed25519.ts +0 -246
  240. package/src/identity/identity/partial.ts +0 -60
  241. package/src/identity/identity/webauthn.ts +0 -265
  242. package/src/identity/index.ts +0 -44
  243. package/src/identity/secp256k1/der.ts +0 -177
  244. package/src/identity/secp256k1/index.ts +0 -52
  245. package/src/identity/secp256k1/pem.ts +0 -39
  246. package/src/identity/secp256k1/secp256k1.ts +0 -276
  247. package/src/index.ts +0 -3
  248. package/src/principal/index.ts +0 -19
  249. package/src/principal/principal.ts +0 -181
  250. package/src/principal/utils/base32.ts +0 -101
  251. package/src/principal/utils/getCrc.ts +0 -52
@@ -1,524 +0,0 @@
1
- import {
2
- type Agent,
3
- type UpdateResult,
4
- type HttpDetailsResponse,
5
- QueryResponseStatus,
6
- } from './agent/index.ts';
7
- import {
8
- InputError,
9
- MissingCanisterIdErrorCode,
10
- RejectError,
11
- UncertifiedRejectErrorCode,
12
- } from './errors.ts';
13
- import { IDL } from '#candid';
14
- import { type PollingOptions, DEFAULT_POLLING_OPTIONS } from './polling/index.ts';
15
- import { Principal } from '#principal';
16
- import type { Certificate, CreateCertificateOptions } from './certificate.ts';
17
- import { HttpAgent } from './agent/http/index.ts';
18
-
19
- /**
20
- * Controls how query and composite_query methods are executed.
21
- *
22
- * - `'query'` — standard non-replicated query call (default).
23
- * - `'update'` — send query methods as update calls that go through consensus.
24
- */
25
- export type QueryStrategy = 'query' | 'update';
26
-
27
- /**
28
- * Configuration to make calls to the Replica.
29
- */
30
- export interface CallConfig {
31
- /**
32
- * An agent to use in this call, otherwise the actor or call will try to discover the
33
- * agent to use.
34
- */
35
- agent?: Agent;
36
-
37
- /**
38
- * Options for controlling polling behavior.
39
- */
40
- pollingOptions?: PollingOptions;
41
-
42
- /**
43
- * The canister ID of this Actor.
44
- */
45
- canisterId?: string | Principal;
46
-
47
- /**
48
- * The effective canister ID.
49
- */
50
- effectiveCanisterId?: Principal;
51
-
52
- /**
53
- * The nonce to use for this call. This is used to prevent replay attacks.
54
- */
55
- nonce?: Uint8Array;
56
- }
57
-
58
- /**
59
- * Configuration that can be passed to customize the Actor behavior.
60
- */
61
- export interface ActorConfig extends Pick<CallConfig, 'agent' | 'effectiveCanisterId'> {
62
- /**
63
- * The Canister ID of this Actor. This is required for an Actor.
64
- */
65
- canisterId: string | Principal;
66
-
67
- /**
68
- * An override function for update calls' CallConfig. This will be called on every calls.
69
- */
70
- callTransform?(
71
- methodName: string,
72
- args: unknown[],
73
- callConfig: CallConfig,
74
- ): Partial<CallConfig> | void;
75
-
76
- /**
77
- * An override function for query calls' CallConfig. This will be called on every query.
78
- */
79
- queryTransform?(
80
- methodName: string,
81
- args: unknown[],
82
- callConfig: CallConfig,
83
- ): Partial<CallConfig> | void;
84
-
85
- /**
86
- * Polyfill for BLS Certificate verification in case wasm is not supported
87
- */
88
- blsVerify?: CreateCertificateOptions['blsVerify'];
89
-
90
- /**
91
- * Polling options to use when making update calls. This will override the default DEFAULT_POLLING_OPTIONS.
92
- */
93
- pollingOptions?: PollingOptions;
94
-
95
- /**
96
- * Controls how query and composite_query methods are executed.
97
- * - `'query'` (default) — standard non-replicated query call.
98
- * - `'update'` — send query methods as update calls that go through consensus.
99
- * @default 'query'
100
- */
101
- queryStrategy?: QueryStrategy;
102
- }
103
-
104
- // TODO: move this to proper typing when Candid support TypeScript.
105
- /**
106
- * A subclass of an actor. Actor class itself is meant to be a based class.
107
- */
108
- export type ActorSubclass<T = Record<string, ActorMethod>> = Actor & T;
109
-
110
- /**
111
- * An actor method type, defined for each methods of the actor service.
112
- */
113
- export interface ActorMethod<Args extends unknown[] = unknown[], Ret = unknown> {
114
- (...args: Args): Promise<Ret>;
115
-
116
- withOptions(options: CallConfig): (...args: Args) => Promise<Ret>;
117
- }
118
-
119
- /**
120
- * An actor method type, defined for each methods of the actor service.
121
- */
122
- export interface ActorMethodWithHttpDetails<
123
- Args extends unknown[] = unknown[],
124
- Ret = unknown,
125
- > extends ActorMethod {
126
- (...args: Args): Promise<{ httpDetails: HttpDetailsResponse; result: Ret }>;
127
- }
128
-
129
- /**
130
- * An actor method type, defined for each methods of the actor service.
131
- */
132
- export interface ActorMethodExtended<
133
- Args extends unknown[] = unknown[],
134
- Ret = unknown,
135
- > extends ActorMethod {
136
- (...args: Args): Promise<{
137
- certificate?: Certificate;
138
- httpDetails?: HttpDetailsResponse;
139
- result: Ret;
140
- }>;
141
- }
142
-
143
- export type FunctionWithArgsAndReturn<Args extends unknown[] = unknown[], Ret = unknown> = (
144
- ...args: Args
145
- ) => Ret;
146
-
147
- // Update all entries of T with the extra information from ActorMethodWithInfo
148
- export type ActorMethodMappedWithHttpDetails<T> = {
149
- [K in keyof T]: T[K] extends FunctionWithArgsAndReturn<infer Args, infer Ret>
150
- ? ActorMethodWithHttpDetails<Args, Ret>
151
- : never;
152
- };
153
-
154
- // Update all entries of T with the extra information from ActorMethodWithInfo
155
- export type ActorMethodMappedExtended<T> = {
156
- [K in keyof T]: T[K] extends FunctionWithArgsAndReturn<infer Args, infer Ret>
157
- ? ActorMethodExtended<Args, Ret>
158
- : never;
159
- };
160
-
161
- /**
162
- * Internal metadata for actors. It's an enhanced version of ActorConfig with
163
- * some fields marked as required (as they are defaulted) and canisterId as
164
- * a Principal type.
165
- */
166
- interface ActorMetadata {
167
- service: IDL.ServiceClass;
168
- agent?: Agent;
169
- config: ActorConfig;
170
- }
171
-
172
- const metadataSymbol = Symbol.for('ic-agent-metadata');
173
-
174
- export interface CreateActorClassOpts {
175
- httpDetails?: boolean;
176
- certificate?: boolean;
177
- }
178
-
179
- /**
180
- * An actor base class. An actor is an object containing only functions that will
181
- * return a promise. These functions are derived from the IDL definition.
182
- */
183
- export class Actor {
184
- /**
185
- * Get the Agent class this Actor would call, or undefined if the Actor would use
186
- * the default agent (global.ic.agent).
187
- * @param actor The actor to get the agent of.
188
- */
189
- public static agentOf(actor: Actor): Agent | undefined {
190
- return actor[metadataSymbol].config.agent;
191
- }
192
-
193
- /**
194
- * Get the interface of an actor, in the form of an instance of a Service.
195
- * @param actor The actor to get the interface of.
196
- */
197
- public static interfaceOf(actor: Actor): IDL.ServiceClass {
198
- return actor[metadataSymbol].service;
199
- }
200
-
201
- public static canisterIdOf(actor: Actor): Principal {
202
- return Principal.from(actor[metadataSymbol].config.canisterId);
203
- }
204
-
205
- public static createActorClass(
206
- interfaceFactory: IDL.InterfaceFactory,
207
- options?: CreateActorClassOpts,
208
- ): ActorConstructor {
209
- const service = interfaceFactory({ IDL });
210
-
211
- class CanisterActor extends Actor {
212
- [x: string]: ActorMethod;
213
-
214
- constructor(config: ActorConfig) {
215
- if (!config.canisterId) {
216
- throw InputError.fromCode(new MissingCanisterIdErrorCode(config.canisterId));
217
- }
218
- const canisterId =
219
- typeof config.canisterId === 'string'
220
- ? Principal.fromText(config.canisterId)
221
- : config.canisterId;
222
-
223
- super({
224
- config: {
225
- ...DEFAULT_ACTOR_CONFIG,
226
- ...config,
227
- canisterId,
228
- },
229
- service,
230
- });
231
-
232
- for (const [methodName, func] of service._fields) {
233
- if (options?.httpDetails) {
234
- func.annotations.push(ACTOR_METHOD_WITH_HTTP_DETAILS);
235
- }
236
- if (options?.certificate) {
237
- func.annotations.push(ACTOR_METHOD_WITH_CERTIFICATE);
238
- }
239
-
240
- this[methodName] = _createActorMethod(this, methodName, func, config.blsVerify);
241
- }
242
- }
243
- }
244
-
245
- return CanisterActor;
246
- }
247
-
248
- /**
249
- * Creates an actor with the given interface factory and configuration.
250
- *
251
- * The [`@icp-sdk/bindgen`](https://js.icp.build/bindgen/) package can be used to generate the interface factory for your canister.
252
- * @param interfaceFactory - the interface factory for the actor, typically generated by the [`@icp-sdk/bindgen`](https://js.icp.build/bindgen/) package
253
- * @param configuration - the configuration for the actor
254
- * @returns an actor with the given interface factory and configuration
255
- * @see The {@link https://js.icp.build/core/latest/canister-environment/ | Canister Environment Guide} for more details on how to configure an actor using the canister environment.
256
- * @example
257
- * Using the interface factory generated by the [`@icp-sdk/bindgen`](https://js.icp.build/bindgen/) package:
258
- * ```ts
259
- * import { Actor, HttpAgent } from '@icp-sdk/core/agent';
260
- * import { Principal } from '@icp-sdk/core/principal';
261
- * import { idlFactory } from './api/declarations/hello-world.did';
262
- *
263
- * // For a convenient way to get the canister ID,
264
- * // see the https://js.icp.build/core/latest/canister-environment/ guide.
265
- * const canisterId = Principal.fromText('rrkah-fqaaa-aaaaa-aaaaq-cai');
266
- *
267
- * const agent = await HttpAgent.create({
268
- * host: 'https://icp-api.io',
269
- * });
270
- *
271
- * const actor = Actor.createActor(idlFactory, {
272
- * agent,
273
- * canisterId,
274
- * });
275
- *
276
- * const response = await actor.greet('world');
277
- * console.log(response);
278
- * ```
279
- * @example
280
- * Using the `createActor` wrapper function generated by the [`@icp-sdk/bindgen`](https://js.icp.build/bindgen/) package:
281
- * ```ts
282
- * import { HttpAgent } from '@icp-sdk/core/agent';
283
- * import { Principal } from '@icp-sdk/core/principal';
284
- * import { createActor } from './api/hello-world';
285
- *
286
- * // For a convenient way to get the canister ID,
287
- * // see the https://js.icp.build/core/latest/canister-environment/ guide.
288
- * const canisterId = Principal.fromText('rrkah-fqaaa-aaaaa-aaaaq-cai');
289
- *
290
- * const agent = await HttpAgent.create({
291
- * host: 'https://icp-api.io',
292
- * });
293
- *
294
- * const actor = createActor(canisterId, {
295
- * agent,
296
- * });
297
- *
298
- * const response = await actor.greet('world');
299
- * console.log(response);
300
- * ```
301
- */
302
- public static createActor<T = Record<string, ActorMethod>>(
303
- interfaceFactory: IDL.InterfaceFactory,
304
- configuration: ActorConfig,
305
- ): ActorSubclass<T> {
306
- if (!configuration.canisterId) {
307
- throw InputError.fromCode(new MissingCanisterIdErrorCode(configuration.canisterId));
308
- }
309
- return new (this.createActorClass(interfaceFactory))(
310
- configuration,
311
- ) as unknown as ActorSubclass<T>;
312
- }
313
-
314
- /**
315
- * Returns an actor with methods that return the http response details along with the result
316
- * @param interfaceFactory - the interface factory for the actor
317
- * @param configuration - the configuration for the actor
318
- * @deprecated - use createActor with actorClassOptions instead
319
- */
320
- public static createActorWithHttpDetails<T = Record<string, ActorMethod>>(
321
- interfaceFactory: IDL.InterfaceFactory,
322
- configuration: ActorConfig,
323
- ): ActorSubclass<ActorMethodMappedWithHttpDetails<T>> {
324
- return new (this.createActorClass(interfaceFactory, { httpDetails: true }))(
325
- configuration,
326
- ) as unknown as ActorSubclass<ActorMethodMappedWithHttpDetails<T>>;
327
- }
328
-
329
- /**
330
- * Returns an actor with methods that return the http response details along with the result
331
- * @param interfaceFactory - the interface factory for the actor
332
- * @param configuration - the configuration for the actor
333
- * @param actorClassOptions - options for the actor class extended details to return with the result
334
- */
335
- public static createActorWithExtendedDetails<T = Record<string, ActorMethod>>(
336
- interfaceFactory: IDL.InterfaceFactory,
337
- configuration: ActorConfig,
338
- actorClassOptions: CreateActorClassOpts = {
339
- httpDetails: true,
340
- certificate: true,
341
- },
342
- ): ActorSubclass<ActorMethodMappedExtended<T>> {
343
- return new (this.createActorClass(interfaceFactory, actorClassOptions))(
344
- configuration,
345
- ) as unknown as ActorSubclass<ActorMethodMappedExtended<T>>;
346
- }
347
-
348
- private [metadataSymbol]: ActorMetadata;
349
-
350
- protected constructor(metadata: ActorMetadata) {
351
- this[metadataSymbol] = Object.freeze(metadata);
352
- }
353
- }
354
-
355
- // IDL functions can have multiple return values, so decoding always
356
- // produces an array. Ensure that functions with single or zero return
357
- // values behave as expected.
358
- function decodeReturnValue(types: IDL.Type[], msg: Uint8Array) {
359
- const returnValues = IDL.decode(types, msg);
360
- switch (returnValues.length) {
361
- case 0:
362
- return undefined;
363
- case 1:
364
- return returnValues[0];
365
- default:
366
- return returnValues;
367
- }
368
- }
369
-
370
- const DEFAULT_ACTOR_CONFIG: Partial<ActorConfig> = {
371
- pollingOptions: DEFAULT_POLLING_OPTIONS,
372
- queryStrategy: 'query',
373
- };
374
-
375
- export type ActorConstructor = new (config: ActorConfig) => ActorSubclass;
376
-
377
- export const ACTOR_METHOD_WITH_HTTP_DETAILS = 'http-details';
378
- export const ACTOR_METHOD_WITH_CERTIFICATE = 'certificate';
379
-
380
- function _createActorMethod(
381
- actor: Actor,
382
- methodName: string,
383
- func: IDL.FuncClass,
384
- blsVerify?: CreateCertificateOptions['blsVerify'],
385
- ): ActorMethod {
386
- let caller: (options: CallConfig, ...args: unknown[]) => Promise<unknown>;
387
- const isQueryAnnotated =
388
- func.annotations.includes('query') || func.annotations.includes('composite_query');
389
- const queryStrategy = actor[metadataSymbol].config.queryStrategy;
390
- if (isQueryAnnotated && queryStrategy === 'query') {
391
- caller = async (options, ...args) => {
392
- // First, if there's a config transformation, call it.
393
- options = {
394
- ...options,
395
- ...actor[metadataSymbol].config.queryTransform?.(methodName, args, {
396
- ...actor[metadataSymbol].config,
397
- ...options,
398
- }),
399
- };
400
-
401
- const agent = options.agent || actor[metadataSymbol].config.agent || new HttpAgent();
402
- const cid = Principal.from(options.canisterId || actor[metadataSymbol].config.canisterId);
403
- const arg = IDL.encode(func.argTypes, args);
404
-
405
- const result = await agent.query(cid, {
406
- methodName,
407
- arg,
408
- effectiveCanisterId: options.effectiveCanisterId,
409
- });
410
- const httpDetails = {
411
- ...result.httpDetails,
412
- requestDetails: result.requestDetails,
413
- } as HttpDetailsResponse;
414
-
415
- switch (result.status) {
416
- case QueryResponseStatus.Rejected: {
417
- const uncertifiedRejectErrorCode = new UncertifiedRejectErrorCode(
418
- result.requestId,
419
- result.reject_code,
420
- result.reject_message,
421
- result.error_code,
422
- result.signatures,
423
- );
424
- uncertifiedRejectErrorCode.callContext = {
425
- canisterId: cid,
426
- methodName,
427
- httpDetails,
428
- };
429
- throw RejectError.fromCode(uncertifiedRejectErrorCode);
430
- }
431
-
432
- case QueryResponseStatus.Replied:
433
- return func.annotations.includes(ACTOR_METHOD_WITH_HTTP_DETAILS)
434
- ? {
435
- httpDetails,
436
- result: decodeReturnValue(func.retTypes, result.reply.arg),
437
- }
438
- : decodeReturnValue(func.retTypes, result.reply.arg);
439
- }
440
- };
441
- } else {
442
- caller = async (options, ...args) => {
443
- // First, if there's a config transformation, call it.
444
- options = {
445
- ...options,
446
- ...actor[metadataSymbol].config.callTransform?.(methodName, args, {
447
- ...actor[metadataSymbol].config,
448
- ...options,
449
- }),
450
- };
451
-
452
- const agent = options.agent || actor[metadataSymbol].config.agent || HttpAgent.createSync();
453
-
454
- const { canisterId, effectiveCanisterId, pollingOptions } = {
455
- ...DEFAULT_ACTOR_CONFIG,
456
- ...actor[metadataSymbol].config,
457
- ...options,
458
- };
459
- const cid = Principal.from(canisterId);
460
- const ecid = effectiveCanisterId !== undefined ? Principal.from(effectiveCanisterId) : cid;
461
- const arg = IDL.encode(func.argTypes, args);
462
-
463
- let reply: Uint8Array;
464
- let certificate: Certificate | undefined;
465
- let callResponse: UpdateResult['callResponse'];
466
- let requestDetails: UpdateResult['requestDetails'];
467
- // Calls the canister and handles v4/v2 responses, falling back to polling on 202
468
- try {
469
- ({ reply, certificate, callResponse, requestDetails } = await agent.update(
470
- cid,
471
- {
472
- methodName,
473
- arg,
474
- effectiveCanisterId: ecid,
475
- nonce: options.nonce,
476
- },
477
- { ...pollingOptions, blsVerify },
478
- ));
479
- } catch (e) {
480
- if (e instanceof RejectError) {
481
- const enrichedCode = Object.create(
482
- Object.getPrototypeOf(e.code),
483
- Object.getOwnPropertyDescriptors(e.code),
484
- );
485
- enrichedCode.callContext = { ...e.code.callContext, canisterId: cid, methodName };
486
- throw RejectError.fromCode(enrichedCode);
487
- }
488
- throw e;
489
- }
490
-
491
- const shouldIncludeHttpDetails = func.annotations.includes(ACTOR_METHOD_WITH_HTTP_DETAILS);
492
- const shouldIncludeCertificate = func.annotations.includes(ACTOR_METHOD_WITH_CERTIFICATE);
493
-
494
- const httpDetails = { ...callResponse, requestDetails } as HttpDetailsResponse;
495
- if (shouldIncludeHttpDetails && shouldIncludeCertificate) {
496
- return {
497
- httpDetails,
498
- certificate,
499
- result: decodeReturnValue(func.retTypes, reply),
500
- };
501
- }
502
- if (shouldIncludeCertificate) {
503
- return {
504
- certificate,
505
- result: decodeReturnValue(func.retTypes, reply),
506
- };
507
- }
508
- if (shouldIncludeHttpDetails) {
509
- return {
510
- httpDetails,
511
- result: decodeReturnValue(func.retTypes, reply),
512
- };
513
- }
514
- return decodeReturnValue(func.retTypes, reply);
515
- };
516
- }
517
-
518
- const handler = (...args: unknown[]) => caller({}, ...args);
519
- handler.withOptions =
520
- (options: CallConfig) =>
521
- (...args: unknown[]) =>
522
- caller(options, ...args);
523
- return handler as ActorMethod;
524
- }