@getpara/cosmos-wallet-connectors 2.11.0 → 2.13.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.
@@ -12,4 +12,4 @@ export type CosmosExternalWalletContextType = ExternalWalletContextType<CosmosSi
12
12
  export type CosmosExternalWalletProviderConfig = ExternalWalletProviderConfigBase;
13
13
  export type CosmosExternalWalletProviderConfigFull = ExternalWalletProviderConfig<WalletWithType, Omit<ParaCosmosProviderConfig, 'wallets'>>;
14
14
  export declare const CosmosExternalWalletContext: import("react").Context<CosmosExternalWalletContextType>;
15
- export declare function CosmosExternalWalletProvider({ children, onSwitchWallet, selectedChainId, wallets: incompleteWallets, chains, multiChain, shouldUseSuggestChainAndConnect, onSwitchChain, para, walletsWithFullAuth, connectedWallet: connectedWalletProp, includeWalletVerification, connectionOnly, }: CosmosExternalWalletProviderConfigFull & PropsWithChildren): import("react/jsx-runtime").JSX.Element;
15
+ export declare function CosmosExternalWalletProvider({ children, onSwitchWallet, selectedChainId, wallets: incompleteWallets, chains, multiChain, shouldUseSuggestChainAndConnect, onSwitchChain, para, walletsWithFullAuth, includeWalletVerification, connectionOnly, }: CosmosExternalWalletProviderConfigFull & PropsWithChildren): import("react/jsx-runtime").JSX.Element;
@@ -19,7 +19,9 @@ import {
19
19
  WALLET_TYPES
20
20
  } from "graz";
21
21
  import { useExternalWalletStore } from "../stores/useStore.js";
22
- import { rawSecp256k1PubkeyToRawAddress } from "@getpara/web-sdk";
22
+ import {
23
+ rawSecp256k1PubkeyToRawAddress
24
+ } from "@getpara/web-sdk";
23
25
  import {
24
26
  defaultCosmosExternalWallet
25
27
  } from "@getpara/react-common";
