Manage Starknet accounts
You can manage Starknet accounts in MetaMask using the
get-starknet
library or the
wallet_invokeSnap
JSON-RPC method.
Notes
- Account creation in Starknet is handled by the wallet provider. As a dapp developer, you do not create accounts directly. Instead, you can guide users to create an account with MetaMask.
- Currently, the Starknet Snap doesn't support multiple Starknet accounts.
Prerequisites
Connect to Starknet from your dapp.
Display account information
After a user connects to their Starknet account in MetaMask, you can display the account details. The following example displays the account address:
- get-starknet
- wallet_invokeSnap
const connectStarknetAccount = async () => {
const starknet = await connect();
await starknet.enable(); // Prompts the user to connect their Starknet account using MetaMask.
return starknet;
};
const showAccountInfo = async () => {
const starknet = await connectStarknetAccount();
if (account) {
document.getElementById("accountAddress").innerText = `Account Address: ${starknet.selectedAddress}`;
}
};
const showAccountInfo = async () => {
if (typeof provider !== "undefined" && provider.isMetaMask) {
try {
// Invoke the Starknet Snap to get account information.
const response = await provider // Or window.ethereum if you don't support EIP-6963.
.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@starknet-snap/snap",
request: {
method: "starknet_recoverAccounts"
}
}
});
if (response && response.length > 0) {
const account = response[0]; // Get the first account.
document.getElementById("accountAddress").innerText = `Account Address: ${account.address}`;
} else {
document.getElementById("accountAddress").innerText = "No Starknet account found";
}
} catch (error) {
console.error("Error fetching Starknet account:", error);
document.getElementById("accountAddress").innerText = "Error fetching account information";
}
} else {
document.getElementById("accountAddress").innerText = "MetaMask not detected or Snaps not supported";
}
};
// Call the function when needed.
showAccountInfo();
Retrieve the connected account
You can retrieve and display a user's connected Starknet account. The following example displays the connected account address if available, and displays buttons to connect or disconnect the account.
- get-starknet
- wallet_invokeSnap
import { useStarknet, useConnectors } from "@starknet-react/core";
import { useState, useEffect } from "react";
function AccountDisplay() {
const { account } = useStarknet();
const { available, connect, disconnect } = useConnectors();
const [accountAddress, setAccountAddress] = useState<string | undefined>();
useEffect(() => {
setAccountAddress(account?.address);
}, [account]);
return (
<div>
{accountAddress ? (
<p>Connected Account: {accountAddress}</p>
) : (
<p>No account connected</p>
)}
{available.map((connector) => (
<button key={connector.id()} onClick={() => connect(connector)}>
Connect {connector.name()}
</button>
))}
{account && (
<button onClick={() => disconnect()}>Disconnect</button>
)}
</div>
);
}
import React, { useState, useEffect } from "react";
const STARKNET_SNAP_ID = "npm:@starknet-snap/snap";
function AccountDisplay() {
const [accountAddress, setAccountAddress] = useState<string | undefined>();
const [isConnected, setIsConnected] = useState(false);
const [error, setError] = useState<string | null>(null);
const connectToSnap = async () => {
if (typeof provider !== "undefined" && provider.isMetaMask) {
try {
// Request permission to access the Snap.
await provider // Or window.ethereum if you don't support EIP-6963.
.request({
method: "wallet_requestSnaps",
params: { [STARKNET_SNAP_ID]: {} }
});
setIsConnected(true);
fetchAccount();
} catch (err) {
console.error("Error connecting to Starknet Snap:", err);
setError("Failed to connect to Starknet Snap");
}
} else {
setError("MetaMask not detected or Snaps not supported");
}
};
const disconnectFromSnap = async () => {
setAccountAddress(undefined);
setIsConnected(false);
};
const fetchAccount = async () => {
if (typeof provider !== "undefined" && provider.isMetaMask) {
try {
const response = await provider // Or window.ethereum if you don't support EIP-6963.
.request({
method: "wallet_invokeSnap",
params: {
snapId: STARKNET_SNAP_ID,
request: {
method: "starknet_recoverAccounts"
}
}
});
if (response && response.length > 0) {
setAccountAddress(response[0].address);
} else {
setError("No Starknet account found");
}
} catch (err) {
console.error("Error fetching Starknet account:", err);
setError("Failed to fetch account information");
}
}
};
useEffect(() => {
if (isConnected) {
fetchAccount();
}
}, [isConnected]);
return (
<div>
{accountAddress ? (
<p>Connected Account: {accountAddress}</p>
) : (
<p>No account connected</p>
)}
{!isConnected ? (
<button onClick={connectToSnap}>Connect to Starknet</button>
) : (
<button onClick={disconnectFromSnap}>Disconnect</button>
)}
{error && <p style={{ color: "red" }}>{error}</p>}
</div>
);
}
export default AccountDisplay;