Get Going
This walkthrough covers how to detect UKey Wallet Polkadot Provider, enable extensions, read Substrate accounts, and initiate transactions with Polkadot.js API.
ℹ️
UKey Wallet implements the Polkadot.js extension interface and can be recognized by wallet tools and DApps compatible with Substrate extensions.
Find Provider
// UKey Wallet appears as a Polkadot.js extension
const provider = window.$ukey?.polkadot;
// Note: Or use the standard web3Enable API
import { web3Enable, web3Accounts } from "@polkadot/extension-dapp";
Enable extension
// Note: Use UKey Wallet provider directly
const enabled = await provider.web3Enable("UKey Wallet Demo");
if (enabled) {
const accounts = await provider.web3Accounts();
console.log("Selected account:", accounts);
}
Use the Polkadot.js Extension API (preferred)
import { web3Enable, web3Accounts } from "@polkadot/extension-dapp";
// Enable every extension, including UKey Wallet
const extensions = await web3Enable("UKey Wallet Demo");
if (extensions.length === 0) {
throw new Error("Extension not found");
}
// Retrieve all accounts
const allAccounts = await web3Accounts();
console.log("Selected account:", allAccounts);
Read wallet
const accounts = await provider.web3Accounts();
accounts.forEach((account) => {
console.log({
address: account.address, // Note: Substrate address
name: account.name, // Note: Account name
type: account.type, // one of 'sr25519' | 'ed25519' | 'ecdsa'
genesisHash: account.genesisHash, // Chain genesis hash when needed
});
});
Subscribe to account changes
const unsubscribe = provider.web3AccountsSubscribe((accounts) => {
console.log("Active account changed:", accounts);
});
// 清理阶段记得调用 unsubscribe()
Use with Polkadot.js API
Setup
npm install @polkadot/api @polkadot/extension-dapp
Connect and sign transactions
import { ApiPromise, WsProvider } from "@polkadot/api";
import {
web3Enable,
web3FromAddress,
web3Accounts,
} from "@polkadot/extension-dapp";
// Note: Enable extension
await web3Enable("UKey Wallet Demo");
// Retrieve an account
const accounts = await web3Accounts();
const account = accounts[0];
// establish connectioned to chain
const wsProvider = new WsProvider("wss:/rpc.polkadot.io");
const api = await ApiPromise.create({ provider: wsProvider });
// Retrieve the injector
const injector = await web3FromAddress(account.address);
// Note: Create and sign transactions
const tx = api.tx.balances.transfer(
recipientAddress,
1000000000000, // equals 1 DOT in planck units
);
// Note: Sign and send
const hash = await tx.signAndSend(account.address, { signer: injector.signer });
console.log("Tx hash:", hash.toHex());