Get Going
This walkthrough covers how to detect the UKey Wallet Cardano Provider, enable a wallet, read balances, addresses and UTxOs, and listen for account changes.
ℹ️
UKey Wallet implements the CIP-30 Cardano dApp Connector and provides a Nami compatible entrance to facilitate the access of existing Cardano DApps.
Find Provider
// UKey Wallet exposes both the ukey and nami interfaces
const ukey = window.cardano?.ukey;
const nami = window.cardano?.nami;
// Verify availability
if (!ukey) {
throw new Error("UKey Wallet Cardano provider not detected");
}
// Read wallet information
console.log("Wallet name:", ukey.name); // 'UKey Wallet'
console.log("API build:", ukey.apiVersion); // '0.1.0'
console.log("Icon URL:", ukey.icon);
Request to enable wallet
// Request wallet permission
const api = await window.cardano.ukey.enable();
// The complete API is now available
const networkId = await api.getNetworkId();
console.log("Active network:", networkId === 1 ? "mainnet" : "testnet");
Check if it is enabled
const isEnabled = await window.cardano.ukey.isEnabled();
if (isEnabled) {
const api = await window.cardano.ukey.enable();
// Continue with the API...
}
Get network ID
const api = await window.cardano.ukey.enable();
const networkId = await api.getNetworkId();
// 0 代表 testnet,1 代表 mainnet
console.log("Active network:", networkId);
Get balance
const balance = await api.getBalance();
// Returns a CBOR-encoded value (hex string)
console.log("CBOR balance:", balance);
// Decode it with cardano-serialization-lib
import { Value } from "@emurgo/cardano-serialization-lib-browser";
const value = Value.from_bytes(Buffer.from(balance, "hex"));
const lovelace = value.coin().to_str();
console.log("Current balance:", parseInt(lovelace) // 1000000, "ADA");
Read address
// Retrieve used addresses (addresses with transaction history)
const usedAddresses = await api.getUsedAddresses();
console.log("Previously used addresses:", usedAddresses);
// Retrieve unused addresses (fresh addresses)
const unusedAddresses = await api.getUnusedAddresses();
console.log("Fresh addresses:", unusedAddresses);
// Retrieve the change address
const changeAddress = await api.getChangeAddress();
console.log("Change output address:", changeAddress);
// Retrieve reward/staking addresses
const rewardAddresses = await api.getRewardAddresses();
console.log("Reward/staking address:", rewardAddresses);
Read UTxO
// Retrieve all UTxO
const utxos = await api.getUtxos();
console.log("Fetched UTxOs:", utxos);
// Retrieve UTxO with minimum amount
const utxosWithAmount = await api.getUtxos(
"1000000", // Example minimum: 1 ADA in lovelace (CBOR encoded)
);
// With pagination
const paginatedUtxos = await api.getUtxos(undefined, {
page: 0,
limit: 10,
});
Event Flow
const api = await window.cardano.ukey.enable();
// Nami-compatible event API
api.experimental.on("accountChange", (addresses) => {
console.log("Active account changed:", addresses);
});
Nami compatibility
If you want to be compatible with existing Nami access, you can also use the same set of capabilities through window.cardano.nami:
// Access through the Nami interface
const nami = window.cardano.nami;
const api = await nami.enable();
// Same API surface as UKey Wallet
const balance = await api.getBalance();