Skip to content

Commit

Permalink
feat: supported ckb2021 vm version selection
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Oct 28, 2021
1 parent 3eaa7f0 commit 2154cfa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/normalizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ export function NormalizeScript(script, { debugPath = "script" } = {}) {
return 0;
case "type":
return 1;
case "data1":
return 2;
case 0:
return value;
case 1:
return value;
case 2:
return value;
default:
throw new Error(`${debugPath}.hash_type has invalid value: ${value}`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function assertHexString(debugPath, string) {

function assertHash(debugPath, hash) {
assertHexString(debugPath, hash);
if (hash.length != 66) {
if (hash.length !== 66) {
throw new Error(`${debugPath} must be a hex string of 66 bytes long!`);
}
}
Expand All @@ -75,7 +75,7 @@ export function ValidateScript(
assertHash(`${debugPath}.code_hash`, script.code_hash);
assertHexString(`${debugPath}.args`, script.args);

if (script.hash_type !== "data" && script.hash_type !== "type") {
if (script.hash_type !== "data" && script.hash_type !== "type" && script.hash_type !== "data1") {
throw new Error(`${debugPath}.hash_type must be either data or type!`);
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ export function ValidateHeader(
[]
);
assertHexString(`${debugPath}.nonce`, header.nonce);
if (header.nonce.length != 34) {
if (header.nonce.length !== 34) {
throw new Error(
`${debugPath}.nonce must be a hex string of 34 bytes long!`
);
Expand All @@ -308,7 +308,7 @@ export function ValidateHeader(

function assertProposalShortId(debugPath, shortId) {
assertHexString(debugPath, shortId);
if (shortId.length != 22) {
if (shortId.length !== 22) {
throw new Error(`${debugPath} must be a hex string of 22 bytes long!`);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/serializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ test("normalize and serialize script", t => {
);
});

test("normalize and serialize ckb2021 script", t => {
const value = {
code_hash:
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
args: "0xaabbccdd44332211",
hash_type: "data1"
};

const normalizedValue = normalizers.NormalizeScript(value);
const serializedValue = CKB.SerializeScript(normalizedValue);
const serializedHex = new Reader(serializedValue).serializeJson();
t.deepEqual(
serializedHex,
"0x3d0000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80208000000aabbccdd44332211"
);
});

test("normalize and serialize script with integer hash type", t => {
const value = {
code_hash:
Expand Down
11 changes: 11 additions & 0 deletions tests/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ test("correct script should pass validation", t => {
t.pass();
});

test("concorect ckb2021 script should pass validation", t => {
validators.ValidateScript({
code_hash:
"0xa98c57135830e1b91345948df6c4b8870828199a786b26f09f7dec4bc27a73da",
args: "0x1234",
hash_type: "data1"
});

t.pass();
})

test("correct script with empty args", t => {
validators.ValidateScript({
code_hash:
Expand Down

0 comments on commit 2154cfa

Please sign in to comment.