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

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 (41) hide show
  1. package/dist/btc/index.d.ts +1 -2
  2. package/dist/btc/index.d.ts.map +1 -1
  3. package/dist/btc/index.js +1 -4
  4. package/dist/ckb/index.d.ts +4 -1
  5. package/dist/ckb/index.d.ts.map +1 -1
  6. package/dist/ckb/index.js +41 -15
  7. package/dist/evm/index.d.ts +1 -2
  8. package/dist/evm/index.d.ts.map +1 -1
  9. package/dist/evm/index.js +1 -4
  10. package/dist/signerFactory/index.d.ts.map +1 -1
  11. package/dist/signerFactory/index.js +7 -5
  12. package/dist.commonjs/advanced.d.ts +1 -1
  13. package/dist.commonjs/advanced.js +40 -19
  14. package/dist.commonjs/advancedBarrel.d.ts +1 -1
  15. package/dist.commonjs/advancedBarrel.js +30 -14
  16. package/dist.commonjs/barrel.d.ts +1 -1
  17. package/dist.commonjs/barrel.js +30 -14
  18. package/dist.commonjs/btc/index.d.ts +22 -16
  19. package/dist.commonjs/btc/index.d.ts.map +1 -1
  20. package/dist.commonjs/btc/index.js +99 -76
  21. package/dist.commonjs/ckb/index.d.ts +33 -23
  22. package/dist.commonjs/ckb/index.d.ts.map +1 -1
  23. package/dist.commonjs/ckb/index.js +204 -135
  24. package/dist.commonjs/common/index.d.ts +34 -21
  25. package/dist.commonjs/common/index.js +50 -45
  26. package/dist.commonjs/connectionsStorage/index.d.ts +23 -14
  27. package/dist.commonjs/connectionsStorage/index.js +28 -25
  28. package/dist.commonjs/evm/index.d.ts +21 -16
  29. package/dist.commonjs/evm/index.d.ts.map +1 -1
  30. package/dist.commonjs/evm/index.js +89 -70
  31. package/dist.commonjs/index.d.ts +1 -1
  32. package/dist.commonjs/index.js +40 -19
  33. package/dist.commonjs/signerFactory/index.d.ts +6 -2
  34. package/dist.commonjs/signerFactory/index.d.ts.map +1 -1
  35. package/dist.commonjs/signerFactory/index.js +39 -26
  36. package/package.json +3 -3
  37. package/src/btc/index.ts +1 -12
  38. package/src/ckb/index.ts +55 -22
  39. package/src/evm/index.ts +1 -11
  40. package/src/signerFactory/index.ts +14 -5
  41. package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
@@ -6,83 +6,106 @@ const common_1 = require("@joyid/common");
6
6
  const common_2 = require("../common");
7
7
  const connectionsStorage_1 = require("../connectionsStorage");
