@icp-sdk/core 5.4.0 → 6.1.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.
- package/lib/cjs/agent/actor.d.ts +7 -3
- package/lib/cjs/agent/actor.d.ts.map +1 -1
- package/lib/cjs/agent/actor.js +18 -5
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +71 -10
- package/lib/cjs/agent/agent/api.d.ts.map +1 -1
- package/lib/cjs/agent/agent/api.js +6 -1
- package/lib/cjs/agent/agent/api.js.map +1 -1
- package/lib/cjs/agent/agent/http/index.d.ts +9 -12
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +224 -176
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/auth.d.ts +6 -5
- package/lib/cjs/agent/auth.d.ts.map +1 -1
- package/lib/cjs/agent/auth.js +3 -3
- package/lib/cjs/agent/auth.js.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts +2 -3
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +65 -122
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/cbor.d.ts +2 -1
- package/lib/cjs/agent/cbor.d.ts.map +1 -1
- package/lib/cjs/agent/cbor.js +4 -1
- package/lib/cjs/agent/cbor.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +6 -5
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +14 -18
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/der.d.ts +3 -2
- package/lib/cjs/agent/der.d.ts.map +1 -1
- package/lib/cjs/agent/der.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +7 -1
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +34 -16
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/polling/index.d.ts +7 -7
- package/lib/cjs/agent/polling/index.d.ts.map +1 -1
- package/lib/cjs/agent/polling/index.js +19 -20
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
- package/lib/cjs/agent/polling/strategy.js +6 -6
- package/lib/cjs/agent/polling/strategy.js.map +1 -1
- package/lib/cjs/agent/polling/types.d.ts +3 -4
- package/lib/cjs/agent/polling/types.d.ts.map +1 -1
- package/lib/cjs/agent/public_key.d.ts +3 -2
- package/lib/cjs/agent/public_key.d.ts.map +1 -1
- package/lib/cjs/agent/public_key.js +2 -2
- package/lib/cjs/agent/public_key.js.map +1 -1
- package/lib/cjs/agent/request_id.js +10 -10
- package/lib/cjs/agent/request_id.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.d.ts +3 -2
- package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.js +65 -88
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
- package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
- package/lib/cjs/agent/utils/bls.js +7 -6
- package/lib/cjs/agent/utils/bls.js.map +1 -1
- package/lib/cjs/agent/utils/readState.d.ts +104 -15
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
- package/lib/cjs/agent/utils/readState.js +106 -17
- package/lib/cjs/agent/utils/readState.js.map +1 -1
- package/lib/cjs/agent/utils/target.d.ts +15 -0
- package/lib/cjs/agent/utils/target.d.ts.map +1 -0
- package/lib/cjs/agent/utils/target.js +27 -0
- package/lib/cjs/agent/utils/target.js.map +1 -0
- package/lib/cjs/candid/utils/buffer.d.ts +2 -1
- package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
- package/lib/cjs/candid/utils/buffer.js +5 -2
- package/lib/cjs/candid/utils/buffer.js.map +1 -1
- package/lib/cjs/identity/identity/delegation.d.ts +10 -3
- package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
- package/lib/cjs/identity/identity/delegation.js +25 -12
- package/lib/cjs/identity/identity/delegation.js.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
- package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.js +3 -2
- package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
- package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
- package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ed25519.js +18 -15
- package/lib/cjs/identity/identity/ed25519.js.map +1 -1
- package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
- package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
- package/lib/cjs/identity/identity/webauthn.js +12 -11
- package/lib/cjs/identity/identity/webauthn.js.map +1 -1
- package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
- package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/der.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
- package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.js +2 -2
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
- package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/cjs/principal/principal.js +5 -5
- package/lib/cjs/principal/principal.js.map +1 -1
- package/lib/esm/agent/actor.d.ts +7 -3
- package/lib/esm/agent/actor.d.ts.map +1 -1
- package/lib/esm/agent/actor.js +18 -5
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +71 -10
- package/lib/esm/agent/agent/api.d.ts.map +1 -1
- package/lib/esm/agent/agent/api.js +1 -0
- package/lib/esm/agent/agent/api.js.map +1 -1
- package/lib/esm/agent/agent/http/index.d.ts +9 -12
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +218 -170
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/auth.d.ts +6 -5
- package/lib/esm/agent/auth.d.ts.map +1 -1
- package/lib/esm/agent/auth.js +1 -1
- package/lib/esm/agent/auth.js.map +1 -1
- package/lib/esm/agent/canister-env/index.js +1 -1
- package/lib/esm/agent/canister-env/index.js.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts +2 -3
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +66 -122
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/cbor.d.ts +2 -1
- package/lib/esm/agent/cbor.d.ts.map +1 -1
- package/lib/esm/agent/cbor.js +4 -1
- package/lib/esm/agent/cbor.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +6 -5
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +5 -9
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/der.d.ts +3 -2
- package/lib/esm/agent/der.d.ts.map +1 -1
- package/lib/esm/agent/der.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +7 -1
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +18 -1
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/polling/index.d.ts +7 -7
- package/lib/esm/agent/polling/index.d.ts.map +1 -1
- package/lib/esm/agent/polling/index.js +19 -20
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
- package/lib/esm/agent/polling/strategy.js +6 -6
- package/lib/esm/agent/polling/strategy.js.map +1 -1
- package/lib/esm/agent/polling/types.d.ts +3 -4
- package/lib/esm/agent/polling/types.d.ts.map +1 -1
- package/lib/esm/agent/public_key.d.ts +3 -2
- package/lib/esm/agent/public_key.d.ts.map +1 -1
- package/lib/esm/agent/public_key.js +2 -2
- package/lib/esm/agent/public_key.js.map +1 -1
- package/lib/esm/agent/request_id.js +2 -2
- package/lib/esm/agent/request_id.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.d.ts +3 -2
- package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/subnetStatus/index.js +63 -86
- package/lib/esm/agent/subnetStatus/index.js.map +1 -1
- package/lib/esm/agent/utils/bls.d.ts.map +1 -1
- package/lib/esm/agent/utils/bls.js +7 -6
- package/lib/esm/agent/utils/bls.js.map +1 -1
- package/lib/esm/agent/utils/readState.d.ts +104 -15
- package/lib/esm/agent/utils/readState.d.ts.map +1 -1
- package/lib/esm/agent/utils/readState.js +103 -16
- package/lib/esm/agent/utils/readState.js.map +1 -1
- package/lib/esm/agent/utils/target.d.ts +15 -0
- package/lib/esm/agent/utils/target.d.ts.map +1 -0
- package/lib/esm/agent/utils/target.js +23 -0
- package/lib/esm/agent/utils/target.js.map +1 -0
- package/lib/esm/candid/utils/buffer.d.ts +2 -1
- package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
- package/lib/esm/candid/utils/buffer.js +5 -2
- package/lib/esm/candid/utils/buffer.js.map +1 -1
- package/lib/esm/identity/identity/delegation.d.ts +10 -3
- package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
- package/lib/esm/identity/identity/delegation.js +22 -9
- package/lib/esm/identity/identity/delegation.js.map +1 -1
- package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
- package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/esm/identity/identity/ecdsa.js +3 -2
- package/lib/esm/identity/identity/ecdsa.js.map +1 -1
- package/lib/esm/identity/identity/ed25519.d.ts +3 -2
- package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/esm/identity/identity/ed25519.js +10 -7
- package/lib/esm/identity/identity/ed25519.js.map +1 -1
- package/lib/esm/identity/identity/webauthn.d.ts +4 -3
- package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
- package/lib/esm/identity/identity/webauthn.js +8 -7
- package/lib/esm/identity/identity/webauthn.js.map +1 -1
- package/lib/esm/identity/secp256k1/der.d.ts +2 -1
- package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/der.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
- package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/pem.js +2 -2
- package/lib/esm/identity/secp256k1/pem.js.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
- package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
- package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/esm/principal/principal.js +2 -2
- package/lib/esm/principal/principal.js.map +1 -1
- package/package.json +20 -10
- package/src/agent/actor.ts +0 -524
- package/src/agent/agent/api.ts +0 -310
- package/src/agent/agent/http/expiry.ts +0 -107
- package/src/agent/agent/http/index.ts +0 -1724
- package/src/agent/agent/http/transforms.ts +0 -54
- package/src/agent/agent/http/types.ts +0 -145
- package/src/agent/agent/index.ts +0 -4
- package/src/agent/auth.ts +0 -147
- package/src/agent/canister-env/index.ts +0 -192
- package/src/agent/canisterStatus/index.ts +0 -260
- package/src/agent/cbor.ts +0 -63
- package/src/agent/certificate.ts +0 -1058
- package/src/agent/constants.ts +0 -24
- package/src/agent/der.ts +0 -191
- package/src/agent/errors.ts +0 -962
- package/src/agent/fetch_candid.ts +0 -37
- package/src/agent/index.ts +0 -130
- package/src/agent/observable.ts +0 -49
- package/src/agent/polling/backoff.ts +0 -129
- package/src/agent/polling/index.ts +0 -238
- package/src/agent/polling/strategy.ts +0 -140
- package/src/agent/polling/types.ts +0 -28
- package/src/agent/public_key.ts +0 -65
- package/src/agent/request_id.ts +0 -99
- package/src/agent/subnetStatus/index.ts +0 -263
- package/src/agent/utils/bls.ts +0 -18
- package/src/agent/utils/buffer.ts +0 -64
- package/src/agent/utils/certificateReject.ts +0 -25
- package/src/agent/utils/createExpirableStore.ts +0 -31
- package/src/agent/utils/expirableStore.ts +0 -47
- package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
- package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
- package/src/agent/utils/leb.ts +0 -14
- package/src/agent/utils/random.ts +0 -30
- package/src/agent/utils/readState.ts +0 -180
- package/src/candid/candid-core.ts +0 -274
- package/src/candid/candid-ui.ts +0 -281
- package/src/candid/idl.ts +0 -2654
- package/src/candid/index.ts +0 -35
- package/src/candid/types.ts +0 -7
- package/src/candid/utils/bigint-math.ts +0 -26
- package/src/candid/utils/buffer.ts +0 -221
- package/src/candid/utils/hash.ts +0 -31
- package/src/candid/utils/leb128.ts +0 -233
- package/src/identity/identity/attributes.ts +0 -91
- package/src/identity/identity/delegation.ts +0 -411
- package/src/identity/identity/ecdsa.ts +0 -162
- package/src/identity/identity/ed25519.ts +0 -246
- package/src/identity/identity/partial.ts +0 -60
- package/src/identity/identity/webauthn.ts +0 -265
- package/src/identity/index.ts +0 -44
- package/src/identity/secp256k1/der.ts +0 -177
- package/src/identity/secp256k1/index.ts +0 -52
- package/src/identity/secp256k1/pem.ts +0 -39
- package/src/identity/secp256k1/secp256k1.ts +0 -276
- package/src/index.ts +0 -3
- package/src/principal/index.ts +0 -19
- package/src/principal/principal.ts +0 -181
- package/src/principal/utils/base32.ts +0 -101
- 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
|
-
}
|
package/src/agent/utils/leb.ts
DELETED
|
@@ -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
|
-
}
|