Nhảy tới nội dung

Legacy Account Key

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

Define sender's address and private key

Define receiver's address

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

Create a value transfer transaction with type: TxType.ValueTransfer so that it can be recovered later with klay_recoverFromTransaction

Sign the transaction with sender's wallet, the populateTransaction method add more params to the transaction object such as gas, nonce...

Send the signed transaction to kaia network

Wait for the transaction to be completed and print the receipt

Recover the address from signed transaction using klay_recoverFromMessage

SignTxWithLegacyExample.js

const { ethers } = require("ethers");
const { Wallet, TxType } = require("@kaiachain/ethers-ext");
const senderAddr = "0xb2ba72e1f84b7b8cb15487a2bf20328f2cf40c25";
const senderPriv = "0xebceaca693ea3740231be94f38af6090d3aded336725d26a09b7d14e8e485e1e";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet = new Wallet(senderPriv, provider);
async function main() {
const tx = {
// for should not be called by a legacy transaction for calling klay_recoverFromTransaction
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: 0,
};
const populatedTx = await wallet.populateTransaction(tx);
const rawTx = await wallet.signTransaction(populatedTx);
console.log("rawTx", rawTx);
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
const addr = await provider.send("klay_recoverFromTransaction", [rawTx, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
}
main().catch(console.error);

output

❯ js SignTxWithLegacyExample.js
rawTx 0x08f87e05850ba43b740082cd1494c40b6909eb7085590e1c26cb3becc25368e249e98094b2ba72e1f84b7b8cb15487a2bf20328f2cf40c25f847f8458207f5a070d2c5f2dd52d6a3b7bba65594b2c875b8ce0473e16b06b43d5c2ec9ebfcd467a048af2fc51f75961a9cebfb1685b12d99db143bbd42a76f9c50970b807e1010b9
sentTx 0xecb117338d7a0e7e9444886ebdab5d0e14fd1b02fa476fee839a2fc3b105f391
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xb2ba72e1f84b7B8Cb15487A2bf20328F2cF40c25',
contractAddress: null,
transactionIndex: 1,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x7eae10aa2fcd3c42ac1705b63ad025f972a222bb71d65feeb0000d92c2e73732',
transactionHash: '0xecb117338d7a0e7e9444886ebdab5d0e14fd1b02fa476fee839a2fc3b105f391',
logs: [],
blockNumber: 152256170,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0x029636', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
recoveredAddr rpc 0xb2ba72e1f84b7b8cb15487a2bf20328f2cf40c25 true