Skip to Content
dApp 接入

快速开始

使用 dApp API 前先调用 enable()。CIP-30 返回的地址是十六进制编码的字节;交易和 value 参数是十六进制编码的 CBOR。在浏览器示例中使用 Buffer 时,需要显式导入 buffer 或使用字节转换工具。

了解如何检测、连接和与 Cardano 进行交互。

OneKey 通过 CIP-30(Cardano dApp Connector)提供本文列出的操作,并提供 Nami 钱包兼容接口。


Provider 检测

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

启用钱包

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

检查是否已启用

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

获取网络 ID

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

获取余额

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.

获取地址

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

获取 UTxO

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

事件处理

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

Nami 兼容性

兼容别名可能属于其他扩展。请使用 window.cardano.onekey 明确选择 OneKey。

// Access via Nami interface const nami = window.cardano.nami const api = await nami.enable() // Same API as OneKey const balance = await api.getBalance()
最后更新于