@ckb-ccc/core 0.0.4-alpha.2 → 0.0.4-alpha.4

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 (106) hide show
  1. package/README.md +11 -6
  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 +34 -0
  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 +36 -0
  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 +97 -0
  14. package/dist/ckb/script.d.ts.map +1 -1
  15. package/dist/ckb/script.js +97 -0
  16. package/dist/ckb/transaction.d.ts +391 -0
  17. package/dist/ckb/transaction.d.ts.map +1 -1
  18. package/dist/ckb/transaction.js +402 -0
  19. package/dist/client/client.d.ts +10 -8
  20. package/dist/client/client.d.ts.map +1 -1
  21. package/dist/client/client.js +18 -0
  22. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  23. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  24. package/dist/client/clientPublicMainnet.d.ts +2 -2
  25. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  26. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  27. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  28. package/dist/client/clientPublicTestnet.d.ts +2 -2
  29. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  30. package/dist/client/clientTypes.d.ts +6 -0
  31. package/dist/client/clientTypes.d.ts.map +1 -1
  32. package/dist/client/jsonRpc/advanced.d.ts +2 -87
  33. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  34. package/dist/client/jsonRpc/advanced.js +2 -65
  35. package/dist/client/jsonRpc/index.d.ts +60 -6
  36. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  37. package/dist/client/jsonRpc/index.js +72 -7
  38. package/dist/client/jsonRpc/transformers.d.ts +28 -0
  39. package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
  40. package/dist/client/jsonRpc/transformers.js +122 -0
  41. package/dist/client/jsonRpc/types.d.ts +41 -0
  42. package/dist/client/jsonRpc/types.d.ts.map +1 -0
  43. package/dist/client/jsonRpc/types.js +1 -0
  44. package/dist/fixedPoint/index.d.ts +42 -0
  45. package/dist/fixedPoint/index.d.ts.map +1 -1
  46. package/dist/fixedPoint/index.js +38 -0
  47. package/dist/hasher/index.d.ts +42 -0
  48. package/dist/hasher/index.d.ts.map +1 -1
  49. package/dist/hasher/index.js +42 -0
  50. package/dist/hex/index.d.ts +19 -0
  51. package/dist/hex/index.d.ts.map +1 -1
  52. package/dist/hex/index.js +12 -0
  53. package/dist/num/index.d.ts +99 -0
  54. package/dist/num/index.d.ts.map +1 -1
  55. package/dist/num/index.js +92 -0
  56. package/dist/signer/btc/signerBtc.d.ts +31 -0
  57. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  58. package/dist/signer/btc/signerBtc.js +22 -1
  59. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +36 -0
  60. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  61. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +36 -0
  62. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +35 -0
  63. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  64. package/dist/signer/ckb/signerCkbScriptReadonly.js +35 -0
  65. package/dist/signer/evm/signerEvm.d.ts +25 -0
  66. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  67. package/dist/signer/evm/signerEvm.js +21 -1
  68. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +30 -0
  69. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  70. package/dist/signer/evm/signerEvmAddressReadonly.js +30 -0
  71. package/dist/signer/helpers.d.ts +19 -1
  72. package/dist/signer/helpers.d.ts.map +1 -1
  73. package/dist/signer/helpers.js +26 -9
  74. package/dist/signer/signer.d.ts +71 -0
  75. package/dist/signer/signer.d.ts.map +1 -1
  76. package/dist/signer/signer.js +53 -0
  77. package/package.json +2 -2
  78. package/src/address/address.advanced.ts +36 -0
  79. package/src/address/index.ts +42 -0
  80. package/src/bytes/advanced.ts +12 -9
  81. package/src/bytes/index.ts +60 -0
  82. package/src/ckb/script.ts +109 -0
  83. package/src/ckb/transaction.ts +453 -0
  84. package/src/client/advanced.ts +1 -0
  85. package/src/client/client.ts +31 -8
  86. package/src/client/clientPublicMainnet.advanced.ts +5 -2
  87. package/src/client/clientPublicMainnet.ts +4 -2
  88. package/src/client/clientPublicTestnet.advanced.ts +5 -2
  89. package/src/client/clientPublicTestnet.ts +4 -2
  90. package/src/client/clientTypes.ts +13 -0
  91. package/src/client/index.ts +1 -0
  92. package/src/client/jsonRpc/advanced.ts +2 -93
  93. package/src/client/jsonRpc/index.ts +99 -11
  94. package/src/client/jsonRpc/transformers.ts +159 -0
  95. package/src/client/jsonRpc/types.ts +48 -0
  96. package/src/fixedPoint/index.ts +50 -0
  97. package/src/hasher/index.ts +46 -0
  98. package/src/hex/index.ts +19 -0
  99. package/src/num/index.ts +101 -0
  100. package/src/signer/btc/signerBtc.ts +32 -1
  101. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +36 -0
  102. package/src/signer/ckb/signerCkbScriptReadonly.ts +36 -0
  103. package/src/signer/evm/signerEvm.ts +26 -1
  104. package/src/signer/evm/signerEvmAddressReadonly.ts +31 -0
  105. package/src/signer/helpers.ts +28 -8
  106. package/src/signer/signer.ts +73 -0
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">
@@ -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) {
@@ -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,6 +49,13 @@ 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
60
  return Address.from({ script, prefix: await client.getAddressPrefix() });
30
61
  }
