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 4 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
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 -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
126 changes: 92 additions & 34 deletions testfiles/blockchain.umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.Blockchain = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Blockchain = {}));
}(this, (function (exports) { 'use strict';

function dataLengthError(actual, required) {
Expand Down Expand Up @@ -84,6 +84,7 @@
}
return buffer;
}

class Uint32 {
constructor(reader, { validate = true } = {}) {
this.view = new DataView(assertArrayBuffer(reader));
Expand Down Expand Up @@ -143,14 +144,6 @@
return this.view.buffer;
}

toBigEndianBigUint64() {
return this.view.getBigUint64(0, false);
}

toLittleEndianBigUint64() {
return this.view.getUint64(0, true);
}
homura marked this conversation as resolved.
Show resolved Hide resolved

static size() {
return 8;
}
Expand Down Expand Up @@ -751,15 +744,15 @@
}

getTxHash() {
return new Byte32(this.view.buffer.slice(0, Byte32.size()), { validate: false });
return new Byte32(this.view.buffer.slice(0, 0 + Byte32.size()), { validate: false });
}

getIndex() {
return new Uint32(this.view.buffer.slice(0 + Byte32.size(), Uint32.size()), { validate: false });
return new Uint32(this.view.buffer.slice(0 + Byte32.size(), 0 + Byte32.size() + Uint32.size()), { validate: false });
}

validate(compatible = false) {
assertDataLength(this.view.byteLength, this.size());
assertDataLength(this.view.byteLength, OutPoint.size());
this.getTxHash().validate(compatible);
this.getIndex().validate(compatible);
}
Expand All @@ -784,15 +777,15 @@
}

getSince() {
return new Uint64(this.view.buffer.slice(0, Uint64.size()), { validate: false });
return new Uint64(this.view.buffer.slice(0, 0 + Uint64.size()), { validate: false });
}

getPreviousOutput() {
return new OutPoint(this.view.buffer.slice(0 + Uint64.size(), OutPoint.size()), { validate: false });
return new OutPoint(this.view.buffer.slice(0 + Uint64.size(), 0 + Uint64.size() + OutPoint.size()), { validate: false });
}

validate(compatible = false) {
assertDataLength(this.view.byteLength, this.size());
assertDataLength(this.view.byteLength, CellInput.size());
this.getSince().validate(compatible);
this.getPreviousOutput().validate(compatible);
}
Expand Down Expand Up @@ -862,15 +855,15 @@
}

getOutPoint() {
return new OutPoint(this.view.buffer.slice(0, OutPoint.size()), { validate: false });
return new OutPoint(this.view.buffer.slice(0, 0 + OutPoint.size()), { validate: false });
}

getDepType() {
return this.view.getUint8(0 + OutPoint.size());
}

validate(compatible = false) {
assertDataLength(this.view.byteLength, this.size());
assertDataLength(this.view.byteLength, CellDep.size());
this.getOutPoint().validate(compatible);
}
static size() {
Expand Down Expand Up @@ -1003,47 +996,47 @@
}

getVersion() {
return new Uint32(this.view.buffer.slice(0, Uint32.size()), { validate: false });
return new Uint32(this.view.buffer.slice(0, 0 + Uint32.size()), { validate: false });
}

getCompactTarget() {
return new Uint32(this.view.buffer.slice(0 + Uint32.size(), Uint32.size()), { validate: false });
return new Uint32(this.view.buffer.slice(0 + Uint32.size(), 0 + Uint32.size() + Uint32.size()), { validate: false });
}

getTimestamp() {
return new Uint64(this.view.buffer.slice(0 + Uint32.size() + Uint32.size(), Uint64.size()), { validate: false });
return new Uint64(this.view.buffer.slice(0 + Uint32.size() + Uint32.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size()), { validate: false });
}

getNumber() {
return new Uint64(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size(), Uint64.size()), { validate: false });
return new Uint64(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size()), { validate: false });
}

getEpoch() {
return new Uint64(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size(), Uint64.size()), { validate: false });
return new Uint64(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size()), { validate: false });
}

getParentHash() {
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size(), Byte32.size()), { validate: false });
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size()), { validate: false });
}

getTransactionsRoot() {
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size(), Byte32.size()), { validate: false });
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size()), { validate: false });
}

getProposalsHash() {
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size(), Byte32.size()), { validate: false });
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size()), { validate: false });
}

getUnclesHash() {
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size(), Byte32.size()), { validate: false });
getExtraHash() {
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size() + Byte32.size()), { validate: false });
}

getDao() {
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size() + Byte32.size(), Byte32.size()), { validate: false });
return new Byte32(this.view.buffer.slice(0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size() + Byte32.size(), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size() + Byte32.size() + Byte32.size()), { validate: false });
}

