@ckb-ccc/joy-id 0.0.5-alpha.7 → 0.0.6-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/btc/index.d.ts +46 -1
- package/dist/btc/index.d.ts.map +1 -1
- package/dist/btc/index.js +46 -3
- package/dist/ckb/index.d.ts +95 -2
- package/dist/ckb/index.d.ts.map +1 -1
- package/dist/ckb/index.js +98 -8
- package/dist/common/index.d.ts +13 -0
- package/dist/common/index.d.ts.map +1 -1
- package/dist/common/index.js +9 -0
- package/dist/connectionsStorage/index.d.ts +58 -0
- package/dist/connectionsStorage/index.d.ts.map +1 -1
- package/dist/connectionsStorage/index.js +30 -0
- package/dist/evm/index.d.ts +51 -1
- package/dist/evm/index.d.ts.map +1 -1
- package/dist/evm/index.js +52 -4
- package/dist/nostr/index.d.ts +51 -0
- package/dist/nostr/index.d.ts.map +1 -0
- package/dist/nostr/index.js +106 -0
- package/dist/signerFactory/index.d.ts +9 -0
- package/dist/signerFactory/index.d.ts.map +1 -1
- package/dist/signerFactory/index.js +20 -2
- package/dist.commonjs/btc/index.d.ts +46 -1
- package/dist.commonjs/btc/index.d.ts.map +1 -1
- package/dist.commonjs/btc/index.js +46 -3
- package/dist.commonjs/ckb/index.d.ts +95 -2
- package/dist.commonjs/ckb/index.d.ts.map +1 -1
- package/dist.commonjs/ckb/index.js +98 -8
- package/dist.commonjs/common/index.d.ts +13 -0
- package/dist.commonjs/common/index.d.ts.map +1 -1
- package/dist.commonjs/common/index.js +9 -0
- package/dist.commonjs/connectionsStorage/index.d.ts +58 -0
- package/dist.commonjs/connectionsStorage/index.d.ts.map +1 -1
- package/dist.commonjs/connectionsStorage/index.js +30 -0
- package/dist.commonjs/evm/index.d.ts +51 -1
- package/dist.commonjs/evm/index.d.ts.map +1 -1
- package/dist.commonjs/evm/index.js +52 -4
- package/dist.commonjs/nostr/index.d.ts +51 -0
- package/dist.commonjs/nostr/index.d.ts.map +1 -0
- package/dist.commonjs/nostr/index.js +110 -0
- package/dist.commonjs/signerFactory/index.d.ts +9 -0
- package/dist.commonjs/signerFactory/index.d.ts.map +1 -1
- package/dist.commonjs/signerFactory/index.js +20 -2
- package/package.json +3 -3
- package/src/btc/index.ts +47 -12
- package/src/ckb/index.ts +104 -19
- package/src/common/index.ts +13 -0
- package/src/connectionsStorage/index.ts +64 -2
- package/src/evm/index.ts +56 -15
- package/src/nostr/index.ts +135 -0
- package/src/signerFactory/index.ts +25 -2
|
@@ -1,16 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if two AccountSelectors are equal.
|
|
3
|
+
* @param {AccountSelector} a - The first account selector.
|
|
4
|
+
* @param {AccountSelector} b - The second account selector.
|
|
5
|
+
* @returns {boolean} True if the selectors are equal, false otherwise.
|
|
6
|
+
*/
|
|
1
7
|
export function isSelectorEq(a, b) {
|
|
2
8
|
return a.uri === b.uri && a.addressType === b.addressType;
|
|
3
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Class representing a local storage-based repository for managing connections.
|
|
12
|
+
* @class
|
|
13
|
+
* @implements {ConnectionsRepo}
|
|
14
|
+
*/
|
|
4
15
|
export class ConnectionsRepoLocalStorage {
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of ConnectionsRepoLocalStorage.
|
|
18
|
+
* @param {string} [storageKey="ccc-joy-id-signer"] - The local storage key.
|
|
19
|
+
*/
|
|
5
20
|
constructor(storageKey = "ccc-joy-id-signer") {
|
|
6
21
|
this.storageKey = storageKey;
|
|
7
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Reads all connections from local storage.
|
|
25
|
+
* @returns {Promise<[AccountSelector, Connection][]>} A promise that resolves to an array of selectors and connections.
|
|
26
|
+
*/
|
|
8
27
|
async readConnections() {
|
|
9
28
|
return JSON.parse(window.localStorage.getItem(this.storageKey) ?? "[]");
|
|
10
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets a connection for the given selector.
|
|
32
|
+
* @param {AccountSelector} selector - The account selector.
|
|
33
|
+
* @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
|
|
34
|
+
*/
|
|
11
35
|
async get(selector) {
|
|
12
36
|
return (await this.readConnections()).find(([s]) => isSelectorEq(selector, s))?.[1];
|
|
13
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Sets a connection for the given selector.
|
|
40
|
+
* @param {AccountSelector} selector - The account selector.
|
|
41
|
+
* @param {Connection | undefined} connection - The connection to set.
|
|
42
|
+
* @returns {Promise<void>}
|
|
43
|
+
*/
|
|
14
44
|
async set(selector, connection) {
|
|
15
45
|
const connections = await this.readConnections();
|
|
16
46
|
if (connection) {
|
package/dist/evm/index.d.ts
CHANGED
|
@@ -1,20 +1,70 @@
|
|
|
1
1
|
import { ccc } from "@ckb-ccc/core";
|
|
2
2
|
import { ConnectionsRepo } from "../connectionsStorage";
|
|
3
|
+
/**
|
|
4
|
+
* Class representing an EVM signer that extends SignerEvm from @ckb-ccc/core.
|
|
5
|
+
* @class
|
|
6
|
+
* @extends {ccc.SignerEvm}
|
|
7
|
+
*/
|
|
3
8
|
export declare class EvmSigner extends ccc.SignerEvm {
|
|
4
9
|
private readonly name;
|
|
5
10
|
private readonly icon;
|
|
6
11
|
private readonly appUri;
|
|
7
12
|
private readonly connectionsRepo;
|
|
8
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
|
+
*/
|
|
9
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
|
+
*/
|
|
10
29
|
constructor(client: ccc.Client, name: string, icon: string, appUri?: string, connectionsRepo?: ConnectionsRepo);
|
|
11
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Gets the configuration for JoyID.
|
|
32
|
+
* @private
|
|
33
|
+
* @returns {object} The configuration object.
|
|
34
|
+
*/
|
|
12
35
|
private getConfig;
|
|
36
|
+
/**
|
|
37
|
+
* Gets the EVM account address.
|
|
38
|
+
* @returns {Promise<string>} A promise that resolves to the EVM account address.
|
|
39
|
+
*/
|
|
13
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
|
+
*/
|
|
14
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
|
+
*/
|
|
15
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
|
+
*/
|
|
16
56
|
signMessageRaw(message: string | ccc.BytesLike): Promise<ccc.Hex>;
|
|
57
|
+
/**
|
|
58
|
+
* Saves the current connection.
|
|
59
|
+
* @private
|
|
60
|
+
* @returns {Promise<void>}
|
|
61
|
+
*/
|
|
17
62
|
private saveConnection;
|
|
63
|
+
/**
|
|
64
|
+
* Restores the previous connection.
|
|
65
|
+
* @private
|
|
66
|
+
* @returns {Promise<void>}
|
|
67
|
+
*/
|
|
18
68
|
private restoreConnection;
|
|
19
69
|
}
|
|
20
70
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/evm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/evm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B,qBAAa,SAAU,SAAQ,GAAG,CAAC,SAAS;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/evm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,GAAG,CAAC,SAAS;IA2BxC,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IA7BlC,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAQxB;;;;;;;OAOG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,MAAM,SAAuB,EAC7B,eAAe,GAAE,eAAmD;IAKvF;;;;OAIG;IACH,OAAO,CAAC,SAAS;IAUjB;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAItC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAgB9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAQrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAuBvE;;;;OAIG;YACW,cAAc;IAU5B;;;;OAIG;YACW,iBAAiB;CAMhC"}
|
package/dist/evm/index.js
CHANGED
|
@@ -2,13 +2,32 @@ import { ccc } from "@ckb-ccc/core";
|
|
|
2
2
|
import { DappRequestType, buildJoyIDURL } from "@joyid/common";
|
|
3
3
|
import { createPopup } from "../common";
|
|
4
4
|
import { ConnectionsRepoLocalStorage, } from "../connectionsStorage";
|
|
5
|
+
/**
|
|
6
|
+
* Class representing an EVM signer that extends SignerEvm from @ckb-ccc/core.
|
|
7
|
+
* @class
|
|
8
|
+
* @extends {ccc.SignerEvm}
|
|
9
|
+
*/
|
|
5
10
|
export class EvmSigner extends ccc.SignerEvm {
|
|
11
|
+
/**
|
|
12
|
+
* Ensures that the signer is connected and returns the connection.
|
|
13
|
+
* @private
|
|
14
|
+
* @throws Will throw an error if not connected.
|
|
15
|
+
* @returns {Connection} The current connection.
|
|
16
|
+
*/
|
|
6
17
|
assertConnection() {
|
|
7
18
|
if (!this.isConnected() || !this.connection) {
|
|
8
19
|
throw new Error("Not connected");
|
|
9
20
|
}
|
|
10
21
|
return this.connection;
|
|
11
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of EvmSigner.
|
|
25
|
+
* @param {ccc.Client} client - The client instance.
|
|
26
|
+
* @param {string} name - The name of the signer.
|
|
27
|
+
* @param {string} icon - The icon URL of the signer.
|
|
28
|
+
* @param {string} [appUri="https://app.joy.id"] - The application URI.
|
|
29
|
+
* @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
|
|
30
|
+
*/
|
|
12
31
|
constructor(client, name, icon, appUri = "https://app.joy.id", connectionsRepo = new ConnectionsRepoLocalStorage()) {
|
|
13
32
|
super(client);
|
|
14
33
|
this.name = name;
|
|
@@ -16,9 +35,11 @@ export class EvmSigner extends ccc.SignerEvm {
|
|
|
16
35
|
this.appUri = appUri;
|
|
17
36
|
this.connectionsRepo = connectionsRepo;
|
|
18
37
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Gets the configuration for JoyID.
|
|
40
|
+
* @private
|
|
41
|
+
* @returns {object} The configuration object.
|
|
42
|
+
*/
|
|
22
43
|
getConfig() {
|
|
23
44
|
return {
|
|
24
45
|
redirectURL: location.href,
|
|
@@ -28,11 +49,19 @@ export class EvmSigner extends ccc.SignerEvm {
|
|
|
28
49
|
logo: this.icon,
|
|
29
50
|
};
|
|
30
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets the EVM account address.
|
|
54
|
+
* @returns {Promise<string>} A promise that resolves to the EVM account address.
|
|
55
|
+
*/
|
|
31
56
|
async getEvmAccount() {
|
|
32
57
|
return this.assertConnection().address;
|
|
33
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Connects to the provider by requesting authentication.
|
|
61
|
+
* @returns {Promise<void>} A promise that resolves when the connection is established.
|
|
62
|
+
*/
|
|
34
63
|
async connect() {
|
|
35
|
-
const config =
|
|
64
|
+
const config = this.getConfig();
|
|
36
65
|
const res = await createPopup(buildJoyIDURL(config, "popup", "/auth"), {
|
|
37
66
|
...config,
|
|
38
67
|
type: DappRequestType.Auth,
|
|
@@ -44,6 +73,10 @@ export class EvmSigner extends ccc.SignerEvm {
|
|
|
44
73
|
};
|
|
45
74
|
await this.saveConnection();
|
|
46
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Checks if the signer is connected.
|
|
78
|
+
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
79
|
+
*/
|
|
47
80
|
async isConnected() {
|
|
48
81
|
if (this.connection) {
|
|
49
82
|
return true;
|
|
@@ -51,6 +84,11 @@ export class EvmSigner extends ccc.SignerEvm {
|
|
|
51
84
|
await this.restoreConnection();
|
|
52
85
|
return this.connection !== undefined;
|
|
53
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Signs a raw message with the EVM account.
|
|
89
|
+
* @param {string | ccc.BytesLike} message - The message to sign.
|
|
90
|
+
* @returns {Promise<ccc.Hex>} A promise that resolves to the signed message.
|
|
91
|
+
*/
|
|
54
92
|
async signMessageRaw(message) {
|
|
55
93
|
const { address } = this.assertConnection();
|
|
56
94
|
const challenge = typeof message === "string" ? message : ccc.hexFrom(message).slice(2);
|
|
@@ -63,12 +101,22 @@ export class EvmSigner extends ccc.SignerEvm {
|
|
|
63
101
|
}, "popup", "/sign-message"), { ...config, type: DappRequestType.SignMessage });
|
|
64
102
|
return ccc.hexFrom(signature);
|
|
65
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Saves the current connection.
|
|
106
|
+
* @private
|
|
107
|
+
* @returns {Promise<void>}
|
|
108
|
+
*/
|
|
66
109
|
async saveConnection() {
|
|
67
110
|
return this.connectionsRepo.set({
|
|
68
111
|
uri: this.getConfig().joyidAppURL,
|
|
69
112
|
addressType: "ethereum",
|
|
70
113
|
}, this.connection);
|
|
71
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Restores the previous connection.
|
|
117
|
+
* @private
|
|
118
|
+
* @returns {Promise<void>}
|
|
119
|
+
*/
|
|
72
120
|
async restoreConnection() {
|
|
73
121
|
this.connection = await this.connectionsRepo.get({
|
|
74
122
|
uri: this.getConfig().joyidAppURL,
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { ConnectionsRepo } from "../connectionsStorage";
|
|
3
|
+
/**
|
|
4
|
+
* Class representing a Nostr signer that extends SignerNostr from @ckb-ccc/core.
|
|
5
|
+
* @class
|
|
6
|
+
* @extends {ccc.SignerNostr}
|
|
7
|
+
*/
|
|
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>>;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nostr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,GAAG,CAAC,WAAW;IA2B5C,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IA7BlC,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAQxB;;;;;;;OAOG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,MAAM,SAAuB,EAC7B,eAAe,GAAE,eAAmD;IAKvF,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO;IAI3C,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAOzD;;;;OAIG;IACH,OAAO,CAAC,SAAS;IAUjB;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAkB9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAY/B,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAQrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;CAWrC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { DappRequestType, buildJoyIDURL } from "@joyid/common";
|
|
3
|
+
import { createPopup } from "../common";
|
|
4
|
+
import { ConnectionsRepoLocalStorage, } from "../connectionsStorage";
|
|
5
|
+
/**
|
|
6
|
+
* Class representing a Nostr signer that extends SignerNostr from @ckb-ccc/core.
|
|
7
|
+
* @class
|
|
8
|
+
* @extends {ccc.SignerNostr}
|
|
9
|
+
*/
|
|
10
|
+
export class NostrSigner extends ccc.SignerNostr {
|
|
11
|
+
/**
|
|
12
|
+
* Ensures that the signer is connected and returns the connection.
|
|
13
|
+
* @private
|
|
14
|
+
* @throws Will throw an error if not connected.
|
|
15
|
+
* @returns {Connection} The current connection.
|
|
16
|
+
*/
|
|
17
|
+
assertConnection() {
|
|
18
|
+
if (!this.isConnected() || !this.connection) {
|
|
19
|
+
throw new Error("Not connected");
|
|
20
|
+
}
|
|
21
|
+
return this.connection;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of NostrSigner.
|
|
25
|
+
* @param {ccc.Client} client - The client instance.
|
|
26
|
+
* @param {string} name - The name of the signer.
|
|
27
|
+
* @param {string} icon - The icon URL of the signer.
|
|
28
|
+
* @param {string} [appUri="https://app.joy.id"] - The application URI.
|
|
29
|
+
* @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
|
|
30
|
+
*/
|
|
31
|
+
constructor(client, name, icon, appUri = "https://app.joy.id", connectionsRepo = new ConnectionsRepoLocalStorage()) {
|
|
32
|
+
super(client);
|
|
33
|
+
this.name = name;
|
|
34
|
+
this.icon = icon;
|
|
35
|
+
this.appUri = appUri;
|
|
36
|
+
this.connectionsRepo = connectionsRepo;
|
|
37
|
+
}
|
|
38
|
+
static isValidClient(client) {
|
|
39
|
+
return client.addressPrefix === "ckt";
|
|
40
|
+
}
|
|
41
|
+
async replaceClient(client) {
|
|
42
|
+
if (!NostrSigner.isValidClient(client)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return super.replaceClient(client);
|
|
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: "nostr",
|
|
57
|
+
name: this.name,
|
|
58
|
+
logo: this.icon,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Connects to the provider by requesting authentication.
|
|
63
|
+
* @returns {Promise<void>} A promise that resolves when the connection is established.
|
|
64
|
+
*/
|
|
65
|
+
async connect() {
|
|
66
|
+
const config = this.getConfig();
|
|
67
|
+
const res = await createPopup(buildJoyIDURL(config, "popup", "/auth"), {
|
|
68
|
+
...config,
|
|
69
|
+
type: DappRequestType.Auth,
|
|
70
|
+
});
|
|
71
|
+
this.connection = {
|
|
72
|
+
address: "",
|
|
73
|
+
publicKey: ccc.hexFrom(res.nostrPubkey),
|
|
74
|
+
keyType: res.keyType,
|
|
75
|
+
};
|
|
76
|
+
await this.connectionsRepo.set({ uri: this.appUri, addressType: "nostr" }, this.connection);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Checks if the signer is connected.
|
|
80
|
+
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
81
|
+
*/
|
|
82
|
+
async isConnected() {
|
|
83
|
+
if (this.connection) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
this.connection = await this.connectionsRepo.get({
|
|
87
|
+
uri: this.appUri,
|
|
88
|
+
addressType: "nostr",
|
|
89
|
+
});
|
|
90
|
+
return this.connection !== undefined;
|
|
91
|
+
}
|
|
92
|
+
async getNostrPublicKey() {
|
|
93
|
+
if (!this.connection) {
|
|
94
|
+
throw new Error("Not connected");
|
|
95
|
+
}
|
|
96
|
+
return this.connection.publicKey;
|
|
97
|
+
}
|
|
98
|
+
async signNostrEvent(event) {
|
|
99
|
+
const config = this.getConfig();
|
|
100
|
+
const res = await createPopup(buildJoyIDURL({ ...config, event }, "popup", "/sign-nostr-event"), {
|
|
101
|
+
...config,
|
|
102
|
+
type: DappRequestType.SignNostrEvent,
|
|
103
|
+
});
|
|
104
|
+
return res.event;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
/**
|
|
3
|
+
* Gets the JoyID signers based on the client, name, and icon.
|
|
4
|
+
* If the browser is standalone or a webview, returns SignerAlwaysError instances.
|
|
5
|
+
* Otherwise, returns instances of CkbSigner, BitcoinSigner, and EvmSigner.
|
|
6
|
+
* @param {ccc.Client} client - The client instance.
|
|
7
|
+
* @param {string} name - The name of the signer.
|
|
8
|
+
* @param {string} icon - The icon URL of the signer.
|
|
9
|
+
* @returns {ccc.SignerInfo[]} An array of signer information objects.
|
|
10
|
+
*/
|
|
2
11
|
export declare function getJoyIdSigners(client: ccc.Client, name: string, icon: string): ccc.SignerInfo[];
|
|
3
12
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signerFactory/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signerFactory/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAOpC;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,GAAG,CAAC,UAAU,EAAE,CAwClB"}
|
|
@@ -1,10 +1,24 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
1
2
|
import { isStandaloneBrowser } from "@joyid/common";
|
|
2
3
|
import { BitcoinSigner } from "../btc";
|
|
3
4
|
import { CkbSigner } from "../ckb";
|
|
4
5
|
import { EvmSigner } from "../evm";
|
|
6
|
+
import { NostrSigner } from "../nostr";
|
|
7
|
+
/**
|
|
8
|
+
* Gets the JoyID signers based on the client, name, and icon.
|
|
9
|
+
* If the browser is standalone or a webview, returns SignerAlwaysError instances.
|
|
10
|
+
* Otherwise, returns instances of CkbSigner, BitcoinSigner, and EvmSigner.
|
|
11
|
+
* @param {ccc.Client} client - The client instance.
|
|
12
|
+
* @param {string} name - The name of the signer.
|
|
13
|
+
* @param {string} icon - The icon URL of the signer.
|
|
14
|
+
* @returns {ccc.SignerInfo[]} An array of signer information objects.
|
|
15
|
+
*/
|
|
5
16
|
export function getJoyIdSigners(client, name, icon) {
|
|
6
|
-
if (isStandaloneBrowser()) {
|
|
7
|
-
return []
|
|
17
|
+
if (isStandaloneBrowser() || ccc.isWebview(window.navigator.userAgent)) {
|
|
18
|
+
return [ccc.SignerType.CKB, ccc.SignerType.EVM, ccc.SignerType.BTC].map((type) => ({
|
|
19
|
+
name: type,
|
|
20
|
+
signer: new ccc.SignerAlwaysError(client, type, "JoyID can only be used with standard browsers"),
|
|
21
|
+
}));
|
|
8
22
|
}
|
|
9
23
|
return [
|
|
10
24
|
{
|
|
@@ -15,6 +29,10 @@ export function getJoyIdSigners(client, name, icon) {
|
|
|
15
29
|
name: "BTC",
|
|
16
30
|
signer: new BitcoinSigner(client, name, icon),
|
|
17
31
|
},
|
|
32
|
+
{
|
|
33
|
+
name: "Nostr",
|
|
34
|
+
signer: new NostrSigner(client, name, icon),
|
|
35
|
+
},
|
|
18
36
|
{
|
|
19
37
|
name: "EVM",
|
|
20
38
|
signer: new EvmSigner(client, name, icon),
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { ccc } from "@ckb-ccc/core";
|
|
2
2
|
import { ConnectionsRepo } from "../connectionsStorage";
|
|
3
|
+
/**
|
|
4
|
+
* Class representing a Bitcoin signer that extends SignerBtc from @ckb-ccc/core.
|
|
5
|
+
* @class
|
|
6
|
+
* @extends {ccc.SignerBtc}
|
|
7
|
+
*/
|
|
3
8
|
export declare class BitcoinSigner extends ccc.SignerBtc {
|
|
4
9
|
private readonly name;
|
|
5
10
|
private readonly icon;
|
|
@@ -7,14 +12,54 @@ export declare class BitcoinSigner extends ccc.SignerBtc {
|
|
|
7
12
|
private readonly appUri;
|
|
8
13
|
private readonly connectionsRepo;
|
|
9
14
|
private connection?;
|
|
15
|
+
/**
|
|
16
|
+
* Ensures that the signer is connected and returns the connection.
|
|
17
|
+
* @private
|
|
18
|
+
* @throws Will throw an error if not connected.
|
|
19
|
+
* @returns {Connection} The current connection.
|
|
20
|
+
*/
|
|
10
21
|
private assertConnection;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of BitcoinSigner.
|
|
24
|
+
* @param {ccc.Client} client - The client instance.
|
|
25
|
+
* @param {string} name - The name of the signer.
|
|
26
|
+
* @param {string} icon - The icon URL of the signer.
|
|
27
|
+
* @param {"auto" | "p2wpkh" | "p2tr"} [addressType="auto"] - The address type.
|
|
28
|
+
* @param {string} [appUri="https://app.joy.id"] - The application URI.
|
|
29
|
+
* @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
|
|
30
|
+
*/
|
|
11
31
|
constructor(client: ccc.Client, name: string, icon: string, addressType?: "auto" | "p2wpkh" | "p2tr", appUri?: string, connectionsRepo?: ConnectionsRepo);
|
|
12
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Gets the configuration for JoyID.
|
|
34
|
+
* @private
|
|
35
|
+
* @returns {object} The configuration object.
|
|
36
|
+
*/
|
|
13
37
|
private getConfig;
|
|
38
|
+
/**
|
|
39
|
+
* Gets the Bitcoin account address.
|
|
40
|
+
* @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
|
|
41
|
+
*/
|
|
14
42
|
getBtcAccount(): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Gets the Bitcoin public key.
|
|
45
|
+
* @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
|
|
46
|
+
*/
|
|
15
47
|
getBtcPublicKey(): Promise<ccc.Hex>;
|
|
48
|
+
/**
|
|
49
|
+
* Connects to the provider by requesting authentication.
|
|
50
|
+
* @returns {Promise<void>} A promise that resolves when the connection is established.
|
|
51
|
+
*/
|
|
16
52
|
connect(): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Checks if the signer is connected.
|
|
55
|
+
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
56
|
+
*/
|
|
17
57
|
isConnected(): Promise<boolean>;
|
|
58
|
+
/**
|
|
59
|
+
* Signs a raw message with the Bitcoin account.
|
|
60
|
+
* @param {string | ccc.BytesLike} message - The message to sign.
|
|
61
|
+
* @returns {Promise<string>} A promise that resolves to the signed message.
|
|
62
|
+
*/
|
|
18
63
|
signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
|
|
19
64
|
}
|
|
20
65
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/btc/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B,qBAAa,aAAc,SAAQ,GAAG,CAAC,SAAS;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/btc/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,GAAG,CAAC,SAAS;IA4B5C,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IA/BlC,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAQxB;;;;;;;;OAQG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,MAAM,GAAG,QAAQ,GAAG,MAAe,EAChD,MAAM,SAAuB,EAC7B,eAAe,GAAE,eAAmD;IAKvF;;;;OAIG;IACH,OAAO,CAAC,SAAS;IAUjB;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAKtC;;;OAGG;IACG,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAKzC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA+B9B;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAYrC;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAsBvE"}
|
|
@@ -5,13 +5,33 @@ const core_1 = require("@ckb-ccc/core");
|
|
|
5
5
|
const common_1 = require("@joyid/common");
|
|
6
6
|
const common_2 = require("../common");
|
|
7
7
|
const connectionsStorage_1 = require("../connectionsStorage");
|
|
8
|
+
/**
|
|
9
|
+
* Class representing a Bitcoin signer that extends SignerBtc from @ckb-ccc/core.
|
|
10
|
+
* @class
|
|
11
|
+
* @extends {ccc.SignerBtc}
|
|
12
|
+
*/
|
|
8
13
|
class BitcoinSigner extends core_1.ccc.SignerBtc {
|
|
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
|
+
*/
|
|
9
20
|
assertConnection() {
|
|
10
21
|
if (!this.isConnected() || !this.connection) {
|
|
11
22
|
throw new Error("Not connected");
|
|
12
23
|
}
|
|
13
24
|
return this.connection;
|
|
14
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates an instance of BitcoinSigner.
|
|
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 {"auto" | "p2wpkh" | "p2tr"} [addressType="auto"] - The address type.
|
|
32
|
+
* @param {string} [appUri="https://app.joy.id"] - The application URI.
|
|
33
|
+
* @param {ConnectionsRepo} [connectionsRepo=new ConnectionsRepoLocalStorage()] - The connections repository.
|
|
34
|
+
*/
|
|
15
35
|
constructor(client, name, icon, addressType = "auto", appUri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
|
|
16
36
|
super(client);
|
|
17
37
|
this.name = name;
|
|
@@ -20,9 +40,11 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
|
|
|
20
40
|
this.appUri = appUri;
|
|
21
41
|
this.connectionsRepo = connectionsRepo;
|
|
22
42
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Gets the configuration for JoyID.
|
|
45
|
+
* @private
|
|
46
|
+
* @returns {object} The configuration object.
|
|
47
|
+
*/
|
|
26
48
|
getConfig() {
|
|
27
49
|
return {
|
|
28
50
|
redirectURL: location.href,
|
|
@@ -32,14 +54,26 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
|
|
|
32
54
|
logo: this.icon,
|
|
33
55
|
};
|
|
34
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets the Bitcoin account address.
|
|
59
|
+
* @returns {Promise<string>} A promise that resolves to the Bitcoin account address.
|
|
60
|
+
*/
|
|
35
61
|
async getBtcAccount() {
|
|
36
62
|
const { address } = this.assertConnection();
|
|
37
63
|
return address;
|
|
38
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Gets the Bitcoin public key.
|
|
67
|
+
* @returns {Promise<ccc.Hex>} A promise that resolves to the Bitcoin public key.
|
|
68
|
+
*/
|
|
39
69
|
async getBtcPublicKey() {
|
|
40
70
|
const { publicKey } = this.assertConnection();
|
|
41
71
|
return publicKey;
|
|
42
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Connects to the provider by requesting authentication.
|
|
75
|
+
* @returns {Promise<void>} A promise that resolves when the connection is established.
|
|
76
|
+
*/
|
|
43
77
|
async connect() {
|
|
44
78
|
const config = this.getConfig();
|
|
45
79
|
const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
|
|
@@ -62,6 +96,10 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
|
|
|
62
96
|
this.connectionsRepo.set({ uri: this.appUri, addressType: "btc-auto" }, this.connection),
|
|
63
97
|
]);
|
|
64
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Checks if the signer is connected.
|
|
101
|
+
* @returns {Promise<boolean>} A promise that resolves to true if connected, false otherwise.
|
|
102
|
+
*/
|
|
65
103
|
async isConnected() {
|
|
66
104
|
if (this.connection) {
|
|
67
105
|
return true;
|
|
@@ -72,6 +110,11 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
|
|
|
72
110
|
});
|
|
73
111
|
return this.connection !== undefined;
|
|
74
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Signs a raw message with the Bitcoin account.
|
|
115
|
+
* @param {string | ccc.BytesLike} message - The message to sign.
|
|
116
|
+
* @returns {Promise<string>} A promise that resolves to the signed message.
|
|
117
|
+
*/
|
|
75
118
|
async signMessageRaw(message) {
|
|
76
119
|
const { address } = this.assertConnection();
|
|
77
120
|
const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
|