@gluwa/connect-kit 0.1.0-next.3 → 0.2.0-next.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.
- package/CHANGELOG.md +6 -0
- package/dist/credit-connect.js +148 -133
- package/dist/index.css +561 -0
- package/dist/index.d.ts +39 -25
- package/dist/index.js +315 -655
- package/dist/package.json +11 -6
- package/package.json +8 -5
- package/src/ConnectKitProvider.tsx +75 -49
- package/src/ConnectModal.tsx +39 -25
- package/src/api/account.ts +21 -0
- package/src/api/balance.ts +10 -0
- package/src/api/chain.ts +17 -0
- package/src/api/contract.ts +33 -0
- package/src/api/transaction.ts +12 -0
- package/src/components/QRFrame.tsx +2 -2
- package/src/core/config.ts +83 -0
- package/src/creditConnectConnector.ts +175 -159
- package/src/events/account.ts +49 -0
- package/src/hooks/useWCState.ts +24 -9
- package/src/hooks/useWagmiConnect.ts +13 -8
- package/src/index.ts +33 -0
- package/src/types.ts +9 -0
- package/src/utils/platform.ts +1 -4
- package/src/views/CreditWalletView.tsx +0 -1
- package/src/views/MetaMaskView.tsx +4 -2
- package/src/views/SwitchChainView.tsx +9 -4
- package/tsup.config.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/credit-connect.js
CHANGED
|
@@ -16,6 +16,7 @@ var creditConnectConnector = /* @__PURE__ */ __name((options) => {
|
|
|
16
16
|
let selectedChainId = null;
|
|
17
17
|
let connectPromise = null;
|
|
18
18
|
let provider = null;
|
|
19
|
+
let providerPromise = null;
|
|
19
20
|
let isLocalDisconnect = false;
|
|
20
21
|
let emitWagmiMessage = null;
|
|
21
22
|
let emitWagmiDisconnect = null;
|
|
@@ -87,6 +88,137 @@ var creditConnectConnector = /* @__PURE__ */ __name((options) => {
|
|
|
87
88
|
emitWagmiDisconnect = /* @__PURE__ */ __name(() => {
|
|
88
89
|
config.emitter.emit("disconnect");
|
|
89
90
|
}, "emitWagmiDisconnect");
|
|
91
|
+
const buildProvider = /* @__PURE__ */ __name(() => {
|
|
92
|
+
const createdProvider = {
|
|
93
|
+
request: /* @__PURE__ */ __name(async (args) => {
|
|
94
|
+
const { method, params: rawParams } = args;
|
|
95
|
+
const params = Array.isArray(rawParams) ? [
|
|
96
|
+
...rawParams
|
|
97
|
+
] : rawParams === void 0 ? [] : [
|
|
98
|
+
rawParams
|
|
99
|
+
];
|
|
100
|
+
const session = getCurrentSession();
|
|
101
|
+
const getSelectedAddressInfo = /* @__PURE__ */ __name((address) => {
|
|
102
|
+
if (!session) throw new Error("No active session");
|
|
103
|
+
const evmInfo = getEvmAddressInfo(session);
|
|
104
|
+
if (evmInfo.length === 0) throw new Error("No EVM address found");
|
|
105
|
+
if (!address) return evmInfo[0];
|
|
106
|
+
const found = evmInfo.find((info) => info.address.toLowerCase() === address.toLowerCase());
|
|
107
|
+
if (!found) throw new Error(`Address not in session: ${address}`);
|
|
108
|
+
return found;
|
|
109
|
+
}, "getSelectedAddressInfo");
|
|
110
|
+
if (method === "eth_accounts") {
|
|
111
|
+
if (!session) return [];
|
|
112
|
+
return getChecksummedAccounts(session);
|
|
113
|
+
}
|
|
114
|
+
if (method === "eth_chainId") {
|
|
115
|
+
if (!session) throw new Error("No active session");
|
|
116
|
+
if (selectedChainId !== null) return `0x${selectedChainId.toString(16)}`;
|
|
117
|
+
const evmInfo = getEvmAddressInfo(session);
|
|
118
|
+
if (evmInfo.length === 0) throw new Error("No EVM address found");
|
|
119
|
+
const chainId = Number.parseInt(evmInfo[0].networkIdentifier, 10);
|
|
120
|
+
if (!Number.isFinite(chainId)) throw new Error(`Invalid chainId: ${chainId}`);
|
|
121
|
+
selectedChainId = chainId;
|
|
122
|
+
return `0x${chainId.toString(16)}`;
|
|
123
|
+
}
|
|
124
|
+
if (method === "wallet_switchEthereumChain") {
|
|
125
|
+
const [{ chainId: rawChainId }] = params != null ? params : [];
|
|
126
|
+
if (!rawChainId) throw new Error("wallet_switchEthereumChain missing chainId param");
|
|
127
|
+
const chainId = Number.parseInt(rawChainId, 16);
|
|
128
|
+
if (!Number.isFinite(chainId)) throw new Error(`Invalid chainId: ${rawChainId}`);
|
|
129
|
+
if (!session) throw new Error("No active session");
|
|
130
|
+
const supported = getEvmAddressInfo(session).some((info) => Number.parseInt(info.networkIdentifier, 10) === chainId);
|
|
131
|
+
if (!supported) {
|
|
132
|
+
const err = new Error(`Unsupported chain: ${chainId}`);
|
|
133
|
+
err.code = 4902;
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
selectedChainId = chainId;
|
|
137
|
+
config.emitter.emit("change", {
|
|
138
|
+
chainId
|
|
139
|
+
});
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
if (method === "personal_sign") {
|
|
143
|
+
const [messageParam, addressParam] = params != null ? params : [];
|
|
144
|
+
const message = typeof messageParam === "string" ? messageParam : String(messageParam != null ? messageParam : "");
|
|
145
|
+
const address = typeof addressParam === "string" ? addressParam : void 0;
|
|
146
|
+
const addressInfo = getSelectedAddressInfo(address);
|
|
147
|
+
const response = await (session == null ? void 0 : session.jsonRpc.request("signMessage", {
|
|
148
|
+
message,
|
|
149
|
+
addressInfo
|
|
150
|
+
}));
|
|
151
|
+
return response == null ? void 0 : response.signature;
|
|
152
|
+
}
|
|
153
|
+
if (method === "eth_sign") {
|
|
154
|
+
const [addressParam, messageParam] = params != null ? params : [];
|
|
155
|
+
const address = typeof addressParam === "string" ? addressParam : void 0;
|
|
156
|
+
const message = typeof messageParam === "string" ? messageParam : String(messageParam != null ? messageParam : "");
|
|
157
|
+
const addressInfo = getSelectedAddressInfo(address);
|
|
158
|
+
const response = await (session == null ? void 0 : session.jsonRpc.request("signMessage", {
|
|
159
|
+
message,
|
|
160
|
+
addressInfo
|
|
161
|
+
}));
|
|
162
|
+
return response == null ? void 0 : response.signature;
|
|
163
|
+
}
|
|
164
|
+
if (method === "eth_signTypedData" || method === "eth_signTypedData_v4") {
|
|
165
|
+
const [addressParam, typedDataParam] = params != null ? params : [];
|
|
166
|
+
const address = typeof addressParam === "string" ? addressParam : void 0;
|
|
167
|
+
const typedData = typeof typedDataParam === "string" ? typedDataParam : JSON.stringify(typedDataParam != null ? typedDataParam : "");
|
|
168
|
+
const addressInfo = getSelectedAddressInfo(address);
|
|
169
|
+
const response = await (session == null ? void 0 : session.jsonRpc.request("signTypedData", {
|
|
170
|
+
typedData,
|
|
171
|
+
addressInfo
|
|
172
|
+
}));
|
|
173
|
+
return response == null ? void 0 : response.signature;
|
|
174
|
+
}
|
|
175
|
+
if (method === "eth_sendTransaction") {
|
|
176
|
+
const [txParams] = params != null ? params : [];
|
|
177
|
+
const addressInfo = getSelectedAddressInfo(txParams == null ? void 0 : txParams.from);
|
|
178
|
+
const response = await (session == null ? void 0 : session.jsonRpc.request("sendTransaction", {
|
|
179
|
+
addressInfo,
|
|
180
|
+
to: txParams == null ? void 0 : txParams.to,
|
|
181
|
+
value: txParams == null ? void 0 : txParams.value,
|
|
182
|
+
data: txParams == null ? void 0 : txParams.data,
|
|
183
|
+
gas: txParams == null ? void 0 : txParams.gas,
|
|
184
|
+
gasPrice: txParams == null ? void 0 : txParams.gasPrice,
|
|
185
|
+
maxFeePerGas: txParams == null ? void 0 : txParams.maxFeePerGas,
|
|
186
|
+
maxPriorityFeePerGas: txParams == null ? void 0 : txParams.maxPriorityFeePerGas,
|
|
187
|
+
nonce: txParams == null ? void 0 : txParams.nonce,
|
|
188
|
+
chainId: txParams == null ? void 0 : txParams.chainId
|
|
189
|
+
}));
|
|
190
|
+
return response == null ? void 0 : response.txHash;
|
|
191
|
+
}
|
|
192
|
+
if (method === "credit_connect_send_message") {
|
|
193
|
+
if (!session) throw new Error("No active session");
|
|
194
|
+
const [messageParam] = params != null ? params : [];
|
|
195
|
+
const message = typeof messageParam === "string" ? messageParam : JSON.stringify(messageParam != null ? messageParam : "");
|
|
196
|
+
await session.msg.sendMessage(message);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
if (method === "credit_connect_jsonrpc_ping") {
|
|
200
|
+
if (!session) throw new Error("No active session");
|
|
201
|
+
return await session.jsonRpc.request("pingPong", {});
|
|
202
|
+
}
|
|
203
|
+
throw new Error(`Unsupported provider method: ${method}`);
|
|
204
|
+
}, "request"),
|
|
205
|
+
on: /* @__PURE__ */ __name((event, listener) => {
|
|
206
|
+
let listeners = providerListeners.get(event);
|
|
207
|
+
if (!listeners) {
|
|
208
|
+
listeners = /* @__PURE__ */ new Set();
|
|
209
|
+
providerListeners.set(event, listeners);
|
|
210
|
+
}
|
|
211
|
+
listeners.add(listener);
|
|
212
|
+
return createdProvider;
|
|
213
|
+
}, "on"),
|
|
214
|
+
removeListener: /* @__PURE__ */ __name((event, listener) => {
|
|
215
|
+
var _a;
|
|
216
|
+
(_a = providerListeners.get(event)) == null ? void 0 : _a.delete(listener);
|
|
217
|
+
return createdProvider;
|
|
218
|
+
}, "removeListener")
|
|
219
|
+
};
|
|
220
|
+
return createdProvider;
|
|
221
|
+
}, "buildProvider");
|
|
90
222
|
return {
|
|
91
223
|
id: CONNECTOR_ID,
|
|
92
224
|
name: CONNECTOR_NAME,
|
|
@@ -191,12 +323,13 @@ var creditConnectConnector = /* @__PURE__ */ __name((options) => {
|
|
|
191
323
|
err.code = 4902;
|
|
192
324
|
throw err;
|
|
193
325
|
}
|
|
326
|
+
const chain = config.chains.find((c) => c.id === chainId);
|
|
327
|
+
if (!chain) throw new Error(`Chain ${chainId} not in config`);
|
|
194
328
|
selectedChainId = chainId;
|
|
195
329
|
config.emitter.emit("change", {
|
|
196
330
|
chainId
|
|
197
331
|
});
|
|
198
|
-
|
|
199
|
-
if (!chain) throw new Error(`Chain ${chainId} not in config`);
|
|
332
|
+
await Promise.resolve();
|
|
200
333
|
return chain;
|
|
201
334
|
},
|
|
202
335
|
async isAuthorized() {
|
|
@@ -215,6 +348,7 @@ var creditConnectConnector = /* @__PURE__ */ __name((options) => {
|
|
|
215
348
|
isLocalDisconnect = false;
|
|
216
349
|
}
|
|
217
350
|
provider = null;
|
|
351
|
+
providerPromise = null;
|
|
218
352
|
resetState();
|
|
219
353
|
config.emitter.emit("disconnect");
|
|
220
354
|
},
|
|
@@ -238,137 +372,18 @@ var creditConnectConnector = /* @__PURE__ */ __name((options) => {
|
|
|
238
372
|
},
|
|
239
373
|
async getProvider() {
|
|
240
374
|
if (provider) return provider;
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const evmInfo = getEvmAddressInfo(session);
|
|
254
|
-
if (evmInfo.length === 0) throw new Error("No EVM address found");
|
|
255
|
-
if (!address) return evmInfo[0];
|
|
256
|
-
const found = evmInfo.find((info) => info.address.toLowerCase() === address.toLowerCase());
|
|
257
|
-
if (!found) throw new Error(`Address not in session: ${address}`);
|
|
258
|
-
return found;
|
|
259
|
-
}, "getSelectedAddressInfo");
|
|
260
|
-
if (method === "eth_accounts") {
|
|
261
|
-
if (!session) return [];
|
|
262
|
-
return getChecksummedAccounts(session);
|
|
263
|
-
}
|
|
264
|
-
if (method === "eth_chainId") {
|
|
265
|
-
if (!session) throw new Error("No active session");
|
|
266
|
-
if (selectedChainId !== null) return `0x${selectedChainId.toString(16)}`;
|
|
267
|
-
const evmInfo = getEvmAddressInfo(session);
|
|
268
|
-
if (evmInfo.length === 0) throw new Error("No EVM address found");
|
|
269
|
-
const chainId = Number.parseInt(evmInfo[0].networkIdentifier, 10);
|
|
270
|
-
if (!Number.isFinite(chainId)) throw new Error(`Invalid chainId: ${chainId}`);
|
|
271
|
-
selectedChainId = chainId;
|
|
272
|
-
return `0x${chainId.toString(16)}`;
|
|
273
|
-
}
|
|
274
|
-
if (method === "wallet_switchEthereumChain") {
|
|
275
|
-
const [{ chainId: rawChainId }] = params != null ? params : [];
|
|
276
|
-
if (!rawChainId) throw new Error("wallet_switchEthereumChain missing chainId param");
|
|
277
|
-
const chainId = Number.parseInt(rawChainId, 16);
|
|
278
|
-
if (!Number.isFinite(chainId)) throw new Error(`Invalid chainId: ${rawChainId}`);
|
|
279
|
-
if (!session) throw new Error("No active session");
|
|
280
|
-
const supported = getEvmAddressInfo(session).some((info) => Number.parseInt(info.networkIdentifier, 10) === chainId);
|
|
281
|
-
if (!supported) {
|
|
282
|
-
const err = new Error(`Unsupported chain: ${chainId}`);
|
|
283
|
-
err.code = 4902;
|
|
284
|
-
throw err;
|
|
285
|
-
}
|
|
286
|
-
selectedChainId = chainId;
|
|
287
|
-
config.emitter.emit("change", {
|
|
288
|
-
chainId
|
|
289
|
-
});
|
|
290
|
-
return null;
|
|
291
|
-
}
|
|
292
|
-
if (method === "personal_sign") {
|
|
293
|
-
const [messageParam, addressParam] = params != null ? params : [];
|
|
294
|
-
const message = typeof messageParam === "string" ? messageParam : String(messageParam != null ? messageParam : "");
|
|
295
|
-
const address = typeof addressParam === "string" ? addressParam : void 0;
|
|
296
|
-
const addressInfo = getSelectedAddressInfo(address);
|
|
297
|
-
const response = await (session == null ? void 0 : session.jsonRpc.request("signMessage", {
|
|
298
|
-
message,
|
|
299
|
-
addressInfo
|
|
300
|
-
}));
|
|
301
|
-
return response == null ? void 0 : response.signature;
|
|
302
|
-
}
|
|
303
|
-
if (method === "eth_sign") {
|
|
304
|
-
const [addressParam, messageParam] = params != null ? params : [];
|
|
305
|
-
const address = typeof addressParam === "string" ? addressParam : void 0;
|
|
306
|
-
const message = typeof messageParam === "string" ? messageParam : String(messageParam != null ? messageParam : "");
|
|
307
|
-
const addressInfo = getSelectedAddressInfo(address);
|
|
308
|
-
const response = await (session == null ? void 0 : session.jsonRpc.request("signMessage", {
|
|
309
|
-
message,
|
|
310
|
-
addressInfo
|
|
311
|
-
}));
|
|
312
|
-
return response == null ? void 0 : response.signature;
|
|
313
|
-
}
|
|
314
|
-
if (method === "eth_signTypedData" || method === "eth_signTypedData_v4") {
|
|
315
|
-
const [addressParam, typedDataParam] = params != null ? params : [];
|
|
316
|
-
const address = typeof addressParam === "string" ? addressParam : void 0;
|
|
317
|
-
const typedData = typeof typedDataParam === "string" ? typedDataParam : JSON.stringify(typedDataParam != null ? typedDataParam : "");
|
|
318
|
-
const addressInfo = getSelectedAddressInfo(address);
|
|
319
|
-
const response = await (session == null ? void 0 : session.jsonRpc.request("signTypedData", {
|
|
320
|
-
typedData,
|
|
321
|
-
addressInfo
|
|
322
|
-
}));
|
|
323
|
-
return response == null ? void 0 : response.signature;
|
|
324
|
-
}
|
|
325
|
-
if (method === "eth_sendTransaction") {
|
|
326
|
-
const [txParams] = params != null ? params : [];
|
|
327
|
-
const addressInfo = getSelectedAddressInfo(txParams == null ? void 0 : txParams.from);
|
|
328
|
-
const response = await (session == null ? void 0 : session.jsonRpc.request("sendTransaction", {
|
|
329
|
-
addressInfo,
|
|
330
|
-
to: txParams == null ? void 0 : txParams.to,
|
|
331
|
-
value: txParams == null ? void 0 : txParams.value,
|
|
332
|
-
data: txParams == null ? void 0 : txParams.data,
|
|
333
|
-
gas: txParams == null ? void 0 : txParams.gas,
|
|
334
|
-
gasPrice: txParams == null ? void 0 : txParams.gasPrice,
|
|
335
|
-
maxFeePerGas: txParams == null ? void 0 : txParams.maxFeePerGas,
|
|
336
|
-
maxPriorityFeePerGas: txParams == null ? void 0 : txParams.maxPriorityFeePerGas,
|
|
337
|
-
nonce: txParams == null ? void 0 : txParams.nonce,
|
|
338
|
-
chainId: txParams == null ? void 0 : txParams.chainId
|
|
339
|
-
}));
|
|
340
|
-
return response == null ? void 0 : response.txHash;
|
|
341
|
-
}
|
|
342
|
-
if (method === "credit_connect_send_message") {
|
|
343
|
-
if (!session) throw new Error("No active session");
|
|
344
|
-
const [messageParam] = params != null ? params : [];
|
|
345
|
-
const message = typeof messageParam === "string" ? messageParam : JSON.stringify(messageParam != null ? messageParam : "");
|
|
346
|
-
await session.msg.sendMessage(message);
|
|
347
|
-
return true;
|
|
348
|
-
}
|
|
349
|
-
if (method === "credit_connect_jsonrpc_ping") {
|
|
350
|
-
if (!session) throw new Error("No active session");
|
|
351
|
-
return await session.jsonRpc.request("pingPong", {});
|
|
352
|
-
}
|
|
353
|
-
throw new Error(`Unsupported provider method: ${method}`);
|
|
354
|
-
}, "request"),
|
|
355
|
-
on: /* @__PURE__ */ __name((event, listener) => {
|
|
356
|
-
let listeners = providerListeners.get(event);
|
|
357
|
-
if (!listeners) {
|
|
358
|
-
listeners = /* @__PURE__ */ new Set();
|
|
359
|
-
providerListeners.set(event, listeners);
|
|
360
|
-
}
|
|
361
|
-
listeners.add(listener);
|
|
362
|
-
return createdProvider;
|
|
363
|
-
}, "on"),
|
|
364
|
-
removeListener: /* @__PURE__ */ __name((event, listener) => {
|
|
365
|
-
var _a;
|
|
366
|
-
(_a = providerListeners.get(event)) == null ? void 0 : _a.delete(listener);
|
|
367
|
-
return createdProvider;
|
|
368
|
-
}, "removeListener")
|
|
369
|
-
};
|
|
370
|
-
provider = createdProvider;
|
|
371
|
-
return provider;
|
|
375
|
+
if (providerPromise) return providerPromise;
|
|
376
|
+
providerPromise = (async () => {
|
|
377
|
+
await ensureManagerInitialized();
|
|
378
|
+
const createdProvider = buildProvider();
|
|
379
|
+
provider = createdProvider;
|
|
380
|
+
return createdProvider;
|
|
381
|
+
})();
|
|
382
|
+
try {
|
|
383
|
+
return await providerPromise;
|
|
384
|
+
} finally {
|
|
385
|
+
providerPromise = null;
|
|
386
|
+
}
|
|
372
387
|
},
|
|
373
388
|
onAccountsChanged(accounts) {
|
|
374
389
|
if (!accounts || accounts.length === 0) {
|