@ckb-ccc/joy-id 0.0.5-alpha.0 → 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.
- package/dist/advancedBarrel.d.ts +1 -0
- package/dist/advancedBarrel.d.ts.map +1 -1
- package/dist/advancedBarrel.js +1 -0
- package/dist/barrel.d.ts +1 -0
- package/dist/barrel.d.ts.map +1 -1
- package/dist/barrel.js +1 -0
- package/dist/btc/index.d.ts +6 -4
- package/dist/btc/index.d.ts.map +1 -1
- package/dist/btc/index.js +26 -18
- package/dist/ckb/index.d.ts +9 -4
- package/dist/ckb/index.d.ts.map +1 -1
- package/dist/ckb/index.js +59 -33
- package/dist/common/index.d.ts +24 -0
- package/dist/common/index.d.ts.map +1 -0
- package/dist/common/index.js +50 -0
- package/dist/evm/index.d.ts +19 -0
- package/dist/evm/index.d.ts.map +1 -0
- package/dist/evm/index.js +75 -0
- package/dist/signerFactory/index.d.ts +1 -1
- package/dist/signerFactory/index.d.ts.map +1 -1
- package/dist/signerFactory/index.js +17 -9
- package/dist.commonjs/advanced.d.ts +1 -1
- package/dist.commonjs/advanced.js +40 -19
- package/dist.commonjs/advancedBarrel.d.ts +2 -1
- package/dist.commonjs/advancedBarrel.d.ts.map +1 -1
- package/dist.commonjs/advancedBarrel.js +31 -14
- package/dist.commonjs/barrel.d.ts +2 -1
- package/dist.commonjs/barrel.d.ts.map +1 -1
- package/dist.commonjs/barrel.js +31 -14
- package/dist.commonjs/btc/index.d.ts +22 -13
- package/dist.commonjs/btc/index.d.ts.map +1 -1
- package/dist.commonjs/btc/index.js +100 -66
- package/dist.commonjs/ckb/index.d.ts +33 -21
- package/dist.commonjs/ckb/index.d.ts.map +1 -1
- package/dist.commonjs/ckb/index.js +205 -136
- package/dist.commonjs/common/index.d.ts +37 -0
- package/dist.commonjs/common/index.d.ts.map +1 -0
- package/dist.commonjs/common/index.js +59 -0
- package/dist.commonjs/connectionsStorage/index.d.ts +23 -14
- package/dist.commonjs/connectionsStorage/index.js +28 -25
- package/dist.commonjs/evm/index.d.ts +25 -0
- package/dist.commonjs/evm/index.d.ts.map +1 -0
- package/dist.commonjs/evm/index.js +101 -0
- package/dist.commonjs/index.d.ts +1 -1
- package/dist.commonjs/index.js +40 -19
- package/dist.commonjs/signerFactory/index.d.ts +6 -2
- package/dist.commonjs/signerFactory/index.d.ts.map +1 -1
- package/dist.commonjs/signerFactory/index.js +41 -22
- package/package.json +3 -3
- package/src/advancedBarrel.ts +1 -0
- package/src/barrel.ts +1 -0
- package/src/btc/index.ts +35 -20
- package/src/ckb/index.ts +84 -48
- package/src/common/index.ts +100 -0
- package/src/evm/index.ts +108 -0
- package/src/signerFactory/index.ts +28 -9
- package/dist.commonjs/tsconfig.commonjs.tsbuildinfo +0 -1
|
@@ -4,143 +4,212 @@ 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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthResponseData,
|
|
3
|
+
DappRequestType,
|
|
4
|
+
EvmWeb2LoginResponse,
|
|
5
|
+
PopupConfigOptions,
|
|
6
|
+
SignCkbTxResponseData,
|
|
7
|
+
SignCotaNFTResponseData,
|
|
8
|
+
SignEvmTxResponseData,
|
|
9
|
+
SignMessageResponseData,
|
|
10
|
+
SignNostrEventData,
|
|
11
|
+
} from "@joyid/common";
|
|
12
|
+
export interface PopupReturnType {
|
|
13
|
+
[DappRequestType.Auth]: AuthResponseData;
|
|
14
|
+
[DappRequestType.SignMessage]: SignMessageResponseData;
|
|
15
|
+
[DappRequestType.SignEvm]: SignEvmTxResponseData;
|
|
16
|
+
[DappRequestType.SignPsbt]: SignEvmTxResponseData;
|
|
17
|
+
[DappRequestType.BatchSignPsbt]: {
|
|
18
|
+
psbts: string[];
|
|
19
|
+
};
|
|
20
|
+
[DappRequestType.SignCkbTx]: SignCkbTxResponseData;
|
|
21
|
+
[DappRequestType.SignCotaNFT]: SignCotaNFTResponseData;
|
|
22
|
+
[DappRequestType.SignCkbRawTx]: SignCkbTxResponseData;
|
|
23
|
+
[DappRequestType.SignNostrEvent]: SignNostrEventData;
|
|
24
|
+
[DappRequestType.EncryptNostrMessage]: any;
|
|
25
|
+
[DappRequestType.DecryptNostrMessage]: any;
|
|
26
|
+
[DappRequestType.AuthMiniApp]: any;
|
|
27
|
+
[DappRequestType.SignMiniAppEvm]: any;
|
|
28
|
+
[DappRequestType.SignMiniAppMessage]: any;
|
|
29
|
+
[DappRequestType.EvmWeb2Login]: EvmWeb2LoginResponse;
|
|
30
|
+
}
|
|
31
|
+
export declare function createPopup<T extends DappRequestType>(
|
|
32
|
+
url: string,
|
|
33
|
+
config: PopupConfigOptions<T> & {
|
|
34
|
+
joyidAppURL: string;
|
|
35
|
+
},
|
|
36
|
+
): Promise<PopupReturnType[T]>;
|
|
37
|
+
//# 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,59 @@
|
|
|
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 () =>
|
|
10
|
+
createPopup(url, config),
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
config.popup.location.href = url;
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
if ((0, common_1.isStandaloneBrowser)()) {
|
|
17
|
+
reject(new common_1.PopupNotSupportedError(config.popup));
|
|
18
|
+
}
|
|
19
|
+
let popupEventListener;
|
|
20
|
+
let timeoutId;
|
|
21
|
+
// Check each second if the popup is closed triggering a PopupCancelledError
|
|
22
|
+
const popupTimer = setInterval(() => {
|
|
23
|
+
if (config.popup?.closed) {
|
|
24
|
+
clearInterval(popupTimer);
|
|
25
|
+
clearTimeout(timeoutId);
|
|
26
|
+
window.removeEventListener("message", popupEventListener, false);
|
|
27
|
+
reject(new common_1.PopupCancelledError(config.popup));
|
|
28
|
+
}
|
|
29
|
+
}, 1000);
|
|
30
|
+
timeoutId = setTimeout(
|
|
31
|
+
() => {
|
|
32
|
+
clearInterval(popupTimer);
|
|
33
|
+
reject(new common_1.PopupTimeoutError(config.popup));
|
|
34
|
+
window.removeEventListener("message", popupEventListener, false);
|
|
35
|
+
},
|
|
36
|
+
(config.timeoutInSeconds ?? 3000) * 1000,
|
|
37
|
+
);
|
|
38
|
+
popupEventListener = (e) => {
|
|
39
|
+
const { joyidAppURL } = config;
|
|
40
|
+
const appURL = new URL(joyidAppURL);
|
|
41
|
+
if (e.origin !== appURL.origin) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!e.data || e.data?.type !== config.type) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
clearTimeout(timeoutId);
|
|
48
|
+
clearInterval(popupTimer);
|
|
49
|
+
window.removeEventListener("message", popupEventListener, false);
|
|
50
|
+
config.popup.close();
|
|
51
|
+
if (e.data.error) {
|
|
52
|
+
reject(new Error(e.data.error));
|
|
53
|
+
}
|
|
54
|
+
resolve(e.data.data);
|
|
55
|
+
};
|
|
56
|
+
window.addEventListener("message", popupEventListener);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
exports.createPopup = createPopup;
|
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
import { ccc } from "@ckb-ccc/core";
|
|
2
2
|
export type AccountSelector = {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
uri: string;
|
|
4
|
+
addressType: string;
|
|
5
5
|
};
|
|
6
|
-
export declare function isSelectorEq(
|
|
6
|
+
export declare function isSelectorEq(
|
|
7
|
+
a: AccountSelector,
|
|
8
|
+
b: AccountSelector,
|
|
9
|
+
): boolean;
|
|
7
10
|
export type Connection = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
readonly address: string;
|
|
12
|
+
readonly publicKey: ccc.Hex;
|
|
13
|
+
readonly keyType: string;
|
|
11
14
|
};
|
|
12
15
|
export interface ConnectionsRepo {
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
get(selector: AccountSelector): Promise<Connection | undefined>;
|
|
17
|
+
set(
|
|
18
|
+
selector: AccountSelector,
|
|
19
|
+
connection: Connection | undefined,
|
|
20
|
+
): Promise<void>;
|
|
15
21
|
}
|
|
16
22
|
export declare class ConnectionsRepoLocalStorage implements ConnectionsRepo {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
private readonly storageKey;
|
|
24
|
+
constructor(storageKey?: string);
|
|
25
|
+
readConnections(): Promise<[AccountSelector, Connection][]>;
|
|
26
|
+
get(selector: AccountSelector): Promise<Connection | undefined>;
|
|
27
|
+
set(
|
|
28
|
+
selector: AccountSelector,
|
|
29
|
+
connection: Connection | undefined,
|
|
30
|
+
): Promise<void>;
|
|
22
31
|
}
|
|
23
|
-
//# sourceMappingURL=index.d.ts.map
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -2,34 +2,37 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConnectionsRepoLocalStorage = exports.isSelectorEq = void 0;
|
|
4
4
|
function isSelectorEq(a, b) {
|
|
5
|
-
|
|
5
|
+
return a.uri === b.uri && a.addressType === b.addressType;
|
|
6
6
|
}
|
|
7
7
|
exports.isSelectorEq = isSelectorEq;
|
|
8
8
|
class ConnectionsRepoLocalStorage {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
9
|
+
constructor(storageKey = "ccc-joy-id-signer") {
|
|
10
|
+
this.storageKey = storageKey;
|
|
11
|
+
}
|
|
12
|
+
async readConnections() {
|
|
13
|
+
return JSON.parse(window.localStorage.getItem(this.storageKey) ?? "[]");
|
|
14
|
+
}
|
|
15
|
+
async get(selector) {
|
|
16
|
+
return (await this.readConnections()).find(([s]) =>
|
|
17
|
+
isSelectorEq(selector, s),
|
|
18
|
+
)?.[1];
|
|
19
|
+
}
|
|
20
|
+
async set(selector, connection) {
|
|
21
|
+
const connections = await this.readConnections();
|
|
22
|
+
if (connection) {
|
|
23
|
+
const existed = connections.find(([s]) => isSelectorEq(s, selector));
|
|
24
|
+
if (existed) {
|
|
25
|
+
existed[1] = connection;
|
|
26
|
+
} else {
|
|
27
|
+
connections.push([selector, connection]);
|
|
28
|
+
}
|
|
29
|
+
window.localStorage.setItem(this.storageKey, JSON.stringify(connections));
|
|
30
|
+
} else {
|
|
31
|
+
window.localStorage.setItem(
|
|
32
|
+
this.storageKey,
|
|
33
|
+
JSON.stringify(connections.filter(([s]) => !isSelectorEq(s, selector))),
|
|
34
|
+
);
|
|
33
35
|
}
|
|
36
|
+
}
|
|
34
37
|
}
|
|
35
38
|
exports.ConnectionsRepoLocalStorage = ConnectionsRepoLocalStorage;
|
|
@@ -0,0 +1,25 @@
|
|
|
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(
|
|
11
|
+
client: ccc.Client,
|
|
12
|
+
name: string,
|
|
13
|
+
icon: string,
|
|
14
|
+
appUri?: string,
|
|
15
|
+
connectionsRepo?: ConnectionsRepo,
|
|
16
|
+
);
|
|
17
|
+
private getConfig;
|
|
18
|
+
getEvmAccount(): Promise<string>;
|
|
19
|
+
connect(): Promise<void>;
|
|
20
|
+
isConnected(): Promise<boolean>;
|
|
21
|
+
signMessageRaw(message: string | ccc.BytesLike): Promise<ccc.Hex>;
|
|
22
|
+
private saveConnection;
|
|
23
|
+
private restoreConnection;
|
|
24
|
+
}
|
|
25
|
+
//# 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;IAKvF,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"}
|