@ckb-ccc/joy-id 0.0.7-alpha.0 → 0.0.7-alpha.2

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.
@@ -12,57 +12,52 @@ const common_1 = require("@joyid/common");
12
12
  * @throws {PopupTimeoutError} If the popup operation times out.
13
13
  */
14
14
  async function createPopup(url, config) {
15
- if (config.popup == null) {
16
- config.popup = (0, common_1.openPopup)("");
17
15
  if (config.popup == null) {
18
- return (0, common_1.createBlockDialog)(async () =>
19
- createPopup(url, config),
20
- );
16
+ config.popup = (0, common_1.openPopup)("");
17
+ if (config.popup == null) {
18
+ return (0, common_1.createBlockDialog)(async () => createPopup(url, config));
19
+ }
21
20
  }
22
- }
23
- config.popup.location.href = url;
24
- return new Promise((resolve, reject) => {
25
- if ((0, common_1.isStandaloneBrowser)()) {
26
- reject(new common_1.PopupNotSupportedError(config.popup));
27
- }
28
- let popupEventListener;
29
- let timeoutId;
30
- // Check each second if the popup is closed triggering a PopupCancelledError
31
- const popupTimer = setInterval(() => {
32
- if (config.popup?.closed) {
33
- clearInterval(popupTimer);
34
- clearTimeout(timeoutId);
35
- window.removeEventListener("message", popupEventListener, false);
36
- reject(new common_1.PopupCancelledError(config.popup));
37
- }
38
- }, 1000);
39
- timeoutId = setTimeout(
40
- () => {
41
- clearInterval(popupTimer);
42
- reject(new common_1.PopupTimeoutError(config.popup));
43
- window.removeEventListener("message", popupEventListener, false);
44
- },
45
- (config.timeoutInSeconds ?? 3000) * 1000,
46
- );
47
- popupEventListener = (e) => {
48
- const { joyidAppURL } = config;
49
- const appURL = new URL(joyidAppURL);
50
- if (e.origin !== appURL.origin) {
51
- return;
52
- }
53
- if (!e.data || e.data?.type !== config.type) {
54
- return;
55
- }
56
- clearTimeout(timeoutId);
57
- clearInterval(popupTimer);
58
- window.removeEventListener("message", popupEventListener, false);
59
- config.popup.close();
60
- if (e.data.error) {
61
- reject(new Error(e.data.error));
62
- }
63
- resolve(e.data.data);
64
- };
65
- window.addEventListener("message", popupEventListener);
66
- });
21
+ config.popup.location.href = url;
22
+ return new Promise((resolve, reject) => {
23
+ if ((0, common_1.isStandaloneBrowser)()) {
24
+ reject(new common_1.PopupNotSupportedError(config.popup));
25
+ }
26
+ let popupEventListener;
27
+ let timeoutId;
28
+ // Check each second if the popup is closed triggering a PopupCancelledError
29
+ const popupTimer = setInterval(() => {
30
+ if (config.popup?.closed) {
31
+ clearInterval(popupTimer);
32
+ clearTimeout(timeoutId);
33
+ window.removeEventListener("message", popupEventListener, false);
34
+ reject(new common_1.PopupCancelledError(config.popup));
35
+ }
36
+ }, 1000);
37
+ timeoutId = setTimeout(() => {
38
+ clearInterval(popupTimer);
39
+ reject(new common_1.PopupTimeoutError(config.popup));
40
+ window.removeEventListener("message", popupEventListener, false);
41
+ }, (config.timeoutInSeconds ?? 3000) * 1000);
42
+ popupEventListener = (e) => {
43
+ const { joyidAppURL } = config;
44
+ const appURL = new URL(joyidAppURL);
45
+ if (e.origin !== appURL.origin) {
46
+ return;
47
+ }
48
+ if (!e.data || e.data?.type !== config.type) {
49
+ return;
50
+ }
51
+ clearTimeout(timeoutId);
52
+ clearInterval(popupTimer);
53
+ window.removeEventListener("message", popupEventListener, false);
54
+ config.popup.close();
55
+ if (e.data.error) {
56
+ reject(new Error(e.data.error));
57
+ }
58
+ resolve(e.data.data);
59
+ };
60
+ window.addEventListener("message", popupEventListener);
61
+ });
67
62
  }
68
63
  exports.createPopup = createPopup;
@@ -6,8 +6,8 @@ import { ccc } from "@ckb-ccc/core";
6
6
  * @property {string} addressType - The address type of the account.
7
7
  */
8
8
  export type AccountSelector = {
9
- uri: string;
10
- addressType: string;
9
+ uri: string;
10
+ addressType: string;
11
11
  };
12
12
  /**
13
13
  * Checks if two AccountSelectors are equal.
@@ -15,10 +15,7 @@ export type AccountSelector = {
15
15
  * @param {AccountSelector} b - The second account selector.
16
16
  * @returns {boolean} True if the selectors are equal, false otherwise.
17
17
  */
18
- export declare function isSelectorEq(
19
- a: AccountSelector,
20
- b: AccountSelector,
21
- ): boolean;
18
+ export declare function isSelectorEq(a: AccountSelector, b: AccountSelector): boolean;
22
19
  /**
23
20
  * Type representing a connection with an address, public key, and key type.
24
21
  * @typedef {Object} Connection
@@ -27,31 +24,28 @@ export declare function isSelectorEq(
27
24
  * @property {string} keyType - The key type of the connection.
28
25
  */
29
26
  export type Connection = {
30
- readonly address: string;
31
- readonly publicKey: ccc.Hex;
32
- readonly keyType: string;
27
+ readonly address: string;
28
+ readonly publicKey: ccc.Hex;
29
+ readonly keyType: string;
33
30
  };
34
31
  /**
35
32
  * Interface representing a repository for managing connections.
36
33
  * @interface
37
34
  */
38
35
  export interface ConnectionsRepo {
39
- /**
40
- * Gets a connection for the given selector.
41
- * @param {AccountSelector} selector - The account selector.
42
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
43
- */
44
- get(selector: AccountSelector): Promise<Connection | undefined>;
45
- /**
46
- * Sets a connection for the given selector.
47
- * @param {AccountSelector} selector - The account selector.
48
- * @param {Connection | undefined} connection - The connection to set.
49
- * @returns {Promise<void>} A promise that resolves when the connection is set.
50
- */
51
- set(
52
- selector: AccountSelector,
53
- connection: Connection | undefined,
54
- ): Promise<void>;
36
+ /**
37
+ * Gets a connection for the given selector.
38
+ * @param {AccountSelector} selector - The account selector.
39
+ * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
40
+ */
41
+ get(selector: AccountSelector): Promise<Connection | undefined>;
42
+ /**
43
+ * Sets a connection for the given selector.
44
+ * @param {AccountSelector} selector - The account selector.
45
+ * @param {Connection | undefined} connection - The connection to set.
46
+ * @returns {Promise<void>} A promise that resolves when the connection is set.
47
+ */
48
+ set(selector: AccountSelector, connection: Connection | undefined): Promise<void>;
55
49
  }
56
50
  /**
57
51
  * Class representing a local storage-based repository for managing connections.
@@ -59,32 +53,29 @@ export interface ConnectionsRepo {
59
53
  * @implements {ConnectionsRepo}
60
54
  */
61
55
  export declare class ConnectionsRepoLocalStorage implements ConnectionsRepo {
62
- private readonly storageKey;
63
- /**
64
- * Creates an instance of ConnectionsRepoLocalStorage.
65
- * @param {string} [storageKey="ccc-joy-id-signer"] - The local storage key.
66
- */
67
- constructor(storageKey?: string);
68
- /**
69
- * Reads all connections from local storage.
70
- * @returns {Promise<[AccountSelector, Connection][]>} A promise that resolves to an array of selectors and connections.
71
- */
72
- readConnections(): Promise<[AccountSelector, Connection][]>;
73
- /**
74
- * Gets a connection for the given selector.
75
- * @param {AccountSelector} selector - The account selector.
76
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
77
- */
78
- get(selector: AccountSelector): Promise<Connection | undefined>;
79
- /**
80
- * Sets a connection for the given selector.
81
- * @param {AccountSelector} selector - The account selector.
82
- * @param {Connection | undefined} connection - The connection to set.
83
- * @returns {Promise<void>}
84
- */
85
- set(
86
- selector: AccountSelector,
87
- connection: Connection | undefined,
88
- ): Promise<void>;
56
+ private readonly storageKey;
57
+ /**
58
+ * Creates an instance of ConnectionsRepoLocalStorage.
59
+ * @param {string} [storageKey="ccc-joy-id-signer"] - The local storage key.
60
+ */
61
+ constructor(storageKey?: string);
62
+ /**
63
+ * Reads all connections from local storage.
64
+ * @returns {Promise<[AccountSelector, Connection][]>} A promise that resolves to an array of selectors and connections.
65
+ */
66
+ readConnections(): Promise<[AccountSelector, Connection][]>;
67
+ /**
68
+ * Gets a connection for the given selector.
69
+ * @param {AccountSelector} selector - The account selector.
70
+ * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
71
+ */
72
+ get(selector: AccountSelector): Promise<Connection | undefined>;
73
+ /**
74
+ * Sets a connection for the given selector.
75
+ * @param {AccountSelector} selector - The account selector.
76
+ * @param {Connection | undefined} connection - The connection to set.
77
+ * @returns {Promise<void>}
78
+ */
79
+ set(selector: AccountSelector, connection: Connection | undefined): Promise<void>;
89
80
  }
