@ckb-ccc/core 0.0.5-alpha.1 → 0.0.5-alpha.8

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 (149) hide show
  1. package/dist/address/index.d.ts +1 -1
  2. package/dist/address/index.d.ts.map +1 -1
  3. package/dist/address/index.js +4 -4
  4. package/dist/client/client.d.ts +2 -2
  5. package/dist/client/client.d.ts.map +1 -1
  6. package/dist/client/clientPublicMainnet.d.ts +1 -1
  7. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  8. package/dist/client/clientPublicMainnet.js +1 -1
  9. package/dist/client/clientPublicTestnet.d.ts +1 -1
  10. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  11. package/dist/client/clientPublicTestnet.js +1 -1
  12. package/dist/client/jsonRpc/index.d.ts +3 -3
  13. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  14. package/dist/client/jsonRpc/index.js +4 -4
  15. package/dist/signer/btc/index.d.ts +1 -0
  16. package/dist/signer/btc/index.d.ts.map +1 -1
  17. package/dist/signer/btc/index.js +1 -0
  18. package/dist/signer/btc/signerBtc.d.ts +9 -1
  19. package/dist/signer/btc/signerBtc.d.ts.map +1 -1
  20. package/dist/signer/btc/signerBtc.js +16 -2
  21. package/dist/signer/btc/verify.d.ts +3 -0
  22. package/dist/signer/btc/verify.d.ts.map +1 -0
  23. package/dist/signer/btc/verify.js +6 -0
  24. package/dist/signer/ckb/index.d.ts +1 -0
  25. package/dist/signer/ckb/index.d.ts.map +1 -1
  26. package/dist/signer/ckb/index.js +1 -0
  27. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +3 -1
  28. package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  29. package/dist/signer/ckb/signerCkbScriptReadonly.js +8 -2
  30. package/dist/signer/ckb/verifyJoyId.d.ts +3 -0
  31. package/dist/signer/ckb/verifyJoyId.d.ts.map +1 -0
  32. package/dist/signer/ckb/verifyJoyId.js +12 -0
  33. package/dist/signer/dummy/alwaysError.d.ts +10 -0
  34. package/dist/signer/dummy/alwaysError.d.ts.map +1 -0
  35. package/dist/signer/dummy/alwaysError.js +13 -0
  36. package/dist/signer/dummy/dummy.d.ts +12 -0
  37. package/dist/signer/dummy/dummy.d.ts.map +1 -0
  38. package/dist/signer/dummy/dummy.js +19 -0
  39. package/dist/signer/dummy/index.d.ts +4 -0
  40. package/dist/signer/dummy/index.d.ts.map +1 -0
  41. package/dist/signer/dummy/index.js +3 -0
  42. package/dist/signer/dummy/openLink.d.ts +10 -0
  43. package/dist/signer/dummy/openLink.d.ts.map +1 -0
  44. package/dist/signer/dummy/openLink.js +13 -0
  45. package/dist/signer/evm/index.d.ts +1 -0
  46. package/dist/signer/evm/index.d.ts.map +1 -1
  47. package/dist/signer/evm/index.js +1 -0
  48. package/dist/signer/evm/signerEvm.d.ts +3 -1
  49. package/dist/signer/evm/signerEvm.d.ts.map +1 -1
  50. package/dist/signer/evm/signerEvm.js +8 -2
  51. package/dist/signer/evm/verify.d.ts +3 -0
  52. package/dist/signer/evm/verify.d.ts.map +1 -0
  53. package/dist/signer/evm/verify.js +6 -0
  54. package/dist/signer/index.d.ts +1 -0
  55. package/dist/signer/index.d.ts.map +1 -1
  56. package/dist/signer/index.js +1 -0
  57. package/dist/signer/{signer.d.ts → signer/index.d.ts} +67 -21
  58. package/dist/signer/signer/index.d.ts.map +1 -0
  59. package/dist/signer/{signer.js → signer/index.js} +95 -16
  60. package/dist/utils/index.d.ts +1 -0
  61. package/dist/utils/index.d.ts.map +1 -1
  62. package/dist/utils/index.js +3 -0
  63. package/dist.commonjs/address/index.d.ts +1 -1
  64. package/dist.commonjs/address/index.d.ts.map +1 -1
  65. package/dist.commonjs/address/index.js +4 -4
  66. package/dist.commonjs/client/client.d.ts +2 -2
  67. package/dist.commonjs/client/client.d.ts.map +1 -1
  68. package/dist.commonjs/client/clientPublicMainnet.d.ts +1 -1
  69. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  70. package/dist.commonjs/client/clientPublicMainnet.js +1 -1
  71. package/dist.commonjs/client/clientPublicTestnet.d.ts +1 -1
  72. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  73. package/dist.commonjs/client/clientPublicTestnet.js +1 -1
  74. package/dist.commonjs/client/jsonRpc/index.d.ts +3 -3
  75. package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
  76. package/dist.commonjs/client/jsonRpc/index.js +4 -4
  77. package/dist.commonjs/signer/btc/index.d.ts +1 -0
  78. package/dist.commonjs/signer/btc/index.d.ts.map +1 -1
  79. package/dist.commonjs/signer/btc/index.js +1 -0
  80. package/dist.commonjs/signer/btc/signerBtc.d.ts +9 -1
  81. package/dist.commonjs/signer/btc/signerBtc.d.ts.map +1 -1
  82. package/dist.commonjs/signer/btc/signerBtc.js +15 -1
  83. package/dist.commonjs/signer/btc/verify.d.ts +3 -0
  84. package/dist.commonjs/signer/btc/verify.d.ts.map +1 -0
  85. package/dist.commonjs/signer/btc/verify.js +10 -0
  86. package/dist.commonjs/signer/ckb/index.d.ts +1 -0
  87. package/dist.commonjs/signer/ckb/index.d.ts.map +1 -1
  88. package/dist.commonjs/signer/ckb/index.js +1 -0
  89. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +3 -1
  90. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
  91. package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +7 -1
  92. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +3 -0
  93. package/dist.commonjs/signer/ckb/verifyJoyId.d.ts.map +1 -0
  94. package/dist.commonjs/signer/ckb/verifyJoyId.js +16 -0
  95. package/dist.commonjs/signer/dummy/alwaysError.d.ts +10 -0
  96. package/dist.commonjs/signer/dummy/alwaysError.d.ts.map +1 -0
  97. package/dist.commonjs/signer/dummy/alwaysError.js +17 -0
  98. package/dist.commonjs/signer/dummy/dummy.d.ts +12 -0
  99. package/dist.commonjs/signer/dummy/dummy.d.ts.map +1 -0
  100. package/dist.commonjs/signer/dummy/dummy.js +23 -0
  101. package/dist.commonjs/signer/dummy/index.d.ts +4 -0
  102. package/dist.commonjs/signer/dummy/index.d.ts.map +1 -0
  103. package/dist.commonjs/signer/dummy/index.js +19 -0
  104. package/dist.commonjs/signer/dummy/openLink.d.ts +10 -0
  105. package/dist.commonjs/signer/dummy/openLink.d.ts.map +1 -0
  106. package/dist.commonjs/signer/dummy/openLink.js +17 -0
  107. package/dist.commonjs/signer/evm/index.d.ts +1 -0
  108. package/dist.commonjs/signer/evm/index.d.ts.map +1 -1
  109. package/dist.commonjs/signer/evm/index.js +1 -0
  110. package/dist.commonjs/signer/evm/signerEvm.d.ts +3 -1
  111. package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
  112. package/dist.commonjs/signer/evm/signerEvm.js +7 -1
  113. package/dist.commonjs/signer/evm/verify.d.ts +3 -0
  114. package/dist.commonjs/signer/evm/verify.d.ts.map +1 -0
  115. package/dist.commonjs/signer/evm/verify.js +10 -0
  116. package/dist.commonjs/signer/index.d.ts +1 -0
  117. package/dist.commonjs/signer/index.d.ts.map +1 -1
  118. package/dist.commonjs/signer/index.js +1 -0
  119. package/dist.commonjs/signer/{signer.d.ts → signer/index.d.ts} +67 -21
  120. package/dist.commonjs/signer/signer/index.d.ts.map +1 -0
  121. package/dist.commonjs/signer/{signer.js → signer/index.js} +97 -17
  122. package/dist.commonjs/utils/index.d.ts +1 -0
  123. package/dist.commonjs/utils/index.d.ts.map +1 -1
  124. package/dist.commonjs/utils/index.js +5 -1
  125. package/package.json +6 -3
  126. package/src/address/index.ts +4 -7
  127. package/src/client/client.ts +2 -2
  128. package/src/client/clientPublicMainnet.ts +1 -1
  129. package/src/client/clientPublicTestnet.ts +1 -1
  130. package/src/client/jsonRpc/index.ts +3 -3
  131. package/src/signer/btc/index.ts +1 -0
  132. package/src/signer/btc/signerBtc.ts +19 -2
  133. package/src/signer/btc/verify.ts +14 -0
  134. package/src/signer/ckb/index.ts +1 -0
  135. package/src/signer/ckb/signerCkbScriptReadonly.ts +10 -2
  136. package/src/signer/ckb/verifyJoyId.ts +20 -0
  137. package/src/signer/dummy/alwaysError.ts +21 -0
  138. package/src/signer/dummy/dummy.ts +28 -0
  139. package/src/signer/dummy/index.ts +3 -0
  140. package/src/signer/dummy/openLink.ts +21 -0
  141. package/src/signer/evm/index.ts +1 -0
  142. package/src/signer/evm/signerEvm.ts +10 -2
  143. package/src/signer/evm/verify.ts +16 -0
  144. package/src/signer/index.ts +1 -0
  145. package/src/signer/{signer.ts → signer/index.ts} +131 -21
  146. package/src/utils/index.ts +4 -0
  147. package/dist/signer/signer.d.ts.map +0 -1
  148. package/dist.commonjs/signer/signer.d.ts.map +0 -1
  149. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.apply = void 0;
