Skip to Content
dApps

Erste Schritte

Rufen Sie enable() auf, bevor Sie die dApp-API verwenden. Von der CIP-30-API zurückgegebene Adressen sind hexkodierte Bytes; Transaktions- und Wertparameter sind hexkodiertes CBOR. Browserbeispiele mit Buffer benötigen einen ausdrücklichen Import von buffer oder eine Hilfsfunktion zur Byte-Konvertierung.

Erfahren Sie, wie Sie Cardano mit dem Provider von OneKey erkennen, verbinden und damit interagieren.

OneKey implementiert CIP-30 (Cardano dApp Connector) für die hier dokumentierten Vorgänge. Es bietet außerdem Kompatibilität mit der Nami-Wallet.


Provider erkennen

// OneKey provides both onekey and nami interfaces const onekey = window.cardano?.onekey const nami = window.cardano?.nami // Check availability if (!onekey) { throw new Error('OneKey Cardano provider not detected') } // Get wallet info console.log('Name:', onekey.name) // 'OneKey' console.log('API Version:', onekey.apiVersion) // '0.1.0' console.log('Icon:', onekey.icon)

Wallet aktivieren

// Request wallet access const api = await window.cardano.onekey.enable() // Now you can use the full API const networkId = await api.getNetworkId() console.log('Network:', networkId === 1 ? 'Mainnet' : 'Testnet')

Prüfen, ob aktiviert

const isEnabled = await window.cardano.onekey.isEnabled() if (isEnabled) { const api = await window.cardano.onekey.enable() // Use API... }

Netzwerk-ID abrufen

const api = await window.cardano.onekey.enable() const networkId = await api.getNetworkId() // 0 = Testnet, 1 = Mainnet console.log('Network:', networkId)

Guthaben abrufen

const balance = await api.getBalance() // Returns CBOR-encoded value (hex string) console.log('Balance (CBOR):', balance) // Decode using cardano-serialization-lib import { Value, BigNum } from '@emurgo/cardano-serialization-lib-browser' const value = Value.from_bytes(Buffer.from(balance, 'hex')) const lovelace = value.coin().to_str() console.log('Balance in lovelace:', lovelace) // Keep the exact integer string.

Adressen abrufen

// Get used addresses (addresses with transaction history) const usedAddresses = await api.getUsedAddresses() console.log('Used addresses:', usedAddresses) // Get unused addresses (fresh addresses) const unusedAddresses = await api.getUnusedAddresses() console.log('Unused addresses:', unusedAddresses) // Get change address const changeAddress = await api.getChangeAddress() console.log('Change address:', changeAddress) // Get reward/staking addresses const rewardAddresses = await api.getRewardAddresses() console.log('Reward addresses:', rewardAddresses)

UTxOs abrufen

// Get all UTxOs const utxos = await api.getUtxos() console.log('UTxOs:', utxos) // Get UTxOs with minimum amount const utxosWithAmount = await api.getUtxos( Buffer.from(Value.new(BigNum.from_str('1000000')).to_bytes()).toString('hex') // CBOR-encoded Value for 1 ADA; import Value and BigNum from the serialization SDK. ) // With pagination const paginatedUtxos = await api.getUtxos(undefined, { page: 0, limit: 10, })

Ereignisbehandlung

const api = await window.cardano.onekey.enable() // Nami-compatible event API api.experimental.on('accountChange', (addresses) => { console.log('Account changed:', addresses) })

Nami-Kompatibilität

Der Kompatibilitätsalias kann zu einer anderen Erweiterung gehören. Verwenden Sie window.cardano.onekey, um OneKey ausdrücklich auszuwählen.

// Access via Nami interface const nami = window.cardano.nami const api = await nami.enable() // Same API as OneKey const balance = await api.getBalance()
Zuletzt aktualisiert am