@ckb-ccc/joy-id 0.0.8-alpha.2 → 0.0.8-alpha.3

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.
@@ -11,120 +11,142 @@ const connectionsStorage_1 = require("../connectionsStorage");
11
11
  * @extends {ccc.SignerEvm}
12
12
  */
13
13
  class EvmSigner extends core_1.ccc.SignerEvm {
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
- assertConnection() {
21
- if (!this.isConnected() || !this.connection) {
22
- throw new Error("Not connected");
23
- }
24
- return this.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
+ assertConnection() {
21
+ if (!this.isConnected() || !this.connection) {
22
+ throw new Error("Not connected");
25
23
  }
26
- /**
27
- * Creates an instance of EvmSigner.
28
- * @param {ccc.Client} client - The client instance.
29
- * @param {string} name - The name of the signer.
30
- * @param {string} icon - The icon URL of the signer.
31
- * @param {string} [appUri="https://app.joy.id"] - The application URI.
32
- * @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
33
- */
34
- constructor(client, name, icon, appUri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
35
- super(client);
36
- this.name = name;
37
- this.icon = icon;
38
- this.appUri = appUri;
39
- this.connectionsRepo = connectionsRepo;
40
- }
41
- /**
42
- * Gets the configuration for JoyID.
43
- * @private
44
- * @returns {object} The configuration object.
45
- */
46
- getConfig() {
47
- return {
48
- redirectURL: location.href,
49
- joyidAppURL: this.appUri,
50
- requestNetwork: `ethereum`,
51
- name: this.name,
52
- logo: this.icon,
53
- };
54
- }
55
- /**
56
- * Gets the EVM account address.
57
- * @returns {Promise<string>} A promise that resolves to the EVM account address.
58
- */
59
- async getEvmAccount() {
60
- return this.assertConnection().address;
61
- }
62
- /**
63
- * Connects to the provider by requesting authentication.
64
- * @returns {Promise<void>} A promise that resolves when the connection is established.
65
- */
66
- async connect() {
67
- const config = this.getConfig();
68
- const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
69
- ...config,
70
- type: common_1.DappRequestType.Auth,
71
- });
72
- this.connection = {
73
- address: res.ethAddress,
74
- publicKey: core_1.ccc.hexFrom(res.pubkey),
75
- keyType: res.keyType,
76
- };
77
- await this.saveConnection();
78
- }
79
- /**
80
- * Checks if the signer is connected.
81
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
82
- */
83
- async isConnected() {
84
- if (this.connection) {
85
- return true;
86
- }
87
- await this.restoreConnection();
88
- return this.connection !== undefined;
89
- }
90
- /**
91
- * Signs a raw message with the EVM account.
92
- * @param {string | ccc.BytesLike} message - The message to sign.
93
- * @returns {Promise<ccc.Hex>} A promise that resolves to the signed message.
94
- */
95
- async signMessageRaw(message) {
96
- const { address } = this.assertConnection();
97
- const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
98
- const config = this.getConfig();
99
- const { signature } = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
100
- ...config,
101
- challenge,
102
- isData: typeof message !== "string",
103
- address,
104
- }, "popup", "/sign-message"), { ...config, type: common_1.DappRequestType.SignMessage });
105
- return core_1.ccc.hexFrom(signature);
106
- }
107
- /**
108
- * Saves the current connection.
109
- * @private
110
- * @returns {Promise<void>}
111
- */
112
- async saveConnection() {
113
- return this.connectionsRepo.set({
114
- uri: this.getConfig().joyidAppURL,
115
- addressType: "ethereum",
116
- }, this.connection);
117
- }
118
- /**
119
- * Restores the previous connection.
120
- * @private
121
- * @returns {Promise<void>}
122
- */
123
- async restoreConnection() {
124
- this.connection = await this.connectionsRepo.get({
125
- uri: this.getConfig().joyidAppURL,
126
- addressType: "ethereum",
127
- });
24
+ return this.connection;
25
+ }
26
+ /**
27
+ * Creates an instance of EvmSigner.
28
+ * @param {ccc.Client} client - The client instance.
29
+ * @param {string} name - The name of the signer.
30
+ * @param {string} icon - The icon URL of the signer.
31
+ * @param {string} [appUri="https://app.joy.id"] - The application URI.
32
+ * @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
33
+ */
34
+ constructor(
35
+ client,
36
+ name,
37
+ icon,
38
+ appUri = "https://app.joy.id",
39
+ connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage(),
40
+ ) {
41
+ super(client);
42
+ this.name = name;
43
+ this.icon = icon;
44
+ this.appUri = appUri;
45
+ this.connectionsRepo = connectionsRepo;
46
+ }
47
+ /**
48
+ * Gets the configuration for JoyID.
49
+ * @private
50
+ * @returns {object} The configuration object.
51
+ */
52
+ getConfig() {
53
+ return {
54
+ redirectURL: location.href,
55
+ joyidAppURL: this.appUri,
56
+ requestNetwork: `ethereum`,
57
+ name: this.name,
58
+ logo: this.icon,
59
+ };
60
+ }
61
+ /**
62
+ * Gets the EVM account address.
63
+ * @returns {Promise<string>} A promise that resolves to the EVM account address.
64
+ */
65
+ async getEvmAccount() {
66
+ return this.assertConnection().address;
67
+ }
68
+ /**
69
+ * Connects to the provider by requesting authentication.
70
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
71
+ */
72
+ async connect() {
73
+ const config = this.getConfig();
74
+ const res = await (0, common_2.createPopup)(
75
+ (0, common_1.buildJoyIDURL)(config, "popup", "/auth"),
76
+ {
77
+ ...config,
78
+ type: common_1.DappRequestType.Auth,
79
+ },
80
+ );
81
+ this.connection = {
82
+ address: res.ethAddress,
83
+ publicKey: core_1.ccc.hexFrom(res.pubkey),
84
+ keyType: res.keyType,
85
+ };
86
+ await this.saveConnection();
87
+ }
88
+ /**
89
+ * Checks if the signer is connected.
90
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
91
+ */
92
+ async isConnected() {
93
+ if (this.connection) {
94
+ return true;
128
95
  }
96
+ await this.restoreConnection();
97
+ return this.connection !== undefined;
98
+ }
99
+ /**
100
+ * Signs a raw message with the EVM account.
101
+ * @param {string | ccc.BytesLike} message - The message to sign.
102
+ * @returns {Promise<ccc.Hex>} A promise that resolves to the signed message.
103
+ */
104
+ async signMessageRaw(message) {
105
+ const { address } = this.assertConnection();
106
+ const challenge =
107
+ typeof message === "string"
108
+ ? message
109
+ : core_1.ccc.hexFrom(message).slice(2);
110
+ const config = this.getConfig();
111
+ const { signature } = await (0, common_2.createPopup)(
112
+ (0, common_1.buildJoyIDURL)(
113
+ {
114
+ ...config,
115
+ challenge,
116
+ isData: typeof message !== "string",
117
+ address,
118
+ },
119
+ "popup",
120
+ "/sign-message",
121
+ ),
122
+ { ...config, type: common_1.DappRequestType.SignMessage },
123
+ );
124
+ return core_1.ccc.hexFrom(signature);
125
+ }
126
+ /**
127
+ * Saves the current connection.
128
+ * @private
129
+ * @returns {Promise<void>}
130
+ */
131
+ async saveConnection() {
132
+ return this.connectionsRepo.set(
133
+ {
134
+ uri: this.getConfig().joyidAppURL,
135
+ addressType: "ethereum",
136
+ },
137
+ this.connection,
138
+ );
139
+ }
140
+ /**
141
+ * Restores the previous connection.
142
+ * @private
143
+ * @returns {Promise<void>}
144
+ */
145
+ async restoreConnection() {
146
+ this.connection = await this.connectionsRepo.get({
147
+ uri: this.getConfig().joyidAppURL,
148
+ addressType: "ethereum",
149
+ });
150
+ }
129
151
  }
