@ckb-ccc/core 0.0.4-alpha.0 → 0.0.4-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/dist/address/address.advanced.d.ts +34 -0
- package/dist/address/address.advanced.d.ts.map +1 -1
- package/dist/address/address.advanced.js +41 -7
- package/dist/address/index.d.ts +36 -0
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +44 -5
- package/dist/bytes/advanced.d.ts +3 -0
- package/dist/bytes/advanced.d.ts.map +1 -1
- package/dist/bytes/index.d.ts +57 -0
- package/dist/bytes/index.d.ts.map +1 -1
- package/dist/bytes/index.js +57 -0
- package/dist/ckb/script.d.ts +98 -1
- package/dist/ckb/script.d.ts.map +1 -1
- package/dist/ckb/script.js +98 -1
- package/dist/ckb/transaction.d.ts +392 -1
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +403 -1
- package/dist/client/advanced.d.ts +1 -0
- package/dist/client/advanced.d.ts.map +1 -1
- package/dist/client/advanced.js +1 -0
- package/dist/client/client.d.ts +10 -8
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +18 -0
- package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.d.ts +3 -3
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.js +2 -2
- package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
- package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.d.ts +3 -3
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.js +2 -2
- package/dist/client/clientTypes.d.ts +6 -0
- package/dist/client/clientTypes.d.ts.map +1 -1
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -0
- package/dist/client/jsonRpc/advanced.d.ts +2 -87
- package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
- package/dist/client/jsonRpc/advanced.js +2 -65
- package/dist/client/jsonRpc/index.d.ts +60 -6
- package/dist/client/jsonRpc/index.d.ts.map +1 -1
- package/dist/client/jsonRpc/index.js +72 -7
- package/dist/client/jsonRpc/transformers.d.ts +28 -0
- package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
- package/dist/client/jsonRpc/transformers.js +122 -0
- package/dist/client/jsonRpc/types.d.ts +41 -0
- package/dist/client/jsonRpc/types.d.ts.map +1 -0
- package/dist/client/jsonRpc/types.js +1 -0
- package/dist/fixedPoint/index.d.ts +43 -1
- package/dist/fixedPoint/index.d.ts.map +1 -1
- package/dist/fixedPoint/index.js +39 -1
- package/dist/hasher/index.d.ts +43 -2
- package/dist/hasher/index.d.ts.map +1 -1
- package/dist/hasher/index.js +42 -0
- package/dist/hex/index.d.ts +19 -0
- package/dist/hex/index.d.ts.map +1 -1
- package/dist/hex/index.js +12 -0
- package/dist/num/index.d.ts +99 -0
- package/dist/num/index.d.ts.map +1 -1
- package/dist/num/index.js +92 -0
- package/dist/signer/btc/signerBtc.d.ts +40 -2
- package/dist/signer/btc/signerBtc.d.ts.map +1 -1
- package/dist/signer/btc/signerBtc.js +36 -4
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +48 -0
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist/signer/btc/signerBtcPublicKeyReadonly.js +53 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +47 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist/signer/ckb/signerCkbScriptReadonly.js +52 -0
- package/dist/signer/evm/signerEvm.d.ts +34 -2
- package/dist/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist/signer/evm/signerEvm.js +35 -4
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts +43 -1
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist/signer/evm/signerEvmAddressReadonly.js +47 -0
- package/dist/signer/helpers.d.ts +35 -2
- package/dist/signer/helpers.d.ts.map +1 -1
- package/dist/signer/helpers.js +67 -11
- package/dist/signer/signer.d.ts +98 -7
- package/dist/signer/signer.d.ts.map +1 -1
- package/dist/signer/signer.js +70 -3
- package/package.json +5 -5
- package/src/address/address.advanced.ts +43 -7
- package/src/address/index.ts +48 -6
- package/src/bytes/advanced.ts +12 -9
- package/src/bytes/index.ts +60 -0
- package/src/ckb/script.ts +111 -2
- package/src/ckb/transaction.ts +455 -2
- package/src/client/advanced.ts +1 -0
- package/src/client/client.ts +31 -8
- package/src/client/clientPublicMainnet.advanced.ts +5 -2
- package/src/client/clientPublicMainnet.ts +6 -4
- package/src/client/clientPublicTestnet.advanced.ts +5 -2
- package/src/client/clientPublicTestnet.ts +6 -4
- package/src/client/clientTypes.ts +13 -0
- package/src/client/index.ts +1 -0
- package/src/client/jsonRpc/advanced.ts +2 -93
- package/src/client/jsonRpc/index.ts +99 -11
- package/src/client/jsonRpc/transformers.ts +161 -0
- package/src/client/jsonRpc/types.ts +48 -0
- package/src/fixedPoint/index.ts +52 -2
- package/src/hasher/index.ts +47 -1
- package/src/hex/index.ts +19 -0
- package/src/num/index.ts +101 -0
- package/src/signer/btc/signerBtc.ts +47 -4
- package/src/signer/btc/signerBtcPublicKeyReadonly.ts +54 -0
- package/src/signer/ckb/signerCkbScriptReadonly.ts +54 -0
- package/src/signer/evm/signerEvm.ts +42 -4
- package/src/signer/evm/signerEvmAddressReadonly.ts +50 -1
- package/src/signer/helpers.ts +87 -11
- package/src/signer/signer.ts +109 -7
package/src/client/client.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Hex } from "../hex";
|
|
3
|
-
import {
|
|
1
|
+
import { Cell, OutPointLike, Script, TransactionLike } from "../ckb";
|
|
2
|
+
import { Hex, HexLike } from "../hex";
|
|
3
|
+
import { numFrom } from "../num";
|
|
4
|
+
import { ClientTransactionResponse, OutputsValidator } from "./clientTypes";
|
|
4
5
|
|
|
5
6
|
export enum KnownScript {
|
|
6
7
|
Secp256k1Blake160,
|
|
@@ -10,14 +11,36 @@ export enum KnownScript {
|
|
|
10
11
|
OmniLock,
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
export
|
|
14
|
-
getUrl(): string;
|
|
14
|
+
export abstract class Client {
|
|
15
|
+
abstract getUrl(): string;
|
|
15
16
|
|
|
16
|
-
getAddressPrefix(): Promise<string>;
|
|
17
|
-
getKnownScript(
|
|
17
|
+
abstract getAddressPrefix(): Promise<string>;
|
|
18
|
+
abstract getKnownScript(
|
|
19
|
+
script: KnownScript,
|
|
20
|
+
): Promise<Pick<Script, "codeHash" | "hashType">>;
|
|
18
21
|
|
|
19
|
-
sendTransaction(
|
|
22
|
+
abstract sendTransaction(
|
|
20
23
|
transaction: TransactionLike,
|
|
21
24
|
validator?: OutputsValidator,
|
|
22
25
|
): Promise<Hex>;
|
|
26
|
+
abstract getTransaction(
|
|
27
|
+
txHash: HexLike,
|
|
28
|
+
): Promise<ClientTransactionResponse | null>;
|
|
29
|
+
|
|
30
|
+
async getCell(outPoint: OutPointLike): Promise<Cell | null> {
|
|
31
|
+
const transaction = await this.getTransaction(outPoint.txHash);
|
|
32
|
+
if (!transaction) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const index = Number(numFrom(outPoint.index));
|
|
37
|
+
if (index >= transaction.transaction.outputs.length) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return Cell.from({
|
|
42
|
+
cellOutput: transaction.transaction.outputs[index],
|
|
43
|
+
outputData: transaction.transaction.outputsData[index] ?? "0x",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
23
46
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Script } from "../ckb";
|
|
2
2
|
import { KnownScript } from "./client";
|
|
3
3
|
|
|
4
|
-
export const MAINNET_SCRIPTS: Record<
|
|
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,18 +1,20 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
5
5
|
|
|
6
6
|
export class ClientPublicMainnet extends ClientJsonRpc {
|
|
7
|
-
constructor(timeout?: number) {
|
|
8
|
-
super(
|
|
7
|
+
constructor(url = "https://mainnet.ckbapp.dev/", timeout?: number) {
|
|
8
|
+
super(url, timeout);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
async getAddressPrefix(): Promise<string> {
|
|
12
12
|
return "ckb";
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
async getKnownScript(
|
|
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 {
|
|
1
|
+
import { Script } from "../ckb";
|
|
2
2
|
import { KnownScript } from "./client";
|
|
3
3
|
|
|
4
|
-
export const TESTNET_SCRIPTS: Record<
|
|
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,18 +1,20 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
5
5
|
|
|
6
6
|
export class ClientPublicTestnet extends ClientJsonRpc {
|
|
7
|
-
constructor(timeout?: number) {
|
|
8
|
-
super(
|
|
7
|
+
constructor(url = "https://testnet.ckbapp.dev/", timeout?: number) {
|
|
8
|
+
super(url, timeout);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
async getAddressPrefix(): Promise<string> {
|
|
12
12
|
return "ckt";
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
async getKnownScript(
|
|
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
|
+
};
|
package/src/client/index.ts
CHANGED
|
@@ -1,93 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
|
3
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.transactionFrom],
|
|
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.transactionResponseTo,
|
|
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,161 @@
|
|
|
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 hashTypeFrom(hashType: HashTypeLike): JsonRpcHashType {
|
|
37
|
+
return hashTypeFrom(hashType);
|
|
38
|
+
}
|
|
39
|
+
static hashTypeTo(hashType: JsonRpcHashType): HashType {
|
|
40
|
+
return hashType;
|
|
41
|
+
}
|
|
42
|
+
static depTypeFrom(depType: DepTypeLike): JsonRpcDepType {
|
|
43
|
+
switch (depTypeFrom(depType)) {
|
|
44
|
+
case "code":
|
|
45
|
+
return "code";
|
|
46
|
+
case "depGroup":
|
|
47
|
+
return "dep_group";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
static depTypeTo(depType: JsonRpcDepType): DepType {
|
|
51
|
+
switch (depType) {
|
|
52
|
+
case "code":
|
|
53
|
+
return "code";
|
|
54
|
+
case "dep_group":
|
|
55
|
+
return "depGroup";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
static scriptFrom(scriptLike: ScriptLike): JsonRpcScript {
|
|
59
|
+
const script = Script.from(scriptLike);
|
|
60
|
+
return {
|
|
61
|
+
code_hash: script.codeHash,
|
|
62
|
+
hash_type: JsonRpcTransformers.hashTypeFrom(script.hashType),
|
|
63
|
+
args: script.args,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
static scriptTo(script: JsonRpcScript): Script {
|
|
67
|
+
return Script.from({
|
|
68
|
+
codeHash: script.code_hash,
|
|
69
|
+
hashType: JsonRpcTransformers.hashTypeTo(script.hash_type),
|
|
70
|
+
args: script.args,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
static outPointFrom(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 outPointTo(outPoint: JsonRpcOutPoint): OutPoint {
|
|
81
|
+
return OutPoint.from({
|
|
82
|
+
index: outPoint.index,
|
|
83
|
+
txHash: outPoint.tx_hash,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
static cellInputFrom(cellInput: CellInputLike): JsonRpcCellInput {
|
|
87
|
+
return {
|
|
88
|
+
previous_output: JsonRpcTransformers.outPointFrom(
|
|
89
|
+
cellInput.previousOutput,
|
|
90
|
+
),
|
|
91
|
+
since: numToHex(cellInput.since),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
static cellInputTo(cellInput: JsonRpcCellInput): CellInput {
|
|
95
|
+
return CellInput.from({
|
|
96
|
+
previousOutput: JsonRpcTransformers.outPointTo(cellInput.previous_output),
|
|
97
|
+
since: cellInput.since,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
static cellOutputFrom(cellOutput: CellOutputLike): JsonRpcCellOutput {
|
|
101
|
+
return {
|
|
102
|
+
capacity: numToHex(cellOutput.capacity),
|
|
103
|
+
lock: JsonRpcTransformers.scriptFrom(cellOutput.lock),
|
|
104
|
+
type: apply(JsonRpcTransformers.scriptFrom, cellOutput.type),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
static cellOutputTo(cellOutput: JsonRpcCellOutput): CellOutput {
|
|
108
|
+
return CellOutput.from({
|
|
109
|
+
capacity: cellOutput.capacity,
|
|
110
|
+
lock: JsonRpcTransformers.scriptTo(cellOutput.lock),
|
|
111
|
+
type: apply(JsonRpcTransformers.scriptTo, cellOutput.type),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
static cellDepFrom(cellDep: CellDepLike): JsonRpcCellDep {
|
|
115
|
+
return {
|
|
116
|
+
out_point: JsonRpcTransformers.outPointFrom(cellDep.outPoint),
|
|
117
|
+
dep_type: JsonRpcTransformers.depTypeFrom(cellDep.depType),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
static cellDepTo(cellDep: JsonRpcCellDep): CellDep {
|
|
121
|
+
return CellDep.from({
|
|
122
|
+
outPoint: JsonRpcTransformers.outPointTo(cellDep.out_point),
|
|
123
|
+
depType: JsonRpcTransformers.depTypeTo(cellDep.dep_type),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
static transactionFrom(txLike: TransactionLike): JsonRpcTransaction {
|
|
127
|
+
const tx = Transaction.from(txLike);
|
|
128
|
+
return {
|
|
129
|
+
version: numToHex(tx.version),
|
|
130
|
+
cell_deps: tx.cellDeps.map((c) => JsonRpcTransformers.cellDepFrom(c)),
|
|
131
|
+
header_deps: tx.headerDeps,
|
|
132
|
+
inputs: tx.inputs.map((i) => JsonRpcTransformers.cellInputFrom(i)),
|
|
133
|
+
outputs: tx.outputs.map((o) => JsonRpcTransformers.cellOutputFrom(o)),
|
|
134
|
+
outputs_data: tx.outputsData,
|
|
135
|
+
witnesses: tx.witnesses,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
static transactionTo(tx: JsonRpcTransaction): Transaction {
|
|
139
|
+
return Transaction.from({
|
|
140
|
+
version: tx.version,
|
|
141
|
+
cellDeps: tx.cell_deps.map((c) => JsonRpcTransformers.cellDepTo(c)),
|
|
142
|
+
headerDeps: tx.header_deps,
|
|
143
|
+
inputs: tx.inputs.map((i) => JsonRpcTransformers.cellInputTo(i)),
|
|
144
|
+
outputs: tx.outputs.map((o) => JsonRpcTransformers.cellOutputTo(o)),
|
|
145
|
+
outputsData: tx.outputs_data,
|
|
146
|
+
witnesses: tx.witnesses,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
static transactionResponseTo({
|
|
150
|
+
tx_status: { status },
|
|
151
|
+
transaction,
|
|
152
|
+
}: {
|
|
153
|
+
tx_status: { status: TransactionStatus };
|
|
154
|
+
transaction: JsonRpcTransaction;
|
|
155
|
+
}): ClientTransactionResponse {
|
|
156
|
+
return {
|
|
157
|
+
transaction: JsonRpcTransformers.transactionTo(transaction),
|
|
158
|
+
status,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -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
|
+
};
|
package/src/fixedPoint/index.ts
CHANGED
|
@@ -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
|
-
|
|
5
|
-
|
|
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");
|