@ckb-ccc/core 0.0.4-alpha.1 → 0.0.4-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/README.md +12 -7
  2. package/dist/address/address.advanced.d.ts +34 -0
  3. package/dist/address/address.advanced.d.ts.map +1 -1
  4. package/dist/address/address.advanced.js +41 -7
  5. package/dist/address/index.d.ts +36 -0
  6. package/dist/address/index.d.ts.map +1 -1
  7. package/dist/address/index.js +44 -5
  8. package/dist/bytes/advanced.d.ts +3 -0
  9. package/dist/bytes/advanced.d.ts.map +1 -1
  10. package/dist/bytes/index.d.ts +57 -0
  11. package/dist/bytes/index.d.ts.map +1 -1
  12. package/dist/bytes/index.js +57 -0
  13. package/dist/ckb/script.d.ts +98 -1
  14. package/dist/ckb/script.d.ts.map +1 -1
  15. package/dist/ckb/script.js +98 -1
  16. package/dist/ckb/transaction.d.ts +392 -1
  17. package/dist/ckb/transaction.d.ts.map +1 -1
  18. package/dist/ckb/transaction.js +403 -1
  19. package/dist/client/client.d.ts +10 -8
  20. package/dist/client/client.d.ts.map +1 -1
  21. package/dist/client/client.js +18 -0
  22. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  23. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  24. package/dist/client/clientPublicMainnet.d.ts +2 -2
  25. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  26. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  27. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  28. package/dist/client/clientPublicTestnet.d.ts +2 -2
  29. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  30. package/dist/client/clientTypes.d.ts +6 -0
  31. package/dist/client/clientTypes.d.ts.map +1 -1
  32. package/dist/client/jsonRpc/advanced.d.ts +2 -87
  33. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  34. package/dist/client/jsonRpc/advanced.js +2 -65
  35. package/dist/client/jsonRpc/index.d.ts +60 -6
  36. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  37. package/dist/client/jsonRpc/index.js +72 -7
  38. package/dist/client/jsonRpc/transformers.d.ts +28 -0
  39. package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
  40. package/dist/client/jsonRpc/transformers.js +122 -0
  41. package/dist/client/jsonRpc/types.d.ts +41 -0
  42. package/dist/client/jsonRpc/types.d.ts.map +1 -0
  43. package/dist/client/jsonRpc/types.js +1 -0
  44. package/dist/fixedPoint/index.d.ts +43 -1
  45. package/dist/fixedPoint/index.d.ts.map +1 -1
  46. package/dist/fixedPoint/index.js +39 -1
  47. package/dist/hasher/index.d.ts +43 -2
  48. package/dist/hasher/index.d.ts.map +1 -1
  49. package/dist/hasher/index.js +42 -0
  50. package/dist/hex/index.d.ts +19 -0
  51. package/dist/hex/index.d.ts.map +1 -1
  52. package/dist/hex/index.js +12 -0
  53. package/dist/num/index.d.ts +99 -0
  54. package/dist/num/index.d.ts.map +1 -1
  55. package/dist/num/index.js +92 -0
  56. package/dist/signer/btc/signerBtc.d.ts +33 -2
  57. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  58. package/dist/signer/btc/signerBtc.js +25 -3
  59. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +36 -0
  60. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  61. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +36 -0
  62. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +35 -0
  63. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  64. package/dist/signer/ckb/signerCkbScriptReadonly.js +35 -0
  65. package/dist/signer/evm/signerEvm.d.ts +27 -2
  66. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  67. package/dist/signer/evm/signerEvm.js +24 -3
  68. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +30 -0
  69. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  70. package/dist/signer/evm/signerEvmAddressReadonly.js +30 -0
  71. package/dist/signer/helpers.d.ts +19 -1
  72. package/dist/signer/helpers.d.ts.map +1 -1
  73. package/dist/signer/helpers.js +26 -9
  74. package/dist/signer/signer.d.ts +75 -4
  75. package/dist/signer/signer.d.ts.map +1 -1
  76. package/dist/signer/signer.js +53 -0
  77. package/package.json +3 -3
  78. package/src/address/address.advanced.ts +54 -18
  79. package/src/address/index.ts +51 -7
  80. package/src/bytes/advanced.ts +12 -9
  81. package/src/bytes/index.ts +60 -0
  82. package/src/ckb/script.ts +111 -2
  83. package/src/ckb/transaction.ts +455 -2
  84. package/src/client/client.ts +31 -8
  85. package/src/client/clientPublicMainnet.advanced.ts +5 -2
  86. package/src/client/clientPublicMainnet.ts +4 -2
  87. package/src/client/clientPublicTestnet.advanced.ts +5 -2
  88. package/src/client/clientPublicTestnet.ts +4 -2
  89. package/src/client/clientTypes.ts +13 -0
  90. package/src/client/jsonRpc/advanced.ts +2 -93
  91. package/src/client/jsonRpc/index.ts +99 -11
  92. package/src/client/jsonRpc/transformers.ts +159 -0
  93. package/src/client/jsonRpc/types.ts +48 -0
  94. package/src/fixedPoint/index.ts +52 -2
  95. package/src/hasher/index.ts +47 -1
  96. package/src/hex/index.ts +19 -0
  97. package/src/num/index.ts +101 -0
  98. package/src/signer/btc/signerBtc.ts +35 -3
  99. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +36 -0
  100. package/src/signer/ckb/signerCkbScriptReadonly.ts +36 -0
  101. package/src/signer/evm/signerEvm.ts +30 -3
  102. package/src/signer/evm/signerEvmAddressReadonly.ts +31 -0
  103. package/src/signer/helpers.ts +28 -8
  104. package/src/signer/signer.ts +77 -4
