@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.
Files changed (114) hide show
  1. package/README.md +37 -0
  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/advanced.d.ts +1 -0
  20. package/dist/client/advanced.d.ts.map +1 -1
  21. package/dist/client/advanced.js +1 -0
  22. package/dist/client/client.d.ts +10 -8
  23. package/dist/client/client.d.ts.map +1 -1
  24. package/dist/client/client.js +18 -0
  25. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  26. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  27. package/dist/client/clientPublicMainnet.d.ts +3 -3
  28. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  29. package/dist/client/clientPublicMainnet.js +2 -2
  30. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  31. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  32. package/dist/client/clientPublicTestnet.d.ts +3 -3
  33. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  34. package/dist/client/clientPublicTestnet.js +2 -2
  35. package/dist/client/clientTypes.d.ts +6 -0
  36. package/dist/client/clientTypes.d.ts.map +1 -1
  37. package/dist/client/index.d.ts +1 -0
  38. package/dist/client/index.d.ts.map +1 -1
  39. package/dist/client/index.js +1 -0
  40. package/dist/client/jsonRpc/advanced.d.ts +2 -87
  41. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  42. package/dist/client/jsonRpc/advanced.js +2 -65
  43. package/dist/client/jsonRpc/index.d.ts +60 -6
  44. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  45. package/dist/client/jsonRpc/index.js +72 -7
  46. package/dist/client/jsonRpc/transformers.d.ts +28 -0
  47. package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
  48. package/dist/client/jsonRpc/transformers.js +122 -0
  49. package/dist/client/jsonRpc/types.d.ts +41 -0
  50. package/dist/client/jsonRpc/types.d.ts.map +1 -0
  51. package/dist/client/jsonRpc/types.js +1 -0
  52. package/dist/fixedPoint/index.d.ts +43 -1
  53. package/dist/fixedPoint/index.d.ts.map +1 -1
  54. package/dist/fixedPoint/index.js +39 -1
  55. package/dist/hasher/index.d.ts +43 -2
  56. package/dist/hasher/index.d.ts.map +1 -1
  57. package/dist/hasher/index.js +42 -0
  58. package/dist/hex/index.d.ts +19 -0
  59. package/dist/hex/index.d.ts.map +1 -1
  60. package/dist/hex/index.js +12 -0
  61. package/dist/num/index.d.ts +99 -0
  62. package/dist/num/index.d.ts.map +1 -1
  63. package/dist/num/index.js +92 -0
  64. package/dist/signer/btc/signerBtc.d.ts +40 -2
  65. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  66. package/dist/signer/btc/signerBtc.js +36 -4
  67. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +48 -0
  68. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  69. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +53 -0
  70. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +47 -0
  71. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  72. package/dist/signer/ckb/signerCkbScriptReadonly.js +52 -0
  73. package/dist/signer/evm/signerEvm.d.ts +34 -2
  74. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  75. package/dist/signer/evm/signerEvm.js +35 -4
  76. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +43 -1
  77. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  78. package/dist/signer/evm/signerEvmAddressReadonly.js +47 -0
  79. package/dist/signer/helpers.d.ts +35 -2
  80. package/dist/signer/helpers.d.ts.map +1 -1
  81. package/dist/signer/helpers.js +67 -11
  82. package/dist/signer/signer.d.ts +98 -7
  83. package/dist/signer/signer.d.ts.map +1 -1
  84. package/dist/signer/signer.js +70 -3
  85. package/package.json +5 -5
  86. package/src/address/address.advanced.ts +43 -7
  87. package/src/address/index.ts +48 -6
  88. package/src/bytes/advanced.ts +12 -9
  89. package/src/bytes/index.ts +60 -0
  90. package/src/ckb/script.ts +111 -2
  91. package/src/ckb/transaction.ts +455 -2
  92. package/src/client/advanced.ts +1 -0
  93. package/src/client/client.ts +31 -8
  94. package/src/client/clientPublicMainnet.advanced.ts +5 -2
  95. package/src/client/clientPublicMainnet.ts +6 -4
  96. package/src/client/clientPublicTestnet.advanced.ts +5 -2
  97. package/src/client/clientPublicTestnet.ts +6 -4
  98. package/src/client/clientTypes.ts +13 -0
  99. package/src/client/index.ts +1 -0
  100. package/src/client/jsonRpc/advanced.ts +2 -93
  101. package/src/client/jsonRpc/index.ts +99 -11
  102. package/src/client/jsonRpc/transformers.ts +161 -0
  103. package/src/client/jsonRpc/types.ts +48 -0
  104. package/src/fixedPoint/index.ts +52 -2
  105. package/src/hasher/index.ts +47 -1
  106. package/src/hex/index.ts +19 -0
  107. package/src/num/index.ts +101 -0
  108. package/src/signer/btc/signerBtc.ts +47 -4
  109. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +54 -0
  110. package/src/signer/ckb/signerCkbScriptReadonly.ts +54 -0
  111. package/src/signer/evm/signerEvm.ts +42 -4
  112. package/src/signer/evm/signerEvmAddressReadonly.ts +50 -1
  113. package/src/signer/helpers.ts +87 -11
  114. package/src/signer/signer.ts +109 -7
