@ckb-ccc/joy-id 1.0.33 → 1.1.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.
Files changed (92) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/advanced.d.mts +2 -0
  3. package/dist/advanced.mjs +1 -0
  4. package/dist/advancedBarrel.d.mts +44 -0
  5. package/dist/advancedBarrel.d.mts.map +1 -0
  6. package/dist/advancedBarrel.mjs +2 -0
  7. package/dist/advancedBarrel.mjs.map +1 -0
  8. package/dist/barrel.d.mts +339 -0
  9. package/dist/barrel.d.mts.map +1 -0
  10. package/dist/barrel.mjs +2 -0
  11. package/dist/barrel.mjs.map +1 -0
  12. package/dist/connectionsStorage-BloUSPun.mjs +2 -0
  13. package/dist/connectionsStorage-BloUSPun.mjs.map +1 -0
  14. package/dist/index-DGqAOLbu.d.mts +91 -0
  15. package/dist/index-DGqAOLbu.d.mts.map +1 -0
  16. package/dist/index.d.mts +2 -0
  17. package/dist/index.mjs +1 -0
  18. package/dist/rolldown-runtime-DK3Fl9T5.mjs +1 -0
  19. package/dist.commonjs/advanced.d.ts +2 -2
  20. package/dist.commonjs/advanced.js +1 -37
  21. package/dist.commonjs/advancedBarrel.d.ts +43 -2
  22. package/dist.commonjs/advancedBarrel.d.ts.map +1 -1
  23. package/dist.commonjs/advancedBarrel.js +2 -18
  24. package/dist.commonjs/advancedBarrel.js.map +1 -0
  25. package/dist.commonjs/barrel.d.ts +338 -5
  26. package/dist.commonjs/barrel.d.ts.map +1 -1
  27. package/dist.commonjs/barrel.js +2 -21
  28. package/dist.commonjs/barrel.js.map +1 -0
  29. package/dist.commonjs/connectionsStorage-CQE3XNWI.js +2 -0
  30. package/dist.commonjs/connectionsStorage-CQE3XNWI.js.map +1 -0
  31. package/dist.commonjs/index-CJeW3ePH.d.ts +93 -0
  32. package/dist.commonjs/index-CJeW3ePH.d.ts.map +1 -0
  33. package/dist.commonjs/index.d.ts +2 -2
  34. package/dist.commonjs/index.js +1 -37
  35. package/package.json +32 -20
  36. package/tsdown.config.mts +43 -0
  37. package/dist/advanced.d.ts +0 -2
  38. package/dist/advanced.d.ts.map +0 -1
  39. package/dist/advanced.js +0 -1
  40. package/dist/advancedBarrel.d.ts +0 -3
  41. package/dist/advancedBarrel.d.ts.map +0 -1
  42. package/dist/advancedBarrel.js +0 -2
  43. package/dist/barrel.d.ts +0 -6
  44. package/dist/barrel.d.ts.map +0 -1
  45. package/dist/barrel.js +0 -5
  46. package/dist/btc/index.d.ts +0 -81
  47. package/dist/btc/index.d.ts.map +0 -1
  48. package/dist/btc/index.js +0 -197
  49. package/dist/ckb/index.d.ts +0 -117
  50. package/dist/ckb/index.d.ts.map +0 -1
  51. package/dist/ckb/index.js +0 -267
  52. package/dist/common/index.d.ts +0 -36
  53. package/dist/common/index.d.ts.map +0 -1
  54. package/dist/common/index.js +0 -61
  55. package/dist/connectionsStorage/index.d.ts +0 -87
  56. package/dist/connectionsStorage/index.d.ts.map +0 -1
  57. package/dist/connectionsStorage/index.js +0 -65
  58. package/dist/evm/index.d.ts +0 -66
  59. package/dist/evm/index.d.ts.map +0 -1
  60. package/dist/evm/index.js +0 -129
  61. package/dist/index.d.ts +0 -2
  62. package/dist/index.d.ts.map +0 -1
  63. package/dist/index.js +0 -1
  64. package/dist/nostr/index.d.ts +0 -57
  65. package/dist/nostr/index.d.ts.map +0 -1
  66. package/dist/nostr/index.js +0 -116
  67. package/dist/signerFactory/index.d.ts +0 -14
  68. package/dist/signerFactory/index.d.ts.map +0 -1
  69. package/dist/signerFactory/index.js +0 -51
  70. package/dist.commonjs/advanced.d.ts.map +0 -1
  71. package/dist.commonjs/btc/index.d.ts +0 -81
  72. package/dist.commonjs/btc/index.d.ts.map +0 -1
  73. package/dist.commonjs/btc/index.js +0 -201
  74. package/dist.commonjs/ckb/index.d.ts +0 -117
  75. package/dist.commonjs/ckb/index.d.ts.map +0 -1
  76. package/dist.commonjs/ckb/index.js +0 -271
  77. package/dist.commonjs/common/index.d.ts +0 -36
  78. package/dist.commonjs/common/index.d.ts.map +0 -1
  79. package/dist.commonjs/common/index.js +0 -64
  80. package/dist.commonjs/connectionsStorage/index.d.ts +0 -87
  81. package/dist.commonjs/connectionsStorage/index.d.ts.map +0 -1
  82. package/dist.commonjs/connectionsStorage/index.js +0 -70
  83. package/dist.commonjs/evm/index.d.ts +0 -66
  84. package/dist.commonjs/evm/index.d.ts.map +0 -1
  85. package/dist.commonjs/evm/index.js +0 -133
  86. package/dist.commonjs/index.d.ts.map +0 -1
  87. package/dist.commonjs/nostr/index.d.ts +0 -57
  88. package/dist.commonjs/nostr/index.d.ts.map +0 -1
  89. package/dist.commonjs/nostr/index.js +0 -120
  90. package/dist.commonjs/signerFactory/index.d.ts +0 -14
  91. package/dist.commonjs/signerFactory/index.d.ts.map +0 -1
  92. package/dist.commonjs/signerFactory/index.js +0 -54
