@ckb-ccc/core 0.0.4-alpha.2 → 0.0.4-alpha.3
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.
- package/README.md +11 -6
- package/dist/address/address.advanced.d.ts +34 -0
- package/dist/address/address.advanced.d.ts.map +1 -1
- package/dist/address/address.advanced.js +34 -0
- package/dist/address/index.d.ts +36 -0
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +36 -0
- package/dist/bytes/advanced.d.ts +3 -0
- package/dist/bytes/advanced.d.ts.map +1 -1
- package/dist/bytes/index.d.ts +57 -0
- package/dist/bytes/index.d.ts.map +1 -1
- package/dist/bytes/index.js +57 -0
- package/dist/ckb/script.d.ts +97 -0
- package/dist/ckb/script.d.ts.map +1 -1
- package/dist/ckb/script.js +97 -0
- package/dist/ckb/transaction.d.ts +391 -0
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +402 -0
- package/dist/client/client.d.ts +10 -8
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +18 -0
- package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.d.ts +2 -2
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
- package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.d.ts +2 -2
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientTypes.d.ts +6 -0
- package/dist/client/clientTypes.d.ts.map +1 -1
- package/dist/client/jsonRpc/advanced.d.ts +2 -87
- package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
- package/dist/client/jsonRpc/advanced.js +2 -65
- package/dist/client/jsonRpc/index.d.ts +60 -6
- package/dist/client/jsonRpc/index.d.ts.map +1 -1
- package/dist/client/jsonRpc/index.js +72 -7
- package/dist/client/jsonRpc/transformers.d.ts +28 -0
- package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
- package/dist/client/jsonRpc/transformers.js +122 -0
- package/dist/client/jsonRpc/types.d.ts +41 -0
- package/dist/client/jsonRpc/types.d.ts.map +1 -0
- package/dist/client/jsonRpc/types.js +1 -0
- package/dist/fixedPoint/index.d.ts +42 -0
- package/dist/fixedPoint/index.d.ts.map +1 -1
- package/dist/fixedPoint/index.js +38 -0
- package/dist/hasher/index.d.ts +42 -0
- package/dist/hasher/index.d.ts.map +1 -1
- package/dist/hasher/index.js +42 -0
- package/dist/hex/index.d.ts +19 -0
- package/dist/hex/index.d.ts.map +1 -1
- package/dist/hex/index.js +12 -0
- package/dist/num/index.d.ts +99 -0
- package/dist/num/index.d.ts.map +1 -1
- package/dist/num/index.js +92 -0
- package/dist/signer/btc/signerBtc.d.ts +31 -0
- package/dist/signer/btc/signerBtc.d.ts.map +1 -1
- package/dist/signer/btc/signerBtc.js +22 -1
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +36 -0
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist/signer/btc/signerBtcPublicKeyReadonly.js +36 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +35 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist/signer/ckb/signerCkbScriptReadonly.js +35 -0
- package/dist/signer/evm/signerEvm.d.ts +25 -0
- package/dist/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist/signer/evm/signerEvm.js +21 -1
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts +30 -0
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist/signer/evm/signerEvmAddressReadonly.js +30 -0
- package/dist/signer/helpers.d.ts +19 -1
- package/dist/signer/helpers.d.ts.map +1 -1
- package/dist/signer/helpers.js +26 -9
- package/dist/signer/signer.d.ts +71 -0
- package/dist/signer/signer.d.ts.map +1 -1
- package/dist/signer/signer.js +53 -0
- package/package.json +2 -2
- package/src/address/address.advanced.ts +36 -0
- package/src/address/index.ts +42 -0
- package/src/bytes/advanced.ts +12 -9
- package/src/bytes/index.ts +60 -0
- package/src/ckb/script.ts +109 -0
- package/src/ckb/transaction.ts +453 -0
- package/src/client/client.ts +31 -8
- package/src/client/clientPublicMainnet.advanced.ts +5 -2
- package/src/client/clientPublicMainnet.ts +4 -2
- package/src/client/clientPublicTestnet.advanced.ts +5 -2
- package/src/client/clientPublicTestnet.ts +4 -2
- package/src/client/clientTypes.ts +13 -0
- package/src/client/jsonRpc/advanced.ts +2 -93
- package/src/client/jsonRpc/index.ts +99 -11
- package/src/client/jsonRpc/transformers.ts +159 -0
- package/src/client/jsonRpc/types.ts +48 -0
- package/src/fixedPoint/index.ts +50 -0
- package/src/hasher/index.ts +46 -0
- package/src/hex/index.ts +19 -0
- package/src/num/index.ts +101 -0
- package/src/signer/btc/signerBtc.ts +32 -1
- package/src/signer/btc/signerBtcPublicKeyReadonly.ts +36 -0
- package/src/signer/ckb/signerCkbScriptReadonly.ts +36 -0
- package/src/signer/evm/signerEvm.ts +26 -1
- package/src/signer/evm/signerEvmAddressReadonly.ts +31 -0
- package/src/signer/helpers.ts +28 -8
- package/src/signer/signer.ts +73 -0
package/src/address/index.ts
CHANGED
|
@@ -14,12 +14,30 @@ export type AddressLike = {
|
|
|
14
14
|
script: ScriptLike;
|
|
15
15
|
prefix: string;
|
|
16
16
|
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Represents a ckb address with associated script and prefix.
|
|
20
|
+
*/
|
|
21
|
+
|
|
17
22
|
export class Address {
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of Address.
|
|
25
|
+
*
|
|
26
|
+
* @param script - The script associated with the address.
|
|
27
|
+
* @param prefix - The address prefix.
|
|
28
|
+
*/
|
|
18
29
|
constructor(
|
|
19
30
|
public script: Script,
|
|
20
31
|
public prefix: string,
|
|
21
32
|
) {}
|
|
22
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Creates an Address instance from an AddressLike object.
|
|
36
|
+
*
|
|
37
|
+
* @param address - An AddressLike object or an instance of Address.
|
|
38
|
+
* @returns An Address instance.
|
|
39
|
+
*/
|
|
40
|
+
|
|
23
41
|
static from(address: AddressLike): Address {
|
|
24
42
|
if (address instanceof Address) {
|
|
25
43
|
return address;
|
|
@@ -28,6 +46,16 @@ export class Address {
|
|
|
28
46
|
return new Address(Script.from(address.script), address.prefix);
|
|
29
47
|
}
|
|
30
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Creates an Address instance from an address string.
|
|
51
|
+
*
|
|
52
|
+
* @param address - The address string to parse.
|
|
53
|
+
* @param clients - A Client instance or a record of Client instances keyed by prefix.
|
|
54
|
+
* @returns A promise that resolves to an Address instance.
|
|
55
|
+
*
|
|
56
|
+
* @throws Will throw an error if the address prefix is unknown or mismatched.
|
|
57
|
+
*/
|
|
58
|
+
|
|
31
59
|
static async fromString(
|
|
32
60
|
address: string,
|
|
33
61
|
clients: Client | Record<string, Client>,
|
|
@@ -50,6 +78,14 @@ export class Address {
|
|
|
50
78
|
);
|
|
51
79
|
}
|
|
52
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Creates an Address instance from a script and client.
|
|
83
|
+
*
|
|
84
|
+
* @param script - The script-like object.
|
|
85
|
+
* @param client - The client instance used to fetch the address prefix.
|
|
86
|
+
* @returns A promise that resolves to an Address instance.
|
|
87
|
+
*/
|
|
88
|
+
|
|
53
89
|
static async fromScript(
|
|
54
90
|
script: ScriptLike,
|
|
55
91
|
client: Client,
|
|
@@ -71,6 +107,12 @@ export class Address {
|
|
|
71
107
|
});
|
|
72
108
|
}
|
|
73
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Converts the Address instance to a string.
|
|
112
|
+
*
|
|
113
|
+
* @returns The address as a string.
|
|
114
|
+
*/
|
|
115
|
+
|
|
74
116
|
toString(): string {
|
|
75
117
|
const data = bytesConcat(
|
|
76
118
|
[AddressFormat.Full],
|
package/src/bytes/advanced.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the possible encoding formats for converting bytes.
|
|
3
|
+
*/
|
|
1
4
|
export type BytesFromEncoding =
|
|
2
|
-
| "utf8"
|
|
3
|
-
| "utf16le"
|
|
4
|
-
| "latin1"
|
|
5
|
-
| "base64"
|
|
6
|
-
| "base64url"
|
|
7
|
-
| "hex"
|
|
8
|
-
| "ascii"
|
|
9
|
-
| "binary"
|
|
10
|
-
| "ucs2";
|
|
5
|
+
| "utf8" // UTF-8 encoding
|
|
6
|
+
| "utf16le" // UTF-16 Little Endian encoding
|
|
7
|
+
| "latin1" // Latin-1 (ISO-8859-1) encoding
|
|
8
|
+
| "base64" // Base64 encoding
|
|
9
|
+
| "base64url" // Base64 URL encoding
|
|
10
|
+
| "hex" // Hexadecimal encoding
|
|
11
|
+
| "ascii" // ASCII encoding
|
|
12
|
+
| "binary" // Binary encoding
|
|
13
|
+
| "ucs2"; // UCS-2 (alias of UTF-16LE) encoding
|
package/src/bytes/index.ts
CHANGED
|
@@ -4,6 +4,24 @@ import { BytesFromEncoding } from "./advanced";
|
|
|
4
4
|
export type Bytes = Uint8Array;
|
|
5
5
|
export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Concatenates multiple byte-like arrays into a single byte array.
|
|
9
|
+
*
|
|
10
|
+
* @param args - The byte-like arrays to concatenate.
|
|
11
|
+
* @returns A Uint8Array containing the concatenated bytes.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const concatenatedBytes = bytesConcat(
|
|
16
|
+
* new Uint8Array([1, 2]),
|
|
17
|
+
* new Uint8Array([3, 4]),
|
|
18
|
+
* "hello",
|
|
19
|
+
* [5, 6, 7]
|
|
20
|
+
* );
|
|
21
|
+
* console.log(concatenatedBytes); // Outputs Uint8Array [1, 2, 3, 4, /* bytes of "hello" *\/, 5, 6, 7]
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
|
|
7
25
|
export function bytesConcat(...args: BytesLike[]): Bytes {
|
|
8
26
|
return new Uint8Array(
|
|
9
27
|
args.reduce((acc: number[], v) => {
|
|
@@ -13,10 +31,52 @@ export function bytesConcat(...args: BytesLike[]): Bytes {
|
|
|
13
31
|
);
|
|
14
32
|
}
|
|
15
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Converts a byte-like value to a string using the specified encoding.
|
|
36
|
+
*
|
|
37
|
+
* @param val - The byte-like value to convert.
|
|
38
|
+
* @param encoding - The encoding to use for the conversion, as defined by the BytesFromEncoding type.
|
|
39
|
+
* @returns A string representing the encoded bytes.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const encodedString = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "utf8");
|
|
44
|
+
* console.log(encodedString); // Outputs "hello"
|
|
45
|
+
*
|
|
46
|
+
* const base64String = bytesTo(new Uint8Array([104, 101, 108, 108, 111]), "base64");
|
|
47
|
+
* console.log(base64String); // Outputs "aGVsbG8="
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
|
|
16
51
|
export function bytesTo(val: BytesLike, encoding: BytesFromEncoding): string {
|
|
17
52
|
return Buffer.from(bytesFrom(val)).toString(encoding);
|
|
18
53
|
}
|
|
19
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Converts various types of byte-like values to a Uint8Array.
|
|
57
|
+
*
|
|
58
|
+
* @param bytes - The byte-like value to convert. It can be a string, Uint8Array, ArrayBuffer, or number array.
|
|
59
|
+
* @param encoding - Optional encoding to use if the input is a string. Defaults to hexadecimal if not specified.
|
|
60
|
+
* @returns A Uint8Array representing the input bytes.
|
|
61
|
+
*
|
|
62
|
+
* @throws Will throw an error if the input bytes are invalid or out of range.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const bytes1 = bytesFrom(new Uint8Array([1, 2, 3]));
|
|
67
|
+
* console.log(bytes1); // Outputs Uint8Array [1, 2, 3]
|
|
68
|
+
*
|
|
69
|
+
* const bytes2 = bytesFrom("68656c6c6f", "hex");
|
|
70
|
+
* console.log(bytes2); // Outputs Uint8Array [104, 101, 108, 108, 111]
|
|
71
|
+
*
|
|
72
|
+
* const bytes3 = bytesFrom("hello", "utf8");
|
|
73
|
+
* console.log(bytes3); // Outputs Uint8Array [104, 101, 108, 108, 111]
|
|
74
|
+
*
|
|
75
|
+
* const bytes4 = bytesFrom([1, 2, 255]);
|
|
76
|
+
* console.log(bytes4); // Outputs Uint8Array [1, 2, 255]
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
|
|
20
80
|
export function bytesFrom(
|
|
21
81
|
bytes: BytesLike,
|
|
22
82
|
encoding?: BytesFromEncoding,
|
package/src/ckb/script.ts
CHANGED
|
@@ -9,6 +9,22 @@ import {
|
|
|
9
9
|
|
|
10
10
|
export type HashTypeLike = string | number | bigint;
|
|
11
11
|
export type HashType = "type" | "data" | "data1" | "data2";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Converts a HashTypeLike value to a HashType.
|
|
15
|
+
*
|
|
16
|
+
* @param val - The value to convert, which can be a string, number, or bigint.
|
|
17
|
+
* @returns The corresponding HashType.
|
|
18
|
+
*
|
|
19
|
+
* @throws Will throw an error if the input value is not a valid hash type.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const hashType = hashTypeFrom(1); // Outputs "data"
|
|
24
|
+
* const hashType = hashTypeFrom("type"); // Outputs "type"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
|
|
12
28
|
export function hashTypeFrom(val: HashTypeLike): HashType {
|
|
13
29
|
const hashType = (() => {
|
|
14
30
|
if (typeof val === "number") {
|
|
@@ -29,9 +45,37 @@ export function hashTypeFrom(val: HashTypeLike): HashType {
|
|
|
29
45
|
}
|
|
30
46
|
return hashType;
|
|
31
47
|
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Converts a HashTypeLike value to its corresponding byte representation.
|
|
51
|
+
*
|
|
52
|
+
* @param hashType - The hash type value to convert.
|
|
53
|
+
* @returns A Uint8Array containing the byte representation of the hash type.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const hashTypeBytes = hashTypeToBytes("type"); // Outputs Uint8Array [0]
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
|
|
32
61
|
export function hashTypeToBytes(hashType: HashTypeLike): Bytes {
|
|
33
62
|
return bytesFrom([HASH_TYPE_TO_NUM[hashTypeFrom(hashType)]]);
|
|
34
63
|
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Converts a byte-like value to a HashType.
|
|
67
|
+
*
|
|
68
|
+
* @param bytes - The byte-like value to convert.
|
|
69
|
+
* @returns The corresponding HashType.
|
|
70
|
+
*
|
|
71
|
+
* @throws Will throw an error if the input bytes do not correspond to a valid hash type.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const hashType = hashTypeFromBytes(new Uint8Array([0])); // Outputs "type"
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
|
|
35
79
|
export function hashTypeFromBytes(bytes: BytesLike): HashType {
|
|
36
80
|
return NUM_TO_HASH_TYPE[bytesFrom(bytes)[0]];
|
|
37
81
|
}
|
|
@@ -42,12 +86,36 @@ export type ScriptLike = {
|
|
|
42
86
|
args: BytesLike;
|
|
43
87
|
};
|
|
44
88
|
export class Script {
|
|
89
|
+
/**
|
|
90
|
+
* Creates an instance of Script.
|
|
91
|
+
*
|
|
92
|
+
* @param codeHash - The code hash of the script.
|
|
93
|
+
* @param hashType - The hash type of the script.
|
|
94
|
+
* @param args - The arguments for the script.
|
|
95
|
+
*/
|
|
96
|
+
|
|
45
97
|
constructor(
|
|
46
98
|
public codeHash: Hex,
|
|
47
99
|
public hashType: HashType,
|
|
48
100
|
public args: Hex,
|
|
49
101
|
) {}
|
|
50
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Creates a Script instance from a ScriptLike object.
|
|
105
|
+
*
|
|
106
|
+
* @param script - A ScriptLike object or an instance of Script.
|
|
107
|
+
* @returns A Script instance.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const script = Script.from({
|
|
112
|
+
* codeHash: "0x1234...",
|
|
113
|
+
* hashType: "type",
|
|
114
|
+
* args: "0xabcd..."
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
|
|
51
119
|
static from(script: ScriptLike): Script {
|
|
52
120
|
if (script instanceof Script) {
|
|
53
121
|
return script;
|
|
@@ -60,6 +128,12 @@ export class Script {
|
|
|
60
128
|
);
|
|
61
129
|
}
|
|
62
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Converts the Script instance to molecule data format.
|
|
133
|
+
*
|
|
134
|
+
* @returns An object representing the script in molecule data format.
|
|
135
|
+
*/
|
|
136
|
+
|
|
63
137
|
_toMolData() {
|
|
64
138
|
return {
|
|
65
139
|
codeHash: bytesFrom(this.codeHash),
|
|
@@ -68,10 +142,33 @@ export class Script {
|
|
|
68
142
|
};
|
|
69
143
|
}
|
|
70
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Converts the Script instance to bytes.
|
|
147
|
+
*
|
|
148
|
+
* @returns A Uint8Array containing the script bytes.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const scriptBytes = script.toBytes();
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
|
|
71
156
|
toBytes(): Bytes {
|
|
72
157
|
return bytesFrom(mol.SerializeScript(this._toMolData()));
|
|
73
158
|
}
|
|
74
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Creates a Script instance from a byte-like value or molecule Script.
|
|
162
|
+
*
|
|
163
|
+
* @param bytes - The byte-like value or molecule Script to convert.
|
|
164
|
+
* @returns A Script instance.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const script = Script.fromBytes(new Uint8Array([/* script bytes *\/]));
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
|
|
75
172
|
static fromBytes(bytes: BytesLike | mol.Script): Script {
|
|
76
173
|
const view =
|
|
77
174
|
bytes instanceof mol.Script ? bytes : new mol.Script(bytesFrom(bytes));
|
|
@@ -83,6 +180,18 @@ export class Script {
|
|
|
83
180
|
);
|
|
84
181
|
}
|
|
85
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Compares the current Script instance with another ScriptLike object for equality.
|
|
185
|
+
*
|
|
186
|
+
* @param val - The ScriptLike object to compare with.
|
|
187
|
+
* @returns True if the scripts are equal, otherwise false.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const isEqual = script.eq(anotherScript);
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
|
|
86
195
|
eq(val: ScriptLike): boolean {
|
|
87
196
|
const script = Script.from(val);
|
|
88
197
|
return (
|