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)

Role-based Account Key

AccountKeyRoleBased represents a role-based key. If an account has an AccountKeyRoleBased object and the transaction type is one except account update, the validation process is done according to each roles like below:

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

Define sender's address, private key and others role-based private keys

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

SignMsgWithRoleBased.js

const { ethers } = require("ethers");
const { Wallet } = require("@kaiachain/ethers-ext");
const senderAddr = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";
const senderPriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const txWallet = new Wallet(senderAddr, senderPriv, provider);
async function main() {
const msg = "hello";
const msghex = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(msg));
const sig = await txWallet.signMessage(msg);
console.log({ senderAddr, msg, msghex, sig });
const addr1 = ethers.utils.verifyMessage(msg, sig);
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === senderAddr.toLowerCase());
const addr2 = await provider.send("klay_recoverFromMessage", [senderAddr, msghex, sig, "latest"]);
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr.toLowerCase());
}
main().catch(console.error);

output

❯ js SignMsgWithRoleBased.js
{
senderAddr: '0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea',
msg: 'hello',
msghex: '0x68656c6c6f',
sig: '0x736460622fcfab0fa7de0ca1cde05178f01b124294a640b5f5820c7271262c6c271f1ad15f0d7b974d68eaac60d5daa1e7dd65301bbfb814beecbca1238b64121c'
}
recoveredAddr lib 0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA true
recoveredAddr rpc 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea true

Cải thiện trang này