@ckb-ccc/core 0.0.2-alpha.2 → 0.0.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 (185) hide show
  1. package/.prettierignore +12 -0
  2. package/dist/address/address.advanced.d.ts +29 -3
  3. package/dist/address/address.advanced.d.ts.map +1 -1
  4. package/dist/address/address.advanced.js +70 -28
  5. package/dist/address/advanced.d.ts +1 -28
  6. package/dist/address/advanced.d.ts.map +1 -1
  7. package/dist/address/advanced.js +1 -75
  8. package/dist/address/index.d.ts +10 -13
  9. package/dist/address/index.d.ts.map +1 -1
  10. package/dist/address/index.js +21 -25
  11. package/dist/advanced.js +2 -31
  12. package/dist/advancedBarrel.d.ts +1 -0
  13. package/dist/advancedBarrel.d.ts.map +1 -1
  14. package/dist/advancedBarrel.js +4 -19
  15. package/dist/barrel.d.ts +4 -2
  16. package/dist/barrel.d.ts.map +1 -1
  17. package/dist/barrel.js +10 -24
  18. package/dist/bytes/index.d.ts +7 -0
  19. package/dist/bytes/index.d.ts.map +1 -0
  20. package/dist/bytes/index.js +34 -0
  21. package/dist/ckb/advanced.d.ts +3 -2
  22. package/dist/ckb/advanced.d.ts.map +1 -1
  23. package/dist/ckb/advanced.js +3 -18
  24. package/dist/ckb/index.d.ts +2 -3
  25. package/dist/ckb/index.d.ts.map +1 -1
  26. package/dist/ckb/index.js +2 -19
  27. package/dist/ckb/molecule.advanced/generated.d.ts.map +1 -0
  28. package/dist/ckb/{types/molecule.advanced → molecule.advanced}/generated.js +444 -231
  29. package/dist/ckb/molecule.advanced/index.d.ts +6 -0
  30. package/dist/ckb/molecule.advanced/index.d.ts.map +1 -0
  31. package/dist/ckb/molecule.advanced/index.js +7 -0
  32. package/dist/ckb/{types/script.advanced.d.ts → script.advanced.d.ts} +1 -0
  33. package/dist/ckb/script.advanced.d.ts.map +1 -0
  34. package/dist/ckb/script.advanced.js +13 -0
  35. package/dist/ckb/script.d.ts +29 -0
  36. package/dist/ckb/script.d.ts.map +1 -0
  37. package/dist/ckb/script.js +61 -0
  38. package/dist/ckb/{types/transaction.advanced.d.ts → transaction.advanced.d.ts} +1 -0
  39. package/dist/ckb/transaction.advanced.d.ts.map +1 -0
  40. package/dist/ckb/transaction.advanced.js +9 -0
  41. package/dist/ckb/transaction.d.ts +142 -0
  42. package/dist/ckb/transaction.d.ts.map +1 -0
  43. package/dist/ckb/transaction.js +227 -0
  44. package/dist/client/advanced.js +2 -18
  45. package/dist/client/client.d.ts +5 -4
  46. package/dist/client/client.d.ts.map +1 -1
  47. package/dist/client/client.js +2 -5
  48. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  49. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  50. package/dist/client/clientPublicMainnet.advanced.js +7 -10
  51. package/dist/client/clientPublicMainnet.d.ts +2 -2
  52. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  53. package/dist/client/clientPublicMainnet.js +4 -8
  54. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  55. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  56. package/dist/client/clientPublicTestnet.advanced.js +7 -10
  57. package/dist/client/clientPublicTestnet.d.ts +2 -2
  58. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  59. package/dist/client/clientPublicTestnet.js +4 -8
  60. package/dist/client/clientTypes.js +1 -2
  61. package/dist/client/index.js +4 -20
  62. package/dist/client/jsonRpc/advanced.d.ts +36 -36
  63. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  64. package/dist/client/jsonRpc/advanced.js +13 -17
  65. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  66. package/dist/client/jsonRpc/index.js +5 -13
  67. package/dist/fixedPoint/index.d.ts +7 -0
  68. package/dist/fixedPoint/index.d.ts.map +1 -0
  69. package/dist/fixedPoint/index.js +22 -0
  70. package/dist/{ckb/hasher.advanced.d.ts → hasher/advanced.d.ts} +1 -1
  71. package/dist/hasher/advanced.d.ts.map +1 -0
  72. package/dist/{ckb/hasher.d.ts → hasher/index.d.ts} +3 -2
  73. package/dist/hasher/index.d.ts.map +1 -0
  74. package/dist/hasher/index.js +18 -0
  75. package/dist/hex/index.d.ts +5 -0
  76. package/dist/hex/index.d.ts.map +1 -0
  77. package/dist/hex/index.js +5 -0
  78. package/dist/index.js +2 -31
  79. package/dist/num/index.d.ts +13 -0
  80. package/dist/num/index.d.ts.map +1 -0
  81. package/dist/num/index.js +36 -0
  82. package/dist/signer/helpers.d.ts +3 -3
  83. package/dist/signer/helpers.d.ts.map +1 -1
  84. package/dist/signer/helpers.js +9 -10
  85. package/dist/signer/index.d.ts +2 -0
  86. package/dist/signer/index.d.ts.map +1 -1
  87. package/dist/signer/index.js +4 -18
  88. package/dist/signer/signer.d.ts +12 -3
  89. package/dist/signer/signer.d.ts.map +1 -1
  90. package/dist/signer/signer.js +13 -6
  91. package/dist/signer/signerReadonly.d.ts +8 -0
  92. package/dist/signer/signerReadonly.d.ts.map +1 -0
  93. package/dist/signer/signerReadonly.js +9 -0
  94. package/dist/{ckb/viewerCkbScript.d.ts → signer/signerReadonlyCkbScript.d.ts} +6 -5
  95. package/dist/signer/signerReadonlyCkbScript.d.ts.map +1 -0
  96. package/dist/signer/signerReadonlyCkbScript.js +17 -0
  97. package/dist/utils/index.d.ts +7 -0
  98. package/dist/utils/index.d.ts.map +1 -0
  99. package/dist/utils/index.js +6 -0
  100. package/package.json +5 -3
  101. package/src/address/address.advanced.ts +76 -19
  102. package/src/address/advanced.ts +1 -59
  103. package/src/address/index.ts +46 -28
  104. package/src/advancedBarrel.ts +1 -0
  105. package/src/barrel.ts +4 -2
  106. package/src/bytes/index.ts +46 -0
  107. package/src/ckb/advanced.ts +3 -2
  108. package/src/ckb/index.ts +2 -3
  109. package/src/ckb/{types/molecule.advanced → molecule.advanced}/generated.d.ts +18 -7
  110. package/src/ckb/{types/molecule.advanced → molecule.advanced}/generated.js +551 -123
  111. package/src/ckb/molecule.advanced/index.ts +11 -0
  112. package/src/ckb/{types/script.advanced.ts → script.advanced.ts} +1 -0
  113. package/src/ckb/script.ts +94 -0
  114. package/src/ckb/{types/transaction.advanced.ts → transaction.advanced.ts} +1 -0
  115. package/src/ckb/transaction.ts +357 -0
  116. package/src/client/client.ts +6 -4
  117. package/src/client/clientPublicMainnet.advanced.ts +2 -2
  118. package/src/client/clientPublicMainnet.ts +2 -2
  119. package/src/client/clientPublicTestnet.advanced.ts +2 -2
  120. package/src/client/clientPublicTestnet.ts +2 -2
  121. package/src/client/clientTypes.ts +1 -1
  122. package/src/client/jsonRpc/advanced.ts +25 -25
  123. package/src/client/jsonRpc/index.ts +1 -3
  124. package/src/fixedPoint/index.ts +30 -0
  125. package/src/hasher/advanced.ts +1 -0
  126. package/src/{ckb/hasher.ts → hasher/index.ts} +5 -5
  127. package/src/hex/index.ts +9 -0
  128. package/src/index.ts +1 -1
  129. package/src/num/index.ts +53 -0
  130. package/src/signer/helpers.ts +9 -6
  131. package/src/signer/index.ts +3 -1
  132. package/src/signer/signer.ts +23 -3
  133. package/src/signer/signerReadonly.ts +13 -0
  134. package/src/{ckb/viewerCkbScript.ts → signer/signerReadonlyCkbScript.ts} +8 -4
  135. package/src/utils/index.ts +28 -0
  136. package/dist/bytes.d.ts +0 -15
  137. package/dist/bytes.d.ts.map +0 -1
  138. package/dist/bytes.js +0 -114
  139. package/dist/ckb/hasher.advanced.d.ts.map +0 -1
  140. package/dist/ckb/hasher.advanced.js +0 -4
  141. package/dist/ckb/hasher.d.ts.map +0 -1
  142. package/dist/ckb/hasher.js +0 -26
  143. package/dist/ckb/types/advanced.d.ts +0 -4
  144. package/dist/ckb/types/advanced.d.ts.map +0 -1
  145. package/dist/ckb/types/advanced.js +0 -32
  146. package/dist/ckb/types/index.d.ts +0 -3
  147. package/dist/ckb/types/index.d.ts.map +0 -1
  148. package/dist/ckb/types/index.js +0 -18
  149. package/dist/ckb/types/molecule.advanced/generated.d.ts.map +0 -1
  150. package/dist/ckb/types/molecule.advanced/index.d.ts +0 -2
  151. package/dist/ckb/types/molecule.advanced/index.d.ts.map +0 -1
  152. package/dist/ckb/types/molecule.advanced/index.js +0 -17
  153. package/dist/ckb/types/script.advanced.d.ts.map +0 -1
  154. package/dist/ckb/types/script.advanced.js +0 -15
  155. package/dist/ckb/types/script.d.ts +0 -26
  156. package/dist/ckb/types/script.d.ts.map +0 -1
  157. package/dist/ckb/types/script.js +0 -75
  158. package/dist/ckb/types/transaction.advanced.d.ts.map +0 -1
  159. package/dist/ckb/types/transaction.advanced.js +0 -11
  160. package/dist/ckb/types/transaction.d.ts +0 -127
  161. package/dist/ckb/types/transaction.d.ts.map +0 -1
  162. package/dist/ckb/types/transaction.js +0 -266
  163. package/dist/ckb/viewerCkbScript.d.ts.map +0 -1
  164. package/dist/ckb/viewerCkbScript.js +0 -18
  165. package/dist/fixedPoint.d.ts +0 -9
  166. package/dist/fixedPoint.d.ts.map +0 -1
  167. package/dist/fixedPoint.js +0 -30
  168. package/dist/primitive.d.ts +0 -5
  169. package/dist/primitive.d.ts.map +0 -1
  170. package/dist/primitive.js +0 -2
  171. package/dist/viewer.d.ts +0 -12
  172. package/dist/viewer.d.ts.map +0 -1
  173. package/dist/viewer.js +0 -19
  174. package/src/bytes.ts +0 -127
  175. package/src/ckb/types/advanced.ts +0 -3
  176. package/src/ckb/types/index.ts +0 -2
  177. package/src/ckb/types/molecule.advanced/index.ts +0 -1
  178. package/src/ckb/types/script.ts +0 -70
  179. package/src/ckb/types/transaction.ts +0 -366
  180. package/src/fixedPoint.ts +0 -53
  181. package/src/primitive.ts +0 -8
  182. package/src/viewer.ts +0 -22
  183. /package/dist/ckb/{types/molecule.advanced → molecule.advanced}/generated.d.ts +0 -0
  184. /package/{src/ckb/hasher.advanced.ts → dist/hasher/advanced.js} +0 -0
  185. /package/src/ckb/{types/molecule.advanced → molecule.advanced}/blockchain.mol +0 -0