130
152
  exports.EvmSigner = EvmSigner;
@@ -1,2 +1,2 @@
1
1
  export * as JoyId from "./barrel";
2
- //# sourceMappingURL=index.d.ts.map
2
+ //# sourceMappingURL=index.d.ts.map
@@ -1,27 +1,48 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (
9
+ !desc ||
10
+ ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
11
+ ) {
12
+ desc = {
13
+ enumerable: true,
14
+ get: function () {
15
+ return m[k];
16
+ },
17
+ };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }
21
+ : function (o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ });
25
+ var __setModuleDefault =
26
+ (this && this.__setModuleDefault) ||
27
+ (Object.create
28
+ ? function (o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }
31
+ : function (o, v) {
32
+ o["default"] = v;
33
+ });
34
+ var __importStar =
35
+ (this && this.__importStar) ||
36
+ function (mod) {
19
37
  if (mod && mod.__esModule) return mod;
20
38
  var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
39
+ if (mod != null)
40
+ for (var k in mod)
41
+ if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
42
+ __createBinding(result, mod, k);
22
43
  __setModuleDefault(result, mod);
23
44
  return result;
24
- };
45
+ };
25
46
  Object.defineProperty(exports, "__esModule", { value: true });
26
47
  exports.JoyId = void 0;
27
48
  exports.JoyId = __importStar(require("./barrel"));
