Actions
Connect Wallet
The dApp Kit SDK provides an action for wallet connection, allowing users to connect, disconnect, and switch between wallets and accounts.
The Connect Button Web Component provides a complete wallet connection UI.
Usage
Auto-connect is enabled by default and will attempt to restore the previous wallet connection on page reload. This provides a seamless user experience but can be disabled if needed.
The connectWallet action prompts a wallet to connect and authorize accounts for your application:
import { createDAppKit } from '@mysten/dapp-kit-core';
const dAppKit = createDAppKit({
/* config */
});
// Connect to a specific wallet
const result = await dAppKit.connectWallet({
wallet: myWallet, // UiWallet instance
account: myAccount, // Optional: specific account to select
});
console.log('Connected accounts:', result.accounts);Parameters
wallet- TheUiWalletinstance to connect toaccount(optional) - A specificUiWalletAccountto set as the selected account. Defaults to the first authorized account
Return Value
Returns a Promise that resolves to an object containing:
accounts- Array of authorizedUiWalletAccountinstances
Example
const wallets = dAppKit.stores.$wallets.get();
const unsubscribe = dAppKit.stores.$connection.subscribe((connection) => {
if (connection.isConnected) {
console.log(`Connected account address: ${connection.account.address}`);
} else {
console.log({ connection });
}
});
if (wallets.length > 0) {
try {
const { accounts } = await dAppKit.connectWallet({
wallet: wallets[0],
});
console.log('Available accounts:', accounts);
} catch (error) {
console.error('Connection failed:', error);
}
}