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: enable validate duck typing #598

Open
wants to merge 4 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
13 changes: 13 additions & 0 deletions .changeset/sweet-trees-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@ckb-lumos/base": minor
"@ckb-lumos/ckb-indexer": minor
"@ckb-lumos/codec": minor
"@ckb-lumos/common-scripts": minor
"@ckb-lumos/e2e-test": minor
"@ckb-lumos/experiment-tx-assembler": minor
"@ckb-lumos/helpers": minor
"@ckb-lumos/lumos": minor
"@ckb-lumos/transaction-manager": minor
---

feat: supports validating duck typing
1 change: 0 additions & 1 deletion packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"dependencies": {
"@ckb-lumos/bi": "0.22.0-next.3",
"@ckb-lumos/codec": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"@types/blake2b": "^2.1.0",
"@types/lodash.isequal": "^4.5.5",
"blake2b": "^2.1.3",
Expand Down
11 changes: 5 additions & 6 deletions packages/base/src/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { validators } from "@ckb-lumos/toolkit";
import * as blockchain from "./blockchain";
import { assertHexadecimal } from "./utils";

import { Cell, Script, Transaction, TransactionWithStatus } from "./api";
import { Hexadecimal, HexString } from "./primitive";
import { Logger } from "./logger";
Expand Down Expand Up @@ -142,10 +141,10 @@
}
// Wrap the plain `Script` into `ScriptWrapper`.
if (lock && !isScriptWrapper(lock)) {
validators.ValidateScript(lock);
blockchain.Script.pack(lock);
this.lock = { script: lock, ioType: "both", argsLen: argsLen };
} else if (lock && lock.script) {
validators.ValidateScript(lock.script);
blockchain.Script.pack(lock.script);
this.lock = lock;
// check ioType, argsLen
if (!lock.argsLen) {
Expand All @@ -158,10 +157,10 @@
if (type === "empty") {
this.type = type;
} else if (type && !isScriptWrapper(type)) {
validators.ValidateScript(type);
blockchain.Script.pack(type);

Check warning on line 160 in packages/base/src/indexer.ts

View check run for this annotation

Codecov / codecov/patch

packages/base/src/indexer.ts#L160

Added line #L160 was not covered by tests
this.type = { script: type, ioType: "both", argsLen: argsLen };
} else if (type && type.script) {
validators.ValidateScript(type.script);
blockchain.Script.pack(type.script);
this.type = type;
// check ioType, argsLen
if (!type.argsLen) {
Expand Down
1 change: 0 additions & 1 deletion packages/ckb-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"@ckb-lumos/bi": "0.22.0-next.3",
"@ckb-lumos/codec": "0.22.0-next.3",
"@ckb-lumos/rpc": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"cross-fetch": "^3.1.5",
"events": "^3.3.0"
},
Expand Down
15 changes: 7 additions & 8 deletions packages/ckb-indexer/src/collector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { utils, Cell, BaseCellCollector } from "@ckb-lumos/base";
import { validators } from "@ckb-lumos/toolkit";
import { utils, Cell, BaseCellCollector, blockchain } from "@ckb-lumos/base";
import {
SearchKeyFilter,
CKBIndexerQueryOptions,
Expand Down Expand Up @@ -60,9 +59,9 @@ export class CKBCellCollector implements BaseCellCollector {

if (queries.lock) {
if (!instanceOfScriptWrapper(queries.lock)) {
validators.ValidateScript(queries.lock);
blockchain.Script.pack(queries.lock);
} else if (instanceOfScriptWrapper(queries.lock)) {
validators.ValidateScript(queries.lock.script);
blockchain.Script.pack(queries.lock.script);
}
}

Expand All @@ -71,12 +70,12 @@ export class CKBCellCollector implements BaseCellCollector {
typeof queries.type === "object" &&
!instanceOfScriptWrapper(queries.type)
) {
validators.ValidateScript(queries.type);
blockchain.Script.pack(queries.type);
} else if (
typeof queries.type === "object" &&
instanceOfScriptWrapper(queries.type)
) {
validators.ValidateScript(queries.type.script);
blockchain.Script.pack(queries.type.script);
}
}

Expand Down Expand Up @@ -140,7 +139,7 @@ export class CKBCellCollector implements BaseCellCollector {
// unWrap `ScriptWrapper` into `Script`.
if (queryLock) {
if (instanceOfScriptWrapper(queryLock)) {
validators.ValidateScript(queryLock.script);
blockchain.Script.pack(queryLock.script);
query.lock = queryLock.script;
}
}
Expand All @@ -151,7 +150,7 @@ export class CKBCellCollector implements BaseCellCollector {
typeof query.type === "object" &&
instanceOfScriptWrapper(query.type)
) {
validators.ValidateScript(query.type.script);
blockchain.Script.pack(query.type.script);
query.type = query.type.script;
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/ckb-indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Output,
utils,
Block,
blockchain,
} from "@ckb-lumos/base";
import { requestBatchTransactionWithStatus } from "./services";
import { CKBCellCollector } from "./collector";
Expand All @@ -31,7 +32,6 @@
import { BI } from "@ckb-lumos/bi";
import { RPC as CKBIndexerRpc } from "./rpc";
import { CKBRPC } from "@ckb-lumos/rpc";
import { validators } from "@ckb-lumos/toolkit";
import type * as IndexerType from "./indexerType";
import { unwrapDataWrapper } from "./ckbIndexerFilter";

Expand Down Expand Up @@ -197,10 +197,10 @@
? BI.from(0)
: BI.from(queries.fromBlock);
if (queries.lock) {
validators.ValidateScript(queries.lock);
blockchain.Script.pack(queries.lock as Script);
emitter.lock = queries.lock as Script;
} else if (queries.type && queries.type !== "empty") {
validators.ValidateScript(queries.type);
blockchain.Script.pack(queries.type as Script);

Check warning on line 203 in packages/ckb-indexer/src/indexer.ts

View check run for this annotation

Codecov / codecov/patch

packages/ckb-indexer/src/indexer.ts#L203

Added line #L203 was not covered by tests
emitter.type = queries.type as Script;
} else {
throw new Error("Either lock or type script must be provided!");
Expand Down
11 changes: 8 additions & 3 deletions packages/ckb-indexer/tests/cell_collector.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import test from "ava";
import { Indexer, CellCollector } from "../src";
import { HexadecimalRange, Script, utils, Cell } from "@ckb-lumos/base";
import {
HexadecimalRange,
Script,
utils,
Cell,
blockchain,
} from "@ckb-lumos/base";
import { spy, SinonSpy, stub } from "sinon";
import { validators } from "@ckb-lumos/toolkit";
import { CKBIndexerQueryOptions } from "../src/type";

const nodeUri = "http://127.0.0.1:8118/rpc";
Expand All @@ -12,7 +17,7 @@ const indexer = new Indexer(indexUri, nodeUri);
let validateScriptSpy: SinonSpy;
let utilsSpy: SinonSpy;
test.before(() => {
validateScriptSpy = spy(validators, "ValidateScript");
validateScriptSpy = spy(blockchain.Script, "pack");
utilsSpy = spy(utils, "assertHexadecimal");
});
test.afterEach(() => {
Expand Down
6 changes: 2 additions & 4 deletions packages/ckb-indexer/tests/indexer.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ test("test subscrib by scriptWrapper", (t) => {
},
{ instanceOf: Error }
);
t.is(
error.message,
"script does not have correct keys! Required keys: [args, codeHash, hashType], optional keys: [], actual keys: [script]"
);
t.true(/Cannot convert undefined to Uint8Array/.test(error.message));

// TODO should work fine here
// const result: IndexerEmitter = indexer.subscribe(queryOption);
// t.deepEqual(result.lock, queryOption.lock.script);
Expand Down
2 changes: 1 addition & 1 deletion packages/codec/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function bytify(bytesLike: BytesLike): Uint8Array {
if (typeof bytesLike === "string") return bytifyHex(bytesLike);
if (Array.isArray(bytesLike)) return bytifyArrayLike(bytesLike);

throw new Error(`Cannot convert ${bytesLike}`);
throw new Error(`Cannot convert ${bytesLike} to Uint8Array`);
}

export function equal(a: BytesLike, b: BytesLike): boolean {
Expand Down
6 changes: 5 additions & 1 deletion packages/codec/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export class CodecExecuteError extends Error {

const path = this.keys.reduceRight(reducer, "input");

return `Expect type ${this.origin.expectedType} at ${path} but got error: ${
const expectedTypeMessage = this.origin.expectedType
? `Expect type ${this.origin.expectedType}`
: "Error";

return `${expectedTypeMessage} at ${path} but got error: ${
this.origin.message
}
${this.origin.stack?.replace(/Error:.+?\n/, "")}
Expand Down
1 change: 0 additions & 1 deletion packages/common-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@ckb-lumos/config-manager": "0.22.0-next.3",
"@ckb-lumos/helpers": "0.22.0-next.3",
"@ckb-lumos/rpc": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"bs58": "^5.0.0",
"bech32": "^2.0.0",
"immutable": "^4.3.0"
Expand Down
1 change: 0 additions & 1 deletion packages/e2e-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@ckb-lumos/light-client": "0.22.0-next.3",
"@ckb-lumos/rpc": "0.22.0-next.3",
"@ckb-lumos/runner": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"@ckb-lumos/utils": "0.22.0-next.3",
"@types/kill-port": "^2.0.0",
"events": "^3.3.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/experiment-tx-assembler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"@ckb-lumos/base": "0.22.0-next.3",
"@ckb-lumos/bi": "0.22.0-next.3",
"@ckb-lumos/config-manager": "0.22.0-next.3",
"@ckb-lumos/helpers": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3"
"@ckb-lumos/helpers": "0.22.0-next.3"
},
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion packages/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@ckb-lumos/bi": "0.22.0-next.3",
"@ckb-lumos/codec": "0.22.0-next.3",
"@ckb-lumos/config-manager": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"bech32": "^2.0.0",
"immutable": "^4.3.0"
},
Expand Down
9 changes: 4 additions & 5 deletions packages/helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
parseFullFormatAddress,
} from "./address-to-script";
import { hexToByteArray } from "./utils";
import { validators } from "@ckb-lumos/toolkit";
import { HashType } from "@ckb-lumos/base/lib/blockchain";

const { bytify, hexify } = bytes;
Expand All @@ -45,7 +44,7 @@ export function minimalScriptCapacityCompatible(
{ validate = true }: { validate?: boolean } = {}
): BI {
if (validate) {
validators.ValidateScript(script);
blockchain.Script.pack(script);
}

let bytes = 0;
Expand Down Expand Up @@ -130,7 +129,7 @@ export function generateAddress(
);
HAS_WARNED_FOR_DEPRECATED_ADDRESS = true;
}
validators.ValidateScript(script);
blockchain.Script.pack(script);

const scriptTemplate = Object.values(config.SCRIPTS).find(
(s) =>
Expand Down Expand Up @@ -236,7 +235,7 @@ export function encodeToAddress(
script: Script,
{ config = undefined }: Options = {}
): Address {
validators.ValidateScript(script);
blockchain.Script.pack(script);
config = config || getConfig();
// https://github.com/nervosnetwork/rfcs/blob/9aef152a5123c8972de1aefc11794cf84d1762ed/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#full-payload-format
// Full payload format directly encodes all data fields of lock script. The encode rule of full payload format is Bech32m.
Expand Down Expand Up @@ -366,7 +365,7 @@ export function createTransactionFromSkeleton(
witnesses: txSkeleton.get("witnesses").toArray(),
};
if (validate) {
validators.ValidateTransaction(tx);
blockchain.Transaction.pack(tx);
}
return tx;
}
Expand Down
1 change: 0 additions & 1 deletion packages/lumos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"@ckb-lumos/helpers": "0.22.0-next.3",
"@ckb-lumos/light-client": "0.22.0-next.3",
"@ckb-lumos/rpc": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"@ckb-lumos/transaction-manager": "0.22.0-next.3"
},
"publishConfig": {
Expand Down
1 change: 0 additions & 1 deletion packages/transaction-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@ckb-lumos/codec": "0.22.0-next.3",
"@ckb-lumos/ckb-indexer": "0.22.0-next.3",
"@ckb-lumos/rpc": "0.22.0-next.3",
"@ckb-lumos/toolkit": "0.22.0-next.3",
"immutable": "^4.3.0"
},
"publishConfig": {
Expand Down
24 changes: 0 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading