@ckb-ccc/core 0.0.13-alpha.0 → 0.0.13-alpha.1

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 (74) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/ckb/transaction.d.ts.map +1 -1
  3. package/dist/ckb/transaction.js +17 -10
  4. package/dist/client/advanced.d.ts +1 -0
  5. package/dist/client/advanced.d.ts.map +1 -1
  6. package/dist/client/advanced.js +1 -0
  7. package/dist/client/client.d.ts.map +1 -1
  8. package/dist/client/client.js +8 -2
  9. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  10. package/dist/client/clientPublicMainnet.js +5 -2
  11. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  12. package/dist/client/clientPublicTestnet.js +5 -2
  13. package/dist/client/clientTypes.d.ts +1 -1
  14. package/dist/client/clientTypes.d.ts.map +1 -1
  15. package/dist/client/jsonRpc/index.d.ts +5 -4
  16. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  17. package/dist/client/jsonRpc/index.js +5 -15
  18. package/dist/client/jsonRpc/types.d.ts +0 -6
  19. package/dist/client/jsonRpc/types.d.ts.map +1 -1
  20. package/dist/client/transports/advanced.d.ts +9 -0
  21. package/dist/client/transports/advanced.d.ts.map +1 -0
  22. package/dist/client/transports/advanced.js +11 -0
  23. package/dist/client/transports/http.d.ts +8 -0
  24. package/dist/client/transports/http.d.ts.map +1 -0
  25. package/dist/client/transports/http.js +20 -0
  26. package/dist/client/transports/transport.d.ts +10 -0
  27. package/dist/client/transports/transport.d.ts.map +1 -0
  28. package/dist/client/transports/transport.js +1 -0
  29. package/dist/client/transports/webSocket.d.ts +10 -0
  30. package/dist/client/transports/webSocket.d.ts.map +1 -0
  31. package/dist/client/transports/webSocket.js +61 -0
  32. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  33. package/dist.commonjs/ckb/transaction.js +17 -10
  34. package/dist.commonjs/client/advanced.d.ts +1 -0
  35. package/dist.commonjs/client/advanced.d.ts.map +1 -1
  36. package/dist.commonjs/client/advanced.js +1 -0
  37. package/dist.commonjs/client/client.d.ts.map +1 -1
  38. package/dist.commonjs/client/client.js +8 -2
  39. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  40. package/dist.commonjs/client/clientPublicMainnet.js +5 -2
  41. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  42. package/dist.commonjs/client/clientPublicTestnet.js +5 -2
  43. package/dist.commonjs/client/clientTypes.d.ts +1 -1
  44. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  45. package/dist.commonjs/client/jsonRpc/index.d.ts +5 -4
  46. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  47. package/dist.commonjs/client/jsonRpc/index.js +15 -28
  48. package/dist.commonjs/client/jsonRpc/types.d.ts +0 -6
  49. package/dist.commonjs/client/jsonRpc/types.d.ts.map +1 -1
  50. package/dist.commonjs/client/transports/advanced.d.ts +9 -0
  51. package/dist.commonjs/client/transports/advanced.d.ts.map +1 -0
  52. package/dist.commonjs/client/transports/advanced.js +29 -0
  53. package/dist.commonjs/client/transports/http.d.ts +8 -0
  54. package/dist.commonjs/client/transports/http.d.ts.map +1 -0
  55. package/dist.commonjs/client/transports/http.js +24 -0
  56. package/dist.commonjs/client/transports/transport.d.ts +10 -0
  57. package/dist.commonjs/client/transports/transport.d.ts.map +1 -0
  58. package/dist.commonjs/client/transports/transport.js +2 -0
  59. package/dist.commonjs/client/transports/webSocket.d.ts +10 -0
  60. package/dist.commonjs/client/transports/webSocket.d.ts.map +1 -0
  61. package/dist.commonjs/client/transports/webSocket.js +65 -0
  62. package/package.json +1 -1
  63. package/src/ckb/transaction.ts +24 -16
  64. package/src/client/advanced.ts +1 -0
  65. package/src/client/client.ts +9 -2
  66. package/src/client/clientPublicMainnet.ts +8 -2
  67. package/src/client/clientPublicTestnet.ts +8 -2
  68. package/src/client/clientTypes.ts +1 -0
  69. package/src/client/jsonRpc/index.ts +13 -19
  70. package/src/client/jsonRpc/types.ts +0 -7
  71. package/src/client/transports/advanced.ts +14 -0
  72. package/src/client/transports/http.ts +26 -0
  73. package/src/client/transports/transport.ts +10 -0
  74. package/src/client/transports/webSocket.ts +81 -0
@@ -8,11 +8,17 @@ import { ClientJsonRpc } from "./jsonRpc/index.js";
8
8
  */
