From a22db87989103b4d28018a90a3a31f096debe928 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 21 Nov 2024 12:29:01 +0100 Subject: [PATCH 1/2] feat: expose ic-mgmt types (#762) # Motivation Few DID types should probably be exposed as they are part of the response but defined by Candid. # Changes - Expose few types in `index.ts` of ic-mgmt --------- Signed-off-by: David Dal Busco --- packages/ic-management/src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/ic-management/src/index.ts b/packages/ic-management/src/index.ts index 0903f672..c0ef371a 100644 --- a/packages/ic-management/src/index.ts +++ b/packages/ic-management/src/index.ts @@ -1,6 +1,10 @@ export type { canister_log_record, + canister_status_result, + chunk_hash, + definite_canister_settings, fetch_canister_logs_result, + log_visibility, snapshot_id, } from "../candid/ic-management"; export { ICManagementCanister } from "./ic-management.canister"; From 80badcfc20abf0c34498cca60211d77755cdc39f Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 21 Nov 2024 12:35:10 +0100 Subject: [PATCH 2/2] feat: utils to mapSnapshotId (#764) # Motivation As suggested by @AntonioVentilii in https://github.com/dfinity/ic-js/pull/759#discussion_r1850750053 we can create a function to map the snapshot id because we will reuse similar pattern in #763. # Changes - Extract mapper in utils - Use mapper in existing implementation --------- Signed-off-by: David Dal Busco --- .../src/ic-management.canister.ts | 8 ++------ .../src/utils/ic-management.utils.spec.ts | 19 ++++++++++++++++++- .../src/utils/ic-management.utils.ts | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/ic-management/src/ic-management.canister.ts b/packages/ic-management/src/ic-management.canister.ts index 3b17493a..a14e872d 100644 --- a/packages/ic-management/src/ic-management.canister.ts +++ b/packages/ic-management/src/ic-management.canister.ts @@ -33,7 +33,7 @@ import type { CanisterStatusResponse, FetchCanisterLogsResponse, } from "./types/ic-management.responses"; -import { decodeSnapshotId } from "./utils/ic-management.utils"; +import { mapSnapshotId } from "./utils/ic-management.utils"; export class ICManagementCanister { private constructor(private readonly service: IcManagementService) { @@ -384,11 +384,7 @@ export class ICManagementCanister { return take_canister_snapshot({ canister_id: canisterId, replace_snapshot: toNullable( - nonNullish(snapshotId) - ? typeof snapshotId === "string" - ? decodeSnapshotId(snapshotId) - : snapshotId - : undefined, + nonNullish(snapshotId) ? mapSnapshotId(snapshotId) : undefined, ), }); }; diff --git a/packages/ic-management/src/utils/ic-management.utils.spec.ts b/packages/ic-management/src/utils/ic-management.utils.spec.ts index d3ce6182..545bf1f0 100644 --- a/packages/ic-management/src/utils/ic-management.utils.spec.ts +++ b/packages/ic-management/src/utils/ic-management.utils.spec.ts @@ -1,5 +1,9 @@ import { mockCanisterId } from "../ic-management.mock"; -import { decodeSnapshotId, encodeSnapshotId } from "./ic-management.utils"; +import { + decodeSnapshotId, + encodeSnapshotId, + mapSnapshotId, +} from "./ic-management.utils"; describe("ic-management.utils", () => { const mockLocalSubnetId = [0, 0, 0, 0, 0, 0, 0, 1]; @@ -29,4 +33,17 @@ describe("ic-management.utils", () => { expect(decoded).toEqual(mockSnapshotId); }); + + describe("mapSnapshotId", () => { + it("should map a Uint8Array snapshot ID without changes", () => { + const result = mapSnapshotId(mockSnapshotId); + + expect(result).toEqual(mockSnapshotId); + }); + + it("should map a string snapshot ID by decoding it", () => { + const result = mapSnapshotId(snapshotIdHex); + expect(result).toEqual(mockSnapshotId); + }); + }); }); diff --git a/packages/ic-management/src/utils/ic-management.utils.ts b/packages/ic-management/src/utils/ic-management.utils.ts index a08728c3..9d8bd9cf 100644 --- a/packages/ic-management/src/utils/ic-management.utils.ts +++ b/packages/ic-management/src/utils/ic-management.utils.ts @@ -27,3 +27,17 @@ export const encodeSnapshotId = (snapshotId: snapshot_id): SnapshotIdText => */ export const decodeSnapshotId = (snapshotId: SnapshotIdText): snapshot_id => hexStringToUint8Array(snapshotId); + +/** + * Maps a snapshot ID to the appropriate format for the IC interface. + * + * @param {SnapshotIdText | snapshot_id} snapshotId - The snapshot ID to map. + * It can either be a `string` (SnapshotIdText) or a `Uint8Array | number[]` (snapshot_id). + * If a `string` is provided, it is decoded into a `Uint8Array` using `decodeSnapshotId`. + * + * @returns {Uint8Array | number[]} The mapped snapshot ID. + */ +export const mapSnapshotId = ( + snapshotId: SnapshotIdText | snapshot_id, +): snapshot_id => + typeof snapshotId === "string" ? decodeSnapshotId(snapshotId) : snapshotId;