方法
enable
授权dApp访问一条或多条Cosmos链。
请求
window.keplr.enable(chainId: string | string[])
响应
void(用户拒绝时抛出异常)
示例
// 单条链
await window.keplr.enable("cosmoshub-4");
// 多条链
await window.keplr.enable(["cosmoshub-4", "osmo-test-5"]);
getKey
获取指定链的公钥和地址信息。
请求
{
chainId: string;
}
响应
{
name: string;
algo: string;
pubKey: Uint8Array;
address: Uint8Array;
bech32Address: string;
ethereumHexAddress: string;
isNanoLedger: boolean;
isKeystone: boolean;
}
示例
const key = await window.keplr.getKey("osmo-test-5");
console.log(key.bech32Address);
// "osmo1fjnqlywrnpj4923dsga7qf5ucvxd7ujmw3ypgv"
signAmino
以Amino(传统)格式签名交易。
请求
{
chainId: string;
signer: string;
signDoc: AminoSignDoc;
signOptions?: SignOptions;
}
响应(AminoSignResponse)
{
signed: AminoSignDoc;
signature: {
pub_key: {
type: string;
value: string;
};
signature: string;
}
}
示例
const result = await window.keplr.signAmino(
"osmo-test-5",
"osmo1fjnqlywrnpj4923dsga7qf5ucvxd7ujmw3ypgv",
{
chain_id: "osmo-test-5",
account_number: "168594",
sequence: "4",
fee: { gas: "265431", amount: [{ denom: "uosmo", amount: "6636" }] },
msgs: [
{
type: "cosmos-sdk/MsgDelegate",
value: {
delegator_address: "osmo1fjnqlywrnpj4923dsga7qf5ucvxd7ujmw3ypgv",
validator_address: "osmovaloper1unurj3mamew8msxy8p7yltmzgvf0dx42aduz33",
amount: { denom: "uosmo", amount: "1300000" }
}
}
],
memo: ""
},
{}
);
console.log(result.signature.signature);
signDirect
以Direct(Protobuf)格式签名交易。
请求
{
chainId: string;
signer: string;
signDoc: DirectSignDoc;
signOptions?: SignOptions;
}
响应(DirectSignResponse)
{
signed: DirectSignDoc;
signature: {
pub_key: {
type: string;
value: string;
};
signature: string;
}
}
signArbitrary
对任意数据进行链外签名,用于身份验证。
请求
{
chainId: string;
signer: string;
data: string;
}
响应
{
pub_key: {
type: string;
value: string;
};
signature: string;
}
示例
const result = await window.keplr.signArbitrary(
"cosmoshub-4",
"cosmos1ef9u3zq2sl4egd0wpmmrzkpzrw3dq247ad8e8w",
"Sign in with Cactus Link"
);
console.log(result.signature);
sendTx
将已签名的交易广播到网络。
请求
{
chainId: string;
tx: Uint8Array;
mode: BroadcastMode;
}
BroadcastMode 取值
| 值 | 说明 |
|---|---|
"block" |
等待交易被区块确认后返回 |
"sync" |
CheckTx通过后返回 |
"async" |
立即返回 |
响应
交易哈希字节(Uint8Array)
示例
const txHash = await window.keplr.sendTx(
"osmo-test-5",
signedTxBytes,
"sync"
);
console.log(txHash);
isConnected
检查钱包是否已与dApp连接。
响应
boolean
示例
const connected = await window.keplr.isConnected();
console.log(connected); // true 或 false
disconnect
断开钱包与dApp的连接。
示例
await window.keplr.disconnect();
getSupportChains
获取钱包支持的链列表。
响应
string[] — 链ID数组
示例
const chains = await window.keplr.getSupportChains();
console.log(chains);
// ["cosmoshub-4", "osmosis-1", ...]
verifyArbitrary
验证任意签名。
暂不支持。
请求
{
chainId: string;
signer: string;
data: string;
signature: string;
}
响应
boolean