跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

Account Update

導入 web3@kaiachain/web3js-ext 軟件包,在 web3 上添加 kaia 功能

定義要更改的發件人地址發件人私人密鑰新私人密鑰

定義繳費人地址私人密鑰

使用指定的 kairos 測試網 URL 設置提供程序。 web3 中的提供者是訪問區塊鏈數據的只讀抽象。

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

使用 "web3.eth.accounts.privateKeyToAccount "創建帶有私鑰的發件人錢包

使用 "web3.eth.accounts.privateKeyToAccount "用私鑰創建付費者錢包

使用getPublicKeyFromPrivate新私鑰生成公鑰

創建具有 `type:TxType.FeeDelegatedAccountUpdate "和 "key "包含新公鑰的交易對象

發送方賬戶簽署交易

使用 "signTransactionAsFeePayer",使用付費者賬戶簽署交易

使用 web3.eth.sendSignedTransaction 將簽名交易發送到區塊鏈。 它將返回交易收據

TxTypeFeeDelegatedAccountUpdate.js

const { Web3, TxType, AccountKeyType, getPublicKeyFromPrivate } = require("@kaiachain/web3js-ext");
// Using senderPriv == senderNewPriv to execute this example repeatedly.
// But you might want to register a different private key.
const senderAddr = "0x30908464d76604420162a6c880c0e1c7e641bad7";
const senderPriv = "0xf8cc7c3813ad23817466b1802ee805ee417001fcce9376ab8728c92dd8ea0a6b";
const senderNewPriv = "0xf8cc7c3813ad23817466b1802ee805ee417001fcce9376ab8728c92dd8ea0a6b";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const feePayerAccount = web3.eth.accounts.privateKeyToAccount(feePayerPriv);
async function main() {
const publicKey = getPublicKeyFromPrivate(senderNewPriv);
console.log({ publicKey });
const tx = {
type: TxType.FeeDelegatedAccountUpdate,
from: senderAddr,
key: {
type: AccountKeyType.Public,
key: publicKey
},
};
// Sign transaction by sender
const signResult1 = await senderAccount.signTransaction(tx);
console.log("senderTxHashRLP", signResult1.rawTransaction);
// Sign and send transaction by fee payer
const signResult2 = await feePayerAccount.signTransactionAsFeePayer(signResult1.rawTransaction);
console.log("signedTx", signResult2.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult2.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ js TxTypeFeeDelegatedAccountUpdate.js
{
publicKey: '0x02dbac81e8486d68eac4e6ef9db617f7fbd79a04a3b323c982a09cdfc61f0ae0e8'
}
senderTxHashRLP 0x21f88d0c850ba43b7400830205949430908464d76604420162a6c880c0e1c7e641bad7a302a102dbac81e8486d68eac4e6ef9db617f7fbd79a04a3b323c982a09cdfc61f0ae0e8f847f8458207f6a06aa4e32d36dd96da8cb0f2404d81302d855defbaabf4fdbf176aabf415179564a02161361ff91f14ebccc265c55c6dff8f4fbba4b467da07c1283d8e5991051cb0
signedTx 0xbf72aae23989ca58eabfca9bd84b1ad9d60354e22b55102f5f5d4af18725a58e
receipt {
blockHash: '0x8d14709da1890fcefa545bdc4f4bccacbed948d799db2c34c9eb87deeb3b1e08',
blockNumber: 148744920n,
cumulativeGasUsed: 429478n,
effectiveGasPrice: 25000000000n,
from: '0x30908464d76604420162a6c880c0e1c7e641bad7',
gasUsed: 51000n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0x30908464d76604420162a6c880c0e1c7e641bad7',
transactionHash: '0xbf72aae23989ca58eabfca9bd84b1ad9d60354e22b55102f5f5d4af18725a58e',
transactionIndex: 2n,
type: 0n
}

讓這個頁面變得更好