@ckb-ccc/core 0.0.6-alpha.0 → 0.0.7-alpha.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.
Files changed (215) hide show
  1. package/dist/address/address.advanced.d.ts.map +1 -1
  2. package/dist/address/address.advanced.js +2 -5
  3. package/dist/address/index.d.ts +1 -1
  4. package/dist/address/index.d.ts.map +1 -1
  5. package/dist/address/index.js +2 -5
  6. package/dist/ckb/script.d.ts +43 -1
  7. package/dist/ckb/script.d.ts.map +1 -1
  8. package/dist/ckb/script.js +51 -0
  9. package/dist/ckb/transaction.d.ts +198 -21
  10. package/dist/ckb/transaction.d.ts.map +1 -1
  11. package/dist/ckb/transaction.js +437 -21
  12. package/dist/client/advanced.d.ts +2 -0
  13. package/dist/client/advanced.d.ts.map +1 -1
  14. package/dist/client/advanced.js +2 -0
  15. package/dist/client/client.advanced.d.ts +10 -0
  16. package/dist/client/client.advanced.d.ts.map +1 -0
  17. package/dist/client/client.advanced.js +80 -0
  18. package/dist/client/client.d.ts +44 -8
  19. package/dist/client/client.d.ts.map +1 -1
  20. package/dist/client/client.js +137 -9
  21. package/dist/client/clientPublicMainnet.advanced.d.ts +4 -2
  22. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  23. package/dist/client/clientPublicMainnet.advanced.js +102 -0
  24. package/dist/client/clientPublicMainnet.d.ts +4 -2
  25. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  26. package/dist/client/clientPublicMainnet.js +5 -1
  27. package/dist/client/clientPublicTestnet.advanced.d.ts +4 -2
  28. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  29. package/dist/client/clientPublicTestnet.advanced.js +151 -0
  30. package/dist/client/clientPublicTestnet.d.ts +4 -2
  31. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  32. package/dist/client/clientPublicTestnet.js +6 -1
  33. package/dist/client/clientTypes.advanced.d.ts +23 -0
  34. package/dist/client/clientTypes.advanced.d.ts.map +1 -0
  35. package/dist/client/clientTypes.advanced.js +4 -0
  36. package/dist/client/clientTypes.d.ts +22 -36
  37. package/dist/client/clientTypes.d.ts.map +1 -1
  38. package/dist/client/clientTypes.js +15 -12
  39. package/dist/client/jsonRpc/index.d.ts +3 -3
  40. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  41. package/dist/client/jsonRpc/index.js +3 -3
  42. package/dist/client/jsonRpc/transformers.d.ts +5 -7
  43. package/dist/client/jsonRpc/transformers.d.ts.map +1 -1
  44. package/dist/client/jsonRpc/transformers.js +8 -6
  45. package/dist/hasher/index.d.ts +1 -1
  46. package/dist/hasher/index.d.ts.map +1 -1
  47. package/dist/hasher/index.js +4 -2
  48. package/dist/num/index.d.ts +12 -0
  49. package/dist/num/index.d.ts.map +1 -1
  50. package/dist/num/index.js +21 -0
  51. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  52. package/dist/signer/btc/signerBtc.js +2 -1
  53. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  54. package/dist/signer/evm/signerEvm.js +5 -3
  55. package/dist/signer/nostr/signerNostr.d.ts.map +1 -1
  56. package/dist/signer/nostr/signerNostr.js +2 -1
  57. package/dist/utils/index.d.ts +1 -1
  58. package/dist/utils/index.d.ts.map +1 -1
  59. package/dist/utils/index.js +1 -1
  60. package/dist.commonjs/address/address.advanced.d.ts +29 -24
  61. package/dist.commonjs/address/address.advanced.d.ts.map +1 -1
  62. package/dist.commonjs/address/address.advanced.js +115 -92
  63. package/dist.commonjs/address/advanced.d.ts +1 -1
  64. package/dist.commonjs/address/advanced.js +30 -14
  65. package/dist.commonjs/address/index.d.ts +51 -44
  66. package/dist.commonjs/address/index.d.ts.map +1 -1
  67. package/dist.commonjs/address/index.js +84 -68
  68. package/dist.commonjs/advanced.d.ts +1 -1
  69. package/dist.commonjs/advanced.js +47 -22
  70. package/dist.commonjs/advancedBarrel.d.ts +1 -1
  71. package/dist.commonjs/advancedBarrel.js +30 -14
  72. package/dist.commonjs/barrel.d.ts +1 -1
  73. package/dist.commonjs/barrel.js +30 -14
  74. package/dist.commonjs/bytes/advanced.d.ts +11 -2
  75. package/dist.commonjs/bytes/index.d.ts +9 -3
  76. package/dist.commonjs/bytes/index.js +28 -26
  77. package/dist.commonjs/ckb/advanced.d.ts +1 -1
  78. package/dist.commonjs/ckb/advanced.js +47 -22
  79. package/dist.commonjs/ckb/index.d.ts +1 -1
  80. package/dist.commonjs/ckb/index.js +30 -14
  81. package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +420 -260
  82. package/dist.commonjs/ckb/molecule.advanced/generated.js +1536 -1332
  83. package/dist.commonjs/ckb/molecule.advanced/index.d.ts +3 -3
  84. package/dist.commonjs/ckb/molecule.advanced/index.js +34 -18
  85. package/dist.commonjs/ckb/script.advanced.d.ts +1 -1
  86. package/dist.commonjs/ckb/script.advanced.js +12 -9
  87. package/dist.commonjs/ckb/script.d.ts +123 -77
  88. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  89. package/dist.commonjs/ckb/script.js +221 -126
  90. package/dist.commonjs/ckb/transaction.advanced.d.ts +1 -1
  91. package/dist.commonjs/ckb/transaction.advanced.js +4 -4
  92. package/dist.commonjs/ckb/transaction.d.ts +796 -544
  93. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  94. package/dist.commonjs/ckb/transaction.js +1371 -725
  95. package/dist.commonjs/client/advanced.d.ts +3 -1
  96. package/dist.commonjs/client/advanced.d.ts.map +1 -1
  97. package/dist.commonjs/client/advanced.js +32 -14
  98. package/dist.commonjs/client/client.advanced.d.ts +27 -0
  99. package/dist.commonjs/client/client.advanced.d.ts.map +1 -0
  100. package/dist.commonjs/client/client.advanced.js +116 -0
  101. package/dist.commonjs/client/client.d.ts +118 -22
  102. package/dist.commonjs/client/client.d.ts.map +1 -1
  103. package/dist.commonjs/client/client.js +218 -56
  104. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +9 -3
  105. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  106. package/dist.commonjs/client/clientPublicMainnet.advanced.js +144 -25
  107. package/dist.commonjs/client/clientPublicMainnet.d.ts +9 -5
  108. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  109. package/dist.commonjs/client/clientPublicMainnet.js +18 -12
  110. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +8 -3
  111. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  112. package/dist.commonjs/client/clientPublicTestnet.advanced.js +204 -28
  113. package/dist.commonjs/client/clientPublicTestnet.d.ts +9 -5
  114. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  115. package/dist.commonjs/client/clientPublicTestnet.js +14 -9
  116. package/dist.commonjs/client/clientTypes.advanced.d.ts +23 -0
  117. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -0
  118. package/dist.commonjs/client/clientTypes.advanced.js +8 -0
  119. package/dist.commonjs/client/clientTypes.d.ts +57 -46
  120. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  121. package/dist.commonjs/client/clientTypes.js +60 -22
  122. package/dist.commonjs/client/index.d.ts +1 -1
  123. package/dist.commonjs/client/index.js +30 -14
  124. package/dist.commonjs/client/jsonRpc/advanced.d.ts +1 -1
  125. package/dist.commonjs/client/jsonRpc/advanced.js +30 -14
  126. package/dist.commonjs/client/jsonRpc/index.d.ts +94 -75
  127. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  128. package/dist.commonjs/client/jsonRpc/index.js +135 -108
  129. package/dist.commonjs/client/jsonRpc/transformers.d.ts +76 -38
  130. package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +1 -1
  131. package/dist.commonjs/client/jsonRpc/transformers.js +174 -150
  132. package/dist.commonjs/client/jsonRpc/types.d.ts +38 -38
  133. package/dist.commonjs/fixedPoint/index.d.ts +9 -3
  134. package/dist.commonjs/fixedPoint/index.js +26 -20
  135. package/dist.commonjs/fixedPoint/index.test.d.ts +1 -1
  136. package/dist.commonjs/fixedPoint/index.test.js +42 -36
  137. package/dist.commonjs/hasher/advanced.d.ts +1 -1
  138. package/dist.commonjs/hasher/index.d.ts +37 -37
  139. package/dist.commonjs/hasher/index.d.ts.map +1 -1
  140. package/dist.commonjs/hasher/index.js +55 -46
  141. package/dist.commonjs/hex/index.d.ts +1 -1
  142. package/dist.commonjs/hex/index.js +1 -1
  143. package/dist.commonjs/index.d.ts +1 -1
  144. package/dist.commonjs/index.js +47 -22
  145. package/dist.commonjs/num/index.d.ts +13 -1
  146. package/dist.commonjs/num/index.d.ts.map +1 -1
  147. package/dist.commonjs/num/index.js +53 -19
  148. package/dist.commonjs/signer/btc/index.d.ts +1 -1
  149. package/dist.commonjs/signer/btc/index.js +30 -14
  150. package/dist.commonjs/signer/btc/signerBtc.d.ts +47 -47
  151. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
  152. package/dist.commonjs/signer/btc/signerBtc.js +89 -65
  153. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +45 -45
  154. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +52 -52
  155. package/dist.commonjs/signer/btc/verify.d.ts +6 -2
  156. package/dist.commonjs/signer/btc/verify.js +10 -3
  157. package/dist.commonjs/signer/ckb/index.d.ts +1 -1
  158. package/dist.commonjs/signer/ckb/index.js +30 -14
  159. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +45 -45
  160. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +56 -56
  161. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +6 -2
  162. package/dist.commonjs/signer/ckb/verifyJoyId.js +11 -8
  163. package/dist.commonjs/signer/dummy/alwaysError.d.ts +4 -4
  164. package/dist.commonjs/signer/dummy/alwaysError.js +7 -7
  165. package/dist.commonjs/signer/dummy/dummy.d.ts +7 -7
  166. package/dist.commonjs/signer/dummy/dummy.js +16 -16
  167. package/dist.commonjs/signer/dummy/index.d.ts +1 -1
  168. package/dist.commonjs/signer/dummy/index.js +30 -14
  169. package/dist.commonjs/signer/dummy/openLink.d.ts +4 -4
  170. package/dist.commonjs/signer/dummy/openLink.js +7 -7
  171. package/dist.commonjs/signer/evm/index.d.ts +1 -1
  172. package/dist.commonjs/signer/evm/index.js +30 -14
  173. package/dist.commonjs/signer/evm/signerEvm.d.ts +42 -38
  174. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
  175. package/dist.commonjs/signer/evm/signerEvm.js +105 -68
  176. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +37 -37
  177. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +42 -42
  178. package/dist.commonjs/signer/evm/verify.d.ts +6 -2
  179. package/dist.commonjs/signer/evm/verify.js +7 -2
  180. package/dist.commonjs/signer/index.d.ts +1 -1
  181. package/dist.commonjs/signer/index.js +30 -14
  182. package/dist.commonjs/signer/nostr/index.d.ts +1 -1
  183. package/dist.commonjs/signer/nostr/index.js +30 -14
  184. package/dist.commonjs/signer/nostr/signerNostr.d.ts +57 -57
  185. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -1
  186. package/dist.commonjs/signer/nostr/signerNostr.js +100 -70
  187. package/dist.commonjs/signer/nostr/verify.d.ts +9 -3
  188. package/dist.commonjs/signer/nostr/verify.js +50 -39
  189. package/dist.commonjs/signer/signer/index.d.ts +156 -150
  190. package/dist.commonjs/signer/signer/index.js +208 -181
  191. package/dist.commonjs/utils/index.d.ts +38 -8
  192. package/dist.commonjs/utils/index.d.ts.map +1 -1
  193. package/dist.commonjs/utils/index.js +18 -12
  194. package/package.json +2 -2
  195. package/src/address/address.advanced.ts +2 -5
  196. package/src/address/index.ts +2 -5
  197. package/src/ckb/script.ts +62 -2
  198. package/src/ckb/transaction.ts +634 -46
  199. package/src/client/advanced.ts +2 -0
  200. package/src/client/client.advanced.ts +134 -0
  201. package/src/client/client.ts +225 -16
  202. package/src/client/clientPublicMainnet.advanced.ts +116 -2
  203. package/src/client/clientPublicMainnet.ts +8 -3
  204. package/src/client/clientPublicTestnet.advanced.ts +171 -2
  205. package/src/client/clientPublicTestnet.ts +9 -3
  206. package/src/client/clientTypes.advanced.ts +26 -0
  207. package/src/client/clientTypes.ts +41 -43
  208. package/src/client/jsonRpc/index.ts +4 -4
  209. package/src/client/jsonRpc/transformers.ts +13 -11
  210. package/src/hasher/index.ts +4 -2
  211. package/src/num/index.ts +22 -0
  212. package/src/signer/btc/signerBtc.ts +2 -1
  213. package/src/signer/evm/signerEvm.ts +5 -3
  214. package/src/signer/nostr/signerNostr.ts +2 -1
  215. package/src/utils/index.ts +6 -2
