From 7fe887cc0bbb806763f8691eb2c9b1bf39c6875b Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Wed, 20 Nov 2024 13:48:49 +0100 Subject: [PATCH 1/4] feat: snapshot id encoder and decoder Signed-off-by: David Dal Busco --- .../src/utils/ic-management.utils.spec.ts | 33 +++++++++++++++++++ .../src/utils/ic-management.utils.ts | 28 ++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 packages/ic-management/src/utils/ic-management.utils.spec.ts create mode 100644 packages/ic-management/src/utils/ic-management.utils.ts diff --git a/packages/ic-management/src/utils/ic-management.utils.spec.ts b/packages/ic-management/src/utils/ic-management.utils.spec.ts new file mode 100644 index 00000000..4780229b --- /dev/null +++ b/packages/ic-management/src/utils/ic-management.utils.spec.ts @@ -0,0 +1,33 @@ +import { uint8ArrayToHexString } from "@dfinity/utils"; +import { mockCanisterId } from "../ic-management.mock"; +import { decodeSnapshotId, encodeSnapshotId } from "./ic-management.utils"; + +describe("ic-management.utils", () => { + const mockLocalSubnetId = [0, 0, 0, 0, 0, 0, 0, 1]; + + const mockSnapshotId = Uint8Array.from([ + ...mockCanisterId.toUint8Array(), + ...mockLocalSubnetId, + ]); + + it("should correctly encode a snapshot ID with encodeSnapshotId", () => { + const expectedHex = uint8ArrayToHexString(mockSnapshotId); + const encoded = encodeSnapshotId(mockSnapshotId); + + expect(encoded).toBe(expectedHex); + }); + + it("should correctly decode a snapshot ID with decodeSnapshotId", () => { + const hex = uint8ArrayToHexString(mockSnapshotId); + const decoded = decodeSnapshotId(hex); + + expect(decoded).toEqual(mockSnapshotId); + }); + + test("should encodeSnapshotId and decodeSnapshotId", () => { + const encoded = encodeSnapshotId(mockSnapshotId); + const decoded = decodeSnapshotId(encoded); + + expect(decoded).toEqual(mockSnapshotId); + }); +}); diff --git a/packages/ic-management/src/utils/ic-management.utils.ts b/packages/ic-management/src/utils/ic-management.utils.ts new file mode 100644 index 00000000..d0547102 --- /dev/null +++ b/packages/ic-management/src/utils/ic-management.utils.ts @@ -0,0 +1,28 @@ +import { hexStringToUint8Array, uint8ArrayToHexString } from "@dfinity/utils"; +import type { snapshot_id } from "../../candid/ic-management"; + +/** + * Encodes a snapshot ID into a hex string representation. + * + * A snapshot ID is a tuple `(CanisterId, u64)`, where: + * - `CanisterId` is a unique identifier for a canister. + * - `u64` is a subnet-local number (incremented for each new snapshot). + * + * @param {snapshot_id} snapshotId - The snapshot ID to encode, represented as a `Uint8Array` or an array of numbers. + * @returns {string} The hex string representation of the snapshot ID. + */ +export const encodeSnapshotId = (snapshotId: snapshot_id): string => + uint8ArrayToHexString(snapshotId); + +/** + * Decodes a hex string representation of a snapshot ID back into its original format. + * + * A snapshot ID is a tuple `(CanisterId, u64)`, where: + * - `CanisterId` is a unique identifier for a canister. + * - `u64` is a subnet-local number (incremented for each new snapshot). + * + * @param {string} snapshotId - The hex string representation of the snapshot ID. + * @returns {snapshot_id} The decoded snapshot ID as a `Uint8Array`. + */ +export const decodeSnapshotId = (snapshotId: string): snapshot_id => + hexStringToUint8Array(snapshotId); From 467c2521cf6c33c9bbe4ba8713e4bf4a88dd6afb Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Wed, 20 Nov 2024 13:54:55 +0100 Subject: [PATCH 2/4] feat: expose utils Signed-off-by: David Dal Busco --- packages/ic-management/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ic-management/src/index.ts b/packages/ic-management/src/index.ts index ee0b6722..ec795651 100644 --- a/packages/ic-management/src/index.ts +++ b/packages/ic-management/src/index.ts @@ -2,3 +2,4 @@ export { ICManagementCanister } from "./ic-management.canister"; export * from "./types/canister.options"; export * from "./types/ic-management.params"; export * from "./types/ic-management.responses"; +export * from "./utils/ic-management.utils"; From b5a6019b395b45a5bbaef3f5284e48485fe4a90a Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Wed, 20 Nov 2024 14:30:53 +0100 Subject: [PATCH 3/4] test: hardcode snapshot hex Signed-off-by: David Dal Busco --- .../ic-management/src/utils/ic-management.utils.spec.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 4780229b..19dbcb23 100644 --- a/packages/ic-management/src/utils/ic-management.utils.spec.ts +++ b/packages/ic-management/src/utils/ic-management.utils.spec.ts @@ -1,4 +1,3 @@ -import { uint8ArrayToHexString } from "@dfinity/utils"; import { mockCanisterId } from "../ic-management.mock"; import { decodeSnapshotId, encodeSnapshotId } from "./ic-management.utils"; @@ -10,16 +9,16 @@ describe("ic-management.utils", () => { ...mockLocalSubnetId, ]); + const snapshotIdHex = "000000000000000201010000000000000001"; + it("should correctly encode a snapshot ID with encodeSnapshotId", () => { - const expectedHex = uint8ArrayToHexString(mockSnapshotId); const encoded = encodeSnapshotId(mockSnapshotId); - expect(encoded).toBe(expectedHex); + expect(encoded).toBe(snapshotIdHex); }); it("should correctly decode a snapshot ID with decodeSnapshotId", () => { - const hex = uint8ArrayToHexString(mockSnapshotId); - const decoded = decodeSnapshotId(hex); + const decoded = decodeSnapshotId(snapshotIdHex); expect(decoded).toEqual(mockSnapshotId); }); From 0498ef938dfbc006a414b3608f4ddd1a3d1f728c Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Wed, 20 Nov 2024 14:35:15 +0100 Subject: [PATCH 4/4] test: it instead of test Signed-off-by: David Dal Busco --- packages/ic-management/src/utils/ic-management.utils.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 19dbcb23..d3ce6182 100644 --- a/packages/ic-management/src/utils/ic-management.utils.spec.ts +++ b/packages/ic-management/src/utils/ic-management.utils.spec.ts @@ -23,7 +23,7 @@ describe("ic-management.utils", () => { expect(decoded).toEqual(mockSnapshotId); }); - test("should encodeSnapshotId and decodeSnapshotId", () => { + it("should encodeSnapshotId and decodeSnapshotId", () => { const encoded = encodeSnapshotId(mockSnapshotId); const decoded = decodeSnapshotId(encoded);