From 49fa5511218c585e459d3bdc621c8b59b91c6f50 Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Tue, 30 Jan 2024 14:02:12 -0800 Subject: [PATCH 1/4] updates to canisterStatus docs --- packages/agent/src/canisterStatus/index.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/agent/src/canisterStatus/index.ts b/packages/agent/src/canisterStatus/index.ts index a680cd7d0..aaa3922da 100644 --- a/packages/agent/src/canisterStatus/index.ts +++ b/packages/agent/src/canisterStatus/index.ts @@ -15,7 +15,7 @@ import { import { toHex } from '../utils/buffer'; import * as Cbor from '../cbor'; import { decodeLeb128, decodeTime } from '../utils/leb'; -import { DerEncodedPublicKey } from '..'; +import { DerEncodedPublicKey, SignIdentity } from '..'; /** * Represents the useful information about a subnet @@ -52,7 +52,10 @@ export type Status = | null; /** - * Interface to define a custom path. Nested paths will be represented as individual buffers, and can be created from text using {@link TextEncoder} + * Interface to define a custom path. Nested paths will be represented as individual buffers, and can be created from text using TextEncoder. + * @param {string} key the key to use to access the returned value in the canisterStatus map + * @param {ArrayBuffer[]} path the path to the desired value, represented as an array of buffers + * @param {string} decodeStrategy the strategy to use to decode the returned value */ export interface CustomPath { key: string; @@ -61,9 +64,10 @@ export interface CustomPath { } /** - * Interface to request metadata from the icp:public or icp:private sections. - * Similar to {@link CustomPath}, but accepts a simple string argument. - * Private metadata will require the ${@link Identity} used by the ${@link HttpAgent} will need to be requested using an identity that controlls the canister. + * @deprecated Use {@link CustomPath} instead + * @param {string} key the key to use to access the returned value in the canisterStatus map + * @param {string} path the path to the desired value, represented as a string + * @param {string} decodeStrategy the strategy to use to decode the returned value */ export interface MetaData { kind: 'metadata'; @@ -94,7 +98,7 @@ export type CanisterStatusOptions = { }; /** - * + * Request information in the * @param {CanisterStatusOptions} options {@link CanisterStatusOptions} * @param {CanisterStatusOptions['canisterId']} options.canisterId {@link Principal} * @param {CanisterStatusOptions['agent']} options.agent {@link HttpAgent} optional authenticated agent to use to make the canister request. Useful for accessing private metadata under icp:private From 6db9c816d16a9eedf0127bd34317e3ca49aa293c Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 2 Feb 2024 15:48:17 -0800 Subject: [PATCH 2/4] feat: new CustomPath class, better docs, and deprecating metadata path type for CanisterStatus --- packages/agent/src/canisterStatus/index.ts | 33 ++++++++++++++++------ packages/agent/src/certificate.ts | 4 +-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/agent/src/canisterStatus/index.ts b/packages/agent/src/canisterStatus/index.ts index aaa3922da..36a9145e7 100644 --- a/packages/agent/src/canisterStatus/index.ts +++ b/packages/agent/src/canisterStatus/index.ts @@ -15,7 +15,7 @@ import { import { toHex } from '../utils/buffer'; import * as Cbor from '../cbor'; import { decodeLeb128, decodeTime } from '../utils/leb'; -import { DerEncodedPublicKey, SignIdentity } from '..'; +import { DerEncodedPublicKey } from '..'; /** * Represents the useful information about a subnet @@ -57,10 +57,19 @@ export type Status = * @param {ArrayBuffer[]} path the path to the desired value, represented as an array of buffers * @param {string} decodeStrategy the strategy to use to decode the returned value */ -export interface CustomPath { - key: string; - path: ArrayBuffer[] | string; - decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw'; +export class CustomPath implements CustomPath { + public key: string; + public path: ArrayBuffer[] | string; + public decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw'; + constructor( + key: string, + path: ArrayBuffer[] | string, + decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw', + ) { + this.key = key; + this.path = path; + this.decodeStrategy = decodeStrategy; + } } /** @@ -98,7 +107,7 @@ export type CanisterStatusOptions = { }; /** - * Request information in the + * Request information in the * @param {CanisterStatusOptions} options {@link CanisterStatusOptions} * @param {CanisterStatusOptions['canisterId']} options.canisterId {@link Principal} * @param {CanisterStatusOptions['agent']} options.agent {@link HttpAgent} optional authenticated agent to use to make the canister request. Useful for accessing private metadata under icp:private @@ -132,8 +141,9 @@ export const request = async (options: { return (async () => { try { const response = await agent.readState(canisterId, { - paths: [encodedPaths[index]], + paths: encodedPaths, }); + response; //?? const cert = await Certificate.create({ certificate: response.certificate, rootKey: agent.rootKey, @@ -215,6 +225,8 @@ export const request = async (options: { } } } catch (error) { + error; + console.log(error); // Break on signature verification errors if ((error as AgentError)?.message?.includes('Invalid certificate')) { throw new AgentError((error as AgentError).message); @@ -233,7 +245,12 @@ export const request = async (options: { }); // Fetch all values separately, as each option can fail - await Promise.all(promises); + try { + await promises[0]; + await Promise.all(promises); + } catch (error) { + error; + } return status; }; diff --git a/packages/agent/src/certificate.ts b/packages/agent/src/certificate.ts index af156aa5f..66bd3da94 100644 --- a/packages/agent/src/certificate.ts +++ b/packages/agent/src/certificate.ts @@ -103,7 +103,7 @@ export function hashTreeToString(tree: HashTree): string { } } -interface Delegation extends Record { +interface Delegation extends Record { subnet_id: ArrayBuffer; certificate: ArrayBuffer; } @@ -153,8 +153,6 @@ export interface CreateCertificateOptions { maxAgeInMinutes?: number; } -type MetricsResult = number | bigint | Map | undefined; - export class Certificate { private readonly cert: Cert; From e2d9f2d0c5386238829ae8a89e345c053573fdfd Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 2 Feb 2024 15:50:44 -0800 Subject: [PATCH 3/4] changelog --- docs/generated/changelog.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/generated/changelog.html b/docs/generated/changelog.html index e564cb003..1fff70449 100644 --- a/docs/generated/changelog.html +++ b/docs/generated/changelog.html @@ -11,7 +11,10 @@

Agent-JS Changelog

Version x.x.x

-
    +
      +
    • feat: new CustomPath class, better docs, and deprecating metadata path type for CanisterStatus +
    • +

    Version 0.21.4

    • fix: edit to the post-release script
    • From e29340de545d66dba67be61669ee8910a9ebff3e Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 2 Feb 2024 16:00:35 -0800 Subject: [PATCH 4/4] fix: removes testing code, fixes jsdoc comment --- packages/agent/src/canisterStatus/index.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/agent/src/canisterStatus/index.ts b/packages/agent/src/canisterStatus/index.ts index 36a9145e7..dd7bc5795 100644 --- a/packages/agent/src/canisterStatus/index.ts +++ b/packages/agent/src/canisterStatus/index.ts @@ -107,7 +107,8 @@ export type CanisterStatusOptions = { }; /** - * Request information in the + * Request information in the request_status state tree for a given canister. + * Can be used to request information about the canister's controllers, time, module hash, candid interface, and more. * @param {CanisterStatusOptions} options {@link CanisterStatusOptions} * @param {CanisterStatusOptions['canisterId']} options.canisterId {@link Principal} * @param {CanisterStatusOptions['agent']} options.agent {@link HttpAgent} optional authenticated agent to use to make the canister request. Useful for accessing private metadata under icp:private @@ -141,9 +142,8 @@ export const request = async (options: { return (async () => { try { const response = await agent.readState(canisterId, { - paths: encodedPaths, + paths: [encodedPaths[index]], }); - response; //?? const cert = await Certificate.create({ certificate: response.certificate, rootKey: agent.rootKey, @@ -225,8 +225,6 @@ export const request = async (options: { } } } catch (error) { - error; - console.log(error); // Break on signature verification errors if ((error as AgentError)?.message?.includes('Invalid certificate')) { throw new AgentError((error as AgentError).message); @@ -245,12 +243,7 @@ export const request = async (options: { }); // Fetch all values separately, as each option can fail - try { - await promises[0]; - await Promise.all(promises); - } catch (error) { - error; - } + await Promise.all(promises); return status; };