Connecting a Cardano Wallet
Connecting to a user wallet is as simple as calling the wallet service's connect
function and passing in a valid wallet name.
Basic example
import { anvil } from "@ada-anvil/sdk-client";
try {
await anvil.wallet.connect("nami");
} catch {
console.log("Error while connecting wallet");
}
React example
Here is a simple React component that presents the user with a wallet selector and connects to that wallet upon selection.
import { anvil } from "@ada-anvil/sdk-client";
const wallets = ["nami", "eternl"] as const;
function isWallet(str: string): str is typeof wallets[number] {
return wallets.includes(str as any);
}
export function ConnectWallet() {
const connectWallet = async (wallet: string) => {
try {
if (isWallet(wallet)) await anvil.wallet.connect(wallet);
} catch (error) {
console.log("error", error);
}
};
return (
<select onChange={event => connectWallet(event.target.value)}>
{wallets.map(wallet => (
<option key={wallet} value={wallet}>
{wallet}
</option>
))}
</select>
);
}