@icp-sdk/core 5.0.0-beta.0 → 5.0.0-beta.1

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 (137) hide show
  1. package/lib/cjs/agent/actor.js +2 -2
  2. package/lib/cjs/agent/actor.js.map +1 -1
  3. package/lib/cjs/agent/agent/api.d.ts +6 -6
  4. package/lib/cjs/agent/agent/api.js +4 -4
  5. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts +30 -0
  6. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js +33 -1
  8. package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -1
  9. package/lib/cjs/agent/agent/http/index.d.ts +22 -3
  10. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  11. package/lib/cjs/agent/agent/http/index.js +131 -51
  12. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  13. package/lib/cjs/agent/canisterStatus/index.d.ts +18 -50
  14. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  15. package/lib/cjs/agent/canisterStatus/index.js +30 -92
  16. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  17. package/lib/cjs/agent/certificate.d.ts +68 -10
  18. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  19. package/lib/cjs/agent/certificate.js +198 -27
  20. package/lib/cjs/agent/certificate.js.map +1 -1
  21. package/lib/cjs/agent/constants.d.ts +4 -0
  22. package/lib/cjs/agent/constants.d.ts.map +1 -1
  23. package/lib/cjs/agent/constants.js +5 -1
  24. package/lib/cjs/agent/constants.js.map +1 -1
  25. package/lib/cjs/agent/errors.d.ts +7 -1
  26. package/lib/cjs/agent/errors.d.ts.map +1 -1
  27. package/lib/cjs/agent/errors.js +19 -7
  28. package/lib/cjs/agent/errors.js.map +1 -1
  29. package/lib/cjs/agent/index.d.ts +97 -3
  30. package/lib/cjs/agent/index.d.ts.map +1 -1
  31. package/lib/cjs/agent/index.js +92 -9
  32. package/lib/cjs/agent/index.js.map +1 -1
  33. package/lib/cjs/agent/polling/index.js +1 -1
  34. package/lib/cjs/agent/polling/index.js.map +1 -1
  35. package/lib/cjs/agent/subnetStatus/index.d.ts +70 -0
  36. package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -0
  37. package/lib/cjs/agent/subnetStatus/index.js +224 -0
  38. package/lib/cjs/agent/subnetStatus/index.js.map +1 -0
  39. package/lib/cjs/agent/utils/readState.d.ts +84 -0
  40. package/lib/cjs/agent/utils/readState.d.ts.map +1 -0
  41. package/lib/cjs/agent/utils/readState.js +149 -0
  42. package/lib/cjs/agent/utils/readState.js.map +1 -0
  43. package/lib/cjs/candid/index.d.ts +20 -1
  44. package/lib/cjs/candid/index.d.ts.map +1 -1
  45. package/lib/cjs/candid/index.js +20 -1
  46. package/lib/cjs/candid/index.js.map +1 -1
  47. package/lib/cjs/identity/index.d.ts +30 -7
  48. package/lib/cjs/identity/index.d.ts.map +1 -1
  49. package/lib/cjs/identity/index.js +31 -11
  50. package/lib/cjs/identity/index.js.map +1 -1
  51. package/lib/cjs/identity/secp256k1/index.d.ts +48 -1
  52. package/lib/cjs/identity/secp256k1/index.d.ts.map +1 -1
  53. package/lib/cjs/identity/secp256k1/index.js +48 -1
  54. package/lib/cjs/identity/secp256k1/index.js.map +1 -1
  55. package/lib/cjs/principal/index.d.ts +15 -0
  56. package/lib/cjs/principal/index.d.ts.map +1 -1
  57. package/lib/cjs/principal/index.js +15 -0
  58. package/lib/cjs/principal/index.js.map +1 -1
  59. package/lib/esm/agent/actor.js +3 -3
  60. package/lib/esm/agent/actor.js.map +1 -1
  61. package/lib/esm/agent/agent/api.d.ts +6 -6
  62. package/lib/esm/agent/agent/api.js +3 -3
  63. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts +30 -0
  64. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -1
  65. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js +33 -1
  66. package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -1
  67. package/lib/esm/agent/agent/http/index.d.ts +22 -3
  68. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  69. package/lib/esm/agent/agent/http/index.js +134 -54
  70. package/lib/esm/agent/agent/http/index.js.map +1 -1
  71. package/lib/esm/agent/canister-env/index.d.ts +2 -1
  72. package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
  73. package/lib/esm/agent/canister-env/index.js +2 -1
  74. package/lib/esm/agent/canister-env/index.js.map +1 -1
  75. package/lib/esm/agent/canisterStatus/index.d.ts +18 -50
  76. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  77. package/lib/esm/agent/canisterStatus/index.js +31 -93
  78. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  79. package/lib/esm/agent/certificate.d.ts +68 -10
  80. package/lib/esm/agent/certificate.d.ts.map +1 -1
  81. package/lib/esm/agent/certificate.js +196 -29
  82. package/lib/esm/agent/certificate.js.map +1 -1
  83. package/lib/esm/agent/constants.d.ts +4 -0
  84. package/lib/esm/agent/constants.d.ts.map +1 -1
  85. package/lib/esm/agent/constants.js +4 -0
  86. package/lib/esm/agent/constants.js.map +1 -1
  87. package/lib/esm/agent/errors.d.ts +7 -1
  88. package/lib/esm/agent/errors.d.ts.map +1 -1
  89. package/lib/esm/agent/errors.js +15 -4
  90. package/lib/esm/agent/errors.js.map +1 -1
  91. package/lib/esm/agent/index.d.ts +97 -3
  92. package/lib/esm/agent/index.d.ts.map +1 -1
  93. package/lib/esm/agent/index.js +97 -3
  94. package/lib/esm/agent/index.js.map +1 -1
  95. package/lib/esm/agent/polling/index.js +1 -1
  96. package/lib/esm/agent/polling/index.js.map +1 -1
  97. package/lib/esm/agent/subnetStatus/index.d.ts +70 -0
  98. package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -0
  99. package/lib/esm/agent/subnetStatus/index.js +183 -0
  100. package/lib/esm/agent/subnetStatus/index.js.map +1 -0
  101. package/lib/esm/agent/utils/readState.d.ts +84 -0
  102. package/lib/esm/agent/utils/readState.d.ts.map +1 -0
  103. package/lib/esm/agent/utils/readState.js +107 -0
  104. package/lib/esm/agent/utils/readState.js.map +1 -0
  105. package/lib/esm/candid/index.d.ts +20 -1
  106. package/lib/esm/candid/index.d.ts.map +1 -1
  107. package/lib/esm/candid/index.js +20 -1
  108. package/lib/esm/candid/index.js.map +1 -1
  109. package/lib/esm/identity/index.d.ts +30 -7
  110. package/lib/esm/identity/index.d.ts.map +1 -1
  111. package/lib/esm/identity/index.js +30 -9
  112. package/lib/esm/identity/index.js.map +1 -1
  113. package/lib/esm/identity/secp256k1/index.d.ts +48 -1
  114. package/lib/esm/identity/secp256k1/index.d.ts.map +1 -1
  115. package/lib/esm/identity/secp256k1/index.js +48 -1
  116. package/lib/esm/identity/secp256k1/index.js.map +1 -1
  117. package/lib/esm/principal/index.d.ts +15 -0
  118. package/lib/esm/principal/index.d.ts.map +1 -1
  119. package/lib/esm/principal/index.js +15 -0
  120. package/lib/esm/principal/index.js.map +1 -1
  121. package/package.json +1 -1
  122. package/src/agent/actor.ts +3 -3
  123. package/src/agent/agent/api.ts +8 -8
  124. package/src/agent/agent/http/index.ts +171 -58
  125. package/src/agent/canister-env/index.ts +2 -1
  126. package/src/agent/canisterStatus/index.ts +46 -191
  127. package/src/agent/certificate.ts +271 -52
  128. package/src/agent/constants.ts +5 -0
  129. package/src/agent/errors.ts +17 -4
  130. package/src/agent/index.ts +97 -3
  131. package/src/agent/polling/index.ts +1 -1
  132. package/src/agent/subnetStatus/index.ts +270 -0
  133. package/src/agent/utils/readState.ts +180 -0
  134. package/src/candid/index.ts +20 -1
  135. package/src/identity/index.ts +30 -10
  136. package/src/identity/secp256k1/index.ts +48 -1
  137. package/src/principal/index.ts +16 -0
