本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)
Account Update
Fee Delegation
导入 ethers 和 @kaiachain/ethers-ext 软件包,在 ethers.js 上添加 kaia 功能
声明发送方、缴费方地址和私人密钥。
senderNewPriv 是要更新的私人密钥
使用指定的 URL 创建 JSON-RPC 提供程序实例
此外,您还可以更改默认提供商。 例如,使用alchemy提供者。
根据新的私人密钥计算新的公开密钥
创建事务对象以更新新的公钥
用发件人的钱包填充交易。
用发送方钱包签署交易
使用付费者的钱包将交易发送到区块链上。 函数 "sendTransactionAsFeePayer "会在发送者的签名上添加一个带有 FeePayer 私钥的签名,并将其传输到区块链网络。
等待交易收据
TxTypeFeeDelegatedAccountUpdate.js
const ethers = require("ethers");const { Wallet, TxType, AccountKeyType } = require("@kaiachain/ethers-ext");// Using senderPriv == senderNewPriv to execute this example repeatedly.// But you might want to register a different private key.const senderAddr = "0xe15cd70a41dfb05e7214004d7d054801b2a2f06b";const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";const senderNewPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");const senderWallet = new Wallet(senderAddr, senderPriv, provider); // decoupled accountconst feePayerWallet = new Wallet(feePayerPriv, provider);async function main() { const pub = ethers.utils.computePublicKey(senderNewPriv, true); console.log("pub", pub); const tx = { type: TxType.FeeDelegatedAccountUpdate, from: senderAddr, key: { type: AccountKeyType.Public, key: pub, } }; // Sign transaction by sender const populatedTx = await senderWallet.populateTransaction(tx); const senderTxHashRLP = await senderWallet.signTransaction(populatedTx); console.log("senderTxHashRLP", senderTxHashRLP); // Sign and send transaction by fee payer const sentTx = await feePayerWallet.sendTransactionAsFeePayer(senderTxHashRLP); console.log("sentTx", sentTx.hash); const receipt = await sentTx.wait(); console.log("receipt", receipt);}main();
output
❯ node TxTypeFeeDelegatedAccountUpdate.jspub 0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdsenderTxHashRLP 0x21f88c73850ba43b740082cd1494e15cd70a41dfb05e7214004d7d054801b2a2f06ba302a103dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdf847f8458207f5a0b1671119941dc92cde038dfe10284b7a41d5ab7fadef6be3480e919c63138dc0a06f1097e05153a83f1353efb579c191070caf5824929fbb7e7b79a266c1746b61sentTx 0xaf6c1121ce8d7d43cfd229b7f026d73049b6982c8f9e24cc3b0f958d1c4d75bbreceipt { to: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', from: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', contractAddress: null, transactionIndex: 2, gasUsed: BigNumber { _hex: '0xc738', _isBigNumber: true }, logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', blockHash: '0x5db31996350447e3aee600ef259f7a381c16a40ecdc0d7e6b3ac4125e86ff400', transactionHash: '0xaf6c1121ce8d7d43cfd229b7f026d73049b6982c8f9e24cc3b0f958d1c4d75bb', logs: [], blockNumber: 148732537, confirmations: 3, cumulativeGasUsed: BigNumber { _hex: '0x05d4ab', _isBigNumber: true }, effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true }, status: 1, type: 0, byzantium: true}