@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/num/index.ts
CHANGED
|
@@ -1,9 +1,29 @@
|
|
|
1
1
|
import { Bytes, BytesLike, bytesConcat, bytesFrom } from "../bytes";
|
|
2
2
|
import { Hex, HexLike, hexFrom } from "../hex";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Represents a numeric value as a bigint.
|
|
6
|
+
*/
|
|
4
7
|
export type Num = bigint;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Represents a value that can be converted to a numeric value.
|
|
11
|
+
* It can be a string, number, bigint, or HexLike.
|
|
12
|
+
*/
|
|
5
13
|
export type NumLike = string | number | bigint | HexLike;
|
|
6
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Converts a NumLike value to a Num (bigint).
|
|
17
|
+
*
|
|
18
|
+
* @param val - The value to convert, which can be a string, number, bigint, or HexLike.
|
|
19
|
+
* @returns A Num (bigint) representing the value.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const num = numFrom("12345"); // Outputs 12345n
|
|
24
|
+
* const numFromHex = numFrom("0x3039"); // Outputs 12345n
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
7
27
|
export function numFrom(val: NumLike): Num {
|
|
8
28
|
if (typeof val === "bigint") {
|
|
9
29
|
return val;
|
|
@@ -16,18 +36,66 @@ export function numFrom(val: NumLike): Num {
|
|
|
16
36
|
return BigInt(hexFrom(val));
|
|
17
37
|
}
|
|
18
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Converts a NumLike value to a hexadecimal string.
|
|
41
|
+
*
|
|
42
|
+
* @param val - The value to convert, which can be a string, number, bigint, or HexLike.
|
|
43
|
+
* @returns A Hex string representing the numeric value.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const hex = numToHex(12345); // Outputs "0x3039"
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
19
50
|
export function numToHex(val: NumLike): Hex {
|
|
20
51
|
return `0x${numFrom(val).toString(16)}`;
|
|
21
52
|
}
|
|
22
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Converts a NumLike value to a byte array in little-endian order.
|
|
56
|
+
*
|
|
57
|
+
* @param val - The value to convert, which can be a string, number, bigint, or HexLike.
|
|
58
|
+
* @param bytes - The number of bytes to use for the representation. If not provided, the exact number of bytes needed is used.
|
|
59
|
+
* @returns A Uint8Array containing the byte representation of the numeric value.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const bytes = numToBytes(12345, 4); // Outputs Uint8Array [57, 48, 0, 0]
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
|
|
23
67
|
export function numToBytes(val: NumLike, bytes?: number): Bytes {
|
|
24
68
|
return numLeToBytes(val, bytes);
|
|
25
69
|
}
|
|
26
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Converts a NumLike value to a byte array in little-endian order.
|
|
73
|
+
*
|
|
74
|
+
* @param val - The value to convert, which can be a string, number, bigint, or HexLike.
|
|
75
|
+
* @param bytes - The number of bytes to use for the representation. If not provided, the exact number of bytes needed is used.
|
|
76
|
+
* @returns A Uint8Array containing the byte representation of the numeric value.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const bytes = numLeToBytes(12345, 4); // Outputs Uint8Array [57, 48, 0, 0]
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
27
83
|
export function numLeToBytes(val: NumLike, bytes?: number): Bytes {
|
|
28
84
|
return numBeToBytes(val, bytes).reverse();
|
|
29
85
|
}
|
|
30
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Converts a NumLike value to a byte array in big-endian order.
|
|
89
|
+
*
|
|
90
|
+
* @param val - The value to convert, which can be a string, number, bigint, or HexLike.
|
|
91
|
+
* @param bytes - The number of bytes to use for the representation. If not provided, the exact number of bytes needed is used.
|
|
92
|
+
* @returns A Uint8Array containing the byte representation of the numeric value.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const bytes = numBeToBytes(12345, 4); // Outputs Uint8Array [0, 0, 48, 57]
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
31
99
|
export function numBeToBytes(val: NumLike, bytes?: number): Bytes {
|
|
32
100
|
const rawBytes = bytesFrom(numFrom(val).toString(16));
|
|
33
101
|
if (bytes == null) {
|
|
@@ -40,14 +108,47 @@ export function numBeToBytes(val: NumLike, bytes?: number): Bytes {
|
|
|
40
108
|
);
|
|
41
109
|
}
|
|
42
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Converts a byte array to a Num (bigint) assuming little-endian order.
|
|
113
|
+
*
|
|
114
|
+
* @param val - The byte array to convert.
|
|
115
|
+
* @returns A Num (bigint) representing the numeric value.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const num = numFromBytes(new Uint8Array([57, 48, 0, 0])); // Outputs 12345n
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
43
122
|
export function numFromBytes(val: BytesLike): Num {
|
|
44
123
|
return numLeFromBytes(val);
|
|
45
124
|
}
|
|
46
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Converts a byte array to a Num (bigint) assuming little-endian order.
|
|
128
|
+
*
|
|
129
|
+
* @param val - The byte array to convert.
|
|
130
|
+
* @returns A Num (bigint) representing the numeric value.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const num = numLeFromBytes(new Uint8Array([57, 48, 0, 0])); // Outputs 12345n
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
47
137
|
export function numLeFromBytes(val: BytesLike): Num {
|
|
48
138
|
return numBeFromBytes(bytesFrom(val).reverse());
|
|
49
139
|
}
|
|
50
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Converts a byte array to a Num (bigint) assuming big-endian order.
|
|
143
|
+
*
|
|
144
|
+
* @param val - The byte array to convert.
|
|
145
|
+
* @returns A Num (bigint) representing the numeric value.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const num = numBeFromBytes(new Uint8Array([0, 0, 48, 57])); // Outputs 12345n
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
51
152
|
export function numBeFromBytes(val: BytesLike): Num {
|
|
52
153
|
return numFrom(bytesFrom(val));
|
|
53
154
|
}
|
|
@@ -9,15 +9,40 @@ import { numToBytes } from "../../num";
|
|
|
9
9
|
import { getSignHashInfo } from "../helpers";
|
|
10
10
|
import { Signer } from "../signer";
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* An abstract class extending the Signer class for Bitcoin-like signing operations.
|
|
14
|
+
* This class provides methods to get Bitcoin account, public key, and internal address,
|
|
15
|
+
* as well as signing transactions.
|
|
16
|
+
*/
|
|
12
17
|
export abstract class SignerBtc extends Signer {
|
|
18
|
+
/**
|
|
19
|
+
* Gets the Bitcoin account associated with the signer.
|
|
20
|
+
*
|
|
21
|
+
* @returns A promise that resolves to a string representing the Bitcoin account.
|
|
22
|
+
*/
|
|
13
23
|
abstract getBtcAccount(): Promise<string>;
|
|
14
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Gets the Bitcoin public key associated with the signer.
|
|
27
|
+
*
|
|
28
|
+
* @returns A promise that resolves to a HexLike value representing the Bitcoin public key.
|
|
29
|
+
*/
|
|
15
30
|
abstract getBtcPublicKey(): Promise<HexLike>;
|
|
16
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Gets the internal address, which is the Bitcoin account in this case.
|
|
34
|
+
*
|
|
35
|
+
* @returns A promise that resolves to a string representing the internal address.
|
|
36
|
+
*/
|
|
17
37
|
async getInternalAddress(): Promise<string> {
|
|
18
38
|
return this.getBtcAccount();
|
|
19
39
|
}
|
|
20
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Gets an array of Address objects representing the known script addresses for the signer.
|
|
43
|
+
*
|
|
44
|
+
* @returns A promise that resolves to an array of Address objects.
|
|
45
|
+
*/
|
|
21
46
|
async getAddressObjs(): Promise<Address[]> {
|
|
22
47
|
const publicKey = await this.getBtcPublicKey();
|
|
23
48
|
const hash = ripemd160(sha256(bytesFrom(publicKey)));
|
|
@@ -31,10 +56,16 @@ export abstract class SignerBtc extends Signer {
|
|
|
31
56
|
];
|
|
32
57
|
}
|
|
33
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Signs a transaction without modifying it.
|
|
61
|
+
*
|
|
62
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
63
|
+
* @returns A promise that resolves to a signed Transaction object.
|
|
64
|
+
*/
|
|
34
65
|
async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
|
|
35
66
|
const tx = Transaction.from(txLike);
|
|
36
67
|
const { script } = await this.getRecommendedAddressObj();
|
|
37
|
-
const info = await getSignHashInfo(tx, script);
|
|
68
|
+
const info = await getSignHashInfo(tx, script, this.client);
|
|
38
69
|
if (!info) {
|
|
39
70
|
return tx;
|
|
40
71
|
}
|
|
@@ -2,9 +2,20 @@ import { Client } from "../../client";
|
|
|
2
2
|
import { Hex, HexLike, hexFrom } from "../../hex";
|
|
3
3
|
import { SignerBtc } from "./signerBtc";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A class extending SignerBtc that provides read-only access to a Bitcoin public key and account.
|
|
7
|
+
* This class does not support signing operations.
|
|
8
|
+
*/
|
|
5
9
|
export class SignerBtcPublicKeyReadonly extends SignerBtc {
|
|
6
10
|
private readonly publicKey: Hex;
|
|
7
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Creates an instance of SignerBtcPublicKeyReadonly.
|
|
14
|
+
*
|
|
15
|
+
* @param client - The client instance used for communication.
|
|
16
|
+
* @param account - The Bitcoin account associated with the signer.
|
|
17
|
+
* @param publicKey - The public key associated with the signer.
|
|
18
|
+
*/
|
|
8
19
|
constructor(
|
|
9
20
|
client: Client,
|
|
10
21
|
private readonly account: string,
|
|
@@ -15,12 +26,37 @@ export class SignerBtcPublicKeyReadonly extends SignerBtc {
|
|
|
15
26
|
this.publicKey = hexFrom(publicKey);
|
|
16
27
|
}
|
|
17
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Connects to the client. This implementation does nothing as the class is read-only.
|
|
31
|
+
*
|
|
32
|
+
* @returns A promise that resolves when the connection is complete.
|
|
33
|
+
*/
|
|
18
34
|
async connect(): Promise<void> {}
|
|
19
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Gets the Bitcoin account associated with the signer.
|
|
38
|
+
*
|
|
39
|
+
* @returns A promise that resolves to a string representing the Bitcoin account.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const account = await signer.getBtcAccount(); // Outputs the Bitcoin account
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
20
46
|
async getBtcAccount(): Promise<string> {
|
|
21
47
|
return this.account;
|
|
22
48
|
}
|
|
23
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Gets the Bitcoin public key associated with the signer.
|
|
52
|
+
*
|
|
53
|
+
* @returns A promise that resolves to a Hex string representing the Bitcoin public key.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const publicKey = await signer.getBtcPublicKey(); // Outputs the Bitcoin public key
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
24
60
|
async getBtcPublicKey(): Promise<Hex> {
|
|
25
61
|
return this.publicKey;
|
|
26
62
|
}
|
|
@@ -3,21 +3,57 @@ import { Script, ScriptLike } from "../../ckb";
|
|
|
3
3
|
import { Client } from "../../client";
|
|
4
4
|
import { Signer } from "../signer";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A class extending Signer that provides read-only access to a CKB script.
|
|
8
|
+
* This class does not support signing operations.
|
|
9
|
+
*/
|
|
6
10
|
export class SignerCkbScriptReadonly extends Signer {
|
|
7
11
|
private readonly script: Script;
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of SignerCkbScriptReadonly.
|
|
15
|
+
*
|
|
16
|
+
* @param client - The client instance used for communication.
|
|
17
|
+
* @param script - The script associated with the signer.
|
|
18
|
+
*/
|
|
9
19
|
constructor(client: Client, script: ScriptLike) {
|
|
10
20
|
super(client);
|
|
11
21
|
|
|
12
22
|
this.script = Script.from(script);
|
|
13
23
|
}
|
|
14
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Connects to the client. This implementation does nothing as the class is read-only.
|
|
27
|
+
*
|
|
28
|
+
* @returns A promise that resolves when the connection is complete.
|
|
29
|
+
*/
|
|
15
30
|
async connect(): Promise<void> {}
|
|
16
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Gets the internal address for the script.
|
|
34
|
+
*
|
|
35
|
+
* @returns A promise that resolves to a string representing the internal address.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const internalAddress = await signer.getInternalAddress(); // Outputs the internal address
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
17
42
|
async getInternalAddress(): Promise<string> {
|
|
18
43
|
return this.getRecommendedAddress();
|
|
19
44
|
}
|
|
20
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Gets an array of Address objects representing the script address.
|
|
48
|
+
*
|
|
49
|
+
* @returns A promise that resolves to an array of Address objects.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const addressObjs = await signer.getAddressObjs(); // Outputs the array of Address objects
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
|
|
21
57
|
async getAddressObjs(): Promise<Address[]> {
|
|
22
58
|
return [await Address.fromScript(this.script, this.client)];
|
|
23
59
|
}
|
|
@@ -7,13 +7,32 @@ import { numToBytes } from "../../num";
|
|
|
7
7
|
import { getSignHashInfo } from "../helpers";
|
|
8
8
|
import { Signer } from "../signer";
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* An abstract class extending Signer for Ethereum Virtual Machine (EVM) based signing operations.
|
|
12
|
+
* This class provides methods to get EVM account, internal address, and signing transactions.
|
|
13
|
+
*/
|
|
10
14
|
export abstract class SignerEvm extends Signer {
|
|
15
|
+
/**
|
|
16
|
+
* Gets the EVM account associated with the signer.
|
|
17
|
+
*
|
|
18
|
+
* @returns A promise that resolves to a string representing the EVM account.
|
|
19
|
+
*/
|
|
11
20
|
abstract getEvmAccount(): Promise<string>;
|
|
12
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Gets the internal address, which is the EVM account in this case.
|
|
24
|
+
*
|
|
25
|
+
* @returns A promise that resolves to a string representing the internal address.
|
|
26
|
+
*/
|
|
13
27
|
async getInternalAddress(): Promise<string> {
|
|
14
28
|
return this.getEvmAccount();
|
|
15
29
|
}
|
|
16
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Gets an array of Address objects representing the known script addresses for the signer.
|
|
33
|
+
*
|
|
34
|
+
* @returns A promise that resolves to an array of Address objects.
|
|
35
|
+
*/
|
|
17
36
|
async getAddressObjs(): Promise<Address[]> {
|
|
18
37
|
const account = await this.getEvmAccount();
|
|
19
38
|
return [
|
|
@@ -25,11 +44,17 @@ export abstract class SignerEvm extends Signer {
|
|
|
25
44
|
];
|
|
26
45
|
}
|
|
27
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Signs a transaction without modifying it.
|
|
49
|
+
*
|
|
50
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
51
|
+
* @returns A promise that resolves to a signed Transaction object.
|
|
52
|
+
*/
|
|
28
53
|
async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
|
|
29
54
|
const tx = Transaction.from(txLike);
|
|
30
55
|
|
|
31
56
|
const { script } = await this.getRecommendedAddressObj();
|
|
32
|
-
const info = await getSignHashInfo(tx, script);
|
|
57
|
+
const info = await getSignHashInfo(tx, script, this.client);
|
|
33
58
|
if (!info) {
|
|
34
59
|
return tx;
|
|
35
60
|
}
|
|
@@ -2,17 +2,48 @@ import { Client } from "../../client";
|
|
|
2
2
|
import { Hex, HexLike, hexFrom } from "../../hex";
|
|
3
3
|
import { SignerEvm } from "./signerEvm";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A class extending SignerEvm that provides read-only access to an EVM address.
|
|
7
|
+
* This class does not support signing operations.
|
|
8
|
+
*/
|
|
5
9
|
export abstract class SignerEvmAddressReadonly extends SignerEvm {
|
|
6
10
|
private readonly address: Hex;
|
|
7
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Creates an instance of SignerEvmAddressReadonly.
|
|
14
|
+
*
|
|
15
|
+
* @param client - The client instance used for communication.
|
|
16
|
+
* @param address - The EVM address associated with the signer.
|
|
17
|
+
*/
|
|
8
18
|
constructor(client: Client, address: HexLike) {
|
|
9
19
|
super(client);
|
|
10
20
|
|
|
11
21
|
this.address = hexFrom(address);
|
|
12
22
|
}
|
|
13
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Connects to the client. This implementation does nothing as the class is read-only.
|
|
26
|
+
*
|
|
27
|
+
* @returns A promise that resolves when the connection is complete.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* await signer.connect();
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
|
|
14
35
|
async connect(): Promise<void> {}
|
|
15
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Gets the EVM account associated with the signer.
|
|
39
|
+
*
|
|
40
|
+
* @returns A promise that resolves to a string representing the EVM account.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const account = await signer.getEvmAccount(); // Outputs the EVM account
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
16
47
|
async getEvmAccount(): Promise<string> {
|
|
17
48
|
return this.address;
|
|
18
49
|
}
|
package/src/signer/helpers.ts
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import { Script, ScriptLike, Transaction, TransactionLike } from "../ckb";
|
|
2
|
+
import { Client } from "../client";
|
|
2
3
|
import { Hasher } from "../hasher";
|
|
3
4
|
import { Hex } from "../hex";
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Computes the signing hash information for a given transaction and script.
|
|
8
|
+
*
|
|
9
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
10
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
11
|
+
* @returns A promise that resolves to an object containing the signing message and the witness position,
|
|
12
|
+
* or undefined if no matching input is found.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const signHashInfo = await getSignHashInfo(transactionLike, scriptLike);
|
|
17
|
+
* if (signHashInfo) {
|
|
18
|
+
* console.log(signHashInfo.message); // Outputs the signing message
|
|
19
|
+
* console.log(signHashInfo.position); // Outputs the witness position
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
5
23
|
export async function getSignHashInfo(
|
|
6
24
|
txLike: TransactionLike,
|
|
7
25
|
scriptLike: ScriptLike,
|
|
26
|
+
client: Client,
|
|
8
27
|
): Promise<{ message: Hex; position: number } | undefined> {
|
|
9
28
|
const tx = Transaction.from(txLike);
|
|
10
29
|
const script = Script.from(scriptLike);
|
|
@@ -12,14 +31,15 @@ export async function getSignHashInfo(
|
|
|
12
31
|
const hasher = new Hasher();
|
|
13
32
|
hasher.update(tx.hash());
|
|
14
33
|
|
|
15
|
-
tx.witnesses.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
for (let i = 0; i < tx.witnesses.length; i += 1) {
|
|
35
|
+
if (tx.inputs[i]) {
|
|
36
|
+
const input = await tx.inputs[i].completeExtraInfos(client);
|
|
37
|
+
|
|
38
|
+
if (!input.cellOutput) {
|
|
39
|
+
throw Error("Unable to resolve inputs info");
|
|
20
40
|
}
|
|
21
41
|
|
|
22
|
-
if (!script.eq(input.cellOutput
|
|
42
|
+
if (!script.eq(input.cellOutput.lock)) {
|
|
23
43
|
return;
|
|
24
44
|
}
|
|
25
45
|
|
|
@@ -32,8 +52,8 @@ export async function getSignHashInfo(
|
|
|
32
52
|
return undefined;
|
|
33
53
|
}
|
|
34
54
|
|
|
35
|
-
Transaction.hashWitnessToHasher(
|
|
36
|
-
}
|
|
55
|
+
Transaction.hashWitnessToHasher(tx.witnesses[i], hasher);
|
|
56
|
+
}
|
|
37
57
|
|
|
38
58
|
if (position === -1) {
|
|
39
59
|
return undefined;
|
package/src/signer/signer.ts
CHANGED
|
@@ -4,49 +4,119 @@ import { Transaction, TransactionLike } from "../ckb";
|
|
|
4
4
|
import { Client } from "../client";
|
|
5
5
|
import { Hex } from "../hex";
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* An abstract class representing a generic signer.
|
|
9
|
+
* This class provides methods to connect, get addresses, and sign transactions.
|
|
10
|
+
*/
|
|
7
11
|
export abstract class Signer {
|
|
8
12
|
constructor(public readonly client: Client) {}
|
|
9
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Connects to the client.
|
|
16
|
+
*
|
|
17
|
+
* @returns A promise that resolves when the connection is complete.
|
|
18
|
+
*/
|
|
10
19
|
abstract connect(): Promise<void>;
|
|
11
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Gets the internal address associated with the signer.
|
|
23
|
+
*
|
|
24
|
+
* @returns A promise that resolves to a string representing the internal address.
|
|
25
|
+
*/
|
|
12
26
|
abstract getInternalAddress(): Promise<string>;
|
|
13
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Gets an array of Address objects associated with the signer.
|
|
30
|
+
*
|
|
31
|
+
* @returns A promise that resolves to an array of Address objects.
|
|
32
|
+
*/
|
|
14
33
|
abstract getAddressObjs(): Promise<Address[]>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gets the recommended Address object for the signer.
|
|
37
|
+
*
|
|
38
|
+
* @param _preference - Optional preference parameter.
|
|
39
|
+
* @returns A promise that resolves to the recommended Address object.
|
|
40
|
+
*/
|
|
15
41
|
async getRecommendedAddressObj(_preference?: unknown): Promise<Address> {
|
|
16
42
|
return (await this.getAddressObjs())[0];
|
|
17
43
|
}
|
|
18
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Gets the recommended address for the signer as a string.
|
|
47
|
+
*
|
|
48
|
+
* @param preference - Optional preference parameter.
|
|
49
|
+
* @returns A promise that resolves to the recommended address as a string.
|
|
50
|
+
*/
|
|
19
51
|
async getRecommendedAddress(preference?: unknown): Promise<string> {
|
|
20
52
|
return (await this.getRecommendedAddressObj(preference)).toString();
|
|
21
53
|
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Gets an array of addresses associated with the signer as strings.
|
|
57
|
+
*
|
|
58
|
+
* @returns A promise that resolves to an array of addresses as strings.
|
|
59
|
+
*/
|
|
22
60
|
async getAddresses(): Promise<string[]> {
|
|
23
61
|
return this.getAddressObjs().then((addresses) =>
|
|
24
62
|
addresses.map((address) => address.toString()),
|
|
25
63
|
);
|
|
26
64
|
}
|
|
27
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Signs a message. This method is not implemented and should be overridden by subclasses.
|
|
68
|
+
*
|
|
69
|
+
* @param _ - The message to sign, as a string or BytesLike object.
|
|
70
|
+
* @returns A promise that resolves to the signed message as a string.
|
|
71
|
+
* @throws Will throw an error if not implemented.
|
|
72
|
+
*/
|
|
28
73
|
signMessage(_: string | BytesLike): Promise<string> {
|
|
29
74
|
throw Error("Signer.signMessage not implemented");
|
|
30
75
|
}
|
|
31
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Sends a transaction after signing it.
|
|
79
|
+
*
|
|
80
|
+
* @param tx - The transaction to send, represented as a TransactionLike object.
|
|
81
|
+
* @returns A promise that resolves to the transaction hash as a Hex string.
|
|
82
|
+
*/
|
|
32
83
|
async sendTransaction(tx: TransactionLike): Promise<Hex> {
|
|
33
84
|
return this.client.sendTransaction(await this.signTransaction(tx));
|
|
34
85
|
}
|
|
35
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Signs a transaction.
|
|
89
|
+
*
|
|
90
|
+
* @param tx - The transaction to sign, represented as a TransactionLike object.
|
|
91
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
92
|
+
*/
|
|
36
93
|
async signTransaction(tx: TransactionLike): Promise<Transaction> {
|
|
37
94
|
return this.signOnlyTransaction(tx);
|
|
38
95
|
}
|
|
39
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
|
|
99
|
+
*
|
|
100
|
+
* @param _ - The transaction to sign, represented as a TransactionLike object.
|
|
101
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
102
|
+
* @throws Will throw an error if not implemented.
|
|
103
|
+
*/
|
|
40
104
|
signOnlyTransaction(_: TransactionLike): Promise<Transaction> {
|
|
41
105
|
throw Error("Signer.signOnlyTransaction not implemented");
|
|
42
106
|
}
|
|
43
107
|
}
|
|
44
108
|
|
|
109
|
+
/**
|
|
110
|
+
* An enumeration of signer types.
|
|
111
|
+
*/
|
|
45
112
|
export enum SignerType {
|
|
46
113
|
EVM = "EVM",
|
|
47
114
|
BTC = "BTC",
|
|
48
115
|
}
|
|
49
116
|
|
|
117
|
+
/**
|
|
118
|
+
* A class representing information about a signer, including its type and the signer instance.
|
|
119
|
+
*/
|
|
50
120
|
export class SignerInfo {
|
|
51
121
|
constructor(
|
|
52
122
|
public type: SignerType,
|
|
@@ -54,6 +124,9 @@ export class SignerInfo {
|
|
|
54
124
|
) {}
|
|
55
125
|
}
|
|
56
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Represents a wallet with a name, icon, and an array of signer information.
|
|
129
|
+
*/
|
|
57
130
|
export type Wallet = {
|
|
58
131
|
name: string;
|
|
59
132
|
icon: string;
|