@icp-sdk/core 5.0.0-beta.0 → 5.0.0-beta.2
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/README.md +0 -2
- package/lib/cjs/agent/actor.js +2 -2
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +6 -6
- package/lib/cjs/agent/agent/api.js +4 -4
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts +30 -0
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js +33 -1
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -1
- package/lib/cjs/agent/agent/http/index.d.ts +23 -5
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +201 -86
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/agent/index.d.ts +2 -0
- package/lib/cjs/agent/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/index.js +2 -0
- package/lib/cjs/agent/agent/index.js.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts +18 -50
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +30 -97
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +68 -10
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +198 -27
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/constants.d.ts +4 -0
- package/lib/cjs/agent/constants.d.ts.map +1 -1
- package/lib/cjs/agent/constants.js +5 -1
- package/lib/cjs/agent/constants.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +9 -2
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +19 -7
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/index.d.ts +97 -5
- package/lib/cjs/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/index.js +92 -11
- package/lib/cjs/agent/index.js.map +1 -1
- package/lib/cjs/agent/polling/index.js +1 -1
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.d.ts +70 -0
- package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -0
- package/lib/cjs/agent/subnetStatus/index.js +219 -0
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -0
- package/lib/cjs/agent/utils/readState.d.ts +84 -0
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -0
- package/lib/cjs/agent/utils/readState.js +149 -0
- package/lib/cjs/agent/utils/readState.js.map +1 -0
- package/lib/cjs/candid/index.d.ts +20 -1
- package/lib/cjs/candid/index.d.ts.map +1 -1
- package/lib/cjs/candid/index.js +20 -1
- package/lib/cjs/candid/index.js.map +1 -1
- package/lib/cjs/identity/index.d.ts +30 -7
- package/lib/cjs/identity/index.d.ts.map +1 -1
- package/lib/cjs/identity/index.js +31 -11
- package/lib/cjs/identity/index.js.map +1 -1
- package/lib/cjs/identity/secp256k1/index.d.ts +48 -1
- package/lib/cjs/identity/secp256k1/index.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/index.js +48 -1
- package/lib/cjs/identity/secp256k1/index.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.js +1 -2
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
- package/lib/cjs/principal/index.d.ts +15 -0
- package/lib/cjs/principal/index.d.ts.map +1 -1
- package/lib/cjs/principal/index.js +15 -0
- package/lib/cjs/principal/index.js.map +1 -1
- package/lib/esm/agent/actor.js +3 -3
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +6 -6
- package/lib/esm/agent/agent/api.js +3 -3
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts +30 -0
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js +33 -1
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -1
- package/lib/esm/agent/agent/http/index.d.ts +23 -5
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +203 -84
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/agent/index.d.ts +2 -0
- package/lib/esm/agent/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/index.js +2 -0
- package/lib/esm/agent/agent/index.js.map +1 -1
- package/lib/esm/agent/canister-env/index.d.ts +2 -1
- package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
- package/lib/esm/agent/canister-env/index.js +2 -1
- package/lib/esm/agent/canister-env/index.js.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts +18 -50
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +31 -98
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +68 -10
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +196 -29
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/constants.d.ts +4 -0
- package/lib/esm/agent/constants.d.ts.map +1 -1
- package/lib/esm/agent/constants.js +4 -0
- package/lib/esm/agent/constants.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +9 -2
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +15 -4
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/index.d.ts +97 -5
- package/lib/esm/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/index.js +97 -5
- package/lib/esm/agent/index.js.map +1 -1
- package/lib/esm/agent/polling/index.js +1 -1
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.d.ts +70 -0
- package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -0
- package/lib/esm/agent/subnetStatus/index.js +178 -0
- package/lib/esm/agent/subnetStatus/index.js.map +1 -0
- package/lib/esm/agent/utils/readState.d.ts +84 -0
- package/lib/esm/agent/utils/readState.d.ts.map +1 -0
- package/lib/esm/agent/utils/readState.js +107 -0
- package/lib/esm/agent/utils/readState.js.map +1 -0
- package/lib/esm/candid/index.d.ts +20 -1
- package/lib/esm/candid/index.d.ts.map +1 -1
- package/lib/esm/candid/index.js +20 -1
- package/lib/esm/candid/index.js.map +1 -1
- package/lib/esm/identity/index.d.ts +30 -7
- package/lib/esm/identity/index.d.ts.map +1 -1
- package/lib/esm/identity/index.js +30 -9
- package/lib/esm/identity/index.js.map +1 -1
- package/lib/esm/identity/secp256k1/index.d.ts +48 -1
- package/lib/esm/identity/secp256k1/index.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/index.js +48 -1
- package/lib/esm/identity/secp256k1/index.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/pem.js +1 -2
- package/lib/esm/identity/secp256k1/pem.js.map +1 -1
- package/lib/esm/principal/index.d.ts +15 -0
- package/lib/esm/principal/index.d.ts.map +1 -1
- package/lib/esm/principal/index.js +15 -0
- package/lib/esm/principal/index.js.map +1 -1
- package/package.json +3 -1
- package/src/agent/actor.ts +3 -3
- package/src/agent/agent/api.ts +8 -8
- package/src/agent/agent/http/index.ts +259 -97
- package/src/agent/agent/index.ts +2 -0
- package/src/agent/canister-env/index.ts +2 -1
- package/src/agent/canisterStatus/index.ts +46 -196
- package/src/agent/certificate.ts +271 -52
- package/src/agent/constants.ts +5 -0
- package/src/agent/errors.ts +19 -5
- package/src/agent/index.ts +97 -5
- package/src/agent/polling/index.ts +1 -1
- package/src/agent/subnetStatus/index.ts +265 -0
- package/src/agent/utils/readState.ts +180 -0
- package/src/candid/index.ts +20 -1
- package/src/identity/index.ts +30 -10
- package/src/identity/secp256k1/index.ts +48 -1
- package/src/identity/secp256k1/pem.ts +1 -2
- package/src/principal/index.ts +16 -0
|
@@ -20,12 +20,14 @@ import {
|
|
|
20
20
|
UnexpectedErrorCode,
|
|
21
21
|
UnknownError,
|
|
22
22
|
HttpErrorCode,
|
|
23
|
-
|
|
23
|
+
HttpV4ApiNotSupportedErrorCode,
|
|
24
24
|
TransportError,
|
|
25
25
|
HttpFetchErrorCode,
|
|
26
26
|
AgentError,
|
|
27
27
|
MalformedLookupFoundValueErrorCode,
|
|
28
28
|
CertificateOutdatedErrorCode,
|
|
29
|
+
CertificateNotAuthorizedErrorCode,
|
|
30
|
+
UNREACHABLE_ERROR,
|
|
29
31
|
} from '../../errors.ts';
|
|
30
32
|
import { AnonymousIdentity, type Identity } from '../../auth.ts';
|
|
31
33
|
import * as cbor from '../../cbor.ts';
|
|
@@ -55,8 +57,17 @@ import {
|
|
|
55
57
|
type ReadStateRequest,
|
|
56
58
|
type HttpHeaderField,
|
|
57
59
|
} from './types.ts';
|
|
58
|
-
import {
|
|
59
|
-
import {
|
|
60
|
+
import { request as canisterStatusRequest } from '../../canisterStatus/index.ts';
|
|
61
|
+
import { request as subnetStatusRequest } from '../../subnetStatus/index.ts';
|
|
62
|
+
import { lookupNodeKeysFromCertificate, type SubnetNodeKeys } from '../../utils/readState.ts';
|
|
63
|
+
import {
|
|
64
|
+
Certificate,
|
|
65
|
+
check_canister_ranges,
|
|
66
|
+
getSubnetIdFromCertificate,
|
|
67
|
+
type HashTree,
|
|
68
|
+
lookup_path,
|
|
69
|
+
LookupPathStatus,
|
|
70
|
+
} from '../../certificate.ts';
|
|
60
71
|
import { ed25519 } from '@noble/curves/ed25519';
|
|
61
72
|
import { ExpirableMap } from '../../utils/expirableMap.ts';
|
|
62
73
|
import { Ed25519PublicKey } from '../../public_key.ts';
|
|
@@ -67,11 +78,9 @@ import {
|
|
|
67
78
|
ExponentialBackoff,
|
|
68
79
|
} from '../../polling/backoff.ts';
|
|
69
80
|
import { decodeTime } from '../../utils/leb.ts';
|
|
70
|
-
import { concatBytes, hexToBytes } from '@noble/hashes/utils';
|
|
81
|
+
import { concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
|
|
71
82
|
import { uint8Equals, uint8FromBufLike } from '../../utils/buffer.ts';
|
|
72
83
|
import { IC_RESPONSE_DOMAIN_SEPARATOR } from '../../constants.ts';
|
|
73
|
-
export * from './transforms.ts';
|
|
74
|
-
export { type Nonce, makeNonce } from './types.ts';
|
|
75
84
|
|
|
76
85
|
export enum RequestStatusResponseStatus {
|
|
77
86
|
Received = 'received',
|
|
@@ -315,7 +324,7 @@ export class HttpAgent implements Agent {
|
|
|
315
324
|
#queryPipeline: HttpAgentRequestTransformFn[] = [];
|
|
316
325
|
#updatePipeline: HttpAgentRequestTransformFn[] = [];
|
|
317
326
|
|
|
318
|
-
#subnetKeys: ExpirableMap<string,
|
|
327
|
+
#subnetKeys: ExpirableMap<string, SubnetNodeKeys> = new ExpirableMap({
|
|
319
328
|
expirationTime: 5 * MINUTE_TO_MSECS,
|
|
320
329
|
});
|
|
321
330
|
#verifyQuerySignatures = true;
|
|
@@ -570,9 +579,9 @@ export class HttpAgent implements Agent {
|
|
|
570
579
|
const backoff = this.#backoffStrategy();
|
|
571
580
|
const requestId = requestIdOf(submit);
|
|
572
581
|
try {
|
|
573
|
-
// Attempt
|
|
582
|
+
// Attempt v4 sync call
|
|
574
583
|
const requestSync = () => {
|
|
575
|
-
const url = new URL(`/api/
|
|
584
|
+
const url = new URL(`/api/v4/canister/${ecid.toText()}/call`, this.host);
|
|
576
585
|
this.log.print(`fetching "${url.pathname}" with request:`, transformedRequest);
|
|
577
586
|
return this.#fetch(url, {
|
|
578
587
|
...this.#callOptions,
|
|
@@ -613,14 +622,14 @@ export class HttpAgent implements Agent {
|
|
|
613
622
|
} catch (error) {
|
|
614
623
|
let callError: AgentError;
|
|
615
624
|
if (error instanceof AgentError) {
|
|
616
|
-
// If the error is due to the
|
|
617
|
-
if (error.hasCode(
|
|
618
|
-
this.log.warn('
|
|
625
|
+
// If the error is due to the v4 api not being supported, fall back to v2
|
|
626
|
+
if (error.hasCode(HttpV4ApiNotSupportedErrorCode)) {
|
|
627
|
+
this.log.warn('v4 api not supported. Fall back to v2');
|
|
619
628
|
return this.call(
|
|
620
629
|
canisterId,
|
|
621
630
|
{
|
|
622
631
|
...options,
|
|
623
|
-
// disable
|
|
632
|
+
// disable v4 api
|
|
624
633
|
callSync: false,
|
|
625
634
|
},
|
|
626
635
|
identity,
|
|
@@ -660,7 +669,7 @@ export class HttpAgent implements Agent {
|
|
|
660
669
|
|
|
661
670
|
const delay = tries === 0 ? 0 : backoff.next();
|
|
662
671
|
|
|
663
|
-
const url = new URL(`/api/
|
|
672
|
+
const url = new URL(`/api/v3/canister/${ecid.toString()}/query`, this.host);
|
|
664
673
|
|
|
665
674
|
this.log.print(`fetching "${url.pathname}" with tries:`, {
|
|
666
675
|
tries,
|
|
@@ -681,39 +690,16 @@ export class HttpAgent implements Agent {
|
|
|
681
690
|
if (delay > 0) {
|
|
682
691
|
await new Promise(resolve => setTimeout(resolve, delay));
|
|
683
692
|
}
|
|
684
|
-
|
|
693
|
+
|
|
694
|
+
let fetchResponse: Response;
|
|
685
695
|
// Make the request and retry if it throws an error
|
|
686
696
|
try {
|
|
687
697
|
this.log.print(`fetching "${url.pathname}" with request:`, transformedRequest);
|
|
688
|
-
|
|
698
|
+
fetchResponse = await this.#fetch(url, {
|
|
689
699
|
...this.#fetchOptions,
|
|
690
700
|
...transformedRequest.request,
|
|
691
701
|
body,
|
|
692
702
|
});
|
|
693
|
-
if (fetchResponse.status === HTTP_STATUS_OK) {
|
|
694
|
-
const queryResponse: QueryResponse = cbor.decode(
|
|
695
|
-
uint8FromBufLike(await fetchResponse.arrayBuffer()),
|
|
696
|
-
);
|
|
697
|
-
response = {
|
|
698
|
-
...queryResponse,
|
|
699
|
-
httpDetails: {
|
|
700
|
-
ok: fetchResponse.ok,
|
|
701
|
-
status: fetchResponse.status,
|
|
702
|
-
statusText: fetchResponse.statusText,
|
|
703
|
-
headers: httpHeadersTransform(fetchResponse.headers),
|
|
704
|
-
},
|
|
705
|
-
requestId,
|
|
706
|
-
};
|
|
707
|
-
} else {
|
|
708
|
-
throw ProtocolError.fromCode(
|
|
709
|
-
new HttpErrorCode(
|
|
710
|
-
fetchResponse.status,
|
|
711
|
-
fetchResponse.statusText,
|
|
712
|
-
httpHeadersTransform(fetchResponse.headers),
|
|
713
|
-
await fetchResponse.text(),
|
|
714
|
-
),
|
|
715
|
-
);
|
|
716
|
-
}
|
|
717
703
|
} catch (error) {
|
|
718
704
|
if (tries < this.#retryTimes) {
|
|
719
705
|
this.log.warn(
|
|
@@ -723,14 +709,43 @@ export class HttpAgent implements Agent {
|
|
|
723
709
|
);
|
|
724
710
|
return await this.#requestAndRetryQuery({ ...args, tries: tries + 1 });
|
|
725
711
|
}
|
|
726
|
-
if (error instanceof AgentError) {
|
|
727
|
-
// if it's an error that we have thrown, just throw it as is
|
|
728
|
-
throw error;
|
|
729
|
-
}
|
|
730
|
-
// if it's an error that we have not thrown, wrap it in a TransportError
|
|
731
712
|
throw TransportError.fromCode(new HttpFetchErrorCode(error));
|
|
732
713
|
}
|
|
733
714
|
|
|
715
|
+
const headers = httpHeadersTransform(fetchResponse.headers);
|
|
716
|
+
|
|
717
|
+
if (fetchResponse.status !== HTTP_STATUS_OK) {
|
|
718
|
+
const responseText = await fetchResponse.text();
|
|
719
|
+
|
|
720
|
+
if (isIngressExpiryInvalidResponse(responseText)) {
|
|
721
|
+
throw InputError.fromCode(
|
|
722
|
+
new IngressExpiryInvalidErrorCode(responseText, this.#maxIngressExpiryInMinutes),
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (tries < this.#retryTimes) {
|
|
727
|
+
return await this.#requestAndRetryQuery({ ...args, tries: tries + 1 });
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
throw ProtocolError.fromCode(
|
|
731
|
+
new HttpErrorCode(fetchResponse.status, fetchResponse.statusText, headers, responseText),
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const queryResponse: QueryResponse = cbor.decode(
|
|
736
|
+
uint8FromBufLike(await fetchResponse.arrayBuffer()),
|
|
737
|
+
);
|
|
738
|
+
const response: ApiQueryResponse = {
|
|
739
|
+
...queryResponse,
|
|
740
|
+
httpDetails: {
|
|
741
|
+
ok: fetchResponse.ok,
|
|
742
|
+
status: fetchResponse.status,
|
|
743
|
+
statusText: fetchResponse.statusText,
|
|
744
|
+
headers,
|
|
745
|
+
},
|
|
746
|
+
requestId,
|
|
747
|
+
};
|
|
748
|
+
|
|
734
749
|
// Skip timestamp verification if the user has set verifyQuerySignatures to false
|
|
735
750
|
if (!this.#verifyQuerySignatures) {
|
|
736
751
|
return response;
|
|
@@ -757,6 +772,7 @@ export class HttpAgent implements Agent {
|
|
|
757
772
|
requestId,
|
|
758
773
|
signatureTimestampMs,
|
|
759
774
|
});
|
|
775
|
+
await this.syncTime(ecid);
|
|
760
776
|
return await this.#requestAndRetryQuery({ ...args, tries: tries + 1 });
|
|
761
777
|
}
|
|
762
778
|
throw TrustError.fromCode(
|
|
@@ -839,12 +855,11 @@ export class HttpAgent implements Agent {
|
|
|
839
855
|
|
|
840
856
|
const responseText = await response.text();
|
|
841
857
|
|
|
842
|
-
if (response.status === HTTP_STATUS_NOT_FOUND && response.url.includes('api/
|
|
843
|
-
throw ProtocolError.fromCode(new
|
|
858
|
+
if (response.status === HTTP_STATUS_NOT_FOUND && response.url.includes('api/v4')) {
|
|
859
|
+
throw ProtocolError.fromCode(new HttpV4ApiNotSupportedErrorCode());
|
|
844
860
|
}
|
|
845
861
|
|
|
846
|
-
|
|
847
|
-
if (responseText.startsWith('Invalid request expiry: ')) {
|
|
862
|
+
if (isIngressExpiryInvalidResponse(responseText)) {
|
|
848
863
|
throw InputError.fromCode(
|
|
849
864
|
new IngressExpiryInvalidErrorCode(responseText, this.#maxIngressExpiryInMinutes),
|
|
850
865
|
);
|
|
@@ -933,17 +948,17 @@ export class HttpAgent implements Agent {
|
|
|
933
948
|
};
|
|
934
949
|
};
|
|
935
950
|
|
|
936
|
-
const
|
|
937
|
-
const
|
|
938
|
-
if (
|
|
939
|
-
return
|
|
951
|
+
const getSubnetNodeKeys = async (): Promise<SubnetNodeKeys> => {
|
|
952
|
+
const cachedSubnetNodeKeys = this.#subnetKeys.get(ecid.toString());
|
|
953
|
+
if (cachedSubnetNodeKeys) {
|
|
954
|
+
return cachedSubnetNodeKeys;
|
|
940
955
|
}
|
|
941
956
|
await this.fetchSubnetKeys(ecid.toString());
|
|
942
|
-
const
|
|
943
|
-
if (!
|
|
957
|
+
const subnetNodeKeys = this.#subnetKeys.get(ecid.toString());
|
|
958
|
+
if (!subnetNodeKeys) {
|
|
944
959
|
throw TrustError.fromCode(new MissingSignatureErrorCode());
|
|
945
960
|
}
|
|
946
|
-
return
|
|
961
|
+
return subnetNodeKeys;
|
|
947
962
|
};
|
|
948
963
|
|
|
949
964
|
try {
|
|
@@ -953,20 +968,29 @@ export class HttpAgent implements Agent {
|
|
|
953
968
|
}
|
|
954
969
|
|
|
955
970
|
// Make query and fetch subnet keys in parallel
|
|
956
|
-
const [queryWithDetails,
|
|
971
|
+
const [queryWithDetails, subnetNodeKeys] = await Promise.all([
|
|
972
|
+
makeQuery(),
|
|
973
|
+
getSubnetNodeKeys(),
|
|
974
|
+
]);
|
|
957
975
|
|
|
958
976
|
try {
|
|
959
|
-
return this.#verifyQueryResponse(queryWithDetails,
|
|
977
|
+
return this.#verifyQueryResponse(queryWithDetails, subnetNodeKeys);
|
|
960
978
|
} catch {
|
|
961
979
|
// In case the node signatures have changed, refresh the subnet keys and try again
|
|
962
980
|
this.log.warn('Query response verification failed. Retrying with fresh subnet keys.');
|
|
963
981
|
this.#subnetKeys.delete(ecid.toString());
|
|
964
|
-
const
|
|
965
|
-
return this.#verifyQueryResponse(queryWithDetails,
|
|
982
|
+
const updatedSubnetNodeKeys = await getSubnetNodeKeys();
|
|
983
|
+
return this.#verifyQueryResponse(queryWithDetails, updatedSubnetNodeKeys);
|
|
966
984
|
}
|
|
967
985
|
} catch (error) {
|
|
968
986
|
let queryError: AgentError;
|
|
969
987
|
if (error instanceof AgentError) {
|
|
988
|
+
if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
|
|
989
|
+
// if there is an ingress expiry error and the time has not been synced yet,
|
|
990
|
+
// sync time with the network and try again
|
|
991
|
+
await this.syncTime(ecid);
|
|
992
|
+
return this.query(canisterId, fields, identity);
|
|
993
|
+
}
|
|
970
994
|
// override the error code to include the request details
|
|
971
995
|
error.code.requestContext = {
|
|
972
996
|
requestId,
|
|
@@ -986,12 +1010,12 @@ export class HttpAgent implements Agent {
|
|
|
986
1010
|
/**
|
|
987
1011
|
* See https://internetcomputer.org/docs/current/references/ic-interface-spec/#http-query for details on validation
|
|
988
1012
|
* @param queryResponse - The response from the query
|
|
989
|
-
* @param
|
|
1013
|
+
* @param subnetNodeKeys - The subnet node keys
|
|
990
1014
|
* @returns ApiQueryResponse
|
|
991
1015
|
*/
|
|
992
1016
|
#verifyQueryResponse = (
|
|
993
1017
|
queryResponse: ApiQueryResponse,
|
|
994
|
-
|
|
1018
|
+
subnetNodeKeys: SubnetNodeKeys,
|
|
995
1019
|
): ApiQueryResponse => {
|
|
996
1020
|
if (this.#verifyQuerySignatures === false) {
|
|
997
1021
|
// This should not be called if the user has disabled verification
|
|
@@ -1029,16 +1053,15 @@ export class HttpAgent implements Agent {
|
|
|
1029
1053
|
|
|
1030
1054
|
const separatorWithHash = concatBytes(IC_RESPONSE_DOMAIN_SEPARATOR, hash);
|
|
1031
1055
|
|
|
1032
|
-
|
|
1033
|
-
const pubKey = subnetStatus.nodeKeys.get(nodeId);
|
|
1056
|
+
const pubKey = subnetNodeKeys.get(nodeId);
|
|
1034
1057
|
if (!pubKey) {
|
|
1035
1058
|
throw ProtocolError.fromCode(new MalformedPublicKeyErrorCode());
|
|
1036
1059
|
}
|
|
1037
1060
|
const rawKey = Ed25519PublicKey.fromDer(pubKey).rawKey;
|
|
1038
1061
|
const valid = ed25519.verify(sig.signature, separatorWithHash, rawKey);
|
|
1039
|
-
if (valid)
|
|
1040
|
-
|
|
1041
|
-
|
|
1062
|
+
if (!valid) {
|
|
1063
|
+
throw TrustError.fromCode(new QuerySignatureVerificationFailedErrorCode(nodeId));
|
|
1064
|
+
}
|
|
1042
1065
|
}
|
|
1043
1066
|
return queryResponse;
|
|
1044
1067
|
};
|
|
@@ -1106,7 +1129,6 @@ export class HttpAgent implements Agent {
|
|
|
1106
1129
|
if (request) {
|
|
1107
1130
|
// This is a pre-signed request
|
|
1108
1131
|
transformedRequest = request;
|
|
1109
|
-
requestId = requestIdOf(transformedRequest);
|
|
1110
1132
|
} else {
|
|
1111
1133
|
// This is fields, we need to create a request
|
|
1112
1134
|
requestId = getRequestId(fields);
|
|
@@ -1119,10 +1141,39 @@ export class HttpAgent implements Agent {
|
|
|
1119
1141
|
transformedRequest = await this.createReadStateRequest(fields, identity);
|
|
1120
1142
|
}
|
|
1121
1143
|
|
|
1122
|
-
const url = new URL(`/api/
|
|
1144
|
+
const url = new URL(`/api/v3/canister/${canister.toString()}/read_state`, this.host);
|
|
1123
1145
|
|
|
1124
|
-
this
|
|
1146
|
+
return await this.#readStateInner(url, { canisterId: canister }, transformedRequest, requestId);
|
|
1147
|
+
}
|
|
1125
1148
|
|
|
1149
|
+
/**
|
|
1150
|
+
* Reads the state of a subnet from the `/api/v3/subnet/{subnetId}/read_state` endpoint.
|
|
1151
|
+
* @param subnetId The ID of the subnet to read the state of. If you have a canister ID, you can use {@link HttpAgent.getSubnetIdFromCanister | getSubnetIdFromCanister} to get the subnet ID.
|
|
1152
|
+
* @param options The options for the read state request.
|
|
1153
|
+
* @returns The response from the read state request.
|
|
1154
|
+
*/
|
|
1155
|
+
public async readSubnetState(
|
|
1156
|
+
subnetId: Principal | string,
|
|
1157
|
+
options: ReadStateOptions,
|
|
1158
|
+
): Promise<ReadStateResponse> {
|
|
1159
|
+
await this.#rootKeyGuard();
|
|
1160
|
+
const subnet = Principal.from(subnetId);
|
|
1161
|
+
|
|
1162
|
+
const url = new URL(`/api/v3/subnet/${subnet.toString()}/read_state`, this.host);
|
|
1163
|
+
const transformedRequest: ReadStateRequest = await this.createReadStateRequest(
|
|
1164
|
+
options,
|
|
1165
|
+
this.#identity ?? undefined,
|
|
1166
|
+
);
|
|
1167
|
+
|
|
1168
|
+
return await this.#readStateInner(url, { subnetId: subnet }, transformedRequest);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
async #readStateInner(
|
|
1172
|
+
url: URL,
|
|
1173
|
+
principal: { canisterId: Principal } | { subnetId: Principal },
|
|
1174
|
+
transformedRequest: ReadStateRequest,
|
|
1175
|
+
requestId?: RequestId,
|
|
1176
|
+
): Promise<ReadStateResponse> {
|
|
1126
1177
|
const backoff = this.#backoffStrategy();
|
|
1127
1178
|
try {
|
|
1128
1179
|
const { responseBodyBytes } = await this.#requestAndRetry({
|
|
@@ -1138,15 +1189,25 @@ export class HttpAgent implements Agent {
|
|
|
1138
1189
|
|
|
1139
1190
|
const decodedResponse: ReadStateResponse = cbor.decode(responseBodyBytes);
|
|
1140
1191
|
|
|
1141
|
-
this.log.print('Read state response:', decodedResponse);
|
|
1142
|
-
|
|
1143
1192
|
return decodedResponse;
|
|
1144
1193
|
} catch (error) {
|
|
1145
1194
|
let readStateError: AgentError;
|
|
1146
1195
|
if (error instanceof AgentError) {
|
|
1196
|
+
if (error.hasCode(IngressExpiryInvalidErrorCode) && !this.#hasSyncedTime) {
|
|
1197
|
+
// if there is an ingress expiry error and the time has not been synced yet,
|
|
1198
|
+
// sync time with the network and try again
|
|
1199
|
+
if ('canisterId' in principal) {
|
|
1200
|
+
await this.syncTime(principal.canisterId);
|
|
1201
|
+
} else if ('subnetId' in principal) {
|
|
1202
|
+
await this.syncTimeWithSubnet(principal.subnetId);
|
|
1203
|
+
} else {
|
|
1204
|
+
throw UNREACHABLE_ERROR;
|
|
1205
|
+
}
|
|
1206
|
+
return await this.#readStateInner(url, principal, transformedRequest, requestId);
|
|
1207
|
+
}
|
|
1147
1208
|
// override the error code to include the request details
|
|
1148
1209
|
error.code.requestContext = {
|
|
1149
|
-
requestId,
|
|
1210
|
+
requestId: requestId ?? requestIdOf(transformedRequest),
|
|
1150
1211
|
senderPubKey: transformedRequest.body.sender_pubkey,
|
|
1151
1212
|
senderSignature: transformedRequest.body.sender_sig,
|
|
1152
1213
|
ingressExpiry: transformedRequest.body.content.ingress_expiry,
|
|
@@ -1197,7 +1258,7 @@ export class HttpAgent implements Agent {
|
|
|
1197
1258
|
}
|
|
1198
1259
|
|
|
1199
1260
|
/**
|
|
1200
|
-
* Allows agent to sync its time with the network. Can be called during
|
|
1261
|
+
* Allows agent to sync its time with the network. Can be called during initialization or mid-lifecycle if the device's clock has drifted away from the network time. This is necessary to set the Expiry for a request
|
|
1201
1262
|
* @param {Principal} canisterIdOverride - Pass a canister ID if you need to sync the time with a particular subnet. Uses the ICP ledger canister by default.
|
|
1202
1263
|
*/
|
|
1203
1264
|
public async syncTime(canisterIdOverride?: Principal): Promise<void> {
|
|
@@ -1242,18 +1303,7 @@ export class HttpAgent implements Agent {
|
|
|
1242
1303
|
}, []),
|
|
1243
1304
|
);
|
|
1244
1305
|
|
|
1245
|
-
|
|
1246
|
-
return typeof current === 'number' && current > max ? current : max;
|
|
1247
|
-
}, 0);
|
|
1248
|
-
|
|
1249
|
-
if (maxReplicaTime > 0) {
|
|
1250
|
-
this.#timeDiffMsecs = maxReplicaTime - callTime;
|
|
1251
|
-
this.#hasSyncedTime = true;
|
|
1252
|
-
this.log.notify({
|
|
1253
|
-
message: `Syncing time: offset of ${this.#timeDiffMsecs}`,
|
|
1254
|
-
level: 'info',
|
|
1255
|
-
});
|
|
1256
|
-
}
|
|
1306
|
+
this.#setTimeDiffMsecs(callTime, replicaTimes);
|
|
1257
1307
|
} catch (error) {
|
|
1258
1308
|
const syncTimeError =
|
|
1259
1309
|
error instanceof AgentError
|
|
@@ -1270,6 +1320,67 @@ export class HttpAgent implements Agent {
|
|
|
1270
1320
|
});
|
|
1271
1321
|
}
|
|
1272
1322
|
|
|
1323
|
+
/**
|
|
1324
|
+
* Allows agent to sync its time with the network.
|
|
1325
|
+
* @param {Principal} subnetId - Pass the subnet ID you need to sync the time with.
|
|
1326
|
+
*/
|
|
1327
|
+
public async syncTimeWithSubnet(subnetId: Principal): Promise<void> {
|
|
1328
|
+
await this.#rootKeyGuard();
|
|
1329
|
+
const callTime = Date.now();
|
|
1330
|
+
|
|
1331
|
+
try {
|
|
1332
|
+
const anonymousAgent = HttpAgent.createSync({
|
|
1333
|
+
identity: new AnonymousIdentity(),
|
|
1334
|
+
host: this.host.toString(),
|
|
1335
|
+
fetch: this.#fetch,
|
|
1336
|
+
retryTimes: 0,
|
|
1337
|
+
rootKey: this.rootKey ?? undefined,
|
|
1338
|
+
shouldSyncTime: false,
|
|
1339
|
+
});
|
|
1340
|
+
|
|
1341
|
+
const replicaTimes = await Promise.all(
|
|
1342
|
+
Array(3)
|
|
1343
|
+
.fill(null)
|
|
1344
|
+
.map(async () => {
|
|
1345
|
+
const status = await subnetStatusRequest({
|
|
1346
|
+
subnetId,
|
|
1347
|
+
agent: anonymousAgent,
|
|
1348
|
+
paths: ['time'],
|
|
1349
|
+
disableCertificateTimeVerification: true, // avoid recursive calls to syncTime
|
|
1350
|
+
});
|
|
1351
|
+
|
|
1352
|
+
const date = status.get('time');
|
|
1353
|
+
if (date instanceof Date) {
|
|
1354
|
+
return date.getTime();
|
|
1355
|
+
}
|
|
1356
|
+
}, []),
|
|
1357
|
+
);
|
|
1358
|
+
|
|
1359
|
+
this.#setTimeDiffMsecs(callTime, replicaTimes);
|
|
1360
|
+
} catch (error) {
|
|
1361
|
+
const syncTimeError =
|
|
1362
|
+
error instanceof AgentError ? error : UnknownError.fromCode(new UnexpectedErrorCode(error));
|
|
1363
|
+
this.log.error('Caught exception while attempting to sync time with subnet', syncTimeError);
|
|
1364
|
+
|
|
1365
|
+
throw syncTimeError;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
#setTimeDiffMsecs(callTime: number, replicaTimes: Array<number | undefined>): void {
|
|
1370
|
+
const maxReplicaTime = replicaTimes.reduce<number>((max, current) => {
|
|
1371
|
+
return typeof current === 'number' && current > max ? current : max;
|
|
1372
|
+
}, 0);
|
|
1373
|
+
|
|
1374
|
+
if (maxReplicaTime > 0) {
|
|
1375
|
+
this.#timeDiffMsecs = maxReplicaTime - callTime;
|
|
1376
|
+
this.#hasSyncedTime = true;
|
|
1377
|
+
this.log.notify({
|
|
1378
|
+
message: `Syncing time: offset of ${this.#timeDiffMsecs}`,
|
|
1379
|
+
level: 'info',
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1273
1384
|
public async status(): Promise<JsonObject> {
|
|
1274
1385
|
const headers: Record<string, string> = this.#credentials
|
|
1275
1386
|
? {
|
|
@@ -1338,22 +1449,63 @@ export class HttpAgent implements Agent {
|
|
|
1338
1449
|
this.#identity = Promise.resolve(identity);
|
|
1339
1450
|
}
|
|
1340
1451
|
|
|
1341
|
-
public async fetchSubnetKeys(canisterId: Principal | string) {
|
|
1452
|
+
public async fetchSubnetKeys(canisterId: Principal | string): Promise<SubnetNodeKeys> {
|
|
1342
1453
|
const effectiveCanisterId: Principal = Principal.from(canisterId);
|
|
1343
1454
|
await this.#asyncGuard(effectiveCanisterId);
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1455
|
+
|
|
1456
|
+
const rootKey = this.rootKey!;
|
|
1457
|
+
|
|
1458
|
+
const canisterReadState = await this.readState(effectiveCanisterId, {
|
|
1459
|
+
paths: [[utf8ToBytes('subnet')]],
|
|
1460
|
+
});
|
|
1461
|
+
const canisterCertificate = await Certificate.create({
|
|
1462
|
+
certificate: canisterReadState.certificate,
|
|
1463
|
+
rootKey,
|
|
1464
|
+
principal: { canisterId: effectiveCanisterId },
|
|
1347
1465
|
agent: this,
|
|
1348
1466
|
});
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1467
|
+
if (!canisterCertificate.cert.delegation) {
|
|
1468
|
+
const subnetId = Principal.selfAuthenticating(rootKey);
|
|
1469
|
+
const canisterInRange = check_canister_ranges({
|
|
1470
|
+
canisterId: effectiveCanisterId,
|
|
1471
|
+
subnetId,
|
|
1472
|
+
tree: canisterCertificate.cert.tree,
|
|
1473
|
+
});
|
|
1474
|
+
if (!canisterInRange) {
|
|
1475
|
+
throw TrustError.fromCode(
|
|
1476
|
+
new CertificateNotAuthorizedErrorCode(effectiveCanisterId, subnetId),
|
|
1477
|
+
);
|
|
1478
|
+
}
|
|
1354
1479
|
}
|
|
1355
|
-
|
|
1356
|
-
|
|
1480
|
+
|
|
1481
|
+
const subnetId = getSubnetIdFromCertificate(canisterCertificate.cert, rootKey);
|
|
1482
|
+
const nodeKeys = lookupNodeKeysFromCertificate(canisterCertificate.cert, subnetId);
|
|
1483
|
+
this.#subnetKeys.set(effectiveCanisterId.toText(), nodeKeys);
|
|
1484
|
+
|
|
1485
|
+
return nodeKeys;
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
/**
|
|
1489
|
+
* Returns the subnet ID for a given canister ID, by looking at the certificate delegation
|
|
1490
|
+
* returned by the canister's state obtained by requesting the `/time` path with {@link HttpAgent.readState | readState}.
|
|
1491
|
+
* @param canisterId The canister ID to get the subnet ID for.
|
|
1492
|
+
* @returns The subnet ID for the given canister ID.
|
|
1493
|
+
*/
|
|
1494
|
+
public async getSubnetIdFromCanister(canisterId: Principal | string): Promise<Principal> {
|
|
1495
|
+
const effectiveCanisterId = Principal.from(canisterId);
|
|
1496
|
+
await this.#asyncGuard(effectiveCanisterId);
|
|
1497
|
+
|
|
1498
|
+
const canisterReadState = await this.readState(effectiveCanisterId, {
|
|
1499
|
+
paths: [[utf8ToBytes('time')]],
|
|
1500
|
+
});
|
|
1501
|
+
const canisterCertificate = await Certificate.create({
|
|
1502
|
+
certificate: canisterReadState.certificate,
|
|
1503
|
+
rootKey: this.rootKey!,
|
|
1504
|
+
principal: { canisterId: effectiveCanisterId },
|
|
1505
|
+
agent: this,
|
|
1506
|
+
});
|
|
1507
|
+
|
|
1508
|
+
return getSubnetIdFromCertificate(canisterCertificate.cert, this.rootKey!);
|
|
1357
1509
|
}
|
|
1358
1510
|
|
|
1359
1511
|
protected _transform(request: HttpAgentRequest): Promise<HttpAgentRequest> {
|
|
@@ -1403,3 +1555,13 @@ export function calculateIngressExpiry(
|
|
|
1403
1555
|
const ingressExpiryMs = maxIngressExpiryInMinutes * MINUTE_TO_MSECS;
|
|
1404
1556
|
return Expiry.fromDeltaInMilliseconds(ingressExpiryMs, timeDiffMsecs);
|
|
1405
1557
|
}
|
|
1558
|
+
|
|
1559
|
+
/**
|
|
1560
|
+
* Checks if the response text is an ingress expiry invalid response.
|
|
1561
|
+
* @see https://github.com/dfinity/ic/blob/23d5990bfc5277c32e54f0087b5a38fa412171e1/rs/validator/src/ingress_validation.rs#L233
|
|
1562
|
+
* @param responseText The response body as string.
|
|
1563
|
+
* @returns `true` if the response text is an ingress expiry invalid response, `false` otherwise.
|
|
1564
|
+
*/
|
|
1565
|
+
function isIngressExpiryInvalidResponse(responseText: string): boolean {
|
|
1566
|
+
return responseText.includes('Invalid request expiry: ');
|
|
1567
|
+
}
|
package/src/agent/agent/index.ts
CHANGED