@@ -1,3 +1,5 @@
1
+ export * from "./client.advanced";
1
2
  export * from "./clientPublicMainnet.advanced";
2
3
  export * from "./clientPublicTestnet.advanced";
4
+ export * from "./clientTypes.advanced";
3
5
  export * from "./jsonRpc/advanced";
@@ -0,0 +1,134 @@
1
+ import { bytesFrom } from "../bytes";
2
+ import { Cell, CellLike, Script, ScriptLike } from "../ckb";
3
+ import { HexLike, hexFrom } from "../hex";
4
+ import { NumLike, numFrom } from "../num";
5
+ import { ClientIndexerSearchKey } from "./clientTypes";
6
+ import {
7
+ ClientCollectableSearchKeyLike,
8
+ clientSearchKeyRangeFrom,
9
+ } from "./clientTypes.advanced";
10
+
11
+ export function filterData(
12
+ dataLike: HexLike,
13
+ filterLike: HexLike | undefined,
14
+ filterMode: "exact" | "prefix" | "partial",
15
+ ): boolean {
16
+ if (!filterLike) {
17
+ return true;
18
+ }
19
+
20
+ const data = hexFrom(dataLike);
21
+ const filter = hexFrom(filterLike);
22
+ if (
23
+ (filterMode === "exact" && data !== filter) ||
24
+ (filterMode === "prefix" && !data.startsWith(filter)) ||
25
+ (filterMode === "partial" && data.search(filter) === -1)
26
+ ) {
27
+ return false;
28
+ }
29
+
30
+ return true;
31
+ }
32
+
33
+ export function filterScript(
34
+ valueLike: ScriptLike | undefined,
35
+ filterLike: ScriptLike | undefined,
36
+ filterMode: "prefix" | "exact" | "partial",
37
+ ): boolean {
38
+ if (!filterLike) {
39
+ return true;
40
+ }
41
+ if (!valueLike) {
42
+ return false;
43
+ }
44
+
45
+ const value = Script.from(valueLike);
46
+ const filter = Script.from(filterLike);
47
+ if (
48
+ value.codeHash !== filter.codeHash ||
49
+ value.hashType !== filter.hashType
50
+ ) {
51
+ return false;
52
+ }
53
+
54
+ return filterData(value.args, filter?.args, filterMode);
55
+ }
56
+
57
+ export function filterNumByRange(
58
+ lengthLike: NumLike,
59
+ range: [NumLike, NumLike] | undefined,
60
+ ) {
61
+ if (!range) {
62
+ return true;
63
+ }
64
+ const length = numFrom(lengthLike);
65
+ const [lower, upper] = clientSearchKeyRangeFrom(range);
66
+
67
+ return lower <= length && length < upper;
68
+ }
69
+
70
+ export function filterScriptByLenRange(
71
+ valueLike?: ScriptLike,
72
+ scriptLenRange?: [NumLike, NumLike],
73
+ ): boolean {
74
+ if (!scriptLenRange) {
75
+ return true;
76
+ }
77
+
78
+ const len = (() => {
79
+ if (!valueLike) {
80
+ return 0;
81
+ }
82
+ return bytesFrom(Script.from(valueLike).args).length + 33;
83
+ })();
84
+ return filterNumByRange(len, scriptLenRange);
85
+ }
86
+
87
+ export function filterCell(
88
+ searchKeyLike: ClientCollectableSearchKeyLike,
89
+ cellLike: CellLike,
90
+ ): boolean {
91
+ const key = ClientIndexerSearchKey.from(searchKeyLike);
92
+ const cell = Cell.from(cellLike);
93
+
94
+ if (key.scriptType === "lock") {
95
+ if (
96
+ !filterScript(cell.cellOutput.lock, key.script, key.scriptSearchMode) ||
97
+ !filterScript(cell.cellOutput.type, key.filter?.script, "prefix") ||
98
+ !filterScriptByLenRange(cell.cellOutput.type, key.filter?.scriptLenRange)
99
+ ) {
100
+ return false;
101
+ }
102
+ }
103
+ if (key.scriptType === "type") {
104
+ if (
105
+ !filterScript(cell.cellOutput.type, key.script, key.scriptSearchMode) ||
106
+ !filterScript(cell.cellOutput.lock, key.filter?.script, "prefix") ||
107
+ !filterScriptByLenRange(cell.cellOutput.lock, key.filter?.scriptLenRange)
108
+ ) {
109
+ return false;
110
+ }
111
+ }
112
+
113
+ if (
114
+ !filterData(
115
+ cell.outputData,
116
+ key.filter?.outputData,
117
+ key.filter?.outputDataSearchMode ?? "prefix",
118
+ ) ||
119
+ !filterNumByRange(
120
+ bytesFrom(cell.outputData).length,
121
+ key.filter?.outputDataLenRange,
122
+ )
123
+ ) {
124
+ return false;
125
+ }
126
+
127
+ if (
128
+ !filterNumByRange(cell.cellOutput.capacity, key.filter?.outputCapacityRange)
129
+ ) {
130
+ return false;
131
+ }
132
+
133
+ return true;
134
+ }
@@ -1,49 +1,115 @@
1
1
  import {
2
2
  Cell,
3
+ CellDep,
4
+ CellDepLike,
5
+ CellLike,
6
+ OutPoint,
3
7
  OutPointLike,
4
8
  Script,
5
9
  ScriptLike,
10
+ Transaction,
6
11
  TransactionLike,
7
12
  } from "../ckb";