@@ -1,34 +1,125 @@
1
1
  import { Address } from "../address";
2
2
  import { BytesLike } from "../bytes";
3
- import { Transaction } from "../ckb";
3
+ import { Transaction, TransactionLike } from "../ckb";
4
4
  import { Client } from "../client";
5
5
  import { Hex } from "../hex";
6
+ /**
7
+ * An abstract class representing a generic signer.
8
+ * This class provides methods to connect, get addresses, and sign transactions.
9
+ */
6
10
  export declare abstract class Signer {
7
- readonly client: Client;
8
- constructor(client: Client);
11
+ protected client_: Client;
12
+ constructor(client_: Client);
13
+ get client(): Client;
14
+ /**
15
+ * Construct a new signer with the client replaced.
16
+ *
17
+ * @returns A promise that resolves the new Signer.
18
+ */
19
+ abstract replaceClient(client: Client): Promise<Signer>;
20
+ /**
21
+ * Connects to the signer.
22
+ *
23
+ * @returns A promise that resolves when the connection is complete.
24
+ */
9
25
  abstract connect(): Promise<void>;
26
+ /**
27
+ * Check if the signer is connected.
28
+ *
29
+ * @returns A promise that resolves the connection status.
30
+ */
31
+ abstract isConnected(): Promise<boolean>;
32
+ /**
33
+ * Gets the internal address associated with the signer.
34
+ *
35
+ * @returns A promise that resolves to a string representing the internal address.
36
+ */
10
37
  abstract getInternalAddress(): Promise<string>;
38
+ /**
39
+ * Gets an array of Address objects associated with the signer.
40
+ *
41
+ * @returns A promise that resolves to an array of Address objects.
42
+ */
11
43
  abstract getAddressObjs(): Promise<Address[]>;
44
+ /**
45
+ * Gets the recommended Address object for the signer.
46
+ *
47
+ * @param _preference - Optional preference parameter.
48
+ * @returns A promise that resolves to the recommended Address object.
49
+ */
12
50
  getRecommendedAddressObj(_preference?: unknown): Promise<Address>;
51
+ /**
52
+ * Gets the recommended address for the signer as a string.
53
+ *
54
+ * @param preference - Optional preference parameter.
55
+ * @returns A promise that resolves to the recommended address as a string.
56
+ */
13
57
  getRecommendedAddress(preference?: unknown): Promise<string>;
58
+ /**
59
+ * Gets an array of addresses associated with the signer as strings.
60
+ *
61
+ * @returns A promise that resolves to an array of addresses as strings.
62
+ */
14
63
  getAddresses(): Promise<string[]>;
64
+ /**
65
+ * Signs a message. This method is not implemented and should be overridden by subclasses.
66
+ *
67
+ * @param _ - The message to sign, as a string or BytesLike object.
68
+ * @returns A promise that resolves to the signed message as a string.
69
+ * @throws Will throw an error if not implemented.
70
+ */
15
71
  signMessage(_: string | BytesLike): Promise<string>;
16
- sendTransaction(tx: Transaction): Promise<Hex>;
17
- signTransaction(tx: Transaction): Promise<Transaction>;
18
- signOnlyTransaction(_: Transaction): Promise<Transaction>;
72
+ /**
73
+ * Sends a transaction after signing it.
74
+ *
75
+ * @param tx - The transaction to send, represented as a TransactionLike object.
76
+ * @returns A promise that resolves to the transaction hash as a Hex string.
77
+ */
78
+ sendTransaction(tx: TransactionLike): Promise<Hex>;
79
+ /**
80
+ * Signs a transaction.
81
+ *
82
+ * @param tx - The transaction to sign, represented as a TransactionLike object.
83
+ * @returns A promise that resolves to the signed Transaction object.
84
+ */
85
+ signTransaction(tx: TransactionLike): Promise<Transaction>;
86
+ /**
87
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
88
+ *
89
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
90
+ * @returns A promise that resolves to the prepared Transaction object.
91
+ * @throws Will throw an error if not implemented.
92
+ */
93
+ prepareTransaction(_: TransactionLike): Promise<Transaction>;
94
+ /**
95
+ * Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
96
+ *
97
+ * @param _ - The transaction to sign, represented as a TransactionLike object.
98
+ * @returns A promise that resolves to the signed Transaction object.
99
+ * @throws Will throw an error if not implemented.
100
+ */
101
+ signOnlyTransaction(_: TransactionLike): Promise<Transaction>;
19
102
  }
