@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,54 +0,0 @@
1
- import {
2
- Endpoint,
3
- type HttpAgentRequest,
4
- type HttpAgentRequestTransformFn,
5
- type HttpHeaderField,
6
- makeNonce,
7
- type Nonce,
8
- } from './types.ts';
9
- import { Expiry } from './expiry.ts';
10
-
11
- export { Expiry, JSON_KEY_EXPIRY, type JsonnableExpiry } from './expiry.ts';
12
-
13
- /**
14
- * Create a Nonce transform, which takes a function that returns a Buffer, and adds it
15
- * as the nonce to every call requests.
16
- * @param nonceFn A function that returns a buffer. By default uses a semi-random method.
17
- */
18
- export function makeNonceTransform(nonceFn: () => Nonce = makeNonce): HttpAgentRequestTransformFn {
19
- return async (request: HttpAgentRequest) => {
20
- // Nonce needs to be inserted into the header for all requests, to enable logs to be correlated with requests.
21
- const headers = request.request.headers;
22
- // TODO: uncomment this when the http proxy supports it.
23
- // headers.set('X-IC-Request-ID', toHex(new Uint8Array(nonce)));
24
- request.request.headers = headers;
25
-
26
- // Nonce only needs to be inserted into the body for async calls, to prevent replay attacks.
27
- if (request.endpoint === Endpoint.Call) {
28
- request.body.nonce = nonceFn();
29
- }
30
- };
31
- }
32
-
33
- /**
34
- * Create a transform that adds a delay (by default 5 minutes) to the expiry.
35
- * @param delayInMilliseconds The delay to add to the call time, in milliseconds.
36
- */
37
- export function makeExpiryTransform(delayInMilliseconds: number): HttpAgentRequestTransformFn {
38
- return async (request: HttpAgentRequest) => {
39
- request.body.ingress_expiry = Expiry.fromDeltaInMilliseconds(delayInMilliseconds);
40
- };
41
- }
42
-
43
- /**
44
- * Maps the default fetch headers field to the serializable HttpHeaderField.
45
- * @param headers Fetch definition of the headers type
46
- * @returns array of header fields
47
- */
48
- export function httpHeadersTransform(headers: Headers): HttpHeaderField[] {
49
- const headerFields: HttpHeaderField[] = [];
50
- headers.forEach((value, key) => {
51
- headerFields.push([key, value]);
52
- });
53
- return headerFields;
54
- }
@@ -1,145 +0,0 @@
1
- import type { Principal } from '#principal';
2
- import type { Expiry } from './expiry.ts';
3
- import { randomNumber } from '../../utils/random.ts';
4
-
5
- /**
6
- * @internal
7
- */
8
- export enum Endpoint {
9
- Query = 'read',
10
- ReadState = 'read_state',
11
- Call = 'call',
12
- }
13
-
14
- // An HttpAgent request, before it gets encoded and sent to the server.
15
- // We create an empty request that we will fill later.
16
- export type HttpAgentRequest =
17
- | HttpAgentQueryRequest
18
- | HttpAgentSubmitRequest
19
- | HttpAgentReadStateRequest;
20
-
21
- export interface HttpAgentBaseRequest {
22
- readonly endpoint: Endpoint;
23
- request: RequestInit;
24
- }
25
-
26
- export type HttpHeaderField = [string, string];
27
-
28
- export interface HttpAgentSubmitRequest extends HttpAgentBaseRequest {
29
- readonly endpoint: Endpoint.Call;
30
- body: CallRequest;
31
- }
32
-
33
- export interface HttpAgentQueryRequest extends HttpAgentBaseRequest {
34
- readonly endpoint: Endpoint.Query;
35
- body: ReadRequest;
36
- }
37
-
38
- export interface HttpAgentReadStateRequest extends HttpAgentBaseRequest {
39
- readonly endpoint: Endpoint.ReadState;
40
- body: ReadRequest;
41
- }
42
-
43
- export interface Signed<T> {
44
- content: T;
45
- sender_pubkey: ArrayBuffer;
46
- sender_sig: ArrayBuffer;
47
- }
48
-
49
- export interface UnSigned<T> {
50
- content: T;
51
- }
52
-
53
- export type Envelope<T> = Signed<T> | UnSigned<T>;
54
-
55
- export interface HttpAgentRequestTransformFn {
56
- (args: HttpAgentRequest): Promise<HttpAgentRequest | undefined | void>;
57
- priority?: number;
58
- }
59
-
60
- // The fields in a "call" submit request.
61
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
- export interface CallRequest extends Record<string, any> {
63
- request_type: SubmitRequestType.Call;
64
- canister_id: Principal;
65
- method_name: string;
66
- arg: Uint8Array;
67
- sender: Uint8Array | Principal;
68
- ingress_expiry: Expiry;
69
- nonce?: Nonce;
70
- }
71
-
72
- // The types of values allowed in the `request_type` field for submit requests.
73
- export enum SubmitRequestType {
74
- Call = 'call',
75
- }
76
-
77
- // The types of values allowed in the `request_type` field for read requests.
78
- export enum ReadRequestType {
79
- Query = 'query',
80
- ReadState = 'read_state',
81
- }
82
-
83
- // The fields in a "query" read request.
84
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
- export interface QueryRequest extends Record<string, any> {
86
- request_type: ReadRequestType.Query;
87
- canister_id: Principal;
88
- method_name: string;
89
- arg: Uint8Array;
90
- sender: Uint8Array | Principal;
91
- ingress_expiry: Expiry;
92
- nonce?: Nonce;
93
- }
94
-
95
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
- export interface ReadStateRequest extends Record<string, any> {
97
- request_type: ReadRequestType.ReadState;
98
- paths: Uint8Array[][];
99
- ingress_expiry: Expiry;
100
- sender: Uint8Array | Principal;
101
- }
102
-
103
- export type ReadRequest = QueryRequest | ReadStateRequest;
104
-
105
- /**
106
- * Possible values for the request status in the IC state tree.
107
- * @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-request-status
108
- */
109
- export enum RequestStatusResponseStatus {
110
- /** The call has made it past the endpoint into the IC's state. */
111
- Received = 'received',
112
- /** The initial effect of the call has happened or will happen. */
113
- Processing = 'processing',
114
- /** The call completed successfully; the reply is available at `/request_status/<id>/reply`. */
115
- Replied = 'replied',
116
- /** The call failed; reject code and message are available at `/request_status/<id>/reject_code` and `/request_status/<id>/reject_message`. */
117
- Rejected = 'rejected',
118
- /** The request status path is absent from the state tree, the request is unknown to the IC (never received or pruned after expiry). */
119
- Unknown = 'unknown',
120
- /** The IC has pruned the response data but remembers the request to prevent replay attacks. */
121
- Done = 'done',
122
- }
123
-
124
- // A Nonce that can be used for calls.
125
- export type Nonce = Uint8Array & { __nonce__: void };
126
-
127
- /**
128
- * Create a random Nonce, based on random values
129
- */
130
- export function makeNonce(): Nonce {
131
- // Encode 128 bits.
132
- const buffer = new ArrayBuffer(16);
133
- const view = new DataView(buffer);
134
- const rand1 = randomNumber();
135
- const rand2 = randomNumber();
136
- const rand3 = randomNumber();
137
- const rand4 = randomNumber();
138
-
139
- view.setUint32(0, rand1);
140
- view.setUint32(4, rand2);
141
- view.setUint32(8, rand3);
142
- view.setUint32(12, rand4);
143
-
144
- return Object.assign(new Uint8Array(buffer), { __nonce__: undefined });
145
- }
@@ -1,4 +0,0 @@
1
- export * from './api.ts';
2
- export * from './http/index.ts';
3
- export * from './http/transforms.ts';
4
- export * from './http/types.ts';
package/src/agent/auth.ts DELETED
@@ -1,147 +0,0 @@
1
- import { Principal } from '#principal';
2
- import type { HttpAgentRequest } from './agent/http/types.ts';
3
- import { requestIdOf } from './request_id.ts';
4
- import { bytesToHex, concatBytes } from '@noble/hashes/utils';
5
- import { IC_REQUEST_DOMAIN_SEPARATOR } from './constants.ts';
6
- /**
7
- * A Key Pair, containing a secret and public key.
8
- */
9
- export interface KeyPair {
10
- secretKey: Uint8Array;
11
- publicKey: PublicKey;
12
- }
13
-
14
- /**
15
- * A public key that is DER encoded. This is a branded Uint8Array.
16
- */
17
- export type DerEncodedPublicKey = Uint8Array & { __derEncodedPublicKey__?: void };
18
-
19
- /**
20
- * A signature array buffer.
21
- */
22
- export type Signature = Uint8Array & { __signature__: void };
23
-
24
- /**
25
- * A Public Key implementation.
26
- */
27
- export interface PublicKey {
28
- toDer(): DerEncodedPublicKey;
29
- // rawKey, toRaw, and derKey are optional for backwards compatibility.
30
- toRaw?(): Uint8Array;
31
- rawKey?: Uint8Array;
32
- derKey?: DerEncodedPublicKey;
33
- }
34
-
35
- /**
36
- * A General Identity object. This does not have to be a private key (for example,
37
- * the Anonymous identity), but it must be able to transform request.
38
- */
39
- export interface Identity {
40
- /**
41
- * Get the principal represented by this identity. Normally should be a
42
- * `Principal.selfAuthenticating()`.
43
- */
44
- getPrincipal(): Principal;
45
-
46
- /**
47
- * Transform a request into a signed version of the request. This is done last
48
- * after the transforms on the body of a request. The returned object can be
49
- * anything, but must be serializable to CBOR.
50
- */
51
- transformRequest(request: HttpAgentRequest): Promise<unknown>;
52
- }
53
-
54
- /**
55
- * An Identity that can sign blobs.
56
- */
57
- export abstract class SignIdentity implements Identity {
58
- protected _principal: Principal | undefined;
59
-
60
- /**
61
- * Returns the public key that would match this identity's signature.
62
- */
63
- public abstract getPublicKey(): PublicKey;
64
-
65
- /**
66
- * Signs a blob of data, with this identity's private key.
67
- */
68
- public abstract sign(blob: Uint8Array): Promise<Signature>;
69
-
70
- /**
71
- * Get the principal represented by this identity. Normally should be a
72
- * `Principal.selfAuthenticating()`.
73
- */
74
- public getPrincipal(): Principal {
75
- if (!this._principal) {
76
- this._principal = Principal.selfAuthenticating(new Uint8Array(this.getPublicKey().toDer()));
77
- }
78
- return this._principal;
79
- }
80
-
81
- /**
82
- * Transform a request into a signed version of the request. This is done last
83
- * after the transforms on the body of a request. The returned object can be
84
- * anything, but must be serializable to CBOR.
85
- * @param request - internet computer request to transform
86
- */
87
- public async transformRequest(request: HttpAgentRequest): Promise<unknown> {
88
- const { body, ...fields } = request;
89
- const requestId = requestIdOf(body);
90
- return {
91
- ...fields,
92
- body: {
93
- content: body,
94
- sender_pubkey: this.getPublicKey().toDer(),
95
- sender_sig: await this.sign(concatBytes(IC_REQUEST_DOMAIN_SEPARATOR, requestId)),
96
- },
97
- };
98
- }
99
- }
100
-
101
- export class AnonymousIdentity implements Identity {
102
- public getPrincipal(): Principal {
103
- return Principal.anonymous();
104
- }
105
-
106
- public async transformRequest(request: HttpAgentRequest): Promise<unknown> {
107
- return {
108
- ...request,
109
- body: { content: request.body },
110
- };
111
- }
112
- }
113
-
114
- /*
115
- * We need to communicate with other agents on the page about identities,
116
- * but those messages may need to go across boundaries where it's not possible to
117
- * serialize/deserialize object prototypes easily.
118
- * So these are lightweight, serializable objects that contain enough information to recreate
119
- * SignIdentities, but don't commit to having all methods of SignIdentity.
120
- *
121
- * Use Case:
122
- * * DOM Events that let differently-versioned components communicate to one another about
123
- * Identities, even if they're using slightly different versions of agent packages to
124
- * create/interpret them.
125
- */
126
- export interface AnonymousIdentityDescriptor {
127
- type: 'AnonymousIdentity';
128
- }
129
- export interface PublicKeyIdentityDescriptor {
130
- type: 'PublicKeyIdentity';
131
- publicKey: string;
132
- }
133
- export type IdentityDescriptor = AnonymousIdentityDescriptor | PublicKeyIdentityDescriptor;
134
-
135
- /**
136
- * Create an IdentityDescriptor from an Identity
137
- * @param identity - identity describe in returned descriptor
138
- */
139
- export function createIdentityDescriptor(
140
- identity: SignIdentity | AnonymousIdentity,
141
- ): IdentityDescriptor {
142
- const identityIndicator: IdentityDescriptor =
143
- 'getPublicKey' in identity
144
- ? { type: 'PublicKeyIdentity', publicKey: bytesToHex(identity.getPublicKey().toDer()) }
145
- : { type: 'AnonymousIdentity' };
146
- return identityIndicator;
147
- }
@@ -1,192 +0,0 @@
1
- /**
2
- * @experimental
3
- * @see https://js.icp.build/core/latest/canister-environment
4
- * @module libs/agent/canister-env/api
5
- */
6
-
7
- import { hexToBytes } from '@noble/hashes/utils';
8
- import {
9
- InputError,
10
- InvalidRootKeyErrorCode,
11
- EmptyCookieErrorCode,
12
- MissingRootKeyErrorCode,
13
- MissingCookieErrorCode,
14
- } from '../errors.ts';
15
-
16
- const IC_ENV_COOKIE_NAME = 'ic_env';
17
-
18
- const ENV_VAR_SEPARATOR = '&';
19
- const ENV_VAR_ASSIGNMENT_SYMBOL = '=';
20
-
21
- const IC_ROOT_KEY_VALUE_NAME = 'ic_root_key';
22
- const IC_ROOT_KEY_ENV_NAME = 'IC_ROOT_KEY'; // the value must be the same as the `CanisterEnv.IC_ROOT_KEY` property name
23
-
24
- const IC_ROOT_KEY_BYTES_LENGTH = 133;
25
-
26
- /**
27
- * The environment variables served by the asset canister that hosts the frontend.
28
- * You can extend the `CanisterEnv` interface to add your own environment variables
29
- * and have strong typing for them.
30
- * @see The {@link https://js.icp.build/core/latest/canister-environment/ | Canister Environment Guide} for more details on how to use the canister environment in a frontend application
31
- * @experimental
32
- * @example
33
- * Extend the global `CanisterEnv` interface to add your own environment variables:
34
- * ```ts title="index.ts"
35
- * // You can declare the interface to have strong typing
36
- * // for your own environment variables across your codebase
37
- * declare module '@icp-sdk/core/agent/canister-env' {
38
- * interface CanisterEnv {
39
- * readonly ['PUBLIC_CANISTER_ID:backend']: string;
40
- * readonly PUBLIC_API_URL: string;
41
- * }
42
- * }
43
- *
44
- * const env = getCanisterEnv();
45
- *
46
- * console.log(env.IC_ROOT_KEY); // by default served by the asset canister
47
- * console.log(env['PUBLIC_CANISTER_ID:backend']); // ✅ TS passes
48
- * console.log(env.PUBLIC_API_URL); // ✅ TS passes
49
- * console.log(env['PUBLIC_CANISTER_ID:another']); // ❌ TS will show an error
50
- * ```
51
- * @example
52
- * Alternatively, use the generic parameter to specify additional properties:
53
- * ```ts title="index.ts"
54
- * const env = getCanisterEnv<{ readonly ['PUBLIC_CANISTER_ID:backend']: string }>();
55
- *
56
- * console.log(env.IC_ROOT_KEY); // by default served by the asset canister
57
- * console.log(env['PUBLIC_CANISTER_ID:backend']); // ✅ from generic parameter, TS passes
58
- * console.log(env['PUBLIC_CANISTER_ID:frontend']); // ❌ TS will show an error
59
- * ```
60
- */
61
- export interface CanisterEnv {
62
- /**
63
- * The root key of the IC network where the asset canister is deployed.
64
- * Served by default by the asset canister that hosts the frontend.
65
- */
66
- readonly IC_ROOT_KEY: Uint8Array; // the key must be the same as the `IC_ROOT_KEY_ENV_NAME` constant
67
- }
68
-
69
- /**
70
- * Options for the {@link getCanisterEnv} function
71
- * @experimental
72
- */
73
- export interface GetCanisterEnvOptions {
74
- /**
75
- * The name of the cookie to get the environment variables from.
76
- * @default 'ic_env'
77
- */
78
- cookieName?: string;
79
- }
80
-
81
- /**
82
- * Get the environment variables served by the asset canister via the cookie.
83
- *
84
- * The returned object always includes `IC_ROOT_KEY` property.
85
- * You can extend the global `CanisterEnv` interface to add your own environment variables
86
- * and have strong typing for them.
87
- *
88
- * In Node.js environment (or any other environment where `globalThis.document` is not available), this function will throw an error.
89
- * Use {@link safeGetCanisterEnv} if you need a function that returns `undefined` instead of throwing errors.
90
- * @param options The options for loading the canister environment variables
91
- * @returns The environment variables for the asset canister, always including `IC_ROOT_KEY`
92
- * @throws {TypeError} When `globalThis.document` is not available
93
- * @throws {InputError} When the cookie is not found
94
- * @throws {InputError} When the `IC_ROOT_KEY` is missing or has an invalid length
95
- * @see The {@link https://js.icp.build/core/latest/canister-environment/ | Canister Environment Guide} for more details on how to use the canister environment in a frontend application
96
- * @experimental
97
- * @example
98
- * ```ts
99
- * type MyCanisterEnv = {
100
- * readonly ['PUBLIC_CANISTER_ID:backend']: string;
101
- * };
102
- *
103
- * const env = getCanisterEnv<MyCanisterEnv>();
104
- *
105
- * console.log(env.IC_ROOT_KEY); // always available (Uint8Array)
106
- * console.log(env['PUBLIC_CANISTER_ID:backend']); // ✅ from generic parameter, TS passes
107
- * console.log(env['PUBLIC_CANISTER_ID:frontend']); // ❌ TS will show an error
108
- * ```
109
- * Have a look at {@link CanisterEnv} for more details on how to extend the interface
110
- */
111
- export function getCanisterEnv<T = Record<string, never>>(
112
- options: GetCanisterEnvOptions = {},
113
- ): CanisterEnv & T {
114
- const { cookieName = IC_ENV_COOKIE_NAME } = options;
115
-
116
- const cookie = getCookie(cookieName);
117
- if (!cookie) {
118
- throw InputError.fromCode(new MissingCookieErrorCode(cookieName));
119
- }
120
-
121
- const cookieValue = cookie.split('=')[1].trim();
122
- if (!cookieValue) {
123
- throw InputError.fromCode(new EmptyCookieErrorCode(cookieName));
124
- }
125
-
126
- const decodedCookieValue = decodeURIComponent(cookieValue);
127
- const envVars = parseEnvVars<T>(decodedCookieValue);
128
- if (!envVars.IC_ROOT_KEY) {
129
- throw InputError.fromCode(new MissingRootKeyErrorCode());
130
- }
131
-
132
- return envVars;
133
- }
134
-
135
- /**
136
- * Safe version of {@link getCanisterEnv} that returns `undefined` instead of throwing errors.
137
- * @param options The options for loading the asset canister environment variables
138
- * @returns The environment variables for the asset canister, or `undefined` if any error occurs
139
- * @see The {@link https://js.icp.build/core/latest/canister-environment/ | Canister Environment Guide} for more details on how to use the canister environment in a frontend application
140
- * @experimental
141
- * @example
142
- * ```ts
143
- * // in a browser environment with valid cookie
144
- * const env = safeGetCanisterEnv();
145
- * console.log(env); // { IC_ROOT_KEY: Uint8Array, ... }
146
- *
147
- * // in a Node.js environment
148
- * const env = safeGetCanisterEnv();
149
- * console.log(env); // undefined
150
- *
151
- * // in a browser without the environment cookie
152
- * const env = safeGetCanisterEnv();
153
- * console.log(env); // undefined
154
- * ```
155
- */
156
- export function safeGetCanisterEnv<T = Record<string, never>>(
157
- options: GetCanisterEnvOptions = {},
158
- ): (CanisterEnv & T) | undefined {
159
- try {
160
- return getCanisterEnv<T>(options);
161
- } catch {
162
- return undefined;
163
- }
164
- }
165
-
166
- function getCookie(cookieName: string): string | undefined {
167
- return globalThis.document.cookie
168
- .split(';')
169
- .find(cookie => cookie.trim().startsWith(`${cookieName}=`));
170
- }
171
-
172
- function parseEnvVars<T = Record<string, never>>(decoded: string): CanisterEnv & T {
173
- const entries = decoded.split(ENV_VAR_SEPARATOR).map(v => {
174
- // we only want to split at the first occurrence of the assignment symbol
175
- const symbolIndex = v.indexOf(ENV_VAR_ASSIGNMENT_SYMBOL);
176
-
177
- const key = v.slice(0, symbolIndex);
178
- const value = v.substring(symbolIndex + 1);
179
-
180
- if (key === IC_ROOT_KEY_VALUE_NAME) {
181
- const rootKey = hexToBytes(value);
182
- if (rootKey.length !== IC_ROOT_KEY_BYTES_LENGTH) {
183
- throw InputError.fromCode(new InvalidRootKeyErrorCode(rootKey, IC_ROOT_KEY_BYTES_LENGTH));
184
- }
185
- return [IC_ROOT_KEY_ENV_NAME, rootKey];
186
- }
187
-
188
- return [key, value];
189
- });
190
-
191
- return Object.fromEntries(entries);
192
- }