@ckb-ccc/eip6963 0.0.9-alpha.0 → 0.0.10-alpha.0

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/dist/signer.d.ts CHANGED
@@ -7,6 +7,7 @@ import { ProviderDetail as EIP6963ProviderDetail } from "./eip6963.advanced";
7
7
  */
8
8
  export declare class Signer extends ccc.SignerEvm {
9
9
  readonly detail: EIP6963ProviderDetail;
10
+ private accountCache?;
10
11
  /**
11
12
  * Creates an instance of Signer.
12
13
  * @param {ccc.Client} client - The client instance.
@@ -17,12 +18,13 @@ export declare class Signer extends ccc.SignerEvm {
17
18
  * Gets the EVM account address.
18
19
  * @returns A promise that resolves to the EVM account address.
19
20
  */
20
- getEvmAccount(): Promise<`0x${string}`>;
21
+ getEvmAccount(): Promise<ccc.Hex>;
21
22
  /**
22
23
  * Connects to the provider by requesting accounts.
23
24
  * @returns {Promise<void>} A promise that resolves when the connection is established.
24
25
  */
25
26
  connect(): Promise<void>;
27
+ onReplaced(listener: () => void): () => void;
26
28
  /**
27
29
  * Checks if the provider is connected.
28
30
  * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE7E;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,MAAM,EAAE,qBAAqB;IAP/C;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,MAAM,EAAE,qBAAqB;IAK/C;;;OAGG;IACG,aAAa;IAInB;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;CAUxE"}
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE7E;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAUrB,MAAM,EAAE,qBAAqB;IAT/C,OAAO,CAAC,YAAY,CAAC,CAAsB;IAE3C;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,MAAM,EAAE,qBAAqB;IAK/C;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAOvC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAiB5C;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;CAWxE"}
package/dist/signer.js CHANGED
@@ -13,13 +13,15 @@ export class Signer extends ccc.SignerEvm {
13
13
  constructor(client, detail) {
14
14
  super(client);
15
15
  this.detail = detail;
16
+ this.accountCache = undefined;
16
17
  }
17
18
  /**
18
19
  * Gets the EVM account address.
19
20
  * @returns A promise that resolves to the EVM account address.
20
21
  */
21
22
  async getEvmAccount() {
22
- return (await this.detail.provider.request({ method: "eth_accounts" }))[0];
23
+ this.accountCache = (await this.detail.provider.request({ method: "eth_accounts" }))[0];
24
+ return this.accountCache;
23
25
  }
24
26
  /**
25
27
  * Connects to the provider by requesting accounts.
@@ -28,6 +30,20 @@ export class Signer extends ccc.SignerEvm {
28
30
  async connect() {
29
31
  await this.detail.provider.request({ method: "eth_requestAccounts" });
30
32
  }
33
+ onReplaced(listener) {
34
+ const stop = [];
35
+ const replacer = async () => {
36
+ listener();
37
+ stop[0]?.();
38
+ };
39
+ stop.push(() => {
40
+ this.detail.provider.removeListener("accountsChanged", replacer);
41
+ this.detail.provider.removeListener("disconnect", replacer);
42
+ });
43
+ this.detail.provider.on("accountsChanged", replacer);
44
+ this.detail.provider.on("disconnect", replacer);
45
+ return stop[0];
46
+ }
31
47
  /**
32
48
  * Checks if the provider is connected.
33
49
  * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
@@ -43,10 +59,10 @@ export class Signer extends ccc.SignerEvm {
43
59
  */
44
60
  async signMessageRaw(message) {
45
61
  const challenge = typeof message === "string" ? ccc.bytesFrom(message, "utf8") : message;
46
- const address = await this.getEvmAccount();
62
+ const account = this.accountCache ?? (await this.getEvmAccount());
47
63
  return this.detail.provider.request({
48
64
  method: "personal_sign",
49
- params: [ccc.hexFrom(challenge), address],
65
+ params: [ccc.hexFrom(challenge), account],
50
66
  });
51
67
  }
52
68
  }
@@ -7,6 +7,7 @@ import { ProviderDetail as EIP6963ProviderDetail } from "./eip6963.advanced";
7
7
  */