@@ -37,6 +68,11 @@ export class Address {
37
68
  prefix: await client.getAddressPrefix(),
38
69
  });
39
70
  }
71
+ /**
72
+ * Converts the Address instance to a string.
73
+ *
74
+ * @returns The address as a string.
75
+ */
40
76
  toString() {
41
77
  const data = bytesConcat([AddressFormat.Full], bytesFrom(this.script.codeHash), hashTypeToBytes(this.script.hashType), bytesFrom(this.script.args));
42
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;
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
+ */
7
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,EAAE,SAAS,EAAa,MAAM,UAAU,CAAC;AACvD,OAAO,EAAE,GAAG,EAAW,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAO3C,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AACpD,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAC3D,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,GAAG,QAAQ,CAmBxD;AACD,wBAAgB,eAAe,CAAC,QAAQ,EAAE,YAAY,GAAG,KAAK,CAE7D;AACD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,GAAG,QAAQ,CAE5D;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,YAAY,CAAC;IACvB,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;AACF,qBAAa,MAAM;IAER,QAAQ,EAAE,GAAG;IACb,QAAQ,EAAE,QAAQ;IAClB,IAAI,EAAE,GAAG;gBAFT,QAAQ,EAAE,GAAG,EACb,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,GAAG;IAGlB,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM;IAYvC,UAAU;;;;;IAQV,OAAO,IAAI,KAAK;IAIhB,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAAC,MAAM,GAAG,MAAM;IAWvD,EAAE,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO;CAQ7B"}
1
+ {"version":3,"file":"script.d.ts","sourceRoot":"","sources":["../../src/ckb/script.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAa,MAAM,UAAU,CAAC;AACvD,OAAO,EAAE,GAAG,EAAW,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAO3C,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AACpD,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAE3D;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,GAAG,QAAQ,CAmBxD;AAED;;;;;;;;;;GAUG;AAEH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,YAAY,GAAG,KAAK,CAE7D;AAED;;;;;;;;;;;;GAYG;AAEH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,GAAG,QAAQ,CAE5D;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,YAAY,CAAC;IACvB,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;AACF,qBAAa,MAAM;IAUR,QAAQ,EAAE,GAAG;IACb,QAAQ,EAAE,QAAQ;IAClB,IAAI,EAAE,GAAG;IAXlB;;;;;;OAMG;gBAGM,QAAQ,EAAE,GAAG,EACb,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,GAAG;IAGlB;;;;;;;;;;;;;;OAcG;IAEH,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM;IAYvC;;;;OAIG;IAEH,UAAU;;;;;IAQV;;;;;;;;;OASG;IAEH,OAAO,IAAI,KAAK;IAIhB;;;;;;;;;;OAUG;IAEH,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAAC,MAAM,GAAG,MAAM;IAWvD;;;;;;;;;;OAUG;IAEH,EAAE,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO;CAQ7B"}
@@ -2,6 +2,20 @@ import { bytesFrom } from "../bytes";
2
2
  import { hexFrom } from "../hex";
3
3
  import * as mol from "./molecule.advanced";
4
4
  import { HASH_TYPES, HASH_TYPE_TO_NUM, NUM_TO_HASH_TYPE, } from "./script.advanced";
5
+ /**
6
+ * Converts a HashTypeLike value to a HashType.
7
+ *
8
+ * @param val - The value to convert, which can be a string, number, or bigint.
9
+ * @returns The corresponding HashType.
10
+ *
11
+ * @throws Will throw an error if the input value is not a valid hash type.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const hashType = hashTypeFrom(1); // Outputs "data"
16
+ * const hashType = hashTypeFrom("type"); // Outputs "type"
17
+ * ```
18
+ */
5
19
  export function hashTypeFrom(val) {
6
20
  const hashType = (() => {
7
21
  if (typeof val === "number") {
@@ -20,24 +34,75 @@ export function hashTypeFrom(val) {
20
34
  }
21
35
  return hashType;
22
36
  }
37
+ /**
38
+ * Converts a HashTypeLike value to its corresponding byte representation.
39
+ *
40
+ * @param hashType - The hash type value to convert.
41
+ * @returns A Uint8Array containing the byte representation of the hash type.
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const hashTypeBytes = hashTypeToBytes("type"); // Outputs Uint8Array [0]
46
+ * ```
47
+ */
23
48
  export function hashTypeToBytes(hashType) {
24
49
  return bytesFrom([HASH_TYPE_TO_NUM[hashTypeFrom(hashType)]]);
25
50
  }
51
+ /**
52
+ * Converts a byte-like value to a HashType.
53
+ *
54
+ * @param bytes - The byte-like value to convert.
55
+ * @returns The corresponding HashType.
56
+ *
57
+ * @throws Will throw an error if the input bytes do not correspond to a valid hash type.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const hashType = hashTypeFromBytes(new Uint8Array([0])); // Outputs "type"
62
+ * ```
63
+ */
26
64
  export function hashTypeFromBytes(bytes) {
27
65
  return NUM_TO_HASH_TYPE[bytesFrom(bytes)[0]];
28
66
  }
29
67
  export class Script {
68
+ /**
69
+ * Creates an instance of Script.
70
+ *
71
+ * @param codeHash - The code hash of the script.
72
+ * @param hashType - The hash type of the script.
73
+ * @param args - The arguments for the script.
74
+ */
30
75
  constructor(codeHash, hashType, args) {
31
76
  this.codeHash = codeHash;
32
77
  this.hashType = hashType;
33
78
  this.args = args;
34
79
  }
80
+ /**
81
+ * Creates a Script instance from a ScriptLike object.
82
+ *
83
+ * @param script - A ScriptLike object or an instance of Script.
84
+ * @returns A Script instance.
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const script = Script.from({
89
+ * codeHash: "0x1234...",
90
+ * hashType: "type",
91
+ * args: "0xabcd..."
92
+ * });
93
+ * ```
94
+ */
35
95
  static from(script) {
36
96
  if (script instanceof Script) {
37
97
  return script;
38
98
  }
39
99
  return new Script(hexFrom(script.codeHash), hashTypeFrom(script.hashType), hexFrom(script.args));
40
100
  }
101
+ /**
102
+ * Converts the Script instance to molecule data format.
103
+ *
104
+ * @returns An object representing the script in molecule data format.
105
+ */
41
106
  _toMolData() {
42
107
  return {
43
108
  codeHash: bytesFrom(this.codeHash),
@@ -45,13 +110,45 @@ export class Script {
45
110
  args: bytesFrom(this.args),
46
111
  };
47
112
  }
113
+ /**
114
+ * Converts the Script instance to bytes.
115
+ *
116
+ * @returns A Uint8Array containing the script bytes.
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * const scriptBytes = script.toBytes();
121
+ * ```
122
+ */
48
123
  toBytes() {
49
124
  return bytesFrom(mol.SerializeScript(this._toMolData()));
50
125
  }
126
+ /**
127
+ * Creates a Script instance from a byte-like value or molecule Script.
128
+ *
129
+ * @param bytes - The byte-like value or molecule Script to convert.
130
+ * @returns A Script instance.
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * const script = Script.fromBytes(new Uint8Array([/* script bytes *\/]));
135
+ * ```
136
+ */
51
137
  static fromBytes(bytes) {
52
138
  const view = bytes instanceof mol.Script ? bytes : new mol.Script(bytesFrom(bytes));
53
139
  return new Script(hexFrom(view.getCodeHash().raw()), hashTypeFromBytes([view.getHashType()]), hexFrom(view.getArgs().raw()));
54
140
  }
141
+ /**
142
+ * Compares the current Script instance with another ScriptLike object for equality.
143
+ *
144
+ * @param val - The ScriptLike object to compare with.
145
+ * @returns True if the scripts are equal, otherwise false.
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * const isEqual = script.eq(anotherScript);
150
+ * ```
151
+ */
55
152
  eq(val) {
56
153
  const script = Script.from(val);
57
154
  return (this.codeHash === script.codeHash &&