@getpara/cosmos-wallet-connectors 2.0.0-alpha.19 → 2.0.0-alpha.21

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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { CosmosExternalWalletContext } from './providers/CosmosExternalWalletContext.js';
2
- export type { CosmosExternalWalletContextType } from './providers/CosmosExternalWalletContext.js';
2
+ export { type CosmosExternalWalletContextType, type CosmosSignResult, defaultCosmosExternalWallet, } from './providers/CosmosExternalWalletContext.js';
3
3
  export { ParaCosmosProvider } from './providers/ParaCosmosContext.js';
4
4
  export type { ParaCosmosProviderProps, ParaGrazProviderProps, ParaCosmosProviderConfig, } from './providers/ParaCosmosContext.js';
5
5
  export * from './wallets/connectors/index.js';
package/dist/index.js CHANGED
@@ -1,9 +1,13 @@
1
1
  "use client";
2
2
  import "./chunk-IV3L3JVM.js";
3
3
  import { CosmosExternalWalletContext } from "./providers/CosmosExternalWalletContext.js";
4
+ import {
5
+ defaultCosmosExternalWallet
6
+ } from "./providers/CosmosExternalWalletContext.js";
4
7
  import { ParaCosmosProvider } from "./providers/ParaCosmosContext.js";
5
8
  export * from "./wallets/connectors/index.js";
6
9
  export {
7
10
  CosmosExternalWalletContext,
8
- ParaCosmosProvider
11
+ ParaCosmosProvider,
12
+ defaultCosmosExternalWallet
9
13
  };
@@ -1,46 +1,26 @@
1
1
  import { PropsWithChildren } from 'react';
2
2
  import { ParaCosmosProviderConfig } from './ParaCosmosContext.js';
3
3
  import { WalletWithType } from '../types/Wallet.js';