@@ -2,116 +2,40 @@ import { Principal } from '#principal';
2
2
  import {
3
3
  CertificateVerificationErrorCode,
4
4
  MissingRootKeyErrorCode,
5
- CertificateNotAuthorizedErrorCode,
6
- LookupErrorCode,
7
- DerKeyLengthMismatchErrorCode,
8
5
  ExternalError,
9
- ProtocolError,
10
- TrustError,
11
6
  AgentError,
12
7
  UnknownError,
13
- HashTreeDecodeErrorCode,
14
8
  UnexpectedErrorCode,
15
9
  InputError,
16
10
  CertificateTimeErrorCode,
17
11
  } from '../errors.ts';
18
12
  import { HttpAgent } from '../agent/http/index.ts';
19
- import {
20
- type Cert,
21
- Certificate,
22
- flatten_forks,
23
- check_canister_ranges,
24
- LookupPathStatus,
25
- lookup_path,
26
- lookupResultToBuffer,
27
- lookup_subtree,
28
- type LabeledHashTree,
29
- LookupSubtreeStatus,
30
- } from '../certificate.ts';
13
+ import { type Cert, Certificate, lookupResultToBuffer } from '../certificate.ts';
31
14
  import * as cbor from '../cbor.ts';
32
- import { decodeLeb128, decodeTime } from '../utils/leb.ts';
33
- import { type DerEncodedPublicKey } from '../auth.ts';
15
+ import { decodeTime } from '../utils/leb.ts';
34
16
  import { utf8ToBytes, bytesToHex } from '@noble/hashes/utils';
