Skip to main content

Account Update

Import the ethers and @kaiachain/ethers-ext packages to add kaia features on ethers.js

Define sender address, sender private key and new private key to be changed

Set up the provider with the specified kaia Baobab testnet URL. A provider in ethers is a read-only abstraction to access the blockchain data.

Also, you can change the provider URL from baobab to allthatnode

Create a sender's wallet with the private key and provider

Compute the public key from the new private key

Declare a transaction in the key field with the computed public key

Send the tx to the blockchain. Function sendTransaction internally signs with the private key of the account and then transmits it to the blockchain network.

The wait function returns the tx receipt if it is completed in the blockchain.

TxTypeAccountUpdate.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 provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet = new Wallet(senderAddr, senderPriv, provider); // decoupled account
async function main() {
const pub = ethers.utils.computePublicKey(senderNewPriv, true);
console.log("pub", pub);
const tx = {
type: TxType.AccountUpdate,
from: senderAddr,
key: {
type: AccountKeyType.Public,
key: pub,
}
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node TxTypeAccountUpdate.js
pub 0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd
sentTx 0xd830186f231d4793ddd190a098ee5a121d13dd8e512085cdbdecb4f03ff6e2bd
receipt {
to: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b',
from: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b',
contractAddress: null,
transactionIndex: 0,
gasUsed: BigNumber { _hex: '0xa028', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x38d57979b2f8b4c00d29a95e7996f676d05a00e1bf784697896fbf093a9465fa',
transactionHash: '0xd830186f231d4793ddd190a098ee5a121d13dd8e512085cdbdecb4f03ff6e2bd',
logs: [],
blockNumber: 148720836,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0xa028', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}