diff --git a/packages/sdk/src/utils/__tests__/Uint8arrayTool.test.ts b/packages/sdk/src/utils/__tests__/Uint8arrayTool.test.ts new file mode 100644 index 00000000..c1c5a389 --- /dev/null +++ b/packages/sdk/src/utils/__tests__/Uint8arrayTool.test.ts @@ -0,0 +1,28 @@ +import { Uint8arrayTool } from "../binary"; + +describe("Uint8arrayTool ", () => { + it("should be able to convert to hex", () => { + const bytes = new Uint8Array([0, 1, 2, 3, 4, 5]); + const hex = Uint8arrayTool.toHex(bytes); + expect(hex).toBe("000102030405"); + }); + + it("should be able to ensure Uint8Array", () => { + const bytes = new Uint8Array([0, 1, 2, 3, 4, 5]); + const b64 = "AAECAwQF"; + const arr = [0, 1, 2, 3, 4, 5]; + expect(Uint8arrayTool.ensureUint8Array(bytes)).toBe(bytes); + expect(Uint8arrayTool.ensureUint8Array(b64)).toEqual(bytes); + expect(Uint8arrayTool.ensureUint8Array(arr)).toEqual(bytes); + }); + + it("should be able to compare bytes", () => { + const a = new Uint8Array([0, 1, 2, 3, 4, 5]); + const b = new Uint8Array([0, 1, 2, 3, 4, 5]); + const c = new Uint8Array([0, 1, 2, 3, 4, 6]); + const d = new Uint8Array([0, 1, 2, 3, 4]); + expect(Uint8arrayTool.bytesEqual(a, b)).toBe(true); + expect(Uint8arrayTool.bytesEqual(a, c)).toBe(false); + expect(Uint8arrayTool.bytesEqual(a, d)).toBe(false); + }); +}); diff --git a/packages/sdk/src/utils/binary/Uint8arrayTool.ts b/packages/sdk/src/utils/binary/Uint8arrayTool.ts index a5cfe55f..173bcba9 100644 --- a/packages/sdk/src/utils/binary/Uint8arrayTool.ts +++ b/packages/sdk/src/utils/binary/Uint8arrayTool.ts @@ -1,13 +1,13 @@ -import { Buffer } from "buffer"; +import { fromB64, toHEX } from "@mysten/sui/utils"; export class Uint8arrayTool { static toHex(bytes: Uint8Array): string { - return Buffer.from(bytes).toString("hex"); + return toHEX(bytes); } static ensureUint8Array(value: string | Uint8Array | number[]): Uint8Array { if (typeof value === "string") { - return Uint8Array.from(Buffer.from(value, "base64")); + return fromB64(value); } else if (value instanceof Uint8Array) { return value; } else {