@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.
Files changed (114) hide show
  1. package/README.md +12 -7
  2. package/dist/address/address.advanced.d.ts +34 -0
  3. package/dist/address/address.advanced.d.ts.map +1 -1
  4. package/dist/address/address.advanced.js +41 -7
  5. package/dist/address/index.d.ts +36 -0
  6. package/dist/address/index.d.ts.map +1 -1
  7. package/dist/address/index.js +44 -5
  8. package/dist/bytes/advanced.d.ts +3 -0
  9. package/dist/bytes/advanced.d.ts.map +1 -1
  10. package/dist/bytes/index.d.ts +57 -0
  11. package/dist/bytes/index.d.ts.map +1 -1
  12. package/dist/bytes/index.js +57 -0
  13. package/dist/ckb/script.d.ts +98 -1
  14. package/dist/ckb/script.d.ts.map +1 -1
  15. package/dist/ckb/script.js +98 -1
  16. package/dist/ckb/transaction.d.ts +392 -1
  17. package/dist/ckb/transaction.d.ts.map +1 -1
  18. package/dist/ckb/transaction.js +403 -1
  19. package/dist/client/advanced.d.ts +1 -0
  20. package/dist/client/advanced.d.ts.map +1 -1
  21. package/dist/client/advanced.js +1 -0
  22. package/dist/client/client.d.ts +10 -8
  23. package/dist/client/client.d.ts.map +1 -1
  24. package/dist/client/client.js +18 -0
  25. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  26. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  27. package/dist/client/clientPublicMainnet.d.ts +3 -3
  28. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  29. package/dist/client/clientPublicMainnet.js +2 -2
  30. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  31. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  32. package/dist/client/clientPublicTestnet.d.ts +3 -3
  33. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  34. package/dist/client/clientPublicTestnet.js +2 -2
  35. package/dist/client/clientTypes.d.ts +6 -0
  36. package/dist/client/clientTypes.d.ts.map +1 -1
  37. package/dist/client/index.d.ts +1 -0
  38. package/dist/client/index.d.ts.map +1 -1
  39. package/dist/client/index.js +1 -0
  40. package/dist/client/jsonRpc/advanced.d.ts +2 -87
  41. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  42. package/dist/client/jsonRpc/advanced.js +2 -65
  43. package/dist/client/jsonRpc/index.d.ts +60 -6
  44. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  45. package/dist/client/jsonRpc/index.js +72 -7
  46. package/dist/client/jsonRpc/transformers.d.ts +28 -0
  47. package/dist/client/jsonRpc/transformers.d.ts.map +1 -0
  48. package/dist/client/jsonRpc/transformers.js +122 -0
  49. package/dist/client/jsonRpc/types.d.ts +41 -0
  50. package/dist/client/jsonRpc/types.d.ts.map +1 -0
  51. package/dist/client/jsonRpc/types.js +1 -0
  52. package/dist/fixedPoint/index.d.ts +43 -1
  53. package/dist/fixedPoint/index.d.ts.map +1 -1
  54. package/dist/fixedPoint/index.js +39 -1
  55. package/dist/hasher/index.d.ts +43 -2
  56. package/dist/hasher/index.d.ts.map +1 -1
  57. package/dist/hasher/index.js +42 -0
  58. package/dist/hex/index.d.ts +19 -0
  59. package/dist/hex/index.d.ts.map +1 -1
  60. package/dist/hex/index.js +12 -0
  61. package/dist/num/index.d.ts +99 -0
  62. package/dist/num/index.d.ts.map +1 -1
  63. package/dist/num/index.js +92 -0
  64. package/dist/signer/btc/signerBtc.d.ts +40 -2
  65. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  66. package/dist/signer/btc/signerBtc.js +36 -4
  67. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +48 -0
  68. package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
  69. package/dist/signer/btc/signerBtcPublicKeyReadonly.js +53 -0
  70. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +47 -0
  71. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  72. package/dist/signer/ckb/signerCkbScriptReadonly.js +52 -0
  73. package/dist/signer/evm/signerEvm.d.ts +34 -2
  74. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  75. package/dist/signer/evm/signerEvm.js +35 -4
  76. package/dist/signer/evm/signerEvmAddressReadonly.d.ts +43 -1
  77. package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
  78. package/dist/signer/evm/signerEvmAddressReadonly.js +47 -0
  79. package/dist/signer/helpers.d.ts +35 -2
  80. package/dist/signer/helpers.d.ts.map +1 -1
  81. package/dist/signer/helpers.js +67 -11
  82. package/dist/signer/signer.d.ts +98 -7
  83. package/dist/signer/signer.d.ts.map +1 -1
  84. package/dist/signer/signer.js +70 -3
  85. package/package.json +3 -3
  86. package/src/address/address.advanced.ts +54 -18
  87. package/src/address/index.ts +51 -7
  88. package/src/bytes/advanced.ts +12 -9
  89. package/src/bytes/index.ts +60 -0
  90. package/src/ckb/script.ts +111 -2
  91. package/src/ckb/transaction.ts +455 -2
  92. package/src/client/advanced.ts +1 -0
  93. package/src/client/client.ts +31 -8
  94. package/src/client/clientPublicMainnet.advanced.ts +5 -2
  95. package/src/client/clientPublicMainnet.ts +6 -4
  96. package/src/client/clientPublicTestnet.advanced.ts +5 -2
  97. package/src/client/clientPublicTestnet.ts +6 -4
  98. package/src/client/clientTypes.ts +13 -0
  99. package/src/client/index.ts +1 -0
  100. package/src/client/jsonRpc/advanced.ts +2 -93
  101. package/src/client/jsonRpc/index.ts +99 -11
  102. package/src/client/jsonRpc/transformers.ts +161 -0
  103. package/src/client/jsonRpc/types.ts +48 -0
  104. package/src/fixedPoint/index.ts +52 -2
  105. package/src/hasher/index.ts +47 -1
  106. package/src/hex/index.ts +19 -0
  107. package/src/num/index.ts +101 -0
  108. package/src/signer/btc/signerBtc.ts +47 -4
  109. package/src/signer/btc/signerBtcPublicKeyReadonly.ts +54 -0
  110. package/src/signer/ckb/signerCkbScriptReadonly.ts +54 -0
  111. package/src/signer/evm/signerEvm.ts +42 -4
  112. package/src/signer/evm/signerEvmAddressReadonly.ts +50 -1
  113. package/src/signer/helpers.ts +87 -11
  114. package/src/signer/signer.ts +109 -7
@@ -1,10 +1,36 @@
1
- import { Script, ScriptLike, Transaction, TransactionLike } from "../ckb";
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.forEach((witness, i) => {
16
- const input = tx.inputs[i];
17
- if (input) {
18
- if (!input.cellOutput?.lock) {
19
- throw Error("Incomplete inputs info");
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?.lock)) {
23
- return;
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(witness, hasher);
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
+ }
@@ -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(public readonly client: Client) {}
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
- async sendTransaction(tx: Transaction): Promise<Hex> {
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
- async signTransaction(tx: Transaction): Promise<Transaction> {
37
- return this.signOnlyTransaction(tx);
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
- signOnlyTransaction(_: Transaction): Promise<Transaction> {
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
  };