@ckb-ccc/core 0.0.10-alpha.0 → 0.0.10-alpha.5

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 (73) hide show
  1. package/dist/ckb/transaction.d.ts +19 -15
  2. package/dist/ckb/transaction.d.ts.map +1 -1
  3. package/dist/ckb/transaction.js +44 -11
  4. package/dist/client/client.d.ts +1 -1
  5. package/dist/client/client.d.ts.map +1 -1
  6. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  7. package/dist/client/clientPublicMainnet.advanced.js +30 -2
  8. package/dist/client/clientPublicTestnet.advanced.js +3 -3
  9. package/dist/client/clientTypes.advanced.d.ts +8 -8
  10. package/dist/client/clientTypes.advanced.d.ts.map +1 -1
  11. package/dist/client/clientTypes.d.ts +2 -2
  12. package/dist/client/clientTypes.d.ts.map +1 -1
  13. package/dist/client/clientTypes.js +2 -2
  14. package/dist/signer/ckb/index.d.ts +2 -0
  15. package/dist/signer/ckb/index.d.ts.map +1 -1
  16. package/dist/signer/ckb/index.js +2 -0
  17. package/dist/signer/ckb/signerCkbPrivateKey.d.ts +4 -13
  18. package/dist/signer/ckb/signerCkbPrivateKey.d.ts.map +1 -1
  19. package/dist/signer/ckb/signerCkbPrivateKey.js +10 -37
  20. package/dist/signer/ckb/signerCkbPublicKey.d.ts +18 -0
  21. package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +1 -0
  22. package/dist/signer/ckb/signerCkbPublicKey.js +44 -0
  23. package/dist/signer/ckb/verifyCkbSecp256k1.d.ts +5 -0
  24. package/dist/signer/ckb/verifyCkbSecp256k1.d.ts.map +1 -0
  25. package/dist/signer/ckb/verifyCkbSecp256k1.js +14 -0
  26. package/dist/signer/signer/index.d.ts.map +1 -1
  27. package/dist/signer/signer/index.js +2 -0
  28. package/dist/utils/index.d.ts +18 -9
  29. package/dist/utils/index.d.ts.map +1 -1
  30. package/dist/utils/index.js +2 -2
  31. package/dist.commonjs/ckb/transaction.d.ts +19 -15
  32. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  33. package/dist.commonjs/ckb/transaction.js +43 -10
  34. package/dist.commonjs/client/client.d.ts +1 -1
  35. package/dist.commonjs/client/client.d.ts.map +1 -1
  36. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  37. package/dist.commonjs/client/clientPublicMainnet.advanced.js +30 -2
  38. package/dist.commonjs/client/clientPublicTestnet.advanced.js +3 -3
  39. package/dist.commonjs/client/clientTypes.advanced.d.ts +8 -8
  40. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -1
  41. package/dist.commonjs/client/clientTypes.d.ts +2 -2
  42. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  43. package/dist.commonjs/client/clientTypes.js +2 -2
  44. package/dist.commonjs/signer/ckb/index.d.ts +2 -0
  45. package/dist.commonjs/signer/ckb/index.d.ts.map +1 -1
  46. package/dist.commonjs/signer/ckb/index.js +2 -0
  47. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.d.ts +4 -13
  48. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.d.ts.map +1 -1
  49. package/dist.commonjs/signer/ckb/signerCkbPrivateKey.js +10 -37
  50. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts +18 -0
  51. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +1 -0
  52. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +48 -0
  53. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.d.ts +5 -0
  54. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.d.ts.map +1 -0
  55. package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.js +19 -0
  56. package/dist.commonjs/signer/signer/index.d.ts.map +1 -1
  57. package/dist.commonjs/signer/signer/index.js +2 -0
  58. package/dist.commonjs/utils/index.d.ts +18 -9
  59. package/dist.commonjs/utils/index.d.ts.map +1 -1
  60. package/dist.commonjs/utils/index.js +2 -2
  61. package/package.json +3 -3
  62. package/src/ckb/transaction.ts +87 -33
  63. package/src/client/client.ts +1 -1
  64. package/src/client/clientPublicMainnet.advanced.ts +34 -2
  65. package/src/client/clientPublicTestnet.advanced.ts +3 -3
  66. package/src/client/clientTypes.advanced.ts +8 -8
  67. package/src/client/clientTypes.ts +4 -4
  68. package/src/signer/ckb/index.ts +2 -0
  69. package/src/signer/ckb/signerCkbPrivateKey.ts +14 -54
  70. package/src/signer/ckb/signerCkbPublicKey.ts +63 -0
  71. package/src/signer/ckb/verifyCkbSecp256k1.ts +27 -0
  72. package/src/signer/signer/index.ts +6 -0
  73. package/src/utils/index.ts +24 -12
