@ckb-ccc/joy-id 0.0.5-alpha.0 → 0.0.5-alpha.1

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 (51) hide show
  1. package/dist/advancedBarrel.d.ts +1 -0
  2. package/dist/advancedBarrel.d.ts.map +1 -1
  3. package/dist/advancedBarrel.js +1 -0
  4. package/dist/barrel.d.ts +1 -0
  5. package/dist/barrel.d.ts.map +1 -1
  6. package/dist/barrel.js +1 -0
  7. package/dist/btc/index.d.ts +5 -2
  8. package/dist/btc/index.d.ts.map +1 -1
  9. package/dist/btc/index.js +27 -16
  10. package/dist/ckb/index.d.ts +5 -3
  11. package/dist/ckb/index.d.ts.map +1 -1
  12. package/dist/ckb/index.js +28 -28
  13. package/dist/common/index.d.ts +24 -0
  14. package/dist/common/index.d.ts.map +1 -0
  15. package/dist/common/index.js +50 -0
  16. package/dist/evm/index.d.ts +20 -0
  17. package/dist/evm/index.d.ts.map +1 -0
  18. package/dist/evm/index.js +78 -0
  19. package/dist/signerFactory/index.d.ts +1 -1
  20. package/dist/signerFactory/index.d.ts.map +1 -1
  21. package/dist/signerFactory/index.js +11 -5
  22. package/dist.commonjs/advancedBarrel.d.ts +1 -0
  23. package/dist.commonjs/advancedBarrel.d.ts.map +1 -1
  24. package/dist.commonjs/advancedBarrel.js +1 -0
  25. package/dist.commonjs/barrel.d.ts +1 -0
  26. package/dist.commonjs/barrel.d.ts.map +1 -1
  27. package/dist.commonjs/barrel.js +1 -0
  28. package/dist.commonjs/btc/index.d.ts +5 -2
  29. package/dist.commonjs/btc/index.d.ts.map +1 -1
  30. package/dist.commonjs/btc/index.js +26 -15
  31. package/dist.commonjs/ckb/index.d.ts +5 -3
  32. package/dist.commonjs/ckb/index.d.ts.map +1 -1
  33. package/dist.commonjs/ckb/index.js +27 -27
  34. package/dist.commonjs/common/index.d.ts +24 -0
  35. package/dist.commonjs/common/index.d.ts.map +1 -0
  36. package/dist.commonjs/common/index.js +54 -0
  37. package/dist.commonjs/evm/index.d.ts +20 -0
  38. package/dist.commonjs/evm/index.d.ts.map +1 -0
  39. package/dist.commonjs/evm/index.js +82 -0
  40. package/dist.commonjs/signerFactory/index.d.ts +1 -1
  41. package/dist.commonjs/signerFactory/index.d.ts.map +1 -1
  42. package/dist.commonjs/signerFactory/index.js +11 -5
  43. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +1 -1
  44. package/package.json +3 -3
  45. package/src/advancedBarrel.ts +1 -0
  46. package/src/barrel.ts +1 -0
  47. package/src/btc/index.ts +44 -18
  48. package/src/ckb/index.ts +45 -42
  49. package/src/common/index.ts +100 -0
  50. package/src/evm/index.ts +118 -0
  51. package/src/signerFactory/index.ts +15 -5
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BitcoinSigner = void 0;
4
4
  const core_1 = require("@ckb-ccc/core");
5
5
  const common_1 = require("@joyid/common");
6
+ const common_2 = require("../common");
6
7
  const connectionsStorage_1 = require("../connectionsStorage");
7
8
  class BitcoinSigner extends core_1.ccc.SignerBtc {
8
9
  assertConnection() {
@@ -11,14 +12,25 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
11
12
  }
12
13
  return this.connection;
13
14
  }
