Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: supported ckb2021 #15

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules/
build/

# ignore ide configurations
.idea
.vscode
2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.node.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.node.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dist": "rollup -c --environment BUILD:production",
"test": "rollup -c --environment BUILD:development && ava",
"fmt": "prettier --write \"{src,tests}/**/*.js\" index.d.ts",
"update-test-files": "curl -L https://raw.githubusercontent.com/nervosnetwork/ckb/27c36a55e6358fd04153ec3da4638b6e10660da6/util/types/schemas/blockchain.mol -o testfiles/blockchain.mol && moleculec --language - --schema-file testfiles/blockchain.mol --format json > testfiles/blockchain.json && moleculec-es -inputFile testfiles/blockchain.json -outputFile testfiles/blockchain.esm.js && rollup -f umd -i testfiles/blockchain.esm.js -o testfiles/blockchain.umd.js --name Blockchain && rm testfiles/blockchain.mol testfiles/blockchain.json testfiles/blockchain.esm.js"
"update-test-files": "curl -L https://raw.githubusercontent.com/nervosnetwork/ckb/5a7efe7a0b720de79ff3761dc6e8424b8d5b22ea/util/types/schemas/blockchain.mol -o testfiles/blockchain.mol && moleculec --language - --schema-file testfiles/blockchain.mol --format json > testfiles/blockchain.json && moleculec-es -hasBigInt -inputFile testfiles/blockchain.json -outputFile testfiles/blockchain.esm.js && rollup -f umd -i testfiles/blockchain.esm.js -o testfiles/blockchain.umd.js --name Blockchain && rm testfiles/blockchain.mol testfiles/blockchain.json testfiles/blockchain.esm.js"
},
"author": "Xuejie Xiao",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion 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 Expand Up @@ -199,7 +203,7 @@ export function NormalizeRawHeader(
parent_hash: normalizeRawData(32),
transactions_root: normalizeRawData(32),
proposals_hash: normalizeRawData(32),
uncles_hash: normalizeRawData(32),
extra_hash: normalizeRawData(32),
dao: normalizeRawData(32)
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export function TransformRawHeader(
parent_hash: invokeSerializeJson,
transactions_root: invokeSerializeJson,
proposals_hash: invokeSerializeJson,
uncles_hash: invokeSerializeJson,
extra_hash: invokeSerializeJson,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this change introduce incompatibility? Or we gonna upgrade the major version

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, maybe we should use a version like0.10x.y to distinguish it from CKB2019

dao: invokeSerializeJson
});

Expand All @@ -232,7 +232,7 @@ export function TransformHeader(
parent_hash: invokeSerializeJson,
transactions_root: invokeSerializeJson,
proposals_hash: invokeSerializeJson,
uncles_hash: invokeSerializeJson,
extra_hash: invokeSerializeJson,
dao: invokeSerializeJson,
nonce: invokeSerializeJson
});
Expand Down
18 changes: 11 additions & 7 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,11 @@ 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 @@ -248,7 +252,7 @@ function assertCommonHeader(debugPath, rawHeader) {
assertHash(`${debugPath}.parent_hash`, rawHeader.parent_hash);
assertHash(`${debugPath}.transactions_root`, rawHeader.transactions_root);
assertHash(`${debugPath}.proposals_hash`, rawHeader.proposals_hash);
assertHash(`${debugPath}.uncles_hash`, rawHeader.uncles_hash);
assertHash(`${debugPath}.extra_hash`, rawHeader.extra_hash);
assertHash(`${debugPath}.dao`, rawHeader.dao);
}

Expand All @@ -268,7 +272,7 @@ export function ValidateRawHeader(
"parent_hash",
"transactions_root",
"proposals_hash",
"uncles_hash",
"extra_hash",
"dao"
],
[]
Expand All @@ -292,14 +296,14 @@ export function ValidateHeader(
"parent_hash",
"transactions_root",
"proposals_hash",
"uncles_hash",
"extra_hash",
"dao",
"nonce"
],
[]
);
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 +312,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
Loading