Get Going
This walkthrough covers how to detect UKey Wallet Aptos Provider in the browser, connect accounts, read network status and listen for account changes.
ℹ️
UKey Wallet Aptos Provider is compatible with the Petra wallet interface and Aptos Wallet Standard. Existing Aptos DApps usually only need to adjust the Provider detection entrance.
Find Provider
// Detect the UKey Wallet Aptos provider
const provider = window.$ukey?.aptos;
if (!provider) {
throw new Error("UKey Wallet Aptos provider not detected");
}
// Check the provider information
console.log("Detected provider:", provider.isUKey ? "UKey Wallet" : "Unrecognized");
Link Wallet
// Connect and retrieve account information
const requestResult = await provider.connect();
console.log({
address: requestResult.address, // resolved account address
publicKey: requestResult.publicKey, // Public key
});
Conn Status
const isConnected = await provider.isConnected();
console.log("Connection established:", isConnected);
Read current account
const account = await provider.account();
console.log({
address: account.address,
publicKey: account.publicKey,
});
Read network information
const network = await provider.network();
console.log("Active network:", network); // 'mainnet', 'testnet', 'devnet'
// Retrieve complete network information
const networkInfo = await provider.getNetwork();
console.log({
name: networkInfo.name,
chainId: networkInfo.chainId,
url: networkInfo.url,
});
Unlink
await provider.disconnect();
event listening
Watch Accounts
provider.onAccountChange((newAccount) => {
if (newAccount) {
console.log("Active account changed:", newAccount.address);
} else {
console.log("Wallet session disconnected");
}
});
Watch Network
provider.onNetworkChange((network) => {
console.log("Network switched:", network);
});
Migrating from Petra access
UKey Wallet is compatible with Petra's interface form. When migrating, read window.$ukey.aptos first, and then fall back to window.petra when there is no UKey Wallet:
// 旧写法(仅 Petra)
const petraProvider = window.petra;
// Updated version: try UKey Wallet first, Petra as backup
const aptosProvider = window.$ukey?.aptos || petraProvider;
Using Aptos Wallet Adapter
React projects can also use the Aptos Wallet Adapter. UKey Wallet will appear in the wallet list through Wallet Standard:
npm install @aptos-labs/wallet-adapter-react
import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";
function AptosProviderShell() {
return (
<AptosWalletAdapterProvider>
<YourApp />
</AptosWalletAdapterProvider>
);
}
UKey Wallet will be automatically detected by Aptos Wallet Adapter, no manual registration is required.