9
9
  export class ClientPublicMainnet extends ClientJsonRpc {
10
10
  constructor(
11
- url = "https://mainnet.ckb.dev/",
11
+ url?: string,
12
12
  timeout?: number,
13
13
  private readonly scripts = MAINNET_SCRIPTS,
14
14
  ) {
15
- super(url, timeout);
15
+ super(
16
+ url ??
17
+ (typeof WebSocket !== "undefined"
18
+ ? "wss://mainnet.ckb.dev/ws"
19
+ : "https://mainnet.ckb.dev/"),
20
+ timeout,
21
+ );
16
22
  }
17
23
 
18
24
  get addressPrefix(): string {
@@ -8,11 +8,17 @@ import { ClientJsonRpc } from "./jsonRpc/index.js";
8
8
  */
9
9
  export class ClientPublicTestnet extends ClientJsonRpc {
10
10
  constructor(
11
- url = "https://testnet.ckb.dev/",
11
+ url?: string,
12
12
  timeout?: number,
13
13
  private readonly scripts = TESTNET_SCRIPTS,
14
14
  ) {
15
- super(url, timeout);
15
+ super(
16
+ url ??
17
+ (typeof WebSocket !== "undefined"
18
+ ? "wss://testnet.ckb.dev/ws"
19
+ : "https://testnet.ckb.dev/"),
20
+ timeout,
21
+ );
16
22
  }
17
23
 
18
24
  get addressPrefix(): string {
@@ -17,6 +17,7 @@ export type OutputsValidator = "passthrough" | "well_known_scripts_only";
17
17
  * @public
18
18
  */
19
19
  export type TransactionStatus =
20
+ | "sent"
20
21
  | "pending"
21
22
  | "proposed"
22
23
  | "committed"
@@ -1,4 +1,3 @@
1
- import fetch from "cross-fetch";
2
1
  import { apply } from "../../barrel.js";
3
2
  import { TransactionLike } from "../../ckb/index.js";
4
3
  import { Hex, HexLike, hexFrom } from "../../hex/index.js";
@@ -10,7 +9,12 @@ import {
10
9
  ClientTransactionResponse,
11
10
  OutputsValidator,
12
11
  } from "../clientTypes.js";
13
- import { JsonRpcPayload, JsonRpcTransformers } from "./advanced.js";
12
+ import {
13
+ JsonRpcPayload,
14
+ Transport,
15
+ transportFromUri,
16
+ } from "../transports/advanced.js";
17
+ import { JsonRpcTransformers } from "./advanced.js";
14
18
 
15
19
  /**
16
20
  * Applies a transformation function to a value if the transformer is provided.
@@ -42,18 +46,22 @@ async function transform(
42
46
  */
43
47
 
44
48
  export abstract class ClientJsonRpc extends Client {
49
+ private readonly transport: Transport;
50
+
45
51
  /**
46
52
  * Creates an instance of ClientJsonRpc.
47
53
  *
48
54
  * @param url_ - The URL of the JSON-RPC server.
49
- * @param timeout - The timeout for requests in milliseconds, default is 30000.
55
+ * @param timeout - The timeout for requests in milliseconds
50
56
  */
51
57
 
52
58
  constructor(
53
59
  private readonly url_: string,
54
- private readonly timeout = 30000,
60
+ private readonly timeout?: number,
55
61
  ) {
56
62
  super();
63
+
64
+ this.transport = transportFromUri(url_);
57
65
  }
58
66
 
59
67
  /**
@@ -232,22 +240,8 @@ export abstract class ClientJsonRpc extends Client {
232
240
  *
233
241
  * @throws Will throw an error if the response ID does not match the request ID, or if the response contains an error.
234
242
  */
235
-
236
243
  async send(payload: JsonRpcPayload): Promise<unknown> {
237
- const aborter = new AbortController();
238
- const abortTimer = setTimeout(() => aborter.abort(), this.timeout);
239
-
240
- const raw = await fetch(this.url, {
241
- method: "POST",
242
- headers: {
243
- "content-type": "application/json",
244
- },
245
- body: JSON.stringify(payload),
246
- signal: aborter.signal,
247
- });
248
- clearTimeout(abortTimer);
249
-
250
- const res = (await raw.json()) as {
244
+ const res = (await this.transport.request(payload)) as {
251
245
  id: number;
252
246
  error: unknown;
253
247
  result: unknown;
@@ -1,12 +1,5 @@
1
1
  import { Hex } from "../../hex/index.js";
2
2
 
3
- export type JsonRpcPayload = {
4
- id: number;
5
- jsonrpc: "2.0";
6
- method: string;
7
- params: unknown[] | Record<string, unknown>;
8
- };
9
-
10
3
  export type JsonRpcHashType = "type" | "data" | "data1" | "data2";
11
4
  export type JsonRpcDepType = "dep_group" | "code";
12
5
 
@@ -0,0 +1,14 @@
1
+ import { TransportHttp } from "./http.js";
2
+ import { TransportWebSocket } from "./webSocket.js";
3
+
4
+ export * from "./http.js";
5
+ export * from "./transport.js";
6
+ export * from "./webSocket.js";
7
+
8
+ export function transportFromUri(uri: string, config?: { timeout?: number }) {
9
+ if (uri.startsWith("wss://")) {
10
+ return new TransportWebSocket(uri, config?.timeout);
11
+ }
12
+
13
+ return new TransportHttp(uri, config?.timeout);
14
+ }
@@ -0,0 +1,26 @@
1
+ import { JsonRpcPayload, Transport } from "./transport.js";
2
+
3
+ export class TransportHttp implements Transport {
4
+ constructor(
5
+ private readonly url: string,
6
+ private readonly timeout = 30000,
7
+ ) {}
8
+
9
+ async request(payload: JsonRpcPayload) {
10
+ const aborter = new AbortController();
11
+ const abortTimer = setTimeout(() => aborter.abort(), this.timeout);
12
+
13
+ const raw = await (
14
+ await fetch(this.url, {
15
+ method: "POST",
16
+ headers: {
17
+ "content-type": "application/json",
18
+ },
19
+ body: JSON.stringify(payload),
20
+ signal: aborter.signal,
21
+ })
22
+ ).json();
23
+ clearTimeout(abortTimer);
24
+ return raw;
25
+ }
26
+ }
@@ -0,0 +1,10 @@
1
+ export type JsonRpcPayload = {
2
+ id: number;
3
+ jsonrpc: "2.0";
4
+ method: string;
5
+ params: unknown[] | Record<string, unknown>;
6
+ };
7
+
8
+ export interface Transport {
9
+ request(data: JsonRpcPayload): Promise<unknown>;
10
+ }
@@ -0,0 +1,81 @@
1
+ import { JsonRpcPayload, Transport } from "./transport.js";
2
+
3
+ export class TransportWebSocket implements Transport {
4
+ private ongoing: Map<
5
+ number,
6
+ [
7
+ (response: unknown) => unknown,
8
+ (error: unknown) => unknown,
9
+ ReturnType<typeof setTimeout>,
10
+ ]
11
+ > = new Map();
12
+ private socket?: Promise<WebSocket>;
13
+
14
+ constructor(
15
+ private readonly url: string,
16
+ private readonly timeout = 30000,
17
+ ) {}
18
+
19
+ async request(data: JsonRpcPayload) {
20
+ const socket = (() => {
21
+ if (this.socket) {
22
+ return this.socket;
23
+ }
24
+
25
+ const socket = new WebSocket(this.url);
26
+ const onMessage = ({ data }: { data: string }) => {
27
+ const res = JSON.parse(data);
28
+ if (typeof res !== "object" || res === null) {
29
+ throw new Error(`Unknown response ${data}`);
30
+ }
31
+
32
+ const req = this.ongoing.get(res.id);
33
+ if (!req) {
34
+ return;
35
+ }
36
+ clearTimeout(req[2]);
37
+ this.ongoing.delete(res.id);
38
+
39
+ req[0](res);
40
+ };
41
+ const onClose = (error?: unknown) => {
42
+ this.socket = undefined;
43
+ this.ongoing.forEach(([_, onError]) =>
44
+ onError(error ?? new Error("Connection closed")),
45
+ );
46
+ this.ongoing.clear();
47
+ };
48
+
49
+ socket.onclose = onClose;
50
+ socket.onerror = onClose;
51
+ socket.onmessage = onMessage;
52
+
53
+ this.socket = new Promise((resolve) => {
54
+ if (socket.readyState === socket.CONNECTING) {
55
+ socket.onopen = resolve;
56
+ } else {
57
+ resolve(undefined);
58
+ }
59
+ }).then(() => socket);
60
+ return this.socket;
61
+ })();
62
+
63
+ return new Promise((resolve, reject) => {
64
+ this.ongoing.set(data.id, [
65
+ resolve,
66
+ reject,
67
+ setTimeout(() => reject(new Error("Request timeout")), this.timeout),
68
+ ]);
69
+ socket.then((socket) => {
70
+ if (
71
+ socket.readyState === socket.CLOSED ||
72
+ socket.readyState === socket.CLOSING
73
+ ) {
74
+ reject(new Error("Connection closed"));
75
+ } else {
76
+ socket.send(JSON.stringify(data));
77
+ }
78
+ });
79
+ });
80
+ }
81
+ }