14
- constructor(client, addressType = "auto", uri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
15
+ constructor(client, name, icon, addressType = "auto", appUri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
15
16
  super(client);
17
+ this.name = name;
18
+ this.icon = icon;
16
19
  this.addressType = addressType;
17
- this.uri = uri;
20
+ this.appUri = appUri;
18
21
  this.connectionsRepo = connectionsRepo;
19
22
  }
20
23
  async replaceClient(client) {
21
- return new BitcoinSigner(client, this.addressType, this.uri);
24
+ return new BitcoinSigner(client, this.name, this.icon, this.addressType, this.appUri, this.connectionsRepo);
25
+ }
26
+ getConfig() {
27
+ return {
28
+ redirectURL: location.href,
29
+ joyidAppURL: this.appUri,
30
+ requestNetwork: `btc-${this.addressType}`,
31
+ name: this.name,
32
+ logo: this.icon,
33
+ };
22
34
  }
23
35
  async getBtcAccount() {
24
36
  const { address } = this.assertConnection();
@@ -29,10 +41,10 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
29
41
  return publicKey;
30
42
  }
31
43
  async connect() {
32
- const res = await (0, common_1.authWithPopup)({
33
- joyidAppURL: this.uri,
34
- redirectURL: location.href,
35
- requestNetwork: `btc-${this.addressType}`,
44
+ const config = this.getConfig();
45
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
46
+ ...config,
47
+ type: common_1.DappRequestType.Auth,
36
48
  });
37
49
  const { address, pubkey } = (() => {
38
50
  if (this.addressType === "auto") {
@@ -46,8 +58,8 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
46
58
  keyType: res.keyType,
47
59
  };
48
60
  await Promise.all([
49
- this.connectionsRepo.set({ uri: this.uri, addressType: `btc-${res.btcAddressType}` }, this.connection),
50
- this.connectionsRepo.set({ uri: this.uri, addressType: "btc-auto" }, this.connection),
61
+ this.connectionsRepo.set({ uri: this.appUri, addressType: `btc-${res.btcAddressType}` }, this.connection),
62
+ this.connectionsRepo.set({ uri: this.appUri, addressType: "btc-auto" }, this.connection),
51
63
  ]);
52
64
  }
53
65
  async isConnected() {
@@ -55,7 +67,7 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
55
67
  return true;
56
68
  }
57
69
  this.connection = await this.connectionsRepo.get({
58
- uri: this.uri,
70
+ uri: this.appUri,
59
71
  addressType: `btc-${this.addressType}`,
60
72
  });
61
73
  return this.connection !== undefined;
@@ -63,14 +75,13 @@ class BitcoinSigner extends core_1.ccc.SignerBtc {
63
75
  async signMessage(message) {
64
76
  const { address } = this.assertConnection();
65
77
  const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
66
- const { signature } = await (0, common_1.signMessageWithPopup)({
67
- joyidAppURL: this.uri,
68
- requestNetwork: `btc-${this.addressType}`,
78
+ const config = this.getConfig();
79
+ const { signature } = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
80
+ ...config,
69
81
  challenge,
70
82
  address,
71
83
  signMessageType: "ecdsa",
72
- redirectURL: location.href,
73
- });
84
+ }, "popup", "/sign-message"), { ...config, type: common_1.DappRequestType.SignMessage });
74
85
  return signature;
75
86
  }
76
87
  }
@@ -1,14 +1,16 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
2
  import { ConnectionsRepo } from "../connectionsStorage";
