跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 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 account
const 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.js
pub 0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd
senderTxHashRLP 0x21f88c73850ba43b740082cd1494e15cd70a41dfb05e7214004d7d054801b2a2f06ba302a103dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdf847f8458207f5a0b1671119941dc92cde038dfe10284b7a41d5ab7fadef6be3480e919c63138dc0a06f1097e05153a83f1353efb579c191070caf5824929fbb7e7b79a266c1746b61
sentTx 0xaf6c1121ce8d7d43cfd229b7f026d73049b6982c8f9e24cc3b0f958d1c4d75bb
receipt {
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
}

讓這個頁面變得更好