@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,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
- };