@ckb-ccc/core 0.0.4-alpha.1 → 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 +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/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 +3 -3
  86. package/src/address/address.advanced.ts +54 -18
  87. package/src/address/index.ts +51 -7
  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
package/README.md CHANGED
@@ -1,12 +1,17 @@
1
- <h1 align="center" style="font-size: 64px;">
2
- CCC
1
+ <h1 align="center" style="font-size: 48px;">
2
+ CCC Core
3
3
  </h1>
4
4
 
5
5
  <p align="center">
6
- <img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/ckb-ecofund/ccc">
7
- <img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/ckb-ecofund/ccc/master">
8
- <img alt="GitHub deployments" src="https://img.shields.io/github/deployments/ckb-ecofund/ccc/production">
9
- <img alt="Demo" src="https://img.shields.io/website?url=https%3A%2F%2Fckbccc-demo.vercel.app%2F&label=Demo">
6
+ <a href="https://www.npmjs.com/package/@ckb-ccc/core"><img
7
+ alt="NPM Version" src="https://img.shields.io/npm/v/%40ckb-ccc%2Fcore"
8
+ /></a>
9
+ <img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/ckb-ecofund/ccc" />
10
+ <img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/ckb-ecofund/ccc/master" />
11
+ <img alt="GitHub deployments" src="https://img.shields.io/github/deployments/ckb-ecofund/ccc/production" />
12
+ <a href="https://ckbccc-demo.vercel.app/"><img
13
+ alt="Demo" src="https://img.shields.io/website?url=https%3A%2F%2Fckbccc-demo.vercel.app%2F&label=Demo"
14
+ /></a>
10
15
  </p>
11
16
 
12
17
  <p align="center">
@@ -29,4 +34,4 @@ This project is still under active development, and we are looking forward to yo
29
34
 
30
35
  <h3 align="center">
31
36
  Read more about CCC on its <a href="https://github.com/ckb-ecofund/ccc">GitHub Repo</a>.
32
- </h3>
37
+ </h3>
@@ -1,10 +1,44 @@
1
1
  import { Client } from "../client";
2
2
  import { type AddressLike } from "./index";
3
+ /**
4
+ * Parses an address string into an address information object.
5
+ *
6
+ * @param address - The address string to parse.
7
+ * @returns An object containing the address prefix, address format, and payload array.
8
+ *
9
+ * @throws Will throw an error if the address format is unknown.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const addressInfo = addressPayloadFromString("ckt1112139193129");
14
+ * console.log(addressInfo.prefix); // Outputs the address prefix
15
+ * console.log(addressInfo.format); // Outputs the address format
16
+ * console.log(addressInfo.payload); // Outputs the payload array
17
+ * ```
18
+ */
3
19
  export declare function addressPayloadFromString(address: string): {
4
20
  prefix: string;
5
21
  format: AddressFormat;
6
22
  payload: number[];
7
23
  };
24
+ /**
25
+ * Converts an address payload into an address-like object.
26
+ *
27
+ * @param prefix - The address prefix.
28
+ * @param format - The format of the address, as defined by the AddressFormat enum.
29
+ * @param payload - The payload array containing the address data.
30
+ * @param client - The client instance used to fetch known scripts.
31
+ * @returns A promise that resolves to an AddressLike object.
32
+ *
33
+ * @throws Will throw an error if the payload length is insufficient or if the script type is unknown.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const address = await addressFromPayload("ckt", AddressFormat.Full, [/* payload data *\/], client);
38
+ * console.log(address.script); // Outputs the script object
39
+ * console.log(address.prefix); // Outputs the address prefix
40
+ * ```
41
+ */
8
42
  export declare function addressFromPayload(prefix: string, format: AddressFormat, payload: number[], client: Client): Promise<AddressLike>;