3
+ exports.isWebview = exports.apply = void 0;
4
4
  function apply(transformer, value) {
5
5
  if (value == null) {
6
6
  return value;
@@ -8,3 +8,7 @@ function apply(transformer, value) {
8
8
  return transformer(value);
9
9
  }
10
10
  exports.apply = apply;
11
+ function isWebview(userAgent) {
12
+ return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
13
+ }
14
+ exports.isWebview = isWebview;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/core",
3
- "version": "0.0.5-alpha.1",
3
+ "version": "0.0.5-alpha.8",
4
4
  "description": "Common Chains Connector Core",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -57,12 +57,15 @@
57
57
  },
58
58
  "dependencies": {
59
59
  "@ckb-lumos/helpers": "^0.22.2",
60
+ "@joyid/ckb": "^0.0.11",
60
61
  "@noble/hashes": "^1.4.0",
62
+ "@unisat/wallet-utils": "^1.0.0",
61
63
  "abort-controller": "^3.0.0",
62
64
  "bech32": "^2.0.0",
63
65
  "blake2b": "^2.1.4",
64
66
  "buffer": "^6.0.3",
65
- "cross-fetch": "^4.0.0"
67
+ "cross-fetch": "^4.0.0",
68
+ "ethers": "^6.13.1"
66
69
  },
67
- "gitHead": "98d6d902fff0cc3d65f536cb85b32c867045d15d"
70
+ "gitHead": "f12b8758d8581bfae0c9a00abbb5f7d2c6d03147"
68
71
  }