8
8
  class BitcoinSigner extends core_1.ccc.SignerBtc {
9
- assertConnection() {
10
- if (!this.isConnected() || !this.connection) {
11
- throw new Error("Not connected");
12
- }
13
- return this.connection;
9
+ assertConnection() {
10
+ if (!this.isConnected() || !this.connection) {
11
+ throw new Error("Not connected");
14
12
  }
15
- constructor(client, name, icon, addressType = "auto", appUri = "https://app.joy.id", connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
16
- super(client);
17
- this.name = name;
18
- this.icon = icon;
19
- this.addressType = addressType;
20
- this.appUri = appUri;
21
- this.connectionsRepo = connectionsRepo;
22
- }
23
- async replaceClient(client) {
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
- };
34
- }
35
- async getBtcAccount() {
36
- const { address } = this.assertConnection();
37
- return address;
38
- }
39
- async getBtcPublicKey() {
40
- const { publicKey } = this.assertConnection();
41
- return publicKey;
42
- }
43
- async connect() {
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,
48
- });
49
- const { address, pubkey } = (() => {
50
- if (this.addressType === "auto") {
51
- return res.btcAddressType === "p2wpkh" ? res.nativeSegwit : res.taproot;
52
- }
53
- return res.btcAddressType === "p2wpkh" ? res.nativeSegwit : res.taproot;
54
- })();
55
- this.connection = {
56
- address,
57
- publicKey: core_1.ccc.hexFrom(pubkey),
58
- keyType: res.keyType,
59
- };
60
- await Promise.all([
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),
63
- ]);
64
- }
65
- async isConnected() {
66
- if (this.connection) {
67
- return true;
68
- }
69
- this.connection = await this.connectionsRepo.get({
70
- uri: this.appUri,
71
- addressType: `btc-${this.addressType}`,
72
- });
73
- return this.connection !== undefined;
74
- }
75
- async signMessage(message) {
76
- const { address } = this.assertConnection();
77
- const challenge = typeof message === "string" ? message : core_1.ccc.hexFrom(message).slice(2);
78
- const config = this.getConfig();
79
- const { signature } = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
80
- ...config,
81
- challenge,
82
- address,
83
- signMessageType: "ecdsa",
84
- }, "popup", "/sign-message"), { ...config, type: common_1.DappRequestType.SignMessage });
85
- return signature;
13
+ return this.connection;
14
+ }
15
+ constructor(
16
+ client,
17
+ name,
18
+ icon,
19
+ addressType = "auto",
20
+ appUri = "https://app.joy.id",
21
+ connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage(),
22
+ ) {
23
+ super(client);
24
+ this.name = name;
25
+ this.icon = icon;
26
+ this.addressType = addressType;
27
+ this.appUri = appUri;
28
+ this.connectionsRepo = connectionsRepo;
29
+ }
30
+ getConfig() {
31
+ return {
32
+ redirectURL: location.href,
33
+ joyidAppURL: this.appUri,
34
+ requestNetwork: `btc-${this.addressType}`,
35
+ name: this.name,
36
+ logo: this.icon,
37
+ };
38
+ }
39
+ async getBtcAccount() {
40
+ const { address } = this.assertConnection();
41
+ return address;
42
+ }
43
+ async getBtcPublicKey() {
44
+ const { publicKey } = this.assertConnection();
45
+ return publicKey;
46
+ }
47
+ async connect() {
48
+ const config = this.getConfig();
49
+ const res = await (0, common_2.createPopup)(
50
+ (0, common_1.buildJoyIDURL)(config, "popup", "/auth"),
51
+ {
52
+ ...config,
53
+ type: common_1.DappRequestType.Auth,
54
+ },
55
+ );
56
+ const { address, pubkey } = (() => {
57
+ if (this.addressType === "auto") {
58
+ return res.btcAddressType === "p2wpkh" ? res.nativeSegwit : res.taproot;
59
+ }
60
+ return res.btcAddressType === "p2wpkh" ? res.nativeSegwit : res.taproot;
61
+ })();
62
+ this.connection = {
63
+ address,
64
+ publicKey: core_1.ccc.hexFrom(pubkey),
65
+ keyType: res.keyType,
66
+ };
67
+ await Promise.all([
68
+ this.connectionsRepo.set(
69
+ { uri: this.appUri, addressType: `btc-${res.btcAddressType}` },
70
+ this.connection,
71
+ ),
72
+ this.connectionsRepo.set(
73
+ { uri: this.appUri, addressType: "btc-auto" },
74
+ this.connection,
75
+ ),
76
+ ]);
77
+ }
78
+ async isConnected() {
79
+ if (this.connection) {
80
+ return true;
86
81
  }
82
+ this.connection = await this.connectionsRepo.get({
83
+ uri: this.appUri,
84
+ addressType: `btc-${this.addressType}`,
85
+ });
86
+ return this.connection !== undefined;
87
+ }
88
+ async signMessageRaw(message) {
89
+ const { address } = this.assertConnection();
90
+ const challenge =
91
+ typeof message === "string"
92
+ ? message
93
+ : core_1.ccc.hexFrom(message).slice(2);
94
+ const config = this.getConfig();
95
+ const { signature } = await (0, common_2.createPopup)(
96
+ (0, common_1.buildJoyIDURL)(
97
+ {
98
+ ...config,
99
+ challenge,
100
+ address,
101
+ signMessageType: "ecdsa",
102
+ },
103
+ "popup",
104
+ "/sign-message",
105
+ ),
106
+ { ...config, type: common_1.DappRequestType.SignMessage },
107
+ );
108
+ return signature;
109
+ }
87
110
  }
