@ckb-ccc/core 0.0.4-alpha.1 → 0.0.4-alpha.11
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 +12 -7
- 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 +41 -7
- package/dist/address/index.d.ts +36 -0
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +44 -5
- 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 +98 -1
- package/dist/ckb/script.d.ts.map +1 -1
- package/dist/ckb/script.js +98 -1
- package/dist/ckb/transaction.d.ts +392 -1
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +403 -1
- package/dist/client/advanced.d.ts +1 -0
- package/dist/client/advanced.d.ts.map +1 -1
- package/dist/client/advanced.js +1 -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 +3 -3
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.js +2 -2
- 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 +3 -3
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.js +2 -2
- package/dist/client/clientTypes.d.ts +6 -0
- package/dist/client/clientTypes.d.ts.map +1 -1
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -0
- 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 +43 -1
- package/dist/fixedPoint/index.d.ts.map +1 -1
- package/dist/fixedPoint/index.js +39 -1
- package/dist/hasher/index.d.ts +43 -2
- 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 +40 -2
- package/dist/signer/btc/signerBtc.d.ts.map +1 -1
- package/dist/signer/btc/signerBtc.js +36 -4
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +48 -0
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist/signer/btc/signerBtcPublicKeyReadonly.js +53 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +47 -0
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist/signer/ckb/signerCkbScriptReadonly.js +52 -0
- package/dist/signer/evm/signerEvm.d.ts +34 -2
- package/dist/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist/signer/evm/signerEvm.js +35 -4
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts +43 -1
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist/signer/evm/signerEvmAddressReadonly.js +47 -0
- package/dist/signer/helpers.d.ts +35 -2
- package/dist/signer/helpers.d.ts.map +1 -1
- package/dist/signer/helpers.js +67 -11
- package/dist/signer/signer.d.ts +98 -7
- package/dist/signer/signer.d.ts.map +1 -1
- package/dist/signer/signer.js +70 -3
- package/package.json +3 -3
- package/src/address/address.advanced.ts +54 -18
- package/src/address/index.ts +51 -7
- package/src/bytes/advanced.ts +12 -9
- package/src/bytes/index.ts +60 -0
- package/src/ckb/script.ts +111 -2
- package/src/ckb/transaction.ts +455 -2
- package/src/client/advanced.ts +1 -0
- package/src/client/client.ts +31 -8
- package/src/client/clientPublicMainnet.advanced.ts +5 -2
- package/src/client/clientPublicMainnet.ts +6 -4
- package/src/client/clientPublicTestnet.advanced.ts +5 -2
- package/src/client/clientPublicTestnet.ts +6 -4
- package/src/client/clientTypes.ts +13 -0
- package/src/client/index.ts +1 -0
- package/src/client/jsonRpc/advanced.ts +2 -93
- package/src/client/jsonRpc/index.ts +99 -11
- package/src/client/jsonRpc/transformers.ts +161 -0
- package/src/client/jsonRpc/types.ts +48 -0
- package/src/fixedPoint/index.ts +52 -2
- package/src/hasher/index.ts +47 -1
- package/src/hex/index.ts +19 -0
- package/src/num/index.ts +101 -0
- package/src/signer/btc/signerBtc.ts +47 -4
- package/src/signer/btc/signerBtcPublicKeyReadonly.ts +54 -0
- package/src/signer/ckb/signerCkbScriptReadonly.ts +54 -0
- package/src/signer/evm/signerEvm.ts +42 -4
- package/src/signer/evm/signerEvmAddressReadonly.ts +50 -1
- package/src/signer/helpers.ts +87 -11
- package/src/signer/signer.ts +109 -7
package/dist/signer/signer.d.ts
CHANGED
|
@@ -1,34 +1,125 @@
|
|
|
1
1
|
import { Address } from "../address";
|
|
2
2
|
import { BytesLike } from "../bytes";
|
|
3
|
-
import { Transaction } from "../ckb";
|
|
3
|
+
import { Transaction, TransactionLike } from "../ckb";
|
|
4
4
|
import { Client } from "../client";
|
|
5
5
|
import { Hex } from "../hex";
|
|
6
|
+
/**
|
|
7
|
+
* An abstract class representing a generic signer.
|
|
8
|
+
* This class provides methods to connect, get addresses, and sign transactions.
|
|
9
|
+
*/
|
|
6
10
|
export declare abstract class Signer {
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
11
|
+
protected client_: Client;
|
|
12
|
+
constructor(client_: Client);
|
|
13
|
+
get client(): Client;
|
|
14
|
+
/**
|
|
15
|
+
* Construct a new signer with the client replaced.
|
|
16
|
+
*
|
|
17
|
+
* @returns A promise that resolves the new Signer.
|
|
18
|
+
*/
|
|
19
|
+
abstract replaceClient(client: Client): Promise<Signer>;
|
|
20
|
+
/**
|
|
21
|
+
* Connects to the signer.
|
|
22
|
+
*
|
|
23
|
+
* @returns A promise that resolves when the connection is complete.
|
|
24
|
+
*/
|
|
9
25
|
abstract connect(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Check if the signer is connected.
|
|
28
|
+
*
|
|
29
|
+
* @returns A promise that resolves the connection status.
|
|
30
|
+
*/
|
|
31
|
+
abstract isConnected(): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the internal address associated with the signer.
|
|
34
|
+
*
|
|
35
|
+
* @returns A promise that resolves to a string representing the internal address.
|
|
36
|
+
*/
|
|
10
37
|
abstract getInternalAddress(): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Gets an array of Address objects associated with the signer.
|
|
40
|
+
*
|
|
41
|
+
* @returns A promise that resolves to an array of Address objects.
|
|
42
|
+
*/
|
|
11
43
|
abstract getAddressObjs(): Promise<Address[]>;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the recommended Address object for the signer.
|
|
46
|
+
*
|
|
47
|
+
* @param _preference - Optional preference parameter.
|
|
48
|
+
* @returns A promise that resolves to the recommended Address object.
|
|
49
|
+
*/
|
|
12
50
|
getRecommendedAddressObj(_preference?: unknown): Promise<Address>;
|
|
51
|
+
/**
|
|
52
|
+
* Gets the recommended address for the signer as a string.
|
|
53
|
+
*
|
|
54
|
+
* @param preference - Optional preference parameter.
|
|
55
|
+
* @returns A promise that resolves to the recommended address as a string.
|
|
56
|
+
*/
|
|
13
57
|
getRecommendedAddress(preference?: unknown): Promise<string>;
|
|
58
|
+
/**
|
|
59
|
+
* Gets an array of addresses associated with the signer as strings.
|
|
60
|
+
*
|
|
61
|
+
* @returns A promise that resolves to an array of addresses as strings.
|
|
62
|
+
*/
|
|
14
63
|
getAddresses(): Promise<string[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Signs a message. This method is not implemented and should be overridden by subclasses.
|
|
66
|
+
*
|
|
67
|
+
* @param _ - The message to sign, as a string or BytesLike object.
|
|
68
|
+
* @returns A promise that resolves to the signed message as a string.
|
|
69
|
+
* @throws Will throw an error if not implemented.
|
|
70
|
+
*/
|
|
15
71
|
signMessage(_: string | BytesLike): Promise<string>;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Sends a transaction after signing it.
|
|
74
|
+
*
|
|
75
|
+
* @param tx - The transaction to send, represented as a TransactionLike object.
|
|
76
|
+
* @returns A promise that resolves to the transaction hash as a Hex string.
|
|
77
|
+
*/
|
|
78
|
+
sendTransaction(tx: TransactionLike): Promise<Hex>;
|
|
79
|
+
/**
|
|
80
|
+
* Signs a transaction.
|
|
81
|
+
*
|
|
82
|
+
* @param tx - The transaction to sign, represented as a TransactionLike object.
|
|
83
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
84
|
+
*/
|
|
85
|
+
signTransaction(tx: TransactionLike): Promise<Transaction>;
|
|
86
|
+
/**
|
|
87
|
+
* prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
|
|
88
|
+
*
|
|
89
|
+
* @param _ - The transaction to prepare, represented as a TransactionLike object.
|
|
90
|
+
* @returns A promise that resolves to the prepared Transaction object.
|
|
91
|
+
* @throws Will throw an error if not implemented.
|
|
92
|
+
*/
|
|
93
|
+
prepareTransaction(_: TransactionLike): Promise<Transaction>;
|
|
94
|
+
/**
|
|
95
|
+
* Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
|
|
96
|
+
*
|
|
97
|
+
* @param _ - The transaction to sign, represented as a TransactionLike object.
|
|
98
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
99
|
+
* @throws Will throw an error if not implemented.
|
|
100
|
+
*/
|
|
101
|
+
signOnlyTransaction(_: TransactionLike): Promise<Transaction>;
|
|
19
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* An enumeration of signer types.
|
|
105
|
+
*/
|
|
20
106
|
export declare enum SignerType {
|
|
21
107
|
EVM = "EVM",
|
|
22
108
|
BTC = "BTC"
|
|
23
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* A class representing information about a signer, including its type and the signer instance.
|
|
112
|
+
*/
|
|
24
113
|
export declare class SignerInfo {
|
|
25
114
|
type: SignerType;
|
|
26
115
|
signer: Signer;
|
|
27
116
|
constructor(type: SignerType, signer: Signer);
|
|
28
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Represents a wallet with a name, icon, and an array of signer information.
|
|
120
|
+
*/
|
|
29
121
|
export type Wallet = {
|
|
30
122
|
name: string;
|
|
31
123
|
icon: string;
|
|
32
|
-
signers: SignerInfo[];
|
|
33
124
|
};
|
|
34
125
|
//# sourceMappingURL=signer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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,eAAe,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7B;;;GAGG;AACH,8BAAsB,MAAM;IACd,SAAS,CAAC,OAAO,EAAE,MAAM;gBAAf,OAAO,EAAE,MAAM;IAErC,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAEvD;;;;OAIG;IACH,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAEjC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAExC;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C;;;;OAIG;IACH,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IAE7C;;;;;OAKG;IACG,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIvE;;;;;OAKG;IACG,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlE;;;;OAIG;IACG,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAMvC;;;;;;OAMG;IACH,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAInD;;;;;OAKG;IACG,eAAe,CAAC,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC;IAIxD;;;;;OAKG;IACG,eAAe,CAAC,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAKhE;;;;;;OAMG;IACH,kBAAkB,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;IAI5D;;;;;;OAMG;IACH,mBAAmB,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,CAAC;CAG9D;AAED;;GAEG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ;AAED;;GAEG;AACH,qBAAa,UAAU;IAEZ,IAAI,EAAE,UAAU;IAChB,MAAM,EAAE,MAAM;gBADd,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM;CAExB;AAED;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
|
package/dist/signer/signer.js
CHANGED
|
@@ -1,34 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An abstract class representing a generic signer.
|
|
3
|
+
* This class provides methods to connect, get addresses, and sign transactions.
|
|
4
|
+
*/
|
|
1
5
|
export class Signer {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
6
|
+
constructor(client_) {
|
|
7
|
+
this.client_ = client_;
|
|
4
8
|
}
|
|
9
|
+
get client() {
|
|
10
|
+
return this.client_;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Gets the recommended Address object for the signer.
|
|
14
|
+
*
|
|
15
|
+
* @param _preference - Optional preference parameter.
|
|
16
|
+
* @returns A promise that resolves to the recommended Address object.
|
|
17
|
+
*/
|
|
5
18
|
async getRecommendedAddressObj(_preference) {
|
|
6
19
|
return (await this.getAddressObjs())[0];
|
|
7
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Gets the recommended address for the signer as a string.
|
|
23
|
+
*
|
|
24
|
+
* @param preference - Optional preference parameter.
|
|
25
|
+
* @returns A promise that resolves to the recommended address as a string.
|
|
26
|
+
*/
|
|
8
27
|
async getRecommendedAddress(preference) {
|
|
9
28
|
return (await this.getRecommendedAddressObj(preference)).toString();
|
|
10
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets an array of addresses associated with the signer as strings.
|
|
32
|
+
*
|
|
33
|
+
* @returns A promise that resolves to an array of addresses as strings.
|
|
34
|
+
*/
|
|
11
35
|
async getAddresses() {
|
|
12
36
|
return this.getAddressObjs().then((addresses) => addresses.map((address) => address.toString()));
|
|
13
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Signs a message. This method is not implemented and should be overridden by subclasses.
|
|
40
|
+
*
|
|
41
|
+
* @param _ - The message to sign, as a string or BytesLike object.
|
|
42
|
+
* @returns A promise that resolves to the signed message as a string.
|
|
43
|
+
* @throws Will throw an error if not implemented.
|
|
44
|
+
*/
|
|
14
45
|
signMessage(_) {
|
|
15
46
|
throw Error("Signer.signMessage not implemented");
|
|
16
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Sends a transaction after signing it.
|
|
50
|
+
*
|
|
51
|
+
* @param tx - The transaction to send, represented as a TransactionLike object.
|
|
52
|
+
* @returns A promise that resolves to the transaction hash as a Hex string.
|
|
53
|
+
*/
|
|
17
54
|
async sendTransaction(tx) {
|
|
18
55
|
return this.client.sendTransaction(await this.signTransaction(tx));
|
|
19
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Signs a transaction.
|
|
59
|
+
*
|
|
60
|
+
* @param tx - The transaction to sign, represented as a TransactionLike object.
|
|
61
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
62
|
+
*/
|
|
20
63
|
async signTransaction(tx) {
|
|
21
|
-
|
|
64
|
+
const preparedTx = await this.prepareTransaction(tx);
|
|
65
|
+
return this.signOnlyTransaction(preparedTx);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
|
|
69
|
+
*
|
|
70
|
+
* @param _ - The transaction to prepare, represented as a TransactionLike object.
|
|
71
|
+
* @returns A promise that resolves to the prepared Transaction object.
|
|
72
|
+
* @throws Will throw an error if not implemented.
|
|
73
|
+
*/
|
|
74
|
+
prepareTransaction(_) {
|
|
75
|
+
throw Error("Signer.prepareTransaction not implemented");
|
|
22
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
|
|
79
|
+
*
|
|
80
|
+
* @param _ - The transaction to sign, represented as a TransactionLike object.
|
|
81
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
82
|
+
* @throws Will throw an error if not implemented.
|
|
83
|
+
*/
|
|
23
84
|
signOnlyTransaction(_) {
|
|
24
85
|
throw Error("Signer.signOnlyTransaction not implemented");
|
|
25
86
|
}
|
|
26
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* An enumeration of signer types.
|
|
90
|
+
*/
|
|
27
91
|
export var SignerType;
|
|
28
92
|
(function (SignerType) {
|
|
29
93
|
SignerType["EVM"] = "EVM";
|
|
30
94
|
SignerType["BTC"] = "BTC";
|
|
31
95
|
})(SignerType || (SignerType = {}));
|
|
96
|
+
/**
|
|
97
|
+
* A class representing information about a signer, including its type and the signer instance.
|
|
98
|
+
*/
|
|
32
99
|
export class SignerInfo {
|
|
33
100
|
constructor(type, signer) {
|
|
34
101
|
this.type = type;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckb-ccc/core",
|
|
3
|
-
"version": "0.0.4-alpha.
|
|
3
|
+
"version": "0.0.4-alpha.11",
|
|
4
4
|
"description": "Common Chains Connector Core",
|
|
5
5
|
"author": "Hanssen0 <hanssen0@hanssen0.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"format": "prettier --write . && eslint --fix"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@ckb-lumos/helpers": "^0.22.2",
|
|
27
26
|
"@eslint/js": "^9.1.1",
|
|
28
27
|
"@types/blake2b": "^2.1.3",
|
|
29
28
|
"eslint": "^9.1.0",
|
|
@@ -40,6 +39,7 @@
|
|
|
40
39
|
"access": "public"
|
|
41
40
|
},
|
|
42
41
|
"dependencies": {
|
|
42
|
+
"@ckb-lumos/helpers": "^0.22.2",
|
|
43
43
|
"@noble/hashes": "^1.4.0",
|
|
44
44
|
"abort-controller": "^3.0.0",
|
|
45
45
|
"bech32": "^2.0.0",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"buffer": "^6.0.3",
|
|
48
48
|
"cross-fetch": "^4.0.0"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "db0297c0753349e2650d2845f42f546316078d5d"
|
|
51
51
|
}
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import { bech32, bech32m } from "bech32";
|
|
2
|
-
import {
|
|
2
|
+
import { hashTypeFromBytes } from "../ckb";
|
|
3
3
|
import { Client, KnownScript } from "../client";
|
|
4
4
|
import { hexFrom } from "../hex";
|
|
5
|
-
import { type
|
|
5
|
+
import { type AddressLike } from "./index";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parses an address string into an address information object.
|
|
9
|
+
*
|
|
10
|
+
* @param address - The address string to parse.
|
|
11
|
+
* @returns An object containing the address prefix, address format, and payload array.
|
|
12
|
+
*
|
|
13
|
+
* @throws Will throw an error if the address format is unknown.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const addressInfo = addressPayloadFromString("ckt1112139193129");
|
|
18
|
+
* console.log(addressInfo.prefix); // Outputs the address prefix
|
|
19
|
+
* console.log(addressInfo.format); // Outputs the address format
|
|
20
|
+
* console.log(addressInfo.payload); // Outputs the payload array
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
6
23
|
|
|
7
24
|
export function addressPayloadFromString(address: string): {
|
|
8
25
|
prefix: string;
|
|
@@ -37,12 +54,31 @@ export function addressPayloadFromString(address: string): {
|
|
|
37
54
|
throw Error(`Unknown address format ${address}`);
|
|
38
55
|
}
|
|
39
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Converts an address payload into an address-like object.
|
|
59
|
+
*
|
|
60
|
+
* @param prefix - The address prefix.
|
|
61
|
+
* @param format - The format of the address, as defined by the AddressFormat enum.
|
|
62
|
+
* @param payload - The payload array containing the address data.
|
|
63
|
+
* @param client - The client instance used to fetch known scripts.
|
|
64
|
+
* @returns A promise that resolves to an AddressLike object.
|
|
65
|
+
*
|
|
66
|
+
* @throws Will throw an error if the payload length is insufficient or if the script type is unknown.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const address = await addressFromPayload("ckt", AddressFormat.Full, [/* payload data *\/], client);
|
|
71
|
+
* console.log(address.script); // Outputs the script object
|
|
72
|
+
* console.log(address.prefix); // Outputs the address prefix
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
|
|
40
76
|
export async function addressFromPayload(
|
|
41
77
|
prefix: string,
|
|
42
78
|
format: AddressFormat,
|
|
43
79
|
payload: number[],
|
|
44
80
|
client: Client,
|
|
45
|
-
): Promise<
|
|
81
|
+
): Promise<AddressLike> {
|
|
46
82
|
if (format === AddressFormat.Full) {
|
|
47
83
|
if (payload.length < 32 + 1) {
|
|
48
84
|
throw new Error(
|
|
@@ -51,11 +87,11 @@ export async function addressFromPayload(
|
|
|
51
87
|
}
|
|
52
88
|
|
|
53
89
|
return {
|
|
54
|
-
script:
|
|
55
|
-
codeHash:
|
|
90
|
+
script: {
|
|
91
|
+
codeHash: payload.slice(0, 32),
|
|
56
92
|
hashType: hashTypeFromBytes(payload.slice(32, 33)),
|
|
57
|
-
args:
|
|
58
|
-
}
|
|
93
|
+
args: payload.slice(33),
|
|
94
|
+
},
|
|
59
95
|
prefix,
|
|
60
96
|
};
|
|
61
97
|
}
|
|
@@ -68,11 +104,11 @@ export async function addressFromPayload(
|
|
|
68
104
|
}
|
|
69
105
|
|
|
70
106
|
return {
|
|
71
|
-
script:
|
|
72
|
-
codeHash:
|
|
107
|
+
script: {
|
|
108
|
+
codeHash: payload.slice(0, 32),
|
|
73
109
|
hashType: "data",
|
|
74
|
-
args:
|
|
75
|
-
}
|
|
110
|
+
args: payload.slice(32),
|
|
111
|
+
},
|
|
76
112
|
prefix,
|
|
77
113
|
};
|
|
78
114
|
}
|
|
@@ -85,11 +121,11 @@ export async function addressFromPayload(
|
|
|
85
121
|
}
|
|
86
122
|
|
|
87
123
|
return {
|
|
88
|
-
script:
|
|
89
|
-
codeHash:
|
|
124
|
+
script: {
|
|
125
|
+
codeHash: payload.slice(0, 32),
|
|
90
126
|
hashType: "type",
|
|
91
|
-
args:
|
|
92
|
-
}
|
|
127
|
+
args: payload.slice(32),
|
|
128
|
+
},
|
|
93
129
|
prefix,
|
|
94
130
|
};
|
|
95
131
|
}
|
|
@@ -112,10 +148,10 @@ export async function addressFromPayload(
|
|
|
112
148
|
}
|
|
113
149
|
|
|
114
150
|
return {
|
|
115
|
-
script:
|
|
151
|
+
script: {
|
|
116
152
|
...(await client.getKnownScript(script)),
|
|
117
|
-
args:
|
|
118
|
-
}
|
|
153
|
+
args: payload.slice(1),
|
|
154
|
+
},
|
|
119
155
|
prefix,
|
|
120
156
|
};
|
|
121
157
|
}
|
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>,
|
|
@@ -45,14 +73,24 @@ export class Address {
|
|
|
45
73
|
);
|
|
46
74
|
}
|
|
47
75
|
|
|
48
|
-
return
|
|
76
|
+
return Address.from(
|
|
77
|
+
await addressFromPayload(prefix, format, payload, client),
|
|
78
|
+
);
|
|
49
79
|
}
|
|
50
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
|
+
|
|
51
89
|
static async fromScript(
|
|
52
90
|
script: ScriptLike,
|
|
53
91
|
client: Client,
|
|
54
92
|
): Promise<Address> {
|
|
55
|
-
return
|
|
93
|
+
return Address.from({ script, prefix: await client.getAddressPrefix() });
|
|
56
94
|
}
|
|
57
95
|
|
|
58
96
|
static async fromKnownScript(
|
|
@@ -60,15 +98,21 @@ export class Address {
|
|
|
60
98
|
args: HexLike,
|
|
61
99
|
client: Client,
|
|
62
100
|
) {
|
|
63
|
-
return
|
|
64
|
-
|
|
101
|
+
return Address.from({
|
|
102
|
+
script: {
|
|
65
103
|
...(await client.getKnownScript(script)),
|
|
66
104
|
args,
|
|
67
|
-
}
|
|
68
|
-
await client.getAddressPrefix(),
|
|
69
|
-
);
|
|
105
|
+
},
|
|
106
|
+
prefix: await client.getAddressPrefix(),
|
|
107
|
+
});
|
|
70
108
|
}
|
|
71
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Converts the Address instance to a string.
|
|
112
|
+
*
|
|
113
|
+
* @returns The address as a string.
|
|
114
|
+
*/
|
|
115
|
+
|
|
72
116
|
toString(): string {
|
|
73
117
|
const data = bytesConcat(
|
|
74
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,
|