@ckb-ccc/core 1.12.4 → 1.13.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 (611) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/dist/address.advanced-CWGrVD-M.mjs +2 -0
  3. package/dist/address.advanced-CWGrVD-M.mjs.map +1 -0
  4. package/dist/advanced.d.mts +2 -0
  5. package/dist/advanced.mjs +1 -0
  6. package/dist/advancedBarrel-BD7EAPVd.d.mts +5480 -0
  7. package/dist/advancedBarrel-BD7EAPVd.d.mts.map +1 -0
  8. package/dist/advancedBarrel.d.mts +2 -0
  9. package/dist/advancedBarrel.mjs +2 -0
  10. package/dist/advancedBarrel.mjs.map +1 -0
  11. package/dist/barrel-DchA_RPN.mjs +7 -0
  12. package/dist/barrel-DchA_RPN.mjs.map +1 -0
  13. package/dist/barrel.d.mts +2 -0
  14. package/dist/barrel.mjs +1 -0
  15. package/dist/index.d.mts +2 -0
  16. package/dist/index.mjs +1 -0
  17. package/dist/rolldown-runtime-DK3Fl9T5.mjs +1 -0
  18. package/dist.commonjs/address.advanced-COp7SNhZ.js +2 -0
  19. package/dist.commonjs/address.advanced-COp7SNhZ.js.map +1 -0
  20. package/dist.commonjs/advanced.d.ts +2 -3
  21. package/dist.commonjs/advanced.js +1 -41
  22. package/dist.commonjs/advancedBarrel-DRDT4GBS.d.ts +5481 -0
  23. package/dist.commonjs/advancedBarrel-DRDT4GBS.d.ts.map +1 -0
  24. package/dist.commonjs/advancedBarrel.d.ts +2 -7
  25. package/dist.commonjs/advancedBarrel.js +2 -22
  26. package/dist.commonjs/advancedBarrel.js.map +1 -0
  27. package/dist.commonjs/barrel-CXC2Uy5L.js +7 -0
  28. package/dist.commonjs/barrel-CXC2Uy5L.js.map +1 -0
  29. package/dist.commonjs/barrel.d.ts +2 -14
  30. package/dist.commonjs/barrel.js +1 -29
  31. package/dist.commonjs/index.d.ts +2 -3
  32. package/dist.commonjs/index.js +1 -41
  33. package/package.json +20 -23
  34. package/src/barrel.ts +1 -0
  35. package/src/ckb/epoch.ts +489 -0
  36. package/src/ckb/index.ts +1 -0
  37. package/src/ckb/script.ts +4 -3
  38. package/src/ckb/transaction.ts +83 -94
  39. package/src/client/client.ts +377 -1
  40. package/src/client/clientPublicMainnet.advanced.ts +24 -1
  41. package/src/client/clientPublicTestnet.advanced.ts +23 -0
  42. package/src/client/clientTypes.ts +1 -2
  43. package/src/client/jsonRpc/transformers.ts +2 -2
  44. package/src/client/knownScript.ts +1 -0
  45. package/src/codec/codec.ts +108 -0
  46. package/src/{molecule → codec}/entity.ts +17 -7
  47. package/src/codec/predefined.ts +145 -0
  48. package/src/hasher/advanced.ts +3 -1
  49. package/src/hasher/hasherCkb.ts +1 -1
  50. package/src/hasher/hasherKeecak256.ts +1 -1
  51. package/src/keystore/index.ts +4 -4
  52. package/src/molecule/barrel.ts +10 -1
  53. package/src/molecule/codec.ts +33 -138
  54. package/src/molecule/predefined.ts +63 -50
  55. package/src/signer/btc/index.ts +1 -0
  56. package/src/signer/btc/psbt.ts +98 -0
  57. package/src/signer/btc/signerBtc.ts +41 -1
  58. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +15 -0
  59. package/src/signer/btc/verify.ts +2 -1
  60. package/src/signer/ckb/signerCkbPrivateKey.ts +6 -11
  61. package/src/signer/ckb/verifyCkbSecp256k1.ts +6 -10
  62. package/src/signer/doge/signerDogePrivateKey.ts +6 -3
  63. package/src/signer/doge/verify.ts +7 -8
  64. package/src/signer/nostr/signerNostrPrivateKey.ts +9 -8
  65. package/src/signer/nostr/verify.ts +2 -2
  66. package/src/utils/index.ts +20 -1
  67. package/tsdown.config.mts +44 -0
  68. package/dist/address/address.advanced.d.ts +0 -65
  69. package/dist/address/address.advanced.d.ts.map +0 -1
  70. package/dist/address/address.advanced.js +0 -147
  71. package/dist/address/advanced.d.ts +0 -2
  72. package/dist/address/advanced.d.ts.map +0 -1
  73. package/dist/address/advanced.js +0 -1
  74. package/dist/address/index.d.ts +0 -58
  75. package/dist/address/index.d.ts.map +0 -1
  76. package/dist/address/index.js +0 -78
  77. package/dist/advanced.d.ts +0 -3
  78. package/dist/advanced.d.ts.map +0 -1
  79. package/dist/advanced.js +0 -2
  80. package/dist/advancedBarrel.d.ts +0 -7
  81. package/dist/advancedBarrel.d.ts.map +0 -1
  82. package/dist/advancedBarrel.js +0 -6
  83. package/dist/barrel.d.ts +0 -14
  84. package/dist/barrel.d.ts.map +0 -1
  85. package/dist/barrel.js +0 -13
  86. package/dist/bytes/advanced.d.ts +0 -6
  87. package/dist/bytes/advanced.d.ts.map +0 -1
  88. package/dist/bytes/advanced.js +0 -1
  89. package/dist/bytes/index.d.ts +0 -112
  90. package/dist/bytes/index.d.ts.map +0 -1
  91. package/dist/bytes/index.js +0 -155
  92. package/dist/ckb/advanced.d.ts +0 -3
  93. package/dist/ckb/advanced.d.ts.map +0 -1
  94. package/dist/ckb/advanced.js +0 -2
  95. package/dist/ckb/hash.d.ts +0 -18
  96. package/dist/ckb/hash.d.ts.map +0 -1
  97. package/dist/ckb/hash.js +0 -19
  98. package/dist/ckb/index.d.ts +0 -6
  99. package/dist/ckb/index.d.ts.map +0 -1
  100. package/dist/ckb/index.js +0 -5
  101. package/dist/ckb/script.advanced.d.ts +0 -5
  102. package/dist/ckb/script.advanced.d.ts.map +0 -1
  103. package/dist/ckb/script.advanced.js +0 -13
  104. package/dist/ckb/script.d.ts +0 -156
  105. package/dist/ckb/script.d.ts.map +0 -1
  106. package/dist/ckb/script.js +0 -184
  107. package/dist/ckb/transaction.advanced.d.ts +0 -5
  108. package/dist/ckb/transaction.advanced.d.ts.map +0 -1
  109. package/dist/ckb/transaction.advanced.js +0 -9
  110. package/dist/ckb/transaction.d.ts +0 -1401
  111. package/dist/ckb/transaction.d.ts.map +0 -1
  112. package/dist/ckb/transaction.js +0 -1880
  113. package/dist/ckb/transactionErrors.d.ts +0 -15
  114. package/dist/ckb/transactionErrors.d.ts.map +0 -1
  115. package/dist/ckb/transactionErrors.js +0 -21
  116. package/dist/ckb/transactionLumos.d.ts +0 -30
  117. package/dist/ckb/transactionLumos.d.ts.map +0 -1
  118. package/dist/ckb/transactionLumos.js +0 -1
  119. package/dist/client/advanced.d.ts +0 -6
  120. package/dist/client/advanced.d.ts.map +0 -1
  121. package/dist/client/advanced.js +0 -5
  122. package/dist/client/cache/advanced.d.ts +0 -2
  123. package/dist/client/cache/advanced.d.ts.map +0 -1
  124. package/dist/client/cache/advanced.js +0 -1
  125. package/dist/client/cache/cache.d.ts +0 -98
  126. package/dist/client/cache/cache.d.ts.map +0 -1
  127. package/dist/client/cache/cache.js +0 -139
  128. package/dist/client/cache/index.d.ts +0 -3
  129. package/dist/client/cache/index.d.ts.map +0 -1
  130. package/dist/client/cache/index.js +0 -2
  131. package/dist/client/cache/memory.advanced.d.ts +0 -52
  132. package/dist/client/cache/memory.advanced.d.ts.map +0 -1
  133. package/dist/client/cache/memory.advanced.js +0 -149
  134. package/dist/client/cache/memory.d.ts +0 -53
  135. package/dist/client/cache/memory.d.ts.map +0 -1
  136. package/dist/client/cache/memory.js +0 -148
  137. package/dist/client/client.d.ts +0 -107
  138. package/dist/client/client.d.ts.map +0 -1
  139. package/dist/client/client.js +0 -328
  140. package/dist/client/clientPublicMainnet.advanced.d.ts +0 -4
  141. package/dist/client/clientPublicMainnet.advanced.d.ts.map +0 -1
  142. package/dist/client/clientPublicMainnet.advanced.js +0 -402
  143. package/dist/client/clientPublicMainnet.d.ts +0 -17
  144. package/dist/client/clientPublicMainnet.d.ts.map +0 -1
  145. package/dist/client/clientPublicMainnet.js +0 -40
  146. package/dist/client/clientPublicTestnet.advanced.d.ts +0 -4
  147. package/dist/client/clientPublicTestnet.advanced.d.ts.map +0 -1
  148. package/dist/client/clientPublicTestnet.advanced.js +0 -412
  149. package/dist/client/clientPublicTestnet.d.ts +0 -17
  150. package/dist/client/clientPublicTestnet.d.ts.map +0 -1
  151. package/dist/client/clientPublicTestnet.js +0 -40
  152. package/dist/client/clientTypes.advanced.d.ts +0 -25
  153. package/dist/client/clientTypes.advanced.d.ts.map +0 -1
  154. package/dist/client/clientTypes.advanced.js +0 -6
  155. package/dist/client/clientTypes.d.ts +0 -335
  156. package/dist/client/clientTypes.d.ts.map +0 -1
  157. package/dist/client/clientTypes.js +0 -232
  158. package/dist/client/index.d.ts +0 -8
  159. package/dist/client/index.d.ts.map +0 -1
  160. package/dist/client/index.js +0 -7
  161. package/dist/client/jsonRpc/advanced.d.ts +0 -3
  162. package/dist/client/jsonRpc/advanced.d.ts.map +0 -1
  163. package/dist/client/jsonRpc/advanced.js +0 -2
  164. package/dist/client/jsonRpc/client.d.ts +0 -160
  165. package/dist/client/jsonRpc/client.d.ts.map +0 -1
  166. package/dist/client/jsonRpc/client.js +0 -223
  167. package/dist/client/jsonRpc/index.d.ts +0 -2
  168. package/dist/client/jsonRpc/index.d.ts.map +0 -1
  169. package/dist/client/jsonRpc/index.js +0 -1
  170. package/dist/client/jsonRpc/transformers.d.ts +0 -51
  171. package/dist/client/jsonRpc/transformers.d.ts.map +0 -1
  172. package/dist/client/jsonRpc/transformers.js +0 -247
  173. package/dist/client/jsonRpc/types.d.ts +0 -101
  174. package/dist/client/jsonRpc/types.d.ts.map +0 -1
  175. package/dist/client/jsonRpc/types.js +0 -1
  176. package/dist/client/knownScript.d.ts +0 -27
  177. package/dist/client/knownScript.d.ts.map +0 -1
  178. package/dist/client/knownScript.js +0 -28
  179. package/dist/fixedPoint/index.d.ts +0 -55
  180. package/dist/fixedPoint/index.d.ts.map +0 -1
  181. package/dist/fixedPoint/index.js +0 -68
  182. package/dist/hasher/advanced.d.ts +0 -2
  183. package/dist/hasher/advanced.d.ts.map +0 -1
  184. package/dist/hasher/advanced.js +0 -1
  185. package/dist/hasher/hasher.d.ts +0 -35
  186. package/dist/hasher/hasher.d.ts.map +0 -1
  187. package/dist/hasher/hasher.js +0 -1
  188. package/dist/hasher/hasherCkb.d.ts +0 -57
  189. package/dist/hasher/hasherCkb.d.ts.map +0 -1
  190. package/dist/hasher/hasherCkb.js +0 -70
  191. package/dist/hasher/hasherKeecak256.d.ts +0 -41
  192. package/dist/hasher/hasherKeecak256.d.ts.map +0 -1
  193. package/dist/hasher/hasherKeecak256.js +0 -46
  194. package/dist/hasher/index.d.ts +0 -4
  195. package/dist/hasher/index.d.ts.map +0 -1
  196. package/dist/hasher/index.js +0 -3
  197. package/dist/hex/index.d.ts +0 -27
  198. package/dist/hex/index.d.ts.map +0 -1
  199. package/dist/hex/index.js +0 -17
  200. package/dist/index.d.ts +0 -3
  201. package/dist/index.d.ts.map +0 -1
  202. package/dist/index.js +0 -2
  203. package/dist/jsonRpc/advanced.d.ts +0 -2
  204. package/dist/jsonRpc/advanced.d.ts.map +0 -1
  205. package/dist/jsonRpc/advanced.js +0 -1
  206. package/dist/jsonRpc/index.d.ts +0 -2
  207. package/dist/jsonRpc/index.d.ts.map +0 -1
  208. package/dist/jsonRpc/index.js +0 -1
  209. package/dist/jsonRpc/requestor.d.ts +0 -49
  210. package/dist/jsonRpc/requestor.d.ts.map +0 -1
  211. package/dist/jsonRpc/requestor.js +0 -109
  212. package/dist/jsonRpc/transports/advanced.d.ts +0 -6
  213. package/dist/jsonRpc/transports/advanced.d.ts.map +0 -1
  214. package/dist/jsonRpc/transports/advanced.js +0 -5
  215. package/dist/jsonRpc/transports/factory.d.ts +0 -6
  216. package/dist/jsonRpc/transports/factory.d.ts.map +0 -1
  217. package/dist/jsonRpc/transports/factory.js +0 -8
  218. package/dist/jsonRpc/transports/fallback.d.ts +0 -8
  219. package/dist/jsonRpc/transports/fallback.d.ts.map +0 -1
  220. package/dist/jsonRpc/transports/fallback.js +0 -22
  221. package/dist/jsonRpc/transports/http.d.ts +0 -8
  222. package/dist/jsonRpc/transports/http.d.ts.map +0 -1
  223. package/dist/jsonRpc/transports/http.js +0 -22
  224. package/dist/jsonRpc/transports/transport.d.ts +0 -18
  225. package/dist/jsonRpc/transports/transport.d.ts.map +0 -1
  226. package/dist/jsonRpc/transports/transport.js +0 -1
  227. package/dist/jsonRpc/transports/webSocket.d.ts +0 -11
  228. package/dist/jsonRpc/transports/webSocket.d.ts.map +0 -1
  229. package/dist/jsonRpc/transports/webSocket.js +0 -87
  230. package/dist/keystore/index.d.ts +0 -31
  231. package/dist/keystore/index.d.ts.map +0 -1
  232. package/dist/keystore/index.js +0 -106
  233. package/dist/molecule/barrel.d.ts +0 -4
  234. package/dist/molecule/barrel.d.ts.map +0 -1
  235. package/dist/molecule/codec.d.ts +0 -146
  236. package/dist/molecule/codec.d.ts.map +0 -1
  237. package/dist/molecule/codec.js +0 -516
  238. package/dist/molecule/entity.d.ts +0 -112
  239. package/dist/molecule/entity.d.ts.map +0 -1
  240. package/dist/molecule/entity.js +0 -142
  241. package/dist/molecule/index.d.ts +0 -2
  242. package/dist/molecule/index.d.ts.map +0 -1
  243. package/dist/molecule/index.js +0 -1
  244. package/dist/molecule/predefined.d.ts +0 -57
  245. package/dist/molecule/predefined.d.ts.map +0 -1
  246. package/dist/molecule/predefined.js +0 -83
  247. package/dist/num/index.d.ts +0 -148
  248. package/dist/num/index.d.ts.map +0 -1
  249. package/dist/num/index.js +0 -204
  250. package/dist/signer/btc/index.d.ts +0 -4
  251. package/dist/signer/btc/index.d.ts.map +0 -1
  252. package/dist/signer/btc/index.js +0 -3
  253. package/dist/signer/btc/signerBtc.d.ts +0 -59
  254. package/dist/signer/btc/signerBtc.d.ts.map +0 -1
  255. package/dist/signer/btc/signerBtc.js +0 -83
  256. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -55
  257. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +0 -1
  258. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +0 -61
  259. package/dist/signer/btc/verify.d.ts +0 -40
  260. package/dist/signer/btc/verify.d.ts.map +0 -1
  261. package/dist/signer/btc/verify.js +0 -69
  262. package/dist/signer/ckb/index.d.ts +0 -6
  263. package/dist/signer/ckb/index.d.ts.map +0 -1
  264. package/dist/signer/ckb/index.js +0 -5
  265. package/dist/signer/ckb/signerCkbPrivateKey.d.ts +0 -16
  266. package/dist/signer/ckb/signerCkbPrivateKey.d.ts.map +0 -1
  267. package/dist/signer/ckb/signerCkbPrivateKey.js +0 -42
  268. package/dist/signer/ckb/signerCkbPublicKey.d.ts +0 -27
  269. package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +0 -1
  270. package/dist/signer/ckb/signerCkbPublicKey.js +0 -101
  271. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +0 -68
  272. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +0 -1
  273. package/dist/signer/ckb/signerCkbScriptReadonly.js +0 -78
  274. package/dist/signer/ckb/verifyCkbSecp256k1.d.ts +0 -11
  275. package/dist/signer/ckb/verifyCkbSecp256k1.d.ts.map +0 -1
  276. package/dist/signer/ckb/verifyCkbSecp256k1.js +0 -22
  277. package/dist/signer/ckb/verifyJoyId.d.ts +0 -6
  278. package/dist/signer/ckb/verifyJoyId.d.ts.map +0 -1
  279. package/dist/signer/ckb/verifyJoyId.js +0 -16
  280. package/dist/signer/doge/index.d.ts +0 -5
  281. package/dist/signer/doge/index.d.ts.map +0 -1
  282. package/dist/signer/doge/index.js +0 -4
  283. package/dist/signer/doge/signerDoge.d.ts +0 -52
  284. package/dist/signer/doge/signerDoge.d.ts.map +0 -1
  285. package/dist/signer/doge/signerDoge.js +0 -82
  286. package/dist/signer/doge/signerDogeAddressReadonly.d.ts +0 -41
  287. package/dist/signer/doge/signerDogeAddressReadonly.d.ts.map +0 -1
  288. package/dist/signer/doge/signerDogeAddressReadonly.js +0 -45
  289. package/dist/signer/doge/signerDogePrivateKey.d.ts +0 -52
  290. package/dist/signer/doge/signerDogePrivateKey.d.ts.map +0 -1
  291. package/dist/signer/doge/signerDogePrivateKey.js +0 -68
  292. package/dist/signer/doge/verify.d.ts +0 -17
  293. package/dist/signer/doge/verify.d.ts.map +0 -1
  294. package/dist/signer/doge/verify.js +0 -29
  295. package/dist/signer/dummy/alwaysError.d.ts +0 -12
  296. package/dist/signer/dummy/alwaysError.d.ts.map +0 -1
  297. package/dist/signer/dummy/alwaysError.js +0 -13
  298. package/dist/signer/dummy/dummy.d.ts +0 -15
  299. package/dist/signer/dummy/dummy.d.ts.map +0 -1
  300. package/dist/signer/dummy/dummy.js +0 -22
  301. package/dist/signer/dummy/index.d.ts +0 -4
  302. package/dist/signer/dummy/index.d.ts.map +0 -1
  303. package/dist/signer/dummy/index.js +0 -3
  304. package/dist/signer/dummy/openLink.d.ts +0 -12
  305. package/dist/signer/dummy/openLink.d.ts.map +0 -1
  306. package/dist/signer/dummy/openLink.js +0 -13
  307. package/dist/signer/evm/index.d.ts +0 -4
  308. package/dist/signer/evm/index.d.ts.map +0 -1
  309. package/dist/signer/evm/index.js +0 -3
  310. package/dist/signer/evm/signerEvm.d.ts +0 -59
  311. package/dist/signer/evm/signerEvm.d.ts.map +0 -1
  312. package/dist/signer/evm/signerEvm.js +0 -139
  313. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +0 -47
  314. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +0 -1
  315. package/dist/signer/evm/signerEvmAddressReadonly.js +0 -51
  316. package/dist/signer/evm/verify.d.ts +0 -6
  317. package/dist/signer/evm/verify.d.ts.map +0 -1
  318. package/dist/signer/evm/verify.js +0 -9
  319. package/dist/signer/index.d.ts +0 -8
  320. package/dist/signer/index.d.ts.map +0 -1
  321. package/dist/signer/index.js +0 -7
  322. package/dist/signer/nostr/index.d.ts +0 -5
  323. package/dist/signer/nostr/index.d.ts.map +0 -1
  324. package/dist/signer/nostr/index.js +0 -4
  325. package/dist/signer/nostr/signerNostr.d.ts +0 -72
  326. package/dist/signer/nostr/signerNostr.d.ts.map +0 -1
  327. package/dist/signer/nostr/signerNostr.js +0 -96
  328. package/dist/signer/nostr/signerNostrPrivateKey.d.ts +0 -14
  329. package/dist/signer/nostr/signerNostrPrivateKey.d.ts.map +0 -1
  330. package/dist/signer/nostr/signerNostrPrivateKey.js +0 -34
  331. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts +0 -15
  332. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +0 -1
  333. package/dist/signer/nostr/signerNostrPublicKeyReadonly.js +0 -26
  334. package/dist/signer/nostr/verify.d.ts +0 -9
  335. package/dist/signer/nostr/verify.d.ts.map +0 -1
  336. package/dist/signer/nostr/verify.js +0 -55
  337. package/dist/signer/signer/index.d.ts +0 -256
  338. package/dist/signer/signer/index.d.ts.map +0 -1
  339. package/dist/signer/signer/index.js +0 -305
  340. package/dist/utils/index.d.ts +0 -97
  341. package/dist/utils/index.d.ts.map +0 -1
  342. package/dist/utils/index.js +0 -55
  343. package/dist.commonjs/address/address.advanced.d.ts +0 -65
  344. package/dist.commonjs/address/address.advanced.d.ts.map +0 -1
  345. package/dist.commonjs/address/address.advanced.js +0 -152
  346. package/dist.commonjs/address/advanced.d.ts +0 -2
  347. package/dist.commonjs/address/advanced.d.ts.map +0 -1
  348. package/dist.commonjs/address/advanced.js +0 -17
  349. package/dist.commonjs/address/index.d.ts +0 -58
  350. package/dist.commonjs/address/index.d.ts.map +0 -1
  351. package/dist.commonjs/address/index.js +0 -82
  352. package/dist.commonjs/advanced.d.ts.map +0 -1
  353. package/dist.commonjs/advancedBarrel.d.ts.map +0 -1
  354. package/dist.commonjs/barrel.d.ts.map +0 -1
  355. package/dist.commonjs/bytes/advanced.d.ts +0 -6
  356. package/dist.commonjs/bytes/advanced.d.ts.map +0 -1
  357. package/dist.commonjs/bytes/advanced.js +0 -2
  358. package/dist.commonjs/bytes/index.d.ts +0 -112
  359. package/dist.commonjs/bytes/index.d.ts.map +0 -1
  360. package/dist.commonjs/bytes/index.js +0 -163
  361. package/dist.commonjs/ckb/advanced.d.ts +0 -3
  362. package/dist.commonjs/ckb/advanced.d.ts.map +0 -1
  363. package/dist.commonjs/ckb/advanced.js +0 -18
  364. package/dist.commonjs/ckb/hash.d.ts +0 -18
  365. package/dist.commonjs/ckb/hash.d.ts.map +0 -1
  366. package/dist.commonjs/ckb/hash.js +0 -22
  367. package/dist.commonjs/ckb/index.d.ts +0 -6
  368. package/dist.commonjs/ckb/index.d.ts.map +0 -1
  369. package/dist.commonjs/ckb/index.js +0 -21
  370. package/dist.commonjs/ckb/script.advanced.d.ts +0 -5
  371. package/dist.commonjs/ckb/script.advanced.d.ts.map +0 -1
  372. package/dist.commonjs/ckb/script.advanced.js +0 -16
  373. package/dist.commonjs/ckb/script.d.ts +0 -156
  374. package/dist.commonjs/ckb/script.d.ts.map +0 -1
  375. package/dist.commonjs/ckb/script.js +0 -190
  376. package/dist.commonjs/ckb/transaction.advanced.d.ts +0 -5
  377. package/dist.commonjs/ckb/transaction.advanced.d.ts.map +0 -1
  378. package/dist.commonjs/ckb/transaction.advanced.js +0 -12
  379. package/dist.commonjs/ckb/transaction.d.ts +0 -1401
  380. package/dist.commonjs/ckb/transaction.d.ts.map +0 -1
  381. package/dist.commonjs/ckb/transaction.js +0 -1894
  382. package/dist.commonjs/ckb/transactionErrors.d.ts +0 -15
  383. package/dist.commonjs/ckb/transactionErrors.d.ts.map +0 -1
  384. package/dist.commonjs/ckb/transactionErrors.js +0 -26
  385. package/dist.commonjs/ckb/transactionLumos.d.ts +0 -30
  386. package/dist.commonjs/ckb/transactionLumos.d.ts.map +0 -1
  387. package/dist.commonjs/ckb/transactionLumos.js +0 -2
  388. package/dist.commonjs/client/advanced.d.ts +0 -6
  389. package/dist.commonjs/client/advanced.d.ts.map +0 -1
  390. package/dist.commonjs/client/advanced.js +0 -21
  391. package/dist.commonjs/client/cache/advanced.d.ts +0 -2
  392. package/dist.commonjs/client/cache/advanced.d.ts.map +0 -1
  393. package/dist.commonjs/client/cache/advanced.js +0 -17
  394. package/dist.commonjs/client/cache/cache.d.ts +0 -98
  395. package/dist.commonjs/client/cache/cache.d.ts.map +0 -1
  396. package/dist.commonjs/client/cache/cache.js +0 -143
  397. package/dist.commonjs/client/cache/index.d.ts +0 -3
  398. package/dist.commonjs/client/cache/index.d.ts.map +0 -1
  399. package/dist.commonjs/client/cache/index.js +0 -18
  400. package/dist.commonjs/client/cache/memory.advanced.d.ts +0 -52
  401. package/dist.commonjs/client/cache/memory.advanced.d.ts.map +0 -1
  402. package/dist.commonjs/client/cache/memory.advanced.js +0 -158
  403. package/dist.commonjs/client/cache/memory.d.ts +0 -53
  404. package/dist.commonjs/client/cache/memory.d.ts.map +0 -1
  405. package/dist.commonjs/client/cache/memory.js +0 -152
  406. package/dist.commonjs/client/client.d.ts +0 -107
  407. package/dist.commonjs/client/client.d.ts.map +0 -1
  408. package/dist.commonjs/client/client.js +0 -332
  409. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +0 -4
  410. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +0 -1
  411. package/dist.commonjs/client/clientPublicMainnet.advanced.js +0 -405
  412. package/dist.commonjs/client/clientPublicMainnet.d.ts +0 -17
  413. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +0 -1
  414. package/dist.commonjs/client/clientPublicMainnet.js +0 -47
  415. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +0 -4
  416. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +0 -1
  417. package/dist.commonjs/client/clientPublicTestnet.advanced.js +0 -415
  418. package/dist.commonjs/client/clientPublicTestnet.d.ts +0 -17
  419. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +0 -1
  420. package/dist.commonjs/client/clientPublicTestnet.js +0 -47
  421. package/dist.commonjs/client/clientTypes.advanced.d.ts +0 -25
  422. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +0 -1
  423. package/dist.commonjs/client/clientTypes.advanced.js +0 -10
  424. package/dist.commonjs/client/clientTypes.d.ts +0 -335
  425. package/dist.commonjs/client/clientTypes.d.ts.map +0 -1
  426. package/dist.commonjs/client/clientTypes.js +0 -251
  427. package/dist.commonjs/client/index.d.ts +0 -8
  428. package/dist.commonjs/client/index.d.ts.map +0 -1
  429. package/dist.commonjs/client/index.js +0 -23
  430. package/dist.commonjs/client/jsonRpc/advanced.d.ts +0 -3
  431. package/dist.commonjs/client/jsonRpc/advanced.d.ts.map +0 -1
  432. package/dist.commonjs/client/jsonRpc/advanced.js +0 -18
  433. package/dist.commonjs/client/jsonRpc/client.d.ts +0 -160
  434. package/dist.commonjs/client/jsonRpc/client.d.ts.map +0 -1
  435. package/dist.commonjs/client/jsonRpc/client.js +0 -227
  436. package/dist.commonjs/client/jsonRpc/index.d.ts +0 -2
  437. package/dist.commonjs/client/jsonRpc/index.d.ts.map +0 -1
  438. package/dist.commonjs/client/jsonRpc/index.js +0 -17
  439. package/dist.commonjs/client/jsonRpc/transformers.d.ts +0 -51
  440. package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +0 -1
  441. package/dist.commonjs/client/jsonRpc/transformers.js +0 -251
  442. package/dist.commonjs/client/jsonRpc/types.d.ts +0 -101
  443. package/dist.commonjs/client/jsonRpc/types.d.ts.map +0 -1
  444. package/dist.commonjs/client/jsonRpc/types.js +0 -2
  445. package/dist.commonjs/client/knownScript.d.ts +0 -27
  446. package/dist.commonjs/client/knownScript.d.ts.map +0 -1
  447. package/dist.commonjs/client/knownScript.js +0 -31
  448. package/dist.commonjs/fixedPoint/index.d.ts +0 -55
  449. package/dist.commonjs/fixedPoint/index.d.ts.map +0 -1
  450. package/dist.commonjs/fixedPoint/index.js +0 -73
  451. package/dist.commonjs/hasher/advanced.d.ts +0 -2
  452. package/dist.commonjs/hasher/advanced.d.ts.map +0 -1
  453. package/dist.commonjs/hasher/advanced.js +0 -4
  454. package/dist.commonjs/hasher/hasher.d.ts +0 -35
  455. package/dist.commonjs/hasher/hasher.d.ts.map +0 -1
  456. package/dist.commonjs/hasher/hasher.js +0 -2
  457. package/dist.commonjs/hasher/hasherCkb.d.ts +0 -57
  458. package/dist.commonjs/hasher/hasherCkb.d.ts.map +0 -1
  459. package/dist.commonjs/hasher/hasherCkb.js +0 -75
  460. package/dist.commonjs/hasher/hasherKeecak256.d.ts +0 -41
  461. package/dist.commonjs/hasher/hasherKeecak256.d.ts.map +0 -1
  462. package/dist.commonjs/hasher/hasherKeecak256.js +0 -50
  463. package/dist.commonjs/hasher/index.d.ts +0 -4
  464. package/dist.commonjs/hasher/index.d.ts.map +0 -1
  465. package/dist.commonjs/hasher/index.js +0 -19
  466. package/dist.commonjs/hex/index.d.ts +0 -27
  467. package/dist.commonjs/hex/index.d.ts.map +0 -1
  468. package/dist.commonjs/hex/index.js +0 -20
  469. package/dist.commonjs/index.d.ts.map +0 -1
  470. package/dist.commonjs/jsonRpc/advanced.d.ts +0 -2
  471. package/dist.commonjs/jsonRpc/advanced.d.ts.map +0 -1
  472. package/dist.commonjs/jsonRpc/advanced.js +0 -17
  473. package/dist.commonjs/jsonRpc/index.d.ts +0 -2
  474. package/dist.commonjs/jsonRpc/index.d.ts.map +0 -1
  475. package/dist.commonjs/jsonRpc/index.js +0 -17
  476. package/dist.commonjs/jsonRpc/requestor.d.ts +0 -49
  477. package/dist.commonjs/jsonRpc/requestor.d.ts.map +0 -1
  478. package/dist.commonjs/jsonRpc/requestor.js +0 -113
  479. package/dist.commonjs/jsonRpc/transports/advanced.d.ts +0 -6
  480. package/dist.commonjs/jsonRpc/transports/advanced.d.ts.map +0 -1
  481. package/dist.commonjs/jsonRpc/transports/advanced.js +0 -21
  482. package/dist.commonjs/jsonRpc/transports/factory.d.ts +0 -6
  483. package/dist.commonjs/jsonRpc/transports/factory.d.ts.map +0 -1
  484. package/dist.commonjs/jsonRpc/transports/factory.js +0 -11
  485. package/dist.commonjs/jsonRpc/transports/fallback.d.ts +0 -8
  486. package/dist.commonjs/jsonRpc/transports/fallback.d.ts.map +0 -1
  487. package/dist.commonjs/jsonRpc/transports/fallback.js +0 -26
  488. package/dist.commonjs/jsonRpc/transports/http.d.ts +0 -8
  489. package/dist.commonjs/jsonRpc/transports/http.d.ts.map +0 -1
  490. package/dist.commonjs/jsonRpc/transports/http.js +0 -26
  491. package/dist.commonjs/jsonRpc/transports/transport.d.ts +0 -18
  492. package/dist.commonjs/jsonRpc/transports/transport.d.ts.map +0 -1
  493. package/dist.commonjs/jsonRpc/transports/transport.js +0 -2
  494. package/dist.commonjs/jsonRpc/transports/webSocket.d.ts +0 -11
  495. package/dist.commonjs/jsonRpc/transports/webSocket.d.ts.map +0 -1
  496. package/dist.commonjs/jsonRpc/transports/webSocket.js +0 -94
  497. package/dist.commonjs/keystore/index.d.ts +0 -31
  498. package/dist.commonjs/keystore/index.d.ts.map +0 -1
  499. package/dist.commonjs/keystore/index.js +0 -110
  500. package/dist.commonjs/molecule/barrel.d.ts +0 -4
  501. package/dist.commonjs/molecule/barrel.d.ts.map +0 -1
  502. package/dist.commonjs/molecule/barrel.js +0 -19
  503. package/dist.commonjs/molecule/codec.d.ts +0 -146
  504. package/dist.commonjs/molecule/codec.d.ts.map +0 -1
  505. package/dist.commonjs/molecule/codec.js +0 -531
  506. package/dist.commonjs/molecule/entity.d.ts +0 -112
  507. package/dist.commonjs/molecule/entity.d.ts.map +0 -1
  508. package/dist.commonjs/molecule/entity.js +0 -147
  509. package/dist.commonjs/molecule/index.d.ts +0 -2
  510. package/dist.commonjs/molecule/index.d.ts.map +0 -1
  511. package/dist.commonjs/molecule/index.js +0 -37
  512. package/dist.commonjs/molecule/predefined.d.ts +0 -57
  513. package/dist.commonjs/molecule/predefined.d.ts.map +0 -1
  514. package/dist.commonjs/molecule/predefined.js +0 -87
  515. package/dist.commonjs/num/index.d.ts +0 -148
  516. package/dist.commonjs/num/index.d.ts.map +0 -1
  517. package/dist.commonjs/num/index.js +0 -216
  518. package/dist.commonjs/signer/btc/index.d.ts +0 -4
  519. package/dist.commonjs/signer/btc/index.d.ts.map +0 -1
  520. package/dist.commonjs/signer/btc/index.js +0 -19
  521. package/dist.commonjs/signer/btc/signerBtc.d.ts +0 -59
  522. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +0 -1
  523. package/dist.commonjs/signer/btc/signerBtc.js +0 -87
  524. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -55
  525. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +0 -1
  526. package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +0 -65
  527. package/dist.commonjs/signer/btc/verify.d.ts +0 -40
  528. package/dist.commonjs/signer/btc/verify.d.ts.map +0 -1
  529. package/dist.commonjs/signer/btc/verify.js +0 -80
  530. package/dist.commonjs/signer/ckb/index.d.ts +0 -6
  531. package/dist.commonjs/signer/ckb/index.d.ts.map +0 -1
  532. package/dist.commonjs/signer/ckb/index.js +0 -21
  533. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.d.ts +0 -16
  534. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.d.ts.map +0 -1
  535. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.js +0 -46
  536. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts +0 -27
  537. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +0 -1
  538. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +0 -105
  539. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +0 -68
  540. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +0 -1
  541. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +0 -82
  542. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.d.ts +0 -11
  543. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.d.ts.map +0 -1
  544. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.js +0 -26
  545. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +0 -6
  546. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts.map +0 -1
  547. package/dist.commonjs/signer/ckb/verifyJoyId.js +0 -19
  548. package/dist.commonjs/signer/doge/index.d.ts +0 -5
  549. package/dist.commonjs/signer/doge/index.d.ts.map +0 -1
  550. package/dist.commonjs/signer/doge/index.js +0 -20
  551. package/dist.commonjs/signer/doge/signerDoge.d.ts +0 -52
  552. package/dist.commonjs/signer/doge/signerDoge.d.ts.map +0 -1
  553. package/dist.commonjs/signer/doge/signerDoge.js +0 -89
  554. package/dist.commonjs/signer/doge/signerDogeAddressReadonly.d.ts +0 -41
  555. package/dist.commonjs/signer/doge/signerDogeAddressReadonly.d.ts.map +0 -1
  556. package/dist.commonjs/signer/doge/signerDogeAddressReadonly.js +0 -49
  557. package/dist.commonjs/signer/doge/signerDogePrivateKey.d.ts +0 -52
  558. package/dist.commonjs/signer/doge/signerDogePrivateKey.d.ts.map +0 -1
  559. package/dist.commonjs/signer/doge/signerDogePrivateKey.js +0 -72
  560. package/dist.commonjs/signer/doge/verify.d.ts +0 -17
  561. package/dist.commonjs/signer/doge/verify.d.ts.map +0 -1
  562. package/dist.commonjs/signer/doge/verify.js +0 -33
  563. package/dist.commonjs/signer/dummy/alwaysError.d.ts +0 -12
  564. package/dist.commonjs/signer/dummy/alwaysError.d.ts.map +0 -1
  565. package/dist.commonjs/signer/dummy/alwaysError.js +0 -17
  566. package/dist.commonjs/signer/dummy/dummy.d.ts +0 -15
  567. package/dist.commonjs/signer/dummy/dummy.d.ts.map +0 -1
  568. package/dist.commonjs/signer/dummy/dummy.js +0 -26
  569. package/dist.commonjs/signer/dummy/index.d.ts +0 -4
  570. package/dist.commonjs/signer/dummy/index.d.ts.map +0 -1
  571. package/dist.commonjs/signer/dummy/index.js +0 -19
  572. package/dist.commonjs/signer/dummy/openLink.d.ts +0 -12
  573. package/dist.commonjs/signer/dummy/openLink.d.ts.map +0 -1
  574. package/dist.commonjs/signer/dummy/openLink.js +0 -17
  575. package/dist.commonjs/signer/evm/index.d.ts +0 -4
  576. package/dist.commonjs/signer/evm/index.d.ts.map +0 -1
  577. package/dist.commonjs/signer/evm/index.js +0 -19
  578. package/dist.commonjs/signer/evm/signerEvm.d.ts +0 -59
  579. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +0 -1
  580. package/dist.commonjs/signer/evm/signerEvm.js +0 -143
  581. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +0 -47
  582. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +0 -1
  583. package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +0 -55
  584. package/dist.commonjs/signer/evm/verify.d.ts +0 -6
  585. package/dist.commonjs/signer/evm/verify.d.ts.map +0 -1
  586. package/dist.commonjs/signer/evm/verify.js +0 -12
  587. package/dist.commonjs/signer/index.d.ts +0 -8
  588. package/dist.commonjs/signer/index.d.ts.map +0 -1
  589. package/dist.commonjs/signer/index.js +0 -23
  590. package/dist.commonjs/signer/nostr/index.d.ts +0 -5
  591. package/dist.commonjs/signer/nostr/index.d.ts.map +0 -1
  592. package/dist.commonjs/signer/nostr/index.js +0 -20
  593. package/dist.commonjs/signer/nostr/signerNostr.d.ts +0 -72
  594. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +0 -1
  595. package/dist.commonjs/signer/nostr/signerNostr.js +0 -100
  596. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts +0 -14
  597. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts.map +0 -1
  598. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.js +0 -38
  599. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts +0 -15
  600. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +0 -1
  601. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.js +0 -30
  602. package/dist.commonjs/signer/nostr/verify.d.ts +0 -9
  603. package/dist.commonjs/signer/nostr/verify.d.ts.map +0 -1
  604. package/dist.commonjs/signer/nostr/verify.js +0 -60
  605. package/dist.commonjs/signer/signer/index.d.ts +0 -256
  606. package/dist.commonjs/signer/signer/index.d.ts.map +0 -1
  607. package/dist.commonjs/signer/signer/index.js +0 -311
  608. package/dist.commonjs/utils/index.d.ts +0 -97
  609. package/dist.commonjs/utils/index.d.ts.map +0 -1
  610. package/dist.commonjs/utils/index.js +0 -62
  611. /package/{dist/molecule/barrel.js → src/codec/index.ts} +0 -0
