更新账户
导入 web3 和 @kaiachain/web3js-ext 软件包,在 web3 上添加 kaia 功能
定义要更改的发件人地址、发件人私人密钥和新私人密钥
使用指定的 kairos 测试网 URL 设置提供程序。 web3 中的提供者是访问区块链数据的只读抽象。
此外,您还可以将提供商 URL 从 kairos 更改为 quicknode
使用提供程序创建KlaytnWeb3实例
用私钥和提供者创建发件人钱包
用新私钥计算公钥
用计算出的 ** 公钥** 在 key 字段中声明一个事务。
用发送方的私人密钥签署交易
向区块链发送已签名的 tx。 它将返回交易的收据
TxTypeAccountUpdate.js
const { KlaytnWeb3, TxType, AccountKeyType, getPublicKeyFromPrivate } = require("@kaiachain/web3js-ext");const { Web3 } = require("web3");// 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";async function main() { const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io"); const web3 = new KlaytnWeb3(provider); const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv); const publicKey = getPublicKeyFromPrivate(senderNewPriv); console.log({ publicKey }); const tx = { type: TxType.AccountUpdate, from: senderAddr, key: { type: AccountKeyType.Public, key: publicKey, } }; const signResult = await senderAccount.signTransaction(tx); console.log("rawTx", signResult.rawTransaction); const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction); console.log("receipt", receipt);}main();
output
❯ js TxTypeAccountUpdate.js{ publicKey: '0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd'}signedTx 0xb54dc3c351e9a1f9ce0a120344598482226a5646838d290e46d9704db521cb94receipt { blockHash: '0x8b729b293388e53e397fe00acbd84f449763743180df5dd51725962b540e0bda', blockNumber: 148742746n, cumulativeGasUsed: 41000n, effectiveGasPrice: 25000000000n, from: '0xe15cd70a41dfb05e7214004d7d054801b2a2f06b', gasUsed: 41000n, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: 1n, to: '0xe15cd70a41dfb05e7214004d7d054801b2a2f06b', transactionHash: '0xb54dc3c351e9a1f9ce0a120344598482226a5646838d290e46d9704db521cb94', transactionIndex: 0n, type: 0n}