17
+ import {
18
+ type BaseSubnetStatus,
19
+ type BaseStatus,
20
+ CustomPath,
21
+ decodeValue,
22
+ decodeControllers,
23
+ encodeMetadataPath,
24
+ isCustomPath,
25
+ lookupNodeKeysFromCertificate,
26
+ IC_ROOT_SUBNET_ID,
27
+ } from '../utils/readState.ts';
35
28
 
36
- /**
37
- * Represents the useful information about a subnet
38
- * @param {string} subnetId the principal id of the canister's subnet
39
- * @param {string[]} nodeKeys the keys of the individual nodes in the subnet
40
- */
41
- export type SubnetStatus = {
42
- // Principal as a string
43
- subnetId: string;
44
- nodeKeys: Map<string, DerEncodedPublicKey>;
45
- metrics?: {
46
- num_canisters: bigint;
47
- canister_state_bytes: bigint;
48
- consumed_cycles_total: {
49
- current: bigint;
50
- deleted: bigint;
51
- };
52
- update_transactions_total: bigint;
53
- };
54
- };
55
-
56
- /**
57
- * Types of an entry on the canisterStatus map.
58
- * An entry of null indicates that the request failed, due to lack of permissions or the result being missing.
59
- */
60
- export type Status =
61
- | string
62
- | Uint8Array
63
- | Date
64
- | Uint8Array[]
65
- | Principal[]
66
- | SubnetStatus
67
- | bigint
68
- | null;
69
-
70
- /**
71
- * Interface to define a custom path. Nested paths will be represented as individual buffers, and can be created from text using TextEncoder.
72
- * @param {string} key the key to use to access the returned value in the canisterStatus map
73
- * @param {Uint8Array[]} path the path to the desired value, represented as an array of buffers
74
- * @param {string} decodeStrategy the strategy to use to decode the returned value
75
- */
76
- export class CustomPath implements CustomPath {
77
- public key: string;
78
- public path: Uint8Array[] | string;
79
- public decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw';
80
- constructor(
81
- key: string,
82
- path: Uint8Array[] | string,
83
- decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw',
84
- ) {
85
- this.key = key;
86
- this.path = path;
87
- this.decodeStrategy = decodeStrategy;
88
- }
89
- }
29
+ // Re-export shared types for backwards compatibility
30
+ export { type DecodeStrategy, CustomPath } from '../utils/readState.ts';
90
31
 
91
- /**
92
- * @deprecated Use {@link CustomPath} instead
93
- * @param {string} key the key to use to access the returned value in the canisterStatus map
94
- * @param {string} path the path to the desired value, represented as a string
95
- * @param {string} decodeStrategy the strategy to use to decode the returned value
96
- */
97
- export interface MetaData {
98
- kind: 'metadata';
99
- key: string;
100
- path: string | Uint8Array;
101
- decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw';
102
- }
32
+ export type SubnetStatus = BaseSubnetStatus;
33
+ export type Status = BaseStatus | SubnetStatus;
103
34
 
104
35
  /**
105
36
  * Pre-configured fields for canister status paths
106
37
  */
107
- export type Path =
108
- | 'time'
109
- | 'controllers'
110
- | 'subnet'
111
- | 'module_hash'
112
- | 'candid'
113
- | MetaData
114
- | CustomPath;
38
+ export type Path = 'time' | 'controllers' | 'subnet' | 'module_hash' | 'candid' | CustomPath;
115
39
 
116
40
  export type StatusMap = Map<Path | string, Status>;
