@ckb-ccc/core 0.0.2-alpha.0 → 0.0.2-alpha.2

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 (208) hide show
  1. package/dist/address/address.advanced.d.ts +5 -0
  2. package/dist/address/address.advanced.d.ts.map +1 -0
  3. package/dist/address/address.advanced.js +68 -0
  4. package/dist/address/advanced.d.ts +7 -9
  5. package/dist/address/advanced.d.ts.map +1 -1
  6. package/dist/address/advanced.js +50 -38
  7. package/dist/address/index.d.ts +20 -5
  8. package/dist/address/index.d.ts.map +1 -1
  9. package/dist/address/index.js +43 -32
  10. package/dist/advanced.d.ts +3 -0
  11. package/dist/advanced.d.ts.map +1 -0
  12. package/dist/{types/index.js → advanced.js} +3 -9
  13. package/dist/advancedBarrel.d.ts +4 -0
  14. package/dist/advancedBarrel.d.ts.map +1 -0
  15. package/dist/advancedBarrel.js +19 -0
  16. package/dist/barrel.d.ts +3 -2
  17. package/dist/barrel.d.ts.map +1 -1
  18. package/dist/barrel.js +3 -2
  19. package/dist/bytes.d.ts +15 -0
  20. package/dist/bytes.d.ts.map +1 -0
  21. package/dist/bytes.js +114 -0
  22. package/dist/ckb/advanced.d.ts +3 -0
  23. package/dist/ckb/advanced.d.ts.map +1 -0
  24. package/dist/ckb/advanced.js +18 -0
  25. package/dist/ckb/hasher.advanced.d.ts +2 -0
  26. package/dist/ckb/hasher.advanced.d.ts.map +1 -0
  27. package/dist/ckb/hasher.advanced.js +4 -0
  28. package/dist/ckb/hasher.d.ts +10 -0
  29. package/dist/ckb/hasher.d.ts.map +1 -0
  30. package/dist/ckb/hasher.js +26 -0
  31. package/dist/ckb/index.d.ts +3 -1
  32. package/dist/ckb/index.d.ts.map +1 -1
  33. package/dist/ckb/index.js +3 -1
  34. package/dist/ckb/types/advanced.d.ts +4 -0
  35. package/dist/ckb/types/advanced.d.ts.map +1 -0
  36. package/dist/ckb/types/advanced.js +32 -0
  37. package/dist/ckb/types/index.d.ts +3 -0
  38. package/dist/ckb/types/index.d.ts.map +1 -0
  39. package/dist/{codec → ckb/types}/index.js +1 -2
  40. package/dist/ckb/types/molecule.advanced/generated.d.ts +355 -0
  41. package/dist/ckb/types/molecule.advanced/generated.d.ts.map +1 -0
  42. package/dist/ckb/types/molecule.advanced/generated.js +1255 -0
  43. package/dist/ckb/types/molecule.advanced/index.d.ts +2 -0
  44. package/dist/ckb/types/molecule.advanced/index.d.ts.map +1 -0
  45. package/dist/ckb/types/molecule.advanced/index.js +17 -0
  46. package/dist/ckb/types/script.advanced.d.ts +4 -0
  47. package/dist/ckb/types/script.advanced.d.ts.map +1 -0
  48. package/dist/{types/ckb.advanced.js → ckb/types/script.advanced.js} +3 -3
  49. package/dist/ckb/types/script.d.ts +26 -0
  50. package/dist/ckb/types/script.d.ts.map +1 -0
  51. package/dist/ckb/types/script.js +75 -0
  52. package/dist/ckb/types/transaction.advanced.d.ts +4 -0
  53. package/dist/ckb/types/transaction.advanced.d.ts.map +1 -0
  54. package/dist/ckb/types/transaction.advanced.js +11 -0
  55. package/dist/ckb/types/transaction.d.ts +127 -0
  56. package/dist/ckb/types/transaction.d.ts.map +1 -0
  57. package/dist/ckb/types/transaction.js +266 -0
  58. package/dist/ckb/viewerCkbScript.d.ts +11 -0
  59. package/dist/ckb/viewerCkbScript.d.ts.map +1 -0
  60. package/dist/ckb/viewerCkbScript.js +18 -0
  61. package/dist/client/advanced.d.ts +3 -0
  62. package/dist/client/advanced.d.ts.map +1 -0
  63. package/dist/client/advanced.js +18 -0
  64. package/dist/client/client.d.ts +7 -4
  65. package/dist/client/client.d.ts.map +1 -1
  66. package/dist/client/client.js +1 -4
  67. package/dist/client/clientPublicMainnet.advanced.d.ts +1 -1
  68. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  69. package/dist/client/clientPublicMainnet.d.ts +5 -3
  70. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  71. package/dist/client/clientPublicMainnet.js +6 -3
  72. package/dist/client/clientPublicTestnet.advanced.d.ts +1 -1
  73. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  74. package/dist/client/clientPublicTestnet.d.ts +5 -3
  75. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  76. package/dist/client/clientPublicTestnet.js +6 -3
  77. package/dist/client/clientTypes.d.ts +2 -0
  78. package/dist/client/clientTypes.d.ts.map +1 -0
  79. package/dist/client/index.d.ts +1 -4
  80. package/dist/client/index.d.ts.map +1 -1
  81. package/dist/client/index.js +1 -19
  82. package/dist/client/jsonRpc/advanced.d.ts +88 -0
  83. package/dist/client/jsonRpc/advanced.d.ts.map +1 -0
  84. package/dist/client/jsonRpc/advanced.js +69 -0
  85. package/dist/client/jsonRpc/index.d.ts +14 -0
  86. package/dist/client/jsonRpc/index.d.ts.map +1 -0
  87. package/dist/client/jsonRpc/index.js +64 -0
  88. package/dist/fixedPoint.d.ts +9 -0
  89. package/dist/fixedPoint.d.ts.map +1 -0
  90. package/dist/fixedPoint.js +30 -0
  91. package/dist/primitive.d.ts +5 -0
  92. package/dist/primitive.d.ts.map +1 -0
  93. package/dist/signer/helpers.d.ts +7 -0
  94. package/dist/signer/helpers.d.ts.map +1 -0
  95. package/dist/signer/helpers.js +35 -0
  96. package/dist/signer/index.d.ts +3 -0
  97. package/dist/signer/index.d.ts.map +1 -0
  98. package/dist/signer/index.js +18 -0
  99. package/dist/signer/signer.d.ts +11 -0
  100. package/dist/signer/signer.d.ts.map +1 -0
  101. package/dist/signer/signer.js +13 -0
  102. package/dist/viewer.d.ts +8 -5
  103. package/dist/viewer.d.ts.map +1 -1
  104. package/dist/viewer.js +13 -0
  105. package/package.json +13 -5
  106. package/src/address/address.advanced.ts +88 -0
  107. package/src/address/advanced.ts +30 -45
  108. package/src/address/index.ts +70 -5
  109. package/src/advanced.ts +2 -0
  110. package/src/advancedBarrel.ts +3 -0
  111. package/src/barrel.ts +3 -2
  112. package/src/bytes.ts +127 -0
  113. package/src/ckb/advanced.ts +2 -0
  114. package/src/ckb/hasher.advanced.ts +1 -0
  115. package/src/ckb/hasher.ts +30 -0
  116. package/src/ckb/index.ts +3 -1
  117. package/src/ckb/types/advanced.ts +3 -0
  118. package/src/ckb/types/index.ts +2 -0
  119. package/src/ckb/types/molecule.advanced/blockchain.mol +118 -0
  120. package/src/ckb/types/molecule.advanced/generated.d.ts +433 -0
  121. package/src/ckb/types/molecule.advanced/generated.js +1376 -0
  122. package/src/ckb/types/molecule.advanced/index.ts +1 -0
  123. package/src/ckb/types/script.advanced.ts +14 -0
  124. package/src/ckb/types/script.ts +70 -0
  125. package/src/ckb/types/transaction.advanced.ts +10 -0
  126. package/src/ckb/types/transaction.ts +366 -0
  127. package/src/ckb/viewerCkbScript.ts +21 -0
  128. package/src/client/advanced.ts +2 -0
  129. package/src/client/client.ts +11 -4
  130. package/src/client/clientPublicMainnet.advanced.ts +1 -1
  131. package/src/client/clientPublicMainnet.ts +9 -4
  132. package/src/client/clientPublicTestnet.advanced.ts +1 -1
  133. package/src/client/clientPublicTestnet.ts +9 -4
  134. package/src/client/clientTypes.ts +1 -0
  135. package/src/client/index.ts +1 -7
  136. package/src/client/jsonRpc/advanced.ts +93 -0
  137. package/src/client/jsonRpc/index.ts +83 -0
  138. package/src/fixedPoint.ts +53 -0
  139. package/src/primitive.ts +8 -0
  140. package/src/signer/helpers.ts +43 -0
  141. package/src/signer/index.ts +2 -0
  142. package/src/signer/signer.ts +19 -0
  143. package/src/viewer.ts +16 -8
  144. package/dist/address/address.d.ts +0 -8
  145. package/dist/address/address.d.ts.map +0 -1
  146. package/dist/address/address.js +0 -6
  147. package/dist/address/addressKnownScript.d.ts +0 -13
  148. package/dist/address/addressKnownScript.d.ts.map +0 -1
  149. package/dist/address/addressKnownScript.js +0 -27
  150. package/dist/address/addressPlain.d.ts +0 -12
  151. package/dist/address/addressPlain.d.ts.map +0 -1
  152. package/dist/address/addressPlain.js +0 -23
  153. package/dist/address/converter.d.ts +0 -8
  154. package/dist/address/converter.d.ts.map +0 -1
  155. package/dist/address/converter.js +0 -65
  156. package/dist/bundle.d.ts +0 -9
  157. package/dist/bundle.d.ts.map +0 -1
  158. package/dist/bundle.js +0 -25
  159. package/dist/ckb/viewerCkbAddress.d.ts +0 -14
  160. package/dist/ckb/viewerCkbAddress.d.ts.map +0 -1
  161. package/dist/ckb/viewerCkbAddress.js +0 -28
  162. package/dist/codec/bytes.d.ts +0 -4
  163. package/dist/codec/bytes.d.ts.map +0 -1
  164. package/dist/codec/bytes.js +0 -29
  165. package/dist/codec/hashType.d.ts +0 -4
  166. package/dist/codec/hashType.d.ts.map +0 -1
  167. package/dist/codec/hashType.js +0 -13
  168. package/dist/codec/index.d.ts +0 -4
  169. package/dist/codec/index.d.ts.map +0 -1
  170. package/dist/codec/script.d.ts +0 -4
  171. package/dist/codec/script.d.ts.map +0 -1
  172. package/dist/codec/script.js +0 -22
  173. package/dist/signer.d.ts +0 -9
  174. package/dist/signer.d.ts.map +0 -1
  175. package/dist/signer.js +0 -12
  176. package/dist/tsconfig.tsbuildinfo +0 -1
  177. package/dist/types/cell.d.ts +0 -5
  178. package/dist/types/cell.d.ts.map +0 -1
  179. package/dist/types/cell.js +0 -8
  180. package/dist/types/ckb.advanced.d.ts +0 -4
  181. package/dist/types/ckb.advanced.d.ts.map +0 -1
  182. package/dist/types/ckb.d.ts +0 -16
  183. package/dist/types/ckb.d.ts.map +0 -1
  184. package/dist/types/ckb.js +0 -36
  185. package/dist/types/index.d.ts +0 -9
  186. package/dist/types/index.d.ts.map +0 -1
  187. package/dist/types/lumos.d.ts +0 -90
  188. package/dist/types/lumos.d.ts.map +0 -1
  189. package/dist/types/primitive.d.ts +0 -3
  190. package/dist/types/primitive.d.ts.map +0 -1
  191. package/src/address/address.ts +0 -9
  192. package/src/address/addressKnownScript.ts +0 -31
  193. package/src/address/addressPlain.ts +0 -27
  194. package/src/address/converter.ts +0 -93
  195. package/src/ckb/viewerCkbAddress.ts +0 -34
  196. package/src/codec/bytes.ts +0 -30
  197. package/src/codec/hashType.ts +0 -10
  198. package/src/codec/index.ts +0 -3
  199. package/src/codec/script.ts +0 -21
  200. package/src/signer.ts +0 -20
  201. package/src/types/cell.ts +0 -5
  202. package/src/types/ckb.advanced.ts +0 -14
  203. package/src/types/ckb.ts +0 -47
  204. package/src/types/index.ts +0 -9
  205. package/src/types/lumos.ts +0 -101
  206. package/src/types/primitive.ts +0 -2
  207. /package/dist/{types/lumos.js → client/clientTypes.js} +0 -0
  208. /package/dist/{types/primitive.js → primitive.js} +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"viewer.d.ts","sourceRoot":"","sources":["../src/viewer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,8BAAsB,MAAM;IAC1B,QAAQ,CAAC,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC;IAErC,QAAQ,CAAC,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IACrE,QAAQ,CAAC,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAG1C,QAAQ,CAAC,wBAAwB,CAC/B,EAAE,EAAE,uBAAuB,GAC1B,OAAO,CAAC,uBAAuB,CAAC;CACpC"}
