@ckb-ccc/spore 1.2.4 → 1.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/spore",
3
- "version": "1.2.4",
3
+ "version": "1.4.0",
4
4
  "description": "CCC - CKBer's Codebase. Common Chains Connector's support for Spore protocol",
5
5
  "author": "ashuralyk <ashuralyk@live.com>",
6
6
  "license": "MIT",
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "axios": "^1.7.7",
57
- "@ckb-ccc/core": "1.5.3"
57
+ "@ckb-ccc/core": "1.7.0"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "rimraf ./dist && rimraf ./dist.commonjs && tsc && tsc --project tsconfig.commonjs.json && copyfiles -u 2 misc/basedirs/**/* .",
@@ -67,12 +67,12 @@ export async function assertCluster(
67
67
  /**
68
68
  * Create a new Cluster cell
69
69
  *
70
- * @param signer who takes the responsibility to balance and sign the transaction
71
- * @param data specific format of data required by Cluster protocol
72
- * @param to the owner of the Cluster cell, which will be replaced with signer if not provided
73
- * @param tx the transaction skeleton, if not provided, a new one will be created
74
- * @param scriptInfo the script info of new cluster, default spore version if undefined
75
- * @param scriptInfoHash the script info hash used in cobuild
70
+ * @param params.signer who takes the responsibility to balance and sign the transaction
71
+ * @param params.data specific format of data required by Cluster protocol
72
+ * @param params.to the owner of the Cluster cell, which will be replaced with signer if not provided
73
+ * @param params.tx the transaction skeleton, if not provided, a new one will be created
74
+ * @param params.scriptInfo the script info of new cluster, default spore version if undefined
75
+ * @param params.scriptInfoHash the script info hash used in cobuild
76
76
  * @returns
77
77
  * - **tx**: a new transaction that contains created Cluster cell
78
78
  * - **id**: the id of the created Cluster cell
@@ -132,11 +132,11 @@ export async function createSporeCluster(params: {
132
132
  /**
133
133
  * Transfer a Cluster cell
134
134
  *
135
- * @param signer who takes the responsibility to balance and sign the transaction
136
- * @param id the id of the Cluster cell to be transferred
137
- * @param to the new owner of the Cluster cell
138
- * @param tx the transaction skeleton, if not provided, a new one will be created
139
- * @param scriptInfoHash the script info hash used in cobuild
135
+ * @param params.signer who takes the responsibility to balance and sign the transaction
136
+ * @param params.id the id of the Cluster cell to be transferred
137
+ * @param params.to the new owner of the Cluster cell
138
+ * @param params.tx the transaction skeleton, if not provided, a new one will be created
139
+ * @param params.scriptInfoHash the script info hash used in cobuild
140
140
  * @returns
141
141
  * - **tx**: a new transaction that contains transferred Cluster cell
142
142
  */
@@ -145,24 +145,24 @@ export async function transferSporeCluster(params: {
145
145
  id: ccc.HexLike;
146
146
  to: ccc.ScriptLike;
147
147
  tx?: ccc.TransactionLike;
148
+ scripts?: SporeScriptInfoLike[];
148
149
  scriptInfoHash?: ccc.HexLike;
149
150
  }): Promise<{
150
151
  tx: ccc.Transaction;
151
152
  }> {
152
- const { signer, id, to, scriptInfoHash } = params;
153
+ const { signer, id, to, scripts, scriptInfoHash } = params;
153
154
 
154
155
  // prepare transaction
155
156
  const tx = ccc.Transaction.from(params.tx ?? {});
156
157
 
157
158
  // build cluster cell
158
- const { cell: cluster, scriptInfo } = await assertCluster(signer.client, id);
159
-
160
- tx.inputs.push(
161
- ccc.CellInput.from({
162
- previousOutput: cluster.outPoint,
163
- ...cluster,
164
- }),
159
+ const { cell: cluster, scriptInfo: clusterScriptInfo } = await assertCluster(
160
+ signer.client,
161
+ id,
162
+ scripts,
165
163
  );
164
+
165
+ tx.addInput(cluster);
166
166
  tx.addOutput(
167
167
  {
168
168
  lock: to,
@@ -172,10 +172,10 @@ export async function transferSporeCluster(params: {
172
172
  );
173
173
 
174
174
  // complete cellDeps
175
- await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
175
+ await tx.addCellDepInfos(signer.client, clusterScriptInfo.cellDeps);
176
176
 
177
177
  // generate cobuild action
178
- const actions = scriptInfo.cobuild
178
+ const actions = clusterScriptInfo.cobuild
179
179
  ? [
180
180
  assembleTransferClusterAction(
181
181
  cluster.cellOutput,
@@ -193,16 +193,11 @@ export async function transferSporeCluster(params: {
193
193
  /**
194
194
  * Search on-chain clusters under the signer's control
195
195
  *
196
- * @param signer the owner of clusters
197
- * @param order the order in creation time of clusters
198
- * @param scriptInfos the deployed script infos of clusters
196
+ * @param params.signer the owner of clusters
197
+ * @param params.order the order in creation time of clusters
198
+ * @param params.scriptInfos the deployed script infos of clusters
199
199
  */
200
- export async function* findSporeClustersBySigner({
201
- signer,
202
- order,
203
- limit,
204
- scriptInfos,
205
- }: {
200
+ export async function* findSporeClustersBySigner(params: {
206
201
  signer: ccc.Signer;
207
202
  order?: "asc" | "desc";
208
203
  limit?: number;
@@ -213,6 +208,7 @@ export async function* findSporeClustersBySigner({
213
208
  clusterData: ClusterDataView;
214
209
  scriptInfo: SporeScriptInfo;
215
210
  }> {
211
+ const { signer, order, limit, scriptInfos } = params;
216
212
  for (const scriptInfo of scriptInfos ??
217
213
  Object.values(getClusterScriptInfos(signer.client))) {
218
214
  if (!scriptInfo) {
@@ -239,3 +235,56 @@ export async function* findSporeClustersBySigner({
239
235
  }
240
236
  }
241
237
  }
238
+
239
+ /**
240
+ * Search on-chain clusters under the specified lock or not
241
+ *
242
+ * @param params.client the client to search clusters
243
+ * @param params.lock the lock of clusters
244
+ */
245
+ export async function* findSporeClusters(params: {
246
+ client: ccc.Client;
247
+ lock?: ccc.ScriptLike;
248
+ order?: "asc" | "desc";
249
+ limit?: number;
250
+ scriptInfos?: SporeScriptInfoLike[];
251
+ }): AsyncGenerator<{
252
+ cell: ccc.Cell;
253
+ cluster: ccc.Cell;
254
+ clusterData: ClusterDataView;
255
+ scriptInfo: SporeScriptInfo;
256
+ }> {
257
+ const { client, lock, order, limit, scriptInfos } = params;
258
+ for (const scriptInfo of scriptInfos ??
259
+ Object.values(getClusterScriptInfos(client))) {
260
+ if (!scriptInfo) {
261
+ continue;
262
+ }
263
+
264
+ for await (const cluster of client.findCells(
265
+ {
266
+ script: {
267
+ ...scriptInfo,
268
+ args: "",
269
+ },
270
+ scriptType: "type",
271
+ scriptSearchMode: "prefix",
272
+ withData: true,
273
+ filter: lock
274
+ ? {
275
+ script: lock,
276
+ }
277
+ : undefined,
278
+ },
279
+ order,
280
+ limit,
281
+ )) {
282
+ yield {
283
+ cell: cluster,
284
+ cluster,
285
+ clusterData: unpackToRawClusterData(cluster.outputData),
286
+ scriptInfo: SporeScriptInfo.from(scriptInfo),
287
+ };
288
+ }
289
+ }
290
+ }
@@ -204,7 +204,8 @@ export function injectCobuild(
204
204
  tx: ccc.Transaction,
205
205
  actions: mol.EncodableType<typeof ActionVec>,
206
206
  ): void {
207
- const witnessLayout = ccc.hexFrom(
207
+ tx.setWitnessAt(
208
+ Math.max(tx.witnesses.length, tx.inputs.length),
208
209
  WitnessLayout.encode({
209
210
  type: "SighashAll",
210
211
  value: {
@@ -215,5 +216,4 @@ export function injectCobuild(
215
216
  },
216
217
  }),
217
218
  );
218
- tx.witnesses.push(ccc.hexFrom(witnessLayout));
219
219
  }
@@ -50,12 +50,7 @@ export async function prepareCluster(
50
50
  return;
51
51
  }
52
52
 
53
- tx.inputs.push(
54
- ccc.CellInput.from({
55
- previousOutput: cluster.outPoint,
56
- ...cluster,
57
- }),
58
- );
53
+ tx.addInput(cluster);
59
54
  tx.addOutput(cluster.cellOutput, cluster.outputData);
60
55
  await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
61
56
  // note: add cluster as cellDep, which will be used in Spore contract
@@ -69,17 +69,17 @@ export async function assertSpore(
69
69
  /**
70
70
  * Create one Spore cell with the specified Spore data.
71
71
  *
72
- * @param signer who takes the responsibility to balance and sign the transaction
73
- * @param data specific format of data required by Spore protocol
74
- * @param to owner of new spore cell, signer if no provided
75
- * @param clusterMode how to process cluster cell **(if clusterId is not provided in SporeData, this parameter will be ignored)**
72
+ * @param params.signer who takes the responsibility to balance and sign the transaction
73
+ * @param params.data specific format of data required by Spore protocol
74
+ * @param params.to owner of new spore cell, signer if no provided
75
+ * @param params.clusterMode how to process cluster cell **(if clusterId is not provided in SporeData, this parameter will be ignored)**
76
76
  * - undefined: error if the spore has a cluster but the clusterMode is not set
77
77
  * - lockProxy: put a cell that uses the same lock from Cluster cell in both Inputs and Outputs
78
78
  * - clusterCell: directly put Cluster cell in Inputs and Outputs
79
79
  * - skip: don't handle the cluster logic
80
- * @param tx the transaction skeleton, if not provided, a new one will be created
81
- * @param scriptInfo the script info of Spore cell, if not provided, the default script info will be used
82
- * @param scriptInfoHash the script info hash used in cobuild
80
+ * @param params.tx the transaction skeleton, if not provided, a new one will be created
81
+ * @param params.scriptInfo the script info of Spore cell, if not provided, the default script info will be used
82
+ * @param params.scriptInfoHash the script info hash used in cobuild
83
83
  * @returns
84
84
  * - **tx**: a new transaction that contains created Spore cells
85
85
  * - **id**: the sporeId of created Spore cell
@@ -151,11 +151,11 @@ export async function createSpore(params: {
151
151
  /**
152
152
  * Transfer one Spore cell
153
153
  *
154
- * @param signer who takes the responsibility to balance and sign the transaction
155
- * @param id sporeId
156
- * @param to Spore's new owner
157
- * @param tx the transaction skeleton, if not provided, a new one will be created
158
- * @param scriptInfoHash the script info hash used in cobuild
154
+ * @param params.signer who takes the responsibility to balance and sign the transaction
155
+ * @param params.id sporeId
156
+ * @param params.to Spore's new owner
157
+ * @param params.tx the transaction skeleton, if not provided, a new one will be created
158
+ * @param params.scriptInfoHash the script info hash used in cobuild
159
159
  * @returns
160
160
  * - **tx**: a new transaction that contains transferred Spore cells
161
161
  */
@@ -164,23 +164,23 @@ export async function transferSpore(params: {
164
164
  id: ccc.HexLike;
165
165
  to: ccc.ScriptLike;
166
166
  tx?: ccc.TransactionLike;
167
+ scripts?: SporeScriptInfoLike[];
167
168
  scriptInfoHash?: ccc.HexLike;
168
169
  }): Promise<{
169
170
  tx: ccc.Transaction;
170
171
  }> {
171
- const { signer, id, to, scriptInfoHash } = params;
172
+ const { signer, id, to, scripts, scriptInfoHash } = params;
172
173
 
173
174
  // prepare transaction
174
175
  const tx = ccc.Transaction.from(params.tx ?? {});
175
176
 
176
- const { cell: sporeCell, scriptInfo } = await assertSpore(signer.client, id);
177
- await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
178
- tx.inputs.push(
179
- ccc.CellInput.from({
180
- previousOutput: sporeCell.outPoint,
181
- ...sporeCell,
182
- }),
177
+ const { cell: sporeCell, scriptInfo: sporeScriptInfo } = await assertSpore(
178
+ signer.client,
179
+ id,
180
+ scripts,
183
181
  );
182
+ await tx.addCellDepInfos(signer.client, sporeScriptInfo.cellDeps);
183
+ tx.addInput(sporeCell);
184
184
  tx.addOutput(
185
185
  {
186
186
  lock: to,
@@ -189,7 +189,7 @@ export async function transferSpore(params: {
189
189
  sporeCell.outputData,
190
190
  );
191
191
 
192
- const actions = scriptInfo.cobuild
192
+ const actions = sporeScriptInfo.cobuild
193
193
  ? [
194
194
  assembleTransferSporeAction(
195
195
  sporeCell.cellOutput,
@@ -207,10 +207,10 @@ export async function transferSpore(params: {
207
207
  /**
208
208
  * Melt one Spore cell
209
209
  *
210
- * @param signer who takes the responsibility to balance and sign the transaction
211
- * @param id sporeId to be melted
212
- * @param tx the transaction skeleton, if not provided, a new one will be created
213
- * @param scriptInfoHash the script info hash used in cobuild
210
+ * @param params.signer who takes the responsibility to balance and sign the transaction
211
+ * @param params.id sporeId to be melted
212
+ * @param params.tx the transaction skeleton, if not provided, a new one will be created
213
+ * @param params.scriptInfoHash the script info hash used in cobuild
214
214
  * @returns
215
215
  * - **tx**: a new transaction that contains melted Spore cell
216
216
  */
@@ -218,26 +218,26 @@ export async function meltSpore(params: {
218
218
  signer: ccc.Signer;
219
219
  id: ccc.HexLike;
220
220
  tx?: ccc.TransactionLike;
221
+ scripts?: SporeScriptInfoLike[];
221
222
  scriptInfoHash?: ccc.HexLike;
222
223
  }): Promise<{
223
224
  tx: ccc.Transaction;
224
225
  }> {
225
- const { signer, id, scriptInfoHash } = params;
226
+ const { signer, id, scripts, scriptInfoHash } = params;
226
227
 
227
228
  // prepare transaction
228
229
  const tx = ccc.Transaction.from(params.tx ?? {});
229
230
 
230
231
  // build spore cell
231
- const { cell: sporeCell, scriptInfo } = await assertSpore(signer.client, id);
232
- await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
233
- tx.inputs.push(
234
- ccc.CellInput.from({
235
- previousOutput: sporeCell.outPoint,
236
- ...sporeCell,
237
- }),
232
+ const { cell: sporeCell, scriptInfo: sporeScriptInfo } = await assertSpore(
233
+ signer.client,
234
+ id,
235
+ scripts,
238
236
  );
237
+ await tx.addCellDepInfos(signer.client, sporeScriptInfo.cellDeps);
238
+ tx.addInput(sporeCell);
239
239
 
240
- const actions = scriptInfo.cobuild
240
+ const actions = sporeScriptInfo.cobuild
241
241
  ? [assembleMeltSporeAction(sporeCell.cellOutput, scriptInfoHash)]
242
242
  : [];
243
243
 
@@ -249,19 +249,13 @@ export async function meltSpore(params: {
249
249
  /**
250
250
  * Search on-chain spores under the signer's control, if cluster provided, filter spores belonging to this cluster
251
251
  *
252
- * @param signer the owner of spores
253
- * @param order the order in creation time of spores
254
- * @param clusterId the cluster that spores belong to. "" to find public spores
255
- * @param scriptInfos the deployed script infos of spores
252
+ * @param params.signer the owner of spores
253
+ * @param params.order the order in creation time of spores
254
+ * @param params.clusterId the cluster that spores belong to. "" to find public spores
255
+ * @param params.scriptInfos the deployed script infos of spores
256
256
  * @returns specified spore cells
257
257
  */
258
- export async function* findSporesBySigner({
259
- signer,
260
- clusterId,
261
- scriptInfos,
262
- limit,
263
- order,
264
- }: {
258
+ export async function* findSporesBySigner(params: {
265
259
  signer: ccc.Signer;
266
260
  order?: "asc" | "desc";
267
261
  limit?: number;
@@ -273,6 +267,7 @@ export async function* findSporesBySigner({
273
267
  sporeData: SporeDataView;
274
268
  scriptInfo: SporeScriptInfo;
275
269
  }> {
270
+ const { signer, clusterId, scriptInfos, limit, order } = params;
276
271
  for (const scriptInfo of scriptInfos ??
277
272
  Object.values(getSporeScriptInfos(signer.client))) {
278
273
  if (!scriptInfo) {
@@ -305,3 +300,68 @@ export async function* findSporesBySigner({
305
300
  }
306
301
  }
307
302
  }
303
+
304
+ /**
305
+ * Search on-chain spores under the specified lock or not, if cluster provided, filter spores belonging to this cluster
306
+ *
307
+ * @param params.client the client to search spores
308
+ * @param params.lock the lock of spores
309
+ * @param params.clusterId the cluster that spores belong to. "" to find public spores
310
+ * @param params.scriptInfos the deployed script infos of spores
311
+ * @param params.limit the limit of spores to search
312
+ * @param params.order the order in creation time of spores
313
+ * @returns specified spore cells
314
+ */
315
+ export async function* findSpores(params: {
316
+ client: ccc.Client;
317
+ lock?: ccc.ScriptLike;
318
+ clusterId?: ccc.HexLike;
319
+ scriptInfos?: SporeScriptInfoLike[];
320
+ limit?: number;
321
+ order?: "asc" | "desc";
322
+ }): AsyncGenerator<{
323
+ cell: ccc.Cell;
324
+ spore: ccc.Cell;
325
+ sporeData: SporeDataView;
326
+ scriptInfo: SporeScriptInfo;
327
+ }> {
328
+ const { client, lock, clusterId, scriptInfos, limit, order } = params;
329
+ for (const scriptInfo of scriptInfos ??
330
+ Object.values(getSporeScriptInfos(client))) {
331
+ if (!scriptInfo) {
332
+ continue;
333
+ }
334
+ for await (const spore of client.findCells(
335
+ {
336
+ script: {
337
+ ...scriptInfo,
338
+ args: [],
339
+ },
340
+ scriptType: "type",
341
+ scriptSearchMode: "prefix",
342
+ withData: true,
343
+ filter: lock
344
+ ? {
345
+ script: lock,
346
+ }
347
+ : undefined,
348
+ },
349
+ order,
350
+ limit,
351
+ )) {
352
+ const sporeData = unpackToRawSporeData(spore.outputData);
353
+ if (
354
+ !clusterId ||
355
+ (clusterId === "" && !sporeData.clusterId) ||
356
+ sporeData.clusterId === ccc.hexFrom(clusterId)
357
+ ) {
358
+ yield {
359
+ cell: spore,
360
+ spore,
361
+ sporeData,
362
+ scriptInfo: SporeScriptInfo.from(scriptInfo),
363
+ };
364
+ }
365
+ }
366
+ }
367
+ }