@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,25 +0,0 @@
1
- import { type Certificate, type NodePath, lookupResultToBuffer } from '../certificate.ts';
2
- import { CertifiedRejectErrorCode } from '../errors.ts';
3
- import type { RequestId } from '../request_id.ts';
4
-
5
- /**
6
- * Read certified reject details from a certificate at the given request status path.
7
- * @param certificate The certificate to read reject details from.
8
- * @param path The lookup path prefix (e.g. `['request_status', requestId]`).
9
- * @param requestId The request ID associated with the rejection.
10
- */
11
- export function readCertifiedReject(
12
- certificate: Certificate,
13
- path: NodePath,
14
- requestId: RequestId,
15
- ): CertifiedRejectErrorCode {
16
- const rejectCode = new Uint8Array(
17
- lookupResultToBuffer(certificate.lookup_path([...path, 'reject_code']))!,
18
- )[0];
19
- const rejectMessage = new TextDecoder().decode(
20
- lookupResultToBuffer(certificate.lookup_path([...path, 'reject_message']))!,
21
- );
22
- const errorCodeBuf = lookupResultToBuffer(certificate.lookup_path([...path, 'error_code']));
23
- const errorCode = errorCodeBuf ? new TextDecoder().decode(errorCodeBuf) : undefined;
24
- return new CertifiedRejectErrorCode(requestId, rejectCode, rejectMessage, errorCode);
25
- }
@@ -1,31 +0,0 @@
1
- import type { ExpirableStore } from './expirableStore.ts';
2
- import { IndexedDBExpirableStore } from './indexedDBExpirableStore.ts';
3
- import { InMemoryExpirableStore } from './inMemoryExpirableStore.ts';
4
-
5
- export interface CreateExpirableStoreOptions {
6
- /**
7
- * Name of the IndexedDB database (used when IndexedDB is available).
8
- */
9
- dbName: string;
10
- /**
11
- * Name of the IndexedDB object store (used when IndexedDB is available).
12
- */
13
- storeName: string;
14
- /**
15
- * Time in milliseconds after which entries expire.
16
- */
17
- expirationTime: number;
18
- }
19
-
20
- /**
21
- * Creates an {@link ExpirableStore} with automatic runtime detection.
22
- * Uses {@link IndexedDBExpirableStore} in browser environments (when IndexedDB is available),
23
- * falls back to {@link InMemoryExpirableStore} otherwise.
24
- * @param options - Configuration for the store (dbName, storeName, and expirationTime).
25
- */
26
- export function createExpirableStore<V>(options: CreateExpirableStoreOptions): ExpirableStore<V> {
27
- if (typeof globalThis.indexedDB !== 'undefined') {
28
- return new IndexedDBExpirableStore<V>(options);
29
- }
30
- return new InMemoryExpirableStore<V>({ expirationTime: options.expirationTime });
31
- }
@@ -1,47 +0,0 @@
1
- /**
2
- * Generic interface for a key-value store with time-based expiration.
3
- * Keys are strings, values are of type V.
4
- * Implementations must handle expiration internally.
5
- */
6
- export interface ExpirableStore<V> {
7
- /**
8
- * Time in milliseconds after which entries expire.
9
- */
10
- readonly expirationTime: number;
11
-
12
- /**
13
- * Get the value for a key.
14
- * Returns undefined if the key is not present or has expired.
15
- */
16
- get(key: string): Promise<V | undefined>;
17
-
18
- /**
19
- * Store a value for a key.
20
- * Prunes expired entries before inserting.
21
- */
22
- set(key: string, value: V): Promise<void>;
23
-
24
- /**
25
- * Delete the entry for a key.
26
- */
27
- delete(key: string): Promise<void>;
28
- }
29
-
30
- export interface ExpirableStoreOptions {
31
- /**
32
- * Time in milliseconds after which entries expire.
33
- * Must be a positive finite number.
34
- */
35
- expirationTime: number;
36
- }
37
-
38
- /**
39
- * Validates that expirationTime is a positive finite number.
40
- * @param expirationTime - Time in milliseconds after which entries expire.
41
- * @throws {Error} if expirationTime is not valid.
42
- */
43
- export function validateExpirationTime(expirationTime: number): void {
44
- if (!Number.isFinite(expirationTime) || expirationTime <= 0) {
45
- throw new Error(`expirationTime must be a positive finite number, got ${expirationTime}`);
46
- }
47
- }
@@ -1,49 +0,0 @@
1
- import { validateExpirationTime } from './expirableStore.ts';
2
- import type { ExpirableStore, ExpirableStoreOptions } from './expirableStore.ts';
3
-
4
- /**
5
- * In-memory key-value store with timestamp-based expiration.
6
- * Prunes all expired entries on every {@link set} call.
7
- */
8
- export class InMemoryExpirableStore<V> implements ExpirableStore<V> {
9
- readonly expirationTime: number;
10
- readonly #entries: Map<string, { value: V; timestamp: number }>;
11
-
12
- constructor(options: ExpirableStoreOptions) {
13
- validateExpirationTime(options.expirationTime);
14
- this.expirationTime = options.expirationTime;
15
- this.#entries = new Map();
16
- }
17
-
18
- get(key: string): Promise<V | undefined> {
19
- const entry = this.#entries.get(key);
20
- if (!entry) {
21
- return Promise.resolve(undefined);
22
- }
23
- if (Date.now() - entry.timestamp > this.expirationTime) {
24
- this.#entries.delete(key);
25
- return Promise.resolve(undefined);
26
- }
27
- return Promise.resolve(entry.value);
28
- }
29
-
30
- set(key: string, value: V): Promise<void> {
31
- this.#prune();
32
- this.#entries.set(key, { value, timestamp: Date.now() });
33
- return Promise.resolve();
34
- }
35
-
36
- delete(key: string): Promise<void> {
37
- this.#entries.delete(key);
38
- return Promise.resolve();
39
- }
40
-
41
- #prune(): void {
42
- const now = Date.now();
43
- for (const [key, entry] of this.#entries) {
44
- if (now - entry.timestamp > this.expirationTime) {
45
- this.#entries.delete(key);
46
- }
47
- }
48
- }
49
- }
@@ -1,191 +0,0 @@
1
- import { validateExpirationTime } from './expirableStore.ts';
2
- import type { ExpirableStore } from './expirableStore.ts';
3
-
4
- interface ExpirableRecord {
5
- key: string;
6
- value: unknown;
7
- timestamp: number;
8
- }
9
-
10
- export interface IndexedDBExpirableStoreOptions {
11
- /**
12
- * Name of the IndexedDB database.
13
- */
14
- dbName: string;
15
- /**
16
- * Name of the IndexedDB object store.
17
- */
18
- storeName: string;
19
- /**
20
- * Time in milliseconds after which entries expire.
21
- */
22
- expirationTime: number;
23
- }
24
-
25
- /**
26
- * Key-value store backed by IndexedDB with time-based expiration.
27
- * Enables sharing cached data across multiple instances and page reloads.
28
- */
29
- export class IndexedDBExpirableStore<V> implements ExpirableStore<V> {
30
- readonly expirationTime: number;
31
- readonly #dbName: string;
32
- readonly #storeName: string;
33
- #dbPromise: Promise<IDBDatabase> | null = null;
34
-
35
- constructor(options: IndexedDBExpirableStoreOptions) {
36
- if (typeof globalThis.indexedDB === 'undefined') {
37
- throw new Error(
38
- 'IndexedDBExpirableStore requires IndexedDB, which is not available in this environment',
39
- );
40
- }
41
- validateExpirationTime(options.expirationTime);
42
- this.#dbName = options.dbName;
43
- this.#storeName = options.storeName;
44
- this.expirationTime = options.expirationTime;
45
- }
46
-
47
- #getDb(): Promise<IDBDatabase> {
48
- if (!this.#dbPromise) {
49
- this.#dbPromise = this.#openDb().catch((error) => {
50
- // Reset the cached promise so future calls can retry opening the DB
51
- this.#dbPromise = null;
52
- throw error;
53
- });
54
- }
55
- return this.#dbPromise;
56
- }
57
-
58
- async #openDb(): Promise<IDBDatabase> {
59
- const db = await this.#openRequest();
60
-
61
- if (db.objectStoreNames.contains(this.#storeName)) {
62
- return db;
63
- }
64
-
65
- // Object store is missing — close and reopen with a bumped version
66
- // to trigger onupgradeneeded.
67
- const nextVersion = db.version + 1;
68
- db.close();
69
- return this.#openRequest(nextVersion);
70
- }
71
-
72
- #openRequest(version?: number): Promise<IDBDatabase> {
73
- const storeName = this.#storeName;
74
-
75
- return new Promise<IDBDatabase>((resolve, reject) => {
76
- const request = globalThis.indexedDB.open(this.#dbName, version);
77
-
78
- request.onupgradeneeded = () => {
79
- const db = request.result;
80
- if (!db.objectStoreNames.contains(storeName)) {
81
- db.createObjectStore(storeName, { keyPath: 'key' });
82
- }
83
- };
84
-
85
- request.onsuccess = () => {
86
- const db = request.result;
87
-
88
- // Cooperatively close when another instance (or this store with a
89
- // different storeName) requests a version upgrade. The next
90
- // operation will re-acquire the connection via #getDb().
91
- db.onversionchange = () => {
92
- db.close();
93
- this.#dbPromise = null;
94
- };
95
-
96
- resolve(db);
97
- };
98
-
99
- request.onerror = () => reject(request.error);
100
- });
101
- }
102
-
103
- async get(key: string): Promise<V | undefined> {
104
- const db = await this.#getDb();
105
-
106
- return new Promise<V | undefined>((resolve, reject) => {
107
- const tx = db.transaction(this.#storeName, 'readonly');
108
- const store = tx.objectStore(this.#storeName);
109
- const request = store.get(key);
110
-
111
- request.onsuccess = () => {
112
- const record = request.result as ExpirableRecord | undefined;
113
- if (!record) {
114
- resolve(undefined);
115
- return;
116
- }
117
-
118
- if (Date.now() - record.timestamp > this.expirationTime) {
119
- // Lazy expiration: delete expired entry and return undefined
120
- this.delete(key).then(
121
- () => resolve(undefined),
122
- () => resolve(undefined),
123
- );
124
- return;
125
- }
126
-
127
- resolve(record.value as V);
128
- };
129
-
130
- request.onerror = () => reject(request.error);
131
- });
132
- }
133
-
134
- async set(key: string, value: V): Promise<void> {
135
- await this.#prune();
136
- const db = await this.#getDb();
137
-
138
- return new Promise<void>((resolve, reject) => {
139
- const tx = db.transaction(this.#storeName, 'readwrite');
140
- const store = tx.objectStore(this.#storeName);
141
-
142
- const record: ExpirableRecord = {
143
- key,
144
- value,
145
- timestamp: Date.now(),
146
- };
147
-
148
- store.put(record);
149
- tx.oncomplete = () => resolve();
150
- tx.onerror = () => reject(tx.error);
151
- });
152
- }
153
-
154
- async delete(key: string): Promise<void> {
155
- const db = await this.#getDb();
156
-
157
- return new Promise<void>((resolve, reject) => {
158
- const tx = db.transaction(this.#storeName, 'readwrite');
159
- const store = tx.objectStore(this.#storeName);
160
- store.delete(key);
161
- tx.oncomplete = () => resolve();
162
- tx.onerror = () => reject(tx.error);
163
- });
164
- }
165
-
166
- async #prune(): Promise<void> {
167
- const db = await this.#getDb();
168
- const now = Date.now();
169
-
170
- return new Promise<void>((resolve, reject) => {
171
- const tx = db.transaction(this.#storeName, 'readwrite');
172
- const store = tx.objectStore(this.#storeName);
173
- const request = store.openCursor();
174
-
175
- request.onsuccess = () => {
176
- const cursor = request.result;
177
- if (!cursor) {
178
- return;
179
- }
180
- const record = cursor.value as ExpirableRecord;
181
- if (now - record.timestamp > this.expirationTime) {
182
- cursor.delete();
183
- }
184
- cursor.continue();
185
- };
186
-
187
- tx.oncomplete = () => resolve();
188
- tx.onerror = () => reject(tx.error);
189
- });
190
- }
191
- }
@@ -1,14 +0,0 @@
1
- import { PipeArrayBuffer, lebDecode } from '#candid';
2
-
3
- const MILLISECOND_TO_NANOSECONDS = BigInt(1_000_000);
4
-
5
- export const decodeLeb128 = (buf: Uint8Array): bigint => {
6
- return lebDecode(new PipeArrayBuffer(buf));
7
- };
8
-
9
- // time is a LEB128-encoded Nat
10
- export const decodeTime = (buf: Uint8Array): Date => {
11
- const timestampNs = decodeLeb128(buf);
12
- const timestampMs = timestampNs / MILLISECOND_TO_NANOSECONDS;
13
- return new Date(Number(timestampMs));
14
- };
@@ -1,30 +0,0 @@
1
- /**
2
- * Generates a random unsigned 32-bit integer between 0 and 0xffffffff
3
- * @returns {number} a random number
4
- */
5
- export const randomNumber = (): number => {
6
- // determine whether browser crypto is available
7
- if (typeof window !== 'undefined' && !!window.crypto && !!window.crypto.getRandomValues) {
8
- const array = new Uint32Array(1);
9
- window.crypto.getRandomValues(array);
10
- return array[0];
11
- }
12
- // A second check for webcrypto, in case it is loaded under global instead of window
13
- if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
14
- const array = new Uint32Array(1);
15
- crypto.getRandomValues(array);
16
- return array[0];
17
- }
18
-
19
- interface nodeCrypto {
20
- randomInt: (min: number, max: number) => number;
21
- }
22
-
23
- // determine whether node crypto is available
24
- if (typeof crypto !== 'undefined' && (crypto as unknown as nodeCrypto).randomInt) {
25
- return (crypto as unknown as nodeCrypto).randomInt(0, 0xffffffff);
26
- }
27
-
28
- // fall back to Math.random
29
- return Math.floor(Math.random() * 0xffffffff);
30
- };
@@ -1,180 +0,0 @@
1
- import { Principal } from '#principal';
2
- import * as cbor from '../cbor.ts';
3
- import { decodeLeb128 } from '../utils/leb.ts';
4
- import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils';
5
- import type { DerEncodedPublicKey } from '../auth.ts';
6
- import {
7
- type Cert,
8
- flatten_forks,
9
- LookupPathStatus,
10
- lookup_path,
11
- lookup_subtree,
12
- type LabeledHashTree,
13
- LookupSubtreeStatus,
14
- } from '../certificate.ts';
15
- import {
16
- LookupErrorCode,
17
- DerKeyLengthMismatchErrorCode,
18
- ProtocolError,
19
- UnknownError,
20
- HashTreeDecodeErrorCode,
21
- } from '../errors.ts';
22
-
23
- /**
24
- * The root subnet ID for IC mainnet
25
- */
26
- export const IC_ROOT_SUBNET_ID = Principal.fromText(
27
- 'tdb26-jop6k-aogll-7ltgs-eruif-6kk7m-qpktf-gdiqx-mxtrf-vb5e6-eqe',
28
- );
29
-
30
- export type SubnetNodeKeys = Map<string, DerEncodedPublicKey>;
31
-
32
- /**
33
- * Represents the useful information about a subnet
34
- */
35
- export interface BaseSubnetStatus {
36
- /**
37
- * The subnet ID
38
- */
39
- subnetId: string;
40
- /**
41
- * The node keys of the subnet
42
- */
43
- nodeKeys: SubnetNodeKeys;
44
- /**
45
- * Not supported
46
- */
47
- metrics?: never;
48
- }
49
-
50
- /**
51
- * Base types of an entry on the status map.
52
- * An entry of null indicates that the request failed, due to lack of permissions or the result being missing.
53
- */
54
- export type BaseStatus = string | Uint8Array | Date | Uint8Array[] | Principal[] | bigint | null;
55
-
56
- /**
57
- * Decode strategy for custom paths
58
- */
59
- export type DecodeStrategy = 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw';
60
-
61
- /**
62
- * Interface to define a custom path. Nested paths will be represented as individual buffers, and can be created from text using TextEncoder.
63
- * @param {string} key the key to use to access the returned value in the status map
64
- * @param {Uint8Array[]} path the path to the desired value, represented as an array of buffers
65
- * @param {string} decodeStrategy the strategy to use to decode the returned value
66
- */
67
- export class CustomPath implements CustomPath {
68
- public key: string;
69
- public path: Uint8Array[] | string;
70
- public decodeStrategy: DecodeStrategy;
71
- constructor(key: string, path: Uint8Array[] | string, decodeStrategy: DecodeStrategy) {
72
- this.key = key;
73
- this.path = path;
74
- this.decodeStrategy = decodeStrategy;
75
- }
76
- }
77
-
78
- /**
79
- * Decode a value based on the specified strategy
80
- * @param data the raw data to decode
81
- * @param strategy the decode strategy to use
82
- * @returns the decoded value
83
- */
84
- export function decodeValue(data: Uint8Array, strategy: DecodeStrategy): BaseStatus {
85
- switch (strategy) {
86
- case 'raw':
87
- return data;
88
- case 'leb128':
89
- return decodeLeb128(data);
90
- case 'cbor':
91
- return cbor.decode(data);
92
- case 'hex':
93
- return bytesToHex(data);
94
- case 'utf-8':
95
- return new TextDecoder().decode(data);
96
- }
97
- }
98
-
99
- /**
100
- * Decode controllers from CBOR-encoded buffer
101
- * @param buf the CBOR-encoded buffer to decode
102
- * @returns an array of principal IDs
103
- */
104
- export function decodeControllers(buf: Uint8Array): Principal[] {
105
- const controllersRaw = cbor.decode<Uint8Array[]>(buf);
106
- return controllersRaw.map(buf => {
107
- return Principal.fromUint8Array(buf);
108
- });
109
- }
110
-
111
- /**
112
- * Encode a metadata path for canister metadata queries
113
- * @param metaPath the metadata path (string or Uint8Array)
114
- * @param canisterUint8Array the canister ID as Uint8Array
115
- * @returns the encoded path
116
- */
117
- export function encodeMetadataPath(
118
- metaPath: string | Uint8Array,
119
- canisterUint8Array: Uint8Array,
120
- ): Uint8Array[] {
121
- const encoded = typeof metaPath === 'string' ? utf8ToBytes(metaPath) : metaPath;
122
- return [utf8ToBytes('canister'), canisterUint8Array, utf8ToBytes('metadata'), encoded];
123
- }
124
-
125
- /**
126
- * Check if a path object has custom path signature (has 'key' and 'path' properties)
127
- * @param path the path to check
128
- * @returns true if the path has custom path signature, false otherwise
129
- */
130
- export function isCustomPath<T>(path: T): path is T & { key: string; path: unknown } {
131
- return typeof path === 'object' && path !== null && 'key' in path && 'path' in path;
132
- }
133
-
134
- /**
135
- * Lookup node keys from a certificate for a given subnet
136
- * This can be used for both canister and subnet status queries
137
- * @param certificate the certificate to fetch node keys from
138
- * @param subnetId the subnet ID to fetch node keys for
139
- * @returns a map of node IDs to public keys
140
- */
141
- export function lookupNodeKeysFromCertificate(
142
- certificate: Cert,
143
- subnetId: Principal,
144
- ): SubnetNodeKeys {
145
- const subnetLookupResult = lookup_subtree(
146
- ['subnet', subnetId.toUint8Array(), 'node'],
147
- certificate.tree,
148
- );
149
- if (subnetLookupResult.status !== LookupSubtreeStatus.Found) {
150
- throw ProtocolError.fromCode(new LookupErrorCode('Node not found', subnetLookupResult.status));
151
- }
152
- if (subnetLookupResult.value instanceof Uint8Array) {
153
- throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid node tree'));
154
- }
155
-
156
- // The forks are all labeled trees with the <node_id> label
157
- const nodeForks = flatten_forks(subnetLookupResult.value) as Array<LabeledHashTree>;
158
- const nodeKeys = new Map<string, DerEncodedPublicKey>();
159
-
160
- nodeForks.forEach(fork => {
161
- const node_id = Principal.from(fork[1]).toText();
162
- const publicKeyLookupResult = lookup_path(['public_key'], fork[2]);
163
- if (publicKeyLookupResult.status !== LookupPathStatus.Found) {
164
- throw ProtocolError.fromCode(
165
- new LookupErrorCode('Public key not found', publicKeyLookupResult.status),
166
- );
167
- }
168
-
169
- const derEncodedPublicKey = publicKeyLookupResult.value;
170
- if (derEncodedPublicKey.byteLength !== 44) {
171
- throw ProtocolError.fromCode(
172
- new DerKeyLengthMismatchErrorCode(44, derEncodedPublicKey.byteLength),
173
- );
174
- } else {
175
- nodeKeys.set(node_id, derEncodedPublicKey as DerEncodedPublicKey);
176
- }
177
- });
178
-
179
- return nodeKeys;
180
- }