@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/src/signer/helpers.ts
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Script,
|
|
3
|
+
ScriptLike,
|
|
4
|
+
Transaction,
|
|
5
|
+
TransactionLike,
|
|
6
|
+
WitnessArgs,
|
|
7
|
+
} from "../ckb";
|
|
8
|
+
import { Client } from "../client";
|
|
2
9
|
import { Hasher } from "../hasher";
|
|
3
|
-
import { Hex } from "../hex";
|
|
10
|
+
import { Hex, hexFrom } from "../hex";
|
|
4
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Computes the signing hash information for a given transaction and script.
|
|
14
|
+
*
|
|
15
|
+
* @param txLike - The transaction to sign, represented as a TransactionLike object.
|
|
16
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
17
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
18
|
+
* @returns A promise that resolves to an object containing the signing message and the witness position,
|
|
19
|
+
* or undefined if no matching input is found.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const signHashInfo = await getSignHashInfo(transactionLike, scriptLike, client);
|
|
24
|
+
* if (signHashInfo) {
|
|
25
|
+
* console.log(signHashInfo.message); // Outputs the signing message
|
|
26
|
+
* console.log(signHashInfo.position); // Outputs the witness position
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
5
30
|
export async function getSignHashInfo(
|
|
6
31
|
txLike: TransactionLike,
|
|
7
32
|
scriptLike: ScriptLike,
|
|
33
|
+
client: Client,
|
|
8
34
|
): Promise<{ message: Hex; position: number } | undefined> {
|
|
9
35
|
const tx = Transaction.from(txLike);
|
|
10
36
|
const script = Script.from(scriptLike);
|
|
@@ -12,15 +38,16 @@ export async function getSignHashInfo(
|
|
|
12
38
|
const hasher = new Hasher();
|
|
13
39
|
hasher.update(tx.hash());
|
|
14
40
|
|
|
15
|
-
tx.witnesses.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
41
|
+
for (let i = 0; i < tx.witnesses.length; i += 1) {
|
|
42
|
+
if (tx.inputs[i]) {
|
|
43
|
+
const input = await tx.inputs[i].completeExtraInfos(client);
|
|
44
|
+
|
|
45
|
+
if (!input.cellOutput) {
|
|
46
|
+
throw Error("Unable to resolve inputs info");
|
|
20
47
|
}
|
|
21
48
|
|
|
22
|
-
if (!script.eq(input.cellOutput
|
|
23
|
-
|
|
49
|
+
if (!script.eq(input.cellOutput.lock)) {
|
|
50
|
+
continue;
|
|
24
51
|
}
|
|
25
52
|
|
|
26
53
|
if (position === -1) {
|
|
@@ -32,8 +59,8 @@ export async function getSignHashInfo(
|
|
|
32
59
|
return undefined;
|
|
33
60
|
}
|
|
34
61
|
|
|
35
|
-
Transaction.hashWitnessToHasher(
|
|
36
|
-
}
|
|
62
|
+
Transaction.hashWitnessToHasher(tx.witnesses[i], hasher);
|
|
63
|
+
}
|
|
37
64
|
|
|
38
65
|
if (position === -1) {
|
|
39
66
|
return undefined;
|
|
@@ -44,3 +71,52 @@ export async function getSignHashInfo(
|
|
|
44
71
|
position,
|
|
45
72
|
};
|
|
46
73
|
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Prepare dummy witness for sighash all method
|
|
77
|
+
*
|
|
78
|
+
* @param txLike - The transaction to prepare, represented as a TransactionLike object.
|
|
79
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
80
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
81
|
+
* @returns A promise that resolves to the prepared transaction
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const tx = await prepareSighashAllWitness(transactionLike, scriptLike, client);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export async function prepareSighashAllWitness(
|
|
89
|
+
txLike: TransactionLike,
|
|
90
|
+
scriptLike: ScriptLike,
|
|
91
|
+
lockLen: number,
|
|
92
|
+
client: Client,
|
|
93
|
+
): Promise<Transaction> {
|
|
94
|
+
const tx = Transaction.from(txLike);
|
|
95
|
+
const script = Script.from(scriptLike);
|
|
96
|
+
|
|
97
|
+
let position = -1;
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < tx.inputs.length; i += 1) {
|
|
100
|
+
const input = await tx.inputs[i].completeExtraInfos(client);
|
|
101
|
+
|
|
102
|
+
if (!input.cellOutput) {
|
|
103
|
+
throw Error("Unable to resolve inputs info");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (script.eq(input.cellOutput.lock)) {
|
|
107
|
+
position = i;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (position === -1) {
|
|
112
|
+
return tx;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const witness = tx.witnesses[position]
|
|
116
|
+
? WitnessArgs.fromBytes(tx.witnesses[position])
|
|
117
|
+
: WitnessArgs.from({});
|
|
118
|
+
witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
|
|
119
|
+
tx.witnesses[position] = hexFrom(witness.toBytes());
|
|
120
|
+
|
|
121
|
+
return tx;
|
|
122
|
+
}
|
package/src/signer/signer.ts
CHANGED
|
@@ -1,52 +1,152 @@
|
|
|
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
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
|
-
constructor(
|
|
12
|
+
constructor(protected client_: Client) {}
|
|
9
13
|
|
|
14
|
+
get client(): Client {
|
|
15
|
+
return this.client_;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Construct a new signer with the client replaced.
|
|
20
|
+
*
|
|
21
|
+
* @returns A promise that resolves the new Signer.
|
|
22
|
+
*/
|
|
23
|
+
abstract replaceClient(client: Client): Promise<Signer>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Connects to the signer.
|
|
27
|
+
*
|
|
28
|
+
* @returns A promise that resolves when the connection is complete.
|
|
29
|
+
*/
|
|
10
30
|
abstract connect(): Promise<void>;
|
|
11
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Check if the signer is connected.
|
|
34
|
+
*
|
|
35
|
+
* @returns A promise that resolves the connection status.
|
|
36
|
+
*/
|
|
37
|
+
abstract isConnected(): Promise<boolean>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Gets the internal address associated with the signer.
|
|
41
|
+
*
|
|
42
|
+
* @returns A promise that resolves to a string representing the internal address.
|
|
43
|
+
*/
|
|
12
44
|
abstract getInternalAddress(): Promise<string>;
|
|
13
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Gets an array of Address objects associated with the signer.
|
|
48
|
+
*
|
|
49
|
+
* @returns A promise that resolves to an array of Address objects.
|
|
50
|
+
*/
|
|
14
51
|
abstract getAddressObjs(): Promise<Address[]>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Gets the recommended Address object for the signer.
|
|
55
|
+
*
|
|
56
|
+
* @param _preference - Optional preference parameter.
|
|
57
|
+
* @returns A promise that resolves to the recommended Address object.
|
|
58
|
+
*/
|
|
15
59
|
async getRecommendedAddressObj(_preference?: unknown): Promise<Address> {
|
|
16
60
|
return (await this.getAddressObjs())[0];
|
|
17
61
|
}
|
|
18
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Gets the recommended address for the signer as a string.
|
|
65
|
+
*
|
|
66
|
+
* @param preference - Optional preference parameter.
|
|
67
|
+
* @returns A promise that resolves to the recommended address as a string.
|
|
68
|
+
*/
|
|
19
69
|
async getRecommendedAddress(preference?: unknown): Promise<string> {
|
|
20
70
|
return (await this.getRecommendedAddressObj(preference)).toString();
|
|
21
71
|
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Gets an array of addresses associated with the signer as strings.
|
|
75
|
+
*
|
|
76
|
+
* @returns A promise that resolves to an array of addresses as strings.
|
|
77
|
+
*/
|
|
22
78
|
async getAddresses(): Promise<string[]> {
|
|
23
79
|
return this.getAddressObjs().then((addresses) =>
|
|
24
80
|
addresses.map((address) => address.toString()),
|
|
25
81
|
);
|
|
26
82
|
}
|
|
27
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Signs a message. This method is not implemented and should be overridden by subclasses.
|
|
86
|
+
*
|
|
87
|
+
* @param _ - The message to sign, as a string or BytesLike object.
|
|
88
|
+
* @returns A promise that resolves to the signed message as a string.
|
|
89
|
+
* @throws Will throw an error if not implemented.
|
|
90
|
+
*/
|
|
28
91
|
signMessage(_: string | BytesLike): Promise<string> {
|
|
29
92
|
throw Error("Signer.signMessage not implemented");
|
|
30
93
|
}
|
|
31
94
|
|
|
32
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Sends a transaction after signing it.
|
|
97
|
+
*
|
|
98
|
+
* @param tx - The transaction to send, represented as a TransactionLike object.
|
|
99
|
+
* @returns A promise that resolves to the transaction hash as a Hex string.
|
|
100
|
+
*/
|
|
101
|
+
async sendTransaction(tx: TransactionLike): Promise<Hex> {
|
|
33
102
|
return this.client.sendTransaction(await this.signTransaction(tx));
|
|
34
103
|
}
|
|
35
104
|
|
|
36
|
-
|
|
37
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Signs a transaction.
|
|
107
|
+
*
|
|
108
|
+
* @param tx - The transaction to sign, represented as a TransactionLike object.
|
|
109
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
110
|
+
*/
|
|
111
|
+
async signTransaction(tx: TransactionLike): Promise<Transaction> {
|
|
112
|
+
const preparedTx = await this.prepareTransaction(tx);
|
|
113
|
+
return this.signOnlyTransaction(preparedTx);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* prepare a transaction before signing. This method is not implemented and should be overridden by subclasses.
|
|
118
|
+
*
|
|
119
|
+
* @param _ - The transaction to prepare, represented as a TransactionLike object.
|
|
120
|
+
* @returns A promise that resolves to the prepared Transaction object.
|
|
121
|
+
* @throws Will throw an error if not implemented.
|
|
122
|
+
*/
|
|
123
|
+
prepareTransaction(_: TransactionLike): Promise<Transaction> {
|
|
124
|
+
throw Error("Signer.prepareTransaction not implemented");
|
|
38
125
|
}
|
|
39
126
|
|
|
40
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
|
|
129
|
+
*
|
|
130
|
+
* @param _ - The transaction to sign, represented as a TransactionLike object.
|
|
131
|
+
* @returns A promise that resolves to the signed Transaction object.
|
|
132
|
+
* @throws Will throw an error if not implemented.
|
|
133
|
+
*/
|
|
134
|
+
signOnlyTransaction(_: TransactionLike): Promise<Transaction> {
|
|
41
135
|
throw Error("Signer.signOnlyTransaction not implemented");
|
|
42
136
|
}
|
|
43
137
|
}
|
|
44
138
|
|
|
139
|
+
/**
|
|
140
|
+
* An enumeration of signer types.
|
|
141
|
+
*/
|
|
45
142
|
export enum SignerType {
|
|
46
143
|
EVM = "EVM",
|
|
47
144
|
BTC = "BTC",
|
|
48
145
|
}
|
|
49
146
|
|
|
147
|
+
/**
|
|
148
|
+
* A class representing information about a signer, including its type and the signer instance.
|
|
149
|
+
*/
|
|
50
150
|
export class SignerInfo {
|
|
51
151
|
constructor(
|
|
52
152
|
public type: SignerType,
|
|
@@ -54,8 +154,10 @@ export class SignerInfo {
|
|
|
54
154
|
) {}
|
|
55
155
|
}
|
|
56
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Represents a wallet with a name, icon, and an array of signer information.
|
|
159
|
+
*/
|
|
57
160
|
export type Wallet = {
|
|
58
161
|
name: string;
|
|
59
162
|
icon: string;
|
|
60
|
-
signers: SignerInfo[];
|
|
61
163
|
};
|