@@ -66,7 +66,7 @@ export class Address {
66
66
  if (!client) {
67
67
  throw new Error(`Unknown address prefix ${prefix}`);
68
68
  }
69
- const expectedPrefix = await client.getAddressPrefix();
69
+ const expectedPrefix = client.addressPrefix;
70
70
  if (expectedPrefix !== prefix) {
71
71
  throw new Error(
72
72
  `Unknown address prefix ${prefix}, expected ${expectedPrefix}`,
@@ -86,11 +86,8 @@ export class Address {
86
86
  * @returns A promise that resolves to an Address instance.
87
87
  */
88
88
 
89
- static async fromScript(
90
- script: ScriptLike,
91
- client: Client,
92
- ): Promise<Address> {
93
- return Address.from({ script, prefix: await client.getAddressPrefix() });
89
+ static fromScript(script: ScriptLike, client: Client): Address {
90
+ return Address.from({ script, prefix: client.addressPrefix });
94
91
  }
95
92
 
96
93
  static async fromKnownScript(
@@ -103,7 +100,7 @@ export class Address {
103
100
  ...(await client.getKnownScript(script)),
104
101
  args,
105
102
  },
106
- prefix: await client.getAddressPrefix(),
103
+ prefix: client.addressPrefix,
107
104
  });
108
105
  }
109
106
 
@@ -25,9 +25,9 @@ export enum KnownScript {
25
25
  }
26
26
 
27
27
  export abstract class Client {
28
- abstract getUrl(): string;
28
+ abstract get url(): string;
29
+ abstract get addressPrefix(): string;
29
30
 
30
- abstract getAddressPrefix(): Promise<string>;
31
31
  abstract getKnownScript(
32
32
  script: KnownScript,
33
33
  ): Promise<Pick<Script, "codeHash" | "hashType">>;
@@ -8,7 +8,7 @@ export class ClientPublicMainnet extends ClientJsonRpc {
8
8
  super(url, timeout);
9
9
  }
10
10
 
11
- async getAddressPrefix(): Promise<string> {
11
+ get addressPrefix(): string {
12
12
  return "ckb";
13
13
  }
14
14
 
@@ -8,7 +8,7 @@ export class ClientPublicTestnet extends ClientJsonRpc {
8
8
  super(url, timeout);
9
9
  }
10
10
 
11
- async getAddressPrefix(): Promise<string> {
11
+ get addressPrefix(): string {
12
12
  return "ckt";
13
13
  }
14
14
 
@@ -49,7 +49,7 @@ export abstract class ClientJsonRpc extends Client {
49
49
  */
50
50
 
51
51
  constructor(
52
- private readonly url: string,
52
+ private readonly url_: string,
53
53
  private readonly timeout = 30000,
54
54
  ) {
55
55
  super();
@@ -61,8 +61,8 @@ export abstract class ClientJsonRpc extends Client {
61
61
  * @returns The URL of the JSON-RPC server.
62
62
  */
63
63
 
64
- getUrl() {
65
- return this.url;
64
+ get url() {
65
+ return this.url_;
66
66
  }
67
67
 
68
68
  /**
@@ -1,2 +1,3 @@
1
1
  export * from "./signerBtc";
2
2
  export * from "./signerBtcPublicKeyReadonly";
3
+ export * from "./verify";
@@ -6,7 +6,7 @@ import { Transaction, TransactionLike, WitnessArgs } from "../../ckb";
6
6
  import { KnownScript } from "../../client";
7
7
  import { HexLike, hexFrom } from "../../hex";
8
8
  import { numToBytes } from "../../num";
9
- import { Signer } from "../signer";
9
+ import { Signer, SignerSignType, SignerType } from "../signer";
10
10
 
11
11
  /**
12
12
  * An abstract class extending the Signer class for Bitcoin-like signing operations.
@@ -14,6 +14,14 @@ import { Signer } from "../signer";
14
14
  * as well as signing transactions.
15
15
  */
16
16
  export abstract class SignerBtc extends Signer {
17
+ get type(): SignerType {
18
+ return SignerType.BTC;
19
+ }
20
+
21
+ get signType(): SignerSignType {
22
+ return SignerSignType.BtcEcdsa;
23
+ }
24
+
17
25
  /**
18
26
  * Gets the Bitcoin account associated with the signer.
19
27
  *
@@ -37,6 +45,15 @@ export abstract class SignerBtc extends Signer {
37
45
  return this.getBtcAccount();
38
46
  }
39
47
 
48
+ /**
49
+ * Gets the identity, which is the Bitcoin public key in this case.
50
+ *
51
+ * @returns A promise that resolves to a string representing the identity
52
+ */
53
+ async getIdentity(): Promise<string> {
54
+ return hexFrom(await this.getBtcPublicKey()).slice(2);
55
+ }
56
+
40
57
  /**
41
58
  * Gets an array of Address objects representing the known script addresses for the signer.
42
59
  *
@@ -85,7 +102,7 @@ export abstract class SignerBtc extends Signer {
85
102
  }
86
103
 
87
104
  const signature = bytesFrom(
88
- await this.signMessage(
105
+ await this.signMessageRaw(
89
106
  `CKB (Bitcoin Layer) transaction: ${info.message}`,
90
107
  ),
91
108
  "base64",
@@ -0,0 +1,14 @@
1
+ import { verifyMessage } from "@unisat/wallet-utils";
2
+ import { BytesLike } from "../../bytes";
3
+ import { hexFrom } from "../../hex";
4
+
5
+ export function verifyMessageBtcEcdsa(
6
+ message: string | BytesLike,
7
+ signature: string,
8
+ publicKey: string,
9
+ ): boolean {
10
+ const challenge =
11
+ typeof message === "string" ? message : hexFrom(message).slice(2);
12
+
13
+ return verifyMessage(publicKey, challenge, signature);
14
+ }
@@ -1 +1,2 @@
1
1
  export * from "./signerCkbScriptReadonly";
2
+ export * from "./verifyJoyId";
@@ -1,13 +1,21 @@
1
1
  import { Address } from "../../address";
2
2
  import { Script, ScriptLike } from "../../ckb";
3
3
  import { Client } from "../../client";
4
- import { Signer } from "../signer";
4
+ import { Signer, SignerSignType, SignerType } from "../signer";
5
5
 
6
6
  /**
7
7
  * A class extending Signer that provides read-only access to a CKB script.
8
8
  * This class does not support signing operations.
9
9
  */
10
10
  export class SignerCkbScriptReadonly extends Signer {
11
+ get type(): SignerType {
12
+ return SignerType.CKB;
13
+ }
14
+
15
+ get signType(): SignerSignType {
16
+ return SignerSignType.Unknown;
17
+ }
18
+
11
19
  private readonly script: Script;
12
20
 
13
21
  /**
@@ -73,6 +81,6 @@ export class SignerCkbScriptReadonly extends Signer {
73
81
  */
74
82
 
75
83
  async getAddressObjs(): Promise<Address[]> {
76
- return [await Address.fromScript(this.script, this.client)];
84
+ return [Address.fromScript(this.script, this.client)];
77
85
  }
78
86
  }
@@ -0,0 +1,20 @@
1
+ import { verifySignature } from "@joyid/ckb";
2
+ import { BytesLike } from "../../bytes";
3
+ import { hexFrom } from "../../hex";
4
+
5
+ export function verifyMessageJoyId(
6
+ message: string | BytesLike,
7
+ signature: string,
8
+ identity: string,
9
+ ): Promise<boolean> {
10
+ const challenge =
11
+ typeof message === "string" ? message : hexFrom(message).slice(2);
12
+ const { publicKey, keyType } = JSON.parse(identity);
13
+
14
+ return verifySignature({
15
+ challenge,
16
+ pubkey: publicKey,
17
+ keyType,
18
+ ...JSON.parse(signature),
19
+ });
20
+ }
@@ -0,0 +1,21 @@
1
+ import { Client } from "../../client";
2
+ import { SignerType } from "../signer";
3
+ import { SignerDummy } from "./dummy";
4
+
5
+ export class SignerAlwaysError extends SignerDummy {
6
+ constructor(
7
+ client: Client,
8
+ type: SignerType,
9
+ private readonly message: string,
10
+ ) {
11
+ super(client, type);
12
+ }
13
+
14
+ async replaceClient(client: Client): Promise<SignerAlwaysError> {
15
+ return new SignerAlwaysError(client, this.type, this.message);
16
+ }
17
+
18
+ async connect(): Promise<void> {
19
+ throw new Error(this.message);
20
+ }
21
+ }
@@ -0,0 +1,28 @@
1
+ import { Address } from "../../address";
2
+ import { Client } from "../../client";
3
+ import { Signer, SignerSignType, SignerType } from "../signer";
4
+
5
+ export abstract class SignerDummy extends Signer {
6
+ get signType(): SignerSignType {
7
+ return SignerSignType.Unknown;
8
+ }
9
+
10
+ constructor(
11
+ client: Client,
12
+ public readonly type: SignerType,
13
+ ) {
14
+ super(client);
15
+ }
16
+
17
+ async isConnected(): Promise<boolean> {
18
+ return false;
19
+ }
20
+
21
+ async getInternalAddress(): Promise<string> {
22
+ throw new Error("Can't get address from SignerDummy");
23
+ }
24
+
25
+ async getAddressObjs(): Promise<Address[]> {
26
+ throw new Error("Can't get addresses from SignerDummy");
27
+ }
28
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./alwaysError";
2
+ export * from "./dummy";
3
+ export * from "./openLink";
@@ -0,0 +1,21 @@
1
+ import { Client } from "../../client";
2
+ import { SignerType } from "../signer";
3
+ import { SignerDummy } from "./dummy";
4
+
5
+ export class SignerOpenLink extends SignerDummy {
6
+ constructor(
7
+ client: Client,
8
+ type: SignerType,
9
+ private readonly link: string,
10
+ ) {
11
+ super(client, type);
12
+ }
13
+
14
+ async replaceClient(client: Client): Promise<SignerOpenLink> {
15
+ return new SignerOpenLink(client, this.type, this.link);
16
+ }
17
+
18
+ async connect(): Promise<void> {
19
+ window.open(this.link, "_blank")?.focus();
20
+ }
21
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./signerEvm";
2
2
  export * from "./signerEvmAddressReadonly";
3
+ export * from "./verify";
@@ -4,13 +4,21 @@ import { Script, Transaction, TransactionLike, WitnessArgs } from "../../ckb";
4
4
  import { KnownScript } from "../../client";
5
5
  import { hexFrom } from "../../hex";
6
6
  import { numToBytes } from "../../num";
7
- import { Signer } from "../signer";
7
+ import { Signer, SignerSignType, SignerType } from "../signer";
8
8
 
9
9
  /**
10
10
  * An abstract class extending Signer for Ethereum Virtual Machine (EVM) based signing operations.
11
11
  * This class provides methods to get EVM account, internal address, and signing transactions.
12
12
  */
13
13
  export abstract class SignerEvm extends Signer {
14
+ get type(): SignerType {
15
+ return SignerType.EVM;
16
+ }
17
+
18
+ get signType(): SignerSignType {
19
+ return SignerSignType.EvmPersonal;
20
+ }
21
+
14
22
  /**
15
23
  * Gets the EVM account associated with the signer.
16
24
  *
@@ -113,7 +121,7 @@ export abstract class SignerEvm extends Signer {
113
121
  }
114
122
 
115
123
  const signature = bytesFrom(
116
- await this.signMessage(messageTransformer(info.message)),
124
+ await this.signMessageRaw(messageTransformer(info.message)),
117
125
  );
118
126
  if (signature[signature.length - 1] >= 27) {
119
127
  signature[signature.length - 1] -= 27;
@@ -0,0 +1,16 @@
1
+ import { verifyMessage } from "ethers";
2
+ import { BytesLike, bytesFrom } from "../../bytes";
3
+
4
+ export function verifyMessageEvmPersonal(
5
+ message: string | BytesLike,
6
+ signature: string,
7
+ address: string,
8
+ ): boolean {
9
+ return (
10
+ address.toLowerCase() ===
11
+ verifyMessage(
12
+ typeof message === "string" ? message : bytesFrom(message),
13
+ signature,
14
+ ).toLowerCase()
15
+ );
16
+ }
@@ -1,4 +1,5 @@
1
1
  export * from "./btc";
2
2
  export * from "./ckb";
3
+ export * from "./dummy";
3
4
  export * from "./evm";
4
5
  export * from "./signer";
@@ -1,9 +1,36 @@
1
- import { Address } from "../address";
2
- import { BytesLike } from "../bytes";
3
- import { Transaction, TransactionLike } from "../ckb";
4
- import { Client } from "../client";
5
- import { Hex } from "../hex";
6
- import { Num } from "../num";
1
+ import { Address } from "../../address";
2
+ import { BytesLike } from "../../bytes";
3
+ import { Transaction, TransactionLike } from "../../ckb";
4
+ import { Client } from "../../client";
5
+ import { Hex } from "../../hex";
6
+ import { Num } from "../../num";
7
+ import { verifyMessageBtcEcdsa } from "../btc";
8
+ import { verifyMessageJoyId } from "../ckb/verifyJoyId";
9
+ import { verifyMessageEvmPersonal } from "../evm/verify";
10
+
11
+ export enum SignerSignType {
12
+ Unknown = "Unknown",
13
+ BtcEcdsa = "BtcEcdsa",
14
+ EvmPersonal = "EvmPersonal",
15
+ JoyId = "JoyId",
16
+ }
17
+
18
+ /**
19
+ * An enumeration of signer display types in wallet.
20
+ */
21
+ export enum SignerType {
22
+ EVM = "EVM",
23
+ BTC = "BTC",
24
+ CKB = "CKB",
25
+ }
26
+
27
+ export class Signature {
28
+ constructor(
29
+ public signature: string,
30
+ public identity: string,
31
+ public signType: SignerSignType,
32
+ ) {}
33
+ }
7
34
 
8
35
  /**
9
36
  * An abstract class representing a generic signer.
@@ -12,10 +39,41 @@ import { Num } from "../num";
12
39
  export abstract class Signer {
13
40
  constructor(protected client_: Client) {}
14
41
 
42
+ abstract get type(): SignerType;
43
+ abstract get signType(): SignerSignType;
44
+
15
45
  get client(): Client {
16
46
  return this.client_;
17
47
  }
18
48
 
49
+ static async verifyMessage(
50
+ message: string | BytesLike,
51
+ signature: Signature,
52
+ ): Promise<boolean> {
53
+ switch (signature.signType) {
54
+ case SignerSignType.EvmPersonal:
55
+ return verifyMessageEvmPersonal(
56
+ message,
57
+ signature.signature,
58
+ signature.identity,
59
+ );
60
+ case SignerSignType.BtcEcdsa:
61
+ return verifyMessageBtcEcdsa(
62
+ message,
63
+ signature.signature,
64
+ signature.identity,
65
+ );
66
+ case SignerSignType.JoyId:
67
+ return verifyMessageJoyId(
68
+ message,
69
+ signature.signature,
70
+ signature.identity,
71
+ );
72
+ case SignerSignType.Unknown:
73
+ throw new Error("Unknown signer sign type");
74
+ }
75
+ }
76
+
19
77
  /**
20
78
  * Construct a new signer with the client replaced.
21
79
  *
@@ -51,6 +109,15 @@ export abstract class Signer {
51
109
  */
52
110
  abstract getInternalAddress(): Promise<string>;
53
111
 
112
+ /**
113
+ * Gets the identity for verifying signature, usually it's address
114
+ *
115
+ * @returns A promise that resolves to a string representing the identity
116
+ */
117
+ async getIdentity(): Promise<string> {
118
+ return this.getInternalAddress();
119
+ }
120
+
54
121
  /**
55
122
  * Gets an array of Address objects associated with the signer.
56
123
  *
@@ -101,14 +168,67 @@ export abstract class Signer {
101
168
  }
102
169
 
103
170
  /**
104
- * Signs a message. This method is not implemented and should be overridden by subclasses.
171
+ * Signs a message.
105
172
  *
106
173
  * @param _ - The message to sign, as a string or BytesLike object.
107
- * @returns A promise that resolves to the signed message as a string.
174
+ * @returns A promise that resolves to the signature info.
108
175
  * @throws Will throw an error if not implemented.
109
176
  */
110
- signMessage(_: string | BytesLike): Promise<string> {
111
- throw Error("Signer.signMessage not implemented");
177
+ async signMessage(message: string | BytesLike): Promise<Signature> {
178
+ return {
179
+ signature: await this.signMessageRaw(message),
180
+ identity: await this.getIdentity(),
181
+ signType: this.signType,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Signs a message and returns signature only. This method is not implemented and should be overridden by subclasses.
187
+ *
188
+ * @param _ - The message to sign, as a string or BytesLike object.
189
+ * @returns A promise that resolves to the signature as a string.
190
+ * @throws Will throw an error if not implemented.
191
+ */
192
+ signMessageRaw(_: string | BytesLike): Promise<string> {
193
+ throw Error("Signer.signMessageRaw not implemented");
194
+ }
195
+
196
+ /**
197
+ * Verify a signature.
198
+ *
199
+ * @param _ - The original message.
200
+ * @param _ - The signature to verify.
201
+ * @returns A promise that resolves to the verification result.
202
+ * @throws Will throw an error if not implemented.
203
+ */
204
+ async verifyMessage(
205
+ message: string | BytesLike,
206
+ signature: string | Signature,
207
+ ): Promise<boolean> {
208
+ if (typeof signature === "string") {
209
+ return this.verifyMessageRaw(message, signature);
210
+ }
211
+
212
+ if (
213
+ signature.identity !== (await this.getIdentity()) ||
214
+ ![SignerSignType.Unknown, this.signType].includes(signature.signType)
215
+ ) {
216
+ return false;
217
+ }
218
+
219
+ return this.verifyMessageRaw(message, signature.signature);
220
+ }
221
+
222
+ /**
223
+ * Verify a string signature. This method is not implemented and should be overridden by subclasses.
224
+ *
225
+ * @param _0 - The original message.
226
+ * @param _1 - The signature to verify.
227
+ * @returns A promise that resolves to the verification result.
228
+ * @throws Will throw an error if not implemented.
229
+ */
230
+ verifyMessageRaw(_0: string | BytesLike, _1: string): Promise<boolean> {
231
+ throw Error("Signer.verifyMessageRaw not implemented");
112
232
  }
113
233
 
114
234
  /**
@@ -144,7 +264,7 @@ export abstract class Signer {
144
264
  }
145
265
 
146
266
  /**
147
- * Signs a transaction without sending it. This method is not implemented and should be overridden by subclasses.
267
+ * Signs a transaction without preparing information for it. This method is not implemented and should be overridden by subclasses.
148
268
  *
149
269
  * @param _ - The transaction to sign, represented as a TransactionLike object.
150
270
  * @returns A promise that resolves to the signed Transaction object.
@@ -155,22 +275,12 @@ export abstract class Signer {
155
275
  }
156
276
  }
157
277
 
158
- /**
159
- * An enumeration of signer types.
160
- */
161
- export enum SignerType {
162
- EVM = "EVM",
163
- BTC = "BTC",
164
- CKB = "CKB",
165
- }
166
-
167
278
  /**
168
279
  * A class representing information about a signer, including its type and the signer instance.
169
280
  */
170
281
  export class SignerInfo {
171
282
  constructor(
172
283
  public name: string,
173
- public type: SignerType,
174
284
  public signer: Signer,
175
285
  ) {}
176
286
  }
@@ -26,3 +26,7 @@ export function apply<T, R>(
26
26
 
27
27
  return transformer(value);
28
28
  }
29
+
30
+ export function isWebview(userAgent: string): boolean {
31
+ return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
32
+ }
@@ -1 +0,0 @@
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;AAC7B,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;IACG,UAAU,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;;;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