Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/transactions/clarity-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ export function parseArgToClarityValue(arg: unknown): ClarityValue {
return boolCV(typedArg.value as boolean);
case "principal":
return principalCV(typedArg.value as string);
case "buffer":
return bufferCV(Buffer.from(typedArg.value as string, "hex"));
case "buffer": {
// Strip optional 0x/0X prefix — without this, Buffer.from("0x...", "hex")
// silently produces an empty buffer (aibtcdev/landing-page#1012 Finding 2)
const hexStr = (typedArg.value as string).replace(/^0x/i, "");
return bufferCV(Buffer.from(hexStr, "hex"));
}
case "none":
return noneCV();
case "some":
Expand Down
14 changes: 14 additions & 0 deletions tests/transactions/clarity-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ describe("parseArgToClarityValue", () => {
expect(cv.type).toBe(ClarityType.Buffer);
});

it("should accept 0x-prefixed hex and produce the same bytes as raw hex", () => {
const raw = parseArgToClarityValue({ type: "buffer", value: "deadbeef" });
const prefixed = parseArgToClarityValue({ type: "buffer", value: "0xdeadbeef" });
expect(prefixed.type).toBe(ClarityType.Buffer);
expect(cvToString(prefixed)).toBe(cvToString(raw));
});

it("should accept 0X-prefixed hex (uppercase prefix)", () => {
const raw = parseArgToClarityValue({ type: "buffer", value: "deadbeef" });
const prefixed = parseArgToClarityValue({ type: "buffer", value: "0Xdeadbeef" });
expect(prefixed.type).toBe(ClarityType.Buffer);
expect(cvToString(prefixed)).toBe(cvToString(raw));
});

it("should convert Buffer instance", () => {
const buffer = Buffer.from([0xde, 0xad, 0xbe, 0xef]);
const cv = parseArgToClarityValue(buffer);
Expand Down