validate(compatible = false) {
assertDataLength(this.view.byteLength, this.size());
assertDataLength(this.view.byteLength, RawHeader.size());
this.getVersion().validate(compatible);
this.getCompactTarget().validate(compatible);
this.getTimestamp().validate(compatible);
Expand All @@ -1052,7 +1045,7 @@
this.getParentHash().validate(compatible);
this.getTransactionsRoot().validate(compatible);
this.getProposalsHash().validate(compatible);
this.getUnclesHash().validate(compatible);
this.getExtraHash().validate(compatible);
this.getDao().validate(compatible);
}
static size() {
Expand All @@ -1070,7 +1063,7 @@
array.set(new Uint8Array(SerializeByte32(value.parent_hash)), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size());
array.set(new Uint8Array(SerializeByte32(value.transactions_root)), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size());
array.set(new Uint8Array(SerializeByte32(value.proposals_hash)), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size());
array.set(new Uint8Array(SerializeByte32(value.uncles_hash)), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size());
array.set(new Uint8Array(SerializeByte32(value.extra_hash)), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size());
array.set(new Uint8Array(SerializeByte32(value.dao)), 0 + Uint32.size() + Uint32.size() + Uint64.size() + Uint64.size() + Uint64.size() + Byte32.size() + Byte32.size() + Byte32.size() + Byte32.size());
return array.buffer;
}
Expand All @@ -1084,15 +1077,15 @@
}

getRaw() {
return new RawHeader(this.view.buffer.slice(0, RawHeader.size()), { validate: false });
return new RawHeader(this.view.buffer.slice(0, 0 + RawHeader.size()), { validate: false });
}

getNonce() {
return new Uint128(this.view.buffer.slice(0 + RawHeader.size(), Uint128.size()), { validate: false });
return new Uint128(this.view.buffer.slice(0 + RawHeader.size(), 0 + RawHeader.size() + Uint128.size()), { validate: false });
}

validate(compatible = false) {
assertDataLength(this.view.byteLength, this.size());
assertDataLength(this.view.byteLength, Header.size());
this.getRaw().validate(compatible);
this.getNonce().validate(compatible);
}
Expand Down Expand Up @@ -1198,6 +1191,69 @@
return serializeTable(buffers);
}

class BlockV1 {
constructor(reader, { validate = true } = {}) {
this.view = new DataView(assertArrayBuffer(reader));
if (validate) {
this.validate();
}
}

validate(compatible = false) {
const offsets = verifyAndExtractOffsets(this.view, 0, true);
new Header(this.view.buffer.slice(offsets[0], offsets[1]), { validate: false }).validate();
new UncleBlockVec(this.view.buffer.slice(offsets[1], offsets[2]), { validate: false }).validate();
new TransactionVec(this.view.buffer.slice(offsets[2], offsets[3]), { validate: false }).validate();
new ProposalShortIdVec(this.view.buffer.slice(offsets[3], offsets[4]), { validate: false }).validate();
new Bytes(this.view.buffer.slice(offsets[4], offsets[5]), { validate: false }).validate();
}

getHeader() {
const start = 4;
const offset = this.view.getUint32(start, true);
const offset_end = this.view.getUint32(start + 4, true);
return new Header(this.view.buffer.slice(offset, offset_end), { validate: false });
}

getUncles() {
const start = 8;
const offset = this.view.getUint32(start, true);
const offset_end = this.view.getUint32(start + 4, true);
return new UncleBlockVec(this.view.buffer.slice(offset, offset_end), { validate: false });
}

getTransactions() {
const start = 12;
const offset = this.view.getUint32(start, true);
const offset_end = this.view.getUint32(start + 4, true);
return new TransactionVec(this.view.buffer.slice(offset, offset_end), { validate: false });
}

getProposals() {
const start = 16;
const offset = this.view.getUint32(start, true);
const offset_end = this.view.getUint32(start + 4, true);
return new ProposalShortIdVec(this.view.buffer.slice(offset, offset_end), { validate: false });
}

getExtension() {
const start = 20;
const offset = this.view.getUint32(start, true);
const offset_end = this.view.byteLength;
return new Bytes(this.view.buffer.slice(offset, offset_end), { validate: false });
}
}

function SerializeBlockV1(value) {
const buffers = [];
buffers.push(SerializeHeader(value.header));
buffers.push(SerializeUncleBlockVec(value.uncles));
buffers.push(SerializeTransactionVec(value.transactions));
buffers.push(SerializeProposalShortIdVec(value.proposals));
buffers.push(SerializeBytes(value.extension));
return serializeTable(buffers);
}

class CellbaseWitness {
constructor(reader, { validate = true } = {}) {
this.view = new DataView(assertArrayBuffer(reader));
Expand Down Expand Up @@ -1280,6 +1336,7 @@
}

exports.Block = Block;
exports.BlockV1 = BlockV1;
exports.Byte32 = Byte32;
exports.Byte32Vec = Byte32Vec;
exports.Bytes = Bytes;
Expand All @@ -1301,6 +1358,7 @@
exports.Script = Script;
exports.ScriptOpt = ScriptOpt;
exports.SerializeBlock = SerializeBlock;
exports.SerializeBlockV1 = SerializeBlockV1;
exports.SerializeByte32 = SerializeByte32;
exports.SerializeByte32Vec = SerializeByte32Vec;
exports.SerializeBytes = SerializeBytes;
Expand Down
Loading