@getpara/cosmos-wallet-connectors 3.0.0 → 3.2.0
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.
|
@@ -26,10 +26,12 @@ import {
|
|
|
26
26
|
defaultCosmosExternalWallet
|
|
27
27
|
} from "@getpara/react-common";
|
|
28
28
|
import { formatEthHexAddress } from "../utils/formatEthHexAddress.js";
|
|
29
|
+
import { COSMOS_WALLET_SIGNING_UNSUPPORTED_ERROR, signCosmosWalletMessage } from "../utils/cosmosMessageSigning.js";
|
|
29
30
|
import { externalHooks } from "./externalHooks.js";
|
|
30
31
|
const CosmosExternalWalletContext = createContext(
|
|
31
32
|
defaultCosmosExternalWallet
|
|
32
33
|
);
|
|
34
|
+
const normalizeWalletIdentifier = (identifier) => identifier == null ? void 0 : identifier.toLowerCase();
|
|
33
35
|
function CosmosExternalWalletProvider({
|
|
34
36
|
children,
|
|
35
37
|
onSwitchWallet,
|
|
@@ -67,6 +69,53 @@ function CosmosExternalWalletProvider({
|
|
|
67
69
|
const wasConnectedRef = useRef(isConnected);
|
|
68
70
|
const disconnectionCallbackRef = useRef(null);
|
|
69
71
|
const walletChangeCallbackRef = useRef(null);
|
|
72
|
+
const injectedWallets = useMemo(
|
|
73
|
+
() => WALLET_TYPES.filter((w) => !incompleteWallets.some((iw) => iw.grazType === w) && checkWallet(w)).map((w) => {
|
|
74
|
+
const wallet = grazGetWallet(w);
|
|
75
|
+
if (!wallet.eip6963ProviderInfo) {
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
const eipInfo = wallet.eip6963ProviderInfo;
|
|
79
|
+
return __spreadValues({
|
|
80
|
+
grazType: w,
|
|
81
|
+
// Using name here for the injected connector since that's the only common id across the networks
|
|
82
|
+
id: eipInfo.name,
|
|
83
|
+
internalId: eipInfo.name,
|
|
84
|
+
iconUrl: eipInfo.icon
|
|
85
|
+
}, eipInfo);
|
|
86
|
+
}).filter((w) => !!w),
|
|
87
|
+
[incompleteWallets]
|
|
88
|
+
);
|
|
89
|
+
const allWallets = useMemo(
|
|
90
|
+
() => [...incompleteWallets, ...injectedWallets],
|
|
91
|
+
[incompleteWallets, injectedWallets]
|
|
92
|
+
);
|
|
93
|
+
const getWallet = useCallback(
|
|
94
|
+
(walletType2) => allWallets.find((w) => w.grazType === walletType2 || w.grazMobileType === walletType2),
|
|
95
|
+
[allWallets]
|
|
96
|
+
);
|
|
97
|
+
const getProvider = useCallback(
|
|
98
|
+
(walletType2) => {
|
|
99
|
+
const wallet = getWallet(walletType2);
|
|
100
|
+
return {
|
|
101
|
+
provider: wallet == null ? void 0 : wallet.name,
|
|
102
|
+
providerId: wallet == null ? void 0 : wallet.id
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
[getWallet]
|
|
106
|
+
);
|
|
107
|
+
const getExternalWalletMetadata = useCallback(
|
|
108
|
+
(externalWallet) => {
|
|
109
|
+
const identifiers = [externalWallet.providerId, externalWallet.provider].map(normalizeWalletIdentifier);
|
|
110
|
+
return allWallets.find((wallet) => {
|
|
111
|
+
const walletIdentifiers = [wallet.id, wallet.internalId, wallet.name, wallet.grazType, wallet.grazMobileType].map(
|
|
112
|
+
normalizeWalletIdentifier
|
|
113
|
+
);
|
|
114
|
+
return identifiers.some((identifier) => identifier && walletIdentifiers.includes(identifier));
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
[allWallets]
|
|
118
|
+
);
|
|
70
119
|
useEffect(() => {
|
|
71
120
|
var _a2;
|
|
72
121
|
if (walletChangeCallbackRef.current && (account == null ? void 0 : account[selectedChainId])) {
|
|
@@ -154,49 +203,63 @@ function CosmosExternalWalletProvider({
|
|
|
154
203
|
});
|
|
155
204
|
initializeConfig();
|
|
156
205
|
}, [para.isSetup]);
|
|
157
|
-
const switchChain = (
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
206
|
+
const switchChain = useCallback(
|
|
207
|
+
(chainId) => __async(this, null, function* () {
|
|
208
|
+
var _a2;
|
|
209
|
+
let error;
|
|
210
|
+
let changeResp = {};
|
|
211
|
+
try {
|
|
212
|
+
let chainInfo;
|
|
213
|
+
if (shouldUseSuggestChainAndConnect) {
|
|
214
|
+
chainInfo = getChainInfo({ chainId });
|
|
215
|
+
if (!chainInfo) {
|
|
216
|
+
console.error("Chain not found.");
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const connectedWallet = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType, chainInfo }) : connectAsync({ walletType, chainId });
|
|
221
|
+
changeResp.address = connectedWallet.accounts[chainId].bech32Address;
|
|
222
|
+
changeResp.ethAddress = formatEthHexAddress(connectedWallet.accounts[chainId].address);
|
|
223
|
+
} catch (err) {
|
|
224
|
+
if (err.message === "No wallet exists") {
|
|
225
|
+
changeResp.error = err.message;
|
|
226
|
+
} else {
|
|
227
|
+
console.error("Graz connection error:", err);
|
|
228
|
+
changeResp.error = "An unknown error occurred.";
|
|
168
229
|
}
|
|
169
230
|
}
|
|
170
|
-
|
|
171
|
-
changeResp.
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
231
|
+
onSwitchWallet(changeResp);
|
|
232
|
+
if (!changeResp.error) {
|
|
233
|
+
onSwitchChain(chainId);
|
|
234
|
+
const storedExternalWallet = para.externalWallets[(_a2 = changeResp.ethAddress) != null ? _a2 : ""];
|
|
235
|
+
const { provider, providerId } = getProvider(walletType);
|
|
236
|
+
para.setExternalWallet({
|
|
237
|
+
partnerId: para.partnerId,
|
|
238
|
+
address: changeResp.ethAddress,
|
|
239
|
+
type: "COSMOS",
|
|
240
|
+
provider,
|
|
241
|
+
providerId,
|
|
242
|
+
addressBech32: changeResp.address,
|
|
243
|
+
withFullParaAuth: storedExternalWallet.isExternalWithParaAuth,
|
|
244
|
+
withVerification: includeWalletVerification,
|
|
245
|
+
isConnectionOnly: connectionOnly
|
|
246
|
+
});
|
|
179
247
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
isConnectionOnly: connectionOnly
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
return { error };
|
|
199
|
-
});
|
|
248
|
+
return { error };
|
|
249
|
+
}),
|
|
250
|
+
[
|
|
251
|
+
connectAsync,
|
|
252
|
+
connectionOnly,
|
|
253
|
+
getProvider,
|
|
254
|
+
includeWalletVerification,
|
|
255
|
+
onSwitchChain,
|
|
256
|
+
onSwitchWallet,
|
|
257
|
+
para,
|
|
258
|
+
shouldUseSuggestChainAndConnect,
|
|
259
|
+
suggestAndConnectAsync,
|
|
260
|
+
walletType
|
|
261
|
+
]
|
|
262
|
+
);
|
|
200
263
|
const switchAccount = useCallback(
|
|
201
264
|
(connectorName) => __async(this, null, function* () {
|
|
202
265
|
const chainId = multiChain ? chains.map((c) => c.chainId) : selectedChainId;
|
|
@@ -205,187 +268,212 @@ function CosmosExternalWalletProvider({
|
|
|
205
268
|
chainId
|
|
206
269
|
});
|
|
207
270
|
}),
|
|
208
|
-
[connectAsync]
|
|
271
|
+
[chains, connectAsync, multiChain, selectedChainId]
|
|
209
272
|
);
|
|
210
|
-
const signMessageBase = (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
cosmosPublicKeyHex: Buffer.from(publicKey).toString("hex"),
|
|
223
|
-
cosmosSigner: bech32Address
|
|
224
|
-
};
|
|
225
|
-
});
|
|
226
|
-
const signMessage = (_0) => __async(this, [_0], function* ({ message, externalWallet }) {
|
|
227
|
-
let wallet, bech32Address, ethAddress;
|
|
228
|
-
if (externalWallet) {
|
|
229
|
-
const commonWallet = wallets.find((w) => w.id === externalWallet.providerId);
|
|
230
|
-
wallet = grazGetWallet(commonWallet == null ? void 0 : commonWallet.grazType);
|
|
231
|
-
bech32Address = externalWallet.addressBech32;
|
|
232
|
-
ethAddress = externalWallet.address;
|
|
233
|
-
} else {
|
|
234
|
-
wallet = grazGetWallet(walletType);
|
|
235
|
-
bech32Address = bech32AddressRef.current;
|
|
236
|
-
ethAddress = ethAddressRef.current;
|
|
237
|
-
}
|
|
238
|
-
if (!wallet) {
|
|
239
|
-
return { error: "Connected wallet not found" };
|
|
240
|
-
}
|
|
241
|
-
try {
|
|
242
|
-
return yield signMessageBase({
|
|
243
|
-
message,
|
|
273
|
+
const signMessageBase = useCallback(
|
|
274
|
+
(_0) => __async(this, [_0], function* ({
|
|
275
|
+
message,
|
|
276
|
+
wallet,
|
|
277
|
+
bech32Address,
|
|
278
|
+
ethAddress
|
|
279
|
+
}) {
|
|
280
|
+
if (!selectedChainId) {
|
|
281
|
+
throw new Error("No selected chain found");
|
|
282
|
+
}
|
|
283
|
+
const signedMessage = yield signCosmosWalletMessage({
|
|
284
|
+
chainId: selectedChainId,
|
|
244
285
|
wallet,
|
|
245
286
|
bech32Address,
|
|
246
|
-
|
|
287
|
+
message
|
|
247
288
|
});
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
289
|
+
return {
|
|
290
|
+
address: ethAddress,
|
|
291
|
+
addressBech32: bech32Address,
|
|
292
|
+
signature: signedMessage.signature,
|
|
293
|
+
cosmosPublicKeyHex: signedMessage.cosmosPublicKeyHex,
|
|
294
|
+
cosmosSigner: bech32Address
|
|
295
|
+
};
|
|
296
|
+
}),
|
|
297
|
+
[selectedChainId]
|
|
298
|
+
);
|
|
299
|
+
const signMessage = useCallback(
|
|
300
|
+
(_0) => __async(this, [_0], function* ({ message, externalWallet }) {
|
|
301
|
+
let wallet, bech32Address, ethAddress;
|
|
302
|
+
if (externalWallet) {
|
|
303
|
+
const commonWallet = getExternalWalletMetadata(externalWallet);
|
|
304
|
+
wallet = commonWallet ? grazGetWallet(commonWallet.grazType) : void 0;
|
|
305
|
+
bech32Address = externalWallet.addressBech32;
|
|
306
|
+
ethAddress = externalWallet.address;
|
|
307
|
+
} else {
|
|
308
|
+
wallet = grazGetWallet(walletType);
|
|
309
|
+
bech32Address = bech32AddressRef.current;
|
|
310
|
+
ethAddress = ethAddressRef.current;
|
|
251
311
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const walletId = (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.id;
|
|
269
|
-
const isFullAuthWallet = walletsWithFullAuth === "ALL" || walletsWithFullAuth.includes(walletId.toUpperCase());
|
|
270
|
-
let chainInfo;
|
|
271
|
-
if (shouldUseSuggestChainAndConnect) {
|
|
272
|
-
if (typeof _chainId !== "string") {
|
|
273
|
-
console.error("multiChain is not compatible with shouldUseSuggestChainAndConnect.");
|
|
274
|
-
return;
|
|
312
|
+
if (!wallet) {
|
|
313
|
+
return { error: "Connected wallet not found" };
|
|
314
|
+
}
|
|
315
|
+
if (!bech32Address || !ethAddress) {
|
|
316
|
+
return { error: "Connected wallet address not found" };
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
return yield signMessageBase({
|
|
320
|
+
message,
|
|
321
|
+
wallet,
|
|
322
|
+
bech32Address,
|
|
323
|
+
ethAddress
|
|
324
|
+
});
|
|
325
|
+
} catch (e) {
|
|
326
|
+
if (e.message.includes("Request rejected")) {
|
|
327
|
+
return { error: "Signature request rejected" };
|
|
275
328
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
console.error("Chain not found.");
|
|
279
|
-
return;
|
|
329
|
+
if (e.message === COSMOS_WALLET_SIGNING_UNSUPPORTED_ERROR) {
|
|
330
|
+
return { error: e.message };
|
|
280
331
|
}
|
|
332
|
+
return { error: "An unknown error occurred" };
|
|
281
333
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
} else {
|
|
291
|
-
rawAddress = accountAddress;
|
|
334
|
+
}),
|
|
335
|
+
[getExternalWalletMetadata, signMessageBase, walletType]
|
|
336
|
+
);
|
|
337
|
+
const connectBase = useCallback(
|
|
338
|
+
(walletType2, chainId) => __async(this, null, function* () {
|
|
339
|
+
var _a2, _b2;
|
|
340
|
+
if (!chainId) {
|
|
341
|
+
yield disconnectAsync();
|
|
292
342
|
}
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
addressBech32,
|
|
300
|
-
provider,
|
|
301
|
-
providerId,
|
|
302
|
-
withFullParaAuth: isFullAuthWallet,
|
|
303
|
-
withVerification: includeWalletVerification,
|
|
304
|
-
isConnectionOnly: connectionOnly
|
|
305
|
-
};
|
|
306
|
-
} catch (e) {
|
|
307
|
-
let error;
|
|
308
|
-
if (e.message === "No wallet exists") {
|
|
309
|
-
error = e.message;
|
|
310
|
-
} else {
|
|
311
|
-
error = `Graz connection error: ${(_b2 = e == null ? void 0 : e.message) != null ? _b2 : e}`;
|
|
343
|
+
const _chainId = chainId != null ? chainId : multiChain ? chains.map((c) => c.chainId) : selectedChainId;
|
|
344
|
+
if (!_chainId) {
|
|
345
|
+
throw new Error("Chain id not provided.");
|
|
346
|
+
}
|
|
347
|
+
if (!walletType2) {
|
|
348
|
+
throw new Error("Graz wallet type not provided.");
|
|
312
349
|
}
|
|
313
|
-
throw error;
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
const connectCallback = (walletType2) => {
|
|
317
|
-
return () => __async(this, null, function* () {
|
|
318
350
|
try {
|
|
319
|
-
const
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
351
|
+
const walletId = (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.id;
|
|
352
|
+
const isFullAuthWallet = walletsWithFullAuth === "ALL" || walletsWithFullAuth.includes(walletId.toUpperCase());
|
|
353
|
+
let chainInfo;
|
|
354
|
+
if (shouldUseSuggestChainAndConnect) {
|
|
355
|
+
if (typeof _chainId !== "string") {
|
|
356
|
+
console.error("multiChain is not compatible with shouldUseSuggestChainAndConnect.");
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
chainInfo = getChainInfo({ chainId: _chainId });
|
|
360
|
+
if (!chainInfo) {
|
|
361
|
+
console.error("Chain not found.");
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const connectedWallet = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType: walletType2, chainInfo }) : connectAsync({ walletType: walletType2, chainId: _chainId });
|
|
366
|
+
const firstChain = !chainId ? selectedChainId : typeof _chainId === "string" ? _chainId : _chainId[0];
|
|
367
|
+
const addressBech32 = connectedWallet.accounts[firstChain].bech32Address;
|
|
368
|
+
let rawAddress;
|
|
369
|
+
const accountAddress = connectedWallet.accounts[firstChain].address;
|
|
370
|
+
if (!accountAddress || accountAddress.length === 0 || accountAddress.byteLength === 0) {
|
|
371
|
+
const pubKey = connectedWallet.accounts[firstChain].pubKey;
|
|
372
|
+
rawAddress = rawSecp256k1PubkeyToRawAddress(pubKey);
|
|
373
|
+
} else {
|
|
374
|
+
rawAddress = accountAddress;
|
|
375
|
+
}
|
|
376
|
+
const address = formatEthHexAddress(rawAddress);
|
|
377
|
+
const { provider, providerId } = getProvider(walletType2);
|
|
378
|
+
return {
|
|
379
|
+
partnerId: para.partnerId,
|
|
380
|
+
type: "COSMOS",
|
|
381
|
+
address,
|
|
382
|
+
addressBech32,
|
|
383
|
+
provider,
|
|
384
|
+
providerId,
|
|
385
|
+
withFullParaAuth: isFullAuthWallet,
|
|
386
|
+
withVerification: includeWalletVerification,
|
|
387
|
+
isConnectionOnly: connectionOnly
|
|
388
|
+
};
|
|
325
389
|
} catch (e) {
|
|
326
|
-
|
|
390
|
+
let error;
|
|
391
|
+
if (e.message === "No wallet exists") {
|
|
392
|
+
error = e.message;
|
|
393
|
+
} else {
|
|
394
|
+
error = `Graz connection error: ${(_b2 = e == null ? void 0 : e.message) != null ? _b2 : e}`;
|
|
395
|
+
}
|
|
396
|
+
throw error;
|
|
327
397
|
}
|
|
328
|
-
})
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
398
|
+
}),
|
|
399
|
+
[
|
|
400
|
+
chains,
|
|
401
|
+
connectAsync,
|
|
402
|
+
connectionOnly,
|
|
403
|
+
disconnectAsync,
|
|
404
|
+
getProvider,
|
|
405
|
+
getWallet,
|
|
406
|
+
includeWalletVerification,
|
|
407
|
+
multiChain,
|
|
408
|
+
para.partnerId,
|
|
409
|
+
selectedChainId,
|
|
410
|
+
shouldUseSuggestChainAndConnect,
|
|
411
|
+
suggestAndConnectAsync,
|
|
412
|
+
walletsWithFullAuth
|
|
413
|
+
]
|
|
414
|
+
);
|
|
415
|
+
const connectCallback = useCallback(
|
|
416
|
+
(walletType2) => {
|
|
417
|
+
return () => __async(this, null, function* () {
|
|
418
|
+
try {
|
|
419
|
+
const _chainId = multiChain ? chains.map((c) => c.chainId) : selectedChainId;
|
|
420
|
+
const firstChain = typeof _chainId === "string" ? _chainId : _chainId[0];
|
|
421
|
+
const walletInfo = yield connectBase(walletType2, firstChain);
|
|
422
|
+
ethAddressRef.current = walletInfo.address;
|
|
423
|
+
bech32AddressRef.current = walletInfo.addressBech32;
|
|
424
|
+
return { externalWallet: walletInfo, chainId: firstChain };
|
|
425
|
+
} catch (e) {
|
|
426
|
+
throw e;
|
|
427
|
+
}
|
|
336
428
|
});
|
|
429
|
+
},
|
|
430
|
+
[chains, connectBase, multiChain, selectedChainId]
|
|
431
|
+
);
|
|
432
|
+
const connect = useCallback(
|
|
433
|
+
(walletType2) => __async(this, null, function* () {
|
|
434
|
+
var _a2, _b2;
|
|
435
|
+
updateExternalWalletState({ isConnecting: true });
|
|
436
|
+
try {
|
|
437
|
+
const authState = yield para.connectExternalWallet({
|
|
438
|
+
connect: connectCallback(walletType2)
|
|
439
|
+
});
|
|
440
|
+
return {
|
|
441
|
+
address: (_a2 = authState.externalWallet) == null ? void 0 : _a2.address,
|
|
442
|
+
authState
|
|
443
|
+
};
|
|
444
|
+
} catch (e) {
|
|
445
|
+
return {
|
|
446
|
+
error: (_b2 = e == null ? void 0 : e.message) != null ? _b2 : e
|
|
447
|
+
};
|
|
448
|
+
} finally {
|
|
449
|
+
updateExternalWalletState({ isConnecting: false });
|
|
450
|
+
}
|
|
451
|
+
}),
|
|
452
|
+
[connectCallback, para, updateExternalWalletState]
|
|
453
|
+
);
|
|
454
|
+
const wallets = useMemo(
|
|
455
|
+
() => allWallets.map((wallet) => {
|
|
456
|
+
return __spreadProps(__spreadValues({
|
|
457
|
+
connect: () => connect(wallet.grazType),
|
|
458
|
+
connectMobile: () => connect(wallet.grazType),
|
|
459
|
+
type: "COSMOS"
|
|
460
|
+
}, wallet), {
|
|
461
|
+
// Using name here since that's the only common id across the networks
|
|
462
|
+
id: wallet.name,
|
|
463
|
+
installed: checkWallet(wallet.grazType)
|
|
464
|
+
});
|
|
465
|
+
}).filter((w) => !!w),
|
|
466
|
+
[allWallets, connect]
|
|
467
|
+
);
|
|
468
|
+
const formattedChains = useMemo(
|
|
469
|
+
() => chains.map((c) => {
|
|
337
470
|
return {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
};
|
|
341
|
-
} catch (e) {
|
|
342
|
-
return {
|
|
343
|
-
error: (_b2 = e == null ? void 0 : e.message) != null ? _b2 : e
|
|
471
|
+
id: c.chainId,
|
|
472
|
+
name: c.chainName
|
|
344
473
|
};
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
});
|
|
349
|
-
const injectedWallets = WALLET_TYPES.filter((w) => !incompleteWallets.some((iw) => iw.grazType === w) && checkWallet(w)).map((w) => {
|
|
350
|
-
const wallet = grazGetWallet(w);
|
|
351
|
-
if (!wallet.eip6963ProviderInfo) {
|
|
352
|
-
return void 0;
|
|
353
|
-
}
|
|
354
|
-
const eipInfo = wallet.eip6963ProviderInfo;
|
|
355
|
-
return __spreadValues({
|
|
356
|
-
grazType: w,
|
|
357
|
-
// Using name here for the injected connector since that's the only common id across the networks
|
|
358
|
-
id: eipInfo.name,
|
|
359
|
-
internalId: eipInfo.name,
|
|
360
|
-
iconUrl: eipInfo.icon
|
|
361
|
-
}, eipInfo);
|
|
362
|
-
}).filter((w) => !!w);
|
|
363
|
-
const allWallets = [...incompleteWallets, ...injectedWallets];
|
|
364
|
-
const getWallet = (walletType2) => allWallets.find((w) => w.grazType === walletType2 || w.grazMobileType === walletType2);
|
|
365
|
-
const getProvider = (walletType2) => {
|
|
366
|
-
const wallet = getWallet(walletType2);
|
|
367
|
-
return {
|
|
368
|
-
provider: wallet == null ? void 0 : wallet.name,
|
|
369
|
-
providerId: wallet == null ? void 0 : wallet.id
|
|
370
|
-
};
|
|
371
|
-
};
|
|
372
|
-
const wallets = allWallets.map((wallet) => {
|
|
373
|
-
return __spreadProps(__spreadValues({
|
|
374
|
-
connect: () => connect(wallet.grazType),
|
|
375
|
-
connectMobile: () => connect(wallet.grazType),
|
|
376
|
-
type: "COSMOS"
|
|
377
|
-
}, wallet), {
|
|
378
|
-
// Using name here since that's the only common id across the networks
|
|
379
|
-
id: wallet.name,
|
|
380
|
-
installed: checkWallet(wallet.grazType)
|
|
381
|
-
});
|
|
382
|
-
}).filter((w) => !!w);
|
|
383
|
-
const formattedChains = chains.map((c) => {
|
|
384
|
-
return {
|
|
385
|
-
id: c.chainId,
|
|
386
|
-
name: c.chainName
|
|
387
|
-
};
|
|
388
|
-
});
|
|
474
|
+
}),
|
|
475
|
+
[chains]
|
|
476
|
+
);
|
|
389
477
|
const connectParaEmbedded = useCallback(() => __async(this, null, function* () {
|
|
390
478
|
if (!para) {
|
|
391
479
|
return { error: "No para instance available" };
|
|
@@ -398,72 +486,74 @@ function CosmosExternalWalletProvider({
|
|
|
398
486
|
const error = err instanceof Error ? err.message : "Unknown error";
|
|
399
487
|
return { error };
|
|
400
488
|
}
|
|
401
|
-
}), [
|
|
402
|
-
const requestInfo = (
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
489
|
+
}), [chains, connectAsync, multiChain, para, selectedChainId]);
|
|
490
|
+
const requestInfo = useCallback(
|
|
491
|
+
(providerId) => __async(this, null, function* () {
|
|
492
|
+
var _a2;
|
|
493
|
+
const wallet = wallets.find((w) => w.id === providerId);
|
|
494
|
+
if (!wallet) {
|
|
495
|
+
throw new Error(`Wallet for provider ${providerId} not found`);
|
|
496
|
+
}
|
|
497
|
+
disconnectTypeRef.current = "ACCOUNT_LINKING";
|
|
498
|
+
try {
|
|
499
|
+
const externalWallet = yield connectBase(
|
|
500
|
+
wallet.grazType,
|
|
501
|
+
multiChain ? chains.map((c) => c.chainId) : selectedChainId
|
|
502
|
+
);
|
|
503
|
+
return externalWallet;
|
|
504
|
+
} catch (e) {
|
|
505
|
+
console.error("Error linking account:", e);
|
|
506
|
+
throw new Error((_a2 = e == null ? void 0 : e.message) != null ? _a2 : e);
|
|
507
|
+
}
|
|
508
|
+
}),
|
|
509
|
+
[chains, connectBase, multiChain, selectedChainId, wallets]
|
|
510
|
+
);
|
|
511
|
+
const disconnectBase = useCallback(
|
|
512
|
+
(_0, ..._1) => __async(this, [_0, ..._1], function* (_, { disconnectType } = {}) {
|
|
513
|
+
var _a2;
|
|
514
|
+
if (disconnectType) {
|
|
515
|
+
disconnectTypeRef.current = disconnectType;
|
|
516
|
+
}
|
|
517
|
+
try {
|
|
518
|
+
yield disconnectAsync();
|
|
519
|
+
} catch (e) {
|
|
520
|
+
console.error("Error linking account:", e);
|
|
521
|
+
throw new Error((_a2 = e == null ? void 0 : e.message) != null ? _a2 : e);
|
|
522
|
+
} finally {
|
|
523
|
+
disconnectTypeRef.current = void 0;
|
|
524
|
+
}
|
|
525
|
+
}),
|
|
526
|
+
[disconnectAsync]
|
|
527
|
+
);
|
|
528
|
+
const contextValue = useMemo(
|
|
529
|
+
() => __spreadValues({
|
|
530
|
+
wallets,
|
|
531
|
+
chains: formattedChains,
|
|
532
|
+
chainId: selectedChainId,
|
|
533
|
+
disconnect: disconnectAsync,
|
|
534
|
+
disconnectStatus,
|
|
535
|
+
switchChain,
|
|
536
|
+
switchConnection: switchAccount,
|
|
537
|
+
connectParaEmbedded,
|
|
538
|
+
signMessage,
|
|
539
|
+
requestInfo,
|
|
540
|
+
disconnectBase
|
|
541
|
+
}, externalHooks),
|
|
542
|
+
[
|
|
543
|
+
wallets,
|
|
544
|
+
formattedChains,
|
|
545
|
+
selectedChainId,
|
|
546
|
+
disconnectAsync,
|
|
547
|
+
disconnectStatus,
|
|
548
|
+
switchChain,
|
|
549
|
+
switchAccount,
|
|
550
|
+
connectParaEmbedded,
|
|
551
|
+
signMessage,
|
|
552
|
+
requestInfo,
|
|
553
|
+
disconnectBase
|
|
554
|
+
]
|
|
466
555
|
);
|
|
556
|
+
return /* @__PURE__ */ jsx(CosmosExternalWalletContext.Provider, { value: contextValue, children });
|
|
467
557
|
}
|
|
468
558
|
export {
|
|
469
559
|
CosmosExternalWalletContext,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Wallet } from 'graz';
|
|
2
|
+
type CosmosAdr36SignDoc = Parameters<Wallet['signAmino']>[2];
|
|
3
|
+
type SignCosmosWalletMessageArgs = {
|
|
4
|
+
chainId: string;
|
|
5
|
+
wallet: Wallet;
|
|
6
|
+
bech32Address: string;
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const COSMOS_WALLET_SIGNING_UNSUPPORTED_ERROR = "Cosmos wallet does not support signArbitrary or signAmino";
|
|
10
|
+
export declare function createCosmosAdr36SignDoc({ signer, message }: {
|
|
11
|
+
signer: string;
|
|
12
|
+
message: string;
|
|
13
|
+
}): CosmosAdr36SignDoc;
|
|
14
|
+
export declare function signCosmosWalletMessage({ chainId, wallet, bech32Address, message, }: SignCosmosWalletMessageArgs): Promise<{
|
|
15
|
+
signature: string;
|
|
16
|
+
cosmosPublicKeyHex: string;
|
|
17
|
+
}>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async
|
|
4
|
+
} from "../chunk-IV3L3JVM.js";
|
|
5
|
+
const COSMOS_WALLET_SIGNING_UNSUPPORTED_ERROR = "Cosmos wallet does not support signArbitrary or signAmino";
|
|
6
|
+
function createCosmosAdr36SignDoc({ signer, message }) {
|
|
7
|
+
return {
|
|
8
|
+
chain_id: "",
|
|
9
|
+
account_number: "0",
|
|
10
|
+
sequence: "0",
|
|
11
|
+
fee: { gas: "0", amount: [] },
|
|
12
|
+
msgs: [
|
|
13
|
+
{
|
|
14
|
+
type: "sign/MsgSignData",
|
|
15
|
+
value: {
|
|
16
|
+
signer,
|
|
17
|
+
data: Buffer.from(message).toString("base64")
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
memo: ""
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function signCosmosWalletMessage(_0) {
|
|
25
|
+
return __async(this, arguments, function* ({
|
|
26
|
+
chainId,
|
|
27
|
+
wallet,
|
|
28
|
+
bech32Address,
|
|
29
|
+
message
|
|
30
|
+
}) {
|
|
31
|
+
var _a;
|
|
32
|
+
const signed = yield signWithWallet({ chainId, wallet, bech32Address, message });
|
|
33
|
+
const signature = signed.signature;
|
|
34
|
+
if (!signature) {
|
|
35
|
+
throw new Error("Cosmos wallet did not return a signature");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
signature,
|
|
39
|
+
cosmosPublicKeyHex: (_a = getSignaturePublicKeyHex(signed)) != null ? _a : yield getWalletPublicKeyHex(wallet, chainId)
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function signWithWallet(_0) {
|
|
44
|
+
return __async(this, arguments, function* ({
|
|
45
|
+
chainId,
|
|
46
|
+
wallet,
|
|
47
|
+
bech32Address,
|
|
48
|
+
message
|
|
49
|
+
}) {
|
|
50
|
+
if (wallet.signArbitrary) {
|
|
51
|
+
return wallet.signArbitrary(chainId, bech32Address, message);
|
|
52
|
+
}
|
|
53
|
+
if (!wallet.signAmino) {
|
|
54
|
+
throw new Error(COSMOS_WALLET_SIGNING_UNSUPPORTED_ERROR);
|
|
55
|
+
}
|
|
56
|
+
const response = yield wallet.signAmino(
|
|
57
|
+
chainId,
|
|
58
|
+
bech32Address,
|
|
59
|
+
createCosmosAdr36SignDoc({ signer: bech32Address, message })
|
|
60
|
+
);
|
|
61
|
+
return response.signature;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function getSignaturePublicKeyHex(signature) {
|
|
65
|
+
var _a;
|
|
66
|
+
const publicKey = (_a = signature.pub_key) == null ? void 0 : _a.value;
|
|
67
|
+
if (!publicKey) {
|
|
68
|
+
return void 0;
|
|
69
|
+
}
|
|
70
|
+
return Buffer.from(publicKey, "base64").toString("hex");
|
|
71
|
+
}
|
|
72
|
+
function getWalletPublicKeyHex(wallet, chainId) {
|
|
73
|
+
return __async(this, null, function* () {
|
|
74
|
+
const publicKey = (yield wallet.getKey(chainId)).pubKey;
|
|
75
|
+
return Buffer.from(publicKey).toString("hex");
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
COSMOS_WALLET_SIGNING_UNSUPPORTED_ERROR,
|
|
80
|
+
createCosmosAdr36SignDoc,
|
|
81
|
+
signCosmosWalletMessage
|
|
82
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getpara/cosmos-wallet-connectors",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@getpara/graz-connector": "3.
|
|
6
|
-
"@getpara/react-common": "3.
|
|
7
|
-
"@getpara/web-sdk": "3.
|
|
5
|
+
"@getpara/graz-connector": "3.2.0",
|
|
6
|
+
"@getpara/react-common": "3.2.0",
|
|
7
|
+
"@getpara/web-sdk": "3.2.0",
|
|
8
8
|
"@leapwallet/cosmos-social-login-capsule-provider": "^0.0.41",
|
|
9
9
|
"zustand": "^4.5.2",
|
|
10
10
|
"zustand-sync-tabs": "^0.2.2"
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dist",
|
|
24
24
|
"package.json"
|
|
25
25
|
],
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "096152f48e9d9a64fdb192588315c64d33d15a1b",
|
|
27
27
|
"main": "dist/index.js",
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"graz": ">=0.4.1",
|