@@ -1,18 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSignHashInfo = void 0;
4
- const ckb_1 = require("../ckb");
5
- async function getSignHashInfo(tx, script) {
1
+ import { Script, Transaction } from "../ckb";
2
+ import { Hasher } from "../hasher";
3
+ export async function getSignHashInfo(txLike, scriptLike) {
4
+ const tx = Transaction.from(txLike);
5
+ const script = Script.from(scriptLike);
6
6
  let position = -1;
7
- const hasher = new ckb_1.Hasher();
8
- hasher.update(ckb_1.Transaction.hashRaw(tx));
7
+ const hasher = new Hasher();
8
+ hasher.update(tx.hash());
9
9
  tx.witnesses.forEach((witness, i) => {
10
10
  const input = tx.inputs[i];
11
11
  if (input) {
12
12
  if (!input.cellOutput?.lock) {
13
13
  throw Error("Incomplete inputs info");
14
14
  }
15
- if (!ckb_1.Script.eq(input.cellOutput?.lock, script)) {
15
+ if (!script.eq(input.cellOutput?.lock)) {
16
16
  return;
17
17
  }
18
18
  if (position === -1) {
@@ -22,7 +22,7 @@ async function getSignHashInfo(tx, script) {
22
22
  if (position === -1) {
23
23
  return undefined;
24
24
  }
25
- ckb_1.Transaction.hashWitnessToHasher(witness, hasher);
25
+ Transaction.hashWitnessToHasher(witness, hasher);
26
26
  });
27
27
  if (position === -1) {
28
28
  return undefined;
@@ -32,4 +32,3 @@ async function getSignHashInfo(tx, script) {
32
32
  position,
33
33
  };
34
34
  }
35
- exports.getSignHashInfo = getSignHashInfo;
@@ -1,3 +1,5 @@
1
1
  export * from "./helpers";
2
2
  export * from "./signer";
3
+ export * from "./signerReadonly";
4
+ export * from "./signerReadonlyCkbScript";
3
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signer/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signer/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
@@ -1,18 +1,4 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./helpers"), exports);
18
- __exportStar(require("./signer"), exports);
1
+ export * from "./helpers";
2
+ export * from "./signer";
3
+ export * from "./signerReadonly";
4
+ export * from "./signerReadonlyCkbScript";
@@ -1,8 +1,17 @@
1
- import { Viewer } from "../viewer";
2
- import { BytesLike, Hex } from "../primitive";
1
+ import { Address } from "../address";
2
+ import { BytesLike } from "../bytes";
3
3
  import { Transaction } from "../ckb";
4
- export declare abstract class Signer extends Viewer {
4
+ import { Client } from "../client";
5
+ import { Hex } from "../hex";
6
+ export declare abstract class Signer {
7
+ readonly client: Client;
8
+ constructor(client: Client);
5
9
  abstract connect(): Promise<void>;
10
+ abstract getInternalAddress(): Promise<string>;
11
+ abstract getAddressObjs(): Promise<Address[]>;
12
+ getRecommendedAddressObj(_preference?: unknown): Promise<Address>;
13
+ getRecommendedAddress(preference?: unknown): Promise<string>;
14
+ getAddresses(): Promise<string[]>;
6
15
  abstract signMessage(message: string | BytesLike): Promise<Hex>;
7
16
  sendTransaction(tx: Transaction): Promise<Hex>;
8
17
  signTransaction(tx: Transaction): Promise<Transaction>;
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAErC,8BAAsB,MAAO,SAAQ,MAAM;IACzC,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC,QAAQ,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC;IAEzD,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAI9C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D,QAAQ,CAAC,mBAAmB,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CACpE"}
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/signer/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjE,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAG5D,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAMvC,QAAQ,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC;IAEzD,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAI9C,eAAe,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D,QAAQ,CAAC,mBAAmB,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CACpE"}
@@ -1,8 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Signer = void 0;
4
- const viewer_1 = require("../viewer");
5
- class Signer extends viewer_1.Viewer {
1
+ export class Signer {
2
+ constructor(client) {
3
+ this.client = client;
4
+ }
5
+ async getRecommendedAddressObj(_preference) {
6
+ return (await this.getAddressObjs())[0];
7
+ }
8
+ async getRecommendedAddress(preference) {
9
+ return (await this.getRecommendedAddressObj(preference)).toString();
10
+ }
11
+ async getAddresses() {
12
+ return this.getAddressObjs().then((addresses) => addresses.map((address) => address.toString()));
13
+ }
6
14
  async sendTransaction(tx) {
7
15
  return this.client.sendTransaction(await this.signTransaction(tx));
8
16
  }
@@ -10,4 +18,3 @@ class Signer extends viewer_1.Viewer {
10
18
  return this.signOnlyTransaction(tx);
11
19
  }
12
20
  }
13
- exports.Signer = Signer;
@@ -0,0 +1,8 @@
1
+ import { Signer } from ".";
2
+ import { Transaction } from "../ckb";
3
+ import { Hex } from "../hex";
4
+ export declare abstract class SignerReadonly extends Signer {
5
+ signMessage(): Promise<Hex>;
6
+ signOnlyTransaction(): Promise<Transaction>;
7
+ }
8
+ //# sourceMappingURL=signerReadonly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signerReadonly.d.ts","sourceRoot":"","sources":["../../src/signer/signerReadonly.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B,8BAAsB,cAAe,SAAQ,MAAM;IACjD,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC;IAI3B,mBAAmB,IAAI,OAAO,CAAC,WAAW,CAAC;CAG5C"}
@@ -0,0 +1,9 @@
1
+ import { Signer } from ".";
2
+ export class SignerReadonly extends Signer {
3
+ signMessage() {
4
+ throw new Error("Method not implemented.");
5
+ }
6
+ signOnlyTransaction() {
7
+ throw new Error("Method not implemented.");
8
+ }
9
+ }
@@ -1,11 +1,12 @@
1
- import { Client } from "../client";
2
- import { Script } from "./types";
3
- import { Viewer } from "../viewer";
4
1
  import { Address } from "../address";
5
- export declare class ViewerCkbScript extends Viewer {
2
+ import { Script } from "../ckb";
3
+ import { Client } from "../client";
4
+ import { SignerReadonly } from "./signerReadonly";
5
+ export declare class SignerReadonlyCkbScript extends SignerReadonly {
6
6
  private readonly script;
7
7
  constructor(script: Script, client: Client);
8
+ connect(): Promise<void>;
8
9
  getInternalAddress(): Promise<string>;
9
10
  getAddressObjs(): Promise<Address[]>;
10
11
  }
11
- //# sourceMappingURL=viewerCkbScript.d.ts.map
12
+ //# sourceMappingURL=signerReadonlyCkbScript.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signerReadonlyCkbScript.d.ts","sourceRoot":"","sources":["../../src/signer/signerReadonlyCkbScript.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,qBAAa,uBAAwB,SAAQ,cAAc;IAEvD,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM,EAC/B,MAAM,EAAE,MAAM;IAKV,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAG3C"}
@@ -0,0 +1,17 @@
1
+ import { Address } from "../address";
2
+ import { SignerReadonly } from "./signerReadonly";
3
+ export class SignerReadonlyCkbScript extends SignerReadonly {
4
+ constructor(script, client) {
5
+ super(client);
6
+ this.script = script;
7
+ }
8
+ async connect() {
9
+ return;
10
+ }
11
+ async getInternalAddress() {
12
+ return this.getRecommendedAddress();
13
+ }
14
+ async getAddressObjs() {
15
+ return [await Address.fromScript(this.script, this.client)];
16
+ }
17
+ }
@@ -0,0 +1,7 @@
1
+ export declare function apply<T, R>(transformer: (val: T) => R, value: undefined): undefined;
2
+ export declare function apply<T, R>(transformer: (val: T) => R, value: null): null;
3
+ export declare function apply<T, R>(transformer: (val: T) => R, value: T): R;
4
+ export declare function apply<T, R>(transformer: (val: T) => R, value: T | undefined): R | undefined;
5
+ export declare function apply<T, R>(transformer: (val: T) => R, value: T | null): R | null;
6
+ export declare function apply<T, R>(transformer: (val: T) => R, value: undefined | null): undefined | null;
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GACf,SAAS,CAAC;AACb,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,CAAC;AAC3E,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;AACrE,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,SAAS,GACnB,CAAC,GAAG,SAAS,CAAC;AACjB,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,IAAI,GACd,CAAC,GAAG,IAAI,CAAC;AACZ,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GAAG,IAAI,GACtB,SAAS,GAAG,IAAI,CAAC"}
@@ -0,0 +1,6 @@
1
+ export function apply(transformer, value) {
2
+ if (value == null) {
3
+ return value;
4
+ }
5
+ return transformer(value);
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.2-alpha.2",
3
+ "version": "0.0.2",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -14,7 +14,8 @@
14
14
  },
15
15
  "scripts": {
16
16
  "build": "rimraf ./dist && tsc",
17
- "lint": "eslint ."
17
+ "lint": "eslint",
18
+ "format": "prettier --write . && eslint --fix"
18
19
  },
19
20
  "devDependencies": {
20
21
  "@ckb-lumos/helpers": "^0.22.2",
@@ -24,6 +25,7 @@
24
25
  "eslint-config-prettier": "^9.1.0",
25
26
  "eslint-plugin-prettier": "^5.1.3",
26
27
  "prettier": "^3.2.5",
28
+ "prettier-plugin-organize-imports": "^3.2.4",
27
29
  "rimraf": "^5.0.5",
28
30
  "ts-essentials": "^9.4.2",
29
31
  "typescript": "^5.4.5",
@@ -39,5 +41,5 @@
39
41
  "buffer": "^6.0.3",
40
42
  "cross-fetch": "^4.0.0"
41
43
  },
42
- "gitHead": "dae8f0bcf3554125cebee4b9497c0045d7195a63"
44
+ "gitHead": "34b7f8d8443258188d47aad71246e787b544300b"
43
45
  }
@@ -1,27 +1,60 @@
1
+ import { bech32, bech32m } from "bech32";
2
+ import { hashTypeFromBytes } from "../ckb";
1
3
  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";
4
+ import { hexFrom } from "../hex";
5
+ import { type AddressLike } from "./index";
6
6
 
7
- export async function fromAddressPayload(
7
+ export function addressPayloadFromString(address: string): {
8
+ prefix: string;
9
+ format: AddressFormat;
10
+ payload: number[];
11
+ } {
12
+ // Try parse full format address
13
+ {
14
+ const { words, prefix } = bech32m.decode(address, ADDRESS_BECH32_LIMIT);
15
+ const [formatType, ...payload] = bech32m.fromWords(words);
16
+
17
+ if (formatType === (AddressFormat.Full as number)) {
18
+ return { prefix, format: AddressFormat.Full, payload };
19
+ }
20
+ }
21
+
22
+ // Try parse legacy 2019 format address
23
+ {
24
+ const { prefix, words } = bech32.decode(address, ADDRESS_BECH32_LIMIT);
25
+ const [formatType, ...payload] = bech32.fromWords(words);
26
+ if (
27
+ [
28
+ AddressFormat.FullData,
29
+ AddressFormat.FullType,
30
+ AddressFormat.Short,
31
+ ].includes(formatType)
32
+ ) {
33
+ return { prefix, format: formatType, payload };
34
+ }
35
+ }
36
+
37
+ throw Error(`Unknown address format ${address}`);
38
+ }
39
+
40
+ export async function addressFromPayload(
8
41
  prefix: string,
9
42
  format: AddressFormat,
10
43
  payload: number[],
11
44
  client: Client,
12
- ): Promise<AddressType> {
45
+ ): Promise<AddressLike> {
13
46
  if (format === AddressFormat.Full) {
14
47
  if (payload.length < 32 + 1) {
15
48
  throw new Error(
16
- `Invalid full address without enough payload ${toHex(payload)}`,
49
+ `Invalid full address without enough payload ${hexFrom(payload)}`,
17
50
  );
18
51
  }
19
52
 
20
53
  return {
21
54
  script: {
22
- codeHash: toHex(payload.slice(0, 32)),
23
- hashType: decodeHashType(payload.slice(32, 33)),
24
- args: toHex(payload.slice(33)),
55
+ codeHash: hexFrom(payload.slice(0, 32)),
56
+ hashType: hashTypeFromBytes(payload.slice(32, 33)),
57
+ args: hexFrom(payload.slice(33)),
25
58
  },
26
59
  prefix,
27
60
  };
@@ -30,15 +63,15 @@ export async function fromAddressPayload(
30
63
  if (format === AddressFormat.FullData) {
31
64
  if (payload.length < 32) {
32
65
  throw new Error(
33
- `Invalid full data address without enough payload ${toHex(payload)}`,
66
+ `Invalid full data address without enough payload ${hexFrom(payload)}`,
34
67
  );
35
68
  }
36
69
 
37
70
  return {
38
71
  script: {
39
- codeHash: toHex(payload.slice(0, 32)),
72
+ codeHash: hexFrom(payload.slice(0, 32)),
40
73
  hashType: "data",
41
- args: toHex(payload.slice(32)),
74
+ args: hexFrom(payload.slice(32)),
42
75
  },
43
76
  prefix,
44
77
  };
@@ -47,15 +80,15 @@ export async function fromAddressPayload(
47
80
  if (format === AddressFormat.FullType) {
48
81
  if (payload.length < 32) {
49
82
  throw new Error(
50
- `Invalid full type address without enough payload ${toHex(payload)}`,
83
+ `Invalid full type address without enough payload ${hexFrom(payload)}`,
51
84
  );
52
85
  }
53
86
 
54
87
  return {
55
88
  script: {
56
- codeHash: toHex(payload.slice(0, 32)),
89
+ codeHash: hexFrom(payload.slice(0, 32)),
57
90
  hashType: "type",
58
- args: toHex(payload.slice(32)),
91
+ args: hexFrom(payload.slice(32)),
59
92
  },
60
93
  prefix,
61
94
  };
@@ -64,7 +97,7 @@ export async function fromAddressPayload(
64
97
  // format === AddressFormat.Short
65
98
  if (payload.length !== 21) {
66
99
  throw new Error(
67
- `Invalid short address without enough payload ${toHex(payload)}`,
100
+ `Invalid short address without enough payload ${hexFrom(payload)}`,
68
101
  );
69
102
  }
70
103
  const script = [
@@ -74,15 +107,39 @@ export async function fromAddressPayload(
74
107
  ][payload[0]];
75
108
  if (script === undefined) {
76
109
  throw new Error(
77
- `Invalid short address with unknown script ${toHex(payload)}`,
110
+ `Invalid short address with unknown script ${hexFrom(payload)}`,
78
111
  );
79
112
  }
80
113
 
81
114
  return {
82
115
  script: {
83
116
  ...(await client.getKnownScript(script)),
84
- args: toHex(payload.slice(1)),
117
+ args: hexFrom(payload.slice(1)),
85
118
  },
86
119
  prefix,
87
120
  };
88
121
  }
122
+
123
+ export enum AddressFormat {
124
+ /**
125
+ * full version identifies the hashType
126
+ */
127
+ Full = 0x00,
128
+ /**
129
+ * @deprecated
130
+ * short version for locks with Known codeHash, deprecated
131
+ */
132
+ Short = 0x01,
133
+ /**
134
+ * @deprecated
135
+ * full version with hashType = "Data", deprecated
136
+ */
137
+ FullData = 0x02,
138
+ /**
139
+ * @deprecated
140
+ * full version with hashType = "Type", deprecated
141
+ */
142
+ FullType = 0x04,
143
+ }
144
+
145
+ export const ADDRESS_BECH32_LIMIT = 1023;
@@ -1,59 +1 @@
1
- import { bech32, bech32m } from "bech32";
2
- export * as Address from "./address.advanced";
3
-
4
- export function parseAddressToPayload(address: string): {
5
- prefix: string;
6
- format: AddressFormat;
7
- payload: number[];
8
- } {
9
- // Try parse full format address
10
- {
11
- const { words, prefix } = bech32m.decode(address, BECH32_LIMIT);
12
- const [formatType, ...payload] = bech32m.fromWords(words);
13
-
14
- if (formatType === (AddressFormat.Full as number)) {
15
- return { prefix, format: AddressFormat.Full, payload };
16
- }
17
- }
18
-
19
- // Try parse legacy 2019 format address
20
- {
21
- const { prefix, words } = bech32.decode(address, BECH32_LIMIT);
22
- const [formatType, ...payload] = bech32.fromWords(words);
23
- if (
24
- [
25
- AddressFormat.FullData,
26
- AddressFormat.FullType,
27
- AddressFormat.Short,
28
- ].includes(formatType)
29
- ) {
30
- return { prefix, format: formatType, payload };
31
- }
32
- }
33
-
34
- throw Error(`Unknown address format ${address}`);
35
- }
36
-
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,
57
- }
58
-
59
- export const BECH32_LIMIT = 1023;
1
+ export * from "./address.advanced";
@@ -1,27 +1,38 @@
1
- import { DeepReadonly } from "ts-essentials";
1
+ import { bech32m } from "bech32";
2
+ import { bytesConcat, bytesFrom } from "../bytes";
3
+ import { Script, ScriptLike, hashTypeToBytes } from "../ckb";
2
4
  import { Client, KnownScript } from "../client";
3
- import { Script, encodeHashType } from "../ckb";
5
+ import { HexLike } from "../hex";
4
6
  import {
7
+ ADDRESS_BECH32_LIMIT,
5
8
  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";
9
+ addressFromPayload,
10
+ addressPayloadFromString,
11
+ } from "./address.advanced";
13
12
 
13
+ export type AddressLike = {
14
+ script: ScriptLike;
15
+ prefix: string;
16
+ };
14
17
  export class Address {
15
18
  constructor(
16
19
  public script: Script,
17
20
  public prefix: string,
18
21
  ) {}
19
22
 
23
+ static from(address: AddressLike): Address {
24
+ if (address instanceof Address) {
25
+ return address;
26
+ }
27
+
28
+ return new Address(Script.from(address.script), address.prefix);
29
+ }
30
+
20
31
  static async fromString(
21
32
  address: string,
22
33
  clients: Client | Record<string, Client>,
23
34
  ): Promise<Address> {
24
- const { prefix, format, payload } = parseAddressToPayload(address);
35
+ const { prefix, format, payload } = addressPayloadFromString(address);
25
36
 
26
37
  const client = (clients as Record<string, Client>)[prefix] ?? clients;
27
38
  if (!client) {
@@ -34,37 +45,44 @@ export class Address {
34
45
  );
35
46
  }
36
47
 
37
- return AdvancedAddress.fromAddressPayload(prefix, format, payload, client);
48
+ return Address.from(
49
+ await addressFromPayload(prefix, format, payload, client),
50
+ );
38
51
  }
39
52
 
40
53
  static async fromScript(
41
- script: DeepReadonly<Script>,
54
+ script: ScriptLike,
42
55
  client: Client,
43
56
  ): Promise<Address> {
44
- return {
45
- script: { ...script },
46
- prefix: await client.getAddressPrefix(),
47
- };
57
+ return new Address(Script.from(script), await client.getAddressPrefix());
48
58
  }
49
59
 
50
- static async fromKnownScript(script: KnownScript, args: Hex, client: Client) {
51
- return {
52
- script: {
60
+ static async fromKnownScript(
61
+ script: KnownScript,
62
+ args: HexLike,
63
+ client: Client,
64
+ ) {
65
+ return new Address(
66
+ Script.from({
53
67
  ...(await client.getKnownScript(script)),
54
68
  args,
55
- },
56
- prefix: await client.getAddressPrefix(),
57
- };
69
+ }),
70
+ await client.getAddressPrefix(),
71
+ );
58
72
  }
59
73
 
60
- static toString(address: DeepReadonly<Address>): string {
61
- const data = concatBytes(
74
+ toString(): string {
75
+ const data = bytesConcat(
62
76
  [AddressFormat.Full],
63
- toBytes(address.script.codeHash),
64
- encodeHashType(address.script.hashType),
65
- toBytes(address.script.args),
77
+ bytesFrom(this.script.codeHash),
78
+ hashTypeToBytes(this.script.hashType),
79
+ bytesFrom(this.script.args),
66
80
  );
67
81
 
68
- return bech32m.encode(address.prefix, bech32m.toWords(data), BECH32_LIMIT);
82
+ return bech32m.encode(
83
+ this.prefix,
84
+ bech32m.toWords(data),
85
+ ADDRESS_BECH32_LIMIT,
86
+ );
69
87
  }
70
88
  }
@@ -1,3 +1,4 @@
1
1
  export * from "./address/advanced";
2
2
  export * from "./ckb/advanced";
3
3
  export * from "./client/advanced";
4
+ export * from "./hasher/advanced";
package/src/barrel.ts CHANGED
@@ -3,6 +3,8 @@ export * from "./bytes";
3
3
  export * from "./ckb";
4
4
  export * from "./client";
5
5
  export * from "./fixedPoint";
6
- export * from "./primitive";
6
+ export * from "./hasher";
7
+ export * from "./hex";
8
+ export * from "./num";
7
9
  export * from "./signer";
8
- export * from "./viewer";
10
+ export * from "./utils";
@@ -0,0 +1,46 @@
1
+ import { Buffer } from "buffer/";
2
+
3
+ export type Bytes = Uint8Array;
4
+ export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
5
+
6
+ export function bytesConcat(...args: BytesLike[]): Bytes {
7
+ return new Uint8Array(
8
+ args.reduce((acc: number[], v) => {
9
+ acc.push(...bytesFrom(v));
10
+ return acc;
11
+ }, []),
12
+ );
13
+ }
14
+
15
+ export function bytesFromUtf8(val: string): Bytes {
16
+ return Buffer.from(val, "utf-8");
17
+ }
18
+
19
+ export function bytesToUtf8(val: BytesLike): string {
20
+ return Buffer.from(bytesFrom(val)).toString("utf-8");
21
+ }
22
+
23
+ export function bytesFrom(bytes: BytesLike): Bytes {
24
+ if (bytes instanceof Uint8Array) {
25
+ return bytes;
26
+ }
27
+
28
+ if (bytes instanceof ArrayBuffer) {
29
+ return new Uint8Array(bytes);
30
+ }
31
+
32
+ if (Array.isArray(bytes)) {
33
+ if (bytes.some((v) => v < 0 || 0xff < v)) {
34
+ throw new Error(`Invalid bytes ${JSON.stringify(bytes)}`);
35
+ }
36
+ return new Uint8Array(bytes);
37
+ }
38
+
39
+ const str = bytes.startsWith("0x") ? bytes.slice(2) : bytes;
40
+ const paddedStr = str.length % 2 === 0 ? str : `0${str}`;
41
+ const data = Buffer.from(paddedStr, "hex");
42
+ if (data.length * 2 !== paddedStr.length) {
43
+ throw new Error(`Invalid bytes ${bytes}`);
44
+ }
45
+ return data;
46
+ }
@@ -1,2 +1,3 @@
1
- export * from "./hasher.advanced";
2
- export * from "./types/advanced";
1
+ export * as moleculeCodecCkb from "./molecule.advanced";
2
+ export * from "./script.advanced";
3
+ export * from "./transaction.advanced";
package/src/ckb/index.ts CHANGED
@@ -1,3 +1,2 @@
1
- export * from "./hasher";
2
- export * from "./types";
3
- export * from "./viewerCkbScript";
1
+ export * from "./script";
2
+ export * from "./transaction";