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

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 +6 -3
  8. package/dist/btc/index.d.ts.map +1 -1
  9. package/dist/btc/index.js +28 -17
  10. package/dist/ckb/index.d.ts +9 -3
  11. package/dist/ckb/index.d.ts.map +1 -1
  12. package/dist/ckb/index.js +58 -30
  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 +10 -10
  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 +6 -3
  29. package/dist.commonjs/btc/index.d.ts.map +1 -1
  30. package/dist.commonjs/btc/index.js +27 -16
  31. package/dist.commonjs/ckb/index.d.ts +9 -3
  32. package/dist.commonjs/ckb/index.d.ts.map +1 -1
  33. package/dist.commonjs/ckb/index.js +57 -29
  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 +10 -10
  43. package/package.json +3 -3
  44. package/src/advancedBarrel.ts +1 -0
  45. package/src/barrel.ts +1 -0
  46. package/src/btc/index.ts +45 -19
  47. package/src/ckb/index.ts +89 -44
  48. package/src/common/index.ts +100 -0
  49. package/src/evm/index.ts +118 -0
  50. package/src/signerFactory/index.ts +14 -9
  51. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
@@ -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;AAEpC,OAAO,EAEL,eAAe,EAEhB,MAAM,uBAAuB,CAAC;AAE/B,qBAAa,aAAc,SAAQ,GAAG,CAAC,SAAS;IAa5C,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,eAAe;IAdlC,OAAO,CAAC,UAAU,CAAC,CAAa;IAEhC,OAAO,CAAC,gBAAgB;gBAStB,MAAM,EAAE,GAAG,CAAC,MAAM,EACD,WAAW,GAAE,MAAM,GAAG,QAAQ,GAAG,MAAe,EAChD,GAAG,SAAuB,EAC1B,eAAe,GAAE,eAAmD;IAKjF,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAIzD,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAKhC,eAAe,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAKnC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA+BxB,WAAW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ports.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,22 +67,21 @@ 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;
62
74
  }
