Skip to main content

Get Going

This walkthrough covers how to detect the UKey Wallet Sui Provider, request authorization, read account and network status, and listen for changes in the application.

ℹ️

UKey Wallet implements the Sui Wallet Standard and can be detected by wallet tools and DApps that are compatible with the standard.


Find Provider

// Detect the UKey Wallet Sui provider
const provider = window.$ukey?.sui;

if (!provider) {
throw new Error("UKey Wallet Sui provider not detected");
}

Check permissions

// Check whether it is connected
const hasPermission = await provider.hasPermissions();

if (hasPermission) {
const accounts = await provider.getAccounts();
console.log("Authorized account:", accounts);
}

Request authorized connection

// Ask for connection permission
const callResult = await provider.requestPermissions();

if (callResult) {
const accounts = await provider.getAccounts();
console.log("Connection established:", accounts[0].address);
}

Read wallet

const accounts = await provider.getAccounts();

accounts.forEach((account) => {
console.log({
address: account.address, // Note: Address
publicKey: account.publicKey, // 十六进制形式的公钥
});
});

Read the current chain

const chain = await provider.getActiveChain();

console.log("Current network key:", chain); // For instance 'sui:testnet'

Conn Status

const isConnected = provider.isConnected();
console.log("Connection established:", isConnected);

await provider.disconnect();

event listening

Watch Accounts

provider.onAccountChange((account) => {
if (account) {
console.log("Active account changed:", account.address);
} else {
console.log("Connection closed");
}
});

Watch Network

provider.onNetworkChange((network) => {
console.log("Network switched:", network);
});

Using Sui Wallet Kit

React projects can use Sui Wallet Kit. UKey Wallet will automatically appear in the wallet list through Wallet Standard:

npm install @mysten/dapp-kit @mysten/sui.js @tanstack/react-query
import { SuiClientProvider, WalletProvider } from "@mysten/dapp-kit";
import { getFullnodeUrl } from "@mysten/sui.js/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

const queryClient = new QueryClient();
const networks = {
mainnet: { url: getFullnodeUrl("mainnet") },
testnet: { url: getFullnodeUrl("testnet") },
};

function SuiWalletShell() {
return (
<QueryClientProvider client={queryClient}>
<SuiClientProvider networks={networks} defaultNetwork="mainnet">
<WalletProvider>
<YourApp />
</WalletProvider>
</SuiClientProvider>
</QueryClientProvider>
);
}

UKey Wallet is automatically detected via Sui Wallet Standard.