@icp-sdk/core 5.0.0 → 5.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 +15 -1
- package/lib/cjs/agent/actor.d.ts.map +1 -1
- package/lib/cjs/agent/actor.js +37 -91
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +29 -6
- package/lib/cjs/agent/agent/api.d.ts.map +1 -1
- package/lib/cjs/agent/agent/api.js.map +1 -1
- package/lib/cjs/agent/agent/http/expiry.d.ts +36 -0
- package/lib/cjs/agent/agent/http/expiry.d.ts.map +1 -0
- package/lib/cjs/agent/agent/http/expiry.js +92 -0
- package/lib/cjs/agent/agent/http/expiry.js.map +1 -0
- package/lib/cjs/agent/agent/http/index.d.ts +11 -2
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +142 -73
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/agent/http/transforms.d.ts +1 -35
- package/lib/cjs/agent/agent/http/transforms.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/transforms.js +6 -90
- package/lib/cjs/agent/agent/http/transforms.js.map +1 -1
- package/lib/cjs/agent/agent/http/types.d.ts +1 -1
- package/lib/cjs/agent/agent/http/types.d.ts.map +1 -1
- package/lib/cjs/agent/auth.d.ts +1 -1
- package/lib/cjs/agent/auth.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts +5 -4
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +6 -10
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/cbor.js +2 -2
- package/lib/cjs/agent/cbor.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +4 -4
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +6 -10
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/constants.d.ts +4 -4
- package/lib/cjs/agent/constants.d.ts.map +1 -1
- package/lib/cjs/agent/der.d.ts.map +1 -1
- package/lib/cjs/agent/der.js +26 -21
- package/lib/cjs/agent/der.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +27 -18
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +18 -15
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/observable.d.ts +1 -1
- package/lib/cjs/agent/observable.d.ts.map +1 -1
- package/lib/cjs/agent/polling/backoff.d.ts +7 -7
- package/lib/cjs/agent/polling/backoff.d.ts.map +1 -1
- package/lib/cjs/agent/polling/backoff.js +3 -5
- package/lib/cjs/agent/polling/backoff.js.map +1 -1
- package/lib/cjs/agent/polling/index.d.ts +11 -10
- package/lib/cjs/agent/polling/index.d.ts.map +1 -1
- package/lib/cjs/agent/polling/index.js +8 -6
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/polling/strategy.d.ts +2 -5
- package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
- package/lib/cjs/agent/polling/strategy.js.map +1 -1
- package/lib/cjs/agent/polling/types.d.ts +18 -0
- package/lib/cjs/agent/polling/types.d.ts.map +1 -0
- package/lib/cjs/agent/polling/types.js +3 -0
- package/lib/cjs/agent/polling/types.js.map +1 -0
- package/lib/cjs/agent/public_key.d.ts +1 -1
- package/lib/cjs/agent/public_key.d.ts.map +1 -1
- package/lib/cjs/agent/request_id.d.ts.map +1 -1
- package/lib/cjs/agent/request_id.js +7 -7
- package/lib/cjs/agent/request_id.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.d.ts +5 -4
- package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.js +6 -10
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
- package/lib/cjs/agent/utils/buffer.d.ts.map +1 -1
- package/lib/cjs/agent/utils/buffer.js +4 -2
- package/lib/cjs/agent/utils/buffer.js.map +1 -1
- package/lib/cjs/agent/utils/certificateReject.d.ts +11 -0
- package/lib/cjs/agent/utils/certificateReject.d.ts.map +1 -0
- package/lib/cjs/agent/utils/certificateReject.js +19 -0
- package/lib/cjs/agent/utils/certificateReject.js.map +1 -0
- package/lib/cjs/agent/utils/expirableMap.d.ts +4 -4
- package/lib/cjs/agent/utils/expirableMap.d.ts.map +1 -1
- package/lib/cjs/agent/utils/expirableMap.js +2 -2
- package/lib/cjs/agent/utils/readState.d.ts +3 -3
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
- package/lib/cjs/candid/candid-core.d.ts +1 -1
- package/lib/cjs/candid/candid-core.d.ts.map +1 -1
- package/lib/cjs/candid/candid-core.js +7 -9
- package/lib/cjs/candid/candid-core.js.map +1 -1
- package/lib/cjs/candid/candid-ui.d.ts.map +1 -1
- package/lib/cjs/candid/candid-ui.js +8 -24
- package/lib/cjs/candid/candid-ui.js.map +1 -1
- package/lib/cjs/candid/idl.d.ts +14 -1
- package/lib/cjs/candid/idl.d.ts.map +1 -1
- package/lib/cjs/candid/idl.js +164 -125
- package/lib/cjs/candid/idl.js.map +1 -1
- package/lib/cjs/candid/utils/leb128.d.ts.map +1 -1
- package/lib/cjs/candid/utils/leb128.js +1 -3
- package/lib/cjs/candid/utils/leb128.js.map +1 -1
- package/lib/cjs/identity/identity/delegation.d.ts +4 -3
- package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
- package/lib/cjs/identity/identity/delegation.js +3 -3
- 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 +2 -4
- package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
- package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ed25519.js +9 -10
- package/lib/cjs/identity/identity/ed25519.js.map +1 -1
- package/lib/cjs/identity/identity/partial.d.ts +1 -1
- package/lib/cjs/identity/identity/partial.d.ts.map +1 -1
- package/lib/cjs/identity/identity/webauthn.js +4 -5
- package/lib/cjs/identity/identity/webauthn.js.map +1 -1
- package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/der.js +27 -22
- package/lib/cjs/identity/secp256k1/der.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.js +1 -1
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.js +6 -6
- package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/cjs/principal/principal.d.ts +2 -2
- package/lib/cjs/principal/principal.d.ts.map +1 -1
- package/lib/cjs/principal/principal.js +10 -6
- package/lib/cjs/principal/principal.js.map +1 -1
- package/lib/esm/agent/actor.d.ts +15 -1
- package/lib/esm/agent/actor.d.ts.map +1 -1
- package/lib/esm/agent/actor.js +40 -94
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +29 -6
- package/lib/esm/agent/agent/api.d.ts.map +1 -1
- package/lib/esm/agent/agent/api.js.map +1 -1
- package/lib/esm/agent/agent/http/expiry.d.ts +36 -0
- package/lib/esm/agent/agent/http/expiry.d.ts.map +1 -0
- package/lib/esm/agent/agent/http/expiry.js +88 -0
- package/lib/esm/agent/agent/http/expiry.js.map +1 -0
- package/lib/esm/agent/agent/http/index.d.ts +11 -2
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +145 -76
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/agent/http/transforms.d.ts +1 -35
- package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/transforms.js +2 -87
- package/lib/esm/agent/agent/http/transforms.js.map +1 -1
- package/lib/esm/agent/agent/http/types.d.ts +1 -1
- package/lib/esm/agent/agent/http/types.d.ts.map +1 -1
- package/lib/esm/agent/auth.d.ts +1 -1
- package/lib/esm/agent/auth.d.ts.map +1 -1
- package/lib/esm/agent/canister-env/index.d.ts +2 -2
- package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts +5 -4
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +6 -10
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/cbor.js +1 -1
- package/lib/esm/agent/cbor.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +4 -4
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +6 -10
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/constants.d.ts +4 -4
- package/lib/esm/agent/constants.d.ts.map +1 -1
- package/lib/esm/agent/der.d.ts.map +1 -1
- package/lib/esm/agent/der.js +26 -21
- package/lib/esm/agent/der.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +27 -18
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +16 -13
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/observable.d.ts +1 -1
- package/lib/esm/agent/observable.d.ts.map +1 -1
- package/lib/esm/agent/polling/backoff.d.ts +7 -7
- package/lib/esm/agent/polling/backoff.d.ts.map +1 -1
- package/lib/esm/agent/polling/backoff.js +3 -5
- package/lib/esm/agent/polling/backoff.js.map +1 -1
- package/lib/esm/agent/polling/index.d.ts +11 -10
- package/lib/esm/agent/polling/index.d.ts.map +1 -1
- package/lib/esm/agent/polling/index.js +9 -7
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/polling/strategy.d.ts +2 -5
- package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
- package/lib/esm/agent/polling/strategy.js.map +1 -1
- package/lib/esm/agent/polling/types.d.ts +18 -0
- package/lib/esm/agent/polling/types.d.ts.map +1 -0
- package/lib/esm/agent/polling/types.js +2 -0
- package/lib/esm/agent/polling/types.js.map +1 -0
- package/lib/esm/agent/public_key.d.ts +1 -1
- package/lib/esm/agent/public_key.d.ts.map +1 -1
- package/lib/esm/agent/request_id.d.ts.map +1 -1
- package/lib/esm/agent/request_id.js +7 -7
- package/lib/esm/agent/request_id.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.d.ts +5 -4
- package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/subnetStatus/index.js +6 -10
- package/lib/esm/agent/subnetStatus/index.js.map +1 -1
- package/lib/esm/agent/utils/buffer.d.ts.map +1 -1
- package/lib/esm/agent/utils/buffer.js +4 -2
- package/lib/esm/agent/utils/buffer.js.map +1 -1
- package/lib/esm/agent/utils/certificateReject.d.ts +11 -0
- package/lib/esm/agent/utils/certificateReject.d.ts.map +1 -0
- package/lib/esm/agent/utils/certificateReject.js +16 -0
- package/lib/esm/agent/utils/certificateReject.js.map +1 -0
- package/lib/esm/agent/utils/expirableMap.d.ts +4 -4
- package/lib/esm/agent/utils/expirableMap.d.ts.map +1 -1
- package/lib/esm/agent/utils/expirableMap.js +2 -2
- package/lib/esm/agent/utils/readState.d.ts +3 -3
- package/lib/esm/agent/utils/readState.d.ts.map +1 -1
- package/lib/esm/candid/candid-core.d.ts +1 -1
- package/lib/esm/candid/candid-core.d.ts.map +1 -1
- package/lib/esm/candid/candid-core.js +7 -9
- package/lib/esm/candid/candid-core.js.map +1 -1
- package/lib/esm/candid/candid-ui.d.ts.map +1 -1
- package/lib/esm/candid/candid-ui.js +7 -23
- package/lib/esm/candid/candid-ui.js.map +1 -1
- package/lib/esm/candid/idl.d.ts +14 -1
- package/lib/esm/candid/idl.d.ts.map +1 -1
- package/lib/esm/candid/idl.js +162 -124
- package/lib/esm/candid/idl.js.map +1 -1
- package/lib/esm/candid/utils/leb128.d.ts.map +1 -1
- package/lib/esm/candid/utils/leb128.js +1 -3
- package/lib/esm/candid/utils/leb128.js.map +1 -1
- package/lib/esm/identity/identity/delegation.d.ts +4 -3
- package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
- package/lib/esm/identity/identity/delegation.js +3 -3
- 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 +2 -4
- package/lib/esm/identity/identity/ecdsa.js.map +1 -1
- package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/esm/identity/identity/ed25519.js +9 -10
- package/lib/esm/identity/identity/ed25519.js.map +1 -1
- package/lib/esm/identity/identity/partial.d.ts +1 -1
- package/lib/esm/identity/identity/partial.d.ts.map +1 -1
- package/lib/esm/identity/identity/webauthn.js +4 -5
- package/lib/esm/identity/identity/webauthn.js.map +1 -1
- package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/der.js +27 -22
- package/lib/esm/identity/secp256k1/der.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.js +1 -1
- package/lib/esm/identity/secp256k1/pem.js.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.js +6 -6
- package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/esm/principal/principal.d.ts +2 -2
- package/lib/esm/principal/principal.d.ts.map +1 -1
- package/lib/esm/principal/principal.js +10 -6
- package/lib/esm/principal/principal.js.map +1 -1
- package/package.json +13 -13
- package/src/agent/actor.ts +74 -126
- package/src/agent/agent/api.ts +36 -6
- package/src/agent/agent/http/expiry.ts +107 -0
- package/src/agent/agent/http/index.ts +217 -90
- package/src/agent/agent/http/transforms.ts +2 -106
- package/src/agent/agent/http/types.ts +1 -1
- package/src/agent/auth.ts +1 -1
- package/src/agent/canister-env/index.ts +2 -2
- package/src/agent/canisterStatus/index.ts +10 -12
- package/src/agent/cbor.ts +1 -1
- package/src/agent/certificate.ts +11 -13
- package/src/agent/der.ts +40 -19
- package/src/agent/errors.ts +41 -31
- package/src/agent/fetch_candid.ts +1 -1
- package/src/agent/observable.ts +1 -1
- package/src/agent/polling/backoff.ts +9 -10
- package/src/agent/polling/index.ts +14 -26
- package/src/agent/polling/strategy.ts +5 -9
- package/src/agent/polling/types.ts +28 -0
- package/src/agent/public_key.ts +1 -1
- package/src/agent/request_id.ts +15 -8
- package/src/agent/subnetStatus/index.ts +10 -12
- package/src/agent/utils/buffer.ts +6 -2
- package/src/agent/utils/certificateReject.ts +25 -0
- package/src/agent/utils/expirableMap.ts +4 -4
- package/src/agent/utils/random.ts +2 -2
- package/src/agent/utils/readState.ts +3 -3
- package/src/candid/candid-core.ts +9 -11
- package/src/candid/candid-ui.ts +8 -16
- package/src/candid/idl.ts +210 -112
- package/src/candid/utils/leb128.ts +1 -2
- package/src/identity/identity/delegation.ts +5 -5
- package/src/identity/identity/ecdsa.ts +12 -8
- package/src/identity/identity/ed25519.ts +16 -9
- package/src/identity/identity/partial.ts +1 -1
- package/src/identity/identity/webauthn.ts +21 -21
- package/src/identity/secp256k1/der.ts +41 -20
- package/src/identity/secp256k1/pem.ts +1 -1
- package/src/identity/secp256k1/secp256k1.ts +12 -6
- package/src/principal/principal.ts +18 -8
- package/LICENSE +0 -201
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { JsonObject } from '#candid';
|
|
2
2
|
import { Principal } from '#principal';
|
|
3
3
|
import {
|
|
4
4
|
HashTreeDecodeErrorCode,
|
|
5
5
|
CreateHttpAgentErrorCode,
|
|
6
|
+
ExcessiveSignaturesErrorCode,
|
|
6
7
|
ExternalError,
|
|
7
|
-
HttpDefaultFetchErrorCode,
|
|
8
8
|
IdentityInvalidErrorCode,
|
|
9
9
|
IngressExpiryInvalidErrorCode,
|
|
10
10
|
InputError,
|
|
@@ -12,11 +12,12 @@ import {
|
|
|
12
12
|
MalformedPublicKeyErrorCode,
|
|
13
13
|
MalformedSignatureErrorCode,
|
|
14
14
|
MissingRootKeyErrorCode,
|
|
15
|
-
MissingSignatureErrorCode,
|
|
16
15
|
ProtocolError,
|
|
17
16
|
QuerySignatureVerificationFailedErrorCode,
|
|
17
|
+
RejectError,
|
|
18
18
|
TimeoutWaitingForResponseErrorCode,
|
|
19
19
|
TrustError,
|
|
20
|
+
UncertifiedRejectUpdateErrorCode,
|
|
20
21
|
UnexpectedErrorCode,
|
|
21
22
|
UnknownError,
|
|
22
23
|
HttpErrorCode,
|
|
@@ -36,11 +37,18 @@ import {
|
|
|
36
37
|
QueryResponseStatus,
|
|
37
38
|
type Agent,
|
|
38
39
|
type ApiQueryResponse,
|
|
40
|
+
type UpdateResult,
|
|
41
|
+
type HttpDetailsResponse,
|
|
39
42
|
type QueryFields,
|
|
40
43
|
type QueryResponse,
|
|
41
44
|
type ReadStateOptions,
|
|
42
45
|
type ReadStateResponse,
|
|
43
46
|
type SubmitResponse,
|
|
47
|
+
type CallOptions,
|
|
48
|
+
type v2ResponseBody,
|
|
49
|
+
type v4ResponseBody,
|
|
50
|
+
isV4ResponseBody,
|
|
51
|
+
isV2ResponseBody,
|
|
44
52
|
} from '../api.ts';
|
|
45
53
|
import { Expiry, httpHeadersTransform, makeNonceTransform } from './transforms.ts';
|
|
46
54
|
import {
|
|
@@ -66,8 +74,10 @@ import {
|
|
|
66
74
|
getSubnetIdFromCertificate,
|
|
67
75
|
type HashTree,
|
|
68
76
|
lookup_path,
|
|
77
|
+
lookupResultToBuffer,
|
|
69
78
|
LookupPathStatus,
|
|
70
79
|
} from '../../certificate.ts';
|
|
80
|
+
import { readCertifiedReject } from '../../utils/certificateReject.ts';
|
|
71
81
|
import { ed25519 } from '@noble/curves/ed25519';
|
|
72
82
|
import { ExpirableMap } from '../../utils/expirableMap.ts';
|
|
73
83
|
import { Ed25519PublicKey } from '../../public_key.ts';
|
|
@@ -78,6 +88,7 @@ import {
|
|
|
78
88
|
ExponentialBackoff,
|
|
79
89
|
} from '../../polling/backoff.ts';
|
|
80
90
|
import { decodeTime } from '../../utils/leb.ts';
|
|
91
|
+
import { pollForResponse, type PollingOptions } from '../../polling/index.ts';
|
|
81
92
|
import { concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
|
|
82
93
|
import { uint8Equals, uint8FromBufLike } from '../../utils/buffer.ts';
|
|
83
94
|
import { IC_RESPONSE_DOMAIN_SEPARATOR } from '../../constants.ts';
|
|
@@ -194,52 +205,11 @@ export interface HttpAgentOptions {
|
|
|
194
205
|
shouldSyncTime?: boolean;
|
|
195
206
|
}
|
|
196
207
|
|
|
197
|
-
function getDefaultFetch(): typeof fetch {
|
|
198
|
-
let defaultFetch;
|
|
199
|
-
|
|
200
|
-
if (typeof window !== 'undefined') {
|
|
201
|
-
// Browser context
|
|
202
|
-
if (window.fetch) {
|
|
203
|
-
defaultFetch = window.fetch.bind(window);
|
|
204
|
-
} else {
|
|
205
|
-
throw ExternalError.fromCode(
|
|
206
|
-
new HttpDefaultFetchErrorCode(
|
|
207
|
-
'Fetch implementation was not available. You appear to be in a browser context, but window.fetch was not present.',
|
|
208
|
-
),
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
} else if (typeof global !== 'undefined') {
|
|
212
|
-
// Node context
|
|
213
|
-
if (global.fetch) {
|
|
214
|
-
defaultFetch = global.fetch.bind(global);
|
|
215
|
-
} else {
|
|
216
|
-
throw ExternalError.fromCode(
|
|
217
|
-
new HttpDefaultFetchErrorCode(
|
|
218
|
-
'Fetch implementation was not available. You appear to be in a Node.js context, but global.fetch was not available.',
|
|
219
|
-
),
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
} else if (typeof self !== 'undefined') {
|
|
223
|
-
if (self.fetch) {
|
|
224
|
-
defaultFetch = self.fetch.bind(self);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (defaultFetch) {
|
|
229
|
-
return defaultFetch;
|
|
230
|
-
}
|
|
231
|
-
throw ExternalError.fromCode(
|
|
232
|
-
new HttpDefaultFetchErrorCode(
|
|
233
|
-
'Fetch implementation was not available. Please provide fetch to the HttpAgent constructor, or ensure it is available in the window or global context.',
|
|
234
|
-
),
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
208
|
function determineHost(configuredHost: string | undefined): string {
|
|
239
209
|
let host: URL;
|
|
240
210
|
if (configuredHost !== undefined) {
|
|
241
211
|
if (!configuredHost.match(/^[a-z]+:/) && typeof window !== 'undefined') {
|
|
242
|
-
host = new URL(window.location.protocol
|
|
212
|
+
host = new URL(`${window.location.protocol}//${configuredHost}`);
|
|
243
213
|
} else {
|
|
244
214
|
host = new URL(configuredHost);
|
|
245
215
|
}
|
|
@@ -261,7 +231,7 @@ function determineHost(configuredHost: string | undefined): string {
|
|
|
261
231
|
if (location && knownHost) {
|
|
262
232
|
// If the user is on a boundary-node provided host, we can use the same host for the agent
|
|
263
233
|
host = new URL(
|
|
264
|
-
`${location.protocol}//${knownHost}${location.port ?
|
|
234
|
+
`${location.protocol}//${knownHost}${location.port ? `:${location.port}` : ''}`,
|
|
265
235
|
);
|
|
266
236
|
} else {
|
|
267
237
|
host = new URL('https://icp-api.io');
|
|
@@ -327,6 +297,9 @@ export class HttpAgent implements Agent {
|
|
|
327
297
|
#subnetKeys: ExpirableMap<string, SubnetNodeKeys> = new ExpirableMap({
|
|
328
298
|
expirationTime: 5 * MINUTE_TO_MSECS,
|
|
329
299
|
});
|
|
300
|
+
// Tracks in-flight fetchSubnetKeys promises to deduplicate parallel requests
|
|
301
|
+
// for the same canister, preventing redundant read_state round-trips.
|
|
302
|
+
#subnetKeysFetching: Map<string, Promise<SubnetNodeKeys>> = new Map();
|
|
330
303
|
#verifyQuerySignatures = true;
|
|
331
304
|
|
|
332
305
|
/**
|
|
@@ -335,7 +308,7 @@ export class HttpAgent implements Agent {
|
|
|
335
308
|
*/
|
|
336
309
|
constructor(options: HttpAgentOptions = {}) {
|
|
337
310
|
this.config = options;
|
|
338
|
-
this.#fetch = options.fetch
|
|
311
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
339
312
|
this.#fetchOptions = options.fetchOptions;
|
|
340
313
|
this.#callOptions = options.callOptions;
|
|
341
314
|
this.#shouldFetchRootKey = options.shouldFetchRootKey ?? false;
|
|
@@ -375,7 +348,7 @@ export class HttpAgent implements Agent {
|
|
|
375
348
|
|
|
376
349
|
if (options.credentials) {
|
|
377
350
|
const { name, password } = options.credentials;
|
|
378
|
-
this.#credentials = `${name}${password ?
|
|
351
|
+
this.#credentials = `${name}${password ? `:${password}` : ''}`;
|
|
379
352
|
}
|
|
380
353
|
this.#identity = Promise.resolve(options.identity || new AnonymousIdentity());
|
|
381
354
|
|
|
@@ -410,6 +383,7 @@ export class HttpAgent implements Agent {
|
|
|
410
383
|
} else if (log.level === 'warn') {
|
|
411
384
|
console.warn(log.message);
|
|
412
385
|
} else {
|
|
386
|
+
// eslint-disable-next-line no-console -- logToConsole opt-in
|
|
413
387
|
console.log(log.message);
|
|
414
388
|
}
|
|
415
389
|
});
|
|
@@ -537,7 +511,7 @@ export class HttpAgent implements Agent {
|
|
|
537
511
|
method: 'POST',
|
|
538
512
|
headers: {
|
|
539
513
|
'Content-Type': 'application/cbor',
|
|
540
|
-
...(this.#credentials ? { Authorization:
|
|
514
|
+
...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
|
|
541
515
|
},
|
|
542
516
|
},
|
|
543
517
|
endpoint: Endpoint.Call,
|
|
@@ -575,7 +549,7 @@ export class HttpAgent implements Agent {
|
|
|
575
549
|
// Apply transform for identity.
|
|
576
550
|
transformedRequest = (await id.transformRequest(transformedRequest)) as HttpAgentSubmitRequest;
|
|
577
551
|
|
|
578
|
-
const body = cbor.encode(transformedRequest.body)
|
|
552
|
+
const body = cbor.encode(transformedRequest.body) as Uint8Array<ArrayBuffer>;
|
|
579
553
|
const backoff = this.#backoffStrategy();
|
|
580
554
|
const requestId = requestIdOf(submit);
|
|
581
555
|
try {
|
|
@@ -634,21 +608,21 @@ export class HttpAgent implements Agent {
|
|
|
634
608
|
},
|
|
635
609
|
identity,
|
|
636
610
|
);
|
|
637
|
-
}
|
|
611
|
+
}
|
|
612
|
+
if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
|
|
638
613
|
// if there is an ingress expiry error and the time has not been synced yet,
|
|
639
614
|
// sync time with the network and try again
|
|
640
615
|
await this.syncTime(canister);
|
|
641
616
|
return this.call(canister, options, identity);
|
|
642
|
-
} else {
|
|
643
|
-
// override the error code to include the request details
|
|
644
|
-
error.code.requestContext = {
|
|
645
|
-
requestId,
|
|
646
|
-
senderPubKey: transformedRequest.body.sender_pubkey,
|
|
647
|
-
senderSignature: transformedRequest.body.sender_sig,
|
|
648
|
-
ingressExpiry: transformedRequest.body.content.ingress_expiry,
|
|
649
|
-
};
|
|
650
|
-
callError = error;
|
|
651
617
|
}
|
|
618
|
+
// override the error code to include the request details
|
|
619
|
+
error.code.requestContext = {
|
|
620
|
+
requestId,
|
|
621
|
+
senderPubKey: transformedRequest.body.sender_pubkey,
|
|
622
|
+
senderSignature: transformedRequest.body.sender_sig,
|
|
623
|
+
ingressExpiry: transformedRequest.body.content.ingress_expiry,
|
|
624
|
+
};
|
|
625
|
+
callError = error;
|
|
652
626
|
} else {
|
|
653
627
|
callError = UnknownError.fromCode(new UnexpectedErrorCode(error));
|
|
654
628
|
}
|
|
@@ -657,10 +631,146 @@ export class HttpAgent implements Agent {
|
|
|
657
631
|
}
|
|
658
632
|
}
|
|
659
633
|
|
|
634
|
+
/**
|
|
635
|
+
* Executes an update call to a canister and returns the certified result.
|
|
636
|
+
* @param canisterId The canister to call.
|
|
637
|
+
* @param fields The call options (method name, arg, effective canister ID, optional nonce).
|
|
638
|
+
* @param pollingOptions Optional polling configuration.
|
|
639
|
+
* @returns The certified result including the certificate, reply bytes, and raw certificate bytes.
|
|
640
|
+
*/
|
|
641
|
+
public async update(
|
|
642
|
+
canisterId: Principal | string,
|
|
643
|
+
fields: CallOptions,
|
|
644
|
+
pollingOptions: PollingOptions = {},
|
|
645
|
+
): Promise<UpdateResult> {
|
|
646
|
+
const effectiveCanisterId = Principal.from(fields.effectiveCanisterId);
|
|
647
|
+
const { requestId, response, requestDetails } = await this.call(canisterId, fields);
|
|
648
|
+
const { body, ...httpDetails } = response;
|
|
649
|
+
|
|
650
|
+
if (isV4ResponseBody(body)) {
|
|
651
|
+
return this.#handleV4SyncResponse(
|
|
652
|
+
body,
|
|
653
|
+
response,
|
|
654
|
+
requestId,
|
|
655
|
+
effectiveCanisterId,
|
|
656
|
+
fields.methodName,
|
|
657
|
+
httpDetails,
|
|
658
|
+
requestDetails,
|
|
659
|
+
pollingOptions,
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (isV2ResponseBody(body)) {
|
|
664
|
+
this.#handleV2Rejection(body, requestId, effectiveCanisterId, fields.methodName, httpDetails);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (response.status === 202) {
|
|
668
|
+
const pollResult = await pollForResponse(
|
|
669
|
+
this,
|
|
670
|
+
effectiveCanisterId,
|
|
671
|
+
requestId,
|
|
672
|
+
pollingOptions,
|
|
673
|
+
);
|
|
674
|
+
return { ...pollResult, requestDetails, callResponse: response };
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const unexpectedErrorCode = new UnexpectedErrorCode(
|
|
678
|
+
`Unexpected response from call: status ${response.status} with unrecognized body`,
|
|
679
|
+
);
|
|
680
|
+
unexpectedErrorCode.callContext = {
|
|
681
|
+
canisterId: effectiveCanisterId,
|
|
682
|
+
methodName: fields.methodName,
|
|
683
|
+
httpDetails,
|
|
684
|
+
};
|
|
685
|
+
throw UnknownError.fromCode(unexpectedErrorCode);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
async #handleV4SyncResponse(
|
|
689
|
+
body: v4ResponseBody,
|
|
690
|
+
response: SubmitResponse['response'],
|
|
691
|
+
requestId: RequestId,
|
|
692
|
+
effectiveCanisterId: Principal,
|
|
693
|
+
methodName: string,
|
|
694
|
+
httpDetails: HttpDetailsResponse,
|
|
695
|
+
requestDetails: UpdateResult['requestDetails'],
|
|
696
|
+
pollingOptions: PollingOptions,
|
|
697
|
+
): Promise<UpdateResult> {
|
|
698
|
+
if (this.rootKey == null) {
|
|
699
|
+
throw ExternalError.fromCode(new MissingRootKeyErrorCode());
|
|
700
|
+
}
|
|
701
|
+
const rawCertificate = body.certificate;
|
|
702
|
+
const certificate = await Certificate.create({
|
|
703
|
+
certificate: rawCertificate,
|
|
704
|
+
rootKey: this.rootKey,
|
|
705
|
+
principal: { canisterId: effectiveCanisterId },
|
|
706
|
+
blsVerify: pollingOptions.blsVerify,
|
|
707
|
+
agent: this,
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
const path = [utf8ToBytes('request_status'), requestId];
|
|
711
|
+
const maybeBuf = lookupResultToBuffer(
|
|
712
|
+
certificate.lookup_path([...path, utf8ToBytes('status')]),
|
|
713
|
+
);
|
|
714
|
+
const status = maybeBuf ? new TextDecoder().decode(maybeBuf) : undefined;
|
|
715
|
+
|
|
716
|
+
switch (status) {
|
|
717
|
+
case RequestStatusResponseStatus.Replied:
|
|
718
|
+
return {
|
|
719
|
+
reply: lookupResultToBuffer(certificate.lookup_path([...path, 'reply']))!,
|
|
720
|
+
certificate,
|
|
721
|
+
rawCertificate,
|
|
722
|
+
requestDetails,
|
|
723
|
+
callResponse: response,
|
|
724
|
+
};
|
|
725
|
+
case RequestStatusResponseStatus.Rejected: {
|
|
726
|
+
const error = readCertifiedReject(certificate, path, requestId);
|
|
727
|
+
error.callContext = {
|
|
728
|
+
canisterId: effectiveCanisterId,
|
|
729
|
+
methodName,
|
|
730
|
+
httpDetails,
|
|
731
|
+
};
|
|
732
|
+
throw RejectError.fromCode(error);
|
|
733
|
+
}
|
|
734
|
+
default: {
|
|
735
|
+
const unexpectedErrorCode = new UnexpectedErrorCode(
|
|
736
|
+
`Unexpected request status in v4 sync response: "${status}"`,
|
|
737
|
+
);
|
|
738
|
+
unexpectedErrorCode.callContext = {
|
|
739
|
+
canisterId: effectiveCanisterId,
|
|
740
|
+
methodName,
|
|
741
|
+
httpDetails,
|
|
742
|
+
};
|
|
743
|
+
throw UnknownError.fromCode(unexpectedErrorCode);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
#handleV2Rejection(
|
|
749
|
+
body: v2ResponseBody,
|
|
750
|
+
requestId: RequestId,
|
|
751
|
+
effectiveCanisterId: Principal,
|
|
752
|
+
methodName: string,
|
|
753
|
+
httpDetails: HttpDetailsResponse,
|
|
754
|
+
): never {
|
|
755
|
+
const { reject_code, reject_message, error_code } = body;
|
|
756
|
+
const errorCode = new UncertifiedRejectUpdateErrorCode(
|
|
757
|
+
requestId,
|
|
758
|
+
reject_code,
|
|
759
|
+
reject_message,
|
|
760
|
+
error_code,
|
|
761
|
+
);
|
|
762
|
+
errorCode.callContext = {
|
|
763
|
+
canisterId: effectiveCanisterId,
|
|
764
|
+
methodName,
|
|
765
|
+
httpDetails,
|
|
766
|
+
};
|
|
767
|
+
throw RejectError.fromCode(errorCode);
|
|
768
|
+
}
|
|
769
|
+
|
|
660
770
|
async #requestAndRetryQuery(args: {
|
|
661
771
|
ecid: Principal;
|
|
662
772
|
transformedRequest: HttpAgentRequest;
|
|
663
|
-
body: Uint8Array
|
|
773
|
+
body: Uint8Array<ArrayBuffer>;
|
|
664
774
|
requestId: RequestId;
|
|
665
775
|
backoff: BackoffStrategy;
|
|
666
776
|
tries: number;
|
|
@@ -820,7 +930,7 @@ export class HttpAgent implements Agent {
|
|
|
820
930
|
}
|
|
821
931
|
|
|
822
932
|
let response: Response;
|
|
823
|
-
let responseBodyBytes = new Uint8Array();
|
|
933
|
+
let responseBodyBytes: Uint8Array = new Uint8Array();
|
|
824
934
|
try {
|
|
825
935
|
response = await requestFn();
|
|
826
936
|
// According to the spec, only 200 responses have a non-empty body
|
|
@@ -917,7 +1027,7 @@ export class HttpAgent implements Agent {
|
|
|
917
1027
|
method: 'POST',
|
|
918
1028
|
headers: {
|
|
919
1029
|
'Content-Type': 'application/cbor',
|
|
920
|
-
...(this.#credentials ? { Authorization:
|
|
1030
|
+
...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
|
|
921
1031
|
},
|
|
922
1032
|
},
|
|
923
1033
|
endpoint: Endpoint.Query,
|
|
@@ -927,7 +1037,7 @@ export class HttpAgent implements Agent {
|
|
|
927
1037
|
// Apply transform for identity.
|
|
928
1038
|
transformedRequest = (await id.transformRequest(transformedRequest)) as HttpAgentRequest;
|
|
929
1039
|
|
|
930
|
-
const body = cbor.encode(transformedRequest.body)
|
|
1040
|
+
const body = cbor.encode(transformedRequest.body) as Uint8Array<ArrayBuffer>;
|
|
931
1041
|
|
|
932
1042
|
const args = {
|
|
933
1043
|
canister: canister.toText(),
|
|
@@ -948,19 +1058,6 @@ export class HttpAgent implements Agent {
|
|
|
948
1058
|
};
|
|
949
1059
|
};
|
|
950
1060
|
|
|
951
|
-
const getSubnetNodeKeys = async (): Promise<SubnetNodeKeys> => {
|
|
952
|
-
const cachedSubnetNodeKeys = this.#subnetKeys.get(ecid.toString());
|
|
953
|
-
if (cachedSubnetNodeKeys) {
|
|
954
|
-
return cachedSubnetNodeKeys;
|
|
955
|
-
}
|
|
956
|
-
await this.fetchSubnetKeys(ecid.toString());
|
|
957
|
-
const subnetNodeKeys = this.#subnetKeys.get(ecid.toString());
|
|
958
|
-
if (!subnetNodeKeys) {
|
|
959
|
-
throw TrustError.fromCode(new MissingSignatureErrorCode());
|
|
960
|
-
}
|
|
961
|
-
return subnetNodeKeys;
|
|
962
|
-
};
|
|
963
|
-
|
|
964
1061
|
try {
|
|
965
1062
|
if (!this.#verifyQuerySignatures) {
|
|
966
1063
|
// Skip verification if the user has disabled it
|
|
@@ -970,7 +1067,7 @@ export class HttpAgent implements Agent {
|
|
|
970
1067
|
// Make query and fetch subnet keys in parallel
|
|
971
1068
|
const [queryWithDetails, subnetNodeKeys] = await Promise.all([
|
|
972
1069
|
makeQuery(),
|
|
973
|
-
|
|
1070
|
+
this.fetchSubnetKeys(ecid),
|
|
974
1071
|
]);
|
|
975
1072
|
|
|
976
1073
|
try {
|
|
@@ -979,7 +1076,7 @@ export class HttpAgent implements Agent {
|
|
|
979
1076
|
// In case the node signatures have changed, refresh the subnet keys and try again
|
|
980
1077
|
this.log.warn('Query response verification failed. Retrying with fresh subnet keys.');
|
|
981
1078
|
this.#subnetKeys.delete(ecid.toString());
|
|
982
|
-
const updatedSubnetNodeKeys = await
|
|
1079
|
+
const updatedSubnetNodeKeys = await this.fetchSubnetKeys(ecid);
|
|
983
1080
|
return this.#verifyQueryResponse(queryWithDetails, updatedSubnetNodeKeys);
|
|
984
1081
|
}
|
|
985
1082
|
} catch (error) {
|
|
@@ -1023,6 +1120,13 @@ export class HttpAgent implements Agent {
|
|
|
1023
1120
|
}
|
|
1024
1121
|
const { status, signatures = [], requestId } = queryResponse;
|
|
1025
1122
|
|
|
1123
|
+
const maxSignatures = subnetNodeKeys.size;
|
|
1124
|
+
if (signatures.length > maxSignatures) {
|
|
1125
|
+
throw ProtocolError.fromCode(
|
|
1126
|
+
new ExcessiveSignaturesErrorCode(signatures.length, maxSignatures),
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1026
1130
|
for (const sig of signatures) {
|
|
1027
1131
|
const { timestamp, identity } = sig;
|
|
1028
1132
|
const nodeId = Principal.fromUint8Array(identity).toText();
|
|
@@ -1032,18 +1136,18 @@ export class HttpAgent implements Agent {
|
|
|
1032
1136
|
if (status === QueryResponseStatus.Replied) {
|
|
1033
1137
|
const { reply } = queryResponse;
|
|
1034
1138
|
hash = hashOfMap({
|
|
1035
|
-
status
|
|
1036
|
-
reply
|
|
1139
|
+
status,
|
|
1140
|
+
reply,
|
|
1037
1141
|
timestamp: BigInt(timestamp),
|
|
1038
1142
|
request_id: requestId,
|
|
1039
1143
|
});
|
|
1040
1144
|
} else if (status === QueryResponseStatus.Rejected) {
|
|
1041
1145
|
const { reject_code, reject_message, error_code } = queryResponse;
|
|
1042
1146
|
hash = hashOfMap({
|
|
1043
|
-
status
|
|
1044
|
-
reject_code
|
|
1045
|
-
reject_message
|
|
1046
|
-
error_code
|
|
1147
|
+
status,
|
|
1148
|
+
reject_code,
|
|
1149
|
+
reject_message,
|
|
1150
|
+
error_code,
|
|
1047
1151
|
timestamp: BigInt(timestamp),
|
|
1048
1152
|
request_id: requestId,
|
|
1049
1153
|
});
|
|
@@ -1083,7 +1187,7 @@ export class HttpAgent implements Agent {
|
|
|
1083
1187
|
method: 'POST',
|
|
1084
1188
|
headers: {
|
|
1085
1189
|
'Content-Type': 'application/cbor',
|
|
1086
|
-
...(this.#credentials ? { Authorization:
|
|
1190
|
+
...(this.#credentials ? { Authorization: `Basic ${btoa(this.#credentials)}` } : {}),
|
|
1087
1191
|
},
|
|
1088
1192
|
},
|
|
1089
1193
|
endpoint: Endpoint.ReadState,
|
|
@@ -1251,9 +1355,9 @@ export class HttpAgent implements Agent {
|
|
|
1251
1355
|
this.log.print('Time from response:', date);
|
|
1252
1356
|
this.log.print('Time from response in milliseconds:', date.getTime());
|
|
1253
1357
|
return date.getTime();
|
|
1254
|
-
} else {
|
|
1255
|
-
this.log.warn('No certificate found in response');
|
|
1256
1358
|
}
|
|
1359
|
+
this.log.warn('No certificate found in response');
|
|
1360
|
+
|
|
1257
1361
|
return 0;
|
|
1258
1362
|
}
|
|
1259
1363
|
|
|
@@ -1384,7 +1488,7 @@ export class HttpAgent implements Agent {
|
|
|
1384
1488
|
public async status(): Promise<JsonObject> {
|
|
1385
1489
|
const headers: Record<string, string> = this.#credentials
|
|
1386
1490
|
? {
|
|
1387
|
-
Authorization:
|
|
1491
|
+
Authorization: `Basic ${btoa(this.#credentials)}`,
|
|
1388
1492
|
}
|
|
1389
1493
|
: {};
|
|
1390
1494
|
|
|
@@ -1424,7 +1528,8 @@ export class HttpAgent implements Agent {
|
|
|
1424
1528
|
async #rootKeyGuard(): Promise<void> {
|
|
1425
1529
|
if (this.rootKey) {
|
|
1426
1530
|
return;
|
|
1427
|
-
}
|
|
1531
|
+
}
|
|
1532
|
+
if (
|
|
1428
1533
|
this.rootKey === null &&
|
|
1429
1534
|
this.host.toString() !== 'https://icp-api.io' &&
|
|
1430
1535
|
this.#shouldFetchRootKey
|
|
@@ -1451,6 +1556,28 @@ export class HttpAgent implements Agent {
|
|
|
1451
1556
|
|
|
1452
1557
|
public async fetchSubnetKeys(canisterId: Principal | string): Promise<SubnetNodeKeys> {
|
|
1453
1558
|
const effectiveCanisterId: Principal = Principal.from(canisterId);
|
|
1559
|
+
|
|
1560
|
+
// Return cached result if available within the TTL.
|
|
1561
|
+
const cached = this.#subnetKeys.get(effectiveCanisterId.toText());
|
|
1562
|
+
if (cached) {
|
|
1563
|
+
return cached;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
// Deduplicate parallel requests for the same canister so that concurrent
|
|
1567
|
+
// query calls share a single read_state round-trip instead of each
|
|
1568
|
+
// issuing their own.
|
|
1569
|
+
const inflight = this.#subnetKeysFetching.get(effectiveCanisterId.toText());
|
|
1570
|
+
if (inflight) {
|
|
1571
|
+
return inflight;
|
|
1572
|
+
}
|
|
1573
|
+
const fetchPromise = this.#doFetchSubnetKeys(effectiveCanisterId).finally(() => {
|
|
1574
|
+
this.#subnetKeysFetching.delete(effectiveCanisterId.toText());
|
|
1575
|
+
});
|
|
1576
|
+
this.#subnetKeysFetching.set(effectiveCanisterId.toText(), fetchPromise);
|
|
1577
|
+
return fetchPromise;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
async #doFetchSubnetKeys(effectiveCanisterId: Principal): Promise<SubnetNodeKeys> {
|
|
1454
1581
|
await this.#asyncGuard(effectiveCanisterId);
|
|
1455
1582
|
|
|
1456
1583
|
const rootKey = this.rootKey!;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { lebEncode } from '#candid';
|
|
2
1
|
import {
|
|
3
2
|
Endpoint,
|
|
4
3
|
type HttpAgentRequest,
|
|
@@ -7,112 +6,9 @@ import {
|
|
|
7
6
|
makeNonce,
|
|
8
7
|
type Nonce,
|
|
9
8
|
} from './types.ts';
|
|
10
|
-
import {
|
|
9
|
+
import { Expiry } from './expiry.ts';
|
|
11
10
|
|
|
12
|
-
export
|
|
13
|
-
const SECONDS_TO_MILLISECONDS = BigInt(1_000);
|
|
14
|
-
const MILLISECONDS_TO_NANOSECONDS = BigInt(1_000_000);
|
|
15
|
-
const MINUTES_TO_SECONDS = BigInt(60);
|
|
16
|
-
const MINUTES_TO_MILLISECONDS = MINUTES_TO_SECONDS * SECONDS_TO_MILLISECONDS;
|
|
17
|
-
|
|
18
|
-
const EXPIRY_DELTA_THRESHOLD_MILLISECONDS = BigInt(1) * MINUTES_TO_MILLISECONDS;
|
|
19
|
-
|
|
20
|
-
function roundToSecond(millis: bigint): bigint {
|
|
21
|
-
return (millis / SECONDS_TO_MILLISECONDS) * SECONDS_TO_MILLISECONDS;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function roundToMinute(millis: bigint): bigint {
|
|
25
|
-
return (millis / MINUTES_TO_MILLISECONDS) * MINUTES_TO_MILLISECONDS;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type JsonnableExpiry = {
|
|
29
|
-
[JSON_KEY_EXPIRY]: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export class Expiry {
|
|
33
|
-
public readonly _isExpiry = true;
|
|
34
|
-
|
|
35
|
-
private constructor(private readonly __expiry__: bigint) {}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Creates an Expiry object from a delta in milliseconds.
|
|
39
|
-
* The expiry is calculated as: current_time + delta + clock_drift
|
|
40
|
-
* The resulting expiry is then rounded:
|
|
41
|
-
* - If rounding down to the nearest minute still provides at least 60 seconds in the future, use minute precision
|
|
42
|
-
* - Otherwise, use second precision
|
|
43
|
-
* @param deltaInMs The milliseconds to add to the current time.
|
|
44
|
-
* @param clockDriftInMs The milliseconds to add to the current time, typically the clock drift between IC network clock and the client's clock. Defaults to `0` if not provided.
|
|
45
|
-
* @returns {Expiry} The constructed Expiry object.
|
|
46
|
-
*/
|
|
47
|
-
public static fromDeltaInMilliseconds(deltaInMs: number, clockDriftInMs: number = 0): Expiry {
|
|
48
|
-
const correctedNowMs = BigInt(Date.now()) + BigInt(clockDriftInMs);
|
|
49
|
-
const expiryMs = correctedNowMs + BigInt(deltaInMs);
|
|
50
|
-
|
|
51
|
-
const roundedToMinute = roundToMinute(expiryMs);
|
|
52
|
-
|
|
53
|
-
let roundedExpiryMs: bigint;
|
|
54
|
-
if (roundedToMinute >= correctedNowMs + EXPIRY_DELTA_THRESHOLD_MILLISECONDS) {
|
|
55
|
-
roundedExpiryMs = roundedToMinute;
|
|
56
|
-
} else {
|
|
57
|
-
const roundedToSecond = roundToSecond(expiryMs);
|
|
58
|
-
roundedExpiryMs = roundedToSecond;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return new Expiry(roundedExpiryMs * MILLISECONDS_TO_NANOSECONDS);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public toBigInt(): bigint {
|
|
65
|
-
return this.__expiry__;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public toHash(): Uint8Array {
|
|
69
|
-
return lebEncode(this.__expiry__);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public toString(): string {
|
|
73
|
-
return this.__expiry__.toString();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Serializes to JSON
|
|
78
|
-
* @returns {JsonnableExpiry} a JSON object with a single key, {@link JSON_KEY_EXPIRY}, whose value is the expiry as a string
|
|
79
|
-
*/
|
|
80
|
-
public toJSON(): JsonnableExpiry {
|
|
81
|
-
return { [JSON_KEY_EXPIRY]: this.toString() };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Deserializes a {@link JsonnableExpiry} object from a JSON string.
|
|
86
|
-
* @param input The JSON string to deserialize.
|
|
87
|
-
* @returns {Expiry} The deserialized Expiry object.
|
|
88
|
-
*/
|
|
89
|
-
public static fromJSON(input: string): Expiry {
|
|
90
|
-
const obj = JSON.parse(input);
|
|
91
|
-
if (obj[JSON_KEY_EXPIRY]) {
|
|
92
|
-
try {
|
|
93
|
-
const expiry = BigInt(obj[JSON_KEY_EXPIRY]);
|
|
94
|
-
return new Expiry(expiry);
|
|
95
|
-
} catch (error) {
|
|
96
|
-
throw new InputError(new ExpiryJsonDeserializeErrorCode(`Not a valid BigInt: ${error}`));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
throw new InputError(
|
|
100
|
-
new ExpiryJsonDeserializeErrorCode(`The input does not contain the key ${JSON_KEY_EXPIRY}`),
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public static isExpiry(other: unknown): other is Expiry {
|
|
105
|
-
return (
|
|
106
|
-
other instanceof Expiry ||
|
|
107
|
-
(typeof other === 'object' &&
|
|
108
|
-
other !== null &&
|
|
109
|
-
'_isExpiry' in other &&
|
|
110
|
-
(other as { _isExpiry: boolean })['_isExpiry'] === true &&
|
|
111
|
-
'__expiry__' in other &&
|
|
112
|
-
typeof (other as { __expiry__: bigint })['__expiry__'] === 'bigint')
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
11
|
+
export { Expiry, JSON_KEY_EXPIRY, type JsonnableExpiry } from './expiry.ts';
|
|
116
12
|
|
|
117
13
|
/**
|
|
118
14
|
* Create a Nonce transform, which takes a function that returns a Buffer, and adds it
|
package/src/agent/auth.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Principal } from '#principal';
|
|
2
|
-
import {
|
|
2
|
+
import type { HttpAgentRequest } from './agent/http/types.ts';
|
|
3
3
|
import { requestIdOf } from './request_id.ts';
|
|
4
4
|
import { bytesToHex, concatBytes } from '@noble/hashes/utils';
|
|
5
5
|
import { IC_REQUEST_DOMAIN_SEPARATOR } from './constants.ts';
|
|
@@ -70,13 +70,13 @@ export interface CanisterEnv {
|
|
|
70
70
|
* Options for the {@link getCanisterEnv} function
|
|
71
71
|
* @experimental
|
|
72
72
|
*/
|
|
73
|
-
export
|
|
73
|
+
export interface GetCanisterEnvOptions {
|
|
74
74
|
/**
|
|
75
75
|
* The name of the cookie to get the environment variables from.
|
|
76
76
|
* @default 'ic_env'
|
|
77
77
|
*/
|
|
78
78
|
cookieName?: string;
|
|
79
|
-
}
|
|
79
|
+
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
82
|
* Get the environment variables served by the asset canister via the cookie.
|