@@ -1,1880 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var OutPoint_1, CellOutput_1, Since_1, CellInput_1, CellDep_1, WitnessArgs_1, Transaction_1;
8
- import { bytesFrom } from "../bytes/index.js";
9
- import { ClientBlockHeader, } from "../client/index.js";
10
- import { KnownScript } from "../client/knownScript.js";
11
- import { Zero, fixedPointFrom } from "../fixedPoint/index.js";
12
- import { HasherCkb, hashCkb } from "../hasher/index.js";
13
- import { hexFrom } from "../hex/index.js";
14
- import { mol } from "../molecule/index.js";
15
- import { numFrom, numFromBytes, numToBytes, numToHex, } from "../num/index.js";
16
- import { apply, reduceAsync } from "../utils/index.js";
17
- import { Script, ScriptOpt } from "./script.js";
18
- import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced.js";
19
- import { ErrorTransactionInsufficientCapacity, ErrorTransactionInsufficientCoin, } from "./transactionErrors.js";
20
- export const DepTypeCodec = mol.Codec.from({
21
- byteLength: 1,
22
- encode: depTypeToBytes,
23
- decode: depTypeFromBytes,
24
- });
25
- /**
26
- * Converts a DepTypeLike value to a DepType.
27
- * @public
28
- *
29
- * @param val - The value to convert, which can be a string, number, or bigint.
30
- * @returns The corresponding DepType.
31
- *
32
- * @throws Will throw an error if the input value is not a valid dep type.
33
- *
34
- * @example
35
- * ```typescript
36
- * const depType = depTypeFrom(1); // Outputs "code"
37
- * const depType = depTypeFrom("depGroup"); // Outputs "depGroup"
38
- * ```
39
- */
40
- export function depTypeFrom(val) {
41
- const depType = (() => {
42
- if (typeof val === "number") {
43
- return NUM_TO_DEP_TYPE[val];
44
- }
45
- if (typeof val === "bigint") {
46
- return NUM_TO_DEP_TYPE[Number(val)];
47
- }
48
- return val;
49
- })();
50
- if (depType === undefined) {
51
- throw new Error(`Invalid dep type ${val}`);
52
- }
53
- return depType;
54
- }
55
- /**
56
- * Converts a DepTypeLike value to its corresponding byte representation.
57
- * @public
58
- *
59
- * @param depType - The dep type value to convert.
60
- * @returns A Uint8Array containing the byte representation of the dep type.
61
- *
62
- * @example
63
- * ```typescript
64
- * const depTypeBytes = depTypeToBytes("code"); // Outputs Uint8Array [1]
65
- * ```
66
- */
67
- export function depTypeToBytes(depType) {
68
- return bytesFrom([DEP_TYPE_TO_NUM[depTypeFrom(depType)]]);
69
- }
70
- /**
71
- * Converts a byte-like value to a DepType.
72
- * @public
73
- *
74
- * @param bytes - The byte-like value to convert.
75
- * @returns The corresponding DepType.
76
- *
77
- * @throws Will throw an error if the input bytes do not correspond to a valid dep type.
78
- *
79
- * @example
80
- * ```typescript
81
- * const depType = depTypeFromBytes(new Uint8Array([1])); // Outputs "code"
82
- * ```
83
- */
84
- export function depTypeFromBytes(bytes) {
85
- return NUM_TO_DEP_TYPE[bytesFrom(bytes)[0]];
86
- }
87
- /**
88
- * @public
89
- */
90
- let OutPoint = OutPoint_1 = class OutPoint extends mol.Entity.Base() {
91
- /**
92
- * Creates an instance of OutPoint.
93
- *
94
- * @param txHash - The transaction hash.
95
- * @param index - The index of the output in the transaction.
96
- */
97
- constructor(txHash, index) {
98
- super();
99
- this.txHash = txHash;
100
- this.index = index;
101
- }
102
- /**
103
- * Creates an OutPoint instance from an OutPointLike object.
104
- *
105
- * @param outPoint - An OutPointLike object or an instance of OutPoint.
106
- * @returns An OutPoint instance.
107
- *
108
- * @example
109
- * ```typescript
110
- * const outPoint = OutPoint.from({ txHash: "0x...", index: 0 });
111
- * ```
112
- */
113
- static from(outPoint) {
114
- if (outPoint instanceof OutPoint_1) {
115
- return outPoint;
116
- }
117
- return new OutPoint_1(hexFrom(outPoint.txHash), numFrom(outPoint.index));
118
- }
119
- /**
120
- * Clone a OutPoint.
121
- *
122
- * @returns A cloned OutPoint instance.
123
- *
124
- * @example
125
- * ```typescript
126
- * const outPoint1 = outPoint0.clone();
127
- * ```
128
- */
129
- clone() {
130
- return new OutPoint_1(this.txHash, this.index);
131
- }
132
- /**
133
- * Check if the OutPoint is equal to another OutPoint.
134
- * @public
135
- * @param other - The other OutPoint to compare with
136
- * @returns True if the OutPoints are equal, false otherwise
137
- *
138
- * @example
139
- * ```typescript
140
- * const isEqual = outPoint0.eq(outPoint1);
141
- * ```
142
- */
143
- eq(other) {
144
- other = OutPoint_1.from(other);
145
- return this.txHash === other.txHash && this.index === other.index;
146
- }
147
- };
148
- OutPoint = OutPoint_1 = __decorate([
149
- mol.codec(mol.struct({
150
- txHash: mol.Byte32,
151
- index: mol.Uint32,
152
- }))
153
- ], OutPoint);
154
- export { OutPoint };
155
- /**
156
- * @public
157
- */
158
- let CellOutput = CellOutput_1 = class CellOutput extends mol.Entity.Base() {
159
- /**
160
- * Creates an instance of CellOutput.
161
- *
162
- * @param capacity - The capacity of the cell.
163
- * @param lock - The lock script of the cell.
164
- * @param type - The optional type script of the cell.
165
- */
166
- constructor(capacity, lock, type) {
167
- super();
168
- this.capacity = capacity;
169
- this.lock = lock;
170
- this.type = type;
171
- }
172
- get occupiedSize() {
173
- return 8 + this.lock.occupiedSize + (this.type?.occupiedSize ?? 0);
174
- }
175
- /**
176
- * Creates a CellOutput instance from a CellOutputLike object.
177
- * This method supports automatic capacity calculation when outputData is provided and capacity is 0 or omitted.
178
- *
179
- * @param cellOutput - A CellOutputLike object or an instance of CellOutput.
180
- * @param outputData - Optional output data used for automatic capacity calculation.
181
- * When provided and capacity is 0, the capacity will be calculated
182
- * as occupiedSize + outputData.length.
183
- * @returns A CellOutput instance.
184
- *
185
- * @example
186
- * ```typescript
187
- * // Basic usage with explicit capacity
188
- * const cellOutput1 = CellOutput.from({
189
- * capacity: 1000n,
190
- * lock: { codeHash: "0x...", hashType: "type", args: "0x..." },
191
- * type: { codeHash: "0x...", hashType: "type", args: "0x..." }
192
- * });
193
- *
194
- * // Automatic capacity calculation
195
- * const cellOutput2 = CellOutput.from({
196
- * lock: { codeHash: "0x...", hashType: "type", args: "0x..." }
197
- * }, "0x1234"); // Capacity will be calculated automatically
198
- * ```
199
- */
200
- static from(cellOutput, outputData) {
201
- const output = (() => {
202
- if (cellOutput instanceof CellOutput_1) {
203
- return cellOutput;
204
- }
205
- return new CellOutput_1(numFrom(cellOutput.capacity ?? 0), Script.from(cellOutput.lock), apply(Script.from, cellOutput.type));
206
- })();
207
- if (output.capacity === Zero && outputData != null) {
208
- output.capacity = fixedPointFrom(output.occupiedSize + bytesFrom(outputData).length);
209
- }
210
- return output;
211
- }
212
- /**
213
- * Clone a CellOutput.
214
- *
215
- * @returns A cloned CellOutput instance.
216
- *
217
- * @example
218
- * ```typescript
219
- * const cellOutput1 = cellOutput0.clone();
220
- * ```
221
- */
222
- clone() {
223
- return new CellOutput_1(this.capacity, this.lock.clone(), this.type?.clone());
224
- }
225
- };
226
- CellOutput = CellOutput_1 = __decorate([
227
- mol.codec(mol.table({
228
- capacity: mol.Uint64,
229
- lock: Script,
230
- type: ScriptOpt,
231
- }))
232
- ], CellOutput);
233
- export { CellOutput };
234
- export const CellOutputVec = mol.vector(CellOutput);
235
- /**
236
- * Represents a CKB cell that can be either on-chain (with an `outPoint`) or off-chain (without an `outPoint`).
237
- * This class provides a unified interface for handling cells before they are included in a transaction,
238
- * or for cells that are already part of the blockchain state.
239
- *
240
- * @public
241
- */
242
- export class CellAny {
243
- /**
244
- * Creates an instance of CellAny.
245
- *
246
- * @param cellOutput - The cell output of the cell.
247
- * @param outputData - The output data of the cell.
248
- * @param outPoint - The optional output point of the cell. If provided, the cell is considered on-chain.
249
- */
250
- constructor(cellOutput, outputData, outPoint) {
251
- this.cellOutput = cellOutput;
252
- this.outputData = outputData;
253
- this.outPoint = outPoint;
254
- }
255
- /**
256
- * Creates a `CellAny` instance from a `CellAnyLike` object.
257
- * This factory method provides a convenient way to create `CellAny` instances
258
- * from plain objects, automatically handling the optional `outPoint` or `previousOutput`.
259
- *
260
- * @param cell - A `CellAnyLike` object.
261
- * @returns A new `CellAny` instance.
262
- *
263
- * @example
264
- * ```typescript
265
- * // Create an off-chain cell (e.g., a new output)
266
- * const offChainCell = CellAny.from({
267
- * cellOutput: { capacity: 1000n, lock: lockScript },
268
- * outputData: "0x"
269
- * });
270
- *
271
- * // Create an on-chain cell from an input
272
- * const onChainCell = CellAny.from({
273
- * outPoint: { txHash: "0x...", index: 0 },
274
- * cellOutput: { capacity: 2000n, lock: lockScript },
275
- * outputData: "0x1234"
276
- * });
277
- * ```
278
- */
279
- static from(cell) {
280
- if (cell instanceof CellAny) {
281
- return cell;
282
- }
283
- const outputData = hexFrom(cell.outputData ?? "0x");
284
- return new CellAny(CellOutput.from(cell.cellOutput, outputData), outputData, apply(OutPoint.from, cell.outPoint ?? cell.previousOutput));
285
- }
286
- /**
287
- * Calculates the total occupied size of the cell in bytes.
288
- * This includes the size of the `CellOutput` structure plus the size of the `outputData`.
289
- *
290
- * @returns The total occupied size in bytes.
291
- */
292
- get occupiedSize() {
293
- return this.cellOutput.occupiedSize + bytesFrom(this.outputData).byteLength;
294
- }
295
- /**
296
- * Calculates the free capacity of the cell.
297
- * Free capacity is the total capacity minus the capacity occupied by the cell's structure and data.
298
- *
299
- * @returns The free capacity in shannons as a `Num`.
300
- */
301
- get capacityFree() {
302
- return this.cellOutput.capacity - fixedPointFrom(this.occupiedSize);
303
- }
304
- /**
305
- * Checks if the cell is a Nervos DAO cell and optionally checks its phase.
306
- *
307
- * @param client - A CKB client instance to fetch known script information.
308
- * @param phase - Optional phase to check: "deposited" or "withdrew".
309
- * If omitted, it checks if the cell is a DAO cell regardless of phase.
310
- * @returns A promise that resolves to `true` if the cell is a matching Nervos DAO cell, `false` otherwise.
311
- */
312
- async isNervosDao(client, phase) {
313
- const { type } = this.cellOutput;
314
- const daoType = await client.getKnownScript(KnownScript.NervosDao);
315
- if (!type ||
316
- type.codeHash !== daoType.codeHash ||
317
- type.hashType !== daoType.hashType) {
318
- // Non Nervos DAO cell
319
- return false;
320
- }
321
- const hasWithdrew = numFrom(this.outputData) !== Zero;
322
- return (!phase ||
323
- (phase === "deposited" && !hasWithdrew) ||
324
- (phase === "withdrew" && hasWithdrew));
325
- }
326
- /**
327
- * Clones the `CellAny` instance.
328
- *
329
- * @returns A new `CellAny` instance that is a deep copy of the current one.
330
- *
331
- * @example
332
- * ```typescript
333
- * const clonedCell = cellAny.clone();
334
- * ```
335
- */
336
- clone() {
337
- return new CellAny(this.cellOutput.clone(), this.outputData, this.outPoint?.clone());
338
- }
339
- }
340
- /**
341
- * Represents an on-chain CKB cell, which is a `CellAny` that is guaranteed to have an `outPoint`.
342
- * This class is typically used for cells that are already part of the blockchain state, such as transaction inputs.
343
- * @public
344
- */
345
- export class Cell extends CellAny {
346
- /**
347
- * Creates an instance of an on-chain Cell.
348
- *
349
- * @param outPoint - The output point of the cell.
350
- * @param cellOutput - The cell output of the cell.
351
- * @param outputData - The output data of the cell.
352
- */
353
- constructor(outPoint, cellOutput, outputData) {
354
- super(cellOutput, outputData, outPoint);
355
- this.outPoint = outPoint;
356
- }
357
- /**
358
- * Creates a Cell instance from a CellLike object.
359
- * This method accepts either `outPoint` or `previousOutput` to specify the cell's location,
360
- * and supports automatic capacity calculation for the cell output.
361
- *
362
- * @param cell - A CellLike object or an instance of Cell. The object can use either:
363
- * - `outPoint`: For referencing a cell output
364
- * - `previousOutput`: For referencing a cell input (alternative name for outPoint)
365
- * The cellOutput can omit capacity for automatic calculation.
366
- * @returns A Cell instance.
367
- *
368
- * @example
369
- * ```typescript
370
- * // Using outPoint with explicit capacity
371
- * const cell1 = Cell.from({
372
- * outPoint: { txHash: "0x...", index: 0 },
373
- * cellOutput: {
374
- * capacity: 1000n,
375
- * lock: { codeHash: "0x...", hashType: "type", args: "0x..." }
376
- * },
377
- * outputData: "0x"
378
- * });
379
- *
380
- * // Using previousOutput with automatic capacity calculation
381
- * const cell2 = Cell.from({
382
- * previousOutput: { txHash: "0x...", index: 0 },
383
- * cellOutput: {
384
- * lock: { codeHash: "0x...", hashType: "type", args: "0x..." }
385
- * // capacity will be calculated automatically
386
- * },
387
- * outputData: "0x1234"
388
- * });
389
- * ```
390
- */
391
- static from(cell) {
392
- if (cell instanceof Cell) {
393
- return cell;
394
- }
395
- return new Cell(OutPoint.from(cell.outPoint ?? cell.previousOutput), CellOutput.from(cell.cellOutput, cell.outputData), hexFrom(cell.outputData ?? "0x"));
396
- }
397
- /**
398
- * Gets confirmed Nervos DAO profit of a Cell
399
- * It returns non-zero value only when the cell is in withdrawal phase 2
400
- * See https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md
401
- *
402
- * @param client - A client for searching DAO related headers
403
- * @returns Profit
404
- *
405
- * @example
406
- * ```typescript
407
- * const profit = await cell.getDaoProfit(client);
408
- * ```
409
- */
410
- async getDaoProfit(client) {
411
- if (!(await this.isNervosDao(client, "withdrew"))) {
412
- return Zero;
413
- }
414
- const { depositHeader, withdrawHeader } = await this.getNervosDaoInfo(client);
415
- if (!withdrawHeader || !depositHeader) {
416
- throw new Error(`Unable to get headers of a Nervos DAO cell ${this.outPoint.txHash}:${this.outPoint.index.toString()}`);
417
- }
418
- return calcDaoProfit(this.capacityFree, depositHeader, withdrawHeader);
419
- }
420
- /**
421
- * Retrieves detailed information about a Nervos DAO cell, including its deposit and withdrawal headers.
422
- *
423
- * @param client - A CKB client instance to fetch cell and header data.
424
- * @returns A promise that resolves to an object containing header information.
425
- * - If not a DAO cell, returns `{}`.
426
- * - If a deposited DAO cell, returns `{ depositHeader }`.
427
- * - If a withdrawn DAO cell, returns `{ depositHeader, withdrawHeader }`.
428
- *
429
- * @throws If the cell is a DAO cell but its corresponding headers cannot be fetched.
430
- *
431
- * @example
432
- * ```typescript
433
- * const daoInfo = await cell.getNervosDaoInfo(client);
434
- * ```
435
- */
436
- async getNervosDaoInfo(client) {
437
- if (!(await this.isNervosDao(client))) {
438
- // Non Nervos DAO cell
439
- return {};
440
- }
441
- if (numFrom(this.outputData) === Zero) {
442
- // Deposited Nervos DAO cell
443
- const depositRes = await client.getCellWithHeader(this.outPoint);
444
- if (!depositRes?.header) {
445
- throw new Error(`Unable to get header of a Nervos DAO deposited cell ${this.outPoint.txHash}:${this.outPoint.index.toString()}`);
446
- }
447
- return {
448
- depositHeader: depositRes.header,
449
- };
450
- }
451
- // Withdrew Nervos DAO cell
452
- const [depositHeader, withdrawRes] = await Promise.all([
453
- client.getHeaderByNumber(numFromBytes(this.outputData)),
454
- client.getCellWithHeader(this.outPoint),
455
- ]);
456
- if (!withdrawRes?.header || !depositHeader) {
457
- throw new Error(`Unable to get headers of a Nervos DAO withdrew cell ${this.outPoint.txHash}:${this.outPoint.index.toString()}`);
458
- }
459
- return {
460
- depositHeader,
461
- withdrawHeader: withdrawRes.header,
462
- };
463
- }
464
- /**
465
- * Clone a Cell
466
- *
467
- * @returns A cloned Cell instance.
468
- *
469
- * @example
470
- * ```typescript
471
- * const cell1 = cell0.clone();
472
- * ```
473
- */
474
- clone() {
475
- return new Cell(this.outPoint.clone(), this.cellOutput.clone(), this.outputData);
476
- }
477
- }
478
- /**
479
- * @public
480
- */
481
- export function epochFrom(epochLike) {
482
- return [numFrom(epochLike[0]), numFrom(epochLike[1]), numFrom(epochLike[2])];
483
- }
484
- /**
485
- * @public
486
- */
487
- export function epochFromHex(hex) {
488
- const num = numFrom(hexFrom(hex));
489
- return [
490
- num & numFrom("0xffffff"),
491
- (num >> numFrom(24)) & numFrom("0xffff"),
492
- (num >> numFrom(40)) & numFrom("0xffff"),
493
- ];
494
- }
495
- /**
496
- * @public
497
- */
498
- export function epochToHex(epochLike) {
499
- const epoch = epochFrom(epochLike);
500
- return numToHex(numFrom(epoch[0]) +
501
- (numFrom(epoch[1]) << numFrom(24)) +
502
- (numFrom(epoch[2]) << numFrom(40)));
503
- }
504
- /**
505
- * @public
506
- */
507
- let Since = Since_1 = class Since extends mol.Entity.Base() {
508
- /**
509
- * Creates an instance of Since.
510
- *
511
- * @param relative - Absolute or relative
512
- * @param metric - The metric of since
513
- * @param value - The value of since
514
- */
515
- constructor(relative, metric, value) {
516
- super();
517
- this.relative = relative;
518
- this.metric = metric;
519
- this.value = value;
520
- }
521
- /**
522
- * Clone a Since.
523
- *
524
- * @returns A cloned Since instance.
525
- *
526
- * @example
527
- * ```typescript
528
- * const since1 = since0.clone();
529
- * ```
530
- */
531
- clone() {
532
- return new Since_1(this.relative, this.metric, this.value);
533
- }
534
- /**
535
- * Creates a Since instance from a SinceLike object.
536
- *
537
- * @param since - A SinceLike object or an instance of Since.
538
- * @returns A Since instance.
539
- *
540
- * @example
541
- * ```typescript
542
- * const since = Since.from("0x1234567812345678");
543
- * ```
544
- */
545
- static from(since) {
546
- if (since instanceof Since_1) {
547
- return since;
548
- }
549
- if (typeof since === "object" && "relative" in since) {
550
- return new Since_1(since.relative, since.metric, numFrom(since.value));
551
- }
552
- return Since_1.fromNum(since);
553
- }
554
- /**
555
- * Converts the Since instance to num.
556
- *
557
- * @returns A num
558
- *
559
- * @example
560
- * ```typescript
561
- * const num = since.toNum();
562
- * ```
563
- */
564
- toNum() {
565
- return (this.value |
566
- (this.relative === "absolute" ? Zero : numFrom("0x8000000000000000")) |
567
- {
568
- blockNumber: numFrom("0x0000000000000000"),
569
- epoch: numFrom("0x2000000000000000"),
570
- timestamp: numFrom("0x4000000000000000"),
571
- }[this.metric]);
572
- }
573
- /**
574
- * Creates a Since instance from a num-like value.
575
- *
576
- * @param numLike - The num-like value to convert.
577
- * @returns A Since instance.
578
- *
579
- * @example
580
- * ```typescript
581
- * const since = Since.fromNum("0x0");
582
- * ```
583
- */
584
- static fromNum(numLike) {
585
- const num = numFrom(numLike);
586
- const relative = num >> numFrom(63) === Zero ? "absolute" : "relative";
587
- const metric = ["blockNumber", "epoch", "timestamp"][Number((num >> numFrom(61)) & numFrom(3))];
588
- const value = num & numFrom("0x00ffffffffffffff");
589
- return new Since_1(relative, metric, value);
590
- }
591
- };
592
- Since = Since_1 = __decorate([
593
- mol.codec(mol.Uint64.mapIn((encodable) => Since.from(encodable).toNum()))
594
- ], Since);
595
- export { Since };
596
- /**
597
- * @public
598
- */
599
- let CellInput = CellInput_1 = class CellInput extends mol.Entity.Base() {
600
- /**
601
- * Creates an instance of CellInput.
602
- *
603
- * @param previousOutput - The previous outpoint of the cell.
604
- * @param since - The since value of the cell input.
605
- * @param cellOutput - The optional cell output associated with the cell input.
606
- * @param outputData - The optional output data associated with the cell input.
607
- */
608
- constructor(previousOutput, since, cellOutput, outputData) {
609
- super();
610
- this.previousOutput = previousOutput;
611
- this.since = since;
612
- this.cellOutput = cellOutput;
613
- this.outputData = outputData;
614
- }
615
- /**
616
- * Creates a CellInput instance from a CellInputLike object.
617
- *
618
- * @param cellInput - A CellInputLike object or an instance of CellInput.
619
- * @returns A CellInput instance.
620
- *
621
- * @example
622
- * ```typescript
623
- * const cellInput = CellInput.from({
624
- * previousOutput: { txHash: "0x...", index: 0 },
625
- * since: 0n
626
- * });
627
- * ```
628
- */
629
- static from(cellInput) {
630
- if (cellInput instanceof CellInput_1) {
631
- return cellInput;
632
- }
633
- return new CellInput_1(OutPoint.from("previousOutput" in cellInput
634
- ? cellInput.previousOutput
635
- : cellInput.outPoint), Since.from(cellInput.since ?? 0).toNum(), apply(CellOutput.from, cellInput.cellOutput), apply(hexFrom, cellInput.outputData));
636
- }
637
- async getCell(client) {
638
- await this.completeExtraInfos(client);
639
- if (!this.cellOutput || !this.outputData) {
640
- throw new Error("Unable to complete input");
641
- }
642
- return Cell.from({
643
- outPoint: this.previousOutput,
644
- cellOutput: this.cellOutput,
645
- outputData: this.outputData,
646
- });
647
- }
648
- /**
649
- * Complete extra infos in the input. Including
650
- * - Previous cell output
651
- * - Previous cell data
652
- * The instance will be modified.
653
- *
654
- * @returns true if succeed.
655
- * @example
656
- * ```typescript
657
- * await cellInput.completeExtraInfos(client);
658
- * ```
659
- */
660
- async completeExtraInfos(client) {
661
- if (this.cellOutput && this.outputData) {
662
- return;
663
- }
664
- const cell = await client.getCell(this.previousOutput);
665
- if (!cell) {
666
- return;
667
- }
668
- this.cellOutput = cell.cellOutput;
669
- this.outputData = cell.outputData;
670
- }
671
- /**
672
- * The extra capacity created when consume this input.
673
- * This is usually NervosDAO interest, see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md.
674
- * And it can also be miners' income. (But this is not implemented yet)
675
- */
676
- async getExtraCapacity(client) {
677
- return (await this.getCell(client)).getDaoProfit(client);
678
- }
679
- /**
680
- * Clone a CellInput.
681
- *
682
- * @returns A cloned CellInput instance.
683
- *
684
- * @example
685
- * ```typescript
686
- * const cellInput1 = cellInput0.clone();
687
- * ```
688
- */
689
- clone() {
690
- return new CellInput_1(this.previousOutput.clone(), this.since, this.cellOutput?.clone(), this.outputData);
691
- }
692
- };
693
- CellInput = CellInput_1 = __decorate([
694
- mol.codec(mol
695
- .struct({
696
- since: Since,
697
- previousOutput: OutPoint,
698
- })
699
- .mapIn((encodable) => CellInput.from(encodable)))
700
- ], CellInput);
701
- export { CellInput };
702
- export const CellInputVec = mol.vector(CellInput);
703
- /**
704
- * @public
705
- */
706
- let CellDep = CellDep_1 = class CellDep extends mol.Entity.Base() {
707
- /**
708
- * Creates an instance of CellDep.
709
- *
710
- * @param outPoint - The outpoint of the cell dependency.
711
- * @param depType - The dependency type.
712
- */
713
- constructor(outPoint, depType) {
714
- super();
715
- this.outPoint = outPoint;
716
- this.depType = depType;
717
- }
718
- /**
719
- * Creates a CellDep instance from a CellDepLike object.
720
- *
721
- * @param cellDep - A CellDepLike object or an instance of CellDep.
722
- * @returns A CellDep instance.
723
- *
724
- * @example
725
- * ```typescript
726
- * const cellDep = CellDep.from({
727
- * outPoint: { txHash: "0x...", index: 0 },
728
- * depType: "depGroup"
729
- * });
730
- * ```
731
- */
732
- static from(cellDep) {
733
- if (cellDep instanceof CellDep_1) {
734
- return cellDep;
735
- }
736
- return new CellDep_1(OutPoint.from(cellDep.outPoint), depTypeFrom(cellDep.depType));
737
- }
738
- /**
739
- * Clone a CellDep.
740
- *
741
- * @returns A cloned CellDep instance.
742
- *
743
- * @example
744
- * ```typescript
745
- * const cellDep1 = cellDep0.clone();
746
- * ```
747
- */
748
- clone() {
749
- return new CellDep_1(this.outPoint.clone(), this.depType);
750
- }
751
- };
752
- CellDep = CellDep_1 = __decorate([
753
- mol.codec(mol.struct({
754
- outPoint: OutPoint,
755
- depType: DepTypeCodec,
756
- }))
757
- ], CellDep);
758
- export { CellDep };
759
- export const CellDepVec = mol.vector(CellDep);
760
- /**
761
- * @public
762
- */
763
- let WitnessArgs = WitnessArgs_1 = class WitnessArgs extends mol.Entity.Base() {
764
- /**
765
- * Creates an instance of WitnessArgs.
766
- *
767
- * @param lock - The optional lock field of the witness.
768
- * @param inputType - The optional input type field of the witness.
769
- * @param outputType - The optional output type field of the witness.
770
- */
771
- constructor(lock, inputType, outputType) {
772
- super();
773
- this.lock = lock;
774
- this.inputType = inputType;
775
- this.outputType = outputType;
776
- }
777
- /**
778
- * Creates a WitnessArgs instance from a WitnessArgsLike object.
779
- *
780
- * @param witnessArgs - A WitnessArgsLike object or an instance of WitnessArgs.
781
- * @returns A WitnessArgs instance.
782
- *
783
- * @example
784
- * ```typescript
785
- * const witnessArgs = WitnessArgs.from({
786
- * lock: "0x...",
787
- * inputType: "0x...",
788
- * outputType: "0x..."
789
- * });
790
- * ```
791
- */
792
- static from(witnessArgs) {
793
- if (witnessArgs instanceof WitnessArgs_1) {
794
- return witnessArgs;
795
- }
796
- return new WitnessArgs_1(apply(hexFrom, witnessArgs.lock), apply(hexFrom, witnessArgs.inputType), apply(hexFrom, witnessArgs.outputType));
797
- }
798
- };
799
- WitnessArgs = WitnessArgs_1 = __decorate([
800
- mol.codec(mol.table({
801
- lock: mol.BytesOpt,
802
- inputType: mol.BytesOpt,
803
- outputType: mol.BytesOpt,
804
- }))
805
- ], WitnessArgs);
806
- export { WitnessArgs };
807
- /**
808
- * Convert a bytes to a num.
809
- *
810
- * @public
811
- */
812
- export function udtBalanceFrom(dataLike) {
813
- const data = bytesFrom(dataLike).slice(0, 16);
814
- return data.length === 0 ? Zero : numFromBytes(data);
815
- }
816
- export const RawTransaction = mol.table({
817
- version: mol.Uint32,
818
- cellDeps: CellDepVec,
819
- headerDeps: mol.Byte32Vec,
820
- inputs: CellInputVec,
821
- outputs: CellOutputVec,
822
- outputsData: mol.BytesVec,
823
- });
824
- /**
825
- * @public
826
- */
827
- let Transaction = Transaction_1 = class Transaction extends mol.Entity.Base() {
828
- /**
829
- * Creates an instance of Transaction.
830
- *
831
- * @param version - The version of the transaction.
832
- * @param cellDeps - The cell dependencies of the transaction.
833
- * @param headerDeps - The header dependencies of the transaction.
834
- * @param inputs - The inputs of the transaction.
835
- * @param outputs - The outputs of the transaction.
836
- * @param outputsData - The data associated with the outputs.
837
- * @param witnesses - The witnesses of the transaction.
838
- */
839
- constructor(version, cellDeps, headerDeps, inputs, outputs, outputsData, witnesses) {
840
- super();
841
- this.version = version;
842
- this.cellDeps = cellDeps;
843
- this.headerDeps = headerDeps;
844
- this.inputs = inputs;
845
- this.outputs = outputs;
846
- this.outputsData = outputsData;
847
- this.witnesses = witnesses;
848
- }
849
- /**
850
- * Creates a default Transaction instance with empty fields.
851
- *
852
- * @returns A default Transaction instance.
853
- *
854
- * @example
855
- * ```typescript
856
- * const defaultTx = Transaction.default();
857
- * ```
858
- */
859
- static default() {
860
- return new Transaction_1(0n, [], [], [], [], [], []);
861
- }
862
- /**
863
- * Copy every properties from another transaction.
864
- * This method replaces all properties of the current transaction with those from the provided transaction.
865
- *
866
- * @param txLike - The transaction-like object to copy properties from.
867
- *
868
- * @example
869
- * ```typescript
870
- * this.copy(Transaction.default());
871
- * ```
872
- */
873
- copy(txLike) {
874
- const tx = Transaction_1.from(txLike);
875
- this.version = tx.version;
876
- this.cellDeps = tx.cellDeps;
877
- this.headerDeps = tx.headerDeps;
878
- this.inputs = tx.inputs;
879
- this.outputs = tx.outputs;
880
- this.outputsData = tx.outputsData;
881
- this.witnesses = tx.witnesses;
882
- }
883
- /**
884
- * Creates a deep copy of the transaction.
885
- * This method creates a new Transaction instance with all nested objects cloned,
886
- * ensuring that modifications to the cloned transaction do not affect the original.
887
- *
888
- * @returns A new Transaction instance that is a deep copy of the current transaction.
889
- *
890
- * @example
891
- * ```typescript
892
- * const originalTx = Transaction.from({
893
- * version: 0,
894
- * inputs: [{ previousOutput: { txHash: "0x...", index: 0 } }],
895
- * outputs: [{ capacity: 1000n, lock: lockScript }],
896
- * outputsData: ["0x"],
897
- * witnesses: ["0x"]
898
- * });
899
- *
900
- * const clonedTx = originalTx.clone();
901
- *
902
- * // Modifications to clonedTx won't affect originalTx
903
- * clonedTx.addOutput({ capacity: 2000n, lock: anotherLockScript });
904
- * console.log(originalTx.outputs.length); // Still 1
905
- * console.log(clonedTx.outputs.length); // Now 2
906
- * ```
907
- *
908
- * @remarks
909
- * The clone operation performs deep copying for:
910
- * - Cell dependencies (cellDeps) - each CellDep is cloned
911
- * - Inputs - each CellInput is cloned
912
- * - Outputs - each CellOutput is cloned
913
- *
914
- * The following are shallow copied (references to immutable data):
915
- * - Header dependencies (headerDeps) - Hex strings are immutable
916
- * - Output data (outputsData) - Hex strings are immutable
917
- * - Witnesses - Hex strings are immutable
918
- * - Version - bigint is immutable
919
- */
920
- clone() {
921
- return new Transaction_1(this.version, this.cellDeps.map((c) => c.clone()), this.headerDeps.map((h) => h), this.inputs.map((i) => i.clone()), this.outputs.map((o) => o.clone()), this.outputsData.map((o) => o), this.witnesses.map((w) => w));
922
- }
923
- /**
924
- * Creates a Transaction instance from a TransactionLike object.
925
- *
926
- * @param tx - A TransactionLike object or an instance of Transaction.
927
- * @returns A Transaction instance.
928
- *
929
- * @example
930
- * ```typescript
931
- * const transaction = Transaction.from({
932
- * version: 0,
933
- * cellDeps: [],
934
- * headerDeps: [],
935
- * inputs: [],
936
- * outputs: [],
937
- * outputsData: [],
938
- * witnesses: []
939
- * });
940
- * ```
941
- */
942
- static from(tx) {
943
- if (tx instanceof Transaction_1) {
944
- return tx;
945
- }
946
- const outputs = tx.outputs?.map((output, i) => CellOutput.from(output, tx.outputsData?.[i] ?? [])) ?? [];
947
- const outputsData = outputs.map((_, i) => hexFrom(tx.outputsData?.[i] ?? "0x"));
948
- if (tx.outputsData != null && outputsData.length < tx.outputsData.length) {
949
- outputsData.push(...tx.outputsData.slice(outputsData.length).map((d) => hexFrom(d)));
950
- }
951
- return new Transaction_1(numFrom(tx.version ?? 0), tx.cellDeps?.map((cellDep) => CellDep.from(cellDep)) ?? [], tx.headerDeps?.map(hexFrom) ?? [], tx.inputs?.map((input) => CellInput.from(input)) ?? [], outputs, outputsData, tx.witnesses?.map(hexFrom) ?? []);
952
- }
953
- /**
954
- * Creates a Transaction instance from a Lumos skeleton.
955
- *
956
- * @param skeleton - The Lumos transaction skeleton.
957
- * @returns A Transaction instance.
958
- *
959
- * @throws Will throw an error if an input's outPoint is missing.
960
- *
961
- * @example
962
- * ```typescript
963
- * const transaction = Transaction.fromLumosSkeleton(skeleton);
964
- * ```
965
- */
966
- static fromLumosSkeleton(skeleton) {
967
- return Transaction_1.from({
968
- version: 0n,
969
- cellDeps: skeleton.cellDeps.toArray(),
970
- headerDeps: skeleton.headerDeps.toArray(),
971
- inputs: skeleton.inputs.toArray().map((input, i) => {
972
- if (!input.outPoint) {
973
- throw new Error("outPoint is required in input");
974
- }
975
- return CellInput.from({
976
- previousOutput: input.outPoint,
977
- since: skeleton.inputSinces.get(i, "0x0"),
978
- cellOutput: input.cellOutput,
979
- outputData: input.data,
980
- });
981
- }),
982
- outputs: skeleton.outputs.toArray().map((output) => output.cellOutput),
983
- outputsData: skeleton.outputs.toArray().map((output) => output.data),
984
- witnesses: skeleton.witnesses.toArray(),
985
- });
986
- }
987
- /**
988
- * @deprecated
989
- * Use ccc.stringify instead.
990
- * stringify the tx to JSON string.
991
- */
992
- stringify() {
993
- return JSON.stringify(this, (_, value) => {
994
- if (typeof value === "bigint") {
995
- return numToHex(value);
996
- }
997
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
998
- return value;
999
- });
1000
- }
1001
- /**
1002
- * Converts the raw transaction data to bytes.
1003
- *
1004
- * @returns A Uint8Array containing the raw transaction bytes.
1005
- *
1006
- * @example
1007
- * ```typescript
1008
- * const rawTxBytes = transaction.rawToBytes();
1009
- * ```
1010
- */
1011
- rawToBytes() {
1012
- return RawTransaction.encode(this);
1013
- }
1014
- /**
1015
- * Calculates the hash of the transaction without witnesses. This is the transaction hash in the usual sense.
1016
- * To calculate the hash of the whole transaction including the witnesses, use transaction.hashFull() instead.
1017
- *
1018
- * @returns The hash of the transaction.
1019
- *
1020
- * @example
1021
- * ```typescript
1022
- * const txHash = transaction.hash();
1023
- * ```
1024
- */
1025
- hash() {
1026
- return hashCkb(this.rawToBytes());
1027
- }
1028
- /**
1029
- * Calculates the hash of the transaction with witnesses.
1030
- *
1031
- * @returns The hash of the transaction with witnesses.
1032
- *
1033
- * @example
1034
- * ```typescript
1035
- * const txFullHash = transaction.hashFull();
1036
- * ```
1037
- */
1038
- hashFull() {
1039
- return hashCkb(this.toBytes());
1040
- }
1041
- /**
1042
- * Hashes a witness and updates the hasher.
1043
- *
1044
- * @param witness - The witness to hash.
1045
- * @param hasher - The hasher instance to update.
1046
- *
1047
- * @example
1048
- * ```typescript
1049
- * Transaction.hashWitnessToHasher("0x...", hasher);
1050
- * ```
1051
- */
1052
- static hashWitnessToHasher(witness, hasher) {
1053
- const raw = bytesFrom(hexFrom(witness));
1054
- hasher.update(numToBytes(raw.length, 8));
1055
- hasher.update(raw);
1056
- }
1057
- /**
1058
- * Computes the signing hash information for a given script.
1059
- *
1060
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
1061
- * @param client - The client for complete extra infos in the transaction.
1062
- * @returns A promise that resolves to an object containing the signing message and the witness position,
1063
- * or undefined if no matching input is found.
1064
- *
1065
- * @example
1066
- * ```typescript
1067
- * const signHashInfo = await tx.getSignHashInfo(scriptLike, client);
1068
- * if (signHashInfo) {
1069
- * console.log(signHashInfo.message); // Outputs the signing message
1070
- * console.log(signHashInfo.position); // Outputs the witness position
1071
- * }
1072
- * ```
1073
- */
1074
- async getSignHashInfo(scriptLike, client, hasher = new HasherCkb()) {
1075
- const script = Script.from(scriptLike);
1076
- let position = -1;
1077
- hasher.update(this.hash());
1078
- for (let i = 0; i < this.witnesses.length; i += 1) {
1079
- const input = this.inputs[i];
1080
- if (input) {
1081
- const { cellOutput } = await input.getCell(client);
1082
- if (!script.eq(cellOutput.lock)) {
1083
- continue;
1084
- }
1085
- if (position === -1) {
1086
- position = i;
1087
- }
1088
- }
1089
- if (position === -1) {
1090
- return undefined;
1091
- }
1092
- Transaction_1.hashWitnessToHasher(this.witnesses[i], hasher);
1093
- }
1094
- if (position === -1) {
1095
- return undefined;
1096
- }
1097
- return {
1098
- message: hasher.digest(),
1099
- position,
1100
- };
1101
- }
1102
- /**
1103
- * Find the first occurrence of a input with the specified lock id
1104
- *
1105
- * @param scriptIdLike - The script associated with the transaction, represented as a ScriptLike object without args.
1106
- * @param client - The client for complete extra infos in the transaction.
1107
- * @returns A promise that resolves to the found index
1108
- *
1109
- * @example
1110
- * ```typescript
1111
- * const index = await tx.findInputIndexByLockId(scriptIdLike, client);
1112
- * ```
1113
- */
1114
- async findInputIndexByLockId(scriptIdLike, client) {
1115
- const script = Script.from({ ...scriptIdLike, args: "0x" });
1116
- for (let i = 0; i < this.inputs.length; i += 1) {
1117
- const { cellOutput } = await this.inputs[i].getCell(client);
1118
- if (script.codeHash === cellOutput.lock.codeHash &&
1119
- script.hashType === cellOutput.lock.hashType) {
1120
- return i;
1121
- }
1122
- }
1123
- }
1124
- /**
1125
- * Find the first occurrence of a input with the specified lock
1126
- *
1127
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
1128
- * @param client - The client for complete extra infos in the transaction.
1129
- * @returns A promise that resolves to the found index, or undefined if no matching input is found.
1130
- *
1131
- * @example
1132
- * ```typescript
1133
- * const index = await tx.findInputIndexByLock(scriptLike, client);
1134
- * ```
1135
- */
1136
- async findInputIndexByLock(scriptLike, client) {
1137
- const script = Script.from(scriptLike);
1138
- for (let i = 0; i < this.inputs.length; i += 1) {
1139
- const { cellOutput } = await this.inputs[i].getCell(client);
1140
- if (script.eq(cellOutput.lock)) {
1141
- return i;
1142
- }
1143
- }
1144
- }
1145
- /**
1146
- * Find the last occurrence of a input with the specified lock
1147
- *
1148
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
1149
- * @param client - The client for complete extra infos in the transaction.
1150
- * @returns A promise that resolves to the found index, or undefined if no matching input is found.
1151
- *
1152
- * @example
1153
- * ```typescript
1154
- * const index = await tx.findLastInputIndexByLock(scriptLike, client);
1155
- * ```
1156
- */
1157
- async findLastInputIndexByLock(scriptLike, client) {
1158
- const script = Script.from(scriptLike);
1159
- for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
1160
- const { cellOutput } = await this.inputs[i].getCell(client);
1161
- if (script.eq(cellOutput.lock)) {
1162
- return i;
1163
- }
1164
- }
1165
- }
1166
- /**
1167
- * Add cell deps if they are not existed
1168
- *
1169
- * @param cellDepLikes - The cell deps to add
1170
- *
1171
- * @example
1172
- * ```typescript
1173
- * tx.addCellDeps(cellDep);
1174
- * ```
1175
- */
1176
- addCellDeps(...cellDepLikes) {
1177
- cellDepLikes.flat().forEach((cellDepLike) => {
1178
- const cellDep = CellDep.from(cellDepLike);
1179
- if (this.cellDeps.some((c) => c.eq(cellDep))) {
1180
- return;
1181
- }
1182
- this.cellDeps.push(cellDep);
1183
- });
1184
- }
1185
- /**
1186
- * Add cell deps at the start if they are not existed
1187
- *
1188
- * @param cellDepLikes - The cell deps to add
1189
- *
1190
- * @example
1191
- * ```typescript
1192
- * tx.addCellDepsAtBegin(cellDep);
1193
- * ```
1194
- */
1195
- addCellDepsAtStart(...cellDepLikes) {
1196
- cellDepLikes.flat().forEach((cellDepLike) => {
1197
- const cellDep = CellDep.from(cellDepLike);
1198
- if (this.cellDeps.some((c) => c.eq(cellDep))) {
1199
- return;
1200
- }
1201
- this.cellDeps.unshift(cellDep);
1202
- });
1203
- }
1204
- /**
1205
- * Add cell dep from infos if they are not existed
1206
- *
1207
- * @param client - A client for searching cell deps
1208
- * @param cellDepInfoLikes - The cell dep infos to add
1209
- *
1210
- * @example
1211
- * ```typescript
1212
- * tx.addCellDepInfos(client, cellDepInfos);
1213
- * ```
1214
- */
1215
- async addCellDepInfos(client, ...cellDepInfoLikes) {
1216
- this.addCellDeps(await client.getCellDeps(...cellDepInfoLikes));
1217
- }
1218
- /**
1219
- * Add cell deps from known script
1220
- *
1221
- * @param client - The client for searching known script and cell deps
1222
- * @param scripts - The known scripts to add
1223
- *
1224
- * @example
1225
- * ```typescript
1226
- * tx.addCellDepsOfKnownScripts(client, KnownScript.OmniLock);
1227
- * ```
1228
- */
1229
- async addCellDepsOfKnownScripts(client, ...scripts) {
1230
- await Promise.all(scripts
1231
- .flat()
1232
- .map(async (script) => this.addCellDepInfos(client, (await client.getKnownScript(script)).cellDeps)));
1233
- }
1234
- /**
1235
- * Set output data at index.
1236
- *
1237
- * @param index - The index of the output data.
1238
- * @param data - The data to set.
1239
- *
1240
- * @example
1241
- * ```typescript
1242
- * tx.setOutputDataAt(0, "0x00");
1243
- * ```
1244
- */
1245
- setOutputDataAt(index, data) {
1246
- if (this.outputsData.length < index) {
1247
- this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
1248
- }
1249
- this.outputsData[index] = hexFrom(data);
1250
- }
1251
- /**
1252
- * get input
1253
- *
1254
- * @param index - The cell input index
1255
- *
1256
- * @example
1257
- * ```typescript
1258
- * await tx.getInput(0);
1259
- * ```
1260
- */
1261
- getInput(index) {
1262
- return this.inputs[Number(numFrom(index))];
1263
- }
1264
- /**
1265
- * add input
1266
- *
1267
- * @param inputLike - The cell input.
1268
- *
1269
- * @example
1270
- * ```typescript
1271
- * await tx.addInput({ });
1272
- * ```
1273
- */
1274
- addInput(inputLike) {
1275
- if (this.witnesses.length > this.inputs.length) {
1276
- this.witnesses.splice(this.inputs.length, 0, "0x");
1277
- }
1278
- return this.inputs.push(CellInput.from(inputLike));
1279
- }
1280
- /**
1281
- * get output
1282
- *
1283
- * @param index - The cell output index
1284
- *
1285
- * @example
1286
- * ```typescript
1287
- * await tx.getOutput(0);
1288
- * ```
1289
- */
1290
- getOutput(index) {
1291
- const i = Number(numFrom(index));
1292
- if (i >= this.outputs.length) {
1293
- return;
1294
- }
1295
- return CellAny.from({
1296
- cellOutput: this.outputs[i],
1297
- outputData: this.outputsData[i] ?? "0x",
1298
- });
1299
- }
1300
- /**
1301
- * Provides an iterable over the transaction's output cells.
1302
- *
1303
- * This getter is a convenient way to iterate through all the output cells (`CellAny`)
1304
- * of the transaction, combining the `outputs` and `outputsData` arrays.
1305
- * It can be used with `for...of` loops or other iterable-consuming patterns.
1306
- *
1307
- * @public
1308
- * @category Getter
1309
- * @returns An `Iterable<CellAny>` that yields each output cell of the transaction.
1310
- *
1311
- * @example
1312
- * ```typescript
1313
- * for (const cell of tx.outputCells) {
1314
- * console.log(`Output cell capacity: ${cell.cellOutput.capacity}`);
1315
- * }
1316
- * ```
1317
- */
1318
- get outputCells() {
1319
- const { outputs, outputsData } = this;
1320
- function* generator() {
1321
- for (let i = 0; i < outputs.length; i++) {
1322
- yield CellAny.from({
1323
- cellOutput: outputs[i],
1324
- outputData: outputsData[i] ?? "0x",
1325
- });
1326
- }
1327
- }
1328
- return generator();
1329
- }
1330
- addOutput(cellOrOutputLike, outputDataLike) {
1331
- const cell = "cellOutput" in cellOrOutputLike
1332
- ? CellAny.from(cellOrOutputLike)
1333
- : CellAny.from({
1334
- cellOutput: cellOrOutputLike,
1335
- outputData: outputDataLike,
1336
- });
1337
- const len = this.outputs.push(cell.cellOutput);
1338
- this.setOutputDataAt(len - 1, cell.outputData);
1339
- return len;
1340
- }
1341
- /**
1342
- * Get witness at index as WitnessArgs
1343
- *
1344
- * @param index - The index of the witness.
1345
- * @returns The witness parsed as WitnessArgs.
1346
- *
1347
- * @example
1348
- * ```typescript
1349
- * const witnessArgs = await tx.getWitnessArgsAt(0);
1350
- * ```
1351
- */
1352
- getWitnessArgsAt(index) {
1353
- const rawWitness = this.witnesses[index];
1354
- return (rawWitness ?? "0x") !== "0x"
1355
- ? WitnessArgs.fromBytes(rawWitness)
1356
- : undefined;
1357
- }
1358
- /**
1359
- * Set witness at index by WitnessArgs
1360
- *
1361
- * @param index - The index of the witness.
1362
- * @param witness - The WitnessArgs to set.
1363
- *
1364
- * @example
1365
- * ```typescript
1366
- * await tx.setWitnessArgsAt(0, witnessArgs);
1367
- * ```
1368
- */
1369
- setWitnessArgsAt(index, witness) {
1370
- this.setWitnessAt(index, witness.toBytes());
1371
- }
1372
- /**
1373
- * Set witness at index
1374
- *
1375
- * @param index - The index of the witness.
1376
- * @param witness - The witness to set.
1377
- *
1378
- * @example
1379
- * ```typescript
1380
- * await tx.setWitnessAt(0, witness);
1381
- * ```
1382
- */
1383
- setWitnessAt(index, witness) {
1384
- if (this.witnesses.length < index) {
1385
- this.witnesses.push(...Array.from(new Array(index - this.witnesses.length), () => "0x"));
1386
- }
1387
- this.witnesses[index] = hexFrom(witness);
1388
- }
1389
- /**
1390
- * Prepare dummy witness for sighash all method
1391
- *
1392
- * @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
1393
- * @param lockLen - The length of dummy lock bytes.
1394
- * @param client - The client for complete extra infos in the transaction.
1395
- * @returns A promise that resolves to the prepared transaction
1396
- *
1397
- * @example
1398
- * ```typescript
1399
- * await tx.prepareSighashAllWitness(scriptLike, 85, client);
1400
- * ```
1401
- */
1402
- async prepareSighashAllWitness(scriptLike, lockLen, client) {
1403
- const position = await this.findInputIndexByLock(scriptLike, client);
1404
- if (position === undefined) {
1405
- return;
1406
- }
1407
- const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
1408
- witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
1409
- this.setWitnessArgsAt(position, witness);
1410
- }
1411
- async getInputsCapacityExtra(client) {
1412
- return reduceAsync(this.inputs, async (acc, input) => acc + (await input.getExtraCapacity(client)), numFrom(0));
1413
- }
1414
- // This also includes extra amount
1415
- async getInputsCapacity(client) {
1416
- return ((await reduceAsync(this.inputs, async (acc, input) => {
1417
- const { cellOutput: { capacity }, } = await input.getCell(client);
1418
- return acc + capacity;
1419
- }, numFrom(0))) + (await this.getInputsCapacityExtra(client)));
1420
- }
1421
- getOutputsCapacity() {
1422
- return this.outputs.reduce((acc, { capacity }) => acc + capacity, numFrom(0));
1423
- }
1424
- async getInputsUdtBalance(client, type) {
1425
- return reduceAsync(this.inputs, async (acc, input) => {
1426
- const { cellOutput, outputData } = await input.getCell(client);
1427
- if (!cellOutput.type?.eq(type)) {
1428
- return;
1429
- }
1430
- return acc + udtBalanceFrom(outputData);
1431
- }, numFrom(0));
1432
- }
1433
- getOutputsUdtBalance(type) {
1434
- return this.outputs.reduce((acc, output, i) => {
1435
- if (!output.type?.eq(type)) {
1436
- return acc;
1437
- }
1438
- return acc + udtBalanceFrom(this.outputsData[i]);
1439
- }, numFrom(0));
1440
- }
1441
- async completeInputs(from, filter, accumulator, init) {
1442
- const collectedCells = [];
1443
- let acc = init;
1444
- let fulfilled = false;
1445
- for await (const cell of from.findCells(filter, true)) {
1446
- if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
1447
- continue;
1448
- }
1449
- const i = collectedCells.push(cell);
1450
- const next = await Promise.resolve(accumulator(acc, cell, i - 1, collectedCells));
1451
- if (next === undefined) {
1452
- fulfilled = true;
1453
- break;
1454
- }
1455
- acc = next;
1456
- }
1457
- collectedCells.forEach((cell) => this.addInput(cell));
1458
- if (fulfilled) {
1459
- return {
1460
- addedCount: collectedCells.length,
1461
- };
1462
- }
1463
- return {
1464
- addedCount: collectedCells.length,
1465
- accumulated: acc,
1466
- };
1467
- }
1468
- async completeInputsByCapacity(from, capacityTweak, filter) {
1469
- const expectedCapacity = this.getOutputsCapacity() + numFrom(capacityTweak ?? 0);
1470
- const inputsCapacity = await this.getInputsCapacity(from.client);
1471
- if (inputsCapacity >= expectedCapacity) {
1472
- return 0;
1473
- }
1474
- const { addedCount, accumulated } = await this.completeInputs(from, filter ?? {
1475
- scriptLenRange: [0, 1],
1476
- outputDataLenRange: [0, 1],
1477
- }, (acc, { cellOutput: { capacity } }) => {
1478
- const sum = acc + capacity;
1479
- return sum >= expectedCapacity ? undefined : sum;
1480
- }, inputsCapacity);
1481
- if (accumulated === undefined) {
1482
- return addedCount;
1483
- }
1484
- throw new ErrorTransactionInsufficientCapacity(expectedCapacity - accumulated);
1485
- }
1486
- async completeInputsAll(from, filter) {
1487
- const { addedCount } = await this.completeInputs(from, filter ?? {
1488
- scriptLenRange: [0, 1],
1489
- outputDataLenRange: [0, 1],
1490
- }, (acc, { cellOutput: { capacity } }) => acc + capacity, Zero);
1491
- return addedCount;
1492
- }
1493
- /**
1494
- * Complete inputs by UDT balance
1495
- *
1496
- * This method succeeds only if enough balance is collected.
1497
- *
1498
- * It will try to collect at least two inputs, even when the first input already contains enough balance, to avoid extra occupation fees introduced by the change cell. An edge case: If the first cell has the same amount as the output, a new cell is not needed.
1499
- * @param from - The signer to complete the inputs.
1500
- * @param type - The type script of the UDT.
1501
- * @param balanceTweak - The tweak of the balance.
1502
- * @returns A promise that resolves to the number of inputs added.
1503
- */
1504
- async completeInputsByUdt(from, type, balanceTweak) {
1505
- const expectedBalance = this.getOutputsUdtBalance(type) + numFrom(balanceTweak ?? 0);
1506
- if (expectedBalance === numFrom(0)) {
1507
- return 0;
1508
- }
1509
- const [inputsBalance, inputsCount] = await reduceAsync(this.inputs, async ([balanceAcc, countAcc], input) => {
1510
- const { cellOutput, outputData } = await input.getCell(from.client);
1511
- if (!cellOutput.type?.eq(type)) {
1512
- return;
1513
- }
1514
- return [balanceAcc + udtBalanceFrom(outputData), countAcc + 1];
1515
- }, [numFrom(0), 0]);
1516
- if (inputsBalance === expectedBalance ||
1517
- (inputsBalance >= expectedBalance && inputsCount >= 2)) {
1518
- return 0;
1519
- }
1520
- const { addedCount, accumulated } = await this.completeInputs(from, {
1521
- script: type,
1522
- outputDataLenRange: [16, numFrom("0xffffffff")],
1523
- }, (acc, { outputData }, _i, collected) => {
1524
- const balance = udtBalanceFrom(outputData);
1525
- const sum = acc + balance;
1526
- return sum === expectedBalance ||
1527
- (sum >= expectedBalance && inputsCount + collected.length >= 2)
1528
- ? undefined
1529
- : sum;
1530
- }, inputsBalance);
1531
- if (accumulated === undefined || accumulated >= expectedBalance) {
1532
- return addedCount;
1533
- }
1534
- throw new ErrorTransactionInsufficientCoin(expectedBalance - accumulated, type);
1535
- }
1536
- async completeInputsAddOne(from, filter) {
1537
- const { addedCount, accumulated } = await this.completeInputs(from, filter ?? {
1538
- scriptLenRange: [0, 1],
1539
- outputDataLenRange: [0, 1],
1540
- }, () => undefined, true);
1541
- if (accumulated === undefined) {
1542
- return addedCount;
1543
- }
1544
- throw new Error(`Insufficient CKB, need at least one new cell`);
1545
- }
1546
- async completeInputsAtLeastOne(from, filter) {
1547
- if (this.inputs.length > 0) {
1548
- return 0;
1549
- }
1550
- return this.completeInputsAddOne(from, filter);
1551
- }
1552
- async getFee(client) {
1553
- return (await this.getInputsCapacity(client)) - this.getOutputsCapacity();
1554
- }
1555
- async getFeeRate(client) {
1556
- return (((await this.getFee(client)) * numFrom(1000)) /
1557
- numFrom(this.toBytes().length + 4));
1558
- }
1559
- estimateFee(feeRate) {
1560
- const txSize = this.toBytes().length + 4;
1561
- // + 999 then / 1000 to ceil the calculated fee
1562
- return (numFrom(txSize) * numFrom(feeRate) + numFrom(999)) / numFrom(1000);
1563
- }
1564
- /**
1565
- * Completes the transaction fee by adding inputs and handling change outputs.
1566
- * This method automatically calculates the required fee based on the transaction size and fee rate,
1567
- * adds necessary inputs to cover the fee, and handles change outputs through the provided change function.
1568
- *
1569
- * @param from - The signer to complete inputs from and prepare the transaction.
1570
- * @param change - A function that handles change capacity. It receives the transaction and excess capacity,
1571
- * and should return the additional capacity needed (0 if change is handled successfully,
1572
- * positive number if more capacity is needed for change cell creation).
1573
- * @param expectedFeeRate - The expected fee rate in shannons per 1000 bytes. If not provided,
1574
- * it will be fetched from the client.
1575
- * @param filter - Optional filter for selecting cells when adding inputs.
1576
- * @param options - Optional configuration object.
1577
- * @param options.feeRateBlockRange - Block range for fee rate calculation when expectedFeeRate is not provided.
1578
- * @param options.maxFeeRate - Maximum allowed fee rate.
1579
- * @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
1580
- * @returns A promise that resolves to a tuple containing:
1581
- * - The number of inputs added during the process
1582
- * - A boolean indicating whether change outputs were created (true) or fee was paid without change (false)
1583
- *
1584
- * @throws {ErrorTransactionInsufficientCapacity} When there's not enough capacity to cover the fee.
1585
- * @throws {Error} When the change function doesn't properly handle the available capacity.
1586
- *
1587
- * @example
1588
- * ```typescript
1589
- * const [addedInputs, hasChange] = await tx.completeFee(
1590
- * signer,
1591
- * (tx, capacity) => {
1592
- * if (capacity >= 61_00000000n) { // Minimum for a change cell
1593
- * tx.addOutput({ capacity, lock: changeScript });
1594
- * return 0;
1595
- * }
1596
- * return 61_00000000n; // Need more capacity for change cell
1597
- * },
1598
- * 1000n // 1000 shannons per 1000 bytes
1599
- * );
1600
- * ```
1601
- */
1602
- async completeFee(from, change, expectedFeeRate, filter, options) {
1603
- const feeRate = expectedFeeRate ??
1604
- (await from.client.getFeeRate(options?.feeRateBlockRange, options));
1605
- // Complete all inputs extra infos for cache
1606
- await this.getInputsCapacity(from.client);
1607
- let leastFee = Zero;
1608
- let leastExtraCapacity = Zero;
1609
- let collected = 0;
1610
- // ===
1611
- // Usually, for the worst situation, three iterations are needed
1612
- // 1. First attempt to complete the transaction.
1613
- // 2. Not enough capacity for the change cell.
1614
- // 3. Fee increased by the change cell.
1615
- // ===
1616
- while (true) {
1617
- collected += await (async () => {
1618
- if (!(options?.shouldAddInputs ?? true)) {
1619
- return 0;
1620
- }
1621
- try {
1622
- return await this.completeInputsByCapacity(from, leastFee + leastExtraCapacity, filter);
1623
- }
1624
- catch (err) {
1625
- if (err instanceof ErrorTransactionInsufficientCapacity &&
1626
- leastExtraCapacity !== Zero) {
1627
- throw new ErrorTransactionInsufficientCapacity(err.amount, {
1628
- isForChange: true,
1629
- });
1630
- }
1631
- throw err;
1632
- }
1633
- })();
1634
- const fee = await this.getFee(from.client);
1635
- if (fee < leastFee + leastExtraCapacity) {
1636
- // Not enough capacity are collected, it should only happens when shouldAddInputs is false
1637
- throw new ErrorTransactionInsufficientCapacity(leastFee + leastExtraCapacity - fee, { isForChange: leastExtraCapacity !== Zero });
1638
- }
1639
- await from.prepareTransaction(this);
1640
- if (leastFee === Zero) {
1641
- // The initial fee is calculated based on prepared transaction
1642
- // This should only happens during the first iteration
1643
- leastFee = this.estimateFee(feeRate);
1644
- }
1645
- // The extra capacity paid the fee without a change
1646
- // leastExtraCapacity should be 0 here, otherwise we should failed in the previous check
1647
- // So this only happens in the first iteration
1648
- if (fee === leastFee) {
1649
- return [collected, false];
1650
- }
1651
- // Invoke the change function on a transaction multiple times may cause problems, so we clone it
1652
- const tx = this.clone();
1653
- const needed = numFrom(await Promise.resolve(change(tx, fee - leastFee)));
1654
- if (needed > Zero) {
1655
- // No enough extra capacity to create new cells for change, collect inputs again
1656
- leastExtraCapacity = needed;
1657
- continue;
1658
- }
1659
- if ((await tx.getFee(from.client)) !== leastFee) {
1660
- throw new Error("The change function doesn't use all available capacity");
1661
- }
1662
- // New change cells created, update the fee
1663
- await from.prepareTransaction(tx);
1664
- const changedFee = tx.estimateFee(feeRate);
1665
- if (leastFee > changedFee) {
1666
- throw new Error("The change function removed existed transaction data");
1667
- }
1668
- // The fee has been paid
1669
- if (leastFee === changedFee) {
1670
- this.copy(tx);
1671
- return [collected, true];
1672
- }
1673
- // The fee after changing is more than the original fee
1674
- leastFee = changedFee;
1675
- }
1676
- }
1677
- /**
1678
- * Completes the transaction fee by adding inputs and creating a change output with the specified lock script.
1679
- * This is a convenience method that automatically creates a change cell with the provided lock script
1680
- * when there's excess capacity after paying the transaction fee.
1681
- *
1682
- * @param from - The signer to complete inputs from and prepare the transaction.
1683
- * @param change - The lock script for the change output cell.
1684
- * @param feeRate - Optional fee rate in shannons per 1000 bytes. If not provided, it will be fetched from the client.
1685
- * @param filter - Optional filter for selecting cells when adding inputs.
1686
- * @param options - Optional configuration object.
1687
- * @param options.feeRateBlockRange - Block range for fee rate calculation when feeRate is not provided.
1688
- * @param options.maxFeeRate - Maximum allowed fee rate.
1689
- * @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
1690
- * @returns A promise that resolves to a tuple containing:
1691
- * - The number of inputs added during the process
1692
- * - A boolean indicating whether change outputs were created (true) or fee was paid without change (false)
1693
- *
1694
- * @example
1695
- * ```typescript
1696
- * const changeScript = Script.from({
1697
- * codeHash: "0x...",
1698
- * hashType: "type",
1699
- * args: "0x..."
1700
- * });
1701
- *
1702
- * const [addedInputs, hasChange] = await tx.completeFeeChangeToLock(
1703
- * signer,
1704
- * changeScript,
1705
- * 1000n // 1000 shannons per 1000 bytes
1706
- * );
1707
- * ```
1708
- */
1709
- completeFeeChangeToLock(from, change, feeRate, filter, options) {
1710
- const script = Script.from(change);
1711
- return this.completeFee(from, (tx, capacity) => {
1712
- const changeCell = CellOutput.from({ capacity: 0, lock: script });
1713
- const occupiedCapacity = fixedPointFrom(changeCell.occupiedSize);
1714
- if (capacity < occupiedCapacity) {
1715
- return occupiedCapacity;
1716
- }
1717
- changeCell.capacity = capacity;
1718
- tx.addOutput(changeCell);
1719
- return 0;
1720
- }, feeRate, filter, options);
1721
- }
1722
- /**
1723
- * Completes the transaction fee using the signer's recommended address for change.
1724
- * This is a convenience method that automatically uses the signer's recommended
1725
- * address as the change destination, making it easier to complete transactions
1726
- * without manually specifying a change address.
1727
- *
1728
- * @param from - The signer to complete inputs from and prepare the transaction.
1729
- * @param feeRate - Optional fee rate in shannons per 1000 bytes. If not provided, it will be fetched from the client.
1730
- * @param filter - Optional filter for selecting cells when adding inputs.
1731
- * @param options - Optional configuration object.
1732
- * @param options.feeRateBlockRange - Block range for fee rate calculation when feeRate is not provided.
1733
- * @param options.maxFeeRate - Maximum allowed fee rate.
1734
- * @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
1735
- * @returns A promise that resolves to a tuple containing:
1736
- * - The number of inputs added during the process
1737
- * - A boolean indicating whether change outputs were created (true) or fee was paid without change (false)
1738
- *
1739
- * @example
1740
- * ```typescript
1741
- * const [addedInputs, hasChange] = await tx.completeFeeBy(
1742
- * signer,
1743
- * 1000n // 1000 shannons per 1000 bytes
1744
- * );
1745
- *
1746
- * // Change will automatically go to signer's recommended address
1747
- * ```
1748
- */
1749
- async completeFeeBy(from, feeRate, filter, options) {
1750
- const { script } = await from.getRecommendedAddressObj();
1751
- return this.completeFeeChangeToLock(from, script, feeRate, filter, options);
1752
- }
1753
- /**
1754
- * Completes the transaction fee by adding excess capacity to an existing output.
1755
- * Instead of creating a new change output, this method adds any excess capacity
1756
- * to the specified existing output in the transaction.
1757
- *
1758
- * @param from - The signer to complete inputs from and prepare the transaction.
1759
- * @param index - The index of the existing output to add excess capacity to.
1760
- * @param feeRate - Optional fee rate in shannons per 1000 bytes. If not provided, it will be fetched from the client.
1761
- * @param filter - Optional filter for selecting cells when adding inputs.
1762
- * @param options - Optional configuration object.
1763
- * @param options.feeRateBlockRange - Block range for fee rate calculation when feeRate is not provided.
1764
- * @param options.maxFeeRate - Maximum allowed fee rate.
1765
- * @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
1766
- * @returns A promise that resolves to a tuple containing:
1767
- * - The number of inputs added during the process
1768
- * - A boolean indicating whether change was applied (true) or fee was paid without change (false)
1769
- *
1770
- * @throws {Error} When the specified output index doesn't exist.
1771
- *
1772
- * @example
1773
- * ```typescript
1774
- * // Add excess capacity to the first output (index 0)
1775
- * const [addedInputs, hasChange] = await tx.completeFeeChangeToOutput(
1776
- * signer,
1777
- * 0, // Output index
1778
- * 1000n // 1000 shannons per 1000 bytes
1779
- * );
1780
- * ```
1781
- */
1782
- completeFeeChangeToOutput(from, index, feeRate, filter, options) {
1783
- const change = Number(numFrom(index));
1784
- if (!this.outputs[change]) {
1785
- throw new Error("Non-existed output to change");
1786
- }
1787
- return this.completeFee(from, (tx, capacity) => {
1788
- tx.outputs[change].capacity += capacity;
1789
- return 0;
1790
- }, feeRate, filter, options);
1791
- }
1792
- };
1793
- Transaction = Transaction_1 = __decorate([
1794
- mol.codec(mol
1795
- .table({
1796
- raw: RawTransaction,
1797
- witnesses: mol.BytesVec,
1798
- })
1799
- .mapIn((txLike) => {
1800
- const tx = Transaction.from(txLike);
1801
- return {
1802
- raw: tx,
1803
- witnesses: tx.witnesses,
1804
- };
1805
- })
1806
- .mapOut((tx) => Transaction.from({ ...tx.raw, witnesses: tx.witnesses })))
1807
- ], Transaction);
1808
- export { Transaction };
1809
- /**
1810
- * Calculate Nervos DAO profit between two blocks.
1811
- * This function computes the profit earned from a Nervos DAO deposit
1812
- * based on the capacity and the time period between deposit and withdrawal.
1813
- *
1814
- * @param profitableCapacity - The capacity that earns profit (total capacity minus occupied capacity).
1815
- * @param depositHeaderLike - The block header when the DAO deposit was made.
1816
- * @param withdrawHeaderLike - The block header when the DAO withdrawal is made.
1817
- * @returns The profit amount in CKB (capacity units).
1818
- *
1819
- * @example
1820
- * ```typescript
1821
- * const profit = calcDaoProfit(
1822
- * ccc.fixedPointFrom(100), // 100 CKB profitable capacity
1823
- * depositHeader,
1824
- * withdrawHeader
1825
- * );
1826
- * console.log(`Profit: ${profit} shannons`);
1827
- * ```
1828
- *
1829
- * @see {@link https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md | Nervos DAO RFC}
1830
- */
1831
- export function calcDaoProfit(profitableCapacity, depositHeaderLike, withdrawHeaderLike) {
1832
- const depositHeader = ClientBlockHeader.from(depositHeaderLike);
1833
- const withdrawHeader = ClientBlockHeader.from(withdrawHeaderLike);
1834
- const profitableSize = numFrom(profitableCapacity);
1835
- return ((profitableSize * withdrawHeader.dao.ar) / depositHeader.dao.ar -
1836
- profitableSize);
1837
- }
1838
- /**
1839
- * Calculate claimable epoch for Nervos DAO withdrawal.
1840
- * This function determines the earliest epoch when a Nervos DAO withdrawal
1841
- * can be claimed based on the deposit and withdrawal epochs.
1842
- *
1843
- * @param depositHeader - The block header when the DAO deposit was made.
1844
- * @param withdrawHeader - The block header when the DAO withdrawal was initiated.
1845
- * @returns The epoch when the withdrawal can be claimed, represented as [number, index, length].
1846
- *
1847
- * @example
1848
- * ```typescript
1849
- * const claimEpoch = calcDaoClaimEpoch(depositHeader, withdrawHeader);
1850
- * console.log(`Can claim at epoch: ${claimEpoch[0]}, index: ${claimEpoch[1]}, length: ${claimEpoch[2]}`);
1851
- * ```
1852
- *
1853
- * @remarks
1854
- * The Nervos DAO has a minimum lock period of 180 epochs (~30 days).
1855
- * This function calculates the exact epoch when the withdrawal becomes claimable
1856
- * based on the deposit epoch and withdrawal epoch timing.
1857
- *
1858
- * @see {@link https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md | Nervos DAO RFC}
1859
- */
1860
- export function calcDaoClaimEpoch(depositHeader, withdrawHeader) {
1861
- const depositEpoch = ClientBlockHeader.from(depositHeader).epoch;
1862
- const withdrawEpoch = ClientBlockHeader.from(withdrawHeader).epoch;
1863
- const intDiff = withdrawEpoch[0] - depositEpoch[0];
1864
- // deposit[1] withdraw[1]
1865
- // ---------- <= -----------
1866
- // deposit[2] withdraw[2]
1867
- if (intDiff % numFrom(180) !== numFrom(0) ||
1868
- depositEpoch[1] * withdrawEpoch[2] <= depositEpoch[2] * withdrawEpoch[1]) {
1869
- return [
1870
- depositEpoch[0] + (intDiff / numFrom(180) + numFrom(1)) * numFrom(180),
1871
- depositEpoch[1],
1872
- depositEpoch[2],
1873
- ];
1874
- }
1875
- return [
1876
- depositEpoch[0] + (intDiff / numFrom(180)) * numFrom(180),
1877
- depositEpoch[1],
1878
- depositEpoch[2],
1879
- ];
1880
- }