Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: snapshot id encoder and decoder #758

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ic-management/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
33 changes: 33 additions & 0 deletions packages/ic-management/src/utils/ic-management.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
28 changes: 28 additions & 0 deletions packages/ic-management/src/utils/ic-management.utils.ts
Original file line number Diff line number Diff line change
@@ -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);
Loading