Skip to main content

Get Going

This walkthrough covers how to detect the UKey Wallet Conflux Provider, request accounts, read chain information and balances, and handle account or network change events.

ℹ️

UKey Wallet Conflux Provider is compatible with both ConfluxPortal and Fluent wallet interfaces, making it easy to migrate existing Conflux DApps.


Find Provider

// Detect the UKey Wallet Conflux provider
const provider = window.$ukey?.conflux;

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

// Inspect compatibility flags
console.log("isConfluxPortal:", provider.isConfluxPortal); // Reference value: true
console.log("isFluent:", provider.isFluent); // Reference value: true
console.log("isUKey:", provider.isUKey); // Reference value: true

Ask Access

const accounts = await provider.request({
method: "cfx_requestAccounts",
});

console.log("Connection established:", accounts[0]);

Check connection

if (provider.isConnected()) {
const accounts = await provider.request({ method: "cfx_accounts" });
console.log("Selected account:", accounts);
}

Read chain ID

const chainId = await provider.request({ method: "cfx_chainId" });
console.log("Reported chain ID:", chainId); // For instance '0x405' means mainnet

Read network version

const networkId = await provider.request({ method: "net_version" });
console.log("Reported network ID:", networkId); // For instance '1029' means mainnet

Read balance

const balance = await provider.request({
method: "cfx_getBalance",
requestParams: [address, "latest_state"],
});

console.log("Current balance:", balance); // In Drips (1 CFX = 10^18 Drip)

Event Flow

Watch Accounts

provider.on("accountsChanged", (accounts) => {
if (accounts.length === 0) {
console.log("Connection closed");
} else {
console.log("Active account changed:", accounts[0]);
}
});

Listening for chain changes

provider.on("chainChanged", (chainId) => {
console.log("Chain switched:", chainId);
// Reloading the page is preferred
window.location.reload();
});

Listen for connections

provider.on("connect", ({ chainId, networkId }) => {
console.log("Connected using:", chainId, networkId);
});

provider.on("disconnect", () => {
console.log("Connection closed");
});

Addr Format

Conflux uses CIP-37 base32 addresses. Please keep the network prefix correct when displaying it to the user:

// 主网地址形态: cfx:...
// 测试网地址形态: cfxtest:...

// Example mainnet address
const address = "cfx:aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91";

// Convert between formats with js-conflux-sdk
import { format } from "js-conflux-sdk";

const hexAddress = format.hexAddress(address);
const base32Address = format.address(hexAddress, 1029); // Network ID

network

NetworkChain ID (hex)Network ID
Mainnet0x4051029
Testnet0x11