8
13
  import { Zero } from "../fixedPoint";
9
- import { Hex, HexLike } from "../hex";
14
+ import { Hex, HexLike, hexFrom } from "../hex";
10
15
  import { Num, NumLike, numFrom } from "../num";
11
- import { reduceAsync } from "../utils";
16
+ import { apply, reduceAsync } from "../utils";
17
+ import { filterCell } from "./client.advanced";
12
18
  import {
13
19
  ClientFindCellsResponse,
20
+ ClientIndexerSearchKey,
14
21
  ClientIndexerSearchKeyLike,
15
22
  ClientTransactionResponse,
16
23
  OutputsValidator,
17
24
  } from "./clientTypes";
25
+ import { ClientCollectableSearchKeyLike } from "./clientTypes.advanced";
18
26
 
19
27
  export enum KnownScript {
20
28
  Secp256k1Blake160 = "Secp256k1Blake160",
21
29
  Secp256k1Multisig = "Secp256k1Multisig",
22
30
  AnyoneCanPay = "AnyoneCanPay",
31
+ TypeId = "TypeId",
32
+ XUdt = "XUdt",
23
33
  JoyId = "JoyId",
24
34
  COTA = "COTA",
25
35
  OmniLock = "OmniLock",
26
36
  NostrLock = "NostrLock",
37
+ SingleUseLock = "SingleUseLock",
38
+ OutputTypeProxyLock = "OutputTypeProxyLock",
39
+ }
40
+
41
+ export type CellDepInfoLike = {
42
+ cellDep: CellDepLike;
43
+ type?: ScriptLike;
44
+ };
45
+
46
+ export class CellDepInfo {
47
+ constructor(
48
+ public cellDep: CellDep,
49
+ public type?: Script,
50
+ ) {}
51
+
52
+ static from(cellDepInfoLike: CellDepInfoLike): CellDepInfo {
53
+ return new CellDepInfo(
54
+ CellDep.from(cellDepInfoLike.cellDep),
55
+ apply(Script.from, cellDepInfoLike.type),
56
+ );
57
+ }
27
58
  }