1
+ {"version":3,"file":"viewer.d.ts","sourceRoot":"","sources":["../src/viewer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,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;CAKxC"}
package/dist/viewer.js CHANGED
@@ -1,6 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Viewer = void 0;
4
+ const address_1 = require("./address");
4
5
  class Viewer {
6
+ constructor(client) {
7
+ this.client = client;
8
+ }
9
+ async getRecommendedAddressObj(_preference) {
10
+ return (await this.getAddressObjs())[0];
11
+ }
12
+ async getRecommendedAddress(preference) {
13
+ return address_1.Address.toString(await this.getRecommendedAddressObj(preference));
14
+ }
15
+ async getAddresses() {
16
+ return this.getAddressObjs().then((addresses) => addresses.map((address) => address_1.Address.toString(address)));
17
+ }
5
18
  }
6
19
  exports.Viewer = Viewer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.2-alpha.0",
3
+ "version": "0.0.2-alpha.2",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -8,18 +8,24 @@
8
8
  "main": "dist/index.js",
9
9
  "exports": {
10
10
  ".": "./dist/index.js",
11
- "./barrel": "./dist/barrel.js"
11
+ "./barrel": "./dist/barrel.js",
12
+ "./advancedBarrel": "./dist/advancedBarrel.js",
13
+ "./advanced": "./dist/advanced.js"
12
14
  },
13
15
  "scripts": {
14
- "build": "tsc",
16
+ "build": "rimraf ./dist && tsc",
15
17
  "lint": "eslint ."
16
18
  },
17
19
  "devDependencies": {
20
+ "@ckb-lumos/helpers": "^0.22.2",
18
21
  "@eslint/js": "^9.1.1",
22
+ "@types/blake2b": "^2.1.3",
19
23
  "eslint": "^9.1.0",
20
24
  "eslint-config-prettier": "^9.1.0",
21
25
  "eslint-plugin-prettier": "^5.1.3",
22
26
  "prettier": "^3.2.5",
27
+ "rimraf": "^5.0.5",
28
+ "ts-essentials": "^9.4.2",
23
29
  "typescript": "^5.4.5",
24
30
  "typescript-eslint": "^7.7.0"
25
31
  },
@@ -27,9 +33,11 @@
27
33
  "access": "public"
28
34
  },