117
41
 
@@ -139,6 +63,10 @@ export type CanisterStatusOptions = {
139
63
  /**
140
64
  * Requests information from a canister's `read_state` endpoint.
141
65
  * Can be used to request information about the canister's controllers, time, module hash, candid interface, and more.
66
+ *
67
+ * > [!WARNING]
68
+ * > Requesting the `subnet` path from the canister status might be deprecated in the future.
69
+ * > Use {@link https://js.icp.build/core/latest/libs/agent/api/namespaces/subnetstatus/functions/request | SubnetStatus.request} to fetch subnet information instead.
142
70
  * @param {CanisterStatusOptions} options The configuration for the canister status request.
143
71
  * @see {@link CanisterStatusOptions} for detailed options.
144
72
  * @returns {Promise<StatusMap>} A map populated with data from the requested paths. Each path is a key in the map, and the value is the data obtained from the certificate for that path.
@@ -175,7 +103,7 @@ export const request = async (options: CanisterStatusOptions): Promise<StatusMap
175
103
  const certificate = await Certificate.create({
176
104
  certificate: response.certificate,
177
105
  rootKey,
178
- canisterId,
106
+ principal: { canisterId },
179
107
  disableTimeVerification: disableCertificateTimeVerification,
180
108
  agent,
181
109
  });
@@ -229,27 +157,8 @@ export const request = async (options: CanisterStatusOptions): Promise<StatusMap
229
157
  }
230
158
  default: {
231
159
  // Check for CustomPath signature
232
- if (typeof path !== 'string' && 'key' in path && 'path' in path) {
233
- switch (path.decodeStrategy) {
234
- case 'raw':
235
- status.set(path.key, data);
236
- break;
237
- case 'leb128': {
238
- status.set(path.key, decodeLeb128(data));
239
- break;
240
- }
241
- case 'cbor': {
242
- status.set(path.key, cbor.decode(data));
243
- break;
244
- }
245
- case 'hex': {
246
- status.set(path.key, bytesToHex(data));
247
- break;
248
- }
249
- case 'utf-8': {
250
- status.set(path.key, new TextDecoder().decode(data));
251
- }
252
- }
160
+ if (isCustomPath(path)) {
161
+ status.set(path.key, decodeValue(data, path.decodeStrategy));
253
162
  }
254
163
  }
255
164
  }
@@ -263,7 +172,7 @@ export const request = async (options: CanisterStatusOptions): Promise<StatusMap
263
172
  ) {
264
173
  throw error;
265
174
  }
266
- if (typeof path !== 'string' && 'key' in path && 'path' in path) {
175
+ if (isCustomPath(path)) {
267
176
  status.set(path.key, null);
268
177
  } else {
269
178
  status.set(path, null);
@@ -282,81 +191,39 @@ export const request = async (options: CanisterStatusOptions): Promise<StatusMap
282
191
  return status;
283
192
  };
284
193
 
194
+ /**
195
+ * Lookup node keys from a certificate for a given canister.
196
+ * The certificate is assumed to be already verified, including whether the canister is in range of the subnet.
197
+ * @param certificate the certificate to lookup node keys from
198
+ * @param canisterId the canister ID to lookup node keys for
199
+ * @param root_key the root key to use to lookup node keys
200
+ * @returns a map of node IDs to public keys
201
+ */
285
202
  export const fetchNodeKeys = (
286
203
  certificate: Uint8Array,
287
204
  canisterId: Principal,
288
205
  root_key?: Uint8Array,
289
- ): SubnetStatus => {
206
+ ): BaseSubnetStatus => {
290
207
  if (!canisterId._isPrincipal) {
291
208
  throw InputError.fromCode(new UnexpectedErrorCode('Invalid canisterId'));
292
209
  }
293
210
  const cert = cbor.decode<Cert>(certificate);
294
- const tree = cert.tree;
295
- let delegation = cert.delegation;
211
+ const { delegation } = cert;
296
212
  let subnetId: Principal;
297
213
  if (delegation && delegation.subnet_id) {
298
214
  subnetId = Principal.fromUint8Array(new Uint8Array(delegation.subnet_id));
299
- }
300
-
301
- // On local replica, with System type subnet, there is no delegation
302
- else if (!delegation && typeof root_key !== 'undefined') {
215
+ } else if (!delegation && typeof root_key !== 'undefined') {
216
+ // On local replica, with System type subnet, there is no delegation
303
217
  subnetId = Principal.selfAuthenticating(new Uint8Array(root_key));
304
- delegation = {
305
- subnet_id: subnetId.toUint8Array(),
306
- certificate: new Uint8Array(0),
307
- };
308
- }
309
- // otherwise use default NNS subnet id
310
- else {
311
- subnetId = Principal.selfAuthenticating(
312
- Principal.fromText(
313
- 'tdb26-jop6k-aogll-7ltgs-eruif-6kk7m-qpktf-gdiqx-mxtrf-vb5e6-eqe',
314
- ).toUint8Array(),
315
- );
316
- delegation = {
317
- subnet_id: subnetId.toUint8Array(),
318
- certificate: new Uint8Array(0),
319
- };
320
- }
321
-
322
- const canisterInRange = check_canister_ranges({ canisterId, subnetId, tree });
323
- if (!canisterInRange) {
324
- throw TrustError.fromCode(new CertificateNotAuthorizedErrorCode(canisterId, subnetId));
218
+ } else {
219
+ // otherwise use default NNS subnet id
220
+ subnetId = IC_ROOT_SUBNET_ID;
325
221
  }
326
222
 
327
- const subnetLookupResult = lookup_subtree(['subnet', delegation.subnet_id, 'node'], tree);
328
- if (subnetLookupResult.status !== LookupSubtreeStatus.Found) {
329
- throw ProtocolError.fromCode(new LookupErrorCode('Node not found', subnetLookupResult.status));
330
- }
331
- if (subnetLookupResult.value instanceof Uint8Array) {
332
- throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid node tree'));
333
- }
334
-
335
- // The forks are all labeled trees with the <node_id> label
336
- const nodeForks = flatten_forks(subnetLookupResult.value) as Array<LabeledHashTree>;
337
- const nodeKeys = new Map<string, DerEncodedPublicKey>();
338
-
339
- nodeForks.forEach(fork => {
340
- const node_id = Principal.from(fork[1]).toText();
341
- const publicKeyLookupResult = lookup_path(['public_key'], fork[2]);
342
- if (publicKeyLookupResult.status !== LookupPathStatus.Found) {
343
- throw ProtocolError.fromCode(
344
- new LookupErrorCode('Public key not found', publicKeyLookupResult.status),
345
- );
346
- }
347
-
348
- const derEncodedPublicKey = publicKeyLookupResult.value;
349
- if (derEncodedPublicKey.byteLength !== 44) {
350
- throw ProtocolError.fromCode(
351
- new DerKeyLengthMismatchErrorCode(44, derEncodedPublicKey.byteLength),
352
- );
353
- } else {
354
- nodeKeys.set(node_id, derEncodedPublicKey as DerEncodedPublicKey);
355
- }
356
- });
223
+ const nodeKeys = lookupNodeKeysFromCertificate(cert, subnetId);
357
224
 
358
225
  return {
359
- subnetId: Principal.fromUint8Array(new Uint8Array(delegation.subnet_id)).toText(),
226
+ subnetId: subnetId.toText(),
360
227
  nodeKeys,
361
228
  };
362
229
  };
@@ -381,16 +248,12 @@ export const encodePath = (path: Path, canisterId: Principal): Uint8Array[] => {
381
248
  ];
382
249
  default: {
383
250
  // Check for CustomPath signature
384
- if ('key' in path && 'path' in path) {
251
+ if (isCustomPath(path)) {
385
252
  // For simplified metadata queries
386
253
  if (typeof path['path'] === 'string' || path['path'] instanceof Uint8Array) {
387
- const metaPath = path.path;
388
- const encoded = typeof metaPath === 'string' ? utf8ToBytes(metaPath) : metaPath;
389
-
390
- return [utf8ToBytes('canister'), canisterUint8Array, utf8ToBytes('metadata'), encoded];
391
-
392
- // For non-metadata, return the provided custompath
254
+ return encodeMetadataPath(path.path, canisterUint8Array);
393
255
  } else {
256
+ // For non-metadata, return the provided custom path
394
257
  return path['path'];
395
258
  }
396
259
  }
@@ -402,11 +265,3 @@ export const encodePath = (path: Path, canisterId: Principal): Uint8Array[] => {
402
265
  ),
403
266
  );
404
267
  };
405
-
406
- // Controllers are CBOR-encoded buffers
407
- const decodeControllers = (buf: Uint8Array): Principal[] => {
408
- const controllersRaw = cbor.decode<Uint8Array[]>(buf);
409
- return controllersRaw.map(buf => {
410
- return Principal.fromUint8Array(buf);
411
- });
412
- };