Skip to content

CairoByteArray toBuffer() and toHexString() corruption #1477

@xxu-circle

Description

@xxu-circle

Describe the bug
Follow up from #1455
it works partially, but the parsing the response still get corrupted bytearray. I have below as minimal repro
I believe the problem are inside toHexString, toBuffer which was doing some bigint conversion - and lost some zeroes along the way

To Reproduce

import { CairoByteArray } from "starknet";

describe("bytearray", () => {
  describe("bytearray", () => {
    const content = "0x000000010000001900000002";
    const contentBuffer = Buffer.from(content.slice(2), "hex");
    const cairoByteArray = new CairoByteArray(contentBuffer);
    const cairoBuffer: Buffer = cairoByteArray.toBuffer();
    const cairoHexString = cairoByteArray.toHexString();

    it("toBuffer() should match", () => {
      expect(cairoBuffer.toString("hex")).toEqual(contentBuffer.toString("hex"));
    });

    it("toHexString() should match", () => {
      expect(cairoHexString).toEqual(content);
    });
  });
});
 FAIL  e2e/bytearray.test.ts
  ● bytearray › bytearray › toBuffer() should match

    expect(received).toEqual(expected) // deep equality

    Expected: "000000010000001900000002"
    Received: "010000001900000002"

  ● bytearray › bytearray › toHexString() should match

    expect(received).toEqual(expected) // deep equality

    Expected: "0x000000010000001900000002"
    Received: "0x10000001900000002000000"

Expected behavior
toBuffer() and toHexString() should resolve to the same byte array.

  • Starknet.js version 8.5.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions