Public Account Key
AccountKeyPublic 用于有一个公钥的账户。 如果账户有一个 AccountKeyPublic 对象,交易验证过程如下:
导入 ethers 和 @kaiachain/ethers-ext 软件包,在 ethers.js 上添加 kaia 功能
定义要更改的发件人地址、发件人私人密钥和新私人密钥
使用指定的 Kairos 测试网 URL 设置提供程序。 以太坊中的提供者是访问区块链数据的只读抽象。
此外,您还可以将提供商 URL 从 kairos 更改为 quicknode
用私钥和提供者创建发件人钱包
用新私钥计算公钥
声明一个事务,其类型为 账户密钥类型.公钥,密钥字段为新计算的公钥。
向区块链发送 tx。 函数 "sendTransaction "使用账户的私钥进行内部签名,然后将其传输到区块链网络。
如果已在区块链中完成发送,wait函数将返回发送回执。
AccountUpdateWithPubKey.js
const { ethers } = require("ethers");const { Wallet, TxType, AccountKeyType } = require("@kaiachain/ethers-ext");// Using senderPriv == senderNewPriv to execute this example repeatedly.// But you should use AccountKeyPublic to register a different private key.const senderAddr = "0xe15cd70a41dfb05e7214004d7d054801b2a2f06b";const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";const senderNewPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");const wallet = new Wallet(senderAddr, senderPriv, provider); // decoupled accountasync function main() { const senderNewPub = ethers.utils.computePublicKey(senderNewPriv, true); console.log("pub", senderNewPub); const tx = { type: TxType.AccountUpdate, from: senderAddr, key: { type: AccountKeyType.Public, key: senderNewPub, } }; const sentTx = await wallet.sendTransaction(tx); console.log("sentTx", sentTx.hash); const receipt = await sentTx.wait(); console.log("receipt", receipt);}main().catch(console.error);
output
❯ js AccountUpdateWithPubKey.jspub 0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdsentTx 0x33a634875a49d8915bc6fde14f351b81d1fc470b64aef28bf95d3ea92f2dc4f7receipt { to: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', from: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', contractAddress: null, transactionIndex: 1, gasUsed: BigNumber { _hex: '0xa028', _isBigNumber: true }, logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', blockHash: '0xb385b18c2e96c36e7fbbeb121cf2a48c0bb15f1a7af2f2969b133236ff7a14ea', transactionHash: '0x33a634875a49d8915bc6fde14f351b81d1fc470b64aef28bf95d3ea92f2dc4f7', logs: [], blockNumber: 152203491, confirmations: 2, cumulativeGasUsed: BigNumber { _hex: '0x02e456', _isBigNumber: true }, effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true }, status: 1, type: 0, byzantium: true}