@@ -6,46 +6,52 @@ import { ConnectionsRepo } from "../connectionsStorage";
6
6
  * @extends {ccc.SignerNostr}
7
7
  */
8
8
  export declare class NostrSigner extends ccc.SignerNostr {
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 NostrSigner.
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
- static isValidClient(client: ccc.Client): boolean;
31
- replaceClient(client: ccc.Client): Promise<boolean>;
32
- /**
33
- * Gets the configuration for JoyID.
34
- * @private
35
- * @returns {object} The configuration object.
36
- */
37
- private getConfig;
38
- /**
39
- * Connects to the provider by requesting authentication.
40
- * @returns {Promise<void>} A promise that resolves when the connection is established.
41
- */
42
- connect(): Promise<void>;
43
- /**
44
- * Checks if the signer is connected.
45
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
46
- */
47
- isConnected(): Promise<boolean>;
48
- getNostrPublicKey(): Promise<ccc.Hex>;
49
- signNostrEvent(event: ccc.NostrEvent): Promise<Required<ccc.NostrEvent>>;
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 NostrSigner.
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
+ static isValidClient(client: ccc.Client): boolean;
37
+ replaceClient(client: ccc.Client): Promise<boolean>;
38
+ /**
39
+ * Gets the configuration for JoyID.
40
+ * @private
41
+ * @returns {object} The configuration object.
42
+ */
43
+ private getConfig;
44
+ /**
45
+ * Connects to the provider by requesting authentication.
46
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
47
+ */
48
+ connect(): Promise<void>;
49
+ /**
50
+ * Checks if the signer is connected.
51
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
52
+ */
53
+ isConnected(): Promise<boolean>;
54
+ getNostrPublicKey(): Promise<ccc.Hex>;
55
+ signNostrEvent(event: ccc.NostrEvent): Promise<Required<ccc.NostrEvent>>;
50
56
  }
51
- //# sourceMappingURL=index.d.ts.map
57
+ //# sourceMappingURL=index.d.ts.map
@@ -11,97 +11,116 @@ const connectionsStorage_1 = require("../connectionsStorage");
11
11
  * @extends {ccc.SignerNostr}
12
12
  */