88
111
  exports.BitcoinSigner = BitcoinSigner;
@@ -1,27 +1,37 @@
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 name;
5
- private readonly icon;
6
- private readonly _appUri?;
7
- private readonly _aggregatorUri?;
8
- private readonly connectionsRepo;
9
- private connection?;
10
- private assertConnection;
11
- constructor(client: ccc.Client, name: string, icon: string, _appUri?: string | undefined, _aggregatorUri?: string | undefined, connectionsRepo?: ConnectionsRepo);
12
- replaceClient(client: ccc.Client): Promise<CkbSigner>;
13
- private getConfig;
14
- private getAggregatorUri;
15
- connect(): Promise<void>;
16
- disconnect(): Promise<void>;
17
- isConnected(): Promise<boolean>;
18
- getInternalAddress(): Promise<string>;
19
- getAddressObj(): Promise<ccc.Address>;
20
- getAddressObjs(): Promise<ccc.Address[]>;
21
- prepareTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
22
- private prepareTransactionForSubKey;
23
- signOnlyTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
24
- private saveConnection;
25
- private restoreConnection;
4
+ private readonly name;
5
+ private readonly icon;
6
+ private readonly _appUri?;
7
+ private readonly _aggregatorUri?;
8
+ private readonly connectionsRepo;
9
+ get type(): ccc.SignerType;
10
+ get signType(): ccc.SignerSignType;
11
+ private connection?;
12
+ private assertConnection;
13
+ constructor(
14
+ client: ccc.Client,
15
+ name: string,
16
+ icon: string,
17
+ _appUri?: string | undefined,
18
+ _aggregatorUri?: string | undefined,
19
+ connectionsRepo?: ConnectionsRepo,
20
+ );
21
+ private getConfig;
22
+ private getAggregatorUri;
23
+ connect(): Promise<void>;
24
+ disconnect(): Promise<void>;
25
+ isConnected(): Promise<boolean>;
26
+ getInternalAddress(): Promise<string>;
27
+ getIdentity(): Promise<string>;
28
+ getAddressObj(): Promise<ccc.Address>;
29
+ getAddressObjs(): Promise<ccc.Address[]>;
30
+ prepareTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
31
+ private prepareTransactionForSubKey;
32
+ signOnlyTransaction(txLike: ccc.TransactionLike): Promise<ccc.Transaction>;
33
+ signMessageRaw(message: string | ccc.BytesLike): Promise<string>;
34
+ private saveConnection;
35
+ private restoreConnection;
26
36
  }
27
- //# sourceMappingURL=index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BAAkB,CACtB,MAAM,EAAE,GAAG,CAAC,eAAe,GAC1B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YAkBb,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"}
@@ -7,140 +7,209 @@ const common_1 = require("@joyid/common");
7
7
  const common_2 = require("../common");
8
8
  const connectionsStorage_1 = require("../connectionsStorage");
