@icp-sdk/core 5.0.0-beta.0 → 5.0.0-beta.1
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.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 +22 -3
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +131 -51
- package/lib/cjs/agent/agent/http/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 -92
- 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 +7 -1
- 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 -3
- package/lib/cjs/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/index.js +92 -9
- 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 +224 -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/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 +22 -3
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +134 -54
- package/lib/esm/agent/agent/http/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 -93
- 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 +7 -1
- 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 -3
- package/lib/esm/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/index.js +97 -3
- 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 +183 -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/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 +1 -1
- package/src/agent/actor.ts +3 -3
- package/src/agent/agent/api.ts +8 -8
- package/src/agent/agent/http/index.ts +171 -58
- package/src/agent/canister-env/index.ts +2 -1
- package/src/agent/canisterStatus/index.ts +46 -191
- package/src/agent/certificate.ts +271 -52
- package/src/agent/constants.ts +5 -0
- package/src/agent/errors.ts +17 -4
- package/src/agent/index.ts +97 -3
- package/src/agent/polling/index.ts +1 -1
- package/src/agent/subnetStatus/index.ts +270 -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/principal/index.ts +16 -0
package/src/agent/certificate.ts
CHANGED
|
@@ -12,11 +12,12 @@ import {
|
|
|
12
12
|
UnknownError,
|
|
13
13
|
HashTreeDecodeErrorCode,
|
|
14
14
|
UNREACHABLE_ERROR,
|
|
15
|
-
MalformedLookupFoundValueErrorCode,
|
|
16
15
|
MissingLookupValueErrorCode,
|
|
17
16
|
UnexpectedErrorCode,
|
|
17
|
+
CertificateNotAuthorizedForSubnetErrorCode,
|
|
18
18
|
} from './errors.ts';
|
|
19
19
|
import { Principal } from '#principal';
|
|
20
|
+
import { compare as uint8Compare } from '#candid';
|
|
20
21
|
import * as bls from './utils/bls.ts';
|
|
21
22
|
import { decodeTime } from './utils/leb.ts';
|
|
22
23
|
import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
|
|
@@ -24,6 +25,7 @@ import { uint8Equals } from './utils/buffer.ts';
|
|
|
24
25
|
import { sha256 } from '@noble/hashes/sha2';
|
|
25
26
|
import type { HttpAgent } from './agent/http/index.ts';
|
|
26
27
|
import type { Agent } from './agent/api.ts';
|
|
28
|
+
import { IC_STATE_ROOT_DOMAIN_SEPARATOR } from './constants.ts';
|
|
27
29
|
|
|
28
30
|
const MINUTES_TO_MSEC = 60 * 1000;
|
|
29
31
|
const HOURS_TO_MINUTES = 60;
|
|
@@ -152,6 +154,21 @@ function isBufferGreaterThan(a: Uint8Array, b: Uint8Array): boolean {
|
|
|
152
154
|
|
|
153
155
|
type VerifyFunc = (pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) => Promise<boolean> | boolean;
|
|
154
156
|
|
|
157
|
+
export type CertificatePrincipal =
|
|
158
|
+
| {
|
|
159
|
+
/**
|
|
160
|
+
* The effective canister ID of the request when verifying a response, or
|
|
161
|
+
* the signing canister ID when verifying a certified variable.
|
|
162
|
+
*/
|
|
163
|
+
canisterId: Principal;
|
|
164
|
+
}
|
|
165
|
+
| {
|
|
166
|
+
/**
|
|
167
|
+
* The subnet ID when verifying a certificate from a subnet.
|
|
168
|
+
*/
|
|
169
|
+
subnetId: Principal;
|
|
170
|
+
};
|
|
171
|
+
|
|
155
172
|
export interface CreateCertificateOptions {
|
|
156
173
|
/**
|
|
157
174
|
* The bytes encoding the certificate to be verified
|
|
@@ -163,10 +180,9 @@ export interface CreateCertificateOptions {
|
|
|
163
180
|
*/
|
|
164
181
|
rootKey: Uint8Array;
|
|
165
182
|
/**
|
|
166
|
-
* The
|
|
167
|
-
* the signing canister ID when verifying a certified variable.
|
|
183
|
+
* The principal for which the certificate is being verified.
|
|
168
184
|
*/
|
|
169
|
-
|
|
185
|
+
principal: CertificatePrincipal;
|
|
170
186
|
/**
|
|
171
187
|
* BLS Verification strategy. Default strategy uses bls12_381 from @noble/curves
|
|
172
188
|
*/
|
|
@@ -188,7 +204,7 @@ export interface CreateCertificateOptions {
|
|
|
188
204
|
|
|
189
205
|
/**
|
|
190
206
|
* The agent used to sync time with the IC network, if the certificate fails the freshness check.
|
|
191
|
-
* If the agent does not implement the {@link HttpAgent.getTimeDiffMsecs}, {@link HttpAgent.hasSyncedTime} and {@link HttpAgent.
|
|
207
|
+
* If the agent does not implement the {@link HttpAgent.getTimeDiffMsecs}, {@link HttpAgent.hasSyncedTime}, {@link HttpAgent.syncTime} and {@link HttpAgent.syncTimeWithSubnet} methods,
|
|
192
208
|
* time will not be synced in case of a freshness check failure.
|
|
193
209
|
* @default undefined
|
|
194
210
|
*/
|
|
@@ -198,8 +214,9 @@ export interface CreateCertificateOptions {
|
|
|
198
214
|
export class Certificate {
|
|
199
215
|
public cert: Cert;
|
|
200
216
|
#disableTimeVerification: boolean = false;
|
|
201
|
-
#agent:
|
|
202
|
-
|
|
217
|
+
#agent:
|
|
218
|
+
| Pick<HttpAgent, 'getTimeDiffMsecs' | 'hasSyncedTime' | 'syncTime' | 'syncTimeWithSubnet'>
|
|
219
|
+
| undefined = undefined;
|
|
203
220
|
|
|
204
221
|
/**
|
|
205
222
|
* Create a new instance of a certificate, automatically verifying it.
|
|
@@ -217,7 +234,7 @@ export class Certificate {
|
|
|
217
234
|
return new Certificate(
|
|
218
235
|
options.certificate,
|
|
219
236
|
options.rootKey,
|
|
220
|
-
options.
|
|
237
|
+
options.principal,
|
|
221
238
|
options.blsVerify ?? bls.blsVerify,
|
|
222
239
|
options.maxAgeInMinutes,
|
|
223
240
|
options.disableTimeVerification,
|
|
@@ -228,7 +245,7 @@ export class Certificate {
|
|
|
228
245
|
private constructor(
|
|
229
246
|
certificate: Uint8Array,
|
|
230
247
|
private _rootKey: Uint8Array,
|
|
231
|
-
private
|
|
248
|
+
private _principal: CertificatePrincipal,
|
|
232
249
|
private _blsVerify: VerifyFunc,
|
|
233
250
|
private _maxAgeInMinutes: number = DEFAULT_CERTIFICATE_MAX_AGE_IN_MINUTES,
|
|
234
251
|
disableTimeVerification: boolean = false,
|
|
@@ -237,8 +254,17 @@ export class Certificate {
|
|
|
237
254
|
this.#disableTimeVerification = disableTimeVerification;
|
|
238
255
|
this.cert = cbor.decode(certificate);
|
|
239
256
|
|
|
240
|
-
if (
|
|
241
|
-
|
|
257
|
+
if (
|
|
258
|
+
agent &&
|
|
259
|
+
'getTimeDiffMsecs' in agent &&
|
|
260
|
+
'hasSyncedTime' in agent &&
|
|
261
|
+
'syncTime' in agent &&
|
|
262
|
+
'syncTimeWithSubnet' in agent
|
|
263
|
+
) {
|
|
264
|
+
this.#agent = agent as Pick<
|
|
265
|
+
HttpAgent,
|
|
266
|
+
'getTimeDiffMsecs' | 'hasSyncedTime' | 'syncTime' | 'syncTimeWithSubnet'
|
|
267
|
+
>;
|
|
242
268
|
}
|
|
243
269
|
}
|
|
244
270
|
|
|
@@ -265,7 +291,7 @@ export class Certificate {
|
|
|
265
291
|
const derKey = await this._checkDelegationAndGetKey(this.cert.delegation);
|
|
266
292
|
const sig = this.cert.signature;
|
|
267
293
|
const key = extractDER(derKey);
|
|
268
|
-
const msg = concatBytes(
|
|
294
|
+
const msg = concatBytes(IC_STATE_ROOT_DOMAIN_SEPARATOR, rootHash);
|
|
269
295
|
|
|
270
296
|
const lookupTime = lookupResultToBuffer(this.lookup_path(['time']));
|
|
271
297
|
if (!lookupTime) {
|
|
@@ -295,7 +321,7 @@ export class Certificate {
|
|
|
295
321
|
this.#agent &&
|
|
296
322
|
!this.#agent.hasSyncedTime()
|
|
297
323
|
) {
|
|
298
|
-
await this
|
|
324
|
+
await this._syncTime();
|
|
299
325
|
return await this.verify();
|
|
300
326
|
}
|
|
301
327
|
|
|
@@ -338,7 +364,7 @@ export class Certificate {
|
|
|
338
364
|
const cert = Certificate.createUnverified({
|
|
339
365
|
certificate: d.certificate,
|
|
340
366
|
rootKey: this._rootKey,
|
|
341
|
-
|
|
367
|
+
principal: this._principal,
|
|
342
368
|
blsVerify: this._blsVerify,
|
|
343
369
|
disableTimeVerification: this.#disableTimeVerification,
|
|
344
370
|
maxAgeInMinutes: DEFAULT_CERTIFICATE_DELEGATION_MAX_AGE_IN_MINUTES,
|
|
@@ -351,30 +377,66 @@ export class Certificate {
|
|
|
351
377
|
|
|
352
378
|
await cert.verify();
|
|
353
379
|
|
|
354
|
-
|
|
355
|
-
const subnetId = Principal.fromUint8Array(subnetIdBytes);
|
|
380
|
+
let subnetId: Principal;
|
|
356
381
|
|
|
357
|
-
|
|
358
|
-
canisterId
|
|
359
|
-
subnetId
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
382
|
+
if (isCanisterPrincipal(this._principal)) {
|
|
383
|
+
const canisterId = this._principal.canisterId;
|
|
384
|
+
subnetId = Principal.fromUint8Array(d.subnet_id);
|
|
385
|
+
|
|
386
|
+
const canisterInRange = check_canister_ranges({
|
|
387
|
+
canisterId,
|
|
388
|
+
subnetId,
|
|
389
|
+
tree: cert.cert.tree,
|
|
390
|
+
});
|
|
391
|
+
if (!canisterInRange) {
|
|
392
|
+
throw TrustError.fromCode(new CertificateNotAuthorizedErrorCode(canisterId, subnetId));
|
|
393
|
+
}
|
|
394
|
+
} else if (isSubnetPrincipal(this._principal)) {
|
|
395
|
+
subnetId = this._principal.subnetId;
|
|
396
|
+
} else {
|
|
397
|
+
throw UNREACHABLE_ERROR;
|
|
364
398
|
}
|
|
365
399
|
|
|
366
400
|
const publicKeyLookup = lookupResultToBuffer(
|
|
367
|
-
cert.lookup_path(['subnet',
|
|
401
|
+
cert.lookup_path(['subnet', subnetId.toUint8Array(), 'public_key']),
|
|
368
402
|
);
|
|
369
403
|
if (!publicKeyLookup) {
|
|
370
|
-
|
|
371
|
-
new
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
404
|
+
if (isSubnetPrincipal(this._principal)) {
|
|
405
|
+
throw TrustError.fromCode(new CertificateNotAuthorizedForSubnetErrorCode(subnetId));
|
|
406
|
+
} else {
|
|
407
|
+
throw TrustError.fromCode(
|
|
408
|
+
new MissingLookupValueErrorCode(
|
|
409
|
+
`Could not find subnet key for subnet ID ${subnetId.toText()}`,
|
|
410
|
+
),
|
|
411
|
+
);
|
|
412
|
+
}
|
|
375
413
|
}
|
|
376
414
|
return publicKeyLookup;
|
|
377
415
|
}
|
|
416
|
+
|
|
417
|
+
private async _syncTime(): Promise<void> {
|
|
418
|
+
if (!this.#agent) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (isCanisterPrincipal(this._principal)) {
|
|
423
|
+
await this.#agent.syncTime(this._principal.canisterId);
|
|
424
|
+
} else {
|
|
425
|
+
await this.#agent.syncTimeWithSubnet(this._principal.subnetId);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function isSubnetPrincipal<T extends CertificatePrincipal>(
|
|
431
|
+
principal: T,
|
|
432
|
+
): principal is T & { subnetId: Principal } {
|
|
433
|
+
return 'subnetId' in principal;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function isCanisterPrincipal<T extends CertificatePrincipal>(
|
|
437
|
+
principal: T,
|
|
438
|
+
): principal is T & { canisterId: Principal } {
|
|
439
|
+
return 'canisterId' in principal;
|
|
378
440
|
}
|
|
379
441
|
|
|
380
442
|
const DER_PREFIX = hexToBytes(
|
|
@@ -784,7 +846,46 @@ export function find_label(label: NodeLabel, tree: HashTree): LabelLookupResult
|
|
|
784
846
|
}
|
|
785
847
|
}
|
|
786
848
|
|
|
787
|
-
|
|
849
|
+
/**
|
|
850
|
+
* Ported from https://github.com/dfinity/response-verification/blob/a4b8b46bcf93f44ac30b5698e60bfd245e33ad4e/packages/ic-certification/src/hash_tree/mod.rs#L527-L541.
|
|
851
|
+
* @param path the path to prepend to the paths of the tree
|
|
852
|
+
* @param tree the tree to list the paths of
|
|
853
|
+
* @returns the paths of the tree
|
|
854
|
+
*/
|
|
855
|
+
function list_paths(path: Array<NodeLabel>, tree: HashTree): Array<Array<NodeLabel>> {
|
|
856
|
+
switch (tree[0]) {
|
|
857
|
+
case NodeType.Empty | NodeType.Pruned: {
|
|
858
|
+
return [];
|
|
859
|
+
}
|
|
860
|
+
case NodeType.Leaf: {
|
|
861
|
+
return [path];
|
|
862
|
+
}
|
|
863
|
+
case NodeType.Fork: {
|
|
864
|
+
return list_paths(path, tree[1]).concat(list_paths(path, tree[2]));
|
|
865
|
+
}
|
|
866
|
+
case NodeType.Labeled: {
|
|
867
|
+
const label = tree[1];
|
|
868
|
+
const subtree = tree[2];
|
|
869
|
+
const pathWithLabel = [...path, label];
|
|
870
|
+
return list_paths(pathWithLabel, subtree);
|
|
871
|
+
}
|
|
872
|
+
default: {
|
|
873
|
+
throw UNREACHABLE_ERROR;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
export type CheckCanisterRangesParams = {
|
|
879
|
+
canisterId: Principal;
|
|
880
|
+
subnetId: Principal;
|
|
881
|
+
tree: HashTree;
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Canister ranges in the form of an array of [start, end] principal tuples,
|
|
886
|
+
* usually decoded from the certificate.
|
|
887
|
+
*/
|
|
888
|
+
export type CanisterRanges = Array<[Principal, Principal]>;
|
|
788
889
|
|
|
789
890
|
/**
|
|
790
891
|
* Check if a canister ID falls within the canister ranges of a given subnet
|
|
@@ -794,44 +895,84 @@ type CanisterRanges = Array<[Principal, Principal]>;
|
|
|
794
895
|
* @param params.tree the hash tree in which to lookup the subnet's canister ranges
|
|
795
896
|
* @returns {boolean} `true` if the canister is in the range, `false` otherwise
|
|
796
897
|
*/
|
|
797
|
-
export function check_canister_ranges(params: {
|
|
798
|
-
|
|
799
|
-
subnetId: Principal;
|
|
800
|
-
tree: HashTree;
|
|
801
|
-
}): boolean {
|
|
802
|
-
const { canisterId, subnetId, tree } = params;
|
|
803
|
-
|
|
804
|
-
const rangesLookupValue = lookupCanisterRanges(subnetId, tree);
|
|
898
|
+
export function check_canister_ranges(params: CheckCanisterRangesParams): boolean {
|
|
899
|
+
const rangesLookupValue = lookupCanisterRanges(params);
|
|
805
900
|
const ranges = decodeCanisterRanges(rangesLookupValue);
|
|
806
901
|
|
|
902
|
+
const { canisterId } = params;
|
|
807
903
|
const canisterInRange = ranges.some(r => r[0].ltEq(canisterId) && r[1].gtEq(canisterId));
|
|
808
904
|
return canisterInRange;
|
|
809
905
|
}
|
|
810
906
|
|
|
811
|
-
|
|
812
|
-
|
|
907
|
+
/**
|
|
908
|
+
* Lookup the canister ranges using the `/canister_ranges/<subnet_id>/<ranges>` path.
|
|
909
|
+
* Certificates returned by `/api/v4/canister/<effective_canister_id>/call`
|
|
910
|
+
* and `/api/v3/canister/<effective_canister_id>/read_state` use this path.
|
|
911
|
+
*
|
|
912
|
+
* If the new lookup is not found, it tries the fallback lookup with {@link lookupCanisterRangesFallback}.
|
|
913
|
+
* @param params the parameters with which to lookup the canister ranges
|
|
914
|
+
* @param params.subnetId the subnet ID to lookup the canister ranges for
|
|
915
|
+
* @param params.tree the tree to search
|
|
916
|
+
* @param params.canisterId the canister ID to check
|
|
917
|
+
* @returns the encoded canister ranges. Use {@link decodeCanisterRanges} to decode them.
|
|
918
|
+
* @see https://internetcomputer.org/docs/references/ic-interface-spec#http-read-state
|
|
919
|
+
* @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-canister-ranges
|
|
920
|
+
*/
|
|
921
|
+
export function lookupCanisterRanges(params: CheckCanisterRangesParams): Uint8Array {
|
|
922
|
+
const { subnetId, tree, canisterId } = params;
|
|
923
|
+
|
|
924
|
+
const canisterRangeShardsLookup = lookup_subtree(
|
|
925
|
+
['canister_ranges', subnetId.toUint8Array()],
|
|
926
|
+
tree,
|
|
927
|
+
);
|
|
928
|
+
if (canisterRangeShardsLookup.status !== LookupSubtreeStatus.Found) {
|
|
929
|
+
return lookupCanisterRangesFallback(subnetId, tree);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
const canisterRangeShards = canisterRangeShardsLookup.value;
|
|
813
933
|
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
`Could not find canister ranges for subnet ${subnetId.toText()}`,
|
|
818
|
-
rangeLookup.status,
|
|
819
|
-
),
|
|
820
|
-
);
|
|
934
|
+
const shardPaths = getCanisterRangeShardPaths(canisterRangeShards);
|
|
935
|
+
if (shardPaths.length === 0) {
|
|
936
|
+
throw ProtocolError.fromCode(new CertificateNotAuthorizedErrorCode(canisterId, subnetId));
|
|
821
937
|
}
|
|
938
|
+
shardPaths.sort(uint8Compare);
|
|
822
939
|
|
|
823
|
-
|
|
940
|
+
const shardDivision = getCanisterRangeShardPartitionPoint(shardPaths, canisterId);
|
|
941
|
+
const maxPotentialShard = shardPaths[shardDivision];
|
|
942
|
+
|
|
943
|
+
const canisterRange = getCanisterRangeFromShards(maxPotentialShard, canisterRangeShards);
|
|
944
|
+
|
|
945
|
+
return canisterRange;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* Lookup the canister ranges using the `/subnet/<subnet_id>/canister_ranges` path.
|
|
950
|
+
* Certificates returned by `/api/v3/canister/<effective_canister_id>/call`
|
|
951
|
+
* and `/api/v2/canister/<effective_canister_id>/read_state` use this path.
|
|
952
|
+
* @param subnetId the subnet ID to lookup the canister ranges for
|
|
953
|
+
* @param tree the tree to search
|
|
954
|
+
* @returns the encoded canister ranges. Use {@link decodeCanisterRanges} to decode them.
|
|
955
|
+
* @see https://internetcomputer.org/docs/references/ic-interface-spec#http-read-state
|
|
956
|
+
*/
|
|
957
|
+
export function lookupCanisterRangesFallback(subnetId: Principal, tree: HashTree): Uint8Array {
|
|
958
|
+
const lookupResult = lookup_path(['subnet', subnetId.toUint8Array(), 'canister_ranges'], tree);
|
|
959
|
+
if (lookupResult.status !== LookupPathStatus.Found) {
|
|
824
960
|
throw ProtocolError.fromCode(
|
|
825
|
-
new
|
|
961
|
+
new LookupErrorCode(
|
|
826
962
|
`Could not find canister ranges for subnet ${subnetId.toText()}`,
|
|
963
|
+
lookupResult.status,
|
|
827
964
|
),
|
|
828
965
|
);
|
|
829
966
|
}
|
|
830
|
-
|
|
831
|
-
return rangeLookup.value;
|
|
967
|
+
return lookupResult.value;
|
|
832
968
|
}
|
|
833
969
|
|
|
834
|
-
|
|
970
|
+
/**
|
|
971
|
+
* Decode canister ranges from CBOR-encoded buffer
|
|
972
|
+
* @param lookupValue the CBOR-encoded value read from the certificate
|
|
973
|
+
* @returns an array of canister range tuples [start, end]
|
|
974
|
+
*/
|
|
975
|
+
export function decodeCanisterRanges(lookupValue: Uint8Array): CanisterRanges {
|
|
835
976
|
const ranges_arr = cbor.decode<Array<[Uint8Array, Uint8Array]>>(lookupValue);
|
|
836
977
|
const ranges: CanisterRanges = ranges_arr.map(v => [
|
|
837
978
|
Principal.fromUint8Array(v[0]),
|
|
@@ -839,3 +980,81 @@ function decodeCanisterRanges(lookupValue: Uint8Array): CanisterRanges {
|
|
|
839
980
|
]);
|
|
840
981
|
return ranges;
|
|
841
982
|
}
|
|
983
|
+
|
|
984
|
+
function getCanisterRangeShardPaths(canisterRangeShards: HashTree): Array<NodeLabel> {
|
|
985
|
+
const shardPaths: Array<NodeLabel> = [];
|
|
986
|
+
|
|
987
|
+
for (const path of list_paths([], canisterRangeShards)) {
|
|
988
|
+
const firstLabel = path[0];
|
|
989
|
+
if (!firstLabel) {
|
|
990
|
+
throw ProtocolError.fromCode(new CertificateVerificationErrorCode('Path is invalid'));
|
|
991
|
+
}
|
|
992
|
+
shardPaths.push(firstLabel);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
return shardPaths;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Finds the partition point that divides shard paths into two groups based on canister ID comparison.
|
|
1000
|
+
* Uses binary search to partition the array where:
|
|
1001
|
+
* - Elements at indices [0, partitionPoint) have values >= canisterId
|
|
1002
|
+
* - Elements at indices [partitionPoint, length) have values < canisterId
|
|
1003
|
+
* @param shardPaths Sorted array of shard paths to search through
|
|
1004
|
+
* @param canisterId The canister ID to compare against
|
|
1005
|
+
* @returns The index of the first shard that is less than the canister ID, or shardPaths.length if all shards are >= canisterId
|
|
1006
|
+
*/
|
|
1007
|
+
function getCanisterRangeShardPartitionPoint(
|
|
1008
|
+
shardPaths: Array<NodeLabel>,
|
|
1009
|
+
canisterId: Principal,
|
|
1010
|
+
): number {
|
|
1011
|
+
const canisterIdBytes = canisterId.toUint8Array();
|
|
1012
|
+
let left = 0;
|
|
1013
|
+
let right = shardPaths.length;
|
|
1014
|
+
|
|
1015
|
+
// Binary search for the first element where shard < canisterId
|
|
1016
|
+
while (left < right) {
|
|
1017
|
+
const mid = Math.floor((left + right) / 2);
|
|
1018
|
+
if (uint8Compare(shardPaths[mid], canisterIdBytes) <= 0) {
|
|
1019
|
+
// Found an element <= canisterId, search left half for earlier occurrence
|
|
1020
|
+
right = mid;
|
|
1021
|
+
} else {
|
|
1022
|
+
// Element is > canisterId, search right half
|
|
1023
|
+
left = mid + 1;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
return left;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
function getCanisterRangeFromShards(
|
|
1031
|
+
maxShardPath: NodeLabel,
|
|
1032
|
+
canisterRangeShards: HashTree,
|
|
1033
|
+
): Uint8Array {
|
|
1034
|
+
const canisterRange = lookup_path([maxShardPath], canisterRangeShards);
|
|
1035
|
+
if (canisterRange.status !== LookupPathStatus.Found) {
|
|
1036
|
+
throw ProtocolError.fromCode(
|
|
1037
|
+
new LookupErrorCode(
|
|
1038
|
+
`Could not find canister range for shard ${maxShardPath.toString()}`,
|
|
1039
|
+
canisterRange.status,
|
|
1040
|
+
),
|
|
1041
|
+
);
|
|
1042
|
+
}
|
|
1043
|
+
return canisterRange.value;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Get the subnet ID from a certificate
|
|
1048
|
+
* If the certificate has a delegation, it returns the subnet ID from the delegation.
|
|
1049
|
+
* If the certificate has no delegation, it returns the root subnet ID.
|
|
1050
|
+
* @param certificate the certificate to get the subnet ID from
|
|
1051
|
+
* @param rootKey the root key to use to get the subnet ID
|
|
1052
|
+
* @returns the subnet ID
|
|
1053
|
+
*/
|
|
1054
|
+
export function getSubnetIdFromCertificate(certificate: Cert, rootKey: Uint8Array): Principal {
|
|
1055
|
+
if (certificate.delegation) {
|
|
1056
|
+
return Principal.fromUint8Array(certificate.delegation.subnet_id);
|
|
1057
|
+
}
|
|
1058
|
+
// If certificate has no delegation, it comes from the root subnet
|
|
1059
|
+
return Principal.selfAuthenticating(rootKey);
|
|
1060
|
+
}
|
package/src/agent/constants.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
// Default delta for ingress expiry is 5 minutes.
|
|
2
2
|
export const DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS = 5 * 60 * 1000;
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The `\x0Dic-state-root` domain separator used in the root hash of IC certificates.
|
|
6
|
+
*/
|
|
7
|
+
export const IC_STATE_ROOT_DOMAIN_SEPARATOR = new TextEncoder().encode('\x0Dic-state-root');
|
|
8
|
+
|
|
4
9
|
/**
|
|
5
10
|
* The `\x0Aic-request` domain separator used in the signature of IC requests.
|
|
6
11
|
*/
|
package/src/agent/errors.ts
CHANGED
|
@@ -260,6 +260,19 @@ export class CertificateNotAuthorizedErrorCode extends ErrorCode {
|
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
+
export class CertificateNotAuthorizedForSubnetErrorCode extends ErrorCode {
|
|
264
|
+
public name = 'CertificateNotAuthorizedForSubnetErrorCode';
|
|
265
|
+
|
|
266
|
+
constructor(public readonly subnetId: Principal) {
|
|
267
|
+
super();
|
|
268
|
+
Object.setPrototypeOf(this, CertificateNotAuthorizedForSubnetErrorCode.prototype);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
public toErrorMessage(): string {
|
|
272
|
+
return `The certificate is not authorized for subnet ${this.subnetId.toText()}`;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
263
276
|
export class LookupErrorCode extends ErrorCode {
|
|
264
277
|
public name = 'LookupErrorCode';
|
|
265
278
|
|
|
@@ -742,16 +755,16 @@ export class HttpErrorCode extends ErrorCode {
|
|
|
742
755
|
}
|
|
743
756
|
}
|
|
744
757
|
|
|
745
|
-
export class
|
|
746
|
-
public name = '
|
|
758
|
+
export class HttpV4ApiNotSupportedErrorCode extends ErrorCode {
|
|
759
|
+
public name = 'HttpV4ApiNotSupportedErrorCode';
|
|
747
760
|
|
|
748
761
|
constructor() {
|
|
749
762
|
super();
|
|
750
|
-
Object.setPrototypeOf(this,
|
|
763
|
+
Object.setPrototypeOf(this, HttpV4ApiNotSupportedErrorCode.prototype);
|
|
751
764
|
}
|
|
752
765
|
|
|
753
766
|
public toErrorMessage(): string {
|
|
754
|
-
return 'HTTP request failed:
|
|
767
|
+
return 'HTTP request failed: v4 API is not supported';
|
|
755
768
|
}
|
|
756
769
|
}
|
|
757
770
|
|
package/src/agent/index.ts
CHANGED
|
@@ -1,5 +1,92 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* JavaScript and TypeScript module to interact with the [Internet Computer](https://internetcomputer.org/) for Node.js and browser applications.
|
|
3
|
+
*
|
|
4
|
+
* ## Usage
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { HttpAgent } from '@icp-sdk/core/agent';
|
|
8
|
+
*
|
|
9
|
+
* const agent = await HttpAgent.create({
|
|
10
|
+
* host: 'https://icp-api.io',
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* agent.call(..) // Send an update call to the canister
|
|
14
|
+
* agent.query(..) // Send a query call to the canister
|
|
15
|
+
* agent.readState(..) // Read the state of the canister
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ### Using an Agent
|
|
19
|
+
*
|
|
20
|
+
* The agent is a low-level interface that the Actor uses to encode and decode messages to the Internet Computer. It provides `call`, `query` and `readState` methods to the Actor, as well as a few additional utilities. For the most part, calls through the agent are intended to be structured through an Actor, configured with a canister interface that can be automatically generated from a [Candid](https://github.com/dfinity/candid) interface.
|
|
21
|
+
*
|
|
22
|
+
* ### Initializing an Actor
|
|
23
|
+
*
|
|
24
|
+
* The most common use for the agent is to create an actor. This is done by calling the `Actor.createActor` constructor:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* Actor.createActor(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<T>
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* The `interfaceFactory` is a function that returns a runtime interface that the Actor uses to strucure calls to a canister. The interfaceFactory can be written manually, but it is recommended to use the `dfx generate` command to generate the interface for your project, or to use the `didc` tool to generate the interface for your project.
|
|
31
|
+
*
|
|
32
|
+
* Actors can also be initialized to include the boundary node http headers, This is done by calling the `Actor.createActor` constructor:
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* Actor.createActorWithHttpDetails(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<ActorMethodMappedWithHttpDetails<T>>
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* ### Inspecting an actor's agent
|
|
39
|
+
*
|
|
40
|
+
* Use the `Actor.agentOf` method to get the agent of an actor:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const defaultAgent = Actor.agentOf(defaultActor);
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* This is useful if you need to replace or invalidate the identity used by an actor's agent.
|
|
47
|
+
*
|
|
48
|
+
* For example, if you want to replace the identity of an actor's agent with a newly authenticated identity from [Internet Identity](https://identity.internetcomputer.org), you can do so by calling the `Actor.replaceAgent` method:
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* defaultAgent.replaceIdentity(await authClient.getIdentity());
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* ### Tips for using fetch
|
|
55
|
+
*
|
|
56
|
+
* The agent uses the browser `fetch` API to make calls to the Internet Computer. If you are not using the agent in the browser, you can pass a custom `fetch` implementation to the agent's constructor. This is useful if you want to use a custom fetch implementation, such as one that adds authentication headers to the request. We recommend using the [isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch) package to provide a consistent fetch API across Node.js and the browser. You will also need to provide a `host` option to the agent's constructor, as the agent will not be able to determine the host from the global context.
|
|
57
|
+
*
|
|
58
|
+
* For example,
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* import fetch from 'isomorphic-fetch';
|
|
62
|
+
* import { HttpAgent } from '@icp-sdk/core/agent';
|
|
63
|
+
*
|
|
64
|
+
* const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://icp-api.io';
|
|
65
|
+
*
|
|
66
|
+
* const agent = new HttpAgent({ fetch, host });
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* You can also pass `fetchOptions` to the agent's constructor, which will be passed to the `fetch` implementation. This is useful if you want to pass additional options to the `fetch` implementation, such as a custom header.
|
|
70
|
+
*
|
|
71
|
+
* For example,
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* import fetch from 'isomorphic-fetch';
|
|
75
|
+
* import { HttpAgent } from '@icp-sdk/core/agent';
|
|
76
|
+
*
|
|
77
|
+
* const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://ic0.app';
|
|
78
|
+
*
|
|
79
|
+
* const fetchOptions: RequestInit = {
|
|
80
|
+
* headers: {
|
|
81
|
+
* 'X-Custom-Header': 'value',
|
|
82
|
+
* },
|
|
83
|
+
* };
|
|
84
|
+
*
|
|
85
|
+
* const agent = new HttpAgent({ fetch, host, fetchOptions });
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* <!-- split here -->
|
|
89
|
+
* @module libs/agent/api
|
|
3
90
|
*/
|
|
4
91
|
|
|
5
92
|
export * from './actor.ts';
|
|
@@ -12,6 +99,7 @@ export {
|
|
|
12
99
|
IC_REQUEST_DOMAIN_SEPARATOR,
|
|
13
100
|
IC_RESPONSE_DOMAIN_SEPARATOR,
|
|
14
101
|
IC_REQUEST_AUTH_DELEGATION_DOMAIN_SEPARATOR,
|
|
102
|
+
IC_STATE_ROOT_DOMAIN_SEPARATOR,
|
|
15
103
|
} from './constants.ts';
|
|
16
104
|
export * from './der.ts';
|
|
17
105
|
export * from './errors.ts';
|
|
@@ -24,7 +112,7 @@ export * from './utils/buffer.ts';
|
|
|
24
112
|
export * from './utils/random.ts';
|
|
25
113
|
export * as polling from './polling/index.ts';
|
|
26
114
|
import * as CanisterStatus from './canisterStatus/index.ts';
|
|
27
|
-
|
|
115
|
+
import * as SubnetStatus from './subnetStatus/index.ts';
|
|
28
116
|
/**
|
|
29
117
|
* The CanisterStatus utility is used to request structured data directly from the IC public API. This data can be accessed using agent.readState, but CanisterStatus provides a helpful abstraction with some known paths.
|
|
30
118
|
*
|
|
@@ -32,6 +120,12 @@ export { CanisterStatus };
|
|
|
32
120
|
*
|
|
33
121
|
* The primary method for this namespace is {@link CanisterStatus.request}
|
|
34
122
|
*/
|
|
35
|
-
|
|
123
|
+
export { CanisterStatus };
|
|
124
|
+
/**
|
|
125
|
+
* The SubnetStatus utility is used to request structured data directly from the IC public API. This data can be accessed using agent.readSubnetState, but SubnetStatus provides a helpful abstraction with some known paths.
|
|
126
|
+
*
|
|
127
|
+
* The primary method for this namespace is {@link SubnetStatus.request}
|
|
128
|
+
*/
|
|
129
|
+
export { SubnetStatus };
|
|
36
130
|
export { Cbor, ToCborValue } from './cbor.ts';
|
|
37
131
|
export * from './polling/index.ts';
|
|
@@ -156,7 +156,7 @@ export async function pollForResponse(
|
|
|
156
156
|
const cert = await Certificate.create({
|
|
157
157
|
certificate: state.certificate,
|
|
158
158
|
rootKey: agent.rootKey,
|
|
159
|
-
|
|
159
|
+
principal: { canisterId },
|
|
160
160
|
blsVerify: options.blsVerify,
|
|
161
161
|
agent,
|
|
162
162
|
});
|