Skip to content

Commit 3384beb

Browse files
committed
chore: update changelog
feat: expose autoConnect function
1 parent e91e6f8 commit 3384beb

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

.changeset/green-rockets-lie.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,15 @@
22
"thirdweb": minor
33
---
44

5-
expose autoConnect as a standalone function for use outside of react
5+
Exposes autoConnect as a standalone function for use outside of react.
6+
7+
```tsx
8+
import { autoConnect } from "thirdweb/wallets";
9+
10+
const walletManager = createConnectionManager();
11+
const isAutoConnected = await autoConnect({
12+
client,
13+
walletManager,
14+
});
15+
console.log('isAutoConnected', isAutoConnected) // true or false
16+
```

packages/thirdweb/src/exports/wallets.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,6 @@ export * as EIP1193 from "../adapters/eip1193/index.js";
160160
export { injectedProvider } from "../wallets/injected/mipdStore.js";
161161

162162
export type { ConnectionManager } from "../wallets/manager/index.js";
163+
164+
export type { AutoConnectProps } from "../wallets/connection/types.js";
165+
export { autoConnect } from "../wallets/connection/autoConnect.js";
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { Wallet } from "ethers5";
2+
import { getDefaultWallets } from "src/react/native/wallets/defaultWallets.js";
3+
import { webLocalStorage } from "src/utils/storage/webStorage.js";
4+
import { createWallet } from "../create-wallet.js";
5+
import { getInstalledWalletProviders } from "../injected/mipdStore.js";
6+
import type { ConnectionManager } from "../manager/index.js";
7+
import { autoConnectCore } from "./autoConnectCore.js";
8+
import type { AutoConnectProps } from "./types.js";
9+
10+
/**
11+
* Attempts to automatically connect to a wallet based on the provided configuration.
12+
* It combines both specified wallets and installed wallet providers that aren't already specified.
13+
*
14+
* @example
15+
*
16+
* ```tsx
17+
* import { autoConnect } from "thirdweb/wallets";
18+
*
19+
* const walletManager = createConnectionManager();
20+
* const autoConnected = await autoConnect({
21+
* client,
22+
* walletManager,
23+
* });
24+
* ```
25+
*
26+
*
27+
* @param props - The auto-connect configuration properties
28+
* @param props.wallets - Array of wallet instances to consider for auto-connection
29+
* @param walletManager - The connection manager instance handling wallet connections
30+
* @returns {boolean} a promise resolving to true or false depending on whether the auto connect function connected to a wallet or not
31+
*/
32+
export const autoConnect = async (
33+
props: AutoConnectProps & {
34+
wallets: Wallet[];
35+
walletManager: ConnectionManager;
36+
},
37+
) => {
38+
const wallets = props.wallets || getDefaultWallets(props);
39+
return autoConnectCore({
40+
storage: webLocalStorage,
41+
props: {
42+
...props,
43+
wallets,
44+
},
45+
createWalletFn: createWallet,
46+
getInstalledWallets: () => {
47+
const specifiedWalletIds = new Set(wallets.map((x) => x.id));
48+
49+
// pass the wallets that are not already specified but are installed by the user
50+
const installedWallets = getInstalledWalletProviders()
51+
.filter((x) => !specifiedWalletIds.has(x.info.rdns))
52+
.map((x) => createWallet(x.info.rdns));
53+
54+
return installedWallets;
55+
},
56+
manager: props.walletManager,
57+
});
58+
};

0 commit comments

Comments
 (0)