Legacy Account Key
AccountKeyLegacy
导入**@kaiachain/web3js-ext**软件包,在 web3 上添加 kaia 功能
定义要更改的发件人地址、发件人私人密钥和新私人密钥
使用指定的 kairos 测试网 URL 设置提供程序。 web3js 中的提供者是访问区块链数据的只读抽象。
此外,您还可以将提供商 URL 从 kairos 更改为 quicknode
使用提供程序定义Web3 实例
用私钥和提供者创建发件人钱包
在键字段中声明一个交易,其类型为 "AccountKeyType.Legacy`"。
用发件人的钱包签署交易
向区块链发送已签名的 tx。 它将返回交易的收据
AccountUpdateWithLegacy.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 = "0xecbf243ac167a3b5097fef758e07881582a89027";const senderPriv = "0xc696ccd259792f2ffb87e0012e4a37ae3526a3224686225af679e3aaa2aeab0d";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);async function main() { const tx = { type: TxType.AccountUpdate, from: senderAddr, key: { type: AccountKeyType.Legacy, } }; const signResult = await senderAccount.signTransaction(tx); console.log("signedTx", signResult.transactionHash); const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction); console.log("receipt", receipt);}main().catch(console.error);
output
❯ js AccountUpdateWithLegacy.jssignedTx 0x5fedabfb343f607fe0f0adfa9ef54d738312bbec98bfc02839cdae2e968f5f90receipt { to: '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', from: '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', contractAddress: null, transactionIndex: 0, gasUsed: BigNumber { _hex: '0xa028', _isBigNumber: true }, logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', blockHash: '0x114a80bfaf346ff14fc818f5053a6a94201684fc02316529d5d2cfdb68e7b98d', transactionHash: '0x5fedabfb343f607fe0f0adfa9ef54d738312bbec98bfc02839cdae2e968f5f90', logs: [], blockNumber: 152203338, confirmations: 2, cumulativeGasUsed: BigNumber { _hex: '0xa028', _isBigNumber: true }, effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true }, status: 1, type: 0, byzantium: true}