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 0x21f88d0c850ba43b7400830205949430908464d76604420162a6c880c0e1c7e641bad7a302a102dbac81e8486d68eac4e6ef9db617f7fbd79a04a3b323c982a09cdfc61f0ae0e8f847f8458207f6a06aa4e32d36dd96da8cb0f2404d81302d855defbaabf4fdbf176aabf415179564a02161361ff91f14ebccc265c55c6dff8f4fbba4b467da07c1283d8e5991051cb0signedTx 0xbf72aae23989ca58eabfca9bd84b1ad9d60354e22b55102f5f5d4af18725a58ereceipt { 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}