3
3
  export declare class CkbSigner extends ccc.Signer {
4
- private readonly _uri?;
4
+ private readonly name;
5
+ private readonly icon;
6
+ private readonly _appUri?;
5
7
  private readonly _aggregatorUri?;
6
8
  private readonly connectionsRepo;
7
9
  private connection?;
8
10
  private assertConnection;
9
- constructor(client: ccc.Client, _uri?: string | undefined, _aggregatorUri?: string | undefined, connectionsRepo?: ConnectionsRepo);
11
+ constructor(client: ccc.Client, name: string, icon: string, _appUri?: string | undefined, _aggregatorUri?: string | undefined, connectionsRepo?: ConnectionsRepo);
10
12
  replaceClient(client: ccc.Client): Promise<CkbSigner>;
11
- private getUri;
13
+ private getConfig;
12
14
  private getAggregatorUri;
13
15
  connect(): Promise<void>;
14
16
  disconnect(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ckb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAUpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B,qBAAa,SAAU,SAAQ,GAAG,CAAC,MAAM;IAarC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,eAAe;IAdlC,OAAO,CAAC,UAAU,CAAC,CAAa;YAElB,gBAAgB;gBAS5B,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,CAAC,oBAAQ,EACb,cAAc,CAAC,oBAAQ,EACvB,eAAe,GAAE,eAAmD;IAKjF,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;YAI7C,MAAM;YAUN,gBAAgB;IAUxB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAexB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3B,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ/B,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;IAOrC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IAIxC,kBAAkB,CACtB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YAgBb,2BAA2B;IAuCnC,mBAAmB,CACvB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YA6Bb,cAAc;YAUd,iBAAiB;CAMhC"}
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,uBAAuB,CAAC;AAE/B,qBAAa,SAAU,SAAQ,GAAG,CAAC,MAAM;IAarC,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;IAhBlC,OAAO,CAAC,UAAU,CAAC,CAAa;YAElB,gBAAgB;gBAS5B,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,oBAAQ,EAChB,cAAc,CAAC,oBAAQ,EACvB,eAAe,GAAE,eAAmD;IAKjF,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;YAW7C,SAAS;YAaT,gBAAgB;IAUxB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAgBxB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3B,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ/B,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAIrC,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;IAOrC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IAIxC,kBAAkB,CACtB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YAgBb,2BAA2B;IAuCnC,mBAAmB,CACvB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YAyBb,cAAc;YAUd,iBAAiB;CAMhC"}
@@ -4,6 +4,7 @@ exports.CkbSigner = void 0;
4
4
  const core_1 = require("@ckb-ccc/core");
5
5
  const ckb_1 = require("@joyid/ckb");
6
6
  const common_1 = require("@joyid/common");
7
+ const common_2 = require("../common");
7
8
  const connectionsStorage_1 = require("../connectionsStorage");
8
9
  class CkbSigner extends core_1.ccc.Signer {
9
10
  async assertConnection() {
@@ -12,22 +13,27 @@ class CkbSigner extends core_1.ccc.Signer {
12
13
  }
13
14
  return this.connection;
14
15
  }
15
- constructor(client, _uri, _aggregatorUri, connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
16
+ constructor(client, name, icon, _appUri, _aggregatorUri, connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
16
17
  super(client);
17
- this._uri = _uri;
18
+ this.name = name;
19
+ this.icon = icon;
20
+ this._appUri = _appUri;
18
21
  this._aggregatorUri = _aggregatorUri;
19
22
  this.connectionsRepo = connectionsRepo;
20
23
  }
21
24
  async replaceClient(client) {
22
- return new CkbSigner(client, this._uri);
25
+ return new CkbSigner(client, this.name, this.icon, this._appUri, this._aggregatorUri, this.connectionsRepo);
23
26
  }
24
- async getUri() {
25
- if (this._uri) {
26
- return this._uri;
27
- }
28
- return (await this.client.getAddressPrefix()) === "ckb"
29
- ? "https://app.joy.id"
30
- : "https://testnet.joyid.dev";
27
+ async getConfig() {
28
+ return {
29
+ redirectURL: location.href,
30
+ joyidAppURL: this._appUri ??
31
+ ((await this.client.getAddressPrefix()) === "ckb"
32
+ ? "https://app.joy.id"
33
+ : "https://testnet.joyid.dev"),
34
+ name: this.name,
35
+ logo: this.icon,
36
+ };
31
37
  }
32
38
  async getAggregatorUri() {
33
39
  if (this._aggregatorUri) {
@@ -38,10 +44,10 @@ class CkbSigner extends core_1.ccc.Signer {
38
44
  : "https://cota.nervina.dev/aggregator";
39
45
  }
40
46
  async connect() {
41
- const uri = await this.getUri();
42
- const res = await (0, common_1.authWithPopup)({
43
- joyidAppURL: uri,
44
- redirectURL: location.href,
47
+ const config = await this.getConfig();
48
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
49
+ ...config,
50
+ type: common_1.DappRequestType.Auth,
45
51
  });
46
52
  this.connection = {
47
53
  address: res.address,
@@ -110,35 +116,29 @@ class CkbSigner extends core_1.ccc.Signer {
110
116
  tx.cellDeps.unshift(...cotaDeps);
111
117
  }
112
118
  async signOnlyTransaction(txLike) {
113
- const popup = (0, common_1.openPopup)("");
114
- if (!popup) {
115
- return (0, common_1.createBlockDialog)(async () => this.signOnlyTransaction(txLike));
116
- }
117
119
  const tx = core_1.ccc.Transaction.from(txLike);
118
120
  const { script } = await this.getAddressObj();
119
- popup.location.href = (0, common_1.buildJoyIDURL)({
120
- joyidAppURL: await this.getUri(),
121
+ const config = await this.getConfig();
122
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
123
+ ...config,
121
124
  tx: JSON.parse(tx.stringify()),
122
125
  signerAddress: (await this.assertConnection()).address,
123
- redirectURL: location.href,
124
126
  witnessIndex: await tx.findInputIndexByLock(script, this.client),
125
- }, "popup", "/sign-ckb-raw-tx");
126
- const res = await (0, common_1.runPopup)({
127
- timeoutInSeconds: 3600,
128
- popup,
127
+ }, "popup", "/sign-ckb-raw-tx"), {
128
+ ...config,
129
129
  type: common_1.DappRequestType.SignCkbRawTx,
130
130
  });
131
131
  return core_1.ccc.Transaction.from(res.tx);
132
132
  }
133
133
  async saveConnection() {
134
134
  return this.connectionsRepo.set({
135
- uri: await this.getUri(),
135
+ uri: (await this.getConfig()).joyidAppURL,
136
136
  addressType: "ckb",
137
137
  }, this.connection);
138
138
  }
139
139
  async restoreConnection() {
140
140
  this.connection = await this.connectionsRepo.get({
141
- uri: await this.getUri(),
141
+ uri: (await this.getConfig()).joyidAppURL,
142
142
  addressType: "ckb",
143
143
  });
144
144
  }
@@ -0,0 +1,24 @@
1
+ import { AuthResponseData, DappRequestType, EvmWeb2LoginResponse, PopupConfigOptions, SignCkbTxResponseData, SignCotaNFTResponseData, SignEvmTxResponseData, SignMessageResponseData, SignNostrEventData } from "@joyid/common";
2
+ export interface PopupReturnType {
3
+ [DappRequestType.Auth]: AuthResponseData;
4
+ [DappRequestType.SignMessage]: SignMessageResponseData;
5
+ [DappRequestType.SignEvm]: SignEvmTxResponseData;
6
+ [DappRequestType.SignPsbt]: SignEvmTxResponseData;
7
+ [DappRequestType.BatchSignPsbt]: {
8
+ psbts: string[];
9
+ };
10
+ [DappRequestType.SignCkbTx]: SignCkbTxResponseData;
11
+ [DappRequestType.SignCotaNFT]: SignCotaNFTResponseData;
12
+ [DappRequestType.SignCkbRawTx]: SignCkbTxResponseData;
13
+ [DappRequestType.SignNostrEvent]: SignNostrEventData;
14
+ [DappRequestType.EncryptNostrMessage]: any;
15
+ [DappRequestType.DecryptNostrMessage]: any;
16
+ [DappRequestType.AuthMiniApp]: any;
17
+ [DappRequestType.SignMiniAppEvm]: any;
18
+ [DappRequestType.SignMiniAppMessage]: any;
19
+ [DappRequestType.EvmWeb2Login]: EvmWeb2LoginResponse;
20
+ }
21
+ export declare function createPopup<T extends DappRequestType>(url: string, config: PopupConfigOptions<T> & {
22
+ joyidAppURL: string;
23
+ }): Promise<PopupReturnType[T]>;
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,oBAAoB,EAEpB,kBAAkB,EAGlB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,EACrB,uBAAuB,EACvB,kBAAkB,EAInB,MAAM,eAAe,CAAC;AAEvB,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,GAAG,CAAC;IAC3C,CAAC,eAAe,CAAC,mBAAmB,CAAC,EAAE,GAAG,CAAC;IAC3C,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,GAAG,CAAC;IACnC,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,GAAG,CAAC;IACtC,CAAC,eAAe,CAAC,kBAAkB,CAAC,EAAE,GAAG,CAAC;IAC1C,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,oBAAoB,CAAC;CACtD;AAED,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"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPopup = void 0;
4
+ const common_1 = require("@joyid/common");
5
+ async function createPopup(url, config) {
6
+ if (config.popup == null) {
7
+ config.popup = (0, common_1.openPopup)("");
8
+ if (config.popup == null) {
9
+ return (0, common_1.createBlockDialog)(async () => createPopup(url, config));
10
+ }
11
+ }
12
+ config.popup.location.href = url;
13
+ return new Promise((resolve, reject) => {
14
+ if ((0, common_1.isStandaloneBrowser)()) {
15
+ reject(new common_1.PopupNotSupportedError(config.popup));
16
+ }
17
+ let popupEventListener;
18
+ let timeoutId;
19
+ // Check each second if the popup is closed triggering a PopupCancelledError
20
+ const popupTimer = setInterval(() => {
21
+ if (config.popup?.closed) {
22
+ clearInterval(popupTimer);
23
+ clearTimeout(timeoutId);
24
+ window.removeEventListener("message", popupEventListener, false);
25
+ reject(new common_1.PopupCancelledError(config.popup));
26
+ }
27
+ }, 1000);
28
+ timeoutId = setTimeout(() => {
29
+ clearInterval(popupTimer);
30
+ reject(new common_1.PopupTimeoutError(config.popup));
31
+ window.removeEventListener("message", popupEventListener, false);
32
+ }, (config.timeoutInSeconds ?? 3000) * 1000);
33
+ popupEventListener = (e) => {
34
+ const { joyidAppURL } = config;
35
+ const appURL = new URL(joyidAppURL);
36
+ if (e.origin !== appURL.origin) {
37
+ return;
38
+ }
39
+ if (!e.data || e.data?.type !== config.type) {
40
+ return;
41
+ }
42
+ clearTimeout(timeoutId);
43
+ clearInterval(popupTimer);
44
+ window.removeEventListener("message", popupEventListener, false);
45
+ config.popup.close();
46
+ if (e.data.error) {
47
+ reject(new Error(e.data.error));
48
+ }
49
+ resolve(e.data.data);
50
+ };
51
+ window.addEventListener("message", popupEventListener);
52
+ });
53
+ }
54
+ exports.createPopup = createPopup;
@@ -0,0 +1,20 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { ConnectionsRepo } from "../connectionsStorage";
3
+ export declare class EvmSigner extends ccc.SignerEvm {
4
+ private readonly name;
5
+ private readonly icon;
6
+ private readonly appUri;
7
+ private readonly connectionsRepo;
8
+ private connection?;
9
+ private assertConnection;
10
+ constructor(client: ccc.Client, name: string, icon: string, appUri?: string, connectionsRepo?: ConnectionsRepo);
11
+ replaceClient(client: ccc.Client): Promise<EvmSigner>;
12
+ private getConfig;
13
+ getEvmAccount(): Promise<string>;
14
+ connect(): Promise<void>;
15
+ isConnected(): Promise<boolean>;
16
+ signMessage(message: string | ccc.BytesLike): Promise<ccc.Hex>;
17
+ private saveConnection;
18
+ private restoreConnection;
19
+ }
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;IAaxC,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;IAflC,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC,OAAO,CAAC,gBAAgB;gBAStB,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,MAAM,SAAuB,EAC7B,eAAe,GAAE,eAAmD;IAKjF,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAU3D,OAAO,CAAC,SAAS;IAUX,aAAa;IAIb,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAgBxB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ/B,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;YAuBtD,cAAc;YAUd,iBAAiB;CAMhC"}
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EvmSigner = void 0;
4
+ const core_1 = require("@ckb-ccc/core");
5
+ const common_1 = require("@joyid/common");
6
+ const common_2 = require("../common");
7
+ const connectionsStorage_1 = require("../connectionsStorage");
8
+ class EvmSigner extends core_1.ccc.SignerEvm {
9
+ assertConnection() {
10
+ if (!this.isConnected() || !this.connection) {
11
+ throw new Error("Not connected");
12
+ }
13
+ return this.connection;
14
+ }
15
+ constructor(client, name, icon, appUri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
16
+ super(client);
17
+ this.name = name;
18
+ this.icon = icon;
19
+ this.appUri = appUri;
20
+ this.connectionsRepo = connectionsRepo;
21
+ }
22
+ async replaceClient(client) {
23
+ return new EvmSigner(client, this.name, this.icon, this.appUri, this.connectionsRepo);
24
+ }
25
+ getConfig() {
26
+ return {
27
+ redirectURL: location.href,
28
+ joyidAppURL: this.appUri,
29
+ requestNetwork: `ethereum`,
30
+ name: this.name,
31
+ logo: this.icon,
32
+ };
33
+ }
34
+ async getEvmAccount() {
35
+ return this.assertConnection().address;
36
+ }
37
+ async connect() {
38
+ const config = await this.getConfig();
39
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
40
+ ...config,
41
+ type: common_1.DappRequestType.Auth,
42
+ });
43
+ this.connection = {
44
+ address: res.ethAddress,
45
+ publicKey: core_1.ccc.hexFrom(res.pubkey),
46
+ keyType: res.keyType,
47
+ };
48
+ await this.saveConnection();
49
+ }
50
+ async isConnected() {
51
+ if (this.connection) {
52
+ return true;
53
+ }
54
+ await this.restoreConnection();
55
+ return this.connection !== undefined;
56
+ }
57
+ async signMessage(message) {
58
+ const { address } = this.assertConnection();
59
+ const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
60
+ const config = this.getConfig();
61
+ const { signature } = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
62
+ ...config,
63
+ challenge,
64
+ isData: typeof message !== "string",
65
+ address,
66
+ }, "popup", "/sign-message"), { ...config, type: common_1.DappRequestType.SignMessage });
67
+ return core_1.ccc.hexFrom(signature);
68
+ }
69
+ async saveConnection() {
70
+ return this.connectionsRepo.set({
71
+ uri: this.getConfig().joyidAppURL,
72
+ addressType: "ethereum",
73
+ }, this.connection);
74
+ }
75
+ async restoreConnection() {
76
+ this.connection = await this.connectionsRepo.get({
77
+ uri: this.getConfig().joyidAppURL,
78
+ addressType: "ethereum",
79
+ });
80
+ }
81
+ }
82
+ exports.EvmSigner = EvmSigner;
@@ -1,3 +1,3 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- export declare function getJoyIdSigners(client: ccc.Client): ccc.SignerInfo[];
2
+ export declare function getJoyIdSigners(client: ccc.Client, name: string, icon: string): ccc.SignerInfo[];
3
3
  //# 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;AAIpC,wBAAgB,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,UAAU,EAAE,CAuBpE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/signerFactory/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAKpC,wBAAgB,eAAe,CAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,GAAG,CAAC,UAAU,EAAE,CA4BlB"}
@@ -4,27 +4,33 @@ exports.getJoyIdSigners = void 0;
4
4
  const core_1 = require("@ckb-ccc/core");
5
5
  const btc_1 = require("../btc");
6
6
  const ckb_1 = require("../ckb");
7
- function getJoyIdSigners(client) {
7
+ const evm_1 = require("../evm");
8
+ function getJoyIdSigners(client, name, icon) {
8
9
  return [
9
10
  {
10
11
  type: core_1.ccc.SignerType.CKB,
11
12
  name: "CKB",
12
- signer: new ckb_1.CkbSigner(client),
13
+ signer: new ckb_1.CkbSigner(client, name, icon),
13
14
  },
14
15
  {
15
16
  type: core_1.ccc.SignerType.BTC,
16
17
  name: "BTC",
17
- signer: new btc_1.BitcoinSigner(client),
18
+ signer: new btc_1.BitcoinSigner(client, name, icon),
19
+ },
20
+ {
21
+ type: core_1.ccc.SignerType.EVM,
22
+ name: "EVM",
23
+ signer: new evm_1.EvmSigner(client, name, icon),
18
24
  },
19
25
  {
20
26
  type: core_1.ccc.SignerType.BTC,
21
27
  name: "BTC (P2WPKH)",
22
- signer: new btc_1.BitcoinSigner(client, "p2wpkh"),
28
+ signer: new btc_1.BitcoinSigner(client, name, icon, "p2wpkh"),
23
29
  },
24
30
  {
25
31
  type: core_1.ccc.SignerType.BTC,
26
32
  name: "BTC (P2TR)",
27
- signer: new btc_1.BitcoinSigner(client, "p2tr"),
33
+ signer: new btc_1.BitcoinSigner(client, name, icon, "p2tr"),
28
34
  },
29
35
  ];
30
36
  }