Nhảy tới nội dung
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Public Account Key

AccountKeyPublic is used for accounts having one public key. If an account has an AccountKeyPublic object, the transaction validation process is done like below:

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

Define sender's address and private key

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

Also, you can change the provider URL from kairos to quicknode

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

Define a message to be signed and recovered

Sign the message with sender's wallet

Recover the address from signed message using ethers.utils.verifyMessage

Recover the address from signed message using klay_recoverFromMessage

SignMsgWithPubkeyExample.js

const { ethers } = require("ethers");
const { Wallet } = require("@kaiachain/ethers-ext/v5");
const senderAddr = "0xe15cd70a41dfb05e7214004d7d054801b2a2f06b";
const senderPriv = "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 msg = "hello";
const msghex = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(msg));
const sig = await wallet.signMessage(msg);
console.log({ senderAddr, msg, msghex, sig });
const addr1 = ethers.utils.verifyMessage(msg, sig);
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === wallet.address.toLowerCase());
const addr2 = await provider.send("klay_recoverFromMessage", [senderAddr, msghex, sig, "latest"]);
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === wallet.address.toLowerCase());
}
main().catch(console.error);

output

❯ js SignMsgWithPubkeyExample.js
{
senderAddr: '0xe15cd70a41dfb05e7214004d7d054801b2a2f06b',
msg: 'hello',
msghex: '0x68656c6c6f',
sig: '0xed55b92b3db953c4b4d928c99f93275d2590fe2ec95f2d8c069068d86d43ce0c0d1206f297351c6a0dfaba9c24d1a2ac293ac8f8a73d16c2b0c39ce90bc36ab71b'
}
recoveredAddr lib 0xA2a8854b1802D8Cd5De631E690817c253d6a9153 true
recoveredAddr rpc 0xa2a8854b1802d8cd5de631e690817c253d6a9153 true

Cải thiện trang này