@@ -39,7 +41,6 @@ function CosmosExternalWalletProvider({
39
41
  onSwitchChain,
40
42
  para,
41
43
  walletsWithFullAuth,
42
- connectedWallet: connectedWalletProp,
43
44
  includeWalletVerification,
44
45
  connectionOnly
45
46
  }) {
@@ -47,9 +48,8 @@ function CosmosExternalWalletProvider({
47
48
  const { suggestAndConnectAsync } = useSuggestChainAndConnect();
48
49
  const {
49
50
  data: account,
50
- isConnecting,
51
- isReconnecting,
52
- isConnected
51
+ isConnected,
52
+ isDisconnected
53
53
  } = useAccount(
54
54
  multiChain ? {
55
55
  chainId: chains.map((c) => c.chainId)
@@ -60,18 +60,100 @@ function CosmosExternalWalletProvider({
60
60
  const { connectAsync } = useConnect();
61
61
  const { disconnectAsync, status: disconnectStatus } = useDisconnect();
62
62
  const { walletType } = useActiveWalletType();
63
- const isLocalConnecting = useExternalWalletStore((state) => state.isConnecting);
64
63
  const updateExternalWalletState = useExternalWalletStore((state) => state.updateState);
65
- const isConnectError = useRef(false);
66
- const connectedWallet = connectedWalletProp ? para.findWallet(connectedWalletProp.id, connectedWalletProp.type) : null;
67
- const ethAddress = (_b = (_a = account == null ? void 0 : account[selectedChainId]) == null ? void 0 : _a.ethereumHexAddress) == null ? void 0 : _b.toLowerCase();
68
- const address = (_c = account == null ? void 0 : account[selectedChainId]) == null ? void 0 : _c.bech32Address;
69
64
  const disconnectTypeRef = useRef();
70
- const verificationMessage = useRef();
71
- const reset = () => __async(this, null, function* () {
72
- yield disconnectAsync();
73
- yield para.logout();
74
- });
65
+ const ethAddressRef = useRef((_b = (_a = account == null ? void 0 : account[selectedChainId]) == null ? void 0 : _a.ethereumHexAddress) == null ? void 0 : _b.toLowerCase());
66
+ const bech32AddressRef = useRef((_c = account == null ? void 0 : account[selectedChainId]) == null ? void 0 : _c.bech32Address);
67
+ const wasConnectedRef = useRef(isConnected);
68
+ const disconnectionCallbackRef = useRef(null);
69
+ const walletChangeCallbackRef = useRef(null);
70
+ useEffect(() => {
71
+ var _a2;
72
+ if (walletChangeCallbackRef.current && (account == null ? void 0 : account[selectedChainId])) {
73
+ const ethAddress = (_a2 = account[selectedChainId].ethereumHexAddress) == null ? void 0 : _a2.toLowerCase();
74
+ const bech32Address = account[selectedChainId].bech32Address;
75
+ if (ethAddress && bech32Address) {
76
+ const { provider, providerId } = getProvider(walletType);
77
+ walletChangeCallbackRef.current({
78
+ externalWallet: {
79
+ providerId,
80
+ provider,
81
+ address: ethAddress,
82
+ addressBech32: bech32Address,
83
+ type: "COSMOS"
84
+ }
85
+ });
86
+ }
87
+ }
88
+ }, [account, selectedChainId, walletType]);
89
+ useEffect(() => {
90
+ if (wasConnectedRef.current && isDisconnected) {
91
+ ethAddressRef.current = void 0;
92
+ bech32AddressRef.current = void 0;
93
+ if (disconnectionCallbackRef.current) {
94
+ disconnectionCallbackRef.current();
95
+ }
96
+ }
97
+ wasConnectedRef.current = isConnected;
98
+ }, [isConnected, isDisconnected]);
99
+ useEffect(() => {
100
+ if (!para.isSetup) {
101
+ return;
102
+ }
103
+ const initializeConfig = () => __async(this, null, function* () {
104
+ yield para.initExternalWalletProvider({
105
+ type: "COSMOS",
106
+ interface: {
107
+ disconnect: () => __async(this, null, function* () {
108
+ yield disconnectAsync();
109
+ }),
110
+ getAddress: () => __async(this, null, function* () {
111
+ return ethAddressRef.current;
112
+ }),
113
+ signMessage: (_0) => __async(this, [_0], function* ({ message }) {
114
+ try {
115
+ const wallet = grazGetWallet(walletType);
116
+ const signAddress = bech32AddressRef.current;
117
+ const signEthAddress = ethAddressRef.current;
118
+ if (!wallet || !signAddress || !signEthAddress) {
119
+ throw new Error("No connected wallet found");
120
+ }
121
+ return yield signMessageBase({
122
+ message,
123
+ wallet,
124
+ bech32Address: signAddress,
125
+ ethAddress: signEthAddress
126
+ });
127
+ } catch (e) {
128
+ console.error(e);
129
+ if (e.message.includes("User rejected the request")) {
130
+ throw new Error("Signature request rejected");
131
+ }
132
+ console.error("Cosmos signature error:", e.message);
133
+ throw new Error("An unknown error occurred");
134
+ }
135
+ }),
136
+ watchDisconnection: (callback) => {
137
+ disconnectionCallbackRef.current = callback;
138
+ return () => {
139
+ disconnectionCallbackRef.current = null;
140
+ };
141
+ },
142
+ watchWalletChange: (callback) => {
143
+ walletChangeCallbackRef.current = callback;
144
+ return () => {
145
+ walletChangeCallbackRef.current = null;
146
+ };
147
+ },
148
+ connectParaConnector: () => __async(this, null, function* () {
149
+ const chainId = multiChain ? chains.map((c) => c.chainId) : selectedChainId;
150
+ yield connectAsync({ walletType: GrazWalletType.PARA, chainId });
151
+ })
152
+ }
153
+ });
154
+ });
155
+ initializeConfig();
156
+ }, [para.isSetup]);
75
157
  const switchChain = (chainId) => __async(this, null, function* () {
76
158
  var _a2;
77
159
  let error;
@@ -85,9 +167,9 @@ function CosmosExternalWalletProvider({
85
167
  return;
86
168
  }
87
169
  }
88
- const connectedWallet2 = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType, chainInfo }) : connectAsync({ walletType, chainId });
89
- changeResp.address = connectedWallet2.accounts[chainId].bech32Address;
90
- changeResp.ethAddress = formatEthHexAddress(connectedWallet2.accounts[chainId].address);
170
+ const connectedWallet = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType, chainInfo }) : connectAsync({ walletType, chainId });
171
+ changeResp.address = connectedWallet.accounts[chainId].bech32Address;
172
+ changeResp.ethAddress = formatEthHexAddress(connectedWallet.accounts[chainId].address);
91
173
  } catch (err) {
92
174
  if (err.message === "No wallet exists") {
93
175
  changeResp.error = err.message;
@@ -115,87 +197,54 @@ function CosmosExternalWalletProvider({
115
197
  }
116
198
  return { error };
117
199
  });
118
- const login = (_0) => __async(this, [_0], function* ({
119
- address: address2,
120
- addressBech32,
121
- withFullParaAuth,
122
- providerId,
123
- provider
124
- }) {
125
- try {
126
- return yield para.loginExternalWallet({
127
- externalWallet: {
128
- partnerId: para.partnerId,
129
- address: address2,
130
- type: "COSMOS",
131
- provider,
132
- providerId,
133
- addressBech32,
134
- withFullParaAuth,
135
- withVerification: includeWalletVerification,
136
- isConnectionOnly: connectionOnly
137
- },
138
- uri: window == null ? void 0 : window.location.origin,
139
- chainId: selectedChainId
200
+ const switchAccount = useCallback(
201
+ (connectorName) => __async(this, null, function* () {
202
+ const chainId = multiChain ? chains.map((c) => c.chainId) : selectedChainId;
203
+ yield connectAsync({
204
+ walletType: connectorName.toLowerCase(),
205
+ chainId
140
206
  });
141
- } catch (e) {
142
- yield reset();
143
- throw "Error logging you in. Please try again.";
144
- }
207
+ }),
208
+ [connectAsync]
209
+ );
210
+ const signMessageBase = (_0) => __async(this, [_0], function* ({
211
+ message,
212
+ wallet,
213
+ bech32Address,
214
+ ethAddress
215
+ }) {
216
+ const publicKey = (yield wallet.getKey(selectedChainId)).pubKey;
217
+ const signature = yield wallet.signArbitrary(selectedChainId, bech32Address, message);
218
+ return {
219
+ address: ethAddress,
220
+ addressBech32: bech32Address,
221
+ signature: signature.signature,
222
+ cosmosPublicKeyHex: Buffer.from(publicKey).toString("hex"),
223
+ cosmosSigner: bech32Address
224
+ };
145
225
  });
146
- useEffect(() => {
147
- const storedExternalWallet = para.externalWallets[ethAddress != null ? ethAddress : ""];
148
- if (isConnected && !isConnecting && !isReconnecting && !isLocalConnecting && !!ethAddress && !storedExternalWallet && walletType !== GrazWalletType.PARA && !disconnectTypeRef.current) {
149
- reset();
150
- }
151
- }, [isConnecting, isLocalConnecting, isReconnecting, isConnected]);
152
- useEffect(() => {
153
- const connect2 = () => __async(this, null, function* () {
154
- var _a2;
155
- if (!isLocalConnecting && !isConnecting && !isReconnecting && connectedWallet && connectedWallet.type === "COSMOS" && (connectedWallet.isExternal ? walletType !== ((_a2 = connectedWallet.name) == null ? void 0 : _a2.toLowerCase()) : walletType !== "para") && !disconnectTypeRef.current && !isConnectError.current) {
156
- const isLoggedIn = yield para.isFullyLoggedIn();
157
- if (!isLoggedIn) {
158
- return;
159
- }
160
- const chainId = multiChain ? chains.map((c) => c.chainId) : selectedChainId;
161
- const targetWalletType = connectedWallet.isExternal ? connectedWallet.name.toLowerCase() : GrazWalletType.PARA;
162
- try {
163
- yield connectAsync({
164
- walletType: targetWalletType,
165
- chainId
166
- });
167
- } catch (e) {
168
- isConnectError.current = true;
169
- }
170
- }
171
- });
172
- connect2();
173
- }, [isLocalConnecting, isConnecting, isReconnecting, walletType, connectedWallet]);
174
226
  const signMessage = (_0) => __async(this, [_0], function* ({ message, externalWallet }) {
175
- let wallet, signAddress, signEthAddress;
227
+ let wallet, bech32Address, ethAddress;
176
228
  if (externalWallet) {
177
229
  const commonWallet = wallets.find((w) => w.id === externalWallet.providerId);
178
230
  wallet = grazGetWallet(commonWallet == null ? void 0 : commonWallet.grazType);
179
- signAddress = externalWallet.addressBech32;
180
- signEthAddress = externalWallet.address;
231
+ bech32Address = externalWallet.addressBech32;
232
+ ethAddress = externalWallet.address;
181
233
  } else {
182
234
  wallet = grazGetWallet(walletType);
183
- signAddress = address;
184
- signEthAddress = ethAddress;
235
+ bech32Address = bech32AddressRef.current;
236
+ ethAddress = ethAddressRef.current;
185
237
  }
186
238
  if (!wallet) {
187
239
  return { error: "Connected wallet not found" };
188
240
  }
189
241
  try {
190
- const publicKey = (yield wallet.getKey(selectedChainId)).pubKey;
191
- const signature = yield wallet.signArbitrary(selectedChainId, signAddress, message);
192
- return {
193
- address: signEthAddress,
194
- addressBech32: signAddress,
195
- signature: signature.signature,
196
- cosmosPublicKeyHex: Buffer.from(publicKey).toString("hex"),
197
- cosmosSigner: signAddress
198
- };
242
+ return yield signMessageBase({
243
+ message,
244
+ wallet,
245
+ bech32Address,
246
+ ethAddress
247
+ });
199
248
  } catch (e) {
200
249
  if (e.message.includes("Request rejected")) {
201
250
  return { error: "Signature request rejected" };
@@ -203,12 +252,8 @@ function CosmosExternalWalletProvider({
203
252
  return { error: "An unknown error occurred" };
204
253
  }
205
254
  });
206
- const signVerificationMessage = () => __async(this, null, function* () {
207
- const signature = yield signMessage({ message: verificationMessage.current });
208
- return signature;
209
- });
210
255
  const connectBase = (walletType2, chainId) => __async(this, null, function* () {
211
- var _a2;
256
+ var _a2, _b2;
212
257
  if (!chainId) {
213
258
  yield disconnectAsync();
214
259
  }
@@ -220,6 +265,8 @@ function CosmosExternalWalletProvider({
220
265
  throw new Error("Graz wallet type not provided.");
221
266
  }
222
267
  try {
268
+ const walletId = (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.id;
269
+ const isFullAuthWallet = walletsWithFullAuth === "ALL" || walletsWithFullAuth.includes(walletId.toUpperCase());
223
270
  let chainInfo;
224
271
  if (shouldUseSuggestChainAndConnect) {
225
272
  if (typeof _chainId !== "string") {
@@ -232,50 +279,63 @@ function CosmosExternalWalletProvider({
232
279
  return;
233
280
  }
234
281
  }
235
- const connectedWallet2 = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType: walletType2, chainInfo }) : connectAsync({ walletType: walletType2, chainId: _chainId });
282
+ const connectedWallet = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType: walletType2, chainInfo }) : connectAsync({ walletType: walletType2, chainId: _chainId });
236
283
  const firstChain = !chainId ? selectedChainId : typeof _chainId === "string" ? _chainId : _chainId[0];
237
- const addressBech32 = connectedWallet2.accounts[firstChain].bech32Address;
284
+ const addressBech32 = connectedWallet.accounts[firstChain].bech32Address;
238
285
  let rawAddress;
239
- const accountAddress = connectedWallet2.accounts[firstChain].address;
286
+ const accountAddress = connectedWallet.accounts[firstChain].address;
240
287
  if (!accountAddress || accountAddress.length === 0 || accountAddress.byteLength === 0) {
241
- const pubKey = connectedWallet2.accounts[firstChain].pubKey;
288
+ const pubKey = connectedWallet.accounts[firstChain].pubKey;
242
289
  rawAddress = rawSecp256k1PubkeyToRawAddress(pubKey);
243
290
  } else {
244
291
  rawAddress = accountAddress;
245
292
  }
246
- const address2 = formatEthHexAddress(rawAddress);
293
+ const address = formatEthHexAddress(rawAddress);
247
294
  const { provider, providerId } = getProvider(walletType2);
248
295
  return {
249
296
  partnerId: para.partnerId,
250
297
  type: "COSMOS",
251
- address: address2,
298
+ address,
252
299
  addressBech32,
253
300
  provider,
254
- providerId
301
+ providerId,
302
+ withFullParaAuth: isFullAuthWallet,
303
+ withVerification: includeWalletVerification,
304
+ isConnectionOnly: connectionOnly
255
305
  };
256
306
  } catch (e) {
257
307
  let error;
258
308
  if (e.message === "No wallet exists") {
259
309
  error = e.message;
260
310
  } else {
261
- error = `Graz connection error: ${(_a2 = e == null ? void 0 : e.message) != null ? _a2 : e}`;
311
+ error = `Graz connection error: ${(_b2 = e == null ? void 0 : e.message) != null ? _b2 : e}`;
262
312
  }
263
313
  throw error;
264
314
  }
265
315
  });
266
- const connect = (walletType2, chainId) => __async(this, null, function* () {
316
+ const connectCallback = (walletType2) => {
317
+ return () => __async(this, null, function* () {
318
+ try {
319
+ const _chainId = multiChain ? chains.map((c) => c.chainId) : selectedChainId;
320
+ const firstChain = typeof _chainId === "string" ? _chainId : _chainId[0];
321
+ const walletInfo = yield connectBase(walletType2, firstChain);
322
+ ethAddressRef.current = walletInfo.address;
323
+ bech32AddressRef.current = walletInfo.addressBech32;
324
+ return { externalWallet: walletInfo, chainId: firstChain };
325
+ } catch (e) {
326
+ throw e;
327
+ }
328
+ });
329
+ };
330
+ const connect = (walletType2) => __async(this, null, function* () {
267
331
  var _a2, _b2;
268
332
  updateExternalWalletState({ isConnecting: true });
269
- const walletId = (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.id;
270
- const isFullAuthWallet = walletsWithFullAuth === "ALL" || walletsWithFullAuth.includes(walletId.toUpperCase());
271
333
  try {
272
- const externalWallet = yield connectBase(walletType2, chainId);
273
- const authState = yield login(__spreadProps(__spreadValues({}, externalWallet), {
274
- withFullParaAuth: isFullAuthWallet
275
- }));
276
- verificationMessage.current = authState.stage === "verify" ? authState.signatureVerificationMessage : void 0;
334
+ const authState = yield para.connectExternalWallet({
335
+ connect: connectCallback(walletType2)
336
+ });
277
337
  return {
278
- address: externalWallet.address,
338
+ address: (_a2 = authState.externalWallet) == null ? void 0 : _a2.address,
279
339
  authState
280
340
  };
281
341
  } catch (e) {
@@ -382,9 +442,9 @@ function CosmosExternalWalletProvider({
382
442
  disconnect: disconnectAsync,
383
443
  disconnectStatus,
384
444
  switchChain,
445
+ switchConnection: switchAccount,
385
446
  connectParaEmbedded,
386
447
  signMessage,
387
- signVerificationMessage,
388
448
  requestInfo,
389
449
  disconnectBase
390
450
  }, externalHooks),
@@ -394,9 +454,9 @@ function CosmosExternalWalletProvider({
394
454
  selectedChainId,
395
455
  disconnectAsync,
396
456
  switchChain,
457
+ switchAccount,
397
458
  connectParaEmbedded,
398
459
  signMessage,
399
- signVerificationMessage,
400
460
  requestInfo,
401
461
  disconnectBase
402
462
  ]
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@getpara/cosmos-wallet-connectors",
3
- "version": "2.11.0",
3
+ "version": "2.13.0",
4
4
  "dependencies": {
5
- "@getpara/graz-connector": "2.11.0",
6
- "@getpara/react-common": "2.11.0",
7
- "@getpara/web-sdk": "2.11.0",
5
+ "@getpara/graz-connector": "2.13.0",
6
+ "@getpara/react-common": "2.13.0",
7
+ "@getpara/web-sdk": "2.13.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": "1cce45857320460cca03c4a9d0a59db169b222d6",
26
+ "gitHead": "75ac5d1ebe87366c23bb4e485481deb1b593e299",
27
27
  "main": "dist/index.js",
28
28
  "peerDependencies": {
29
29
  "graz": ">=0.4.1",