@ckb-ccc/lumos-patches 0.0.5-alpha.2 → 0.0.8-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,11 +21,13 @@
21
21
  </p>
22
22
 
23
23
  <p align="center">
24
- "Common Chains Connector" is where CCC begins.
24
+ "CCC - CKBers' Codebase" is the next step of "Common Chains Connector".
25
25
  <br />
26
- CCC helps you to interoperate wallets from different chain ecosystems with CKB,
26
+ Empower yourself with CCC to discover the unlimited potential of CKB.
27
27
  <br />
28
- fully enabling CKB's cryptographic freedom power.
28
+ Interoperate with wallets from different chain ecosystems.
29
+ <br />
30
+ Fully enabling CKB's Turing completeness and cryptographic freedom power.
29
31
  </p>
30
32
 
31
33
  ## Preview
@@ -36,7 +38,7 @@
36
38
  </a>
37
39
  </p>
38
40
 
39
- This project is still under active development, and we are looking forward to your feedback. [Try its demo now here](https://ckbccc-demo.vercel.app/).
41
+ This project is still under active development, and we are looking forward to your feedback. [Try its demo now here](https://ckbccc-demo.vercel.app/). It showcases how to use CCC for some basic scenarios in CKB.
40
42
 
41
43
  <h3 align="center">
42
44
  Read more about CCC on its <a href="https://github.com/ckb-ecofund/ccc">GitHub Repo</a>.
@@ -0,0 +1,15 @@
1
+ import { LockScriptInfo } from "@ckb-lumos/common-scripts";
2
+ import { CellDep, Script } from "@ckb-lumos/base";
3
+ /**
4
+ * Generates custom lock script information.
5
+ * @param {string} codeHash - The code hash of the custom script.
6
+ * @param {CellDep[]} cellDeps - The cell dependencies for the custom script.
7
+ * @returns {LockScriptInfo} The lock script information.
8
+ */
9
+ export declare function generateScriptInfo(codeHash: string, cellDeps: CellDep[], cellDepTypes?: Script[]): LockScriptInfo;
10
+ /**
11
+ * Generates default script information for CCC.
12
+ * @returns {LockScriptInfo[]} An array of lock script information.
13
+ */
14
+ export declare function generateDefaultScriptInfos(): LockScriptInfo[];
15
+ //# sourceMappingURL=default.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default.d.ts","sourceRoot":"","sources":["../src/default.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAG3D,OAAO,EAGL,OAAO,EAGP,MAAM,EACP,MAAM,iBAAiB,CAAC;AA8EzB;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EAAE,EACnB,YAAY,GAAE,MAAM,EAAO,GAC1B,cAAc,CAmEhB;AAWD;;;GAGG;AACH,wBAAgB,0BAA0B,IAAI,cAAc,EAAE,CAU7D"}
@@ -0,0 +1,138 @@
1
+ import { getJoyIDCellDep, getJoyIDLockScript } from "@joyid/ckb";
2
+ import { bytes } from "@ckb-lumos/codec";
3
+ import { parseFromInfo } from "@ckb-lumos/common-scripts";
4
+ import { addCellDep } from "@ckb-lumos/common-scripts/lib/helper";
5
+ import { getConfig } from "@ckb-lumos/config-manager";
6
+ import { asserts } from "./utils";
7
+ /**
8
+ * Generates a class for collecting custom script cells.
9
+ * @param {string} codeHash - The code hash of the custom script.
10
+ * @returns {typeof JoyIDCellCollector} The CustomCellCollector class.
11
+ */
12
+ function generateCollectorClass(codeHash) {
13
+ /**
14
+ * Class representing a collector for custom script cells.
15
+ * @class
16
+ */
17
+ return class CustomCellCollector {
18
+ /**
19
+ * Creates an instance of CustomCollector.
20
+ * @param {FromInfo} fromInfo - The information about the address to collect cells from.
21
+ * @param {CellProvider} cellProvider - The provider to collect cells from.
22
+ * @param {Object} options - The options for the collector.
23
+ * @param {QueryOptions} [options.queryOptions={}] - The query options for collecting cells.
24
+ * @param {Config} [options.config=getConfig()] - The Lumos configuration.
25
+ * @throws {Error} If cellProvider is not provided or fromInfo is not a string.
26
+ */
27
+ constructor(fromInfo, cellProvider, { queryOptions = {}, config = getConfig(), }) {
28
+ if (!cellProvider) {
29
+ throw new Error(`cellProvider is required when collecting cells`);
30
+ }
31
+ const { fromScript } = parseFromInfo(fromInfo, { config });
32
+ this.fromScript = fromScript;
33
+ if (!bytes.equal(fromScript.codeHash, codeHash)) {
34
+ this.cellCollector = undefined;
35
+ return;
36
+ }
37
+ queryOptions = {
38
+ ...queryOptions,
39
+ lock: this.fromScript,
40
+ type: queryOptions.type || "empty",
41
+ data: queryOptions.data || "0x",
42
+ };
43
+ this.cellCollector = cellProvider.collector(queryOptions);
44
+ }
45
+ /**
46
+ * Collects custom cells.
47
+ * @async
48
+ * @generator
49
+ * @yields {Cell} The collected cell.
50
+ */
51
+ async *collect() {
52
+ if (!this.cellCollector) {
53
+ return;
54
+ }
55
+ for await (const inputCell of this.cellCollector.collect()) {
56
+ yield inputCell;
57
+ }
58
+ }
59
+ };
60
+ }
61
+ /**
62
+ * Generates custom lock script information.
63
+ * @param {string} codeHash - The code hash of the custom script.
64
+ * @param {CellDep[]} cellDeps - The cell dependencies for the custom script.
65
+ * @returns {LockScriptInfo} The lock script information.
66
+ */
67
+ export function generateScriptInfo(codeHash, cellDeps, cellDepTypes = []) {
68
+ return {
69
+ codeHash: codeHash,
70
+ hashType: "type",
71
+ lockScriptInfo: {
72
+ CellCollector: generateCollectorClass(codeHash),
73
+ prepareSigningEntries: () => {
74
+ throw new Error("Custom scripts doesn't support prepareSigningEntries.");
75
+ },
76
+ async setupInputCell(txSkeleton, inputCell, _, options = {}) {
77
+ const fromScript = inputCell.cellOutput.lock;
78
+ asserts(bytes.equal(fromScript.codeHash, codeHash), `The input script is not specified script`);
79
+ // add inputCell to txSkeleton
80
+ txSkeleton = txSkeleton.update("inputs", (inputs) => inputs.push(inputCell));
81
+ const output = {
82
+ cellOutput: {
83
+ capacity: inputCell.cellOutput.capacity,
84
+ lock: inputCell.cellOutput.lock,
85
+ type: inputCell.cellOutput.type,
86
+ },
87
+ data: inputCell.data,
88
+ };
89
+ txSkeleton = txSkeleton.update("outputs", (outputs) => outputs.push(output));
90
+ const since = options.since;
91
+ if (since) {
92
+ txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
93
+ return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
94
+ });
95
+ }
96
+ cellDeps.forEach((item) => {
97
+ txSkeleton = addCellDep(txSkeleton, item);
98
+ });
99
+ if (txSkeleton.cellProvider != null) {
100
+ await Promise.all(cellDepTypes.map(async (type) => {
101
+ for await (const cell of txSkeleton
102
+ .cellProvider.collector({
103
+ type,
104
+ })
105
+ .collect()) {
106
+ txSkeleton = addCellDep(txSkeleton, {
107
+ depType: "code",
108
+ outPoint: cell.outPoint,
109
+ });
110
+ }
111
+ }));
112
+ }
113
+ return txSkeleton;
114
+ },
115
+ },
116
+ };
117
+ }
118
+ const NOSTR_TESTNET_TYPE_HASH = "0x6ae5ee0cb887b2df5a9a18137315b9bdc55be8d52637b2de0624092d5f0c91d5";
119
+ const NOSTR_TESTNET_TYPE = {
120
+ codeHash: "0x00000000000000000000000000000000000000000000000000545950455f4944",
121
+ hashType: "type",
122
+ args: "0x8dc56c6f35f0c535e23ded1629b1f20535477a1b43e59f14617d11e32c50e0aa",
123
+ };
124
+ /**
125
+ * Generates default script information for CCC.
126
+ * @returns {LockScriptInfo[]} An array of lock script information.
127
+ */
128
+ export function generateDefaultScriptInfos() {
129
+ return [
130
+ generateScriptInfo(getJoyIDLockScript(false).codeHash, [
131
+ getJoyIDCellDep(false),
132
+ ]),
133
+ generateScriptInfo(getJoyIDLockScript(true).codeHash, [
134
+ getJoyIDCellDep(true),
135
+ ]),
136
+ generateScriptInfo(NOSTR_TESTNET_TYPE_HASH, [], [NOSTR_TESTNET_TYPE]),
137
+ ];
138
+ }
package/dist/index.d.ts CHANGED
@@ -1,16 +1,3 @@
1
- import { Cell, CellCollector, CellDep, CellProvider, QueryOptions, Script } from "@ckb-lumos/base";
2
- import { FromInfo, LockScriptInfo } from "@ckb-lumos/common-scripts";
3
- import { Config } from "@ckb-lumos/config-manager";
4
- export declare function generateCollectorClass(codeHash: string): {
5
- new (fromInfo: FromInfo, cellProvider: CellProvider, { queryOptions, config, }: {
6
- queryOptions?: QueryOptions;
7
- config?: Config;
8
- }): {
9
- readonly fromScript: Script;
10
- readonly cellCollector: CellCollector;
11
- collect(): AsyncGenerator<Cell>;
12
- };
13
- };
14
- export declare function generateJoyIDInfo(codeHash: string, cellDeps: CellDep[]): LockScriptInfo;
15
- export declare function generateDefaultScriptInfos(): LockScriptInfo[];
1
+ export * from "./default";
2
+ export * from "./utils";
16
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,aAAa,EACb,OAAO,EACP,YAAY,EACZ,YAAY,EACZ,MAAM,EACP,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,QAAQ,EACR,cAAc,EAEf,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAa,MAAM,2BAA2B,CAAC;AAG9D,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM;mBAMvC,QAAQ,gBACJ,YAAY,6BAIvB;QAAE,YAAY,CAAC,EAAE,YAAY,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE;6BAThC,MAAM;gCACH,aAAa;mBAiCnB,eAAe,IAAI,CAAC;;EAUzC;AAED,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EAAE,GAClB,cAAc,CAkDhB;AAED,wBAAgB,0BAA0B,IAAI,cAAc,EAAE,CAS7D"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,86 +1,2 @@
1
- import { bytes } from "@ckb-lumos/codec";
2
- import { parseFromInfo, } from "@ckb-lumos/common-scripts";
3
- import { addCellDep } from "@ckb-lumos/common-scripts/lib/helper";
4
- import { getConfig } from "@ckb-lumos/config-manager";
5
- import { getJoyIDCellDep, getJoyIDLockScript } from "@joyid/ckb";
6
- export function generateCollectorClass(codeHash) {
7
- return class JoyIDCellCollector {
8
- constructor(fromInfo, cellProvider, { queryOptions = {}, config = getConfig(), }) {
9
- if (!cellProvider) {
10
- throw new Error(`cellProvider is required when collecting JoyID-related cells`);
11
- }
12
- if (typeof fromInfo !== "string") {
13
- throw new Error(`Only the address FromInfo is supported`);
14
- }
15
- const { fromScript } = parseFromInfo(fromInfo, { config });
16
- this.fromScript = fromScript;
17
- queryOptions = {
18
- ...queryOptions,
19
- lock: this.fromScript,
20
- type: queryOptions.type || "empty",
21
- data: queryOptions.data || "0x",
22
- };
23
- this.cellCollector = cellProvider.collector(queryOptions);
24
- }
25
- async *collect() {
26
- if (!bytes.equal(this.fromScript.codeHash, codeHash)) {
27
- return;
28
- }
29
- for await (const inputCell of this.cellCollector.collect()) {
30
- yield inputCell;
31
- }
32
- }
33
- };
34
- }
35
- export function generateJoyIDInfo(codeHash, cellDeps) {
36
- return {
37
- codeHash: codeHash,
38
- hashType: "type",
39
- lockScriptInfo: {
40
- CellCollector: generateCollectorClass(codeHash),
41
- prepareSigningEntries: () => {
42
- throw new Error("JoyID doesn't support prepareSigningEntries, please do not mix JoyID locks with other locks in a transaction");
43
- },
44
- async setupInputCell(txSkeleton, inputCell, _, options = {}) {
45
- const fromScript = inputCell.cellOutput.lock;
46
- asserts(bytes.equal(fromScript.codeHash, codeHash), `The input script is not JoyID script`);
47
- // add inputCell to txSkeleton
48
- txSkeleton = txSkeleton.update("inputs", (inputs) => inputs.push(inputCell));
49
- const output = {
50
- cellOutput: {
51
- capacity: inputCell.cellOutput.capacity,
52
- lock: inputCell.cellOutput.lock,
53
- type: inputCell.cellOutput.type,
54
- },
55
- data: inputCell.data,
56
- };
57
- txSkeleton = txSkeleton.update("outputs", (outputs) => outputs.push(output));
58
- const since = options.since;
59
- if (since) {
60
- txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
61
- return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
62
- });
63
- }
64
- cellDeps.forEach((item) => {
65
- txSkeleton = addCellDep(txSkeleton, item);
66
- });
67
- return txSkeleton;
68
- },
69
- },
70
- };
71
- }
72
- export function generateDefaultScriptInfos() {
73
- return [
74
- generateJoyIDInfo(getJoyIDLockScript(false).codeHash, [
75
- getJoyIDCellDep(false),
76
- ]),
77
- generateJoyIDInfo(getJoyIDLockScript(true).codeHash, [
78
- getJoyIDCellDep(true),
79
- ]),
80
- ];
81
- }
82
- function asserts(condition, message = "Assert failed") {
83
- if (!condition) {
84
- throw new Error(message);
85
- }
86
- }
1
+ export * from "./default";
2
+ export * from "./utils";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Asserts that a condition is true, throwing an error if it is not.
3
+ * @param {unknown} condition - The condition to assert.
4
+ * @param {string} [message="Assert failed"] - The error message to throw if the condition is false.
5
+ * @throws {Error} If the condition is false.
6
+ */
7
+ export declare function asserts(condition: unknown, message?: string): asserts condition;
8
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,OAAO,CACrB,SAAS,EAAE,OAAO,EAClB,OAAO,SAAkB,GACxB,OAAO,CAAC,SAAS,CAInB"}
package/dist/utils.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Asserts that a condition is true, throwing an error if it is not.
3
+ * @param {unknown} condition - The condition to assert.
4
+ * @param {string} [message="Assert failed"] - The error message to throw if the condition is false.
5
+ * @throws {Error} If the condition is false.
6
+ */
7
+ export function asserts(condition, message = "Assert failed") {
8
+ if (!condition) {
9
+ throw new Error(message);
10
+ }
11
+ }
@@ -0,0 +1,19 @@
1
+ import { CellDep, Script } from "@ckb-lumos/base";
2
+ import { LockScriptInfo } from "@ckb-lumos/common-scripts";
3
+ /**
4
+ * Generates custom lock script information.
5
+ * @param {string} codeHash - The code hash of the custom script.
6
+ * @param {CellDep[]} cellDeps - The cell dependencies for the custom script.
7
+ * @returns {LockScriptInfo} The lock script information.
8
+ */
9
+ export declare function generateScriptInfo(
10
+ codeHash: string,
11
+ cellDeps: CellDep[],
12
+ cellDepTypes?: Script[],
13
+ ): LockScriptInfo;
14
+ /**
15
+ * Generates default script information for CCC.
16
+ * @returns {LockScriptInfo[]} An array of lock script information.
17
+ */
18
+ export declare function generateDefaultScriptInfos(): LockScriptInfo[];
19
+ //# sourceMappingURL=default.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default.d.ts","sourceRoot":"","sources":["../src/default.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAG3D,OAAO,EAGL,OAAO,EAGP,MAAM,EACP,MAAM,iBAAiB,CAAC;AA8EzB;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EAAE,EACnB,YAAY,GAAE,MAAM,EAAO,GAC1B,cAAc,CAmEhB;AAWD;;;GAGG;AACH,wBAAgB,0BAA0B,IAAI,cAAc,EAAE,CAU7D"}
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateDefaultScriptInfos = exports.generateScriptInfo = void 0;
4
+ const ckb_1 = require("@joyid/ckb");
5
+ const codec_1 = require("@ckb-lumos/codec");
6
+ const common_scripts_1 = require("@ckb-lumos/common-scripts");
7
+ const helper_1 = require("@ckb-lumos/common-scripts/lib/helper");
8
+ const config_manager_1 = require("@ckb-lumos/config-manager");
9
+ const utils_1 = require("./utils");
10
+ /**
11
+ * Generates a class for collecting custom script cells.
12
+ * @param {string} codeHash - The code hash of the custom script.
13
+ * @returns {typeof JoyIDCellCollector} The CustomCellCollector class.
14
+ */
15
+ function generateCollectorClass(codeHash) {
16
+ /**
17
+ * Class representing a collector for custom script cells.
18
+ * @class
19
+ */
20
+ return class CustomCellCollector {
21
+ /**
22
+ * Creates an instance of CustomCollector.
23
+ * @param {FromInfo} fromInfo - The information about the address to collect cells from.
24
+ * @param {CellProvider} cellProvider - The provider to collect cells from.
25
+ * @param {Object} options - The options for the collector.
26
+ * @param {QueryOptions} [options.queryOptions={}] - The query options for collecting cells.
27
+ * @param {Config} [options.config=getConfig()] - The Lumos configuration.
28
+ * @throws {Error} If cellProvider is not provided or fromInfo is not a string.
29
+ */
30
+ constructor(
31
+ fromInfo,
32
+ cellProvider,
33
+ { queryOptions = {}, config = (0, config_manager_1.getConfig)() },
34
+ ) {
35
+ if (!cellProvider) {
36
+ throw new Error(`cellProvider is required when collecting cells`);
37
+ }
38
+ const { fromScript } = (0, common_scripts_1.parseFromInfo)(fromInfo, {
39
+ config,
40
+ });
41
+ this.fromScript = fromScript;
42
+ if (!codec_1.bytes.equal(fromScript.codeHash, codeHash)) {
43
+ this.cellCollector = undefined;
44
+ return;
45
+ }
46
+ queryOptions = {
47
+ ...queryOptions,
48
+ lock: this.fromScript,
49
+ type: queryOptions.type || "empty",
50
+ data: queryOptions.data || "0x",
51
+ };
52
+ this.cellCollector = cellProvider.collector(queryOptions);
53
+ }
54
+ /**
55
+ * Collects custom cells.
56
+ * @async
57
+ * @generator
58
+ * @yields {Cell} The collected cell.
59
+ */
60
+ async *collect() {
61
+ if (!this.cellCollector) {
62
+ return;
63
+ }
64
+ for await (const inputCell of this.cellCollector.collect()) {
65
+ yield inputCell;
66
+ }
67
+ }
68
+ };
69
+ }
70
+ /**
71
+ * Generates custom lock script information.
72
+ * @param {string} codeHash - The code hash of the custom script.
73
+ * @param {CellDep[]} cellDeps - The cell dependencies for the custom script.
74
+ * @returns {LockScriptInfo} The lock script information.
75
+ */
76
+ function generateScriptInfo(codeHash, cellDeps, cellDepTypes = []) {
77
+ return {
78
+ codeHash: codeHash,
79
+ hashType: "type",
80
+ lockScriptInfo: {
81
+ CellCollector: generateCollectorClass(codeHash),
82
+ prepareSigningEntries: () => {
83
+ throw new Error(
84
+ "Custom scripts doesn't support prepareSigningEntries.",
85
+ );
86
+ },
87
+ async setupInputCell(txSkeleton, inputCell, _, options = {}) {
88
+ const fromScript = inputCell.cellOutput.lock;
89
+ (0, utils_1.asserts)(
90
+ codec_1.bytes.equal(fromScript.codeHash, codeHash),
91
+ `The input script is not specified script`,
92
+ );
93
+ // add inputCell to txSkeleton
94
+ txSkeleton = txSkeleton.update("inputs", (inputs) =>
95
+ inputs.push(inputCell),
96
+ );
97
+ const output = {
98
+ cellOutput: {
99
+ capacity: inputCell.cellOutput.capacity,
100
+ lock: inputCell.cellOutput.lock,
101
+ type: inputCell.cellOutput.type,
102
+ },
103
+ data: inputCell.data,
104
+ };
105
+ txSkeleton = txSkeleton.update("outputs", (outputs) =>
106
+ outputs.push(output),
107
+ );
108
+ const since = options.since;
109
+ if (since) {
110
+ txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
111
+ return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
112
+ });
113
+ }
114
+ cellDeps.forEach((item) => {
115
+ txSkeleton = (0, helper_1.addCellDep)(txSkeleton, item);
116
+ });
117
+ if (txSkeleton.cellProvider != null) {
118
+ await Promise.all(
119
+ cellDepTypes.map(async (type) => {
120
+ for await (const cell of txSkeleton.cellProvider
121
+ .collector({
122
+ type,
123
+ })
124
+ .collect()) {
125
+ txSkeleton = (0, helper_1.addCellDep)(txSkeleton, {
126
+ depType: "code",
127
+ outPoint: cell.outPoint,
128
+ });
129
+ }
130
+ }),
131
+ );
132
+ }
133
+ return txSkeleton;
134
+ },
135
+ },
136
+ };
137
+ }
138
+ exports.generateScriptInfo = generateScriptInfo;
139
+ const NOSTR_TESTNET_TYPE_HASH =
140
+ "0x6ae5ee0cb887b2df5a9a18137315b9bdc55be8d52637b2de0624092d5f0c91d5";
141
+ const NOSTR_TESTNET_TYPE = {
142
+ codeHash:
143
+ "0x00000000000000000000000000000000000000000000000000545950455f4944",
144
+ hashType: "type",
145
+ args: "0x8dc56c6f35f0c535e23ded1629b1f20535477a1b43e59f14617d11e32c50e0aa",
146
+ };
147
+ /**
148
+ * Generates default script information for CCC.
149
+ * @returns {LockScriptInfo[]} An array of lock script information.
150
+ */
151
+ function generateDefaultScriptInfos() {
152
+ return [
153
+ generateScriptInfo((0, ckb_1.getJoyIDLockScript)(false).codeHash, [
154
+ (0, ckb_1.getJoyIDCellDep)(false),
155
+ ]),
156
+ generateScriptInfo((0, ckb_1.getJoyIDLockScript)(true).codeHash, [
157
+ (0, ckb_1.getJoyIDCellDep)(true),
158
+ ]),
159
+ generateScriptInfo(NOSTR_TESTNET_TYPE_HASH, [], [NOSTR_TESTNET_TYPE]),
160
+ ];
161
+ }
162
+ exports.generateDefaultScriptInfos = generateDefaultScriptInfos;
@@ -1,16 +1,3 @@
1
- import { Cell, CellCollector, CellDep, CellProvider, QueryOptions, Script } from "@ckb-lumos/base";
2
- import { FromInfo, LockScriptInfo } from "@ckb-lumos/common-scripts";
3
- import { Config } from "@ckb-lumos/config-manager";
4
- export declare function generateCollectorClass(codeHash: string): {
5
- new (fromInfo: FromInfo, cellProvider: CellProvider, { queryOptions, config, }: {
6
- queryOptions?: QueryOptions;
7
- config?: Config;
8
- }): {
9
- readonly fromScript: Script;
10
- readonly cellCollector: CellCollector;
11
- collect(): AsyncGenerator<Cell>;
12
- };
13
- };
14
- export declare function generateJoyIDInfo(codeHash: string, cellDeps: CellDep[]): LockScriptInfo;
15
- export declare function generateDefaultScriptInfos(): LockScriptInfo[];
16
- //# sourceMappingURL=index.d.ts.map
1
+ export * from "./default";
2
+ export * from "./utils";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,aAAa,EACb,OAAO,EACP,YAAY,EACZ,YAAY,EACZ,MAAM,EACP,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,QAAQ,EACR,cAAc,EAEf,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAa,MAAM,2BAA2B,CAAC;AAG9D,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM;mBAMvC,QAAQ,gBACJ,YAAY,6BAIvB;QAAE,YAAY,CAAC,EAAE,YAAY,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE;6BAThC,MAAM;gCACH,aAAa;mBAiCnB,eAAe,IAAI,CAAC;;EAUzC;AAED,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EAAE,GAClB,cAAc,CAkDhB;AAED,wBAAgB,0BAA0B,IAAI,cAAc,EAAE,CAS7D"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
@@ -1,92 +1,34 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateDefaultScriptInfos = exports.generateJoyIDInfo = exports.generateCollectorClass = void 0;
4
- const codec_1 = require("@ckb-lumos/codec");
5
- const common_scripts_1 = require("@ckb-lumos/common-scripts");
6
- const helper_1 = require("@ckb-lumos/common-scripts/lib/helper");
7
- const config_manager_1 = require("@ckb-lumos/config-manager");
8
- const ckb_1 = require("@joyid/ckb");
9
- function generateCollectorClass(codeHash) {
10
- return class JoyIDCellCollector {
11
- constructor(fromInfo, cellProvider, { queryOptions = {}, config = (0, config_manager_1.getConfig)(), }) {
12
- if (!cellProvider) {
13
- throw new Error(`cellProvider is required when collecting JoyID-related cells`);
14
- }
15
- if (typeof fromInfo !== "string") {
16
- throw new Error(`Only the address FromInfo is supported`);
17
- }
18
- const { fromScript } = (0, common_scripts_1.parseFromInfo)(fromInfo, { config });
19
- this.fromScript = fromScript;
20
- queryOptions = {
21
- ...queryOptions,
22
- lock: this.fromScript,
23
- type: queryOptions.type || "empty",
24
- data: queryOptions.data || "0x",
25
- };
26
- this.cellCollector = cellProvider.collector(queryOptions);
27
- }
28
- async *collect() {
29
- if (!codec_1.bytes.equal(this.fromScript.codeHash, codeHash)) {
30
- return;
31
- }
32
- for await (const inputCell of this.cellCollector.collect()) {
33
- yield inputCell;
34
- }
35
- }
36
- };
37
- }
38
- exports.generateCollectorClass = generateCollectorClass;
39
- function generateJoyIDInfo(codeHash, cellDeps) {
40
- return {
41
- codeHash: codeHash,
42
- hashType: "type",
43
- lockScriptInfo: {
44
- CellCollector: generateCollectorClass(codeHash),
45
- prepareSigningEntries: () => {
46
- throw new Error("JoyID doesn't support prepareSigningEntries, please do not mix JoyID locks with other locks in a transaction");
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (
9
+ !desc ||
10
+ ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
11
+ ) {
12
+ desc = {
13
+ enumerable: true,
14
+ get: function () {
15
+ return m[k];
47
16
  },
48
- async setupInputCell(txSkeleton, inputCell, _, options = {}) {
49
- const fromScript = inputCell.cellOutput.lock;
50
- asserts(codec_1.bytes.equal(fromScript.codeHash, codeHash), `The input script is not JoyID script`);
51
- // add inputCell to txSkeleton
52
- txSkeleton = txSkeleton.update("inputs", (inputs) => inputs.push(inputCell));
53
- const output = {
54
- cellOutput: {
55
- capacity: inputCell.cellOutput.capacity,
56
- lock: inputCell.cellOutput.lock,
57
- type: inputCell.cellOutput.type,
58
- },
59
- data: inputCell.data,
60
- };
61
- txSkeleton = txSkeleton.update("outputs", (outputs) => outputs.push(output));
62
- const since = options.since;
63
- if (since) {
64
- txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
65
- return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
66
- });
67
- }
68
- cellDeps.forEach((item) => {
69
- txSkeleton = (0, helper_1.addCellDep)(txSkeleton, item);
70
- });
71
- return txSkeleton;
72
- },
73
- },
74
- };
75
- }
76
- exports.generateJoyIDInfo = generateJoyIDInfo;
77
- function generateDefaultScriptInfos() {
78
- return [
79
- generateJoyIDInfo((0, ckb_1.getJoyIDLockScript)(false).codeHash, [
80
- (0, ckb_1.getJoyIDCellDep)(false),
81
- ]),
82
- generateJoyIDInfo((0, ckb_1.getJoyIDLockScript)(true).codeHash, [
83
- (0, ckb_1.getJoyIDCellDep)(true),
84
- ]),
85
- ];
86
- }
87
- exports.generateDefaultScriptInfos = generateDefaultScriptInfos;
88
- function asserts(condition, message = "Assert failed") {
89
- if (!condition) {
90
- throw new Error(message);
91
- }
92
- }
17
+ };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }
21
+ : function (o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ });
25
+ var __exportStar =
26
+ (this && this.__exportStar) ||
27
+ function (m, exports) {
28
+ for (var p in m)
29
+ if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p))
30
+ __createBinding(exports, m, p);
31
+ };
32
+ Object.defineProperty(exports, "__esModule", { value: true });
33
+ __exportStar(require("./default"), exports);
34
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Asserts that a condition is true, throwing an error if it is not.
3
+ * @param {unknown} condition - The condition to assert.
4
+ * @param {string} [message="Assert failed"] - The error message to throw if the condition is false.
5
+ * @throws {Error} If the condition is false.
6
+ */
7
+ export declare function asserts(
8
+ condition: unknown,
9
+ message?: string,
10
+ ): asserts condition;
11
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,OAAO,CACrB,SAAS,EAAE,OAAO,EAClB,OAAO,SAAkB,GACxB,OAAO,CAAC,SAAS,CAInB"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.asserts = void 0;
4
+ /**
5
+ * Asserts that a condition is true, throwing an error if it is not.
6
+ * @param {unknown} condition - The condition to assert.
7
+ * @param {string} [message="Assert failed"] - The error message to throw if the condition is false.
8
+ * @throws {Error} If the condition is false.
9
+ */
10
+ function asserts(condition, message = "Assert failed") {
11
+ if (!condition) {
12
+ throw new Error(message);
13
+ }
14
+ }
15
+ exports.asserts = asserts;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/lumos-patches",
3
- "version": "0.0.5-alpha.2",
3
+ "version": "0.0.8-alpha.3",
4
4
  "description": "Patches for using Lumos with CCC",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -44,5 +44,5 @@