103
+ /**
104
+ * An enumeration of signer types.
105
+ */
20
106
  export declare enum SignerType {
21
107
  EVM = "EVM",
22
108
  BTC = "BTC"
23
109
  }
110
+ /**
111
+ * A class representing information about a signer, including its type and the signer instance.
112
+ */
24
113
  export declare class SignerInfo {
25
114
  type: SignerType;
26
115
  signer: Signer;
27
116
  constructor(type: SignerType, signer: Signer);
28
117
  }
118
+ /**
119
+ * Represents a wallet with a name, icon, and an array of signer information.
120
+ */
29
121
  export type Wallet = {
30
122
  name: string;
31
123
  icon: string;
32
- signers: SignerInfo[];
33
124
  };
34
125
  //# sourceMappingURL=signer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjE,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAG5D,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAMvC,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAI7C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAI9C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D,mBAAmB,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CAG1D;AAED,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED,qBAAa,UAAU;IAEZ,IAAI,EAAE,UAAU;IAChB,MAAM,EAAE,MAAM;gBADd,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM;CAExB;AAED,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,EAAE,CAAC;CACvB,CAAC"}
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B;;;GAGG;AACH,8BAAsB,MAAM;IACd,SAAS,CAAC,OAAO,EAAE,MAAM;gBAAf,OAAO,EAAE,MAAM;IAErC,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAEvD;;;;OAIG;IACH,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAExC;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C;;;;OAIG;IACH,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAE7C;;;;;OAKG;IACG,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIvE;;;;;OAKG;IACG,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlE;;;;OAIG;IACG,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAMvC;;;;;;OAMG;IACH,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAInD;;;;;OAKG;IACG,eAAe,CAAC,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC;IAIxD;;;;;OAKG;IACG,eAAe,CAAC,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAKhE;;;;;;OAMG;IACH,kBAAkB,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D;;;;;;OAMG;IACH,mBAAmB,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;CAG9D;AAED;;GAEG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED;;GAEG;AACH,qBAAa,UAAU;IAEZ,IAAI,EAAE,UAAU;IAChB,MAAM,EAAE,MAAM;gBADd,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM;CAExB;AAED;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
@@ -1,34 +1,101 @@
1
+ /**
2
+ * An abstract class representing a generic signer.
3
+ * This class provides methods to connect, get addresses, and sign transactions.
4
+ */
1
5
  export class Signer {
2
- constructor(client) {
3
- this.client = client;
6
+ constructor(client_) {
7
+ this.client_ = client_;
4
8
  }
9
+ get client() {
10
+ return this.client_;
11
+ }
12
+ /**
13
+ * Gets the recommended Address object for the signer.
14
+ *
15
+ * @param _preference - Optional preference parameter.
16
+ * @returns A promise that resolves to the recommended Address object.
17
+ */
5
18
  async getRecommendedAddressObj(_preference) {
6
19
  return (await this.getAddressObjs())[0];
7
20
  }
21
+ /**
22
+ * Gets the recommended address for the signer as a string.
23
+ *
24
+ * @param preference - Optional preference parameter.
25
+ * @returns A promise that resolves to the recommended address as a string.
26
+ */
8
27
  async getRecommendedAddress(preference) {
9
28
  return (await this.getRecommendedAddressObj(preference)).toString();
10
29
  }
30
+ /**
31
+ * Gets an array of addresses associated with the signer as strings.
32
+ *
33
+ * @returns A promise that resolves to an array of addresses as strings.
34
+ */
11
35
  async getAddresses() {
12
36
  return this.getAddressObjs().then((addresses) => addresses.map((address) => address.toString()));
13
37
  }
38
+ /**
39
+ * Signs a message. This method is not implemented and should be overridden by subclasses.
40
+ *
41
+ * @param _ - The message to sign, as a string or BytesLike object.
42
+ * @returns A promise that resolves to the signed message as a string.
43
+ * @throws Will throw an error if not implemented.
44
+ */
14
45
  signMessage(_) {
15
46
  throw Error("Signer.signMessage not implemented");
16
47
  }
48
+ /**
49
+ * Sends a transaction after signing it.
50
+ *
51
+ * @param tx - The transaction to send, represented as a TransactionLike object.
52
+ * @returns A promise that resolves to the transaction hash as a Hex string.
53
+ */
17
54
  async sendTransaction(tx) {
18
55
  return this.client.sendTransaction(await this.signTransaction(tx));
19
56
  }
57
+ /**
58
+ * Signs a transaction.
59
+ *
60
+ * @param tx - The transaction to sign, represented as a TransactionLike object.
61
+ * @returns A promise that resolves to the signed Transaction object.
62
+ */
20
63
  async signTransaction(tx) {
21
- return this.signOnlyTransaction(tx);
64
+ const preparedTx = await this.prepareTransaction(tx);
65
+ return this.signOnlyTransaction(preparedTx);
66
+ }
67
+ /**
68
+ * prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
69
+ *
70
+ * @param _ - The transaction to prepare, represented as a TransactionLike object.
71
+ * @returns A promise that resolves to the prepared Transaction object.
72
+ * @throws Will throw an error if not implemented.
73
+ */
74
+ prepareTransaction(_) {
75
+ throw Error("Signer.prepareTransaction not implemented");
22
76
  }
77
+ /**
78
+ * Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
79
+ *
80
+ * @param _ - The transaction to sign, represented as a TransactionLike object.
81
+ * @returns A promise that resolves to the signed Transaction object.
82
+ * @throws Will throw an error if not implemented.
83
+ */
23
84
  signOnlyTransaction(_) {
24
85
  throw Error("Signer.signOnlyTransaction not implemented");
25
86
  }
26
87
  }
88
+ /**
89
+ * An enumeration of signer types.
90
+ */
27
91
  export var SignerType;
28
92
  (function (SignerType) {
29
93
  SignerType["EVM"] = "EVM";
30
94
  SignerType["BTC"] = "BTC";
31
95
  })(SignerType || (SignerType = {}));
96
+ /**
97
+ * A class representing information about a signer, including its type and the signer instance.
98
+ */
32
99
  export class SignerInfo {
33
100
  constructor(type, signer) {
34
101
  this.type = type;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.4-alpha.0",
3
+ "version": "0.0.4-alpha.11",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
7
7
  "private": false,
8
- "homepage": "https://github.com/Hanssen0/ccc",
8
+ "homepage": "https://github.com/ckb-ecofund/ccc",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git://github.com/Hanssen0/ccc.git"
11
+ "url": "git://github.com/ckb-ecofund/ccc.git"
12
12
  },
13
13
  "main": "dist/index.js",
14
14
  "exports": {
@@ -23,7 +23,6 @@
23
23
  "format": "prettier --write . && eslint --fix"
24
24
  },
25
25
  "devDependencies": {
26
- "@ckb-lumos/helpers": "^0.22.2",
27
26
  "@eslint/js": "^9.1.1",
28
27
  "@types/blake2b": "^2.1.3",
29
28
  "eslint": "^9.1.0",
@@ -40,6 +39,7 @@
40
39
  "access": "public"
41
40
  },
42
41
  "dependencies": {
42
+ "@ckb-lumos/helpers": "^0.22.2",
43
43
  "@noble/hashes": "^1.4.0",
44
44
  "abort-controller": "^3.0.0",
45
45
  "bech32": "^2.0.0",
@@ -47,5 +47,5 @@
47
47
  "buffer": "^6.0.3",
48
48
  "cross-fetch": "^4.0.0"
49
49
  },
50
- "gitHead": "e49045ed4b2778c92d6f49b79c17125245141a21"
50
+ "gitHead": "db0297c0753349e2650d2845f42f546316078d5d"
51
51
  }
@@ -4,6 +4,23 @@ import { Client, KnownScript } from "../client";
4
4
  import { hexFrom } from "../hex";
5
5
  import { type AddressLike } from "./index";
6
6
 
7
+ /**
8
+ * Parses an address string into an address information object.
9
+ *
10
+ * @param address - The address string to parse.
11
+ * @returns An object containing the address prefix, address format, and payload array.
12
+ *
13
+ * @throws Will throw an error if the address format is unknown.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const addressInfo = addressPayloadFromString("ckt1112139193129");
18
+ * console.log(addressInfo.prefix); // Outputs the address prefix
19
+ * console.log(addressInfo.format); // Outputs the address format
20
+ * console.log(addressInfo.payload); // Outputs the payload array
21
+ * ```
22
+ */
23
+
7
24
  export function addressPayloadFromString(address: string): {
8
25
  prefix: string;
9
26
  format: AddressFormat;
@@ -37,6 +54,25 @@ export function addressPayloadFromString(address: string): {
37
54
  throw Error(`Unknown address format ${address}`);
38
55
  }
39
56
 
57
+ /**
58
+ * Converts an address payload into an address-like object.
59
+ *
60
+ * @param prefix - The address prefix.
61
+ * @param format - The format of the address, as defined by the AddressFormat enum.
62
+ * @param payload - The payload array containing the address data.
63
+ * @param client - The client instance used to fetch known scripts.
64
+ * @returns A promise that resolves to an AddressLike object.
65
+ *
66
+ * @throws Will throw an error if the payload length is insufficient or if the script type is unknown.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const address = await addressFromPayload("ckt", AddressFormat.Full, [/* payload data *\/], client);
71
+ * console.log(address.script); // Outputs the script object
72
+ * console.log(address.prefix); // Outputs the address prefix
73
+ * ```
74
+ */
75
+
40
76
  export async function addressFromPayload(
41
77
  prefix: string,
42
78
  format: AddressFormat,
@@ -52,9 +88,9 @@ export async function addressFromPayload(
52
88
 
53
89
  return {
54
90
  script: {
55
- codeHash: hexFrom(payload.slice(0, 32)),
91
+ codeHash: payload.slice(0, 32),
56
92
  hashType: hashTypeFromBytes(payload.slice(32, 33)),
57
- args: hexFrom(payload.slice(33)),
93
+ args: payload.slice(33),
58
94
  },
59
95
  prefix,
60
96
  };
@@ -69,9 +105,9 @@ export async function addressFromPayload(
69
105
 
70
106
  return {
71
107
  script: {
72
- codeHash: hexFrom(payload.slice(0, 32)),
108
+ codeHash: payload.slice(0, 32),
73
109
  hashType: "data",
74
- args: hexFrom(payload.slice(32)),
110
+ args: payload.slice(32),
75
111
  },
76
112
  prefix,
77
113
  };
@@ -86,9 +122,9 @@ export async function addressFromPayload(
86
122
 
87
123
  return {
88
124
  script: {
89
- codeHash: hexFrom(payload.slice(0, 32)),
125
+ codeHash: payload.slice(0, 32),
90
126
  hashType: "type",
91
- args: hexFrom(payload.slice(32)),
127
+ args: payload.slice(32),
92
128
  },
93
129
  prefix,
94
130
  };
@@ -114,7 +150,7 @@ export async function addressFromPayload(
114
150
  return {
115
151
  script: {
116
152
  ...(await client.getKnownScript(script)),
117
- args: hexFrom(payload.slice(1)),
153
+ args: payload.slice(1),
118
154
  },
119
155
  prefix,
120
156
  };
@@ -14,12 +14,30 @@ export type AddressLike = {
14
14
  script: ScriptLike;
15
15
  prefix: string;
16
16
  };
17
+
18
+ /**
19
+ * Represents a ckb address with associated script and prefix.
20
+ */
21
+
17
22
  export class Address {
23
+ /**
24
+ * Creates an instance of Address.
25
+ *
26
+ * @param script - The script associated with the address.
27
+ * @param prefix - The address prefix.
28
+ */
18
29
  constructor(
19
30
  public script: Script,
20
31
  public prefix: string,
21
32
  ) {}
22
33
 
34
+ /**
35
+ * Creates an Address instance from an AddressLike object.
36
+ *
37
+ * @param address - An AddressLike object or an instance of Address.
38
+ * @returns An Address instance.
39
+ */
40
+
23
41
  static from(address: AddressLike): Address {
24
42
  if (address instanceof Address) {
25
43
  return address;
@@ -28,6 +46,16 @@ export class Address {
28
46
  return new Address(Script.from(address.script), address.prefix);
29
47
  }
30
48
 
49
+ /**
50
+ * Creates an Address instance from an address string.
51
+ *
52
+ * @param address - The address string to parse.
53
+ * @param clients - A Client instance or a record of Client instances keyed by prefix.
54
+ * @returns A promise that resolves to an Address instance.
55
+ *
56
+ * @throws Will throw an error if the address prefix is unknown or mismatched.
57
+ */
58
+
31
59
  static async fromString(
32
60
  address: string,
33
61
  clients: Client | Record<string, Client>,
@@ -50,11 +78,19 @@ export class Address {
50
78
  );
51
79
  }
52
80
 
81
+ /**
82
+ * Creates an Address instance from a script and client.
83
+ *
84
+ * @param script - The script-like object.
85
+ * @param client - The client instance used to fetch the address prefix.
86
+ * @returns A promise that resolves to an Address instance.
87
+ */
88
+
53
89
  static async fromScript(
54
90
  script: ScriptLike,
55
91
  client: Client,
56
92
  ): Promise<Address> {
57
- return new Address(Script.from(script), await client.getAddressPrefix());
93
+ return Address.from({ script, prefix: await client.getAddressPrefix() });
58
94
  }
59
95
 
60
96
  static async fromKnownScript(
@@ -62,15 +98,21 @@ export class Address {
62
98
  args: HexLike,
63
99
  client: Client,
64
100
  ) {
65
- return new Address(
66
- Script.from({
101
+ return Address.from({
102
+ script: {
67
103
  ...(await client.getKnownScript(script)),
68
104
  args,
69
- }),
70
- await client.getAddressPrefix(),
71
- );
105
+ },
106
+ prefix: await client.getAddressPrefix(),
107
+ });
72
108
  }
73
109
 
110
+ /**
111
+ * Converts the Address instance to a string.
112
+ *
113
+ * @returns The address as a string.
114
+ */
115
+
74
116
  toString(): string {
75
117
  const data = bytesConcat(
76
118
  [AddressFormat.Full],
@@ -1,10 +1,13 @@
1
+ /**
2
+ * Represents the possible encoding formats for converting bytes.
3
+ */
1
4
  export type BytesFromEncoding =
2
- | "utf8"
3
- | "utf16le"
4
- | "latin1"
5
- | "base64"
6
- | "base64url"
7
- | "hex"
8
- | "ascii"
9
- | "binary"
10
- | "ucs2";
5
+ | "utf8" // UTF-8 encoding
6
+ | "utf16le" // UTF-16 Little Endian encoding
7
+ | "latin1" // Latin-1 (ISO-8859-1) encoding
8
+ | "base64" // Base64 encoding
9
+ | "base64url" // Base64 URL encoding
10
+ | "hex" // Hexadecimal encoding
11
+ | "ascii" // ASCII encoding
12
+ | "binary" // Binary encoding
13
+ | "ucs2"; // UCS-2 (alias of UTF-16LE) encoding
@@ -4,6 +4,24 @@ import { BytesFromEncoding } from "./advanced";
4
4
  export type Bytes = Uint8Array;
5
5
  export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
6
6
 
7
+ /**
8
+ * Concatenates multiple byte-like arrays into a single byte array.
9
+ *
10
+ * @param args - The byte-like arrays to concatenate.
11
+ * @returns A Uint8Array containing the concatenated bytes.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const concatenatedBytes = bytesConcat(
16
+ * new Uint8Array([1, 2]),
17
+ * new Uint8Array([3, 4]),
18
+ * "hello",
19
+ * [5, 6, 7]
20
+ * );
21
+ * console.log(concatenatedBytes); // Outputs Uint8Array [1, 2, 3, 4, /* bytes of "hello" *\/, 5, 6, 7]
22
+ * ```
23
+ */
24
+
7
25
  export function bytesConcat(...args: BytesLike[]): Bytes {
8
26
  return new Uint8Array(
9
27
  args.reduce((acc: number[], v) => {
@@ -13,10 +31,52 @@ export function bytesConcat(...args: BytesLike[]): Bytes {
13
31
  );
14
32
  }
15
33
 
34
+ /**
35
+ * Converts a byte-like value to a string using the specified encoding.
36
+ *
37
+ * @param val - The byte-like value to convert.
38
+ * @param encoding - The encoding to use for the conversion, as defined by the BytesFromEncoding type.
39
+ * @returns A string representing the encoded bytes.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const encodedString = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "utf8");
44
+ * console.log(encodedString); // Outputs "hello"
45
+ *
46
+ * const base64String = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "base64");
47
+ * console.log(base64String); // Outputs "aGVsbG8="
48
+ * ```
49
+ */
50
+
16
51
  export function bytesTo(val: BytesLike, encoding: BytesFromEncoding): string {
17
52
  return Buffer.from(bytesFrom(val)).toString(encoding);
18
53
  }
19
54
 
55
+ /**
56
+ * Converts various types of byte-like values to a Uint8Array.
57
+ *
58
+ * @param bytes - The byte-like value to convert. It can be a string, Uint8Array, ArrayBuffer, or number array.
59
+ * @param encoding - Optional encoding to use if the input is a string. Defaults to hexadecimal if not specified.
60
+ * @returns A Uint8Array representing the input bytes.
61
+ *
62
+ * @throws Will throw an error if the input bytes are invalid or out of range.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const bytes1 = bytesFrom(new Uint8Array([1, 2, 3]));
67
+ * console.log(bytes1); // Outputs Uint8Array [1, 2, 3]
68
+ *
69
+ * const bytes2 = bytesFrom("68656c6c6f", "hex");
70
+ * console.log(bytes2); // Outputs Uint8Array [104, 101, 108, 108, 111]
71
+ *
72
+ * const bytes3 = bytesFrom("hello", "utf8");
73
+ * console.log(bytes3); // Outputs Uint8Array [104, 101, 108, 108, 111]
74
+ *
75
+ * const bytes4 = bytesFrom([1, 2, 255]);
76
+ * console.log(bytes4); // Outputs Uint8Array [1, 2, 255]
77
+ * ```
78
+ */
79
+
20
80
  export function bytesFrom(
21
81
  bytes: BytesLike,
22
82
  encoding?: BytesFromEncoding,