Skip to Content
dAppsCosmos

Cosmos

Intégrez les chaînes de l’écosystème Cosmos avec le fournisseur OneKey compatible Keplr. Accès via window.$onekey.cosmos.

OneKey expose des méthodes compatibles Keplr. Vérifiez que les méthodes et chaînes requises par votre application sont prises en charge par la version cible du portefeuille.



Détection du fournisseur

// 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')

Démarrage rapide

Se connecter à une chaîne

const chainId = 'cosmoshub-4' // Request access to the chain. Read key information separately with getKey(). await provider.enable(chainId) // Or enable multiple chains at once await provider.enable(['cosmoshub-4', 'osmosis-1', 'juno-1'])

Obtenir les informations du compte

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, // Raw address bytes bech32Address: key.bech32Address, // Bech32 formatted address isNanoLedger: key.isNanoLedger, // Ledger compatibility flag })

Gestion des chaînes

Ajouter une chaîne personnalisée

Utilisez experimentalSuggestChain pour ajouter des chaînes qui ne sont pas prises en charge nativement :

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, }, })

Déconnexion

await provider.disconnect()

Signature de messages

Signer en Amino (historique)

Pour les transactions historiques encodées en Amino :

const chainId = 'cosmoshub-4' const signer = 'cosmos1...' const signDoc = { chain_id: chainId, account_number: '0', sequence: '0', fee: { amount: [{ denom: 'uatom', amount: '5000' }], gas: '200000', }, msgs: [{ type: 'cosmos-sdk/MsgSend', value: { from_address: signer, to_address: 'cosmos1...', amount: [{ denom: 'uatom', amount: '1000000' }], }, }], memo: '', } const result = await provider.signAmino(chainId, signer, signDoc) console.log({ signed: result.signed, // Signed document signature: result.signature, // { pub_key, signature } })

Signer en Direct (Protobuf)

Pour les transactions modernes encodées en Protobuf :

import { makeSignDoc } from '@cosmjs/proto-signing' const chainId = 'cosmoshub-4' const signer = 'cosmos1...' const signDoc = makeSignDoc( bodyBytes, // Transaction body authInfoBytes, // Auth info with fee chainId, accountNumber ) const result = await provider.signDirect(chainId, signer, signDoc) console.log({ signed: result.signed, signature: result.signature, })

Signer des données arbitraires

Pour signer des messages arbitraires (utile pour l’authentification) :

const chainId = 'cosmoshub-4' const signer = 'cosmos1...' const data = 'Login to MyApp at 2024-01-01T00:00:00Z' const signature = await provider.signArbitrary(chainId, signer, data) // Verify the signature const isValid = await provider.verifyArbitrary(chainId, signer, data, signature) console.log('Signature valid:', isValid)

Envoi de transactions

Diffuser une transaction

const chainId = 'cosmoshub-4' // `txBytes` is the complete signed transaction as a Uint8Array. // Broadcast modes: 'block', 'sync', 'async' const result = await provider.sendTx(chainId, txBytes, 'sync') console.log('TX Hash:', result)

Signataires hors ligne

Obtenir des signataires hors ligne à utiliser avec 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' } )

Référence de l’API

Méthodes

MéthodeDescription
enable(chainIds)Demander l’accès à une ou plusieurs chaînes ; se résout sans valeur
disable(chainIds)Désactiver une ou plusieurs chaînes
disconnect()Se déconnecter de toutes les chaînes
getKey(chainId)Obtenir la clé de compte pour la chaîne
signAmino(chainId, signer, signDoc)Signer une transaction encodée en Amino
signDirect(chainId, signer, signDoc)Signer une transaction encodée en Protobuf
sendTx(chainId, tx, mode)Diffuser une transaction signée
signArbitrary(chainId, signer, data)Signer un message arbitraire
verifyArbitrary(chainId, signer, data, signature)Vérifier une signature
experimentalSuggestChain(chainInfo)Ajouter une chaîne personnalisée
getOfflineSigner(chainId)Obtenir le signataire hors ligne Direct
getOfflineSignerOnlyAmino(chainId)Obtenir le signataire hors ligne Amino uniquement
getOfflineSignerAuto(chainId)Détecter automatiquement le meilleur type de signataire

Interface Key

interface Key { name: string // Human-readable name algo: string // Signing algorithm pubKey: Uint8Array // Public key bytes address: Uint8Array // Address bytes bech32Address: string // Bech32 formatted address isNanoLedger: boolean // Ledger compatibility flag isKeystone: boolean // True if Keystone hardware }

Chaînes prises en charge

ChaîneID de chaîneDisponibilité dans le portefeuille
Cosmos Hubcosmoshub-4Vérifier dans le portefeuille cible
Osmosisosmosis-1Vérifier dans le portefeuille cible
Junojuno-1Vérifier dans le portefeuille cible
Stargazestargaze-1Vérifier dans le portefeuille cible
Akashakashnet-2Vérifier dans le portefeuille cible
Secret Networksecret-4Vérifier dans le portefeuille cible
PersonnaliséPropre à la chaîneDemander avec experimentalSuggestChain ; l’acceptation n’est pas garantie

Gestion des erreurs

try { await provider.enable('cosmoshub-4') } catch (error) { if (error.code === 4001) { console.log('User rejected the request') } else if (error.message.includes('not supported')) { console.log('Chain not supported, try experimentalSuggestChain') } else { console.error('Connection failed:', error) } }

Migration depuis Keplr

Le fournisseur Cosmos OneKey est compatible Keplr. Modifiez simplement votre détection du fournisseur :

// Before (Keplr only) const provider = window.keplr // After (OneKey with Keplr fallback) const provider = window.$onekey?.cosmos || window.keplr
Dernière mise à jour le