TronWeb 那些API 需要配置 API Key?

Date
Oct 9, 2024
Property
Medium
Created by
Status
In progress
Tags
Tron
TronWeb 中,並不是所有 API 都需要配置 API Key,只有當你通過 TronGrid 或其他需要 API Key 的公共節點進行操作時,才需要使用 API Key。以下是需要 API Key 和不需要 API Key 的 API 區分:

需要配置 API Key 的情況

當你使用 TronGrid 提供的公共節點(例如 https://api.trongrid.iohttps://api.shasta.trongrid.io)時,以下 API 請求需要配置 API Key:

1. 帳戶相關操作

  • tronWeb.trx.getBalance(address)
    • 獲取指定地址的餘額。
  • tronWeb.trx.getAccount(address)
    • 獲取帳戶的詳細信息。
  • tronWeb.trx.getTransactionsRelated(address, direction)
    • 獲取與指定帳戶相關的交易。

2. 交易相關操作

  • tronWeb.trx.getTransaction(txID)
    • 根據交易哈希值(txID)查詢交易信息。
  • tronWeb.trx.getConfirmedTransaction(txID)
    • 獲取已確認的交易詳細信息。

3. 智能合約相關操作

  • tronWeb.contract().at(contractAddress)
    • 加載智能合約實例。
  • tronWeb.contract().methodName().call()
    • 調用智能合約的方法。
  • tronWeb.contract().methodName().send()
    • 調用智能合約並執行交易。

4. 區塊鏈數據

  • tronWeb.trx.getCurrentBlock()
    • 獲取當前區塊的信息。
  • tronWeb.trx.getBlock(blockNumber)
    • 獲取指定區塊的信息。
  • tronWeb.trx.getBlockByHash(blockHash)
    • 根據區塊哈希值獲取區塊信息。

5. 事件監聽

  • tronWeb.getEventByTransactionID(txID)
    • 根據交易哈希獲取事件。
  • tronWeb.getContractEvents(options)
    • 獲取智能合約觸發的事件。

不需要配置 API Key 的情況

當你使用自己部署的節點(FullNode 或 SolidityNode)時,不需要 API Key,所有 API 都可以直接使用。
此外,如果你只進行本地操作,也不需要 API Key,例如:
  1. 生成帳戶
      • tronWeb.createAccount()
        • 創建一個新的 TRON 帳戶(生成私鑰和地址)。
  1. 簽名操作
      • tronWeb.trx.sign(transaction, privateKey)
        • 本地簽名交易。
      • tronWeb.utils.crypto.signString(data, privateKey)
        • 本地簽署字符串。
  1. 加解密操作
      • tronWeb.utils.crypto.getAddressFromPrivateKey(privateKey)
        • 從私鑰生成地址。
      • tronWeb.utils.crypto.verifySignature(data, signature, publicKey)
        • 驗證簽名。