Skip to content

Commit

Permalink
fix: remove 0x prefix of normalize hex number
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyouxin committed Oct 13, 2023
1 parent bc0519a commit fe92d3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/toolkit/src/normalizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Reader } from "./reader";
function normalizeHexNumber(length) {
return function (debugPath, value) {
if (!(value instanceof ArrayBuffer)) {
let intValue = BI.from(value).toHexString();
let intValue = BI.from(value).toHexString().substring(2);
if (intValue.length % 2 !== 0) {
intValue = "0" + intValue;
}
Expand Down
26 changes: 24 additions & 2 deletions packages/toolkit/tests/normailizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@ const test = require("ava");
const { normalizers } = require("../lib");

test("correct outPoint should pass validation", (t) => {
normalizers.NormalizeOutPoint({
const normalizedOutpoint = normalizers.NormalizeOutPoint({
txHash: `0x${"00".repeat(32)}`,
index: "0x1",
});
t.pass();
const expectedNormalizedIndex = new ArrayBuffer(4);
const view = new Int32Array(expectedNormalizedIndex);
view[0] = 1;

t.deepEqual(normalizedOutpoint, {
index: expectedNormalizedIndex,
txHash: new ArrayBuffer(32),
});
});

test("error outPoint should not pass validation", (t) => {
t.throws(() => {
normalizers.NormalizeOutPoint({
txHash: `0x${"00".repeat(32)}`,
index: "0x",
});
});
t.throws(() => {
normalizers.NormalizeOutPoint({
txHash: `0x${"00".repeat(32)}`,
index: "not a number",
});
});
});

0 comments on commit fe92d3e

Please sign in to comment.