13
13
  class NostrSigner extends core_1.ccc.SignerNostr {
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
- assertConnection() {
21
- if (!this.isConnected() || !this.connection) {
22
- throw new Error("Not connected");
23
- }
24
- return this.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
+ assertConnection() {
21
+ if (!this.isConnected() || !this.connection) {
22
+ throw new Error("Not connected");
25
23
  }
26
- /**
27
- * Creates an instance of NostrSigner.
28
- * @param {ccc.Client} client - The client instance.
29
- * @param {string} name - The name of the signer.
30
- * @param {string} icon - The icon URL of the signer.
31
- * @param {string} [appUri="https://app.joy.id"] - The application URI.
32
- * @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
33
- */
34
- constructor(client, name, icon, appUri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
35
- super(client);
36
- this.name = name;
37
- this.icon = icon;
38
- this.appUri = appUri;
39
- this.connectionsRepo = connectionsRepo;
24
+ return this.connection;
25
+ }
26
+ /**
27
+ * Creates an instance of NostrSigner.
28
+ * @param {ccc.Client} client - The client instance.
29
+ * @param {string} name - The name of the signer.
30
+ * @param {string} icon - The icon URL of the signer.
31
+ * @param {string} [appUri="https://app.joy.id"] - The application URI.
32
+ * @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
33
+ */
34
+ constructor(
35
+ client,
36
+ name,
37
+ icon,
38
+ appUri = "https://app.joy.id",
39
+ connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage(),
40
+ ) {
41
+ super(client);
42
+ this.name = name;
43
+ this.icon = icon;
44
+ this.appUri = appUri;
45
+ this.connectionsRepo = connectionsRepo;
46
+ }
47
+ static isValidClient(client) {
48
+ return client.addressPrefix === "ckt";
49
+ }
50
+ async replaceClient(client) {
51
+ if (!NostrSigner.isValidClient(client)) {
52
+ return false;
40
53
  }
41
- static isValidClient(client) {
42
- return client.addressPrefix === "ckt";
43
- }
44
- async replaceClient(client) {
45
- if (!NostrSigner.isValidClient(client)) {
46
- return false;
47
- }
48
- return super.replaceClient(client);
49
- }
50
- /**
51
- * Gets the configuration for JoyID.
52
- * @private
53
- * @returns {object} The configuration object.
54
- */
55
- getConfig() {
56
- return {
57
- redirectURL: location.href,
58
- joyidAppURL: this.appUri,
59
- requestNetwork: "nostr",
60
- name: this.name,
61
- logo: this.icon,
62
- };
63
- }
64
- /**
65
- * Connects to the provider by requesting authentication.
66
- * @returns {Promise<void>} A promise that resolves when the connection is established.
67
- */
68
- async connect() {
69
- const config = this.getConfig();
70
- const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
71
- ...config,
72
- type: common_1.DappRequestType.Auth,
73
- });
74
- this.connection = {
75
- address: "",
76
- publicKey: core_1.ccc.hexFrom(res.nostrPubkey),
77
- keyType: res.keyType,
78
- };
79
- await this.connectionsRepo.set({ uri: this.appUri, addressType: "nostr" }, this.connection);
80
- }
81
- /**
82
- * Checks if the signer is connected.
83
- * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
84
- */
85
- async isConnected() {
86
- if (this.connection) {
87
- return true;
88
- }
89
- this.connection = await this.connectionsRepo.get({
90
- uri: this.appUri,
91
- addressType: "nostr",
92
- });
93
- return this.connection !== undefined;
94
- }
95
- async getNostrPublicKey() {
96
- return this.assertConnection().publicKey;
97
- }
98
- async signNostrEvent(event) {
99
- const config = this.getConfig();
100
- const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({ ...config, event }, "popup", "/sign-nostr-event"), {
101
- ...config,
102
- type: common_1.DappRequestType.SignNostrEvent,
103
- });
104
- return res.event;
54
+ return super.replaceClient(client);
55
+ }
56
+ /**
57
+ * Gets the configuration for JoyID.
58
+ * @private
59
+ * @returns {object} The configuration object.
60
+ */
61
+ getConfig() {
62
+ return {
63
+ redirectURL: location.href,
64
+ joyidAppURL: this.appUri,
65
+ requestNetwork: "nostr",
66
+ name: this.name,
67
+ logo: this.icon,
68
+ };
69
+ }
70
+ /**
71
+ * Connects to the provider by requesting authentication.
72
+ * @returns {Promise<void>} A promise that resolves when the connection is established.
73
+ */
74
+ async connect() {
75
+ const config = this.getConfig();
76
+ const res = await (0, common_2.createPopup)(
77
+ (0, common_1.buildJoyIDURL)(config, "popup", "/auth"),
78
+ {
79
+ ...config,
80
+ type: common_1.DappRequestType.Auth,
81
+ },
82
+ );
83
+ this.connection = {
84
+ address: "",
85
+ publicKey: core_1.ccc.hexFrom(res.nostrPubkey),
86
+ keyType: res.keyType,
87
+ };
88
+ await this.connectionsRepo.set(
89
+ { uri: this.appUri, addressType: "nostr" },
90
+ this.connection,
91
+ );
92
+ }
93
+ /**
94
+ * Checks if the signer is connected.
95
+ * @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
96
+ */
97
+ async isConnected() {
98
+ if (this.connection) {
99
+ return true;
105
100
  }
101
+ this.connection = await this.connectionsRepo.get({
102
+ uri: this.appUri,
103
+ addressType: "nostr",
104
+ });
105
+ return this.connection !== undefined;
106
+ }
107
+ async getNostrPublicKey() {
108
+ return this.assertConnection().publicKey;
109
+ }
110
+ async signNostrEvent(event) {
111
+ const config = this.getConfig();
112
+ const res = await (0, common_2.createPopup)(
113
+ (0, common_1.buildJoyIDURL)(
114
+ { ...config, event },
115
+ "popup",
116
+ "/sign-nostr-event",
117
+ ),
118
+ {
119
+ ...config,
120
+ type: common_1.DappRequestType.SignNostrEvent,
121
+ },
122
+ );
123
+ return res.event;
124
+ }
106
125
  }
107
126
  exports.NostrSigner = NostrSigner;
@@ -8,5 +8,9 @@ import { ccc } from "@ckb-ccc/core";
8
8
  * @param {string} icon - The icon URL of the signer.
9
9
  * @returns {ccc.SignerInfo[]} An array of signer information objects.
10
10
  */
11
- export declare function getJoyIdSigners(client: ccc.Client, name: string, icon: string): ccc.SignerInfo[];
12
- //# sourceMappingURL=index.d.ts.map
11
+ export declare function getJoyIdSigners(
12
+ client: ccc.Client,
13
+ name: string,
14
+ icon: string,
15
+ ): ccc.SignerInfo[];
16
+ //# sourceMappingURL=index.d.ts.map