90
- //# sourceMappingURL=index.d.ts.map
81
+ //# sourceMappingURL=index.d.ts.map
@@ -8,7 +8,7 @@ exports.ConnectionsRepoLocalStorage = exports.isSelectorEq = void 0;
8
8
  * @returns {boolean} True if the selectors are equal, false otherwise.
9
9
  */
10
10
  function isSelectorEq(a, b) {
11
- return a.uri === b.uri && a.addressType === b.addressType;
11
+ return a.uri === b.uri && a.addressType === b.addressType;
12
12
  }
13
13
  exports.isSelectorEq = isSelectorEq;
14
14
  /**
@@ -17,52 +17,49 @@ exports.isSelectorEq = isSelectorEq;
17
17
  * @implements {ConnectionsRepo}
18
18
  */
19
19
  class ConnectionsRepoLocalStorage {
20
- /**
21
- * Creates an instance of ConnectionsRepoLocalStorage.
22
- * @param {string} [storageKey="ccc-joy-id-signer"] - The local storage key.
23
- */
24
- constructor(storageKey = "ccc-joy-id-signer") {
25
- this.storageKey = storageKey;
26
- }
27
- /**
28
- * Reads all connections from local storage.
29
- * @returns {Promise<[AccountSelector, Connection][]>} A promise that resolves to an array of selectors and connections.
30
- */
31
- async readConnections() {
32
- return JSON.parse(window.localStorage.getItem(this.storageKey) ?? "[]");
33
- }
34
- /**
35
- * Gets a connection for the given selector.
36
- * @param {AccountSelector} selector - The account selector.
37
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
38
- */
39
- async get(selector) {
40
- return (await this.readConnections()).find(([s]) =>
41
- isSelectorEq(selector, s),
42
- )?.[1];
43
- }
44
- /**
45
- * Sets a connection for the given selector.
46
- * @param {AccountSelector} selector - The account selector.
47
- * @param {Connection | undefined} connection - The connection to set.
48
- * @returns {Promise<void>}
49
- */
50
- async set(selector, connection) {
51
- const connections = await this.readConnections();
52
- if (connection) {
53
- const existed = connections.find(([s]) => isSelectorEq(s, selector));
54
- if (existed) {
55
- existed[1] = connection;
56
- } else {
57
- connections.push([selector, connection]);
58
- }
59
- window.localStorage.setItem(this.storageKey, JSON.stringify(connections));
60
- } else {
61
- window.localStorage.setItem(
62
- this.storageKey,
63
- JSON.stringify(connections.filter(([s]) => !isSelectorEq(s, selector))),
64
- );
20
+ /**
21
+ * Creates an instance of ConnectionsRepoLocalStorage.
22
+ * @param {string} [storageKey="ccc-joy-id-signer"] - The local storage key.
23
+ */
24
+ constructor(storageKey = "ccc-joy-id-signer") {
25
+ this.storageKey = storageKey;
26
+ }
27
+ /**
28
+ * Reads all connections from local storage.
29
+ * @returns {Promise<[AccountSelector, Connection][]>} A promise that resolves to an array of selectors and connections.
30
+ */
31
+ async readConnections() {
32
+ return JSON.parse(window.localStorage.getItem(this.storageKey) ?? "[]");
33
+ }
34
+ /**
35
+ * Gets a connection for the given selector.
36
+ * @param {AccountSelector} selector - The account selector.
37
+ * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
38
+ */
39
+ async get(selector) {
40
+ return (await this.readConnections()).find(([s]) => isSelectorEq(selector, s))?.[1];
41
+ }
42
+ /**
43
+ * Sets a connection for the given selector.
44
+ * @param {AccountSelector} selector - The account selector.
45
+ * @param {Connection | undefined} connection - The connection to set.
46
+ * @returns {Promise<void>}
47
+ */
48
+ async set(selector, connection) {
49
+ const connections = await this.readConnections();
50
+ if (connection) {
51
+ const existed = connections.find(([s]) => isSelectorEq(s, selector));
52
+ if (existed) {
53
+ existed[1] = connection;
54
+ }
55
+ else {
56
+ connections.push([selector, connection]);
57
+ }
58
+ window.localStorage.setItem(this.storageKey, JSON.stringify(connections));
59
+ }
60
+ else {
61
+ window.localStorage.setItem(this.storageKey, JSON.stringify(connections.filter(([s]) => !isSelectorEq(s, selector))));
62
+ }
65
63
  }
66
- }
67
64
  }
68
65
  exports.ConnectionsRepoLocalStorage = ConnectionsRepoLocalStorage;
@@ -6,71 +6,65 @@ import { ConnectionsRepo } from "../connectionsStorage";
6
6
  * @extends {ccc.SignerEvm}
7
7
  */
8
8
  export declare class EvmSigner extends ccc.SignerEvm {
9
- private readonly name;
10
- private readonly icon;
11
- private readonly appUri;
12
- private readonly connectionsRepo;
13
- private connection?;
14
- /**
15
- * Ensures that the signer is connected and returns the connection.
16
- * @private
17
- * @throws Will throw an error if not connected.
18
- * @returns {Connection} The current connection.
19
- */
20
- private assertConnection;
21
- /**
22
- * Creates an instance of EvmSigner.
23
- * @param {ccc.Client} client - The client instance.
24
- * @param {string} name - The name of the signer.
25
- * @param {string} icon - The icon URL of the signer.
26
- * @param {string} [appUri="https://app.joy.id"] - The application URI.
27
- * @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
28
- */
29
- constructor(
30
- client: ccc.Client,
31
- name: string,
32
- icon: string,
33
- appUri?: string,
34
- connectionsRepo?: ConnectionsRepo,
35
- );
36
- /**
37
- * Gets the configuration for JoyID.
38
- * @private
39
- * @returns {object} The configuration object.
40
- */
41
- private getConfig;
42
- /**
43
- * Gets the EVM account address.
44
- * @returns {Promise<string>} A promise that resolves to the EVM account address.
45
- */
46
- getEvmAccount(): Promise<string>;
47
- /**
48
- * Connects to the provider by requesting authentication.
49
- * @returns {Promise<void>} A promise that resolves when the connection is established.
50
- */
51
- connect(): Promise<void>;
52
- /**
53
- * Checks if the signer is connected.
54
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
55
- */
56
- isConnected(): Promise<boolean>;
57
- /**
58
- * Signs a raw message with the EVM account.
59
- * @param {string | ccc.BytesLike} message - The message to sign.
60
- * @returns {Promise<ccc.Hex>} A promise that resolves to the signed message.
61
- */
62
- signMessageRaw(message: string | ccc.BytesLike): Promise<ccc.Hex>;
63
- /**
64
- * Saves the current connection.
65
- * @private
66
- * @returns {Promise<void>}
67
- */
68
- private saveConnection;
69
- /**
70
- * Restores the previous connection.
71
- * @private
72
- * @returns {Promise<void>}
73
- */
74
- private restoreConnection;
9
+ private readonly name;
10
+ private readonly icon;
11
+ private readonly appUri;
12
+ private readonly connectionsRepo;
13
+ private connection?;
14
+ /**
15
+ * Ensures that the signer is connected and returns the connection.
16
+ * @private
17
+ * @throws Will throw an error if not connected.
18
+ * @returns {Connection} The current connection.
19
+ */
20
+ private assertConnection;
21
+ /**
22
+ * Creates an instance of EvmSigner.
23
+ * @param {ccc.Client} client - The client instance.
24
+ * @param {string} name - The name of the signer.
25
+ * @param {string} icon - The icon URL of the signer.
26
+ * @param {string} [appUri="https://app.joy.id"] - The application URI.
27
+ * @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
28
+ */
29
+ constructor(client: ccc.Client, name: string, icon: string, appUri?: string, connectionsRepo?: ConnectionsRepo);
30
+ /**
31
+ * Gets the configuration for JoyID.
32
+ * @private
33
+ * @returns {object} The configuration object.
34
+ */
35
+ private getConfig;
36
+ /**
37
+ * Gets the EVM account address.
38
+ * @returns {Promise<string>} A promise that resolves to the EVM account address.
39
+ */
40
+ getEvmAccount(): Promise<string>;
41
+ /**
42
+ * Connects to the provider by requesting authentication.
43
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
44
+ */
45
+ connect(): Promise<void>;
46
+ /**
47
+ * Checks if the signer is connected.
48
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
49
+ */
50
+ isConnected(): Promise<boolean>;
51
+ /**
52
+ * Signs a raw message with the EVM account.
53
+ * @param {string | ccc.BytesLike} message - The message to sign.
54
+ * @returns {Promise<ccc.Hex>} A promise that resolves to the signed message.
55
+ */
56
+ signMessageRaw(message: string | ccc.BytesLike): Promise<ccc.Hex>;
57
+ /**
58
+ * Saves the current connection.
59
+ * @private
60
+ * @returns {Promise<void>}
61
+ */
62
+ private saveConnection;
63
+ /**
64
+ * Restores the previous connection.
65
+ * @private
66
+ * @returns {Promise<void>}
67
+ */
68
+ private restoreConnection;
75
69
  }
76
- //# sourceMappingURL=index.d.ts.map
70
+ //# sourceMappingURL=index.d.ts.map