29
35
  "dependencies": {
36
+ "abort-controller": "^3.0.0",
30
37
  "bech32": "^2.0.0",
38
+ "blake2b": "^2.1.4",
31
39
  "buffer": "^6.0.3",
32
- "immutable": "5.0.0-beta.5"
40
+ "cross-fetch": "^4.0.0"
33
41
  },
34
- "gitHead": "c0cf1de7309204ba3121121bae68e2ef002f001f"
42
+ "gitHead": "dae8f0bcf3554125cebee4b9497c0045d7195a63"
35
43
  }
@@ -0,0 +1,88 @@
1
+ import { Client, KnownScript } from "../client";
2
+ import { toHex } from "../bytes";
3
+ import { AddressFormat } from "./advanced";
4
+ import { type Address as AddressType } from "./index";
5
+ import { decodeHashType } from "../ckb";
6
+
7
+ export async function fromAddressPayload(
8
+ prefix: string,
9
+ format: AddressFormat,
10
+ payload: number[],
11
+ client: Client,
12
+ ): Promise<AddressType> {
13
+ if (format === AddressFormat.Full) {
14
+ if (payload.length < 32 + 1) {
15
+ throw new Error(
16
+ `Invalid full address without enough payload ${toHex(payload)}`,
17
+ );
18
+ }
19
+
20
+ return {
21
+ script: {
22
+ codeHash: toHex(payload.slice(0, 32)),
23
+ hashType: decodeHashType(payload.slice(32, 33)),
24
+ args: toHex(payload.slice(33)),
25
+ },
26
+ prefix,
27
+ };
28
+ }
29
+
30
+ if (format === AddressFormat.FullData) {
31
+ if (payload.length < 32) {
32
+ throw new Error(
33
+ `Invalid full data address without enough payload ${toHex(payload)}`,
34
+ );
35
+ }
36
+
37
+ return {
38
+ script: {
39
+ codeHash: toHex(payload.slice(0, 32)),
40
+ hashType: "data",
41
+ args: toHex(payload.slice(32)),
42
+ },
43
+ prefix,
44
+ };
45
+ }
46
+
47
+ if (format === AddressFormat.FullType) {
48
+ if (payload.length < 32) {
49
+ throw new Error(
50
+ `Invalid full type address without enough payload ${toHex(payload)}`,
51
+ );
52
+ }
53
+
54
+ return {
55
+ script: {
56
+ codeHash: toHex(payload.slice(0, 32)),
57
+ hashType: "type",
58
+ args: toHex(payload.slice(32)),
59
+ },
60
+ prefix,
61
+ };
62
+ }
63
+
64
+ // format === AddressFormat.Short
65
+ if (payload.length !== 21) {
66
+ throw new Error(
67
+ `Invalid short address without enough payload ${toHex(payload)}`,
68
+ );
69
+ }
70
+ const script = [
71
+ KnownScript.Secp256k1Blake160,
72
+ KnownScript.Secp256k1Multisig,
73
+ KnownScript.AnyoneCanPay,
74
+ ][payload[0]];
75
+ if (script === undefined) {
76
+ throw new Error(
77
+ `Invalid short address with unknown script ${toHex(payload)}`,
78
+ );
79
+ }
80
+
81
+ return {
82
+ script: {
83
+ ...(await client.getKnownScript(script)),
84
+ args: toHex(payload.slice(1)),
85
+ },
86
+ prefix,
87
+ };
88
+ }
@@ -1,52 +1,25 @@
1
1
  import { bech32, bech32m } from "bech32";
