Getting Started
Learn how to detect, connect, and interact with Cosmos chains using OneKey’s provider.
Provider Detection
// Detect OneKey Cosmos provider
const provider = window.$onekey?.cosmos
if (!provider) {
throw new Error('OneKey Cosmos provider not detected')
}
// Check if it's OneKey
console.log('Provider:', provider.isOneKey ? 'OneKey' : 'Unknown')Connect to Chain
const chainId = 'cosmoshub-4'
// Enable the chain - returns key info
const key = await provider.enable(chainId)
// Or enable multiple chains at once
await provider.enable(['cosmoshub-4', 'osmosis-1', 'juno-1'])Get Account Info
const chainId = 'cosmoshub-4'
const key = await provider.getKey(chainId)
console.log({
name: key.name, // Account name
algo: key.algo, // Signing algorithm (secp256k1)
pubKey: key.pubKey, // Public key (Uint8Array)
address: key.address, // Bech32 address (Uint8Array)
bech32Address: key.bech32Address, // Bech32 formatted address
isNanoLedger: key.isNanoLedger, // Hardware wallet flag
})Add Custom Chain
Use experimentalSuggestChain to add chains not natively supported:
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,
},
})Disconnect
await provider.disconnect()Using with CosmJS
Get offline signers for use with CosmJS:
import { SigningStargateClient } from '@cosmjs/stargate'
const chainId = 'cosmoshub-4'
// Auto-detect best signer type
const offlineSigner = await provider.getOfflineSignerAuto(chainId)
// Or use specific signer types
const aminoSigner = provider.getOfflineSignerOnlyAmino(chainId)
const directSigner = provider.getOfflineSigner(chainId)
// Use with CosmJS
const client = await SigningStargateClient.connectWithSigner(
'https://rpc.cosmos.network',
offlineSigner
)
// Send tokens
const result = await client.sendTokens(
senderAddress,
recipientAddress,
[{ denom: 'uatom', amount: '1000000' }],
{ amount: [{ denom: 'uatom', amount: '5000' }], gas: '200000' }
)Migration from Keplr
OneKey’s Cosmos provider is Keplr-compatible. Simply change your provider detection:
// Before (Keplr only)
const provider = window.keplr
// After (OneKey with Keplr fallback)
const provider = window.$onekey?.cosmos || window.keplrLast updated on