@@ -1,7 +1,10 @@
1
- import { ScriptLike } from "../ckb";
1
+ import { Script } from "../ckb";
2
2
  import { KnownScript } from "./client";
3
3
 
4
- export const MAINNET_SCRIPTS: Record<KnownScript, Omit<ScriptLike, "args">> = {
4
+ export const MAINNET_SCRIPTS: Record<
5
+ KnownScript,
6
+ Pick<Script, "codeHash" | "hashType">
7
+ > = {
5
8
  [KnownScript.Secp256k1Blake160]: {
6
9
  codeHash:
7
10
  "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
@@ -1,4 +1,4 @@
1
- import { ScriptLike } from "../ckb";
1
+ import { Script } from "../ckb";
2
2
  import { KnownScript } from "./client";
3
3
  import { MAINNET_SCRIPTS } from "./clientPublicMainnet.advanced";
4
4
  import { ClientJsonRpc } from "./jsonRpc";
@@ -12,7 +12,9 @@ export class ClientPublicMainnet extends ClientJsonRpc {
12
12
  return "ckb";
13
13
  }
14
14
 
15
- async getKnownScript(script: KnownScript): Promise<Omit<ScriptLike, "args">> {
15
+ async getKnownScript(
16
+ script: KnownScript,
17
+ ): Promise<Pick<Script, "codeHash" | "hashType">> {
16
18
  return { ...MAINNET_SCRIPTS[script] };
17
19
  }
18
20
  }
@@ -1,7 +1,10 @@
1
- import { ScriptLike } from "../ckb";
1
+ import { Script } from "../ckb";
2
2
  import { KnownScript } from "./client";
3
3
 
4
- export const TESTNET_SCRIPTS: Record<KnownScript, Omit<ScriptLike, "args">> = {
4
+ export const TESTNET_SCRIPTS: Record<
5
+ KnownScript,
6
+ Pick<Script, "codeHash" | "hashType">
7
+ > = {
5
8
  [KnownScript.Secp256k1Blake160]: {
6
9
  codeHash:
7
10
  "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
@@ -1,4 +1,4 @@
1
- import { ScriptLike } from "../ckb";
1
+ import { Script } from "../ckb";
2
2
  import { KnownScript } from "./client";
3
3
  import { TESTNET_SCRIPTS } from "./clientPublicTestnet.advanced";
4
4
  import { ClientJsonRpc } from "./jsonRpc";
@@ -12,7 +12,9 @@ export class ClientPublicTestnet extends ClientJsonRpc {
12
12
  return "ckt";
13
13
  }
14
14
 
15
- async getKnownScript(script: KnownScript): Promise<Omit<ScriptLike, "args">> {
15
+ async getKnownScript(
16
+ script: KnownScript,
17
+ ): Promise<Pick<Script, "codeHash" | "hashType">> {
16
18
  return { ...TESTNET_SCRIPTS[script] };
17
19
  }
18
20
  }
@@ -1 +1,14 @@
1
+ import { Transaction } from "../ckb";
2
+
1
3
  export type OutputsValidator = "passthrough" | "well_known_scripts_only";
4
+
5
+ export type TransactionStatus =
6
+ | "pending"
7
+ | "proposed"
8
+ | "committed"
9
+ | "unknown"
10
+ | "rejected";
11
+ export type ClientTransactionResponse = {
12
+ transaction: Transaction;
13
+ status: TransactionStatus;
14
+ };
@@ -1,93 +1,2 @@
1
- import {
2
- CellDepLike,
3
- CellInputLike,
4
- CellOutputLike,
5
- DepTypeLike,
6
- OutPointLike,
7
- ScriptLike,
8
- TransactionLike,
9
- depTypeFrom,
10
- } from "../../ckb";
11
- import { hexFrom } from "../../hex";
12
- import { numToHex } from "../../num";
13
- import { apply } from "../../utils";
14
- import type { Client } from "../client";
15
-
16
- export type JsonRpcPayload = {
17
- id: number;
18
- jsonrpc: "2.0";
19
- method: string;
20
- params: unknown[] | Record<string, unknown>;
21
- };
22
-
23
- export type JsonRpcMethod = {
24
- method: keyof Client;
25
- rpcMethod: string;
26
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
- inTransformers: (((_: any) => unknown) | undefined)[];
28
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- outTransformer?: (_: any) => unknown;
30
- };
31
-
32
- export class JsonRpcTransformers {
33
- static toDepType(depType: DepTypeLike) {
34
- switch (depTypeFrom(depType)) {
35
- case "code":
36
- return "code";
37
- case "depGroup":
38
- return "dep_group";
39
- }
40
- }
41
- static toScript(script: ScriptLike) {
42
- return {
43
- code_hash: script.codeHash,
44
- hash_type: script.hashType,
45
- args: script.args,
46
- };
47
- }
48
- static toOutPoint(outPoint: OutPointLike) {
49
- return {
50
- index: numToHex(outPoint.index),
51
- tx_hash: outPoint.txHash,
52
- };
53
- }
54
- static toCellInput(cellInput: CellInputLike) {
55
- return {
56
- previous_output: JsonRpcTransformers.toOutPoint(cellInput.previousOutput),
57
- since: numToHex(cellInput.since),
58
- };
59
- }
60
- static toCellOutput(cellOutput: CellOutputLike) {
61
- return {
62
- capacity: numToHex(cellOutput.capacity),
63
- lock: JsonRpcTransformers.toScript(cellOutput.lock),
64
- type: apply(JsonRpcTransformers.toScript, cellOutput.type),
65
- };
66
- }
67
- static toCellDep(cellDep: CellDepLike) {
68
- return {
69
- out_point: JsonRpcTransformers.toOutPoint(cellDep.outPoint),
70
- dep_type: JsonRpcTransformers.toDepType(cellDep.depType),
71
- };
72
- }
73
- static toTransaction(tx: TransactionLike) {
74
- return {
75
- version: numToHex(tx.version),
76
- cell_deps: tx.cellDeps.map((c) => JsonRpcTransformers.toCellDep(c)),
77
- header_deps: tx.headerDeps,
78
- inputs: tx.inputs.map((i) => JsonRpcTransformers.toCellInput(i)),
79
- outputs: tx.outputs.map((o) => JsonRpcTransformers.toCellOutput(o)),
80
- outputs_data: tx.outputsData,
81
- witnesses: tx.witnesses,
82
- };
83
- }
84
- }
85
-
86
- export const CkbRpcMethods: JsonRpcMethod[] = [
87
- {
88
- method: "sendTransaction",
89
- rpcMethod: "send_transaction",
90
- inTransformers: [JsonRpcTransformers.toTransaction],
91
- outTransformer: hexFrom,
92
- },
93
- ];
1
+ export * from "./transformers";
2
+ export * from "./types";
@@ -1,8 +1,23 @@
1
1
  import fetch from "cross-fetch";
2
- import type { Client } from "../client";
3
- import { CkbRpcMethods, JsonRpcMethod, JsonRpcPayload } from "./advanced";
2
+ import { TransactionLike } from "../../ckb";
3
+ import { Hex, HexLike, hexFrom } from "../../hex";
4
+ import { Client } from "../client";
5
+ import { ClientTransactionResponse, OutputsValidator } from "../clientTypes";
6
+ import { JsonRpcPayload, JsonRpcTransformers } from "./advanced";
4
7
 
5
- export interface ClientJsonRpc extends Pick<Client, "sendTransaction"> {}
8
+ /**
9
+ * Applies a transformation function to a value if the transformer is provided.
10
+ *
11
+ * @param value - The value to be transformed.
12
+ * @param transformer - An optional transformation function.
13
+ * @returns The transformed value if a transformer is provided, otherwise the original value.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const result = await transform(5, (x) => x * 2); // Outputs 10
18
+ * const resultWithoutTransformer = await transform(5); // Outputs 5
19
+ * ```
20
+ */
6
21
 
7
22
  async function transform(
8
23
  value: unknown,
@@ -14,24 +29,80 @@ async function transform(
14
29
  return value;
15
30
  }
16
31
 
17
- export abstract class ClientJsonRpc implements Pick<Client, "sendTransaction"> {
32
+ /**
33
+ * An abstract class implementing JSON-RPC client functionality for a specific URL and timeout.
34
+ * Provides methods for sending transactions and building JSON-RPC payloads.
35
+ */
36
+
37
+ export abstract class ClientJsonRpc extends Client {
38
+ /**
39
+ * Creates an instance of ClientJsonRpc.
40
+ *
41
+ * @param url - The URL of the JSON-RPC server.
42
+ * @param timeout - The timeout for requests in milliseconds, default is 30000.
43
+ */
44
+
18
45
  constructor(
19
46
  private readonly url: string,
20
47
  private readonly timeout = 30000,
21
48
  ) {
22
- CkbRpcMethods.map((method) =>
23
- Object.defineProperty(this, method.method, {
24
- value: this.buildSender(method),
25
- enumerable: true,
26
- }),
27
- );
49
+ super();
28
50
  }
29
51
 
52
+ /**
53
+ * Returns the URL of the JSON-RPC server.
54
+ *
55
+ * @returns The URL of the JSON-RPC server.
56
+ */
57
+
30
58
  getUrl() {
31
59
  return this.url;
32
60
  }
33
61
 
34
- buildSender({ rpcMethod, inTransformers, outTransformer }: JsonRpcMethod) {
62
+ /**
63
+ * Send a transaction to node.
64
+ *
65
+ * @param transaction - The transaction to send.
66
+ * @param validator - "passthrough": Disable validation. "well_known_scripts_only": Only accept well known scripts in the transaction.
67
+ * @returns Transaction hash.
68
+ */
69
+
70
+ sendTransaction = this.buildSender(
71
+ "send_transaction",
72
+ [JsonRpcTransformers.toTransaction],
73
+ hexFrom,
74
+ ) as (
75
+ transaction: TransactionLike,
76
+ validator?: OutputsValidator | undefined,
77
+ ) => Promise<Hex>;
78
+
79
+ /**
80
+ * Get a transaction from node.
81
+ *
82
+ * @param txHash - The hash of the transaction.
83
+ * @returns The transaction with status.
84
+ */
85
+
86
+ getTransaction = this.buildSender(
87
+ "get_transaction",
88
+ [hexFrom],
89
+ JsonRpcTransformers.fromTransactionResponse,
90
+ ) as (txHash: HexLike) => Promise<ClientTransactionResponse>;
91
+
92
+ /**
93
+ * Builds a sender function for a JSON-RPC method.
94
+ *
95
+ * @param rpcMethod - The JSON-RPC method.
96
+ * @param inTransformers - An array of input transformers.
97
+ * @param outTransformer - An output transformer function.
98
+ * @returns A function that sends a JSON-RPC request with the given method and transformed parameters.
99
+ */
100
+
101
+ buildSender(
102
+ rpcMethod: string,
103
+ inTransformers: (((_: any) => unknown) | undefined)[],
104
+ outTransformer?: (_: any) => unknown,
105
+ ) {
35
106
  return async (...req: unknown[]) => {
36
107
  const payload = ClientJsonRpc.buildPayload(
37
108
  rpcMethod,
@@ -42,6 +113,15 @@ export abstract class ClientJsonRpc implements Pick<Client, "sendTransaction"> {
42
113
  };
43
114
  }
44
115
 
116
+ /**
117
+ * Sends a JSON-RPC request to the server.
118
+ *
119
+ * @param payload - The JSON-RPC payload to send.
120
+ * @returns The result of the JSON-RPC request.
121
+ *
122
+ * @throws Will throw an error if the response ID does not match the request ID, or if the response contains an error.
123
+ */
124
+
45
125
  async send(payload: JsonRpcPayload) {
46
126
  const aborter = new AbortController();
47
127
  const abortTimer = setTimeout(() => aborter.abort(), this.timeout);
@@ -70,6 +150,14 @@ export abstract class ClientJsonRpc implements Pick<Client, "sendTransaction"> {
70
150
  return res.result;
71
151
  }
72
152
 
153
+ /**
154
+ * Builds a JSON-RPC payload for the given method and parameters.
155
+ *
156
+ * @param method - The JSON-RPC method name.
157
+ * @param req - The parameters for the JSON-RPC method.
158
+ * @returns The JSON-RPC payload.
159
+ */
160
+
73
161
  static buildPayload(method: string, req: unknown[]): JsonRpcPayload {
74
162
  return {
75
163
  id: Math.round(Math.random() * 10000),
@@ -0,0 +1,159 @@
1
+ import {
2
+ CellDep,
3
+ CellDepLike,
4
+ CellInput,
5
+ CellInputLike,
6
+ CellOutput,
7
+ CellOutputLike,
8
+ DepType,
9
+ DepTypeLike,
10
+ HashType,
11
+ HashTypeLike,
12
+ OutPoint,
13
+ OutPointLike,
14
+ Script,
15
+ ScriptLike,
16
+ Transaction,
17
+ TransactionLike,
18
+ depTypeFrom,
19
+ hashTypeFrom,
20
+ } from "../../ckb";
21
+ import { numToHex } from "../../num";
22
+ import { apply } from "../../utils";
23
+ import { ClientTransactionResponse, TransactionStatus } from "../clientTypes";
24
+ import {
25
+ JsonRpcCellDep,
26
+ JsonRpcCellInput,
27
+ JsonRpcCellOutput,
28
+ JsonRpcDepType,
29
+ JsonRpcHashType,
30
+ JsonRpcOutPoint,
31
+ JsonRpcScript,
32
+ JsonRpcTransaction,
33
+ } from "./types";
34
+
35
+ export class JsonRpcTransformers {
36
+ static toHashType(hashType: HashTypeLike): JsonRpcHashType {
37
+ return hashTypeFrom(hashType);
38
+ }
39
+ static fromHashType(hashType: JsonRpcHashType): HashType {
40
+ return hashType;
41
+ }
42
+ static toDepType(depType: DepTypeLike): JsonRpcDepType {
43
+ switch (depTypeFrom(depType)) {
44
+ case "code":
45
+ return "code";
46
+ case "depGroup":
47
+ return "dep_group";
48
+ }
49
+ }
50
+ static fromDepType(depType: JsonRpcDepType): DepType {
51
+ switch (depType) {
52
+ case "code":
53
+ return "code";
54
+ case "dep_group":
55
+ return "depGroup";
56
+ }
57
+ }
58
+ static toScript(scriptLike: ScriptLike): JsonRpcScript {
59
+ const script = Script.from(scriptLike);
60
+ return {
61
+ code_hash: script.codeHash,
62
+ hash_type: JsonRpcTransformers.toHashType(script.hashType),
63
+ args: script.args,
64
+ };
65
+ }
66
+ static fromScript(script: JsonRpcScript): Script {
67
+ return Script.from({
68
+ codeHash: script.code_hash,
69
+ hashType: JsonRpcTransformers.fromHashType(script.hash_type),
70
+ args: script.args,
71
+ });
72
+ }
73
+ static toOutPoint(outPointLike: OutPointLike): JsonRpcOutPoint {
74
+ const outPoint = OutPoint.from(outPointLike);
75
+ return {
76
+ index: numToHex(outPoint.index),
77
+ tx_hash: outPoint.txHash,
78
+ };
79
+ }
80
+ static fromOutPoint(outPoint: JsonRpcOutPoint): OutPoint {
81
+ return OutPoint.from({
82
+ index: outPoint.index,
83
+ txHash: outPoint.tx_hash,
84
+ });
85
+ }
86
+ static toCellInput(cellInput: CellInputLike): JsonRpcCellInput {
87
+ return {
88
+ previous_output: JsonRpcTransformers.toOutPoint(cellInput.previousOutput),
89
+ since: numToHex(cellInput.since),
90
+ };
91
+ }
92
+ static fromCellInput(cellInput: JsonRpcCellInput): CellInput {
93
+ return CellInput.from({
94
+ previousOutput: this.fromOutPoint(cellInput.previous_output),
95
+ since: cellInput.since,
96
+ });
97
+ }
98
+ static toCellOutput(cellOutput: CellOutputLike): JsonRpcCellOutput {
99
+ return {
100
+ capacity: numToHex(cellOutput.capacity),
101
+ lock: JsonRpcTransformers.toScript(cellOutput.lock),
102
+ type: apply(JsonRpcTransformers.toScript, cellOutput.type),
103
+ };
104
+ }
105
+ static fromCellOutput(cellOutput: JsonRpcCellOutput): CellOutput {
106
+ return CellOutput.from({
107
+ capacity: cellOutput.capacity,
108
+ lock: JsonRpcTransformers.fromScript(cellOutput.lock),
109
+ type: apply(JsonRpcTransformers.fromScript, cellOutput.type),
110
+ });
111
+ }
112
+ static toCellDep(cellDep: CellDepLike): JsonRpcCellDep {
113
+ return {
114
+ out_point: JsonRpcTransformers.toOutPoint(cellDep.outPoint),
115
+ dep_type: JsonRpcTransformers.toDepType(cellDep.depType),
116
+ };
117
+ }
118
+ static fromCellDep(cellDep: JsonRpcCellDep): CellDep {
119
+ return CellDep.from({
120
+ outPoint: JsonRpcTransformers.fromOutPoint(cellDep.out_point),
121
+ depType: JsonRpcTransformers.fromDepType(cellDep.dep_type),
122
+ });
123
+ }
124
+ static toTransaction(txLike: TransactionLike): JsonRpcTransaction {
125
+ const tx = Transaction.from(txLike);
126
+ return {
127
+ version: numToHex(tx.version),
128
+ cell_deps: tx.cellDeps.map((c) => JsonRpcTransformers.toCellDep(c)),
129
+ header_deps: tx.headerDeps,
130
+ inputs: tx.inputs.map((i) => JsonRpcTransformers.toCellInput(i)),
131
+ outputs: tx.outputs.map((o) => JsonRpcTransformers.toCellOutput(o)),
132
+ outputs_data: tx.outputsData,
133
+ witnesses: tx.witnesses,
134
+ };
135
+ }
136
+ static fromTransaction(tx: JsonRpcTransaction): Transaction {
137
+ return Transaction.from({
138
+ version: tx.version,
139
+ cellDeps: tx.cell_deps.map((c) => JsonRpcTransformers.fromCellDep(c)),
140
+ headerDeps: tx.header_deps,
141
+ inputs: tx.inputs.map((i) => JsonRpcTransformers.fromCellInput(i)),
142
+ outputs: tx.outputs.map((o) => JsonRpcTransformers.fromCellOutput(o)),
143
+ outputsData: tx.outputs_data,
144
+ witnesses: tx.witnesses,
145
+ });
146
+ }
147
+ static fromTransactionResponse({
148
+ tx_status: { status },
149
+ transaction,
150
+ }: {
151
+ tx_status: { status: TransactionStatus };
152
+ transaction: JsonRpcTransaction;
153
+ }): ClientTransactionResponse {
154
+ return {
155
+ transaction: JsonRpcTransformers.fromTransaction(transaction),
156
+ status,
157
+ };
158
+ }
159
+ }
@@ -0,0 +1,48 @@
1
+ import { Hex } from "../../hex";
2
+
3
+ export type JsonRpcPayload = {
4
+ id: number;
5
+ jsonrpc: "2.0";
6
+ method: string;
7
+ params: unknown[] | Record<string, unknown>;
8
+ };
9
+
10
+ export type JsonRpcHashType = "type" | "data" | "data1" | "data2";
11
+ export type JsonRpcDepType = "dep_group" | "code";
12
+
13
+ export type JsonRpcScript = {
14
+ code_hash: Hex;
15
+ hash_type: JsonRpcHashType;
16
+ args: Hex;
17
+ };
18
+
19
+ export type JsonRpcOutPoint = {
20
+ index: Hex;
21
+ tx_hash: Hex;
22
+ };
23
+
24
+ export type JsonRpcCellInput = {
25
+ previous_output: JsonRpcOutPoint;
26
+ since: Hex;
27
+ };
28
+
29
+ export type JsonRpcCellOutput = {
30
+ capacity: Hex;
31
+ lock: JsonRpcScript;
32
+ type?: JsonRpcScript;
33
+ };
34
+
35
+ export type JsonRpcCellDep = {
36
+ out_point: JsonRpcOutPoint;
37
+ dep_type: JsonRpcDepType;
38
+ };
39
+
40
+ export type JsonRpcTransaction = {
41
+ version: Hex;
42
+ cell_deps: JsonRpcCellDep[];
43
+ header_deps: Hex[];
44
+ inputs: JsonRpcCellInput[];
45
+ outputs: JsonRpcCellOutput[];
46
+ outputs_data: Hex[];
47
+ witnesses: Hex[];
48
+ };
@@ -1,8 +1,33 @@
1
+ /**
2
+ * Represents a fixed point value as a bigint.
3
+ */
4
+
1
5
  export type FixedPoint = bigint;
6
+
7
+ /**
8
+ * Represents a value that can be converted to a fixed point value.
9
+ * It can be a bigint, string, or number.
10
+ */
11
+
2
12
  export type FixedPointLike = bigint | string | number;
3
13
 
4
- export function fixedPointToString(val: FixedPoint, decimals = 8): string {
5
- const str = val.toString();
14
+ /**
15
+ * Converts a FixedPointLike value to its string representation with fixed-point decimals.
16
+ *
17
+ * @param val - The value to convert, which can be a bigint, string, or number.
18
+ * @param decimals - The number of decimal places for the fixed-point representation. Default is 8.
19
+ * @returns A string representing the fixed-point value.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const str = fixedPointToString(123456789n, 8); // Outputs "1.23456789"
24
+ * const strFromString = fixedPointToString("123456789", 8); // Outputs "1.23456789"
25
+ * const strFromNumber = fixedPointToString(123456789, 8); // Outputs "1.23456789"
26
+ * ```
27
+ */
28
+
29
+ export function fixedPointToString(val: FixedPointLike, decimals = 8): string {
30
+ const str = fixedPointFrom(val).toString();
6
31
  const l = str.length <= decimals ? "0" : str.slice(0, -decimals);
7
32
  const r = str.slice(-decimals).padStart(decimals, "0").replace(/0*$/, "");
8
33
  if (r === "") {
@@ -12,6 +37,21 @@ export function fixedPointToString(val: FixedPoint, decimals = 8): string {
12
37
  return `${l}.${r}`;
13
38
  }
14
39
 
40
+ /**
41
+ * Converts a FixedPointLike value to a FixedPoint (bigint) with fixed-point decimals.
42
+ *
43
+ * @param val - The value to convert, which can be a bigint, string, or number.
44
+ * @param decimals - The number of decimal places for the fixed-point representation. Default is 8.
45
+ * @returns A FixedPoint (bigint) representing the value with fixed-point decimals.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const fixedPoint = fixedPointFrom(1.23456789, 8); // Outputs 123456789n
50
+ * const fixedPointFromString = fixedPointFrom("1.23456789", 8); // Outputs 123456789n
51
+ * const fixedPointFromNumber = fixedPointFrom(1.23456789, 8); // Outputs 123456789n
52
+ * ```
53
+ */
54
+
15
55
  export function fixedPointFrom(val: FixedPointLike, decimals = 8): FixedPoint {
16
56
  if (typeof val === "bigint") {
17
57
  return val;
@@ -26,5 +66,15 @@ export function fixedPointFrom(val: FixedPointLike, decimals = 8): FixedPoint {
26
66
  return lVal + BigInt(r.slice(0, decimals).padEnd(decimals, "0"));
27
67
  }
28
68
 
69
+ /**
70
+ * Represents the fixed point value of zero as a bigint.
71
+ */
72
+
29
73
  export const Zero = 0n;
74
+
75
+ /**
76
+ * Represents the fixed point value of one as a FixedPoint (bigint).
77
+ * Equivalent to 1 in fixed-point representation with default decimals (8).
78
+ */
79
+
30
80
  export const One = fixedPointFrom("1");
@@ -4,7 +4,14 @@ import { Hex } from "../hex";
4
4
  import { CKB_BLAKE2B_PERSONAL } from "./advanced";
5
5
 
6
6
  export class Hasher {
7
- hasher: Blake2b;
7
+ private readonly hasher: Blake2b;
8
+
9
+ /**
10
+ * Creates an instance of Hasher.
11
+ *
12
+ * @param outLength - The output length of the hash in bytes. Default is 32.
13
+ * @param personal - The personal string for the Blake2b algorithm. Default is CKB_BLAKE2B_PERSONAL.
14
+ */
8
15
 
9
16
  constructor(outLength = 32, personal = CKB_BLAKE2B_PERSONAL) {
10
17
  this.hasher = blake2b(
@@ -15,16 +22,55 @@ export class Hasher {
15
22
  );
16
23
  }
17
24
 
25
+ /**
26
+ * Updates the hash with the given data.
27
+ *
28
+ * @param data - The data to update the hash with.
29
+ * @returns The current Hasher instance for chaining.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const hasher = new Hasher();
34
+ * hasher.update("some data").update("more data");
35
+ * const hash = hasher.digest();
36
+ * ```
37
+ */
38
+
18
39
  update(data: BytesLike): Hasher {
19
40
  this.hasher.update(bytesFrom(data));
20
41
  return this;
21
42
  }
22
43
 
44
+ /**
45
+ * Finalizes the hash and returns the digest as a hexadecimal string.
46
+ *
47
+ * @returns The hexadecimal string representation of the hash.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const hasher = new Hasher();
52
+ * hasher.update("some data");
53
+ * const hash = hasher.digest(); // Outputs something like "0x..."
54
+ * ```
55
+ */
56
+
23
57
  digest(): Hex {
24
58
  return `0x${this.hasher.digest("hex")}`;
25
59
  }
26
60
  }
27
61
 
62
+ /**
63
+ * Computes the CKB hash of the given data using the Blake2b algorithm.
64
+ *
65
+ * @param data - The data to hash.
66
+ * @returns The hexadecimal string representation of the hash.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const hash = ckbHash("some data"); // Outputs something like "0x..."
71
+ * ```
72
+ */
73
+
28
74
  export function ckbHash(data: BytesLike): Hex {
29
75
  return new Hasher().update(data).digest();
30
76
  }
package/src/hex/index.ts CHANGED
@@ -1,8 +1,27 @@
1
1
  import { bytesFrom, BytesLike, bytesTo } from "../bytes";
2
2
 
3
+ /**
4
+ * Represents a hexadecimal string prefixed with "0x".
5
+ */
3
6
  export type Hex = `0x${string}`;
7
+ /**
8
+ * Represents a value that can be converted to a hexadecimal string.
9
+ * It extends the BytesLike type.
10
+ */
4
11
  export type HexLike = BytesLike;
5
12
 
13
+ /**
14
+ * Converts a HexLike value to a Hex string.
15
+ *
16
+ * @param hex - The value to convert, which can be a string, Uint8Array, ArrayBuffer, or number array.
17
+ * @returns A Hex string representing the value.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const hexString = hexFrom("68656c6c6f"); // Outputs "0x68656c6c6f"
22
+ * const hexStringFromBytes = hexFrom(new Uint8Array([104, 101, 108, 108, 111])); // Outputs "0x68656c6c6f"
23
+ * ```
24
+ */
6
25
  export function hexFrom(hex: HexLike): Hex {
7
26
  return `0x${bytesTo(bytesFrom(hex), "hex")}`;
8
27
  }