9
9
  class CkbSigner extends core_1.ccc.Signer {
10
- async assertConnection() {
11
- if (!(await this.isConnected()) || !this.connection) {
12
- throw new Error("Not connected");
13
- }
14
- return this.connection;
15
- }
16
- constructor(client, name, icon, _appUri, _aggregatorUri, connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage()) {
17
- super(client);
18
- this.name = name;
19
- this.icon = icon;
20
- this._appUri = _appUri;
21
- this._aggregatorUri = _aggregatorUri;
22
- this.connectionsRepo = connectionsRepo;
23
- }
24
- async replaceClient(client) {
25
- return new CkbSigner(client, this.name, this.icon, this._appUri, this._aggregatorUri, this.connectionsRepo);
26
- }
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
- };
37
- }
38
- async getAggregatorUri() {
39
- if (this._aggregatorUri) {
40
- return this._aggregatorUri;
41
- }
42
- return (await this.client.getAddressPrefix()) === "ckb"
43
- ? "https://cota.nervina.dev/mainnet-aggregator"
44
- : "https://cota.nervina.dev/aggregator";
45
- }
46
- async connect() {
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,
51
- });
52
- this.connection = {
53
- address: res.address,
54
- publicKey: core_1.ccc.hexFrom(res.pubkey),
55
- keyType: res.keyType,
56
- };
57
- await this.saveConnection();
58
- }
59
- async disconnect() {
60
- this.connection = undefined;
61
- await this.saveConnection();
62
- }
63
- async isConnected() {
64
- if (this.connection) {
65
- return true;
66
- }
67
- await this.restoreConnection();
68
- return this.connection !== undefined;
69
- }
70
- async getInternalAddress() {
71
- return (await this.assertConnection()).address;
72
- }
73
- async getAddressObj() {
74
- return await core_1.ccc.Address.fromString(await this.getInternalAddress(), this.client);
75
- }
76
- async getAddressObjs() {
77
- return [await this.getAddressObj()];
78
- }
79
- async prepareTransaction(txLike) {
80
- const tx = core_1.ccc.Transaction.from(txLike);
81
- const position = await tx.findInputIndexByLock((await this.getAddressObj()).script, this.client);
82
- if (position === undefined) {
83
- return tx;
84
- }
85
- const witness = tx.getWitnessArgsAt(position) ?? core_1.ccc.WitnessArgs.from({});
86
- witness.lock = "0x";
87
- await this.prepareTransactionForSubKey(tx, witness);
88
- return tx.setWitnessArgsAt(position, witness);
89
- }
90
- async prepareTransactionForSubKey(tx, witness) {
91
- if (this.connection?.keyType !== "sub_key") {
92
- return [];
93
- }
94
- const pubkeyHash = core_1.ccc.ckbHash(this.connection.publicKey).substring(0, 42);
95
- const lock = (await this.getAddressObj()).script;
96
- const aggregator = new ckb_1.Aggregator(await this.getAggregatorUri());
97
- const { unlock_entry: unlockEntry } = await aggregator.generateSubkeyUnlockSmt({
98
- alg_index: 1,
99
- pubkey_hash: pubkeyHash,
100
- lock_script: core_1.ccc.hexFrom(lock.toBytes()),
101
- });
102
- witness.outputType = core_1.ccc.hexFrom(unlockEntry);
103
- const cotaDeps = [];
104
- for await (const cell of this.client.findCellsByLockAndType(lock, {
105
- ...(await this.client.getKnownScript(core_1.ccc.KnownScript.COTA)),
106
- args: "0x",
107
- })) {
108
- cotaDeps.push(core_1.ccc.CellDep.from({
109
- depType: "code",
110
- outPoint: cell.outPoint,
111
- }));
112
- }
113
- if (cotaDeps.length === 0) {
114
- throw new Error("No COTA cells for sub key wallet");
115
- }
116
- tx.cellDeps.unshift(...cotaDeps);
117
- }
118
- async signOnlyTransaction(txLike) {
119
- const tx = core_1.ccc.Transaction.from(txLike);
120
- const { script } = await this.getAddressObj();
121
- const config = await this.getConfig();
122
- const res = await (0, common_2.createPopup)((0, common_1.buildJoyIDURL)({
123
- ...config,
124
- tx: JSON.parse(tx.stringify()),
125
- signerAddress: (await this.assertConnection()).address,
126
- witnessIndex: await tx.findInputIndexByLock(script, this.client),
127
- }, "popup", "/sign-ckb-raw-tx"), {
128
- ...config,
129
- type: common_1.DappRequestType.SignCkbRawTx,
130
- });
131
- return core_1.ccc.Transaction.from(res.tx);
132
- }
133
- async saveConnection() {
134
- return this.connectionsRepo.set({
135
- uri: (await this.getConfig()).joyidAppURL,
136
- addressType: "ckb",
137
- }, this.connection);
138
- }
139
- async restoreConnection() {
140
- this.connection = await this.connectionsRepo.get({
141
- uri: (await this.getConfig()).joyidAppURL,
142
- addressType: "ckb",
143
- });
144
- }
10
+ get type() {
11
+ return core_1.ccc.SignerType.CKB;
12
+ }
13
+ get signType() {
14
+ return core_1.ccc.SignerSignType.JoyId;
15
+ }
16
+ async assertConnection() {
17
+ if (!(await this.isConnected()) || !this.connection) {
18
+ throw new Error("Not connected");
19
+ }
20
+ return this.connection;
21
+ }
22
+ constructor(
23
+ client,
24
+ name,
25
+ icon,
26
+ _appUri,
27
+ _aggregatorUri,
28
+ connectionsRepo = new connectionsStorage_1.ConnectionsRepoLocalStorage(),
29
+ ) {
30
+ super(client);
31
+ this.name = name;
32
+ this.icon = icon;
33
+ this._appUri = _appUri;
34
+ this._aggregatorUri = _aggregatorUri;
35
+ this.connectionsRepo = connectionsRepo;
36
+ }
37
+ getConfig() {
38
+ return {
39
+ redirectURL: location.href,
40
+ joyidAppURL:
41
+ this._appUri ?? this.client.addressPrefix === "ckb"
42
+ ? "https://app.joy.id"
43
+ : "https://testnet.joyid.dev",
44
+ name: this.name,
45
+ logo: this.icon,
46
+ };
47
+ }
48
+ getAggregatorUri() {
49
+ if (this._aggregatorUri) {
50
+ return this._aggregatorUri;
51
+ }
52
+ return this.client.addressPrefix === "ckb"
53
+ ? "https://cota.nervina.dev/mainnet-aggregator"
54
+ : "https://cota.nervina.dev/aggregator";
55
+ }
56
+ async connect() {
57
+ const config = this.getConfig();
58
+ const res = await (0, common_2.createPopup)(
59
+ (0, common_1.buildJoyIDURL)(config, "popup", "/auth"),
60
+ {
61
+ ...config,
62
+ type: common_1.DappRequestType.Auth,
63
+ },
64
+ );
65
+ this.connection = {
66
+ address: res.address,
67
+ publicKey: core_1.ccc.hexFrom(res.pubkey),
68
+ keyType: res.keyType,
69
+ };
70
+ await this.saveConnection();
71
+ }
72
+ async disconnect() {
73
+ this.connection = undefined;
74
+ await this.saveConnection();
75
+ }
76
+ async isConnected() {
77
+ if (this.connection) {
78
+ return true;
79
+ }
80
+ await this.restoreConnection();
81
+ return this.connection !== undefined;
82
+ }
83
+ async getInternalAddress() {
84
+ return (await this.assertConnection()).address;
85
+ }
86
+ async getIdentity() {
87
+ const connection = await this.assertConnection();
88
+ return JSON.stringify({
89
+ keyType: connection.keyType,
90
+ publicKey: connection.publicKey.slice(2),
91
+ });
92
+ }
93
+ async getAddressObj() {
94
+ return await core_1.ccc.Address.fromString(
95
+ await this.getInternalAddress(),
96
+ this.client,
97
+ );
98
+ }
99
+ async getAddressObjs() {
100
+ return [await this.getAddressObj()];
101
+ }
102
+ async prepareTransaction(txLike) {
103
+ const tx = core_1.ccc.Transaction.from(txLike);
104
+ const position = await tx.findInputIndexByLock(
105
+ (await this.getAddressObj()).script,
106
+ this.client,
107
+ );
108
+ if (position === undefined) {
109
+ return tx;
110
+ }
111
+ const witness =
112
+ tx.getWitnessArgsAt(position) ?? core_1.ccc.WitnessArgs.from({});
113
+ witness.lock = "0x";
114
+ await this.prepareTransactionForSubKey(tx, witness);
115
+ tx.setWitnessArgsAt(position, witness);
116
+ return tx;
117
+ }
118
+ async prepareTransactionForSubKey(tx, witness) {
119
+ if (this.connection?.keyType !== "sub_key") {
120
+ return [];
121
+ }
122
+ const pubkeyHash = core_1.ccc
123
+ .ckbHash(this.connection.publicKey)
124
+ .substring(0, 42);
125
+ const lock = (await this.getAddressObj()).script;
126
+ const aggregator = new ckb_1.Aggregator(await this.getAggregatorUri());
127
+ const { unlock_entry: unlockEntry } =
128
+ await aggregator.generateSubkeyUnlockSmt({
129
+ alg_index: 1,
130
+ pubkey_hash: pubkeyHash,
131
+ lock_script: core_1.ccc.hexFrom(lock.toBytes()),
132
+ });
133
+ witness.outputType = core_1.ccc.hexFrom(unlockEntry);
134
+ const cotaDeps = [];
135
+ for await (const cell of this.client.findCellsByLockAndType(lock, {
136
+ ...(await this.client.getKnownScript(core_1.ccc.KnownScript.COTA)),
137
+ args: "0x",
138
+ })) {
139
+ cotaDeps.push(
140
+ core_1.ccc.CellDep.from({
141
+ depType: "code",
142
+ outPoint: cell.outPoint,
143
+ }),
144
+ );
145
+ }
146
+ if (cotaDeps.length === 0) {
147
+ throw new Error("No COTA cells for sub key wallet");
148
+ }
149
+ tx.cellDeps.unshift(...cotaDeps);
150
+ }
151
+ async signOnlyTransaction(txLike) {
152
+ const tx = core_1.ccc.Transaction.from(txLike);
153
+ const { script } = await this.getAddressObj();
154
+ const config = this.getConfig();
155
+ const res = await (0, common_2.createPopup)(
156
+ (0, common_1.buildJoyIDURL)(
157
+ {
158
+ ...config,
159
+ tx: JSON.parse(tx.stringify()),
160
+ signerAddress: (await this.assertConnection()).address,
161
+ witnessIndex: await tx.findInputIndexByLock(script, this.client),
162
+ },
163
+ "popup",
164
+ "/sign-ckb-raw-tx",
165
+ ),
166
+ {
167
+ ...config,
168
+ type: common_1.DappRequestType.SignCkbRawTx,
169
+ },
170
+ );
171
+ return core_1.ccc.Transaction.from(res.tx);
172
+ }
173
+ async signMessageRaw(message) {
174
+ const { address } = await this.assertConnection();
175
+ const challenge =
176
+ typeof message === "string"
177
+ ? message
178
+ : core_1.ccc.hexFrom(message).slice(2);
179
+ const config = this.getConfig();
180
+ const res = await (0, common_2.createPopup)(
181
+ (0, common_1.buildJoyIDURL)(
182
+ {
183
+ ...config,
184
+ challenge,
185
+ isData: typeof message !== "string",
186
+ address,
187
+ },
188
+ "popup",
189
+ "/sign-message",
190
+ ),
191
+ { ...config, type: common_1.DappRequestType.SignMessage },
192
+ );
193
+ return JSON.stringify({
194
+ signature: res.signature,
195
+ alg: res.alg,
196
+ message: res.message,
197
+ });
198
+ }
199
+ async saveConnection() {
200
+ return this.connectionsRepo.set(
201
+ {
202
+ uri: this.getConfig().joyidAppURL,
203
+ addressType: "ckb",
204
+ },
205
+ this.connection,
206
+ );
207
+ }
208
+ async restoreConnection() {
209
+ this.connection = await this.connectionsRepo.get({
210
+ uri: this.getConfig().joyidAppURL,
211
+ addressType: "ckb",
212
+ });
213
+ }
145
214
  }
146
215
  exports.CkbSigner = CkbSigner;