diff --git a/.changeset/sweet-trees-push.md b/.changeset/sweet-trees-push.md new file mode 100644 index 000000000..e2f5fb51a --- /dev/null +++ b/.changeset/sweet-trees-push.md @@ -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 diff --git a/packages/base/package.json b/packages/base/package.json index 9408f74d8..6569cf655 100644 --- a/packages/base/package.json +++ b/packages/base/package.json @@ -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", diff --git a/packages/base/src/indexer.ts b/packages/base/src/indexer.ts index 77d2d4ffd..dfcf4348b 100644 --- a/packages/base/src/indexer.ts +++ b/packages/base/src/indexer.ts @@ -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"; @@ -142,10 +141,10 @@ class TransactionCollector { } // 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) { @@ -158,10 +157,10 @@ class TransactionCollector { if (type === "empty") { this.type = type; } else if (type && !isScriptWrapper(type)) { - validators.ValidateScript(type); + blockchain.Script.pack(type); 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) { diff --git a/packages/ckb-indexer/package.json b/packages/ckb-indexer/package.json index edb954267..8d9c607fd 100644 --- a/packages/ckb-indexer/package.json +++ b/packages/ckb-indexer/package.json @@ -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" }, diff --git a/packages/ckb-indexer/src/collector.ts b/packages/ckb-indexer/src/collector.ts index a53713aa5..2e2d6353a 100644 --- a/packages/ckb-indexer/src/collector.ts +++ b/packages/ckb-indexer/src/collector.ts @@ -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, @@ -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); } } @@ -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); } } @@ -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; } } @@ -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; } } diff --git a/packages/ckb-indexer/src/indexer.ts b/packages/ckb-indexer/src/indexer.ts index 35663c6ec..2d898ab1d 100644 --- a/packages/ckb-indexer/src/indexer.ts +++ b/packages/ckb-indexer/src/indexer.ts @@ -7,6 +7,7 @@ import { Output, utils, Block, + blockchain, } from "@ckb-lumos/base"; import { requestBatchTransactionWithStatus } from "./services"; import { CKBCellCollector } from "./collector"; @@ -31,7 +32,6 @@ import { 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"; @@ -197,10 +197,10 @@ export class CkbIndexer implements CellProvider, TerminableCellFetcher { ? 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); emitter.type = queries.type as Script; } else { throw new Error("Either lock or type script must be provided!"); diff --git a/packages/ckb-indexer/tests/cell_collector.unit.test.ts b/packages/ckb-indexer/tests/cell_collector.unit.test.ts index 4c504785b..006e6f763 100644 --- a/packages/ckb-indexer/tests/cell_collector.unit.test.ts +++ b/packages/ckb-indexer/tests/cell_collector.unit.test.ts @@ -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"; @@ -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(() => { diff --git a/packages/ckb-indexer/tests/indexer.unit.test.ts b/packages/ckb-indexer/tests/indexer.unit.test.ts index b492662fc..d7d17ad71 100644 --- a/packages/ckb-indexer/tests/indexer.unit.test.ts +++ b/packages/ckb-indexer/tests/indexer.unit.test.ts @@ -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); diff --git a/packages/codec/src/bytes.ts b/packages/codec/src/bytes.ts index 7a879f7fd..579e11175 100644 --- a/packages/codec/src/bytes.ts +++ b/packages/codec/src/bytes.ts @@ -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 { diff --git a/packages/codec/src/error.ts b/packages/codec/src/error.ts index c45df1d81..4ba50965c 100644 --- a/packages/codec/src/error.ts +++ b/packages/codec/src/error.ts @@ -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/, "")} diff --git a/packages/common-scripts/package.json b/packages/common-scripts/package.json index f75187383..3b7697a58 100644 --- a/packages/common-scripts/package.json +++ b/packages/common-scripts/package.json @@ -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" diff --git a/packages/e2e-test/package.json b/packages/e2e-test/package.json index 68c210189..ce7b2aba0 100644 --- a/packages/e2e-test/package.json +++ b/packages/e2e-test/package.json @@ -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", diff --git a/packages/experiment-tx-assembler/package.json b/packages/experiment-tx-assembler/package.json index 5df3d5b18..b73c86ba3 100644 --- a/packages/experiment-tx-assembler/package.json +++ b/packages/experiment-tx-assembler/package.json @@ -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", diff --git a/packages/helpers/package.json b/packages/helpers/package.json index d84304ab1..9a338c1ee 100644 --- a/packages/helpers/package.json +++ b/packages/helpers/package.json @@ -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" }, diff --git a/packages/helpers/src/index.ts b/packages/helpers/src/index.ts index 65cd071f3..5c92f36bf 100644 --- a/packages/helpers/src/index.ts +++ b/packages/helpers/src/index.ts @@ -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; @@ -45,7 +44,7 @@ export function minimalScriptCapacityCompatible( { validate = true }: { validate?: boolean } = {} ): BI { if (validate) { - validators.ValidateScript(script); + blockchain.Script.pack(script); } let bytes = 0; @@ -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) => @@ -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. @@ -366,7 +365,7 @@ export function createTransactionFromSkeleton( witnesses: txSkeleton.get("witnesses").toArray(), }; if (validate) { - validators.ValidateTransaction(tx); + blockchain.Transaction.pack(tx); } return tx; } diff --git a/packages/lumos/package.json b/packages/lumos/package.json index 70387a8c6..c1d44ef60 100644 --- a/packages/lumos/package.json +++ b/packages/lumos/package.json @@ -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": { diff --git a/packages/transaction-manager/package.json b/packages/transaction-manager/package.json index 53bb5ed41..4bfb0a05c 100644 --- a/packages/transaction-manager/package.json +++ b/packages/transaction-manager/package.json @@ -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": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f2971ea95..4e074045a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -125,9 +125,6 @@ importers: '@ckb-lumos/codec': specifier: 0.22.0-next.3 version: link:../codec - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit '@types/blake2b': specifier: ^2.1.0 version: 2.1.0 @@ -168,9 +165,6 @@ importers: '@ckb-lumos/rpc': specifier: 0.22.0-next.3 version: link:../rpc - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit cross-fetch: specifier: ^3.1.5 version: 3.1.5 @@ -236,9 +230,6 @@ importers: '@ckb-lumos/rpc': specifier: 0.22.0-next.3 version: link:../rpc - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit bech32: specifier: ^2.0.0 version: 2.0.0 @@ -370,9 +361,6 @@ importers: '@ckb-lumos/runner': specifier: 0.22.0-next.3 version: link:../runner - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit '@ckb-lumos/utils': specifier: 0.22.0-next.3 version: link:../utils @@ -419,9 +407,6 @@ importers: '@ckb-lumos/helpers': specifier: 0.22.0-next.3 version: link:../helpers - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit packages/hd: dependencies: @@ -502,9 +487,6 @@ importers: '@ckb-lumos/config-manager': specifier: 0.22.0-next.3 version: link:../config-manager - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit bech32: specifier: ^2.0.0 version: 2.0.0 @@ -578,9 +560,6 @@ importers: '@ckb-lumos/rpc': specifier: 0.22.0-next.3 version: link:../rpc - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit '@ckb-lumos/transaction-manager': specifier: 0.22.0-next.3 version: link:../transaction-manager @@ -750,9 +729,6 @@ importers: '@ckb-lumos/rpc': specifier: 0.22.0-next.3 version: link:../rpc - '@ckb-lumos/toolkit': - specifier: 0.22.0-next.3 - version: link:../toolkit immutable: specifier: ^4.3.0 version: 4.3.0