2
- import { Script } from "../types";
3
- import { Client } from "../client";
4
- import { ClientPublicTestnet, ClientPublicMainnet } from "../client";
5
- import { encodeScript } from "../codec";
2
+ export * as Address from "./address.advanced";
6
3
 
7
- export const BECH32_LIMIT = 1023;
8
-
9
- export enum AddressFormat {
10
- /**
11
- * full version identifies the hashType
12
- */
13
- Full = 0x00,
14
- /**
15
- * @deprecated
16
- * short version for locks with Known codeHash, deprecated
17
- */
18
- Short = 0x01,
19
- /**
20
- * @deprecated
21
- * full version with hashType = "Data", deprecated
22
- */
23
- FullData = 0x02,
24
- /**
25
- * @deprecated
26
- * full version with hashType = "Type", deprecated
27
- */
28
- FullType = 0x04,
29
- }
30
-
31
- export function parseAddress(address: string): {
4
+ export function parseAddressToPayload(address: string): {
32
5
  prefix: string;
33
6
  format: AddressFormat;
34
- body: number[];
7
+ payload: number[];
35
8
  } {
36
9
  // Try parse full format address
37
10
  {
38
11
  const { words, prefix } = bech32m.decode(address, BECH32_LIMIT);
39
- const [formatType, ...body] = bech32m.fromWords(words);
12
+ const [formatType, ...payload] = bech32m.fromWords(words);
40
13
 
41
- if (formatType === AddressFormat.Full) {
42
- return { prefix, format: AddressFormat.Full, body };
14
+ if (formatType === (AddressFormat.Full as number)) {
15
+ return { prefix, format: AddressFormat.Full, payload };
43
16
  }
44
17
  }
45
18
 
46
19
  // Try parse legacy 2019 format address
47
20
  {
48
21
  const { prefix, words } = bech32.decode(address, BECH32_LIMIT);
49
- const [formatType, ...body] = bech32.fromWords(words);
22
+ const [formatType, ...payload] = bech32.fromWords(words);
50
23
  if (
51
24
  [
52
25
  AddressFormat.FullData,
@@ -54,21 +27,33 @@ export function parseAddress(address: string): {
54
27
  AddressFormat.Short,
55
28
  ].includes(formatType)
56
29
  ) {
57
- return { prefix, format: formatType, body };
30
+ return { prefix, format: formatType, payload };
58
31
  }
59
32
  }
60
33
 
61
34
  throw Error(`Unknown address format ${address}`);
62
35
  }
63
36
 
64
- export function decodeAddressInfo(format: AddressFormat, body: number[]) {}
65
-
66
- export function getClientFromAddressPrefix(prefix: string): Client | undefined {
67
- if (prefix === "ckb") {
68
- return new ClientPublicMainnet();
69
- }
70
-
71
- if (prefix === "ckt") {
72
- return new ClientPublicTestnet();
73
- }
37
+ export enum AddressFormat {
38
+ /**
39
+ * full version identifies the hashType
40
+ */
41
+ Full = 0x00,
42
+ /**
43
+ * @deprecated
44
+ * short version for locks with Known codeHash, deprecated
45
+ */
46
+ Short = 0x01,
47
+ /**
48
+ * @deprecated
49
+ * full version with hashType = "Data", deprecated
50
+ */
51
+ FullData = 0x02,
52
+ /**
53
+ * @deprecated
54
+ * full version with hashType = "Type", deprecated
55
+ */
56
+ FullType = 0x04,
74
57
  }
58
+
59
+ export const BECH32_LIMIT = 1023;
@@ -1,5 +1,70 @@
1
- export * from "./address";
2
- export * from "./addressPlain";
3
- export * from "./addressKnownScript";
4
- export * as AdvancedAddress from "./advanced";
5
- export * from "./converter";
1
+ import { DeepReadonly } from "ts-essentials";
2
+ import { Client, KnownScript } from "../client";
3
+ import { Script, encodeHashType } from "../ckb";
4
+ import {
5
+ AddressFormat,
6
+ BECH32_LIMIT,
7
+ Address as AdvancedAddress,
8
+ parseAddressToPayload,
9
+ } from "./advanced";
10
+ import { bech32m } from "bech32";
11
+ import { Hex } from "../primitive";
12
+ import { concatBytes, toBytes } from "../bytes";
13
+
14
+ export class Address {
15
+ constructor(
16
+ public script: Script,
17
+ public prefix: string,
18
+ ) {}
19
+
20
+ static async fromString(
21
+ address: string,
22
+ clients: Client | Record<string, Client>,
23
+ ): Promise<Address> {
24
+ const { prefix, format, payload } = parseAddressToPayload(address);
25
+
26
+ const client = (clients as Record<string, Client>)[prefix] ?? clients;
27
+ if (!client) {
28
+ throw new Error(`Unknown address prefix ${prefix}`);
29
+ }
30
+ const expectedPrefix = await client.getAddressPrefix();
31
+ if (expectedPrefix !== prefix) {
32
+ throw new Error(
33
+ `Unknown address prefix ${prefix}, expected ${expectedPrefix}`,
34
+ );
35
+ }
36
+
37
+ return AdvancedAddress.fromAddressPayload(prefix, format, payload, client);
38
+ }
39
+
40
+ static async fromScript(
41
+ script: DeepReadonly<Script>,
42
+ client: Client,
43
+ ): Promise<Address> {
44
+ return {
45
+ script: { ...script },
46
+ prefix: await client.getAddressPrefix(),
47
+ };
48
+ }
49
+
50
+ static async fromKnownScript(script: KnownScript, args: Hex, client: Client) {
51
+ return {
52
+ script: {
53
+ ...(await client.getKnownScript(script)),
54
+ args,
55
+ },
56
+ prefix: await client.getAddressPrefix(),
57
+ };
58
+ }
59
+
60
+ static toString(address: DeepReadonly<Address>): string {
61
+ const data = concatBytes(
62
+ [AddressFormat.Full],
63
+ toBytes(address.script.codeHash),
64
+ encodeHashType(address.script.hashType),
65
+ toBytes(address.script.args),
66
+ );
67
+
68
+ return bech32m.encode(address.prefix, bech32m.toWords(data), BECH32_LIMIT);
69
+ }
70
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./advancedBarrel";
2
+ export * as cccA from "./advancedBarrel";
@@ -0,0 +1,3 @@
1
+ export * from "./address/advanced";
2
+ export * from "./ckb/advanced";
3
+ export * from "./client/advanced";
package/src/barrel.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export * from "./address";
2
+ export * from "./bytes";
2
3
  export * from "./ckb";
3
4
  export * from "./client";
4
- export * from "./codec";
5
- export * from "./types";
5
+ export * from "./fixedPoint";
6
+ export * from "./primitive";
6
7
  export * from "./signer";
7
8
  export * from "./viewer";
package/src/bytes.ts ADDED
@@ -0,0 +1,127 @@
1
+ import { BytesLike, Hex, HexLike, NumberLike } from "./primitive";
2
+ import { Buffer } from "buffer/";
3
+
4
+ export function concatBytes(...args: BytesLike[]): Uint8Array {
5
+ return new Uint8Array(
6
+ args.reduce((acc: number[], v) => {
7
+ acc.push(...toBytes(v));
8
+ return acc;
9
+ }, []),
10
+ );
11
+ }
12
+
13
+ export function encodeUtf8(val: string): Uint8Array {
14
+ return Buffer.from(val, "utf-8");
15
+ }
16
+
17
+ export function decodeUtf8(val: BytesLike): string {
18
+ return Buffer.from(toBytes(val)).toString("utf-8");
19
+ }
20
+
21
+ export function toBytes(bytes: BytesLike): Uint8Array {
22
+ if (bytes instanceof Uint8Array) {
23
+ return bytes;
24
+ }
25
+
26
+ if (bytes instanceof ArrayBuffer) {
27
+ return new Uint8Array(bytes);
28
+ }
29
+
30
+ if (Array.isArray(bytes)) {
31
+ if (bytes.some((v) => v < 0 || 0xff < v)) {
32
+ throw new Error(`Invalid bytes ${JSON.stringify(bytes)}`);
33
+ }
34
+ return new Uint8Array(bytes);
35
+ }
36
+
37
+ const str = bytes.length % 2 === 0 ? bytes.slice(2) : `0${bytes.slice(2)}`;
38
+ const data = Buffer.from(str, "hex");
39
+ if (data.length * 2 !== str.length) {
40
+ throw new Error(`Invalid bytes ${bytes}`);
41
+ }
42
+ return data;
43
+ }
44
+
45
+ export function toHexFromHex(bytes: HexLike): Hex {
46
+ if (typeof bytes === "string" && !bytes.startsWith("0x")) {
47
+ return `0x${bytes}`;
48
+ }
49
+
50
+ return toHex(bytes);
51
+ }
52
+
53
+ export function toHex(bytes: HexLike): Hex {
54
+ if (typeof bytes === "number") {
55
+ return `0x${bytes.toString(16)}`;
56
+ }
57
+
58
+ if (typeof bytes === "string" && !bytes.startsWith("0x")) {
59
+ return `0x${BigInt(bytes).toString(16)}`;
60
+ }
61
+
62
+ return `0x${Buffer.from(toBytes(bytes as BytesLike)).toString("hex")}`;
63
+ }
64
+
65
+ export function toNumber(val: NumberLike): string {
66
+ return BigInt(val).toString();
67
+ }
68
+
69
+ export function toBytesFromNumber(val: NumberLike, bytes?: number): Uint8Array {
70
+ return toBytesFromLENumber(val, bytes);
71
+ }
72
+
73
+ export function toBytesFromLENumber(
74
+ val: NumberLike,
75
+ bytes?: number,
76
+ ): Uint8Array {
77
+ return toBytesFromBENumber(val, bytes).reverse();
78
+ }
79
+
80
+ export function toBytesFromBENumber(
81
+ val: NumberLike,
82
+ bytes?: number,
83
+ ): Uint8Array {
84
+ let valStr = val;
85
+ if (typeof valStr === "number" || typeof valStr === "bigint") {
86
+ valStr = valStr.toString(16);
87
+ } else if (typeof valStr === "string") {
88
+ if (valStr.startsWith("0x")) {
89
+ valStr = valStr.slice(2);
90
+ } else {
91
+ valStr = BigInt(valStr).toString(16);
92
+ }
93
+ }
94
+
95
+ if (valStr.length % 2 !== 0) {
96
+ valStr = `0${valStr}`;
97
+ }
98
+
99
+ const res = toBytes(`0x${valStr}`);
100
+ if (bytes == null) {
101
+ return res;
102
+ }
103
+ if (res.length > bytes) {
104
+ return res.slice(res.length - bytes);
105
+ }
106
+ return new Uint8Array([
107
+ ...Array.from(Array(bytes - res.length), () => 0),
108
+ ...res,
109
+ ]);
110
+ }
111
+
112
+ export function toNumberFromBytes(val: BytesLike): string {
113
+ return toLENumberFromBytes(val);
114
+ }
115
+
116
+ export function toLENumberFromBytes(val: BytesLike): string {
117
+ return toBENumberFromBytes(toBytes(val).reverse());
118
+ }
119
+
120
+ export function toBENumberFromBytes(val: BytesLike): string {
121
+ const str = toHex(val).replace(/0x0*/, "0x");
122
+ if (str.length === 2) {
123
+ return "0";
124
+ }
125
+
126
+ return BigInt(str).toString();
127
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./hasher.advanced";
2
+ export * from "./types/advanced";
@@ -0,0 +1 @@
1
+ export const CKB_BLAKE2B_PERSONAL = "ckb-default-hash";
@@ -0,0 +1,30 @@
1
+ import blake2b, { Blake2b } from "blake2b";
2
+ import { BytesLike, Hex } from "../primitive";
3
+ import { encodeUtf8, toBytes } from "../bytes";
4
+ import { CKB_BLAKE2B_PERSONAL } from "./hasher.advanced";
5
+
6
+ export class Hasher {
7
+ hasher: Blake2b;
8
+
9
+ constructor(outLength = 32, personal = CKB_BLAKE2B_PERSONAL) {
10
+ this.hasher = blake2b(
11
+ outLength,
12
+ undefined,
13
+ undefined,
14
+ encodeUtf8(personal),
15
+ );
16
+ }
17
+
18
+ update(data: BytesLike): Hasher {
19
+ this.hasher.update(toBytes(data));
20
+ return this;
21
+ }
22
+
23
+ digest(): Hex {
24
+ return `0x${this.hasher.digest("hex")}`;
25
+ }
26
+ }
27
+
28
+ export function ckbHash(data: BytesLike): Hex {
29
+ return new Hasher().update(data).digest();
30
+ }
package/src/ckb/index.ts CHANGED
@@ -1 +1,3 @@
1
- export * from "./viewerCkbAddress";
1
+ export * from "./hasher";
2
+ export * from "./types";
3
+ export * from "./viewerCkbScript";
@@ -0,0 +1,3 @@
1
+ export * from "./script.advanced";
2
+ export * from "./transaction.advanced";
3
+ export * as moleculeCodecCkb from "./molecule.advanced";
@@ -0,0 +1,2 @@
1
+ export * from "./script";
2
+ export * from "./transaction";
@@ -0,0 +1,118 @@
1
+ /* Basic Types */
2
+
3
+ // The `UintN` is used to store a `N` bits unsigned integer
4
+ // as a byte array in little endian.
5
+ array Uint32 [byte; 4];
6
+ array Uint64 [byte; 8];
7
+ array Uint128 [byte; 16];
8
+ array Byte32 [byte; 32];
9
+ array Uint256 [byte; 32];
10
+
11
+ vector Bytes <byte>;
12
+ option BytesOpt (Bytes);
13
+ vector BytesOptVec <BytesOpt>;
14
+ vector BytesVec <Bytes>;
15
+ vector Byte32Vec <Byte32>;
16
+
17
+ /* Types for Chain */
18
+
19
+ option ScriptOpt (Script);
20
+
21
+ array ProposalShortId [byte; 10];
22
+
23
+ vector UncleBlockVec <UncleBlock>;
24
+ vector TransactionVec <Transaction>;
25
+ vector ProposalShortIdVec <ProposalShortId>;
26
+ vector CellDepVec <CellDep>;
27
+ vector CellInputVec <CellInput>;
28
+ vector CellOutputVec <CellOutput>;
29
+
30
+ table Script {
31
+ codeHash: Byte32,
32
+ hashType: byte,
33
+ args: Bytes,
34
+ }
35
+
36
+ struct OutPoint {
37
+ txHash: Byte32,
38
+ index: Uint32,
39
+ }
40
+
41
+ struct CellInput {
42
+ since: Uint64,
43
+ previousOutput: OutPoint,
44
+ }
45
+
46
+ table CellOutput {
47
+ capacity: Uint64,
48
+ lock: Script,
49
+ type: ScriptOpt,
50
+ }
51
+
52
+ struct CellDep {
53
+ outPoint: OutPoint,
54
+ depType: byte,
55
+ }
56
+
57
+ table RawTransaction {
58
+ version: Uint32,
59
+ cellDeps: CellDepVec,
60
+ headerDeps: Byte32Vec,
61
+ inputs: CellInputVec,
62
+ outputs: CellOutputVec,
63
+ outputsData: BytesVec,
64
+ }
65
+
66
+ table Transaction {
67
+ raw: RawTransaction,
68
+ witnesses: BytesVec,
69
+ }
70
+
71
+ struct RawHeader {
72
+ version: Uint32,
73
+ compactTarget: Uint32,
74
+ timestamp: Uint64,
75
+ number: Uint64,
76
+ epoch: Uint64,
77
+ parentHash: Byte32,
78
+ transactionsRoot: Byte32,
79
+ proposalsHash: Byte32,
80
+ extraHash: Byte32,
81
+ dao: Byte32,
82
+ }
83
+
84
+ struct Header {
85
+ raw: RawHeader,
86
+ nonce: Uint128,
87
+ }
88
+
89
+ table UncleBlock {
90
+ header: Header,
91
+ proposals: ProposalShortIdVec,
92
+ }
93
+
94
+ table Block {
95
+ header: Header,
96
+ uncles: UncleBlockVec,
97
+ transactions: TransactionVec,
98
+ proposals: ProposalShortIdVec,
99
+ }
100
+
101
+ table BlockV1 {
102
+ header: Header,
103
+ uncles: UncleBlockVec,
104
+ transactions: TransactionVec,
105
+ proposals: ProposalShortIdVec,
106
+ extension: Bytes,
107
+ }
108
+
109
+ table CellbaseWitness {
110
+ lock: Script,
111
+ message: Bytes,
112
+ }
113
+
114
+ table WitnessArgs {
115
+ lock: BytesOpt, // Lock args
116
+ inputType: BytesOpt, // Type args for input
117
+ outputType: BytesOpt, // Type args for output
118
+ }