44
44
  "@ckb-lumos/config-manager": "^0.22.2",
45
45
  "@joyid/ckb": "^0.0.11"
46
46
  },
47
- "gitHead": "abbcbe8e25dc561e0a134d3906f40bb49b8a035f"
47
+ "gitHead": "bddfa3e5631252194dfde32b79ee589703f18afb"
48
48
  }
package/src/default.ts ADDED
@@ -0,0 +1,191 @@
1
+ import { LockScriptInfo } from "@ckb-lumos/common-scripts";
2
+ import { getJoyIDCellDep, getJoyIDLockScript } from "@joyid/ckb";
3
+
4
+ import {
5
+ Cell,
6
+ CellCollector,
7
+ CellDep,
8
+ CellProvider,
9
+ QueryOptions,
10
+ Script,
11
+ } from "@ckb-lumos/base";
12
+ import { bytes } from "@ckb-lumos/codec";
13
+ import { FromInfo, parseFromInfo } from "@ckb-lumos/common-scripts";
14
+ import { addCellDep } from "@ckb-lumos/common-scripts/lib/helper";
15
+ import { Config, getConfig } from "@ckb-lumos/config-manager";
16
+ import { asserts } from "./utils";
17
+
18
+ /**
19
+ * Generates a class for collecting custom script cells.
20
+ * @param {string} codeHash - The code hash of the custom script.
21
+ * @returns {typeof JoyIDCellCollector} The CustomCellCollector class.
22
+ */
23
+ function generateCollectorClass(codeHash: string) {
24
+ /**
25
+ * Class representing a collector for custom script cells.
26
+ * @class
27
+ */
28
+ return class CustomCellCollector {
29
+ readonly fromScript: Script;
30
+ readonly cellCollector: CellCollector | undefined;
31
+
32
+ /**
33
+ * Creates an instance of CustomCollector.
34
+ * @param {FromInfo} fromInfo - The information about the address to collect cells from.
35
+ * @param {CellProvider} cellProvider - The provider to collect cells from.
36
+ * @param {Object} options - The options for the collector.
37
+ * @param {QueryOptions} [options.queryOptions={}] - The query options for collecting cells.
38
+ * @param {Config} [options.config=getConfig()] - The Lumos configuration.
39
+ * @throws {Error} If cellProvider is not provided or fromInfo is not a string.
40
+ */
41
+ constructor(
42
+ fromInfo: FromInfo,
43
+ cellProvider: CellProvider,
44
+ {
45
+ queryOptions = {},
46
+ config = getConfig(),
47
+ }: { queryOptions?: QueryOptions; config?: Config },
48
+ ) {
49
+ if (!cellProvider) {
50
+ throw new Error(`cellProvider is required when collecting cells`);
51
+ }
52
+
53
+ const { fromScript } = parseFromInfo(fromInfo, { config });
54
+ this.fromScript = fromScript;
55
+
56
+ if (!bytes.equal(fromScript.codeHash, codeHash)) {
57
+ this.cellCollector = undefined;
58
+ return;
59
+ }
60
+
61
+ queryOptions = {
62
+ ...queryOptions,
63
+ lock: this.fromScript,
64
+ type: queryOptions.type || "empty",
65
+ data: queryOptions.data || "0x",
66
+ };
67
+
68
+ this.cellCollector = cellProvider.collector(queryOptions);
69
+ }
70
+
71
+ /**
72
+ * Collects custom cells.
73
+ * @async
74
+ * @generator
75
+ * @yields {Cell} The collected cell.
76
+ */
77
+ async *collect(): AsyncGenerator<Cell> {
78
+ if (!this.cellCollector) {
79
+ return;
80
+ }
81
+
82
+ for await (const inputCell of this.cellCollector.collect()) {
83
+ yield inputCell;
84
+ }
85
+ }
86
+ };
87
+ }
88
+
89
+ /**
90
+ * Generates custom lock script information.
91
+ * @param {string} codeHash - The code hash of the custom script.
92
+ * @param {CellDep[]} cellDeps - The cell dependencies for the custom script.
93
+ * @returns {LockScriptInfo} The lock script information.
94
+ */
95
+ export function generateScriptInfo(
96
+ codeHash: string,
97
+ cellDeps: CellDep[],
98
+ cellDepTypes: Script[] = [],
99
+ ): LockScriptInfo {
100
+ return {
101
+ codeHash: codeHash,
102
+ hashType: "type",
103
+ lockScriptInfo: {
104
+ CellCollector: generateCollectorClass(codeHash),
105
+ prepareSigningEntries: () => {
106
+ throw new Error(
107
+ "Custom scripts doesn't support prepareSigningEntries.",
108
+ );
109
+ },
110
+ async setupInputCell(txSkeleton, inputCell, _, options = {}) {
111
+ const fromScript = inputCell.cellOutput.lock;
112
+ asserts(
113
+ bytes.equal(fromScript.codeHash, codeHash),
114
+ `The input script is not specified script`,
115
+ );
116
+ // add inputCell to txSkeleton
117
+ txSkeleton = txSkeleton.update("inputs", (inputs) =>
118
+ inputs.push(inputCell),
119
+ );
120
+
121
+ const output: Cell = {
122
+ cellOutput: {
123
+ capacity: inputCell.cellOutput.capacity,
124
+ lock: inputCell.cellOutput.lock,
125
+ type: inputCell.cellOutput.type,
126
+ },
127
+ data: inputCell.data,
128
+ };
129
+
130
+ txSkeleton = txSkeleton.update("outputs", (outputs) =>
131
+ outputs.push(output),
132
+ );
133
+
134
+ const since = options.since;
135
+ if (since) {
136
+ txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
137
+ return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
138
+ });
139
+ }
140
+
141
+ cellDeps.forEach((item) => {
142
+ txSkeleton = addCellDep(txSkeleton, item);
143
+ });
144
+
145
+ if (txSkeleton.cellProvider != null) {
146
+ await Promise.all(
147
+ cellDepTypes.map(async (type) => {
148
+ for await (const cell of txSkeleton
149
+ .cellProvider!.collector({
150
+ type,
151
+ })
152
+ .collect()) {
153
+ txSkeleton = addCellDep(txSkeleton, {
154
+ depType: "code",
155
+ outPoint: cell.outPoint!,
156
+ });
157
+ }
158
+ }),
159
+ );
160
+ }
161
+
162
+ return txSkeleton;
163
+ },
164
+ },
165
+ };
166
+ }
167
+
168
+ const NOSTR_TESTNET_TYPE_HASH =
169
+ "0x6ae5ee0cb887b2df5a9a18137315b9bdc55be8d52637b2de0624092d5f0c91d5";
170
+ const NOSTR_TESTNET_TYPE: Script = {
171
+ codeHash:
172
+ "0x00000000000000000000000000000000000000000000000000545950455f4944",
173
+ hashType: "type",
174
+ args: "0x8dc56c6f35f0c535e23ded1629b1f20535477a1b43e59f14617d11e32c50e0aa",
175
+ };
176
+
177
+ /**
178
+ * Generates default script information for CCC.
179
+ * @returns {LockScriptInfo[]} An array of lock script information.
180
+ */
181
+ export function generateDefaultScriptInfos(): LockScriptInfo[] {
182
+ return [
183
+ generateScriptInfo(getJoyIDLockScript(false).codeHash, [
184
+ getJoyIDCellDep(false),
185
+ ]),
186
+ generateScriptInfo(getJoyIDLockScript(true).codeHash, [
187
+ getJoyIDCellDep(true),
188
+ ]),
189
+ generateScriptInfo(NOSTR_TESTNET_TYPE_HASH, [], [NOSTR_TESTNET_TYPE]),
190
+ ];
191
+ }
package/src/index.ts CHANGED
@@ -1,140 +1,2 @@
1
- import {
2
- Cell,
3
- CellCollector,
4
- CellDep,
5
- CellProvider,
6
- QueryOptions,
7
- Script,
8
- } from "@ckb-lumos/base";
9
- import { bytes } from "@ckb-lumos/codec";
10
- import {
11
- FromInfo,
12
- LockScriptInfo,
13
- parseFromInfo,
14
- } from "@ckb-lumos/common-scripts";
15
- import { addCellDep } from "@ckb-lumos/common-scripts/lib/helper";
16
- import { Config, getConfig } from "@ckb-lumos/config-manager";
17
- import { getJoyIDCellDep, getJoyIDLockScript } from "@joyid/ckb";
18
-
19
- export function generateCollectorClass(codeHash: string) {
20
- return class JoyIDCellCollector {
21
- readonly fromScript: Script;
22
- readonly cellCollector: CellCollector;
23
-
24
- constructor(
25
- fromInfo: FromInfo,
26
- cellProvider: CellProvider,
27
- {
28
- queryOptions = {},
29
- config = getConfig(),
30
- }: { queryOptions?: QueryOptions; config?: Config },
31
- ) {
32
- if (!cellProvider) {
33
- throw new Error(
34
- `cellProvider is required when collecting JoyID-related cells`,
35
- );
36
- }
37
-
38
- if (typeof fromInfo !== "string") {
39
- throw new Error(`Only the address FromInfo is supported`);
40
- }
41
-
42
- const { fromScript } = parseFromInfo(fromInfo, { config });
43
- this.fromScript = fromScript;
44
-
45
- queryOptions = {
46
- ...queryOptions,
47
- lock: this.fromScript,
48
- type: queryOptions.type || "empty",
49
- data: queryOptions.data || "0x",
50
- };
51
-
52
- this.cellCollector = cellProvider.collector(queryOptions);
53
- }
54
-
55
- async *collect(): AsyncGenerator<Cell> {
56
- if (!bytes.equal(this.fromScript.codeHash, codeHash)) {
57
- return;
58
- }
59
-
60
- for await (const inputCell of this.cellCollector.collect()) {
61
- yield inputCell;
62
- }
63
- }
64
- };
65
- }
66
-
67
- export function generateJoyIDInfo(
68
- codeHash: string,
69
- cellDeps: CellDep[],
70
- ): LockScriptInfo {
71
- return {
72
- codeHash: codeHash,
73
- hashType: "type",
74
- lockScriptInfo: {
75
- CellCollector: generateCollectorClass(codeHash),
76
- prepareSigningEntries: () => {
77
- throw new Error(
78
- "JoyID doesn't support prepareSigningEntries, please do not mix JoyID locks with other locks in a transaction",
79
- );
80
- },
81
- async setupInputCell(txSkeleton, inputCell, _, options = {}) {
82
- const fromScript = inputCell.cellOutput.lock;
83
- asserts(
84
- bytes.equal(fromScript.codeHash, codeHash),
85
- `The input script is not JoyID script`,
86
- );
87
- // add inputCell to txSkeleton
88
- txSkeleton = txSkeleton.update("inputs", (inputs) =>
89
- inputs.push(inputCell),
90
- );
91
-
92
- const output: Cell = {
93
- cellOutput: {
94
- capacity: inputCell.cellOutput.capacity,
95
- lock: inputCell.cellOutput.lock,
96
- type: inputCell.cellOutput.type,
97
- },
98
- data: inputCell.data,
99
- };
100
-
101
- txSkeleton = txSkeleton.update("outputs", (outputs) =>
102
- outputs.push(output),
103
- );
104
-
105
- const since = options.since;
106
- if (since) {
107
- txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
108
- return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
109
- });
110
- }
111
-
112
- cellDeps.forEach((item) => {
113
- txSkeleton = addCellDep(txSkeleton, item);
114
- });
115
-
116
- return txSkeleton;
117
- },
118
- },
119
- };
120
- }
121
-
122
- export function generateDefaultScriptInfos(): LockScriptInfo[] {
123
- return [
124
- generateJoyIDInfo(getJoyIDLockScript(false).codeHash, [
125
- getJoyIDCellDep(false),
126
- ]),
127
- generateJoyIDInfo(getJoyIDLockScript(true).codeHash, [
128
- getJoyIDCellDep(true),
129
- ]),
130
- ];
131
- }
132
-
133
- function asserts(
134
- condition: unknown,
135
- message = "Assert failed",
136
- ): asserts condition {
137
- if (!condition) {
138
- throw new Error(message);
139
- }
140
- }
1
+ export * from "./default";
2
+ export * from "./utils";
package/src/utils.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Asserts that a condition is true, throwing an error if it is not.
3
+ * @param {unknown} condition - The condition to assert.
4
+ * @param {string} [message="Assert failed"] - The error message to throw if the condition is false.
5
+ * @throws {Error} If the condition is false.
6
+ */
7
+ export function asserts(
8
+ condition: unknown,
9
+ message = "Assert failed",
10
+ ): asserts condition {
11
+ if (!condition) {
12
+ throw new Error(message);
13
+ }
14
+ }