@ckb-ccc/core 0.0.2-alpha.0

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 (117) hide show
  1. package/dist/address/address.d.ts +8 -0
  2. package/dist/address/address.d.ts.map +1 -0
  3. package/dist/address/address.js +6 -0
  4. package/dist/address/addressKnownScript.d.ts +13 -0
  5. package/dist/address/addressKnownScript.d.ts.map +1 -0
  6. package/dist/address/addressKnownScript.js +27 -0
  7. package/dist/address/addressPlain.d.ts +12 -0
  8. package/dist/address/addressPlain.d.ts.map +1 -0
  9. package/dist/address/addressPlain.js +23 -0
  10. package/dist/address/advanced.d.ts +31 -0
  11. package/dist/address/advanced.d.ts.map +1 -0
  12. package/dist/address/advanced.js +63 -0
  13. package/dist/address/converter.d.ts +8 -0
  14. package/dist/address/converter.d.ts.map +1 -0
  15. package/dist/address/converter.js +65 -0
  16. package/dist/address/index.d.ts +6 -0
  17. package/dist/address/index.d.ts.map +1 -0
  18. package/dist/address/index.js +34 -0
  19. package/dist/barrel.d.ts +8 -0
  20. package/dist/barrel.d.ts.map +1 -0
  21. package/dist/barrel.js +23 -0
  22. package/dist/bundle.d.ts +9 -0
  23. package/dist/bundle.d.ts.map +1 -0
  24. package/dist/bundle.js +25 -0
  25. package/dist/ckb/index.d.ts +2 -0
  26. package/dist/ckb/index.d.ts.map +1 -0
  27. package/dist/ckb/index.js +17 -0
  28. package/dist/ckb/viewerCkbAddress.d.ts +14 -0
  29. package/dist/ckb/viewerCkbAddress.d.ts.map +1 -0
  30. package/dist/ckb/viewerCkbAddress.js +28 -0
  31. package/dist/client/client.d.ts +13 -0
  32. package/dist/client/client.d.ts.map +1 -0
  33. package/dist/client/client.js +14 -0
  34. package/dist/client/clientPublicMainnet.advanced.d.ts +4 -0
  35. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -0
  36. package/dist/client/clientPublicMainnet.advanced.js +26 -0
  37. package/dist/client/clientPublicMainnet.d.ts +7 -0
  38. package/dist/client/clientPublicMainnet.d.ts.map +1 -0
  39. package/dist/client/clientPublicMainnet.js +14 -0
  40. package/dist/client/clientPublicTestnet.advanced.d.ts +4 -0
  41. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -0
  42. package/dist/client/clientPublicTestnet.advanced.js +26 -0
  43. package/dist/client/clientPublicTestnet.d.ts +7 -0
  44. package/dist/client/clientPublicTestnet.d.ts.map +1 -0
  45. package/dist/client/clientPublicTestnet.js +14 -0
  46. package/dist/client/index.d.ts +8 -0
  47. package/dist/client/index.d.ts.map +1 -0
  48. package/dist/client/index.js +38 -0
  49. package/dist/codec/bytes.d.ts +4 -0
  50. package/dist/codec/bytes.d.ts.map +1 -0
  51. package/dist/codec/bytes.js +29 -0
  52. package/dist/codec/hashType.d.ts +4 -0
  53. package/dist/codec/hashType.d.ts.map +1 -0
  54. package/dist/codec/hashType.js +13 -0
  55. package/dist/codec/index.d.ts +4 -0
  56. package/dist/codec/index.d.ts.map +1 -0
  57. package/dist/codec/index.js +19 -0
  58. package/dist/codec/script.d.ts +4 -0
  59. package/dist/codec/script.d.ts.map +1 -0
  60. package/dist/codec/script.js +22 -0
  61. package/dist/index.d.ts +3 -0
  62. package/dist/index.d.ts.map +1 -0
  63. package/dist/index.js +31 -0
  64. package/dist/signer.d.ts +9 -0
  65. package/dist/signer.d.ts.map +1 -0
  66. package/dist/signer.js +12 -0
  67. package/dist/tsconfig.tsbuildinfo +1 -0
  68. package/dist/types/cell.d.ts +5 -0
  69. package/dist/types/cell.d.ts.map +1 -0
  70. package/dist/types/cell.js +8 -0
  71. package/dist/types/ckb.advanced.d.ts +4 -0
  72. package/dist/types/ckb.advanced.d.ts.map +1 -0
  73. package/dist/types/ckb.advanced.js +15 -0
  74. package/dist/types/ckb.d.ts +16 -0
  75. package/dist/types/ckb.d.ts.map +1 -0
  76. package/dist/types/ckb.js +36 -0
  77. package/dist/types/index.d.ts +9 -0
  78. package/dist/types/index.d.ts.map +1 -0
  79. package/dist/types/index.js +37 -0
  80. package/dist/types/lumos.d.ts +90 -0
  81. package/dist/types/lumos.d.ts.map +1 -0
  82. package/dist/types/lumos.js +2 -0
  83. package/dist/types/primitive.d.ts +3 -0
  84. package/dist/types/primitive.d.ts.map +1 -0
  85. package/dist/types/primitive.js +2 -0
  86. package/dist/viewer.d.ts +9 -0
  87. package/dist/viewer.d.ts.map +1 -0
  88. package/dist/viewer.js +6 -0
  89. package/package.json +35 -0
  90. package/src/address/address.ts +9 -0
  91. package/src/address/addressKnownScript.ts +31 -0
  92. package/src/address/addressPlain.ts +27 -0
  93. package/src/address/advanced.ts +74 -0
  94. package/src/address/converter.ts +93 -0
  95. package/src/address/index.ts +5 -0
  96. package/src/barrel.ts +7 -0
  97. package/src/ckb/index.ts +1 -0
  98. package/src/ckb/viewerCkbAddress.ts +34 -0
  99. package/src/client/client.ts +14 -0
  100. package/src/client/clientPublicMainnet.advanced.ts +30 -0
  101. package/src/client/clientPublicMainnet.ts +13 -0
  102. package/src/client/clientPublicTestnet.advanced.ts +30 -0
  103. package/src/client/clientPublicTestnet.ts +13 -0
  104. package/src/client/index.ts +10 -0
  105. package/src/codec/bytes.ts +30 -0
  106. package/src/codec/hashType.ts +10 -0
  107. package/src/codec/index.ts +3 -0
  108. package/src/codec/script.ts +21 -0
  109. package/src/index.ts +2 -0
  110. package/src/signer.ts +20 -0
  111. package/src/types/cell.ts +5 -0
  112. package/src/types/ckb.advanced.ts +14 -0
  113. package/src/types/ckb.ts +47 -0
  114. package/src/types/index.ts +9 -0
  115. package/src/types/lumos.ts +101 -0
  116. package/src/types/primitive.ts +2 -0
  117. package/src/viewer.ts +14 -0
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@ckb-ccc/core",
3
+ "version": "0.0.2-alpha.0",
4
+ "description": "Common Chains Connector Core",
5
+ "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
+ "license": "MIT",
7
+ "private": false,
8
+ "main": "dist/index.js",
9
+ "exports": {
10
+ ".": "./dist/index.js",
11
+ "./barrel": "./dist/barrel.js"
12
+ },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "lint": "eslint ."
16
+ },
17
+ "devDependencies": {
18
+ "@eslint/js": "^9.1.1",
19
+ "eslint": "^9.1.0",
20
+ "eslint-config-prettier": "^9.1.0",
21
+ "eslint-plugin-prettier": "^5.1.3",
22
+ "prettier": "^3.2.5",
23
+ "typescript": "^5.4.5",
24
+ "typescript-eslint": "^7.7.0"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "dependencies": {
30
+ "bech32": "^2.0.0",
31
+ "buffer": "^6.0.3",
32
+ "immutable": "5.0.0-beta.5"
33
+ },
34
+ "gitHead": "c0cf1de7309204ba3121121bae68e2ef002f001f"
35
+ }
@@ -0,0 +1,9 @@
1
+ import { Client } from "../client";
2
+ import { Script } from "../types";
3
+
4
+ export abstract class Address {
5
+ abstract getClient(): Promise<Client>;
6
+
7
+ abstract getAddress(): Promise<string>;
8
+ abstract getScript(): Promise<Script>;
9
+ }
@@ -0,0 +1,31 @@
1
+ import { Client, KnownScript } from "../client";
2
+ import { Script } from "../types";
3
+ import { Address } from "./address";
4
+ import { encodeScriptToAddress } from "./converter";
5
+
6
+ export class AddressKnownScript extends Address {
7
+ constructor(
8
+ private readonly script: KnownScript,
9
+ private readonly args: Script["args"],
10
+ private readonly client: Client,
11
+ ) {
12
+ super();
13
+ }
14
+
15
+ async getClient(): Promise<Client> {
16
+ return this.client;
17
+ }
18
+
19
+ async getAddress(): Promise<string> {
20
+ const prefix = await (await this.getClient()).getAddressPrefix();
21
+
22
+ return encodeScriptToAddress(prefix, await this.getScript());
23
+ }
24
+
25
+ async getScript(): Promise<Script> {
26
+ return {
27
+ ...(await this.client.getKnownScript(this.script)),
28
+ args: this.args,
29
+ };
30
+ }
31
+ }
@@ -0,0 +1,27 @@
1
+ import { Client } from "../client";
2
+ import { Script } from "../types";
3
+ import { Address } from "./address";
4
+ import { encodeScriptToAddress } from "./converter";
5
+
6
+ export class AddressPlain extends Address {
7
+ constructor(
8
+ private readonly script: Script,
9
+ private readonly client: Client,
10
+ ) {
11
+ super();
12
+ }
13
+
14
+ async getClient(): Promise<Client> {
15
+ return this.client;
16
+ }
17
+
18
+ async getAddress(): Promise<string> {
19
+ const prefix = await (await this.getClient()).getAddressPrefix();
20
+
21
+ return encodeScriptToAddress(prefix, this.script);
22
+ }
23
+
24
+ async getScript(): Promise<Script> {
25
+ return this.script;
26
+ }
27
+ }
@@ -0,0 +1,74 @@
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";
6
+
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): {
32
+ prefix: string;
33
+ format: AddressFormat;
34
+ body: number[];
35
+ } {
36
+ // Try parse full format address
37
+ {
38
+ const { words, prefix } = bech32m.decode(address, BECH32_LIMIT);
39
+ const [formatType, ...body] = bech32m.fromWords(words);
40
+
41
+ if (formatType === AddressFormat.Full) {
42
+ return { prefix, format: AddressFormat.Full, body };
43
+ }
44
+ }
45
+
46
+ // Try parse legacy 2019 format address
47
+ {
48
+ const { prefix, words } = bech32.decode(address, BECH32_LIMIT);
49
+ const [formatType, ...body] = bech32.fromWords(words);
50
+ if (
51
+ [
52
+ AddressFormat.FullData,
53
+ AddressFormat.FullType,
54
+ AddressFormat.Short,
55
+ ].includes(formatType)
56
+ ) {
57
+ return { prefix, format: formatType, body };
58
+ }
59
+ }
60
+
61
+ throw Error(`Unknown address format ${address}`);
62
+ }
63
+
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
+ }
74
+ }
@@ -0,0 +1,93 @@
1
+ import { Client, KnownScript } from "../client";
2
+ import { Script } from "../types";
3
+ import { toHex, decodeScript, encodeScript } from "../codec";
4
+ import { AddressPlain } from "./addressPlain";
5
+ import {
6
+ AddressFormat,
7
+ BECH32_LIMIT,
8
+ getClientFromAddressPrefix,
9
+ parseAddress,
10
+ } from "./advanced";
11
+ import { AddressKnownScript } from "./addressKnownScript";
12
+ import { Address } from "./address";
13
+ import { bech32m } from "bech32";
14
+
15
+ export function decodeAddressFromString(
16
+ address: string,
17
+ getClient?: (prefix: string) => Client | undefined,
18
+ ): Address {
19
+ const { prefix, format, body } = parseAddress(address);
20
+
21
+ const client = getClient?.(prefix) ?? getClientFromAddressPrefix(prefix);
22
+ if (!client) {
23
+ throw new Error(`Unknown address prefix ${prefix}`);
24
+ }
25
+
26
+ if (format === AddressFormat.Full) {
27
+ if (body.length < 32 + 1) {
28
+ throw new Error(`Invalid address without enough payload ${address}`);
29
+ }
30
+
31
+ return new AddressPlain(decodeScript(body), client);
32
+ }
33
+
34
+ if (format === AddressFormat.FullData) {
35
+ if (body.length < 32) {
36
+ throw new Error(`Invalid address without enough payload ${address}`);
37
+ }
38
+
39
+ return new AddressPlain(
40
+ {
41
+ codeHash: toHex(body.slice(0, 32)),
42
+ hashType: "data",
43
+ args: toHex(body.slice(32)),
44
+ },
45
+ client,
46
+ );
47
+ }
48
+
49
+ if (format === AddressFormat.FullType) {
50
+ if (body.length < 32) {
51
+ throw new Error(`Invalid address without enough payload ${address}`);
52
+ }
53
+
54
+ return new AddressPlain(
55
+ {
56
+ codeHash: toHex(body.slice(0, 32)),
57
+ hashType: "type",
58
+ args: toHex(body.slice(32)),
59
+ },
60
+ client,
61
+ );
62
+ }
63
+
64
+ // format === AddressFormat.Short
65
+ if (body.length !== 21) {
66
+ throw new Error(`Invalid address without enough payload ${address}`);
67
+ }
68
+ const script = [
69
+ KnownScript.Secp256k1Blake160,
70
+ KnownScript.Secp256k1Multisig,
71
+ KnownScript.AnyoneCanPay,
72
+ ][body[0]];
73
+ if (script === undefined) {
74
+ throw new Error(
75
+ `Invalid short format address with unknown script ${address}`,
76
+ );
77
+ }
78
+
79
+ return new AddressKnownScript(script, toHex(body.slice(1)), client);
80
+ }
81
+
82
+ export function decodeAddressFromScript(
83
+ script: Script,
84
+ client: Client,
85
+ ): AddressPlain {
86
+ return new AddressPlain(script, client);
87
+ }
88
+
89
+ export function encodeScriptToAddress(prefix: string, script: Script) {
90
+ const data = [AddressFormat.Full, ...encodeScript(script)];
91
+
92
+ return bech32m.encode(prefix, bech32m.toWords(data), BECH32_LIMIT);
93
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./address";
2
+ export * from "./addressPlain";
3
+ export * from "./addressKnownScript";
4
+ export * as AdvancedAddress from "./advanced";
5
+ export * from "./converter";
package/src/barrel.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./address";
2
+ export * from "./ckb";
3
+ export * from "./client";
4
+ export * from "./codec";
5
+ export * from "./types";
6
+ export * from "./signer";
7
+ export * from "./viewer";
@@ -0,0 +1 @@
1
+ export * from "./viewerCkbAddress";
@@ -0,0 +1,34 @@
1
+ import { encodeScriptToAddress } from "../address";
2
+ import { Client } from "../client";
3
+ import { Script } from "../types";
4
+ import { Viewer } from "../viewer";
5
+ import { TransactionSkeletonType } from "../types/lumos";
6
+
7
+ export class ViewerCkbAddress extends Viewer {
8
+ constructor(
9
+ private readonly script: Script,
10
+ private readonly client: Client,
11
+ ) {
12
+ super();
13
+ }
14
+
15
+ async getClient(): Promise<Client> {
16
+ return this.client;
17
+ }
18
+
19
+ async getRecommendedAddress(): Promise<string> {
20
+ return (await this.getAddresses())[0];
21
+ }
22
+ async getAddresses(): Promise<string[]> {
23
+ return [
24
+ encodeScriptToAddress(await this.client.getAddressPrefix(), this.script),
25
+ ];
26
+ }
27
+
28
+ // Will be deprecated in the future
29
+ async completeLumosTransaction(
30
+ tx: TransactionSkeletonType,
31
+ ): Promise<TransactionSkeletonType> {
32
+ return tx;
33
+ }
34
+ }
@@ -0,0 +1,14 @@
1
+ import { Script } from "../types";
2
+
3
+ export enum KnownScript {
4
+ Secp256k1Blake160,
5
+ Secp256k1Multisig,
6
+ AnyoneCanPay,
7
+ JoyId,
8
+ OmniLock,
9
+ }
10
+
11
+ export abstract class Client {
12
+ abstract getAddressPrefix(): Promise<string>;
13
+ abstract getKnownScript(script: KnownScript): Promise<Omit<Script, "args">>;
14
+ }
@@ -0,0 +1,30 @@
1
+ import { Script } from "../types";
2
+ import { KnownScript } from "./client";
3
+
4
+ export const MAINNET_SCRIPTS: Record<KnownScript, Omit<Script, "args">> = {
5
+ [KnownScript.Secp256k1Blake160]: {
6
+ codeHash:
7
+ "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
8
+ hashType: "type",
9
+ },
10
+ [KnownScript.Secp256k1Multisig]: {
11
+ codeHash:
12
+ "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8",
13
+ hashType: "type",
14
+ },
15
+ [KnownScript.AnyoneCanPay]: {
16
+ codeHash:
17
+ "0xd369597ff47f29fbc0d47d2e3775370d1250b85140c670e4718af712983a2354",
18
+ hashType: "type",
19
+ },
20
+ [KnownScript.JoyId]: {
21
+ codeHash:
22
+ "0xd00c84f0ec8fd441c38bc3f87a371f547190f2fcff88e642bc5bf54b9e318323",
23
+ hashType: "type",
24
+ },
25
+ [KnownScript.OmniLock]: {
26
+ codeHash:
27
+ "0x9b819793a64463aed77c615d6cb226eea5487ccfc0783043a587254cda2b6f26",
28
+ hashType: "type",
29
+ },
30
+ };
@@ -0,0 +1,13 @@
1
+ import { Script } from "../types";
2
+ import { Client, KnownScript } from "./client";
3
+ import { MAINNET_SCRIPTS } from "./clientPublicMainnet.advanced";
4
+
5
+ export class ClientPublicMainnet extends Client {
6
+ async getAddressPrefix(): Promise<string> {
7
+ return "ckb";
8
+ }
9
+
10
+ async getKnownScript(script: KnownScript): Promise<Omit<Script, "args">> {
11
+ return MAINNET_SCRIPTS[script];
12
+ }
13
+ }
@@ -0,0 +1,30 @@
1
+ import { Script } from "../types";
2
+ import { KnownScript } from "./client";
3
+
4
+ export const TESTNET_SCRIPTS: Record<KnownScript, Omit<Script, "args">> = {
5
+ [KnownScript.Secp256k1Blake160]: {
6
+ codeHash:
7
+ "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
8
+ hashType: "type",
9
+ },
10
+ [KnownScript.Secp256k1Multisig]: {
11
+ codeHash:
12
+ "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8",
13
+ hashType: "type",
14
+ },
15
+ [KnownScript.AnyoneCanPay]: {
16
+ codeHash:
17
+ "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356",
18
+ hashType: "type",
19
+ },
20
+ [KnownScript.JoyId]: {
21
+ codeHash:
22
+ "0xd23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac",
23
+ hashType: "type",
24
+ },
25
+ [KnownScript.OmniLock]: {
26
+ codeHash:
27
+ "0xf329effd1c475a2978453c8600e1eaf0bc2087ee093c3ee64cc96ec6847752cb",
28
+ hashType: "type",
29
+ },
30
+ };
@@ -0,0 +1,13 @@
1
+ import { Script } from "../types";
2
+ import { Client, KnownScript } from "./client";
3
+ import { TESTNET_SCRIPTS } from "./clientPublicTestnet.advanced";
4
+
5
+ export class ClientPublicTestnet extends Client {
6
+ async getAddressPrefix(): Promise<string> {
7
+ return "ckt";
8
+ }
9
+
10
+ async getKnownScript(script: KnownScript): Promise<Omit<Script, "args">> {
11
+ return TESTNET_SCRIPTS[script];
12
+ }
13
+ }
@@ -0,0 +1,10 @@
1
+ export * from "./client";
2
+ import * as clientPublicMainnetAdvanced from "./clientPublicMainnet.advanced";
3
+ export * from "./clientPublicMainnet";
4
+ import * as clientPublicTestnetAdvanced from "./clientPublicTestnet.advanced";
5
+ export * from "./clientPublicTestnet";
6
+
7
+ export const AdvancedClient = {
8
+ ...clientPublicMainnetAdvanced,
9
+ ...clientPublicTestnetAdvanced,
10
+ };
@@ -0,0 +1,30 @@
1
+ import { BytesLike, HexString } from "../types";
2
+ import { Buffer } from "buffer";
3
+
4
+ export function toBytes(bytes: BytesLike | string): Uint8Array {
5
+ if (bytes instanceof Uint8Array) {
6
+ return bytes;
7
+ }
8
+
9
+ if (Array.isArray(bytes)) {
10
+ if (bytes.some((v) => v < 0 || 0xff < v)) {
11
+ throw new Error(`Invalid bytes ${bytes}`);
12
+ }
13
+ return new Uint8Array(bytes);
14
+ }
15
+
16
+ if (bytes.length % 2 !== 0) {
17
+ throw new Error(`Invalid bytes with odd length ${bytes}`);
18
+ }
19
+
20
+ const strData = bytes.startsWith("0x") ? bytes.slice(2) : bytes;
21
+ const data = Buffer.from(strData, "hex");
22
+ if (data.length * 2 !== strData.length) {
23
+ throw new Error(`Invalid bytes ${bytes}`);
24
+ }
25
+ return data;
26
+ }
27
+
28
+ export function toHex(bytes: BytesLike | string): HexString {
29
+ return `0x${new Buffer(toBytes(bytes)).toString("hex")}`;
30
+ }
@@ -0,0 +1,10 @@
1
+ import { BytesLike, HashType, formatHashType, parseHashType } from "../types";
2
+ import { toBytes } from "./bytes";
3
+
4
+ export function encodeHashType(hashType: HashType): Uint8Array {
5
+ return toBytes([parseHashType(hashType)]);
6
+ }
7
+
8
+ export function decodeHashType(bytes: BytesLike): HashType {
9
+ return formatHashType(toBytes(bytes)[0]);
10
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./bytes";
2
+ export * from "./hashType";
3
+ export * from "./script";
@@ -0,0 +1,21 @@
1
+ import { BytesLike, Script } from "../types";
2
+ import { toBytes, toHex } from "./bytes";
3
+ import { decodeHashType, encodeHashType } from "./hashType";
4
+
5
+ export function encodeScript(script: Script): Uint8Array {
6
+ return toBytes([
7
+ ...toBytes(script.codeHash),
8
+ ...encodeHashType(script.hashType),
9
+ ...toBytes(script.args),
10
+ ]);
11
+ }
12
+
13
+ export function decodeScript(bytes: BytesLike): Script {
14
+ const formattedBytes = toBytes(bytes);
15
+
16
+ return {
17
+ codeHash: toHex(formattedBytes.slice(0, 32)),
18
+ hashType: decodeHashType(formattedBytes.slice(32, 33)),
19
+ args: toHex(formattedBytes.slice(33)),
20
+ };
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./barrel";
2
+ export * as ccc from "./barrel";
package/src/signer.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { TransactionSkeletonType } from "./types/lumos";
2
+ import { Viewer } from "./viewer";
3
+ import { BytesLike, HexString } from "./types";
4
+
5
+ export abstract class Signer extends Viewer {
6
+ abstract signMessage(message: string | BytesLike): Promise<HexString>;
7
+
8
+ // Will be deprecated in the future
9
+ signLumosTransaction(
10
+ tx: TransactionSkeletonType,
11
+ ): Promise<TransactionSkeletonType> {
12
+ const completedTx = this.completeLumosTransaction(tx);
13
+ return this.signOnlyLumosTransaction(tx);
14
+ }
15
+
16
+ // Will be deprecated in the future
17
+ abstract signOnlyLumosTransaction(
18
+ tx: TransactionSkeletonType,
19
+ ): Promise<TransactionSkeletonType>;
20
+ }
@@ -0,0 +1,5 @@
1
+ export class Cell {
2
+ constructor() {}
3
+
4
+ static from() {}
5
+ }
@@ -0,0 +1,14 @@
1
+ import { HashType } from "./ckb";
2
+
3
+ export const HASHTYPE_TO_NUM: Record<HashType, number> = {
4
+ type: 0x01,
5
+ data: 0x00,
6
+ data1: 0x02,
7
+ data2: 0x04,
8
+ };
9
+ export const NUM_TO_HASHTYPE: Record<number, HashType> = {
10
+ 0x01: "type",
11
+ 0x00: "data",
12
+ 0x02: "data1",
13
+ 0x04: "data2",
14
+ };
@@ -0,0 +1,47 @@
1
+ import { toHex } from "../codec";
2
+ import { HASHTYPE_TO_NUM, NUM_TO_HASHTYPE } from "./ckb.advanced";
3
+ import { BytesLike, HexString } from "./primitive";
4
+
5
+ export type HashType = "type" | "data" | "data1" | "data2";
6
+ export function parseHashType(hashType: HashType) {
7
+ const val = HASHTYPE_TO_NUM[hashType];
8
+
9
+ if (val === undefined) {
10
+ throw new Error(`Invalid hash type ${hashType}`);
11
+ }
12
+
13
+ return val;
14
+ }
15
+ export function formatHashType(hashType: number): HashType {
16
+ const val = NUM_TO_HASHTYPE[hashType];
17
+
18
+ if (val === undefined) {
19
+ throw new Error(`Invalid hash type ${hashType}`);
20
+ }
21
+
22
+ return val;
23
+ }
24
+
25
+ export class Script {
26
+ constructor(
27
+ public readonly codeHash: HexString,
28
+ public readonly hashType: HashType,
29
+ public readonly args: HexString,
30
+ ) {}
31
+
32
+ static from({
33
+ codeHash,
34
+ hashType,
35
+ args,
36
+ }: {
37
+ codeHash: BytesLike;
38
+ hashType: string;
39
+ args: BytesLike;
40
+ }): Script {
41
+ return {
42
+ codeHash: toHex(codeHash),
43
+ hashType: hashType as HashType,
44
+ args: toHex(args),
45
+ };
46
+ }
47
+ }
@@ -0,0 +1,9 @@
1
+ export * from "./cell";
2
+ import * as ckbAdvanced from "./ckb.advanced";
3
+ export * from "./ckb";
4
+ export * as lumos from "./lumos";
5
+ export * from "./primitive";
6
+
7
+ export const AdvancedType = {
8
+ ...ckbAdvanced,
9
+ }