Get Going
This walkthrough covers how to detect the UKey Wallet Cosmos Provider, enable the target chain, read account information, and complete transactions with CosmJS.
Find Provider
// Note: Detect UKey Wallet Cosmos provider
const provider = window.$ukey?.cosmos;
if (!provider) {
throw new Error("UKey Wallet Cosmos provider not detected");
}
// Note: Check if it is UKey Wallet
console.log("Detected provider:", provider.isUKey ? "UKey Wallet" : "Unrecognized");
Enable target chain
const chainId = "cosmoshub-4";
// Note: Enable chain - returns key information
const key = await provider.enable(chainId);
// Note: Or enable multiple chains at once
await provider.enable(["cosmoshub-4", "osmosis-1", "juno-1"]);
Read account information
const chainId = "cosmoshub-4";
const key = await provider.getKey(chainId);
console.log({
name: key.name, // Note: Account name
algo: key.algo, // signing algorithm, for instance secp256k1
pubKey: key.pubKey, // public key as a Uint8Array
address: key.address, // wallet address as Uint8Array
bech32Address: key.bech32Address, // Note: Bech32 format address
isNanoLedger: key.isNanoLedger, // whether this signer is backed by hardware
});
Add custom chain
If the target chain does not yet have built-in support, you can provide the chain configuration via experimentalSuggestChain and request the user to add it:
await provider.experimentalSuggestChain({
chainId: "my-chain-1",
chainName: "My Custom Chain",
rpc: "https://rpc.mychain.io",
rest: "https://lcd.mychain.io",
bip44: { coinType: 118 },
bech32Config: {
bech32PrefixAccAddr: "mychain",
bech32PrefixAccPub: "mychainpub",
bech32PrefixValAddr: "mychainvaloper",
bech32PrefixValPub: "mychainvaloperpub",
bech32PrefixConsAddr: "mychainvalcons",
bech32PrefixConsPub: "mychainvalconspub",
},
currencies: [
{
coinDenom: "TOKEN",
coinMinimalDenom: "utoken",
coinDecimals: 6,
},
],
feeCurrencies: [
{
coinDenom: "TOKEN",
coinMinimalDenom: "utoken",
coinDecimals: 6,
gasPriceStep: { low: 0.01, average: 0.025, high: 0.04 },
},
],
stakeCurrency: {
coinDenom: "TOKEN",
coinMinimalDenom: "utoken",
coinDecimals: 6,
},
});
Unlink
await provider.disconnect();
Use with CosmJS
You can get the offline signer from Provider and then hand it over to CosmJS to construct and send the transaction:
import { SigningStargateClient } from "@cosmjs/stargate";
const chainId = "cosmoshub-4";
// Note: Automatically detect the best signer type
const offlineSigner = await provider.getOfflineSignerAuto(chainId);
// Note: or use a specific signer type
const aminoSigner = provider.getOfflineSignerOnlyAmino(chainId);
const directSigner = provider.getOfflineSigner(chainId);
// Note: Use with CosmJS
const client = await SigningStargateClient.connectWithSigner(
"https://rpc.cosmos.network",
offlineSigner,
);
// Note: Send tokens
const callResult = await client.sendTokens(
senderAddress,
recipientAddress,
[{ denom: "uatom", amount: "1000000" }],
{ amount: [{ denom: "uatom", amount: "5000" }], gas: "200000" },
);
Migrating from Keplr access
UKey Wallet Cosmos Provider is compatible with the Keplr interface. During migration, UKey Wallet is read first, and then falls back to Keplr if it is not installed:
// 旧写法(仅 Keplr)
const keplrProvider = window.keplr;
// Updated version: UKey Wallet first, Keplr as backup
const cosmosProvider = window.$ukey?.cosmos || keplrProvider;