package/dist/btc/index.js DELETED
@@ -1,197 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
- import { DappRequestType, buildJoyIDURL } from "@joyid/common";
3
- import { createPopup } from "../common/index.js";
4
- import { ConnectionsRepoLocalStorage, } from "../connectionsStorage/index.js";
5
- /**
6
- * Class representing a Bitcoin signer that extends SignerBtc
7
- * @public
8
- */
9
- export class BitcoinSigner extends ccc.SignerBtc {
10
- /**
11
- * Ensures that the signer is connected and returns the connection.
12
- * @throws Will throw an error if not connected.
13
- * @returns The current connection.
14
- */
15
- async assertConnection() {
16
- if (!(await this.isConnected()) || !this.connection) {
17
- throw new Error("Not connected");
18
- }
19
- return this.connection;
20
- }
21
- /**
22
- * Creates an instance of BitcoinSigner.
23
- * @param client - The client instance.
24
- * @param name - The name of the signer.
25
- * @param icon - The icon URL of the signer.
26
- * @param addressType - The address type.
27
- * @param _appUri - The application URI.
28
- * @param connectionsRepo - The connections repository.
29
- */
30
- constructor(client, name, icon, preferredNetworks = [
31
- {
32
- addressPrefix: "ckb",
33
- signerType: ccc.SignerType.BTC,
34
- network: "btc",
35
- },
36
- {
37
- addressPrefix: "ckt",
38
- signerType: ccc.SignerType.BTC,
39
- network: "btcTestnet",
40
- },
41
- ], addressType = "auto", _appUri, connectionsRepo = new ConnectionsRepoLocalStorage()) {
42
- super(client);
43
- this.name = name;
44
- this.icon = icon;
45
- this.preferredNetworks = preferredNetworks;
46
- this.addressType = addressType;
47
- this._appUri = _appUri;
48
- this.connectionsRepo = connectionsRepo;
49
- this.network = "btcTestnet";
50
- }
51
- /**
52
- * Gets the configuration for JoyID.
53
- * @returns The configuration object.
54
- */
55
- getConfig() {
56
- const { network } = this.matchNetworkPreference(this.preferredNetworks, this.network) ?? { network: this.network };
57
- if (this.network !== network) {
58
- this.connection = undefined;
59
- }
60
- this.network = network;
61
- const url = {
62
- btc: "https://app.joy.id",
63
- btcTestnet: "https://testnet.joyid.dev",
64
- }[network];
65
- if (!url) {
66
- throw new Error(`JoyID wallet doesn't support the requested chain ${this.network}`);
67
- }
68
- return {
69
- redirectURL: location.href,
70
- joyidAppURL: this._appUri ?? url,
71
- requestNetwork: `btc-${this.addressType}`,
72
- name: this.name,
73
- logo: this.icon,
74
- };
75
- }
76
- async disconnect() {
77
- await super.disconnect();
78
- await this.connectionsRepo.set({ uri: this.getConfig().joyidAppURL, addressType: "btc" }, undefined);
79
- }
80
- /**
81
- * Gets the Bitcoin account address.
82
- * @returns A promise that resolves to the Bitcoin account address.
83
- */
84
- async getBtcAccount() {
85
- const { address } = await this.assertConnection();
86
- return address;
87
- }
88
- /**
89
- * Gets the Bitcoin public key.
90
- * @returns A promise that resolves to the Bitcoin public key.
91
- */
92
- async getBtcPublicKey() {
93
- const { publicKey } = await this.assertConnection();
94
- return publicKey;
95
- }
96
- /**
97
- * Connects to the provider by requesting authentication.
98
- * @returns A promise that resolves when the connection is established.
99
- */
100
- async connect() {
101
- const config = this.getConfig();
102
- const res = await createPopup(buildJoyIDURL(config, "popup", "/auth"), {
103
- ...config,
104
- type: DappRequestType.Auth,
105
- });
106
- const { address, pubkey } = (() => {
107
- if (this.addressType === "auto") {
108
- return res.btcAddressType === "p2wpkh" ? res.nativeSegwit : res.taproot;
109
- }
110
- return this.addressType === "p2wpkh" ? res.nativeSegwit : res.taproot;
111
- })();
112
- this.connection = {
113
- address,
114
- publicKey: ccc.hexFrom(pubkey),
115
- keyType: res.keyType,
116
- };
117
- await Promise.all([
118
- this.connectionsRepo.set({ uri: config.joyidAppURL, addressType: `btc-${this.addressType}` }, this.connection),
119
- this.connectionsRepo.set({ uri: config.joyidAppURL, addressType: "btc-auto" }, this.connection),
120
- ]);
121
- }
122
- /**
123
- * Checks if the signer is connected.
124
- * @returns A promise that resolves to true if connected, false otherwise.
125
- */
126
- async isConnected() {
127
- if (this.connection) {
128
- return true;
129
- }
130
- this.connection = await this.connectionsRepo.get({
131
- uri: this.getConfig().joyidAppURL,
132
- addressType: `btc-${this.addressType}`,
133
- });
134
- return this.connection !== undefined;
135
- }
136
- /**
137
- * Signs a raw message with the Bitcoin account.
138
- * @param message - The message to sign.
139
- * @returns A promise that resolves to the signed message.
140
- */
141
- async signMessageRaw(message) {
142
- const { address } = await this.assertConnection();
143
- const challenge = typeof message === "string" ? message : ccc.hexFrom(message).slice(2);
144
- const config = this.getConfig();
145
- const { signature } = await createPopup(buildJoyIDURL({
146
- ...config,
147
- challenge,
148
- address,
149
- signMessageType: "ecdsa",
150
- }, "popup", "/sign-message"), { ...config, type: DappRequestType.SignMessage });
151
- return signature;
152
- }
153
- /**
154
- * Signs a PSBT using JoyID wallet.
155
- *
156
- * @param psbtHex - The hex string of PSBT to sign.
157
- * @returns A promise that resolves to the signed PSBT as a Hex string.
158
- */
159
- async signPsbt(psbtHex, options) {
160
- const { address } = await this.assertConnection();
161
- const formattedOptions = ccc.SignPsbtOptions.from(options);
162
- const config = this.getConfig();
163
- const { tx: signedPsbtHex } = await createPopup(buildJoyIDURL({
164
- ...config,
165
- tx: ccc.hexFrom(psbtHex).slice(2),
166
- options: formattedOptions,
167
- signerAddress: address,
168
- autoFinalized: formattedOptions.autoFinalized,
169
- }, "popup", "/sign-psbt"), { ...config, type: DappRequestType.SignPsbt });
170
- return ccc.hexFrom(signedPsbtHex);
171
- }
172
- /**
173
- * Broadcasts a PSBT to the Bitcoin network.
174
- *
175
- * @remarks
176
- * JoyID does not support broadcasting a signed PSBT directly.
177
- * It only supports "Sign and Broadcast" as a single atomic operation via `signAndBroadcastPsbt`.
178
- */
179
- async broadcastPsbt(_psbtHex, _options) {
180
- throw new Error("JoyID does not support broadcasting signed PSBTs directly. Use signAndBroadcastPsbt instead.");
181
- }
182
- async signAndBroadcastPsbt(psbtHex, options) {
183
- const { address } = await this.assertConnection();
184
- const formattedOptions = ccc.SignPsbtOptions.from(options);
185
- const config = this.getConfig();
186
- // ccc.hexFrom adds 0x prefix, but BTC expects non-0x
187
- const { tx: txid } = await createPopup(buildJoyIDURL({
188
- ...config,
189
- tx: ccc.hexFrom(psbtHex).slice(2),
190
- options: formattedOptions,
191
- signerAddress: address,
192
- autoFinalized: true, // sendPsbt always finalizes
193
- isSend: true,
194
- }, "popup", "/sign-psbt"), { ...config, type: DappRequestType.SignPsbt });
195
- return ccc.hexFrom(txid);
196
- }
197
- }
@@ -1,117 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
- import { ConnectionsRepo } from "../connectionsStorage/index.js";
3
- /**
4
- * Class representing a CKB signer that extends Signer
5
- * @public
6
- */
7
- export declare class CkbSigner extends ccc.Signer {
8
- private readonly name;
9
- private readonly icon;
10
- private readonly _appUri?;
11
- private readonly _aggregatorUri?;
12
- private readonly connectionsRepo;
13
- /**
14
- * Gets the signer type.
15
- * @returns The type of the signer.
16
- */
17
- get type(): ccc.SignerType;
18
- /**
19
- * Gets the sign type.
20
- * @returns The sign type.
21
- */
22
- get signType(): ccc.SignerSignType;
23
- private connection?;
24
- /**
25
- * Ensures that the signer is connected and returns the connection.
26
- * @throws Will throw an error if not connected.
27
- * @returns A promise that resolves to the current connection.
28
- */
29
- private assertConnection;
30
- /**
31
- * Creates an instance of CkbSigner.
32
- * @param client - The client instance.
33
- * @param name - The name of the signer.
34
- * @param icon - The icon URL of the signer.
35
- * @param _appUri - The application URI.
36
- * @param _aggregatorUri - The aggregator URI.
37
- * @param connectionsRepo - The connections repository.
38
- */
39
- constructor(client: ccc.Client, name: string, icon: string, _appUri?: string | undefined, _aggregatorUri?: string | undefined, connectionsRepo?: ConnectionsRepo);
40
- /**
41
- * Gets the configuration for JoyID.
42
- * @returns The configuration object.
43
- */
44
- private getConfig;
45
- /**
46
- * Gets the aggregator URI.
47
- * @returns The aggregator URI.
48
- */
49
- private getAggregatorUri;
50
- /**
51
- * Connects to the provider by requesting authentication.
52
- * @returns A promise that resolves when the connection is established.
53
- */
54
- connect(): Promise<void>;
55
- disconnect(): Promise<void>;
56
- /**
57
- * Checks if the signer is connected.
58
- * @returns A promise that resolves to true if connected, false otherwise.
59
- */
60
- isConnected(): Promise<boolean>;
61
- /**
62
- * Gets the internal address.
63
- * @returns A promise that resolves to the internal address.
64
- */
65
- getInternalAddress(): Promise<string>;
66
- /**
67
- * Gets the identity of the signer.
68
- * @returns A promise that resolves to the identity.
69
- */
70
- getIdentity(): Promise<string>;
71
- /**
72
- * Gets the address object.
73
- * @returns A promise that resolves to the address object.
74
- */
75
- getAddressObj(): Promise<ccc.Address>;
76
- /**
77
- * Gets the address objects.
78
- * @returns A promise that resolves to an array of address objects.
79
- */
80
- getAddressObjs(): Promise<ccc.Address[]>;
81
- /**
82
- * Prepares a transaction.
83
- * @param txLike - The transaction-like object.
84
- * @returns A promise that resolves to the prepared transaction.
85
- */
86
- prepareTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
87
- /**
88
- * Prepares a transaction for a sub key.
89
- * @param tx - The transaction object.
90
- * @param witness - The witness arguments.
91
- * @throws Will throw an error if no COTA cells are found for the sub key wallet.
92
- */
93
- private prepareTransactionForSubKey;
94
- /**
95
- * Signs a transaction.
96
- * @param txLike - The transaction-like object.
97
- * @returns A promise that resolves to the signed transaction.
98
- */
99
- signOnlyTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
100
- /**
101
- * Signs a raw message with the account.
102
- * @param message - The message to sign.
103
- * @returns A promise that resolves to the signed message.
104
- */
105
- signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
106
- /**
107
- * Saves the current connection.
108
- * @returns
109
- */
110
- private saveConnection;
111
- /**
112
- * Restores the previous connection.
113
- * @returns
114
- */
115
- private restoreConnection;
116
- }
117
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ckb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAIpC,OAAO,EAEL,eAAe,EAEhB,MAAM,gCAAgC,CAAC;AAExC;;;GAGG;AACH,qBAAa,SAAU,SAAQ,GAAG,CAAC,MAAM;IA2CrC,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;IACzB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,eAAe;IA9ClC;;;OAGG;IACH,IAAI,IAAI,IAAI,GAAG,CAAC,UAAU,CAEzB;IAED;;;OAGG;IACH,IAAI,QAAQ,IAAI,GAAG,CAAC,cAAc,CAEjC;IAED,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC;;;;OAIG;YACW,gBAAgB;IAQ9B;;;;;;;;OAQG;gBAED,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,YAAA,EAChB,cAAc,CAAC,EAAE,MAAM,YAAA,EACvB,eAAe,GAAE,eAAmD;IAKvF;;;OAGG;IACH,OAAO,CAAC,SAAS;IAajB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IASxB;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAgBxB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAOjC;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAQrC;;;OAGG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAI3C;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;IAQpC;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;IAO3C;;;OAGG;IACG,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IAI9C;;;;OAIG;IACG,kBAAkB,CACtB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;IAmB3B;;;;;OAKG;YACW,2BAA2B;IA0CzC;;;;OAIG;IACG,mBAAmB,CACvB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;IA4C3B;;;;OAIG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IA2BtE;;;OAGG;YACW,cAAc;IAU5B;;;OAGG;YACW,iBAAiB;CAMhC"}
package/dist/ckb/index.js DELETED
@@ -1,267 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
- import { Aggregator } from "@joyid/ckb";
3
- import { DappRequestType, buildJoyIDURL } from "@joyid/common";
4
- import { createPopup } from "../common/index.js";
5
- import { ConnectionsRepoLocalStorage, } from "../connectionsStorage/index.js";
6
- /**
7
- * Class representing a CKB signer that extends Signer
8
- * @public
9
- */
10
- export class CkbSigner extends ccc.Signer {
11
- /**
12
- * Gets the signer type.
13
- * @returns The type of the signer.
14
- */
15
- get type() {
16
- return ccc.SignerType.CKB;
17
- }
18
- /**
19
- * Gets the sign type.
20
- * @returns The sign type.
21
- */
22
- get signType() {
23
- return ccc.SignerSignType.JoyId;
24
- }
25
- /**
26
- * Ensures that the signer is connected and returns the connection.
27
- * @throws Will throw an error if not connected.
28
- * @returns A promise that resolves to the current connection.
29
- */
30
- async assertConnection() {
31
- if (!(await this.isConnected()) || !this.connection) {
32
- throw new Error("Not connected");
33
- }
34
- return this.connection;
35
- }
36
- /**
37
- * Creates an instance of CkbSigner.
38
- * @param client - The client instance.
39
- * @param name - The name of the signer.
40
- * @param icon - The icon URL of the signer.
41
- * @param _appUri - The application URI.
42
- * @param _aggregatorUri - The aggregator URI.
43
- * @param connectionsRepo - The connections repository.
44
- */
45
- constructor(client, name, icon, _appUri, _aggregatorUri, connectionsRepo = new ConnectionsRepoLocalStorage()) {
46
- super(client);
47
- this.name = name;
48
- this.icon = icon;
49
- this._appUri = _appUri;
50
- this._aggregatorUri = _aggregatorUri;
51
- this.connectionsRepo = connectionsRepo;
52
- }
53
- /**
54
- * Gets the configuration for JoyID.
55
- * @returns The configuration object.
56
- */
57
- getConfig() {
58
- return {
59
- redirectURL: location.href,
60
- joyidAppURL: this._appUri ??
61
- (this.client.addressPrefix === "ckb"
62
- ? "https://app.joy.id"
63
- : "https://testnet.joyid.dev"),
64
- name: this.name,
65
- logo: this.icon,
66
- };
67
- }
68
- /**
69
- * Gets the aggregator URI.
70
- * @returns The aggregator URI.
71
- */
72
- getAggregatorUri() {
73
- return (this._aggregatorUri ??
74
- (this.client.addressPrefix === "ckb"
75
- ? "https://cota.nervina.dev/mainnet-aggregator"
76
- : "https://cota.nervina.dev/aggregator"));
77
- }
78
- /**
79
- * Connects to the provider by requesting authentication.
80
- * @returns A promise that resolves when the connection is established.
81
- */
82
- async connect() {
83
- const config = this.getConfig();
84
- const res = await createPopup(buildJoyIDURL(config, "popup", "/auth"), {
85
- ...config,
86
- type: DappRequestType.Auth,
87
- });
88
- this.connection = {
89
- address: res.address,
90
- publicKey: ccc.hexFrom(res.pubkey),
91
- keyType: res.keyType,
92
- };
93
- await this.saveConnection();
94
- }
95
- async disconnect() {
96
- await super.disconnect();
97
- this.connection = undefined;
98
- await this.saveConnection();
99
- }
100
- /**
101
- * Checks if the signer is connected.
102
- * @returns A promise that resolves to true if connected, false otherwise.
103
- */
104
- async isConnected() {
105
- if (this.connection) {
106
- return true;
107
- }
108
- await this.restoreConnection();
109
- return this.connection !== undefined;
110
- }
111
- /**
112
- * Gets the internal address.
113
- * @returns A promise that resolves to the internal address.
114
- */
115
- async getInternalAddress() {
116
- return (await this.assertConnection()).address;
117
- }
118
- /**
119
- * Gets the identity of the signer.
120
- * @returns A promise that resolves to the identity.
121
- */
122
- async getIdentity() {
123
- const connection = await this.assertConnection();
124
- return JSON.stringify({
125
- keyType: connection.keyType,
126
- publicKey: connection.publicKey.slice(2),
127
- });
128
- }
129
- /**
130
- * Gets the address object.
131
- * @returns A promise that resolves to the address object.
132
- */
133
- async getAddressObj() {
134
- return await ccc.Address.fromString(await this.getInternalAddress(), this.client);
135
- }
136
- /**
137
- * Gets the address objects.
138
- * @returns A promise that resolves to an array of address objects.
139
- */
140
- async getAddressObjs() {
141
- return [await this.getAddressObj()];
142
- }
143
- /**
144
- * Prepares a transaction.
145
- * @param txLike - The transaction-like object.
146
- * @returns A promise that resolves to the prepared transaction.
147
- */
148
- async prepareTransaction(txLike) {
149
- const tx = ccc.Transaction.from(txLike);
150
- await tx.addCellDepsOfKnownScripts(this.client, ccc.KnownScript.JoyId);
151
- const position = await tx.findInputIndexByLock((await this.getAddressObj()).script, this.client);
152
- if (position === undefined) {
153
- return tx;
154
- }
155
- const witness = tx.getWitnessArgsAt(position) ?? ccc.WitnessArgs.from({});
156
- witness.lock = ccc.hexFrom("00".repeat(1000));
157
- await this.prepareTransactionForSubKey(tx, witness);
158
- tx.setWitnessArgsAt(position, witness);
159
- return tx;
160
- }
161
- /**
162
- * Prepares a transaction for a sub key.
163
- * @param tx - The transaction object.
164
- * @param witness - The witness arguments.
165
- * @throws Will throw an error if no COTA cells are found for the sub key wallet.
166
- */
167
- async prepareTransactionForSubKey(tx, witness) {
168
- if (this.connection?.keyType !== "sub_key" ||
169
- (witness.outputType ?? "0x") !== "0x") {
170
- return;
171
- }
172
- const pubkeyHash = ccc.hashCkb(this.connection.publicKey).substring(0, 42);
173
- const lock = (await this.getAddressObj()).script;
174
- const aggregator = new Aggregator(this.getAggregatorUri());
175
- const { unlock_entry: unlockEntry } = await aggregator.generateSubkeyUnlockSmt({
176
- alg_index: 1,
177
- pubkey_hash: pubkeyHash,
178
- lock_script: ccc.hexFrom(lock.toBytes()),
179
- });
180
- witness.outputType = ccc.hexFrom(unlockEntry);
181
- const cotaDeps = [];
182
- for await (const cell of this.client.findCellsByLock(lock, await ccc.Script.fromKnownScript(this.client, ccc.KnownScript.COTA, "0x"))) {
183
- cotaDeps.push(ccc.CellDep.from({
184
- depType: "code",
185
- outPoint: cell.outPoint,
186
- }));
187
- }
188
- if (cotaDeps.length === 0) {
189
- throw new Error("No COTA cells for sub key wallet");
190
- }
191
- tx.addCellDepsAtStart(cotaDeps);
192
- }
193
- /**
194
- * Signs a transaction.
195
- * @param txLike - The transaction-like object.
196
- * @returns A promise that resolves to the signed transaction.
197
- */
198
- async signOnlyTransaction(txLike) {
199
- const tx = ccc.Transaction.from(txLike);
200
- const { script } = await this.getAddressObj();
201
- const witnessIndexes = await ccc.reduceAsync(tx.inputs, async (acc, input, i) => {
202
- const { cellOutput } = await input.getCell(this.client);
203
- if (cellOutput.lock.eq(script)) {
204
- acc.push(i);
205
- }
206
- }, []);
207
- // Trim unnecessary fields to reduce tx size
208
- await tx.prepareSighashAllWitness(script, 0, this.client);
209
- tx.inputs.forEach((i) => {
210
- i.cellOutput = undefined;
211
- i.outputData = undefined;
212
- });
213
- const config = this.getConfig();
214
- const res = await createPopup(buildJoyIDURL({
215
- ...config,
216
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
217
- tx: JSON.parse(tx.stringify()),
218
- signerAddress: (await this.assertConnection()).address,
219
- witnessIndexes,
220
- }, "popup", "/sign-ckb-raw-tx"), {
221
- ...config,
222
- type: DappRequestType.SignCkbRawTx,
223
- });
224
- return ccc.Transaction.from(res.tx);
225
- }
226
- /**
227
- * Signs a raw message with the account.
228
- * @param message - The message to sign.
229
- * @returns A promise that resolves to the signed message.
230
- */
231
- async signMessageRaw(message) {
232
- const { address } = await this.assertConnection();
233
- const challenge = typeof message === "string" ? message : ccc.hexFrom(message).slice(2);
234
- const config = this.getConfig();
235
- const res = await createPopup(buildJoyIDURL({
236
- ...config,
237
- challenge,
238
- isData: typeof message !== "string",
239
- address,
240
- }, "popup", "/sign-message"), { ...config, type: DappRequestType.SignMessage });
241
- return JSON.stringify({
242
- signature: res.signature,
243
- alg: res.alg,
244
- message: res.message,
245
- });
246
- }
247
- /**
248
- * Saves the current connection.
249
- * @returns
250
- */
251
- async saveConnection() {
252
- return this.connectionsRepo.set({
253
- uri: this.getConfig().joyidAppURL,
254
- addressType: "ckb",
255
- }, this.connection);
256
- }
257
- /**
258
- * Restores the previous connection.
259
- * @returns
260
- */
261
- async restoreConnection() {
262
- this.connection = await this.connectionsRepo.get({
263
- uri: this.getConfig().joyidAppURL,
264
- addressType: "ckb",
265
- });
266
- }
267
- }
@@ -1,36 +0,0 @@
1
- import { AuthResponseData, DappRequestType, EvmWeb2LoginResponse, PopupConfigOptions, SignCkbTxResponseData, SignCotaNFTResponseData, SignEvmTxResponseData, SignMessageResponseData, SignNostrEventData } from "@joyid/common";
2
- /**
3
- * Interface representing the return type for various Dapp request types.
4
- */
5
- export interface PopupReturnType {
6
- [DappRequestType.Auth]: AuthResponseData;
7
- [DappRequestType.SignMessage]: SignMessageResponseData;
8
- [DappRequestType.SignEvm]: SignEvmTxResponseData;
9
- [DappRequestType.SignPsbt]: SignEvmTxResponseData;
10
- [DappRequestType.BatchSignPsbt]: {
11
- psbts: string[];
12
- };
13
- [DappRequestType.SignCkbTx]: SignCkbTxResponseData;
14
- [DappRequestType.SignCotaNFT]: SignCotaNFTResponseData;
15
- [DappRequestType.SignCkbRawTx]: SignCkbTxResponseData;
16
- [DappRequestType.SignNostrEvent]: SignNostrEventData;
17
- [DappRequestType.EncryptNostrMessage]: unknown;
18
- [DappRequestType.DecryptNostrMessage]: unknown;
19
- [DappRequestType.AuthMiniApp]: unknown;
20
- [DappRequestType.SignMiniAppEvm]: unknown;
21
- [DappRequestType.SignMiniAppMessage]: unknown;
22
- [DappRequestType.EvmWeb2Login]: EvmWeb2LoginResponse;
23
- }
24
- /**
25
- * Creates a popup window for JoyID Dapp requests.
26
- * @param url - The URL to open in the popup.
27
- * @param config - The popup configuration options.
28
- * @returns A promise that resolves to the response data of the requested type.
29
- * @throws {PopupNotSupportedError} If popups are not supported in the current browser.
30
- * @throws {PopupCancelledError} If the popup is closed by the user.
31
- * @throws {PopupTimeoutError} If the popup operation times out.
32
- */
33
- export declare function createPopup<T extends DappRequestType>(url: string, config: PopupConfigOptions<T> & {
34
- joyidAppURL: string;
35
- }): Promise<PopupReturnType[T]>;
36
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,oBAAoB,EAEpB,kBAAkB,EAGlB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,EACrB,uBAAuB,EACvB,kBAAkB,EAInB,MAAM,eAAe,CAAC;AAEvB;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,gBAAgB,CAAC;IACzC,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,uBAAuB,CAAC;IACvD,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,qBAAqB,CAAC;IACjD,CAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,qBAAqB,CAAC;IAClD,CAAC,eAAe,CAAC,aAAa,CAAC,EAAE;QAC/B,KAAK,EAAE,MAAM,EAAE,CAAC;KACjB,CAAC;IACF,CAAC,eAAe,CAAC,SAAS,CAAC,EAAE,qBAAqB,CAAC;IACnD,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,uBAAuB,CAAC;IACvD,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,qBAAqB,CAAC;IACtD,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,kBAAkB,CAAC;IACrD,CAAC,eAAe,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC/C,CAAC,eAAe,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC/C,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IACvC,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAC1C,CAAC,eAAe,CAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC9C,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,oBAAoB,CAAC;CACtD;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,CAAC,SAAS,eAAe,EACzD,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAAG;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,GACtD,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CA0D7B"}
@@ -1,61 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-call */
2
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
3
- import { DappRequestType, PopupCancelledError, PopupNotSupportedError, PopupTimeoutError, createBlockDialog, isStandaloneBrowser, openPopup, } from "@joyid/common";
4
- /**
5
- * Creates a popup window for JoyID Dapp requests.
6
- * @param url - The URL to open in the popup.
7
- * @param config - The popup configuration options.
8
- * @returns A promise that resolves to the response data of the requested type.
9
- * @throws {PopupNotSupportedError} If popups are not supported in the current browser.
10
- * @throws {PopupCancelledError} If the popup is closed by the user.
11
- * @throws {PopupTimeoutError} If the popup operation times out.
12
- */
13
- export async function createPopup(url, config) {
14
- if (config.popup == null) {
15
- config.popup = openPopup("");
16
- if (config.popup == null) {
17
- return createBlockDialog(async () => createPopup(url, config));
18
- }
19
- }
20
- config.popup.location.href = url;
21
- return new Promise((resolve, reject) => {
22
- if (isStandaloneBrowser()) {
23
- reject(new PopupNotSupportedError(config.popup));
24
- }
25
- let popupEventListener;
26
- let timeoutId;
27
- // Check each second if the popup is closed triggering a PopupCancelledError
28
- const popupTimer = setInterval(() => {
29
- if (config.popup?.closed) {
30
- clearInterval(popupTimer);
31
- clearTimeout(timeoutId);
32
- window.removeEventListener("message", popupEventListener, false);
33
- reject(new PopupCancelledError(config.popup));
34
- }
35
- }, 1000);
36
- timeoutId = setTimeout(() => {
37
- clearInterval(popupTimer);
38
- reject(new PopupTimeoutError(config.popup));
39
- window.removeEventListener("message", popupEventListener, false);
40
- }, (config.timeoutInSeconds ?? 3000) * 1000);
41
- popupEventListener = (e) => {
42
- const { joyidAppURL } = config;
43
- const appURL = new URL(joyidAppURL);
44
- if (e.origin !== appURL.origin) {
45
- return;
46
- }
47
- if (!e.data || e.data?.type !== config.type) {
48
- return;
49
- }
50
- clearTimeout(timeoutId);
51
- clearInterval(popupTimer);
52
- window.removeEventListener("message", popupEventListener, false);
53
- config.popup.close();
54
- if (e.data.error) {
55
- reject(new Error(e.data.error));
56
- }
57
- resolve(e.data.data);
58
- };
59
- window.addEventListener("message", popupEventListener);
60
- });
61
- }