From 9d8b8815c1bbe5f853f9b1a1759e64dbbf80983c Mon Sep 17 00:00:00 2001 From: homura Date: Thu, 23 Nov 2023 19:39:28 +0800 Subject: [PATCH] fix: data2 should be serialized to 4 --- packages/toolkit/src/normalizers.js | 6 +++--- packages/toolkit/tests/normailizers.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/toolkit/src/normalizers.js b/packages/toolkit/src/normalizers.js index 7eda6645b..ea33b619d 100644 --- a/packages/toolkit/src/normalizers.js +++ b/packages/toolkit/src/normalizers.js @@ -66,7 +66,7 @@ function normalizeObject(debugPath, object, keys) { for (const [key, f] of Object.entries(keys)) { const value = object[key]; - if (!value) { + if (value == null) { throw new Error(`${debugPath} is missing ${key}!`); } result[key] = f(`${debugPath}.${key}`, value); @@ -88,14 +88,14 @@ export function NormalizeScript(script, { debugPath = "script" } = {}) { case "data1": return 2; case "data2": - return 3; + return 4; case 0: return value; case 1: return value; case 2: return value; - case 3: + case 4: return value; default: throw new Error(`${debugPath}.hashType has invalid value: ${value}`); diff --git a/packages/toolkit/tests/normailizers.js b/packages/toolkit/tests/normailizers.js index 7ff3475dd..a2f56050c 100644 --- a/packages/toolkit/tests/normailizers.js +++ b/packages/toolkit/tests/normailizers.js @@ -30,3 +30,23 @@ test("error outPoint should not pass validation", (t) => { }); }); }); + +test("normalizeScript should work", (t) => { + ["type", "data", "data1", "data2", 0, 1, 2, 4].forEach((hashType) => { + normalizers.NormalizeScript({ + codeHash: `0x${"00".repeat(32)}`, + args: "0x", + hashType, + }); + }); + + t.pass(); + + t.throws(() => { + normalizers.NormalizeScript({ + codeHash: `0x${"00".repeat(32)}`, + args: "0x", + hashType: "unknown", + }); + }); +});