@ckb-ccc/joy-id 0.0.5-alpha.8 → 0.0.7-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 +94 -2
- package/dist/ckb/index.d.ts.map +1 -1
- package/dist/ckb/index.js +115 -14
- 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 +14 -0
- package/dist.commonjs/advanced.d.ts +1 -1
- package/dist.commonjs/advanced.js +40 -19
- package/dist.commonjs/advancedBarrel.d.ts +1 -1
- package/dist.commonjs/advancedBarrel.js +30 -14
- package/dist.commonjs/barrel.d.ts +1 -1
- package/dist.commonjs/barrel.js +30 -14
- package/dist.commonjs/btc/index.d.ts +68 -16
- package/dist.commonjs/btc/index.d.ts.map +1 -1
- package/dist.commonjs/btc/index.js +145 -76
- package/dist.commonjs/ckb/index.d.ts +126 -27
- package/dist.commonjs/ckb/index.d.ts.map +1 -1
- package/dist.commonjs/ckb/index.js +312 -154
- package/dist.commonjs/common/index.d.ts +47 -21
- package/dist.commonjs/common/index.d.ts.map +1 -1
- package/dist.commonjs/common/index.js +59 -45
- package/dist.commonjs/connectionsStorage/index.d.ts +81 -14
- package/dist.commonjs/connectionsStorage/index.d.ts.map +1 -1
- package/dist.commonjs/connectionsStorage/index.js +58 -25
- package/dist.commonjs/evm/index.d.ts +72 -16
- package/dist.commonjs/evm/index.d.ts.map +1 -1
- package/dist.commonjs/evm/index.js +140 -70
- package/dist.commonjs/index.d.ts +1 -1
- package/dist.commonjs/index.js +40 -19
- package/dist.commonjs/nostr/index.d.ts +57 -0
- package/dist.commonjs/nostr/index.d.ts.map +1 -0
- package/dist.commonjs/nostr/index.js +129 -0
- package/dist.commonjs/signerFactory/index.d.ts +15 -2
- package/dist.commonjs/signerFactory/index.d.ts.map +1 -1
- package/dist.commonjs/signerFactory/index.js +52 -27
- package/package.json +5 -5
- package/src/btc/index.ts +47 -12
- package/src/ckb/index.ts +127 -25
- 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 +14 -0
|
@@ -2,53 +2,67 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createPopup = void 0;
|
|
4
4
|
const common_1 = require("@joyid/common");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a popup window for JoyID Dapp requests.
|
|
7
|
+
* @param {string} url - The URL to open in the popup.
|
|
8
|
+
* @param {PopupConfigOptions<T> & { joyidAppURL: string }} config - The popup configuration options.
|
|
9
|
+
* @returns {Promise<PopupReturnType[T]>} A promise that resolves to the response data of the requested type.
|
|
10
|
+
* @throws {PopupNotSupportedError} If popups are not supported in the current browser.
|
|
11
|
+
* @throws {PopupCancelledError} If the popup is closed by the user.
|
|
12
|
+
* @throws {PopupTimeoutError} If the popup operation times out.
|
|
13
|
+
*/
|
|
5
14
|
async function createPopup(url, config) {
|
|
15
|
+
if (config.popup == null) {
|
|
16
|
+
config.popup = (0, common_1.openPopup)("");
|
|
6
17
|
if (config.popup == null) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
18
|
+
return (0, common_1.createBlockDialog)(async () =>
|
|
19
|
+
createPopup(url, config),
|
|
20
|
+
);
|
|
11
21
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
popupEventListener
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
});
|
|
53
67
|
}
|
|
54
68
|
exports.createPopup = createPopup;
|
|
@@ -1,23 +1,90 @@
|
|
|
1
1
|
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
/**
|
|
3
|
+
* Type representing an account selector with a URI and address type.
|
|
4
|
+
* @typedef {Object} AccountSelector
|
|
5
|
+
* @property {string} uri - The URI of the account.
|
|
6
|
+
* @property {string} addressType - The address type of the account.
|
|
7
|
+
*/
|
|
2
8
|
export type AccountSelector = {
|
|
3
|
-
|
|
4
|
-
|
|
9
|
+
uri: string;
|
|
10
|
+
addressType: string;
|
|
5
11
|
};
|
|
6
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Checks if two AccountSelectors are equal.
|
|
14
|
+
* @param {AccountSelector} a - The first account selector.
|
|
15
|
+
* @param {AccountSelector} b - The second account selector.
|
|
16
|
+
* @returns {boolean} True if the selectors are equal, false otherwise.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isSelectorEq(
|
|
19
|
+
a: AccountSelector,
|
|
20
|
+
b: AccountSelector,
|
|
21
|
+
): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Type representing a connection with an address, public key, and key type.
|
|
24
|
+
* @typedef {Object} Connection
|
|
25
|
+
* @property {string} address - The address of the connection.
|
|
26
|
+
* @property {ccc.Hex} publicKey - The public key of the connection.
|
|
27
|
+
* @property {string} keyType - The key type of the connection.
|
|
28
|
+
*/
|
|
7
29
|
export type Connection = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
30
|
+
readonly address: string;
|
|
31
|
+
readonly publicKey: ccc.Hex;
|
|
32
|
+
readonly keyType: string;
|
|
11
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Interface representing a repository for managing connections.
|
|
36
|
+
* @interface
|
|
37
|
+
*/
|
|
12
38
|
export interface ConnectionsRepo {
|
|
13
|
-
|
|
14
|
-
|
|
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>;
|
|
15
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Class representing a local storage-based repository for managing connections.
|
|
58
|
+
* @class
|
|
59
|
+
* @implements {ConnectionsRepo}
|
|
60
|
+
*/
|
|
16
61
|
export declare class ConnectionsRepoLocalStorage implements ConnectionsRepo {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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>;
|
|
22
89
|
}
|
|
23
|
-
//# sourceMappingURL=index.d.ts.map
|
|
90
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/connectionsStorage/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,wBAAgB,YAAY,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/connectionsStorage/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,EAAE,eAAe,GAAG,OAAO,CAE5E;AAED;;;;;;GAMG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,GAAG,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,GAAG,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;IAEhE;;;;;OAKG;IACH,GAAG,CACD,QAAQ,EAAE,eAAe,EACzB,UAAU,EAAE,UAAU,GAAG,SAAS,GACjC,OAAO,CAAC,IAAI,CAAC,CAAC;CAClB;AAED;;;;GAIG;AACH,qBAAa,2BAA4B,YAAW,eAAe;IAKrD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAJvC;;;OAGG;gBAC0B,UAAU,SAAsB;IAE7D;;;OAGG;IACG,eAAe,IAAI,OAAO,CAAC,CAAC,eAAe,EAAE,UAAU,CAAC,EAAE,CAAC;IAIjE;;;;OAIG;IACG,GAAG,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAMrE;;;;;OAKG;IACG,GAAG,CACP,QAAQ,EAAE,eAAe,EACzB,UAAU,EAAE,UAAU,GAAG,SAAS,GACjC,OAAO,CAAC,IAAI,CAAC;CAkBjB"}
|
|
@@ -1,35 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConnectionsRepoLocalStorage = exports.isSelectorEq = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Checks if two AccountSelectors are equal.
|
|
6
|
+
* @param {AccountSelector} a - The first account selector.
|
|
7
|
+
* @param {AccountSelector} b - The second account selector.
|
|
8
|
+
* @returns {boolean} True if the selectors are equal, false otherwise.
|
|
9
|
+
*/
|
|
4
10
|
function isSelectorEq(a, b) {
|
|
5
|
-
|
|
11
|
+
return a.uri === b.uri && a.addressType === b.addressType;
|
|
6
12
|
}
|
|
7
13
|
exports.isSelectorEq = isSelectorEq;
|
|
14
|
+
/**
|
|
15
|
+
* Class representing a local storage-based repository for managing connections.
|
|
16
|
+
* @class
|
|
17
|
+
* @implements {ConnectionsRepo}
|
|
18
|
+
*/
|
|
8
19
|
class ConnectionsRepoLocalStorage {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
);
|
|
33
65
|
}
|
|
66
|
+
}
|
|
34
67
|
}
|
|
35
68
|
exports.ConnectionsRepoLocalStorage = ConnectionsRepoLocalStorage;
|
|
@@ -1,20 +1,76 @@
|
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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;
|
|
19
75
|
}
|
|
20
|
-
//# sourceMappingURL=index.d.ts.map
|
|
76
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -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"}
|
|
@@ -5,78 +5,148 @@ 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 an EVM signer that extends SignerEvm from @ckb-ccc/core.
|
|
10
|
+
* @class
|
|
11
|
+
* @extends {ccc.SignerEvm}
|
|
12
|
+
*/
|
|
8
13
|
class EvmSigner extends core_1.ccc.SignerEvm {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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");
|
|
14
23
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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;
|
|
80
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
|
+
}
|
|
81
151
|
}
|
|
82
152
|
exports.EvmSigner = EvmSigner;
|
package/dist.commonjs/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * as JoyId from "./barrel";
|
|
2
|
-
//# sourceMappingURL=index.d.ts.map
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|