@@ -23,7 +23,7 @@ export type ClientTransactionResponse = {
23
23
 
24
24
  export type ClientIndexerSearchKeyFilterLike =
25
25
  ClientCollectableSearchKeyFilterLike & {
26
- blockRange?: [NumLike, NumLike];
26
+ blockRange?: [NumLike, NumLike] | null;
27
27
  };
28
28
  export class ClientIndexerSearchKeyFilter {
29
29
  constructor(
@@ -43,7 +43,7 @@ export class ClientIndexerSearchKeyFilter {
43
43
  apply(Script.from, filterLike.script),
44
44
  apply(clientSearchKeyRangeFrom, filterLike.scriptLenRange),
45
45
  apply(hexFrom, filterLike.outputData),
46
- filterLike.outputDataSearchMode,
46
+ filterLike.outputDataSearchMode ?? undefined,
47
47
  apply(clientSearchKeyRangeFrom, filterLike.outputDataLenRange),
48
48
  apply(clientSearchKeyRangeFrom, filterLike.outputCapacityRange),
49
49
  apply(clientSearchKeyRangeFrom, filterLike.blockRange),
@@ -52,7 +52,7 @@ export class ClientIndexerSearchKeyFilter {
52
52
  }
53
53
 
54
54
  export type ClientIndexerSearchKeyLike = ClientCollectableSearchKeyLike & {
55
- filter?: ClientIndexerSearchKeyFilterLike;
55
+ filter?: ClientIndexerSearchKeyFilterLike | null;
56
56
  };
57
57
 
58
58
  export class ClientIndexerSearchKey {
@@ -70,7 +70,7 @@ export class ClientIndexerSearchKey {
70
70
  keyLike.scriptType,
71
71
  keyLike.scriptSearchMode,
72
72
  apply(ClientIndexerSearchKeyFilter.from, keyLike.filter),
73
- keyLike.withData,
73
+ keyLike.withData ?? undefined,
74
74
  );
75
75
  }
76
76
  }
@@ -1,3 +1,5 @@
1
1
  export * from "./signerCkbPrivateKey";
2
+ export * from "./signerCkbPublicKey";
2
3
  export * from "./signerCkbScriptReadonly";
4
+ export * from "./verifyCkbSecp256k1";
3
5
  export * from "./verifyJoyId";
@@ -1,57 +1,32 @@
1
1
  import { secp256k1 } from "@noble/curves/secp256k1";
2
- import { Address } from "../../address";
3
- import { Bytes, bytesConcat, bytesFrom, BytesLike } from "../../bytes";
2
+ import { bytesConcat, bytesFrom, BytesLike } from "../../bytes";
4
3
  import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
5
- import { Client, KnownScript } from "../../client";
4
+ import { Client } from "../../client";
6
5
  import { hashCkb } from "../../hasher";
7
6
  import { Hex, hexFrom, HexLike } from "../../hex";
8
7
  import { numBeToBytes } from "../../num";
9
- import { Signer, SignerSignType, SignerType } from "../signer";
8
+ import { SignerCkbPublicKey } from "./signerCkbPublicKey";
10
9
 
11
- export class SignerCkbPrivateKey extends Signer {
12
- get type(): SignerType {
13
- return SignerType.CKB;
14
- }
15
-
16
- get signType(): SignerSignType {
17
- return SignerSignType.CkbSecp256k1;
18
- }
19
-
20
- private readonly privateKey: Hex;
10
+ export class SignerCkbPrivateKey extends SignerCkbPublicKey {
11
+ public readonly privateKey: Hex;
21
12
 
22
13
  constructor(client: Client, privateKey: HexLike) {
23
- super(client);
24
- this.privateKey = hexFrom(privateKey);
25
-
26
- if (bytesFrom(this.privateKey).length !== 32) {
14
+ const pk = hexFrom(privateKey);
15
+ if (bytesFrom(pk).length !== 32) {
27
16
  throw new Error("Private key must be 32 bytes!");
28
17
  }
29
- }
30
18
 
31
- async connect(): Promise<void> {}
32
-
33
- async isConnected(): Promise<boolean> {
34
- return true;
35
- }
36
-
37
- async getInternalAddress(): Promise<string> {
38
- return this.getRecommendedAddress();
39
- }
40
-
41
- async getAddressObjs(): Promise<Address[]> {
42
- const publicKey = this.getPublicKey();
43
- return [
44
- await Address.fromKnownScript(
45
- this.client,
46
- KnownScript.Secp256k1Blake160,
47
- bytesFrom(hashCkb(publicKey)).slice(0, 20),
48
- ),
49
- ];
19
+ super(client, secp256k1.getPublicKey(bytesFrom(pk), true));
20
+ this.privateKey = pk;
50
21
  }
51
22
 
52
23
  async signMessageRaw(message: string | BytesLike): Promise<Hex> {
24
+ const msg = typeof message === "string" ? message : hexFrom(message);
25
+ const buffer = bytesFrom(`Nervos Message:${msg}`, "utf8");
26
+ const hash = hashCkb(buffer);
27
+
53
28
  const signature = secp256k1.sign(
54
- bytesFrom(message),
29
+ bytesFrom(hash),
55
30
  bytesFrom(this.privateKey),
56
31
  );
57
32
  const { r, s, recovery } = signature;
@@ -65,21 +40,6 @@ export class SignerCkbPrivateKey extends Signer {
65
40
  );
66
41
  }
67
42
 
68
- getPublicKey(): Bytes {
69
- return secp256k1.getPublicKey(bytesFrom(this.privateKey), true);
70
- }
71
-
72
- async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
73
- const tx = Transaction.from(txLike);
74
- const { script } = await this.getRecommendedAddressObj();
75
- await tx.addCellDepsOfKnownScripts(
76
- this.client,
77
- KnownScript.Secp256k1Blake160,
78
- );
79
- await tx.prepareSighashAllWitness(script, 65, this.client);
80
- return tx;
81
- }
82
-
83
43
  async signOnlyTransaction(txLike: TransactionLike): Promise<Transaction> {
84
44
  const tx = Transaction.from(txLike);
85
45
  const { script } = await this.getRecommendedAddressObj();
@@ -0,0 +1,63 @@
1
+ import { Address } from "../../address";
2
+ import { bytesFrom } from "../../bytes";
3
+ import { Transaction, TransactionLike } from "../../ckb";
4
+ import { Client, KnownScript } from "../../client";
5
+ import { hashCkb } from "../../hasher";
6
+ import { Hex, HexLike, hexFrom } from "../../hex";
7
+ import { Signer, SignerSignType, SignerType } from "../signer";
8
+
9
+ export class SignerCkbPublicKey extends Signer {
10
+ get type(): SignerType {
11
+ return SignerType.CKB;
12
+ }
13
+
14
+ get signType(): SignerSignType {
15
+ return SignerSignType.CkbSecp256k1;
16
+ }
17
+
18
+ public readonly publicKey: Hex;
19
+
20
+ constructor(client: Client, publicKey: HexLike) {
21
+ super(client);
22
+ this.publicKey = hexFrom(publicKey);
23
+
24
+ if (bytesFrom(this.publicKey).length !== 33) {
25
+ throw new Error("Public key must be 33 bytes!");
26
+ }
27
+ }
28
+
29
+ async connect(): Promise<void> {}
30
+
31
+ async isConnected(): Promise<boolean> {
32
+ return true;
33
+ }
34
+
35
+ async getInternalAddress(): Promise<string> {
36
+ return this.getRecommendedAddress();
37
+ }
38
+
39
+ async getIdentity(): Promise<string> {
40
+ return this.publicKey;
41
+ }
42
+
43
+ async getAddressObjs(): Promise<Address[]> {
44
+ return [
45
+ await Address.fromKnownScript(
46
+ this.client,
47
+ KnownScript.Secp256k1Blake160,
48
+ bytesFrom(hashCkb(this.publicKey)).slice(0, 20),
49
+ ),
50
+ ];
51
+ }
52
+
53
+ async prepareTransaction(txLike: TransactionLike): Promise<Transaction> {
54
+ const tx = Transaction.from(txLike);
55
+ const { script } = await this.getRecommendedAddressObj();
56
+ await tx.addCellDepsOfKnownScripts(
57
+ this.client,
58
+ KnownScript.Secp256k1Blake160,
59
+ );
60
+ await tx.prepareSighashAllWitness(script, 65, this.client);
61
+ return tx;
62
+ }
63
+ }
@@ -0,0 +1,27 @@
1
+ import { secp256k1 } from "@noble/curves/secp256k1";
2
+ import { BytesLike, bytesFrom } from "../../bytes";
3
+ import { hashCkb } from "../../hasher";
4
+ import { Hex, hexFrom } from "../../hex";
5
+ import { numFrom } from "../../num";
6
+
7
+ export function messageHashCkbSecp256k1(message: string | BytesLike): Hex {
8
+ const msg = typeof message === "string" ? message : hexFrom(message);
9
+ const buffer = bytesFrom(`Nervos Message:${msg}`, "utf8");
10
+ return hashCkb(buffer);
11
+ }
12
+
13
+ export function verifyMessageCkbSecp256k1(
14
+ message: string | BytesLike,
15
+ signature: string,
16
+ publicKey: string,
17
+ ): boolean {
18
+ const signatureBytes = bytesFrom(signature);
19
+ return secp256k1.verify(
20
+ new secp256k1.Signature(
21
+ numFrom(signatureBytes.slice(0, 32)),
22
+ numFrom(signatureBytes.slice(32, 64)),
23
+ ).addRecoveryBit(Number(numFrom(signatureBytes.slice(64, 65)))),
24
+ bytesFrom(messageHashCkbSecp256k1(message)),
25
+ bytesFrom(publicKey),
26
+ );
27
+ }
@@ -5,6 +5,7 @@ import { Client } from "../../client";
5
5
  import { Hex } from "../../hex";
6
6
  import { Num } from "../../num";
7
7
  import { verifyMessageBtcEcdsa } from "../btc";
8
+ import { verifyMessageCkbSecp256k1 } from "../ckb/verifyCkbSecp256k1";
8
9
  import { verifyMessageJoyId } from "../ckb/verifyJoyId";
9
10
  import { verifyMessageEvmPersonal } from "../evm/verify";
10
11
  import { verifyMessageNostrEvent } from "../nostr/verify";
@@ -118,6 +119,11 @@ export abstract class Signer {
118
119
  signature.identity,
119
120
  );
120
121
  case SignerSignType.CkbSecp256k1:
122
+ return verifyMessageCkbSecp256k1(
123
+ message,
124
+ signature.signature,
125
+ signature.identity,
126
+ );
121
127
  case SignerSignType.Unknown:
122
128
  throw new Error("Unknown signer sign type");
123
129
  }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @param transformer - The transformer.
5
5
  * @param value - The value to be transformed.
6
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
6
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
7
7
  */
8
8
  export function apply<T, R>(
9
9
  transformer: (val: T) => R,
@@ -14,15 +14,15 @@ export function apply<T, R>(
14
14
  *
15
15
  * @param transformer - The transformer.
16
16
  * @param value - The value to be transformed.
17
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
17
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
18
18
  */
19
- export function apply<T, R>(transformer: (val: T) => R, value: null): null;
19
+ export function apply<T, R>(transformer: (val: T) => R, value: null): undefined;
20
20
  /**
21
21
  * A type safe way to apply a transformer on a value if it's not empty.
22
22
  *
23
23
  * @param transformer - The transformer.
24
24
  * @param value - The value to be transformed.
25
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
25
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
26
26
  */
27
27
  export function apply<T, R>(transformer: (val: T) => R, value: T): R;
28
28
  /**
@@ -30,7 +30,7 @@ export function apply<T, R>(transformer: (val: T) => R, value: T): R;
30
30
  *
31
31
  * @param transformer - The transformer.
32
32
  * @param value - The value to be transformed.
33
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
33
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
34
34
  */
35
35
  export function apply<T, R>(
36
36
  transformer: (val: T) => R,
@@ -41,36 +41,48 @@ export function apply<T, R>(
41
41
  *
42
42
  * @param transformer - The transformer.
43
43
  * @param value - The value to be transformed.
44
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
44
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
45
45
  */
46
46
  export function apply<T, R>(
47
47
  transformer: (val: T) => R,
48
48
  value: T | null,
49
- ): R | null;
49
+ ): R | undefined;
50
50
  /**
51
51
  * A type safe way to apply a transformer on a value if it's not empty.
52
52
  *
53
53
  * @param transformer - The transformer.
54
54
  * @param value - The value to be transformed.
55
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
55
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
56
56
  */
57
57
  export function apply<T, R>(
58
58
  transformer: (val: T) => R,
59
59
  value: undefined | null,
60
- ): undefined | null;
60
+ ): undefined;
61
+ /**
62
+ /**
63
+ * A type safe way to apply a transformer on a value if it's not empty.
64
+ *
65
+ * @param transformer - The transformer.
66
+ * @param value - The value to be transformed.
67
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
68
+ */
69
+ export function apply<T, R>(
70
+ transformer: (val: T) => R,
71
+ value: T | undefined | null,
72
+ ): R | undefined;
61
73
  /**
62
74
  * A type safe way to apply a transformer on a value if it's not empty.
63
75
  *
64
76
  * @param transformer - The transformer.
65
77
  * @param value - The value to be transformed.
66
- * @returns If the value is empty, it remains its type. Otherwise it will be transformed.
78
+ * @returns If the value is empty, it becomes undefined. Otherwise it will be transformed.
67
79
  */
68
80
  export function apply<T, R>(
69
81
  transformer: (val: T) => R,
70
82
  value: T | undefined | null,
71
- ): R | undefined | null {
83
+ ): R | undefined {
72
84
  if (value == null) {
73
- return value as undefined | null;
85
+ return undefined;
74
86
  }
75
87
 
76
88
  return transformer(value);