8
8
  export declare class Signer extends ccc.SignerEvm {
9
9
  readonly detail: EIP6963ProviderDetail;
10
+ private accountCache?;
10
11
  /**
11
12
  * Creates an instance of Signer.
12
13
  * @param {ccc.Client} client - The client instance.
@@ -17,12 +18,13 @@ export declare class Signer extends ccc.SignerEvm {
17
18
  * Gets the EVM account address.
18
19
  * @returns A promise that resolves to the EVM account address.
19
20
  */
20
- getEvmAccount(): Promise<`0x${string}`>;
21
+ getEvmAccount(): Promise<ccc.Hex>;
21
22
  /**
22
23
  * Connects to the provider by requesting accounts.
23
24
  * @returns {Promise<void>} A promise that resolves when the connection is established.
24
25
  */
25
26
  connect(): Promise<void>;
27
+ onReplaced(listener: () => void): () => void;
26
28
  /**
27
29
  * Checks if the provider is connected.
28
30
  * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE7E;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAQrB,MAAM,EAAE,qBAAqB;IAP/C;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,MAAM,EAAE,qBAAqB;IAK/C;;;OAGG;IACG,aAAa;IAInB;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;CAUxE"}
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE7E;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,GAAG,CAAC,SAAS;aAUrB,MAAM,EAAE,qBAAqB;IAT/C,OAAO,CAAC,YAAY,CAAC,CAAsB;IAE3C;;;;OAIG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,MAAM,EAAE,qBAAqB;IAK/C;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAOvC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAiB5C;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAOrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;CAWxE"}
@@ -16,13 +16,15 @@ class Signer extends core_1.ccc.SignerEvm {
16
16
  constructor(client, detail) {
17
17
  super(client);
18
18
  this.detail = detail;
19
+ this.accountCache = undefined;
19
20
  }
20
21
  /**
21
22
  * Gets the EVM account address.
22
23
  * @returns A promise that resolves to the EVM account address.
23
24
  */
24
25
  async getEvmAccount() {
25
- return (await this.detail.provider.request({ method: "eth_accounts" }))[0];
26
+ this.accountCache = (await this.detail.provider.request({ method: "eth_accounts" }))[0];
27
+ return this.accountCache;
26
28
  }
27
29
  /**
28
30
  * Connects to the provider by requesting accounts.
@@ -31,6 +33,20 @@ class Signer extends core_1.ccc.SignerEvm {
31
33
  async connect() {
32
34
  await this.detail.provider.request({ method: "eth_requestAccounts" });
33
35
  }
36
+ onReplaced(listener) {
37
+ const stop = [];
38
+ const replacer = async () => {
39
+ listener();
40
+ stop[0]?.();
41
+ };
42
+ stop.push(() => {
43
+ this.detail.provider.removeListener("accountsChanged", replacer);
44
+ this.detail.provider.removeListener("disconnect", replacer);
45
+ });
46
+ this.detail.provider.on("accountsChanged", replacer);
47
+ this.detail.provider.on("disconnect", replacer);
48
+ return stop[0];
49
+ }
34
50
  /**
35
51
  * Checks if the provider is connected.
36
52
  * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
@@ -46,10 +62,10 @@ class Signer extends core_1.ccc.SignerEvm {
46
62
  */
47
63
  async signMessageRaw(message) {
48
64
  const challenge = typeof message === "string" ? core_1.ccc.bytesFrom(message, "utf8") : message;
49
- const address = await this.getEvmAccount();
65
+ const account = this.accountCache ?? (await this.getEvmAccount());
50
66
  return this.detail.provider.request({
51
67
  method: "personal_sign",
52
- params: [core_1.ccc.hexFrom(challenge), address],
68
+ params: [core_1.ccc.hexFrom(challenge), account],
53
69
  });
54
70
  }
55
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/eip6963",
3
- "version": "0.0.9-alpha.0",
3
+ "version": "0.0.10-alpha.0",
4
4
  "description": "CCC - CKBer's Codebase. Common Chains Connector's support for EIP6963",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  "access": "public"
43
43
  },
44
44
  "dependencies": {
45
- "@ckb-ccc/core": "0.0.9-alpha.0"
45
+ "@ckb-ccc/core": "0.0.10-alpha.0"
46
46
  },
47
- "gitHead": "ac4435006da9c9e48b3b66fc329e6f539156b252"
47
+ "gitHead": "8a4c789419f5e12e1f230cead213c59373834a1a"
48
48
  }
package/src/signer.ts CHANGED
@@ -7,6 +7,8 @@ import { ProviderDetail as EIP6963ProviderDetail } from "./eip6963.advanced";
7
7
  * @extends {ccc.SignerEvm}
8
8
  */
9
9
  export class Signer extends ccc.SignerEvm {
10
+ private accountCache?: ccc.Hex = undefined;
11
+
10
12
  /**
11
13
  * Creates an instance of Signer.
12
14
  * @param {ccc.Client} client - The client instance.
@@ -23,8 +25,11 @@ export class Signer extends ccc.SignerEvm {
23
25
  * Gets the EVM account address.
24
26
  * @returns A promise that resolves to the EVM account address.
25
27
  */
26
- async getEvmAccount() {
27
- return (await this.detail.provider.request({ method: "eth_accounts" }))[0];
28
+ async getEvmAccount(): Promise<ccc.Hex> {
29
+ this.accountCache = (
30
+ await this.detail.provider.request({ method: "eth_accounts" })
31
+ )[0];
32
+ return this.accountCache;
28
33
  }
29
34
 
30
35
  /**
@@ -35,6 +40,23 @@ export class Signer extends ccc.SignerEvm {
35
40
  await this.detail.provider.request({ method: "eth_requestAccounts" });
36
41
  }
37
42
 
43
+ onReplaced(listener: () => void): () => void {
44
+ const stop: (() => void)[] = [];
45
+ const replacer = async () => {
46
+ listener();
47
+ stop[0]?.();
48
+ };
49
+ stop.push(() => {
50
+ this.detail.provider.removeListener("accountsChanged", replacer);
51
+ this.detail.provider.removeListener("disconnect", replacer);
52
+ });
53
+
54
+ this.detail.provider.on("accountsChanged", replacer);
55
+ this.detail.provider.on("disconnect", replacer);
56
+
57
+ return stop[0];
58
+ }
59
+
38
60
  /**
39
61
  * Checks if the provider is connected.
40
62
  * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
@@ -54,11 +76,12 @@ export class Signer extends ccc.SignerEvm {
54
76
  async signMessageRaw(message: string | ccc.BytesLike): Promise<ccc.Hex> {
55
77
  const challenge =
56
78
  typeof message === "string" ? ccc.bytesFrom(message, "utf8") : message;
57
- const address = await this.getEvmAccount();
79
+
80
+ const account = this.accountCache ?? (await this.getEvmAccount());
58
81
 
59
82
  return this.detail.provider.request({
60
83
  method: "personal_sign",
61
- params: [ccc.hexFrom(challenge), address],
84
+ params: [ccc.hexFrom(challenge), account],
62
85
  });
63
86
  }
64
87
  }