Skip to Content
dApps

はじめに

鍵の読み取りや署名の要求前に、対象のチェーンを有効にしてください。そのチェーンのアカウント番号、シーケンス、手数料、RPC エンドポイントを使用します。署名例のプレースホルダーの値は使用前に置き換える必要があります。

OneKey のプロバイダーを検出して接続し、Cosmos チェーンを操作する方法を説明します。


プロバイダーの検出

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

チェーンに接続

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

アカウント情報を取得

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

カスタムチェーンを追加

experimentalSuggestChain で標準対応していないチェーンを追加します:

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

切断

await provider.disconnect()

CosmJS との併用

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

Keplr からの移行

OneKey の Cosmos プロバイダーは Keplr と互換性があります。プロバイダーの検出処理を変更するだけで利用できます:

// Before (Keplr only) const provider = window.keplr // After (OneKey with Keplr fallback) const provider = window.$onekey?.cosmos || window.keplr
最終更新日