28
59
 
29
60
  export abstract class Client {
61
+ private readonly cachedTransactions: Transaction[] = [];
62
+ private readonly unusableOutPoints: OutPoint[] = [];
63
+ private readonly usableCells: Cell[] = [];
64
+ private readonly knownCells: Cell[] = [];
65
+
30
66
  abstract get url(): string;
31
67
  abstract get addressPrefix(): string;
32
68
 
33
69
  abstract getKnownScript(
34
70
  script: KnownScript,
35
- ): Promise<Pick<Script, "codeHash" | "hashType">>;
71
+ ): Promise<
72
+ Pick<Script, "codeHash" | "hashType"> & { cellDeps: CellDepInfo[] }
73
+ >;
74
+
75
+ async markUsable(cellLike: CellLike): Promise<void> {
76
+ const cell = Cell.from(cellLike).clone();
77
+ this.usableCells.push(cell);
78
+ this.knownCells.push(cell);
36
79
 
37
- abstract sendTransaction(
80
+ const index = this.unusableOutPoints.findIndex((o) => cell.outPoint.eq(o));
81
+ if (index !== -1) {
82
+ this.unusableOutPoints.splice(index, 1);
83
+ }
84
+ }
85
+
86
+ async markUnusable(outPointLike: OutPointLike): Promise<void> {
87
+ const outPoint = OutPoint.from(outPointLike);
88
+ this.unusableOutPoints.push(outPoint.clone());
89
+
90
+ const index = this.usableCells.findIndex((c) => c.outPoint.eq(outPoint));
91
+ if (index !== -1) {
92
+ this.usableCells.splice(index, 1);
93
+ }
94
+ }
95
+
96
+ abstract sendTransactionNoCache(
38
97
  transaction: TransactionLike,
39
98
  validator?: OutputsValidator,
40
99
  ): Promise<Hex>;
41
- abstract getTransaction(
100
+ abstract getTransactionNoCache(
42
101
  txHash: HexLike,
43
102
  ): Promise<ClientTransactionResponse | null>;
44
103
 
45
- async getCell(outPoint: OutPointLike): Promise<Cell | null> {
46
- const transaction = await this.getTransaction(outPoint.txHash);
104
+ async getCell(outPointLike: OutPointLike): Promise<Cell | null> {
105
+ const outPoint = OutPoint.from(outPointLike);
106
+ const cached = this.knownCells.find((cell) => cell.outPoint.eq(outPoint));
107
+
108
+ if (cached) {
109
+ return cached.clone();
110
+ }
111
+
112
+ const transaction = await this.getTransactionNoCache(outPoint.txHash);
47
113
  if (!transaction) {
48
114
  return null;
49
115
  }
@@ -53,20 +119,31 @@ export abstract class Client {
53
119
  return null;
54
120
  }
55
121
 
56
- return Cell.from({
122
+ const cell = Cell.from({
57
123
  outPoint,
58
124
  cellOutput: transaction.transaction.outputs[index],
59
125
  outputData: transaction.transaction.outputsData[index] ?? "0x",
60
- blockNumber: transaction.blockNumber,
61
126
  });
127
+ this.knownCells.push(cell);
128
+ return cell.clone();
62
129
  }
63
130
 
64
- abstract findCellsPaged(
131
+ abstract findCellsPagedNoCache(
65
132
  key: ClientIndexerSearchKeyLike,
66
133
  order?: "asc" | "desc",
67
134
  limit?: NumLike,
68
135
  after?: string,
69
136
  ): Promise<ClientFindCellsResponse>;
137
+ async findCellsPaged(
138
+ key: ClientIndexerSearchKeyLike,
139
+ order?: "asc" | "desc",
140
+ limit?: NumLike,
141
+ after?: string,
142
+ ): Promise<ClientFindCellsResponse> {
143
+ const res = await this.findCellsPagedNoCache(key, order, limit, after);
144
+ this.knownCells.push(...res.cells);
145
+ return res;
146
+ }
70
147
 
71
148
  async *findCells(
72
149
  key: ClientIndexerSearchKeyLike,
@@ -92,20 +169,105 @@ export abstract class Client {
92
169
  }
93
170
  }
94
171
 
172
+ /**
173
+ * Find cells by search key designed for collectable cells.
174
+ *
175
+ * @param key - The search key.
176
+ * @returns A async generator for yielding cells.
177
+ */
178
+ async *findCellsByCollectableSearchKey(
179
+ keyLike: ClientCollectableSearchKeyLike,
180
+ order?: "asc" | "desc",
181
+ limit = 10,
182
+ ): AsyncGenerator<Cell> {
183
+ const key = ClientIndexerSearchKey.from(keyLike);
184
+ for (const cell of this.usableCells) {
185
+ if (filterCell(key, cell)) {
186
+ yield cell;
187
+ }
188
+ }
189
+
190
+ for await (const cell of this.findCells(key, order, limit)) {
191
+ if (!this.unusableOutPoints.some((o) => o.eq(cell.outPoint))) {
192
+ yield cell;
193
+ }
194
+ }
195
+ }
196
+
95
197
  findCellsByLockAndType(
96
198
  lock: ScriptLike,
97
199
  type: ScriptLike,
98
200
  withData = true,
201
+ order?: "asc" | "desc",
202
+ limit = 10,
99
203
  ): AsyncGenerator<Cell> {
100
- return this.findCells({
101
- script: lock,
102
- scriptType: "lock",
103
- scriptSearchMode: "exact",
104
- filter: {
204
+ return this.findCellsByCollectableSearchKey(
205
+ {
206
+ script: lock,
207
+ scriptType: "lock",
208
+ scriptSearchMode: "exact",
209
+ filter: {
210
+ script: type,
211
+ },
212
+ withData,
213
+ },
214
+ order,
215
+ limit,
216
+ );
217
+ }
218
+
219
+ findCellsByType(
220
+ type: ScriptLike,
221
+ withData = true,
222
+ order?: "asc" | "desc",
223
+ limit = 10,
224
+ ): AsyncGenerator<Cell> {
225
+ return this.findCellsByCollectableSearchKey(
226
+ {
105
227
  script: type,
228
+ scriptType: "type",
229
+ scriptSearchMode: "exact",
230
+ withData,
106
231
  },
232
+ order,
233
+ limit,
234
+ );
235
+ }
236
+
237
+ async findSingletonCellByType(
238
+ type: ScriptLike,
239
+ withData = false,
240
+ ): Promise<Cell | undefined> {
241
+ for await (const cell of this.findCellsByType(
242
+ type,
107
243
  withData,
108
- });
244
+ undefined,
245
+ 1,
246
+ )) {
247
+ return cell;
248
+ }
249
+ }
250
+
251
+ async getCellDeps(
252
+ ...cellDepsInfoLike: (CellDepInfoLike | CellDepInfoLike[])[]
253
+ ): Promise<CellDep[]> {
254
+ return Promise.all(
255
+ cellDepsInfoLike.flat().map(async (infoLike) => {
256
+ const { cellDep, type } = CellDepInfo.from(infoLike);
257
+ if (type === undefined) {
258
+ return cellDep;
259
+ }
260
+ const found = await this.findSingletonCellByType(type);
261
+ if (!found) {
262
+ return cellDep;
263
+ }
264
+
265
+ return CellDep.from({
266
+ outPoint: found.outPoint,
267
+ depType: cellDep.depType,
268
+ });
269
+ }),
270
+ );
109
271
  }
110
272
 
111
273
  abstract getCellsCapacity(key: ClientIndexerSearchKeyLike): Promise<Num>;
@@ -129,4 +291,51 @@ export abstract class Client {
129
291
  Zero,
130
292
  );
131
293
  }
294
+
295
+ async sendTransaction(
296
+ transaction: TransactionLike,
297
+ validator?: OutputsValidator,
298
+ ): Promise<Hex> {
299
+ const tx = Transaction.from(transaction);
300
+
301
+ const txHash = await this.sendTransactionNoCache(tx, validator);
302
+
303
+ this.cachedTransactions.push(tx.clone());
304
+ await Promise.all(
305
+ tx.inputs.map((i) => this.markUnusable(i.previousOutput)),
306
+ );
307
+ await Promise.all(
308
+ tx.outputs.map((o, i) =>
309
+ this.markUsable({
310
+ cellOutput: o,
311
+ outputData: tx.outputsData[i],
312
+ outPoint: {
313
+ txHash,
314
+ index: i,
315
+ },
316
+ }),
317
+ ),
318
+ );
319
+ return txHash;
320
+ }
321
+
322
+ async getTransaction(
323
+ txHashLike: HexLike,
324
+ ): Promise<ClientTransactionResponse | null> {
325
+ const txHash = hexFrom(txHashLike);
326
+ const res = await this.getTransactionNoCache(txHash);
327
+ if (res !== null) {
328
+ return res;
329
+ }
330
+
331
+ const tx = this.cachedTransactions.find((t) => t.hash() === txHash);
332
+ if (!tx) {
333
+ return null;
334
+ }
335
+
336
+ return {
337
+ transaction: tx,
338
+ status: "proposed",
339
+ };
340
+ }
132
341
  }
@@ -1,39 +1,153 @@
1
1
  import { Script } from "../ckb";
2
- import { KnownScript } from "./client";
2
+ import { CellDepInfoLike, KnownScript } from "./client";
3
3
 
4
4
  export const MAINNET_SCRIPTS: Record<
5
5
  KnownScript,
6
- Pick<Script, "codeHash" | "hashType"> | undefined
6
+ | (Pick<Script, "codeHash" | "hashType"> & { cellDeps: CellDepInfoLike[] })
7
+ | undefined
7
8
  > = {
8
9
  [KnownScript.Secp256k1Blake160]: {
9
10
  codeHash:
10
11
  "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
11
12
  hashType: "type",
13
+ cellDeps: [
14
+ {
15
+ cellDep: {
16
+ outPoint: {
17
+ txHash:
18
+ "0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
19
+ index: 0,
20
+ },
21
+ depType: "depGroup",
22
+ },
23
+ },
24
+ ],
12
25
  },
13
26
  [KnownScript.Secp256k1Multisig]: {
14
27
  codeHash:
15
28
  "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8",
16
29
  hashType: "type",
30
+ cellDeps: [
31
+ {
32
+ cellDep: {
33
+ outPoint: {
34
+ txHash:
35
+ "0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
36
+ index: 1,
37
+ },
38
+ depType: "depGroup",
39
+ },
40
+ },
41
+ ],
17
42
  },
18
43
  [KnownScript.AnyoneCanPay]: {
19
44
  codeHash:
20
45
  "0xd369597ff47f29fbc0d47d2e3775370d1250b85140c670e4718af712983a2354",
21
46
  hashType: "type",
47
+ cellDeps: [
48
+ {
49
+ cellDep: {
50
+ outPoint: {
51
+ txHash:
52
+ "0x4153a2014952d7cac45f285ce9a7c5c0c0e1b21f2d378b82ac1433cb11c25c4d",
53
+ index: 0,
54
+ },
55
+ depType: "depGroup",
56
+ },
57
+ },
58
+ ],
59
+ },
60
+ [KnownScript.TypeId]: {
61
+ codeHash:
62
+ "0x00000000000000000000000000000000000000000000000000545950455f4944",
63
+ hashType: "type",
64
+ cellDeps: [],
65
+ },
66
+ [KnownScript.XUdt]: {
67
+ codeHash:
68
+ "0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95",
69
+ hashType: "data1",
70
+ cellDeps: [
71
+ {
72
+ cellDep: {
73
+ outPoint: {
74
+ txHash:
75
+ "0xc07844ce21b38e4b071dd0e1ee3b0e27afd8d7532491327f39b786343f558ab7",
76
+ index: 0,
77
+ },
78
+ depType: "code",
79
+ },
80
+ },
81
+ ],
22
82
  },
23
83
  [KnownScript.JoyId]: {
24
84
  codeHash:
25
85
  "0xd00c84f0ec8fd441c38bc3f87a371f547190f2fcff88e642bc5bf54b9e318323",
26
86
  hashType: "type",
87
+ cellDeps: [
88
+ {
89
+ cellDep: {
90
+ outPoint: {
91
+ txHash:
92
+ "0xf05188e5f3a6767fc4687faf45ba5f1a6e25d3ada6129dae8722cb282f262493",
93
+ index: 0,
94
+ },
95
+ depType: "depGroup",
96
+ },
97
+ },
98
+ ],
27
99
  },
28
100
  [KnownScript.COTA]: {
29
101
  codeHash:
30
102
  "0x1122a4fb54697cf2e6e3a96c9d80fd398a936559b90954c6e88eb7ba0cf652df",
31
103
  hashType: "type",
104
+ cellDeps: [
105
+ {
106
+ cellDep: {
107
+ outPoint: {
108
+ txHash:
109
+ "0xabaa25237554f0d6c586dc010e7e85e6870bcfd9fb8773257ecacfbe1fd738a0",
110
+ index: 0,
111
+ },
112
+ depType: "depGroup",
113
+ },
114
+ },
115
+ ],
32
116
  },
33
117
  [KnownScript.OmniLock]: {
34
118
  codeHash:
35
119
  "0x9b819793a64463aed77c615d6cb226eea5487ccfc0783043a587254cda2b6f26",
36
120
  hashType: "type",
121
+ cellDeps: [
122
+ {
123
+ cellDep: {
124
+ outPoint: {
125
+ txHash:
126
+ "0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c",
127
+ index: 0,
128
+ },
129
+ depType: "depGroup",
130
+ },
131
+ },
132
+ {
133
+ cellDep: {
134
+ outPoint: {
135
+ txHash:
136
+ "0xc76edf469816aa22f416503c38d0b533d2a018e253e379f134c3985b3472c842",
137
+ index: 0,
138
+ },
139
+ depType: "code",
140
+ },
141
+ type: {
142
+ codeHash:
143
+ "0x00000000000000000000000000000000000000000000000000545950455f4944",
144
+ hashType: "type",
145
+ args: "0x855508fe0f0ca25b935b070452ecaee48f6c9f1d66cd15f046616b99e948236a",
146
+ },
147
+ },
148
+ ],
37
149
  },
38
150
  [KnownScript.NostrLock]: undefined,
151
+ [KnownScript.SingleUseLock]: undefined,
152
+ [KnownScript.OutputTypeProxyLock]: undefined,
39
153
  };
@@ -1,5 +1,5 @@
1
1
  import { Script } from "../ckb";
2
- import { KnownScript } from "./client";
2
+ import { CellDepInfo, KnownScript } from "./client";
3
3
  import { MAINNET_SCRIPTS } from "./clientPublicMainnet.advanced";
4
4
  import { ClientJsonRpc } from "./jsonRpc";
5
5
 
@@ -14,13 +14,18 @@ export class ClientPublicMainnet extends ClientJsonRpc {
14
14
 
15
15
  async getKnownScript(
16
16
  script: KnownScript,
17
- ): Promise<Pick<Script, "codeHash" | "hashType">> {
17
+ ): Promise<
18
+ Pick<Script, "codeHash" | "hashType"> & { cellDeps: CellDepInfo[] }
19
+ > {
18
20
  const found = MAINNET_SCRIPTS[script];
19
21
  if (!found) {
20
22
  throw new Error(
21
23
  `No script information was found for ${script} on ${this.addressPrefix}`,
22
24
  );
23
25
  }
24
- return { ...found };
26
+ return {
27
+ ...found,
28
+ cellDeps: found.cellDeps.map((c) => CellDepInfo.from(c)),
29
+ };
25
30
  }
26
31
  }