본문으로 건너뛰기
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 and role-based private keys

Define receiver's address

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

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

Sign the transaction with the wallet that has role sending transaction, 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

SignTxWithRoleBasedExample.js

const { ethers } = require("ethers");
const { Wallet, TxType, parseKlay } = require("@kaiachain/ethers-ext/v6");
const senderAddr = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";
const senderPriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new ethers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const txWallet = new Wallet(senderAddr, senderRoleTransactionPriv, provider);
async function main() {
let tx = { // use Klaytn TxType to send transaction from Klaytn typed account
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
gasLimit: 100000,
};
const populatedTx = await txWallet.populateTransaction(tx);
const rawTx = await txWallet.signTransaction(populatedTx);
console.log("rawTx", rawTx);
const sentTx = await txWallet.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 SignTxWithRoleBasedExample.js
rawTx 0x08f88641850ba43b7400830186a094c40b6909eb7085590e1c26cb3becc25368e249e9872386f26fc10000945bd2fb3c21564c023a4a735935a2b7a238c4cceaf847f8458207f6a04886eb1e6d8d5ee59fe4d125b40080409c3341fdc0a7e04b612e7d802edbeaeba0415c08f73a3789f6c27177bb5326579ffbe96f8e0db7090b08ce2fe059d949a9
sentTx 0x3f7ee99c699ad2143bf0fac72dac1ff689c992e382e4705f7366aceaecc09477
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA',
contractAddress: null,
transactionIndex: 2,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x74c7b258b81b75866cfa0b60a08be9aa23cdbd113eb314b2258b65f00475790e',
transactionHash: '0x3f7ee99c699ad2143bf0fac72dac1ff689c992e382e4705f7366aceaecc09477',
logs: [],
blockNumber: 152257043,
confirmations: 1,
cumulativeGasUsed: BigNumber { _hex: '0x062366', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
recoveredAddr rpc 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea true

페이지를 개선해 주세요