9
43
  export declare enum AddressFormat {
10
44
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"address.advanced.d.ts","sourceRoot":"","sources":["../../src/address/address.advanced.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAe,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,GAAG;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,aAAa,CAAC;IACtB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CA2BA;AAED,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,MAAM,EAAE,EACjB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,WAAW,CAAC,CA4EtB;AAED,oBAAY,aAAa;IACvB;;OAEG;IACH,IAAI,IAAO;IACX;;;OAGG;IACH,KAAK,IAAO;IACZ;;;OAGG;IACH,QAAQ,IAAO;IACf;;;OAGG;IACH,QAAQ,IAAO;CAChB;AAED,eAAO,MAAM,oBAAoB,OAAO,CAAC"}
1
+ {"version":3,"file":"address.advanced.d.ts","sourceRoot":"","sources":["../../src/address/address.advanced.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAe,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;;;;;;;GAeG;AAEH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,GAAG;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,aAAa,CAAC;IACtB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CA2BA;AAED;;;;;;;;;;;;;;;;;GAiBG;AAEH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,MAAM,EAAE,EACjB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,WAAW,CAAC,CA4EtB;AAED,oBAAY,aAAa;IACvB;;OAEG;IACH,IAAI,IAAO;IACX;;;OAGG;IACH,KAAK,IAAO;IACZ;;;OAGG;IACH,QAAQ,IAAO;IACf;;;OAGG;IACH,QAAQ,IAAO;CAChB;AAED,eAAO,MAAM,oBAAoB,OAAO,CAAC"}
@@ -2,6 +2,22 @@ import { bech32, bech32m } from "bech32";
2
2
  import { hashTypeFromBytes } from "../ckb";
3
3
  import { KnownScript } from "../client";
4
4
  import { hexFrom } from "../hex";
5
+ /**
6
+ * Parses an address string into an address information object.
7
+ *
8
+ * @param address - The address string to parse.
9
+ * @returns An object containing the address prefix, address format, and payload array.
10
+ *
11
+ * @throws Will throw an error if the address format is unknown.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const addressInfo = addressPayloadFromString("ckt1112139193129");
16
+ * console.log(addressInfo.prefix); // Outputs the address prefix
17
+ * console.log(addressInfo.format); // Outputs the address format
18
+ * console.log(addressInfo.payload); // Outputs the payload array
19
+ * ```
20
+ */
5
21
  export function addressPayloadFromString(address) {
6
22
  // Try parse full format address
7
23
  {
@@ -25,6 +41,24 @@ export function addressPayloadFromString(address) {
25
41
  }
26
42
  throw Error(`Unknown address format ${address}`);
27
43
  }
44
+ /**
45
+ * Converts an address payload into an address-like object.
46
+ *
47
+ * @param prefix - The address prefix.
48
+ * @param format - The format of the address, as defined by the AddressFormat enum.
49
+ * @param payload - The payload array containing the address data.
50
+ * @param client - The client instance used to fetch known scripts.
51
+ * @returns A promise that resolves to an AddressLike object.
52
+ *
53
+ * @throws Will throw an error if the payload length is insufficient or if the script type is unknown.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const address = await addressFromPayload("ckt", AddressFormat.Full, [/* payload data *\/], client);
58
+ * console.log(address.script); // Outputs the script object
59
+ * console.log(address.prefix); // Outputs the address prefix
60
+ * ```
61
+ */
28
62
  export async function addressFromPayload(prefix, format, payload, client) {
29
63
  if (format === AddressFormat.Full) {
30
64
  if (payload.length < 32 + 1) {
@@ -32,9 +66,9 @@ export async function addressFromPayload(prefix, format, payload, client) {
32
66
  }
33
67
  return {
34
68
  script: {
35
- codeHash: hexFrom(payload.slice(0, 32)),
69
+ codeHash: payload.slice(0, 32),
36
70
  hashType: hashTypeFromBytes(payload.slice(32, 33)),
37
- args: hexFrom(payload.slice(33)),
71
+ args: payload.slice(33),
38
72
  },
39
73
  prefix,
40
74
  };
@@ -45,9 +79,9 @@ export async function addressFromPayload(prefix, format, payload, client) {
45
79
  }
46
80
  return {
47
81
  script: {
48
- codeHash: hexFrom(payload.slice(0, 32)),
82
+ codeHash: payload.slice(0, 32),
49
83
  hashType: "data",
50
- args: hexFrom(payload.slice(32)),
84
+ args: payload.slice(32),
51
85
  },
52
86
  prefix,
53
87
  };
@@ -58,9 +92,9 @@ export async function addressFromPayload(prefix, format, payload, client) {
58
92
  }
59
93
  return {
60
94
  script: {
61
- codeHash: hexFrom(payload.slice(0, 32)),
95
+ codeHash: payload.slice(0, 32),
62
96
  hashType: "type",
63
- args: hexFrom(payload.slice(32)),
97
+ args: payload.slice(32),
64
98
  },
65
99
  prefix,
66
100
  };
@@ -80,7 +114,7 @@ export async function addressFromPayload(prefix, format, payload, client) {
80
114
  return {
81
115
  script: {
82
116
  ...(await client.getKnownScript(script)),
83
- args: hexFrom(payload.slice(1)),
117
+ args: payload.slice(1),
84
118
  },
85
119
  prefix,
86
120
  };
@@ -5,14 +5,50 @@ export type AddressLike = {
5
5
  script: ScriptLike;
6
6
  prefix: string;
7
7
  };
8
+ /**
9
+ * Represents a ckb address with associated script and prefix.
10
+ */
8
11
  export declare class Address {
9
12
  script: Script;
10
13
  prefix: string;
14
+ /**
15
+ * Creates an instance of Address.
16
+ *
17
+ * @param script - The script associated with the address.
18
+ * @param prefix - The address prefix.
19
+ */
11
20
  constructor(script: Script, prefix: string);
21
+ /**
22
+ * Creates an Address instance from an AddressLike object.
23
+ *
24
+ * @param address - An AddressLike object or an instance of Address.
25
+ * @returns An Address instance.
26
+ */
12
27
  static from(address: AddressLike): Address;
28
+ /**
29
+ * Creates an Address instance from an address string.
30
+ *
31
+ * @param address - The address string to parse.
32
+ * @param clients - A Client instance or a record of Client instances keyed by prefix.
33
+ * @returns A promise that resolves to an Address instance.
34
+ *
35
+ * @throws Will throw an error if the address prefix is unknown or mismatched.
36
+ */
13
37
  static fromString(address: string, clients: Client | Record<string, Client>): Promise<Address>;
38
+ /**
39
+ * Creates an Address instance from a script and client.
40
+ *
41
+ * @param script - The script-like object.
42
+ * @param client - The client instance used to fetch the address prefix.
43
+ * @returns A promise that resolves to an Address instance.
44
+ */
14
45
  static fromScript(script: ScriptLike, client: Client): Promise<Address>;
15
46
  static fromKnownScript(script: KnownScript, args: HexLike, client: Client): Promise<Address>;
47
+ /**
48
+ * Converts the Address instance to a string.
49
+ *
50
+ * @returns The address as a string.
51
+ */
16
52
  toString(): string;
17
53
  }
18
54
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/address/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAmB,MAAM,QAAQ,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAQjC,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AACF,qBAAa,OAAO;IAET,MAAM,EAAE,MAAM;IACd,MAAM,EAAE,MAAM;gBADd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM;IAGvB,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;WAQ7B,UAAU,CACrB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACvC,OAAO,CAAC,OAAO,CAAC;WAmBN,UAAU,CACrB,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,OAAO,CAAC;WAIN,eAAe,CAC1B,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,MAAM;IAWhB,QAAQ,IAAI,MAAM;CAcnB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/address/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAmB,MAAM,QAAQ,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAQjC,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AAEH,qBAAa,OAAO;IAQT,MAAM,EAAE,MAAM;IACd,MAAM,EAAE,MAAM;IARvB;;;;;OAKG;gBAEM,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM;IAGvB;;;;;OAKG;IAEH,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;IAQ1C;;;;;;;;OAQG;WAEU,UAAU,CACrB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACvC,OAAO,CAAC,OAAO,CAAC;IAmBnB;;;;;;OAMG;WAEU,UAAU,CACrB,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,OAAO,CAAC;WAIN,eAAe,CAC1B,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,MAAM;IAWhB;;;;OAIG;IAEH,QAAQ,IAAI,MAAM;CAcnB"}
@@ -2,17 +2,41 @@ import { bech32m } from "bech32";
2
2
  import { bytesConcat, bytesFrom } from "../bytes";
3
3
  import { Script, hashTypeToBytes } from "../ckb";
4
4
  import { ADDRESS_BECH32_LIMIT, AddressFormat, addressFromPayload, addressPayloadFromString, } from "./address.advanced";
5
+ /**
6
+ * Represents a ckb address with associated script and prefix.
7
+ */
5
8
  export class Address {
9
+ /**
10
+ * Creates an instance of Address.
11
+ *
12
+ * @param script - The script associated with the address.
13
+ * @param prefix - The address prefix.
14
+ */
6
15
  constructor(script, prefix) {
7
16
  this.script = script;
8
17
  this.prefix = prefix;
9
18
  }
19
+ /**
20
+ * Creates an Address instance from an AddressLike object.
21
+ *
22
+ * @param address - An AddressLike object or an instance of Address.
23
+ * @returns An Address instance.
24
+ */
10
25
  static from(address) {
11
26
  if (address instanceof Address) {
12
27
  return address;
13
28
  }
14
29
  return new Address(Script.from(address.script), address.prefix);
15
30
  }
31
+ /**
32
+ * Creates an Address instance from an address string.
33
+ *
34
+ * @param address - The address string to parse.
35
+ * @param clients - A Client instance or a record of Client instances keyed by prefix.
36
+ * @returns A promise that resolves to an Address instance.
37
+ *
38
+ * @throws Will throw an error if the address prefix is unknown or mismatched.
39
+ */
16
40
  static async fromString(address, clients) {
17
41
  const { prefix, format, payload } = addressPayloadFromString(address);
18
42
  const client = clients[prefix] ?? clients;
@@ -25,15 +49,30 @@ export class Address {
25
49
  }
26
50
  return Address.from(await addressFromPayload(prefix, format, payload, client));
27
51
  }
52
+ /**
53
+ * Creates an Address instance from a script and client.
54
+ *
55
+ * @param script - The script-like object.
56
+ * @param client - The client instance used to fetch the address prefix.
57
+ * @returns A promise that resolves to an Address instance.
58
+ */
28
59
  static async fromScript(script, client) {
29
- return new Address(Script.from(script), await client.getAddressPrefix());
60
+ return Address.from({ script, prefix: await client.getAddressPrefix() });
30
61
  }
31
62
  static async fromKnownScript(script, args, client) {
32
- return new Address(Script.from({
33
- ...(await client.getKnownScript(script)),
34
- args,
35
- }), await client.getAddressPrefix());
63
+ return Address.from({
64
+ script: {
65
+ ...(await client.getKnownScript(script)),
66
+ args,
67
+ },
68
+ prefix: await client.getAddressPrefix(),
69
+ });
36
70
  }
71
+ /**
72
+ * Converts the Address instance to a string.
73
+ *
74
+ * @returns The address as a string.
75
+ */
37
76
  toString() {
38
77
  const data = bytesConcat([AddressFormat.Full], bytesFrom(this.script.codeHash), hashTypeToBytes(this.script.hashType), bytesFrom(this.script.args));
39
78
  return bech32m.encode(this.prefix, bech32m.toWords(data), ADDRESS_BECH32_LIMIT);
@@ -1,2 +1,5 @@
1
+ /**
2
+ * Represents the possible encoding formats for converting bytes.
3
+ */
1
4
  export type BytesFromEncoding = "utf8" | "utf16le" | "latin1" | "base64" | "base64url" | "hex" | "ascii" | "binary" | "ucs2";
2
5
  //# sourceMappingURL=advanced.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/bytes/advanced.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,GACzB,MAAM,GACN,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,WAAW,GACX,KAAK,GACL,OAAO,GACP,QAAQ,GACR,MAAM,CAAC"}
1
+ {"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/bytes/advanced.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,MAAM,GACN,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,WAAW,GACX,KAAK,GACL,OAAO,GACP,QAAQ,GACR,MAAM,CAAC"}
@@ -1,7 +1,64 @@
1
1
  import { BytesFromEncoding } from "./advanced";
2
2
  export type Bytes = Uint8Array;
3
3
  export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
4
+ /**
5
+ * Concatenates multiple byte-like arrays into a single byte array.
6
+ *
7
+ * @param args - The byte-like arrays to concatenate.
8
+ * @returns A Uint8Array containing the concatenated bytes.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const concatenatedBytes = bytesConcat(
13
+ * new Uint8Array([1, 2]),
14
+ * new Uint8Array([3, 4]),
15
+ * "hello",
16
+ * [5, 6, 7]
17
+ * );
18
+ * console.log(concatenatedBytes); // Outputs Uint8Array [1, 2, 3, 4, /* bytes of "hello" *\/, 5, 6, 7]
19
+ * ```
20
+ */
4
21
  export declare function bytesConcat(...args: BytesLike[]): Bytes;
22
+ /**
23
+ * Converts a byte-like value to a string using the specified encoding.
24
+ *
25
+ * @param val - The byte-like value to convert.
26
+ * @param encoding - The encoding to use for the conversion, as defined by the BytesFromEncoding type.
27
+ * @returns A string representing the encoded bytes.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const encodedString = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "utf8");
32
+ * console.log(encodedString); // Outputs "hello"
33
+ *
34
+ * const base64String = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "base64");
35
+ * console.log(base64String); // Outputs "aGVsbG8="
36
+ * ```
37
+ */
5
38
  export declare function bytesTo(val: BytesLike, encoding: BytesFromEncoding): string;
39
+ /**
40
+ * Converts various types of byte-like values to a Uint8Array.
41
+ *
42
+ * @param bytes - The byte-like value to convert. It can be a string, Uint8Array, ArrayBuffer, or number array.
43
+ * @param encoding - Optional encoding to use if the input is a string. Defaults to hexadecimal if not specified.
44
+ * @returns A Uint8Array representing the input bytes.
45
+ *
46
+ * @throws Will throw an error if the input bytes are invalid or out of range.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const bytes1 = bytesFrom(new Uint8Array([1, 2, 3]));
51
+ * console.log(bytes1); // Outputs Uint8Array [1, 2, 3]
52
+ *
53
+ * const bytes2 = bytesFrom("68656c6c6f", "hex");
54
+ * console.log(bytes2); // Outputs Uint8Array [104, 101, 108, 108, 111]
55
+ *
56
+ * const bytes3 = bytesFrom("hello", "utf8");
57
+ * console.log(bytes3); // Outputs Uint8Array [104, 101, 108, 108, 111]
58
+ *
59
+ * const bytes4 = bytesFrom([1, 2, 255]);
60
+ * console.log(bytes4); // Outputs Uint8Array [1, 2, 255]
61
+ * ```
62
+ */
6
63
  export declare function bytesFrom(bytes: BytesLike, encoding?: BytesFromEncoding): Bytes;
7
64
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bytes/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC;AAC/B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,EAAE,CAAC;AAErE,wBAAgB,WAAW,CAAC,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,KAAK,CAOvD;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,iBAAiB,GAAG,MAAM,CAE3E;AAED,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,EAChB,QAAQ,CAAC,EAAE,iBAAiB,GAC3B,KAAK,CA2BP"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bytes/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC;AAC/B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,EAAE,CAAC;AAErE;;;;;;;;;;;;;;;;GAgBG;AAEH,wBAAgB,WAAW,CAAC,GAAG,IAAI,EAAE,SAAS,EAAE,GAAG,KAAK,CAOvD;AAED;;;;;;;;;;;;;;;GAeG;AAEH,wBAAgB,OAAO,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,iBAAiB,GAAG,MAAM,CAE3E;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,EAChB,QAAQ,CAAC,EAAE,iBAAiB,GAC3B,KAAK,CA2BP"}
@@ -1,13 +1,70 @@
1
1
  import { Buffer } from "buffer/";
2
+ /**
3
+ * Concatenates multiple byte-like arrays into a single byte array.
4
+ *
5
+ * @param args - The byte-like arrays to concatenate.
6
+ * @returns A Uint8Array containing the concatenated bytes.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const concatenatedBytes = bytesConcat(
11
+ * new Uint8Array([1, 2]),
12
+ * new Uint8Array([3, 4]),
13
+ * "hello",
14
+ * [5, 6, 7]
15
+ * );
16
+ * console.log(concatenatedBytes); // Outputs Uint8Array [1, 2, 3, 4, /* bytes of "hello" *\/, 5, 6, 7]
17
+ * ```
18
+ */
2
19
  export function bytesConcat(...args) {
3
20
  return new Uint8Array(args.reduce((acc, v) => {
4
21
  acc.push(...bytesFrom(v));
5
22
  return acc;
6
23
  }, []));
7
24
  }
25
+ /**
26
+ * Converts a byte-like value to a string using the specified encoding.
27
+ *
28
+ * @param val - The byte-like value to convert.
29
+ * @param encoding - The encoding to use for the conversion, as defined by the BytesFromEncoding type.
30
+ * @returns A string representing the encoded bytes.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const encodedString = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "utf8");
35
+ * console.log(encodedString); // Outputs "hello"
36
+ *
37
+ * const base64String = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "base64");
38
+ * console.log(base64String); // Outputs "aGVsbG8="
39
+ * ```
40
+ */
8
41
  export function bytesTo(val, encoding) {
9
42
  return Buffer.from(bytesFrom(val)).toString(encoding);
10
43
  }
44
+ /**
45
+ * Converts various types of byte-like values to a Uint8Array.
46
+ *
47
+ * @param bytes - The byte-like value to convert. It can be a string, Uint8Array, ArrayBuffer, or number array.
48
+ * @param encoding - Optional encoding to use if the input is a string. Defaults to hexadecimal if not specified.
49
+ * @returns A Uint8Array representing the input bytes.
50
+ *
51
+ * @throws Will throw an error if the input bytes are invalid or out of range.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const bytes1 = bytesFrom(new Uint8Array([1, 2, 3]));
56
+ * console.log(bytes1); // Outputs Uint8Array [1, 2, 3]
57
+ *
58
+ * const bytes2 = bytesFrom("68656c6c6f", "hex");
59
+ * console.log(bytes2); // Outputs Uint8Array [104, 101, 108, 108, 111]
60
+ *
61
+ * const bytes3 = bytesFrom("hello", "utf8");
62
+ * console.log(bytes3); // Outputs Uint8Array [104, 101, 108, 108, 111]
63
+ *
64
+ * const bytes4 = bytesFrom([1, 2, 255]);
65
+ * console.log(bytes4); // Outputs Uint8Array [1, 2, 255]
66
+ * ```
67
+ */
11
68
  export function bytesFrom(bytes, encoding) {
12
69
  if (bytes instanceof Uint8Array) {
13
70
  return bytes;
@@ -3,8 +3,46 @@ import { Hex } from "../hex";
3
3
  import * as mol from "./molecule.advanced";
4
4
  export type HashTypeLike = string | number | bigint;
5
5
  export type HashType = "type" | "data" | "data1" | "data2";
6
+ /**
7
+ * Converts a HashTypeLike value to a HashType.
8
+ *
9
+ * @param val - The value to convert, which can be a string, number, or bigint.
10
+ * @returns The corresponding HashType.
11
+ *
12
+ * @throws Will throw an error if the input value is not a valid hash type.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const hashType = hashTypeFrom(1); // Outputs "data"
17
+ * const hashType = hashTypeFrom("type"); // Outputs "type"
18
+ * ```
19
+ */
6
20
  export declare function hashTypeFrom(val: HashTypeLike): HashType;
7
- export declare function hashTypeToBytes(hashType: HashType): Bytes;
21
+ /**
22
+ * Converts a HashTypeLike value to its corresponding byte representation.
23
+ *
24
+ * @param hashType - The hash type value to convert.
25
+ * @returns A Uint8Array containing the byte representation of the hash type.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const hashTypeBytes = hashTypeToBytes("type"); // Outputs Uint8Array [0]
30
+ * ```
31
+ */
32
+ export declare function hashTypeToBytes(hashType: HashTypeLike): Bytes;
33
+ /**
34
+ * Converts a byte-like value to a HashType.
35
+ *
36
+ * @param bytes - The byte-like value to convert.
37
+ * @returns The corresponding HashType.
38
+ *
39
+ * @throws Will throw an error if the input bytes do not correspond to a valid hash type.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const hashType = hashTypeFromBytes(new Uint8Array([0])); // Outputs "type"
44
+ * ```
45
+ */
8
46
  export declare function hashTypeFromBytes(bytes: BytesLike): HashType;
9
47
  export type ScriptLike = {
10
48
  codeHash: BytesLike;
@@ -15,15 +53,74 @@ export declare class Script {
15
53
  codeHash: Hex;
16
54
  hashType: HashType;
17
55
  args: Hex;
56
+ /**
57
+ * Creates an instance of Script.
58
+ *
59
+ * @param codeHash - The code hash of the script.
60
+ * @param hashType - The hash type of the script.
61
+ * @param args - The arguments for the script.
62
+ */
18
63
  constructor(codeHash: Hex, hashType: HashType, args: Hex);
64
+ /**
65
+ * Creates a Script instance from a ScriptLike object.
66
+ *
67
+ * @param script - A ScriptLike object or an instance of Script.
68
+ * @returns A Script instance.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const script = Script.from({
73
+ * codeHash: "0x1234...",
74
+ * hashType: "type",
75
+ * args: "0xabcd..."
76
+ * });
77
+ * ```
78
+ */
19
79
  static from(script: ScriptLike): Script;
80
+ /**
81
+ * Converts the Script instance to molecule data format.
82
+ *
83
+ * @returns An object representing the script in molecule data format.
84
+ */
20
85
  _toMolData(): {
21
86
  codeHash: Uint8Array;
22
87
  hashType: Uint8Array;
23
88
  args: Uint8Array;
24
89
  };
90
+ /**
91
+ * Converts the Script instance to bytes.
92
+ *
93
+ * @returns A Uint8Array containing the script bytes.
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * const scriptBytes = script.toBytes();
98
+ * ```
99
+ */
25
100
  toBytes(): Bytes;
101
+ /**
102
+ * Creates a Script instance from a byte-like value or molecule Script.
103
+ *
104
+ * @param bytes - The byte-like value or molecule Script to convert.
105
+ * @returns A Script instance.
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const script = Script.fromBytes(new Uint8Array([/* script bytes *\/]));
110
+ * ```
111
+ */
26
112
  static fromBytes(bytes: BytesLike | mol.Script): Script;
113
+ /**
114
+ * Compares the current Script instance with another ScriptLike object for equality.
115
+ *
116
+ * @param val - The ScriptLike object to compare with.
117
+ * @returns True if the scripts are equal, otherwise false.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * const isEqual = script.eq(anotherScript);
122
+ * ```
123
+ */
27
124
  eq(val: ScriptLike): boolean;
28
125
  }
29
126
  //# sourceMappingURL=script.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"script.d.ts","sourceRoot":"","sources":["../../src/ckb/script.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EA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