API说明
这里列出 UKey Wallet Cosmos Provider 的方法、类型、事件和错误码。调用前请确认目标 chainId 已启用,并根据链能力选择 Amino 或 Direct 签名。
方法集
| 方法 | 说明 |
|---|---|
enable(chainIds) | 启用链并请求账户授权 |
disable(chainIds) | 禁用链 |
disconnect() | 断开所有链的连接 |
getKey(chainId) | 读取指定链账户密钥 |
signAmino(chainId, signer, signDoc) | 签名 Amino 编码交易 |
signDirect(chainId, signer, signDoc) | 签名 Protobuf 编码交易 |
sendTx(chainId, tx, mode) | 广播已签名的交易 |
signArbitrary(chainId, signer, data) | 签名任意消息 |
verifyArbitrary(chainId, signer, data, signature) | 验证签名 |
signEthereum(chainId, signer, data, type) | 签名以太坊风格数据 |
experimentalSuggestChain(chainInfo) | 添加自定义链 |
getOfflineSigner(chainId) | 获取 Direct 离线签名器 |
getOfflineSignerOnlyAmino(chainId) | 获取仅 Amino 离线签名器 |
getOfflineSignerAuto(chainId) | 自动检测最佳签名器类别 |
getChainInfosWithoutEndpoints() | 获取所有链信息 |
getChainInfoWithoutEndpoints(chainId) | 获取特定链信息 |
ping() | 核验连接状态 |
babylonConnectWallet() | 连接 Babylon 钱包 |
babylonGetKey() | 获取 Babylon 钱包密钥 |
Key 接口
interface Key {
name: string; // 展示名称
algo: string; // 签名方案
pubKey: Uint8Array; // 公钥内容字节序列
address: Uint8Array; // 地址值字节
bech32Address: string; // Bech32 地址
isNanoLedger: boolean; // 是否来自硬件设备
isKeystone: boolean; // 是否属于 Keystone 设备
}
签名文档类型
Amino 签名文档
interface StdSignDoc {
chain_id: string;
account_number: string;
sequence: string;
fee: {
amount: { denom: string; amount: string }[];
gas: string;
};
msgs: {
type: string;
value: any;
}[];
memo: string;
}
Direct 签名文档
interface DirectSignDoc {
bodyBytes: Uint8Array | null;
authInfoBytes: Uint8Array | null;
chainId: string | null;
accountNumber: Long | null;
}
签名响应类型
interface AminoSignResponse {
signed: StdSignDoc;
signature: StdSignature;
}
interface DirectSignResponse {
signed: DirectSignDoc;
signature: StdSignature;
}
interface StdSignature {
pub_key: {
type: string;
value: string; // Base64 编码后的内容
};
signature: string; // Base64 编码的签名
}
广播模式
| 模式 | 说明 |
|---|---|
block | 等待区块确认 |
sync | 等待 CheckTx |
async | 立即返回 |
链列表
| 链 | 链 ID | 原生支持 |
|---|---|---|
| Cosmos Hub | cosmoshub-4 | 是 |
| Osmosis | osmosis-1 | 是 |
| Juno | juno-1 | 是 |
| Stargaze | stargaze-1 | 是 |
| Akash | akashnet-2 | 是 |
| Secret Network | secret-4 | 是 |
| Evmos | evmos_9001-2 | 是 |
| Injective | injective-1 | 是 |
| 自定义链 | 任意链 ID | 使用 experimentalSuggestChain 补充 |
事件流
| 事件 | 说明 |
|---|---|
keplr_keystorechange | 账户或密钥状态变更 |
connect | 链连接已建立 |
disconnect | 连接已断开 |
// 监听密钥存储的变化
window.addEventListener("keplr_keystorechange", () => {
console.log("密钥状态已变化,请刷新账户信息");
});
错误码
| 错误码 | 说明 |
|---|---|
| 4001 | 用户取消了授权请求 |
| 4100 | 当前尚未授权 |
| -32603 | 内部处理失败 |