63
- async signMessage(message) {
75
+ async signMessageRaw(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,24 +1,30 @@
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;
9
+ get type(): ccc.SignerType;
10
+ get signType(): ccc.SignerSignType;
7
11
  private connection?;
8
12
  private assertConnection;
9
- constructor(client: ccc.Client, _uri?: string | undefined, _aggregatorUri?: string | undefined, connectionsRepo?: ConnectionsRepo);
13
+ constructor(client: ccc.Client, name: string, icon: string, _appUri?: string | undefined, _aggregatorUri?: string | undefined, connectionsRepo?: ConnectionsRepo);
10
14
  replaceClient(client: ccc.Client): Promise<CkbSigner>;
11
- private getUri;
15
+ private getConfig;
12
16
  private getAggregatorUri;
13
17
  connect(): Promise<void>;
14
18
  disconnect(): Promise<void>;
15
19
  isConnected(): Promise<boolean>;
16
20
  getInternalAddress(): Promise<string>;
21
+ getIdentity(): Promise<string>;
17
22
  getAddressObj(): Promise<ccc.Address>;
18
23
  getAddressObjs(): Promise<ccc.Address[]>;
19
24
  prepareTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
20
25
  private prepareTransactionForSubKey;
21
26
  signOnlyTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
27
+ signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
22
28
  private saveConnection;
23
29
  private restoreConnection;
24
30
  }
@@ -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,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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;IAW3D,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,gBAAgB;IAUlB,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,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;IAQ9B,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;IAyBrB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;YA2BxD,cAAc;YAUd,iBAAiB;CAMhC"}
@@ -4,44 +4,55 @@ 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 {
10
+ get type() {
11
+ return core_1.ccc.SignerType.CKB;
12
+ }
13
+ get signType() {
14
+ return core_1.ccc.SignerSignType.JoyId;
15
+ }
9
16
  async assertConnection() {
10
17
  if (!(await this.isConnected()) || !this.connection) {
11
18
  throw new Error("Not connected");
12
19
  }
13
20
  return this.connection;
14
21
  }
15
- constructor(client, _uri, _aggregatorUri, connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
22
+ constructor(client, name, icon, _appUri, _aggregatorUri, connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
16
23
  super(client);
17
- this._uri = _uri;
24
+ this.name = name;
25
+ this.icon = icon;
26
+ this._appUri = _appUri;
18
27
  this._aggregatorUri = _aggregatorUri;
19
28
  this.connectionsRepo = connectionsRepo;
20
29
  }
21
30
  async replaceClient(client) {
22
- return new CkbSigner(client, this._uri);
31
+ return new CkbSigner(client, this.name, this.icon, this._appUri, this._aggregatorUri, this.connectionsRepo);
23
32
  }
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";
33
+ getConfig() {
34
+ return {
35
+ redirectURL: location.href,
36
+ joyidAppURL: this._appUri ?? this.client.addressPrefix === "ckb"
37
+ ? "https://app.joy.id"
38
+ : "https://testnet.joyid.dev",
39
+ name: this.name,
40
+ logo: this.icon,
41
+ };
31
42
  }
32
- async getAggregatorUri() {
43
+ getAggregatorUri() {
33
44
  if (this._aggregatorUri) {
34
45
  return this._aggregatorUri;
35
46
  }
36
- return (await this.client.getAddressPrefix()) === "ckb"
47
+ return this.client.addressPrefix === "ckb"
37
48
  ? "https://cota.nervina.dev/mainnet-aggregator"
38
49
  : "https://cota.nervina.dev/aggregator";
39
50
  }
40
51
  async connect() {
41
- const uri = await this.getUri();
42
- const res = await (0, common_1.authWithPopup)({
43
- joyidAppURL: uri,
44
- redirectURL: location.href,
52
+ const config = this.getConfig();
53
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)(config, "popup", "/auth"), {
54
+ ...config,
55
+ type: common_1.DappRequestType.Auth,
45
56
  });
46
57
  this.connection = {
47
58
  address: res.address,
@@ -64,6 +75,13 @@ class CkbSigner extends core_1.ccc.Signer {
64
75
  async getInternalAddress() {
65
76
  return (await this.assertConnection()).address;
66
77
  }
78
+ async getIdentity() {
79
+ const connection = await this.assertConnection();
80
+ return JSON.stringify({
81
+ keyType: connection.keyType,
82
+ publicKey: connection.publicKey.slice(2),
83
+ });
84
+ }
67
85
  async getAddressObj() {
68
86
  return await core_1.ccc.Address.fromString(await this.getInternalAddress(), this.client);
69
87
  }
@@ -110,35 +128,45 @@ class CkbSigner extends core_1.ccc.Signer {
110
128
  tx.cellDeps.unshift(...cotaDeps);
111
129
  }
112
130
  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
131
  const tx = core_1.ccc.Transaction.from(txLike);
118
132
  const { script } = await this.getAddressObj();
119
- popup.location.href = (0, common_1.buildJoyIDURL)({
120
- joyidAppURL: await this.getUri(),
133
+ const config = this.getConfig();
134
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
135
+ ...config,
121
136
  tx: JSON.parse(tx.stringify()),
122
137
  signerAddress: (await this.assertConnection()).address,
123
- redirectURL: location.href,
124
138
  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,
139
+ }, "popup", "/sign-ckb-raw-tx"), {
140
+ ...config,
129
141
  type: common_1.DappRequestType.SignCkbRawTx,
130
142
  });
131
143
  return core_1.ccc.Transaction.from(res.tx);
132
144
  }
145
+ async signMessageRaw(message) {
146
+ const { address } = await this.assertConnection();
147
+ const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
148
+ const config = this.getConfig();
149
+ const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
150
+ ...config,
151
+ challenge,
152
+ isData: typeof message !== "string",
153
+ address,
154
+ }, "popup", "/sign-message"), { ...config, type: common_1.DappRequestType.SignMessage });
155
+ return JSON.stringify({
156
+ signature: res.signature,
157
+ alg: res.alg,
158
+ message: res.message,
159
+ });
160
+ }
133
161
  async saveConnection() {
134
162
  return this.connectionsRepo.set({
135
- uri: await this.getUri(),
163
+ uri: this.getConfig().joyidAppURL,
136
164
  addressType: "ckb",
137
165
  }, this.connection);
138
166
  }
139
167
  async restoreConnection() {
140
168
  this.connection = await this.connectionsRepo.get({
141
- uri: await this.getUri(),
169
+ uri: this.getConfig().joyidAppURL,
142
170
  addressType: "ckb",
143
171
  });
144
172
  }
@@ -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
+ signMessageRaw(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,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;YAuBzD,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 signMessageRaw(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,CAuBlB"}
@@ -1,30 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getJoyIdSigners = void 0;
4
- const core_1 = require("@ckb-ccc/core");
5
4
  const btc_1 = require("../btc");
6
5
  const ckb_1 = require("../ckb");
7
- function getJoyIdSigners(client) {
6
+ const evm_1 = require("../evm");
7
+ function getJoyIdSigners(client, name, icon) {
8
8
  return [
9
9
  {
10
- type: core_1.ccc.SignerType.CKB,
11
10
  name: "CKB",
12
- signer: new ckb_1.CkbSigner(client),
11
+ signer: new ckb_1.CkbSigner(client, name, icon),
13
12
  },
14
13
  {
15
- type: core_1.ccc.SignerType.BTC,
16
14
  name: "BTC",
17
- signer: new btc_1.BitcoinSigner(client),
15
+ signer: new btc_1.BitcoinSigner(client, name, icon),
16
+ },
17
+ {
18
+ name: "EVM",
19
+ signer: new evm_1.EvmSigner(client, name, icon),
18
20
  },
19
21
  {
20
- type: core_1.ccc.SignerType.BTC,
21
22
  name: "BTC (P2WPKH)",
22
- signer: new btc_1.BitcoinSigner(client, "p2wpkh"),
23
+ signer: new btc_1.BitcoinSigner(client, name, icon, "p2wpkh"),
23
24
  },
24
25
  {
25
- type: core_1.ccc.SignerType.BTC,
26
26
  name: "BTC (P2TR)",
27
- signer: new btc_1.BitcoinSigner(client, "p2tr"),
27
+ signer: new btc_1.BitcoinSigner(client, name, icon, "p2tr"),
28
28
  },
29
29
  ];
30
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/joy-id",
3
- "version": "0.0.5-alpha.0",
3
+ "version": "0.0.5-alpha.2",
4
4
  "description": "Connector's support for JoyID",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -38,9 +38,9 @@
38
38
  "access": "public"
39
39
  },
40
40
  "dependencies": {
41
- "@ckb-ccc/core": "0.0.5-alpha.0",
41
+ "@ckb-ccc/core": "0.0.5-alpha.2",
42
42
  "@joyid/ckb": "^0.0.11",
43
43
  "@joyid/common": "^0.0.3"
44
44
  },
45
- "gitHead": "8a927f901a8f2fe7dfe2f6bf78a44689d6503949"
45
+ "gitHead": "abbcbe8e25dc561e0a134d3906f40bb49b8a035f"
46
46
  }
@@ -1 +1,2 @@
1
+ export * from "./common";
1
2
  export * from "./connectionsStorage";
package/src/barrel.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./btc";
2
2
  export * from "./ckb";
3
+ export * from "./evm";
3
4
  export * from "./signerFactory";