4
- import ParaWeb, { Wallet } from '@getpara/web-sdk';
5
- import type { CommonChain, CommonWallet, TExternalWallet } from '@getpara/react-common';
6
- export type CosmosExternalWalletContextType = {
7
- wallets: CommonWallet[];
8
- chains: CommonChain[];
9
- chainId?: string;
4
+ import type { ChainManagement, ConnectParaEmbedded, ExternalWalletContextType, ExternalWalletProviderConfig, ExternalWalletProviderConfigBase, SignResult } from '@getpara/react-common';
5
+ export declare const defaultCosmosExternalWallet: {
6
+ wallets: any[];
7
+ chains: any[];
8
+ chainId: any;
10
9
  disconnect: () => Promise<void>;
11
- switchChain: (chainId: string) => Promise<{
12
- error?: string[];
13
- }>;
14
- connectParaEmbedded: () => Promise<{
15
- result?: unknown;
16
- error?: string;
17
- }>;
18
- signMessage: (message: string) => Promise<{
19
- signature?: string;
20
- error?: string;
21
- }>;
22
- signVerificationMessage: () => Promise<{
23
- address?: string;
24
- signature?: string;
25
- cosmosPublicKeyHex?: string;
26
- cosmosSigner?: string;
27
- error?: string;
28
- addressBech32?: string;
29
- }>;
10
+ switchChain: () => Promise<void>;
11
+ connectParaEmbedded: () => Promise<{}>;
12
+ signMessage: () => Promise<{}>;
13
+ signVerificationMessage: () => Promise<{}>;
14
+ requestInfo: () => Promise<any>;
15
+ disconnectBase: () => Promise<void>;
30
16
  };
31
- export type CosmosExternalWalletProviderConfig = {
32
- onSwitchWallet?: (args: {
33
- address?: string;
34
- error?: string;
35
- }) => void;
36
- para: ParaWeb;
37
- walletsWithFullAuth: TExternalWallet[];
38
- includeWalletVerification?: boolean;
39
- connectionOnly?: boolean;
40
- connectedWallet?: Omit<Wallet, 'signer'> | null;
17
+ export type CosmosSignResult = SignResult & {
18
+ cosmosPublicKeyHex?: string;
19
+ cosmosSigner?: string;
20
+ addressBech32?: string;
41
21
  };
42
- export type CosmosExternalWalletProviderConfigFull = {
43
- wallets: WalletWithType[];
44
- } & Omit<ParaCosmosProviderConfig, 'wallets'> & CosmosExternalWalletProviderConfig;
22
+ export type CosmosExternalWalletContextType = ExternalWalletContextType<CosmosSignResult> & ChainManagement<string> & ConnectParaEmbedded;
23
+ export type CosmosExternalWalletProviderConfig = ExternalWalletProviderConfigBase;
24
+ export type CosmosExternalWalletProviderConfigFull = ExternalWalletProviderConfig<WalletWithType, Omit<ParaCosmosProviderConfig, 'wallets'>>;
45
25
  export declare const CosmosExternalWalletContext: import("react").Context<CosmosExternalWalletContextType>;
46
26
  export declare function CosmosExternalWalletProvider({ children, onSwitchWallet, selectedChainId, wallets: incompleteWallets, chains, multiChain, shouldUseSuggestChainAndConnect, onSwitchChain, para, walletsWithFullAuth, connectedWallet, includeWalletVerification, connectionOnly, }: CosmosExternalWalletProviderConfigFull & PropsWithChildren): import("react/jsx-runtime").JSX.Element;
@@ -18,16 +18,19 @@ import {
18
18
  getWallet as grazGetWallet
19
19
  } from "@getpara/graz";
20
20
  import { useExternalWalletStore } from "../stores/useStore.js";
21
+ import { rawSecp256k1PubkeyToRawAddress } from "@getpara/web-sdk";
21
22
  import { formatEthHexAddress } from "../utils/formatEthHexAddress.js";
22
23
  const defaultCosmosExternalWallet = {
23
24
  wallets: [],
24
25
  chains: [],
25
26
  chainId: void 0,
26
27
  disconnect: () => Promise.resolve(),
27
- switchChain: () => Promise.resolve({}),
28
+ switchChain: () => Promise.resolve(),
28
29
  connectParaEmbedded: () => Promise.resolve({}),
29
30
  signMessage: () => Promise.resolve({}),
30
- signVerificationMessage: () => Promise.resolve({})
31
+ signVerificationMessage: () => Promise.resolve({}),
32
+ requestInfo: () => Promise.resolve({}),
33
+ disconnectBase: () => Promise.resolve()
31
34
  };
32
35
  const CosmosExternalWalletContext = createContext(defaultCosmosExternalWallet);
33
36
  function CosmosExternalWalletProvider({
@@ -63,6 +66,7 @@ function CosmosExternalWalletProvider({
63
66
  const updateExternalWalletState = useExternalWalletStore((state) => state.updateState);
64
67
  const ethAddress = multiChain ? (_b = (_a = account == null ? void 0 : account[selectedChainId]) == null ? void 0 : _a.ethereumHexAddress) == null ? void 0 : _b.toLowerCase() : (_c = account == null ? void 0 : account.ethereumHexAddress) == null ? void 0 : _c.toLowerCase();
65
68
  const address = multiChain ? (_d = account == null ? void 0 : account[selectedChainId]) == null ? void 0 : _d.bech32Address : account == null ? void 0 : account.bech32Address;
69
+ const isLinkingAccount = useRef(false);
66
70
  const verificationMessage = useRef();
67
71
  const reset = () => __async(this, null, function* () {
68
72
  yield disconnectAsync();
@@ -96,10 +100,12 @@ function CosmosExternalWalletProvider({
96
100
  if (!changeResp.error) {
97
101
  onSwitchChain(chainId);
98
102
  const storedExternalWallet = para.externalWallets[(_a2 = changeResp.ethAddress) != null ? _a2 : ""];
103
+ const { provider, providerId } = getProvider(walletType);
99
104
  para.setExternalWallet({
100
105
  address: changeResp.ethAddress,
101
106
  type: "COSMOS",
102
- provider: getProviderName(walletType),
107
+ provider,
108
+ providerId,
103
109
  addressBech32: changeResp.address,
104
110
  withFullParaAuth: storedExternalWallet.isExternalWithParaAuth,
105
111
  withVerification: includeWalletVerification,
@@ -108,15 +114,22 @@ function CosmosExternalWalletProvider({
108
114
  }
109
115
  return { error };
110
116
  });
111
- const login = (ethAddress2, address2, isFullAuthWallet, providerName) => __async(this, null, function* () {
117
+ const login = (_0) => __async(this, [_0], function* ({
118
+ address: address2,
119
+ addressBech32,
120
+ withFullParaAuth,
121
+ providerId,
122
+ provider
123
+ }) {
112
124
  try {
113
125
  return yield para.loginExternalWallet({
114
126
  externalWallet: {
115
- address: ethAddress2,
127
+ address: address2,
116
128
  type: "COSMOS",
117
- provider: providerName,
118
- addressBech32: address2,
119
- withFullParaAuth: isFullAuthWallet,
129
+ provider,
130
+ providerId,
131
+ addressBech32,
132
+ withFullParaAuth,
120
133
  withVerification: includeWalletVerification,
121
134
  isConnectionOnly: connectionOnly
122
135
  }
@@ -128,14 +141,14 @@ function CosmosExternalWalletProvider({
128
141
  });
129
142
  useEffect(() => {
130
143
  const storedExternalWallet = para.externalWallets[ethAddress != null ? ethAddress : ""];
131
- if (isConnected && !isConnecting && !isReconnecting && !isLocalConnecting && !!ethAddress && !storedExternalWallet && walletType !== GrazWalletType.PARA) {
144
+ if (isConnected && !isConnecting && !isReconnecting && !isLocalConnecting && !!ethAddress && !storedExternalWallet && walletType !== GrazWalletType.PARA && !isLinkingAccount.current) {
132
145
  reset();
133
146
  }
134
147
  }, [isConnecting, isLocalConnecting, isReconnecting, isConnected]);
135
148
  useEffect(() => {
136
149
  const connect2 = () => __async(this, null, function* () {
137
150
  var _a2;
138
- if (!isLocalConnecting && !isConnecting && !isReconnecting && connectedWallet && connectedWallet.type === "COSMOS" && (connectedWallet.isExternal ? walletType !== ((_a2 = connectedWallet.name) == null ? void 0 : _a2.toLowerCase()) : walletType !== "para")) {
151
+ if (!isLocalConnecting && !isConnecting && !isReconnecting && connectedWallet && connectedWallet.type === "COSMOS" && (connectedWallet.isExternal ? walletType !== ((_a2 = connectedWallet.name) == null ? void 0 : _a2.toLowerCase()) : walletType !== "para") && !isLinkingAccount.current) {
139
152
  const isLoggedIn = yield para.isFullyLoggedIn();
140
153
  if (!isLoggedIn) {
141
154
  return;
@@ -149,20 +162,30 @@ function CosmosExternalWalletProvider({
149
162
  });
150
163
  connect2();
151
164
  }, [isLocalConnecting, isConnecting, isReconnecting, walletType, connectedWallet]);
152
- const signMessage = (message) => __async(this, null, function* () {
153
- const wallet = grazGetWallet(walletType);
165
+ const signMessage = (_0) => __async(this, [_0], function* ({ message, externalWallet }) {
166
+ let wallet, signAddress, signEthAddress;
167
+ if (externalWallet) {
168
+ const commonWallet = wallets.find((w) => w.internalId === externalWallet.providerId);
169
+ wallet = grazGetWallet(commonWallet == null ? void 0 : commonWallet.grazType);
170
+ signAddress = externalWallet.addressBech32;
171
+ signEthAddress = externalWallet.address;
172
+ } else {
173
+ wallet = grazGetWallet(walletType);
174
+ signAddress = address;
175
+ signEthAddress = ethAddress;
176
+ }
154
177
  if (!wallet) {
155
178
  return { error: "Connected wallet not found" };
156
179
  }
157
180
  try {
158
181
  const publicKey = (yield wallet.getKey(selectedChainId)).pubKey;
159
- const signature = yield wallet.signArbitrary(selectedChainId, address, message);
182
+ const signature = yield wallet.signArbitrary(selectedChainId, signAddress, message);
160
183
  return {
161
- address: ethAddress,
162
- addressBech32: address,
184
+ address: signEthAddress,
185
+ addressBech32: signAddress,
163
186
  signature: signature.signature,
164
187
  cosmosPublicKeyHex: Buffer.from(publicKey).toString("hex"),
165
- cosmosSigner: address
188
+ cosmosSigner: signAddress
166
189
  };
167
190
  } catch (e) {
168
191
  if (e.message.includes("Request rejected")) {
@@ -172,74 +195,94 @@ function CosmosExternalWalletProvider({
172
195
  }
173
196
  });
174
197
  const signVerificationMessage = () => __async(this, null, function* () {
175
- const signature = yield signMessage(verificationMessage.current);
198
+ const signature = yield signMessage({ message: verificationMessage.current });
176
199
  return signature;
177
200
  });
178
- const connect = (walletType2, chainId) => __async(this, null, function* () {
201
+ const connectBase = (walletType2, chainId) => __async(this, null, function* () {
179
202
  var _a2;
180
- updateExternalWalletState({ isConnecting: true });
181
- const walletId = (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.id;
182
- const isFullAuthWallet = walletsWithFullAuth.includes(walletId.toUpperCase());
183
203
  if (!chainId) {
184
204
  yield disconnectAsync();
185
205
  }
186
206
  const _chainId = chainId != null ? chainId : multiChain ? chains.map((c) => c.chainId) : selectedChainId;
187
207
  if (!_chainId) {
188
- console.error("Chain id not provided.");
189
- return;
208
+ throw new Error("Chain id not provided.");
190
209
  }
191
- let address2;
192
- let ethAddress2;
193
- let error;
194
- let authState;
195
210
  if (!walletType2) {
196
- console.error("Graz wallet type not provided.");
197
- return;
198
- } else {
199
- try {
200
- let chainInfo;
201
- if (shouldUseSuggestChainAndConnect) {
202
- if (typeof _chainId !== "string") {
203
- console.error("multiChain is not compatible with shouldUseSuggestChainAndConnect.");
204
- return;
205
- }
206
- chainInfo = getChainInfo({ chainId: _chainId });
207
- if (!chainInfo) {
208
- console.error("Chain not found.");
209
- return;
210
- }
211
- }
212
- const connectedWallet2 = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType: walletType2, chainInfo }) : connectAsync({ walletType: walletType2, chainId: _chainId });
213
- const firstChain = !chainId ? selectedChainId : typeof _chainId === "string" ? _chainId : _chainId[0];
214
- address2 = connectedWallet2.accounts[firstChain].bech32Address;
215
- ethAddress2 = formatEthHexAddress(connectedWallet2.accounts[firstChain].address);
216
- if (connectedWallet2.accounts[firstChain]) {
217
- try {
218
- authState = yield login(ethAddress2, address2, isFullAuthWallet, getProviderName(walletType2));
219
- verificationMessage.current = authState.stage === "verify" ? authState.signatureVerificationMessage : void 0;
220
- } catch (err) {
221
- authState = void 0;
222
- ethAddress2 = void 0;
223
- address2 = void 0;
224
- error = err;
225
- }
211
+ throw new Error("Graz wallet type not provided.");
212
+ }
213
+ try {
214
+ let chainInfo;
215
+ if (shouldUseSuggestChainAndConnect) {
216
+ if (typeof _chainId !== "string") {
217
+ console.error("multiChain is not compatible with shouldUseSuggestChainAndConnect.");
218
+ return;
226
219
  }
227
- } catch (err) {
228
- if (err.message === "No wallet exists") {
229
- error = err.message;
230
- } else {
231
- console.error("Graz connection error:", err);
232
- error = "An unknown error occurred.";
220
+ chainInfo = getChainInfo({ chainId: _chainId });
221
+ if (!chainInfo) {
222
+ console.error("Chain not found.");
223
+ return;
233
224
  }
234
225
  }
226
+ const connectedWallet2 = yield shouldUseSuggestChainAndConnect ? suggestAndConnectAsync({ walletType: walletType2, chainInfo }) : connectAsync({ walletType: walletType2, chainId: _chainId });
227
+ const firstChain = !chainId ? selectedChainId : typeof _chainId === "string" ? _chainId : _chainId[0];
228
+ const addressBech32 = connectedWallet2.accounts[firstChain].bech32Address;
229
+ let rawAddress;
230
+ const accountAddress = connectedWallet2.accounts[firstChain].address;
231
+ if (!accountAddress || accountAddress.length === 0 || accountAddress.byteLength === 0) {
232
+ const pubKey = connectedWallet2.accounts[firstChain].pubKey;
233
+ rawAddress = rawSecp256k1PubkeyToRawAddress(pubKey);
234
+ } else {
235
+ rawAddress = accountAddress;
236
+ }
237
+ const address2 = formatEthHexAddress(rawAddress);
238
+ const { provider, providerId } = getProvider(walletType2);
239
+ return {
240
+ type: "COSMOS",
241
+ address: address2,
242
+ addressBech32,
243
+ provider,
244
+ providerId
245
+ };
246
+ } catch (e) {
247
+ let error;
248
+ if (e.message === "No wallet exists") {
249
+ error = e.message;
250
+ } else {
251
+ error = `Graz connection error: ${(_a2 = e == null ? void 0 : e.message) != null ? _a2 : e}`;
252
+ }
253
+ throw error;
254
+ }
255
+ });
256
+ const connect = (walletType2, chainId) => __async(this, null, function* () {
257
+ var _a2, _b2;
258
+ updateExternalWalletState({ isConnecting: true });
259
+ const walletId = (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.id;
260
+ const isFullAuthWallet = walletsWithFullAuth.includes(walletId.toUpperCase());
261
+ try {
262
+ const externalWallet = yield connectBase(walletType2, chainId);
263
+ const authState = yield login(__spreadProps(__spreadValues({}, externalWallet), {
264
+ withFullParaAuth: isFullAuthWallet
265
+ }));
266
+ verificationMessage.current = authState.stage === "verify" ? authState.signatureVerificationMessage : void 0;
267
+ return {
268
+ address: externalWallet.address,
269
+ authState
270
+ };
271
+ } catch (e) {
272
+ return {
273
+ error: (_b2 = e == null ? void 0 : e.message) != null ? _b2 : e
274
+ };
275
+ } finally {
276
+ updateExternalWalletState({ isConnecting: false });
235
277
  }
236
- updateExternalWalletState({ isConnecting: false });
237
- return { authState, address: address2, ethAddress: ethAddress2, error };
238
278
  });
239
279
  const getWallet = (walletType2) => incompleteWallets.find((w) => w.grazType === walletType2 || w.grazMobileType === walletType2);
240
- const getProviderName = (walletType2) => {
241
- var _a2;
242
- return (_a2 = getWallet(walletType2)) == null ? void 0 : _a2.name;
280
+ const getProvider = (walletType2) => {
281
+ const wallet = getWallet(walletType2);
282
+ return {
283
+ provider: wallet == null ? void 0 : wallet.name,
284
+ providerId: wallet == null ? void 0 : wallet.internalId
285
+ };
243
286
  };
244
287
  const wallets = incompleteWallets.map((wallet) => {
245
288
  return __spreadProps(__spreadValues({
@@ -270,6 +313,34 @@ function CosmosExternalWalletProvider({
270
313
  return { error };
271
314
  }
272
315
  }), [para, multiChain, chains, selectedChainId]);
316
+ const requestInfo = (providerId) => __async(this, null, function* () {
317
+ var _a2;
318
+ const wallet = wallets.find((w) => w.internalId === providerId);
319
+ if (!wallet) {
320
+ throw new Error(`Wallet for provider ${providerId} not found`);
321
+ }
322
+ isLinkingAccount.current = true;
323
+ try {
324
+ const externalWallet = yield connectBase(
325
+ wallet.grazType,
326
+ multiChain ? chains.map((c) => c.chainId) : selectedChainId
327
+ );
328
+ return externalWallet;
329
+ } catch (e) {
330
+ console.error("Error linking account:", e);
331
+ throw new Error((_a2 = e == null ? void 0 : e.message) != null ? _a2 : e);
332
+ }
333
+ });
334
+ const disconnectBase = () => __async(this, null, function* () {
335
+ var _a2;
336
+ isLinkingAccount.current = true;
337
+ try {
338
+ yield disconnectAsync();
339
+ } catch (e) {
340
+ console.error("Error linking account:", e);
341
+ throw new Error((_a2 = e == null ? void 0 : e.message) != null ? _a2 : e);
342
+ }
343
+ });
273
344
  return /* @__PURE__ */ jsx(
274
345
  CosmosExternalWalletContext.Provider,
275
346
  {
@@ -282,7 +353,9 @@ function CosmosExternalWalletProvider({
282
353
  switchChain,
283
354
  connectParaEmbedded,
284
355
  signMessage,
285
- signVerificationMessage
356
+ signVerificationMessage,
357
+ requestInfo,
358
+ disconnectBase
286
359
  }),
287
360
  [
288
361
  wallets,
@@ -292,7 +365,9 @@ function CosmosExternalWalletProvider({
292
365
  switchChain,
293
366
  connectParaEmbedded,
294
367
  signMessage,
295
- signVerificationMessage
368
+ signVerificationMessage,
369
+ requestInfo,
370
+ disconnectBase
296
371
  ]
297
372
  ),
298
373
  children
@@ -301,5 +376,6 @@ function CosmosExternalWalletProvider({
301
376
  }
302
377
  export {
303
378
  CosmosExternalWalletContext,
304
- CosmosExternalWalletProvider
379
+ CosmosExternalWalletProvider,
380
+ defaultCosmosExternalWallet
305
381
  };
@@ -6,6 +6,7 @@ import { isMobile } from "@getpara/web-sdk";
6
6
  const cosmostationWallet = () => {
7
7
  return {
8
8
  id: "cosmostation",
9
+ internalId: "COSMOSTATION",
9
10
  name: "Cosmostation",
10
11
  iconUrl: icon,
11
12
  isExtension: true,
@@ -6,6 +6,7 @@ import { isMobile } from "@getpara/web-sdk";
6
6
  const keplrWallet = () => {
7
7
  return {
8
8
  id: "keplr",
9
+ internalId: "KEPLR",
9
10
  name: "Keplr",
10
11
  iconUrl: icon,
11
12
  isExtension: true,
@@ -6,6 +6,7 @@ import { isMobile } from "@getpara/web-sdk";
6
6
  const leapWallet = () => {
7
7
  return {
8
8
  id: "leap",
9
+ internalId: "LEAP",
9
10
  name: "Leap",
10
11
  iconUrl: icon,
11
12
  isExtension: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getpara/cosmos-wallet-connectors",
3
- "version": "2.0.0-alpha.19",
3
+ "version": "2.0.0-alpha.21",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,14 +19,14 @@
19
19
  "@cosmjs/proto-signing": "<=0.31.3",
20
20
  "@cosmjs/stargate": "<=0.33.1",
21
21
  "@cosmjs/tendermint-rpc": "<=0.31.3",
22
- "@getpara/graz": "^1.0.0-dev.2",
23
- "@getpara/web-sdk": "2.0.0-alpha.19",
22
+ "@getpara/graz": "^2.0.0-alpha.1",
23
+ "@getpara/web-sdk": "2.0.0-alpha.21",
24
24
  "@leapwallet/cosmos-social-login-capsule-provider": "^0.0.41",
25
25
  "zustand": "^4.5.2",
26
26
  "zustand-sync-tabs": "^0.2.2"
27
27
  },
28
28
  "devDependencies": {
29
- "@getpara/react-common": "2.0.0-alpha.19",
29
+ "@getpara/react-common": "2.0.0-alpha.21",
30
30
  "@types/react": "^18.0.31",
31
31
  "@types/react-dom": "^18.2.7",
32
32
  "typescript": "^5.4.3"
@@ -39,5 +39,5 @@
39
39
  "dist",
40
40
  "package.json"
41
41
  ],
42
- "gitHead": "80d2c5d7e1203fb12378aa29bfd4d7003c7d7075"
42
+ "gitHead": "03daa3537312bc52e0640225c8abdbf8047c15da"
43
43
  }