本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)
kaia 的 Web3.js 擴展
Web3.js Extension for kaia 提供:
- 可直接替代
new Web3(...)
,支持涉及 AccountKey 和 TxTypes 的以太坊和 kaia 交易類型。 詳見[修改 Web3 對象](#modifications-to-the-web3-object)部分
安裝
NOTE: @kaiachain/web3js-ext@^1.2.0 如果您有 ES 模組解析的問題,建議使用 node 22 或更新的版本。
Node.js
- 安裝
npm install --save @kaiachain/web3js-ext
- ESM 或 TypeScript
import { Web3 } from "@kaiachain/web3js-ext";const web3 = new Web3("https://public-en-kairos.node.kaia.io");
- CommonJS
const { Web3 } = require("@kaiachain/web3js-ext");const web3 = new Web3("https://public-en-kairos.node.kaia.io");
瀏覽器
不建議在生產中使用 CDN,但可以在下面使用 CDN 進行快速原型開發。
<script src="https://cdn.jsdelivr.net/npm/@kaiachain/web3js-ext@latest/dist/web3js-ext.bundle.js"></script><script>const web3 = new web3_ext.Web3(window.klaytn);</script>
使用方式
修改 Web3 物件
有關原始碼組織,請參閱 DESIGN。
帳戶
-
下列函式可以處理 Klaytn TxTypes。 請參閱 src/accounts/index.ts
// account independent functionsweb3.eth.accounts.recoverTransaction(rlp)web3.eth.accounts.signTransaction(obj or rlp)web3.eth.accounts.signTransactionAsFeePayer(obj or rlp)// account-bound functionsvar account = web3.eth.accounts.create()var account = web3.eth.accounts.privateKeyToAccount(priv)var account = web3.eth.accounts.decrypt(keystore)account.signTransaction(obj or rlp)account.signTransactionAsFeePayer(obj or rlp) -
下列函式可以處理 KIP-3 Klaytn keystore format v4
web3.eth.accounts.decrypt(keystore)web3.eth.accounts.decryptList(keystore)
Eth RPC 封裝程序
- 下列函式呼叫不同的 RPC,並處理 Klaytn TxTypes。 請參閱 src/eth/index.ts
// Try klay_protocolVersion, falls back to eth_protocolVersionweb3.eth.getProtocolVersion()// klay_sendTransaction if Klaytn TxType, otherwise eth_sendTransaction// 為了與Kaikas相容的額外處理web3.eth.sendTransaction(obj) // klay_sendRawTransaction if Klaytn TxType, otherwise eth_sendRawTransaction sendTransaction(obj)// klay_sendRawTransaction if Klaytn TxType, otherwise eth_sendRawTransactionweb3.eth.sendSignedTransaction(rlp)// klay_signTransaction if Klaytn TxType, otherwise eth_signTransaction// 與 Kaikas 相容的附加處理web3.eth.signTransaction(obj)
Klaytn RPC
-
下列函式呼叫 Klaytn RPC。 請參閱 src/web3.ts
web3.klay.blockNumber() // klay_blockNumberweb3.net.networkID() // net_networkID