@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,260 +0,0 @@
1
- import { Principal } from '#principal';
2
- import {
3
- CertificateVerificationErrorCode,
4
- MissingRootKeyErrorCode,
5
- ExternalError,
6
- AgentError,
7
- UnknownError,
8
- UnexpectedErrorCode,
9
- InputError,
10
- CertificateTimeErrorCode,
11
- } from '../errors.ts';
12
- import type { HttpAgent } from '../agent/http/index.ts';
13
- import { type Cert, Certificate, lookupResultToBuffer } from '../certificate.ts';
14
- import * as cbor from '../cbor.ts';
15
- import { decodeTime } from '../utils/leb.ts';
16
- import { utf8ToBytes, bytesToHex } from '@noble/hashes/utils';
17
- import type { CustomPath } from '../utils/readState.ts';
18
- import {
19
- type BaseSubnetStatus,
20
- type BaseStatus,
21
- decodeValue,
22
- decodeControllers,
23
- encodeMetadataPath,
24
- isCustomPath,
25
- lookupNodeKeysFromCertificate,
26
- IC_ROOT_SUBNET_ID,
27
- } from '../utils/readState.ts';
28
-
29
- // Re-export shared types for backwards compatibility
30
- export { type DecodeStrategy, CustomPath } from '../utils/readState.ts';
31
-
32
- export type SubnetStatus = BaseSubnetStatus;
33
- export type Status = BaseStatus | SubnetStatus;
34
-
35
- /**
36
- * Pre-configured fields for canister status paths
37
- */
38
- export type Path = 'time' | 'controllers' | 'subnet' | 'module_hash' | 'candid' | CustomPath;
39
-
40
- export type StatusMap = Map<Path | string, Status>;
41
-
42
- export interface CanisterStatusOptions {
43
- /**
44
- * The effective canister ID to use in the underlying {@link HttpAgent.readState} call.
45
- */
46
- canisterId: Principal;
47
- /**
48
- * The agent to use to make the canister request. Must be authenticated.
49
- */
50
- agent: HttpAgent;
51
- /**
52
- * The paths to request.
53
- * @default []
54
- */
55
- paths?: Path[] | Set<Path>;
56
- /**
57
- * Whether to disable the certificate freshness checks.
58
- * @default false
59
- */
60
- disableCertificateTimeVerification?: boolean;
61
- }
62
-
63
- /**
64
- * Requests information from a canister's `read_state` endpoint.
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.
70
- * @param {CanisterStatusOptions} options The configuration for the canister status request.
71
- * @see {@link CanisterStatusOptions} for detailed options.
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.
73
- * @example
74
- * const status = await canisterStatus({
75
- * paths: ['controllers', 'candid'],
76
- * ...options
77
- * });
78
- *
79
- * const controllers = status.get('controllers');
80
- */
81
- export const request = async (options: CanisterStatusOptions): Promise<StatusMap> => {
82
- const { agent, paths, disableCertificateTimeVerification = false } = options;
83
- const canisterId = Principal.from(options.canisterId);
84
-
85
- const uniquePaths = [...new Set(paths)];
86
- const status = new Map<string | Path, Status>();
87
-
88
- const promises = uniquePaths.map((path, index) => {
89
- const encodedPath = encodePath(path, canisterId);
90
-
91
- return (async () => {
92
- try {
93
- if (agent.rootKey === null) {
94
- throw ExternalError.fromCode(new MissingRootKeyErrorCode());
95
- }
96
-
97
- const rootKey = agent.rootKey;
98
-
99
- const response = await agent.readState(canisterId, {
100
- paths: [encodedPath],
101
- });
102
-
103
- const certificate = await Certificate.create({
104
- certificate: response.certificate,
105
- rootKey,
106
- principal: { canisterId },
107
- disableTimeVerification: disableCertificateTimeVerification,
108
- agent,
109
- });
110
-
111
- const lookup = (cert: Certificate, path: Path) => {
112
- if (path === 'subnet') {
113
- const data = fetchNodeKeys(response.certificate, canisterId, rootKey);
114
- return {
115
- path,
116
- data,
117
- };
118
- }
119
- return {
120
- path,
121
- data: lookupResultToBuffer(cert.lookup_path(encodedPath)),
122
- };
123
- };
124
-
125
- // must pass in the rootKey if we have no delegation
126
- const { path, data } = lookup(certificate, uniquePaths[index]);
127
- if (!data) {
128
- // Typically, the cert lookup will throw
129
- if (typeof path === 'string') {
130
- status.set(path, null);
131
- } else {
132
- status.set(path.key, null);
133
- }
134
- } else {
135
- switch (path) {
136
- case 'time': {
137
- status.set(path, decodeTime(data));
138
- break;
139
- }
140
- case 'controllers': {
141
- status.set(path, decodeControllers(data));
142
- break;
143
- }
144
- case 'module_hash': {
145
- status.set(path, bytesToHex(data));
146
- break;
147
- }
148
- case 'subnet': {
149
- status.set(path, data);
150
- break;
151
- }
152
- case 'candid': {
153
- status.set(path, new TextDecoder().decode(data));
154
- break;
155
- }
156
- default: {
157
- // Check for CustomPath signature
158
- if (isCustomPath(path)) {
159
- status.set(path.key, decodeValue(data, path.decodeStrategy));
160
- }
161
- }
162
- }
163
- }
164
- } catch (error) {
165
- // Throw on certificate errors
166
- if (
167
- error instanceof AgentError &&
168
- (error.hasCode(CertificateVerificationErrorCode) ||
169
- error.hasCode(CertificateTimeErrorCode))
170
- ) {
171
- throw error;
172
- }
173
- if (isCustomPath(path)) {
174
- status.set(path.key, null);
175
- } else {
176
- status.set(path, null);
177
- }
178
- }
179
- })();
180
- });
181
-
182
- // Fetch all values separately, as each option can fail
183
- await Promise.all(promises);
184
-
185
- return status;
186
- };
187
-
188
- /**
189
- * Lookup node keys from a certificate for a given canister.
190
- * The certificate is assumed to be already verified, including whether the canister is in range of the subnet.
191
- * @param certificate the certificate to lookup node keys from
192
- * @param canisterId the canister ID to lookup node keys for
193
- * @param root_key the root key to use to lookup node keys
194
- * @returns a map of node IDs to public keys
195
- */
196
- export const fetchNodeKeys = (
197
- certificate: Uint8Array,
198
- canisterId: Principal,
199
- root_key?: Uint8Array,
200
- ): BaseSubnetStatus => {
201
- if (!canisterId._isPrincipal) {
202
- throw InputError.fromCode(new UnexpectedErrorCode('Invalid canisterId'));
203
- }
204
- const cert = cbor.decode<Cert>(certificate);
205
- const { delegation } = cert;
206
- let subnetId: Principal;
207
- if (delegation && delegation.subnet_id) {
208
- subnetId = Principal.fromUint8Array(new Uint8Array(delegation.subnet_id));
209
- } else if (!delegation && typeof root_key !== 'undefined') {
210
- // On local replica, with System type subnet, there is no delegation
211
- subnetId = Principal.selfAuthenticating(new Uint8Array(root_key));
212
- } else {
213
- // otherwise use default NNS subnet id
214
- subnetId = IC_ROOT_SUBNET_ID;
215
- }
216
-
217
- const nodeKeys = lookupNodeKeysFromCertificate(cert, subnetId);
218
-
219
- return {
220
- subnetId: subnetId.toText(),
221
- nodeKeys,
222
- };
223
- };
224
-
225
- export const encodePath = (path: Path, canisterId: Principal): Uint8Array[] => {
226
- const canisterUint8Array = canisterId.toUint8Array();
227
- switch (path) {
228
- case 'time':
229
- return [utf8ToBytes('time')];
230
- case 'controllers':
231
- return [utf8ToBytes('canister'), canisterUint8Array, utf8ToBytes('controllers')];
232
- case 'module_hash':
233
- return [utf8ToBytes('canister'), canisterUint8Array, utf8ToBytes('module_hash')];
234
- case 'subnet':
235
- return [utf8ToBytes('subnet')];
236
- case 'candid':
237
- return [
238
- utf8ToBytes('canister'),
239
- canisterUint8Array,
240
- utf8ToBytes('metadata'),
241
- utf8ToBytes('candid:service'),
242
- ];
243
- default: {
244
- // Check for CustomPath signature
245
- if (isCustomPath(path)) {
246
- // For simplified metadata queries
247
- if (typeof path['path'] === 'string' || path['path'] instanceof Uint8Array) {
248
- return encodeMetadataPath(path.path, canisterUint8Array);
249
- }
250
- // For non-metadata, return the provided custom path
251
- return path['path'];
252
- }
253
- }
254
- }
255
- throw UnknownError.fromCode(
256
- new UnexpectedErrorCode(
257
- `Error while encoding your path for canister status. Please ensure that your path ${path} was formatted correctly.`,
258
- ),
259
- );
260
- };
package/src/agent/cbor.ts DELETED
@@ -1,63 +0,0 @@
1
- import { Principal } from '#principal';
2
- import * as cbor from '@dfinity/cbor';
3
- import { CborDecodeErrorCode, CborEncodeErrorCode, InputError } from './errors.ts';
4
- import { Expiry } from './agent/http/expiry.ts';
5
-
6
- /**
7
- * Used to extend classes that need to provide a custom value for the CBOR encoding process.
8
- */
9
- export abstract class ToCborValue {
10
- /**
11
- * Returns a value that can be encoded with CBOR. Typically called in the replacer function of the {@link encode} function.
12
- */
13
- public abstract toCborValue(): cbor.CborValue;
14
- }
15
-
16
- function hasCborValueMethod(value: unknown): value is ToCborValue {
17
- return typeof value === 'object' && value !== null && 'toCborValue' in value;
18
- }
19
-
20
- /**
21
- * Encode a JavaScript value into CBOR. If the value is an instance of {@link ToCborValue},
22
- * the {@link ToCborValue.toCborValue} method will be called to get the value to encode.
23
- * @param value The value to encode
24
- */
25
- export function encode(value: unknown): Uint8Array {
26
- try {
27
- return cbor.encodeWithSelfDescribedTag(value, value => {
28
- if (Principal.isPrincipal(value)) {
29
- return value.toUint8Array();
30
- }
31
-
32
- if (Expiry.isExpiry(value)) {
33
- return value.toBigInt();
34
- }
35
-
36
- if (hasCborValueMethod(value)) {
37
- return value.toCborValue();
38
- }
39
-
40
- return value;
41
- });
42
- } catch (error) {
43
- throw InputError.fromCode(new CborEncodeErrorCode(error, value));
44
- }
45
- }
46
-
47
- /**
48
- * Decode a CBOR encoded value into a JavaScript value.
49
- * @param input The CBOR encoded value
50
- */
51
- export function decode<T>(input: Uint8Array): T {
52
- try {
53
- return cbor.decode(input) as T;
54
- } catch (error) {
55
- throw InputError.fromCode(new CborDecodeErrorCode(error, input));
56
- }
57
- }
58
-
59
- // Not strictly necessary, we're just keeping it for backwards compatibility.
60
- export const Cbor = {
61
- encode,
62
- decode,
63
- };