Skip to main content

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 necessary utils from eth_utils, web3 and eth_account

Import extend from web3py_ext to extend web3 to kaia web3

Define a web3 connection using Web3.HTTPProvider and RPC endpoint

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

Define a wallet from private key using Account.from_key_pair which is a role key of role-based account

Define any message to sign

Format the message using encode_defunct to make it compatible with EIP-191 standard.

Sign the transaction with the created account

Print out the signature and its length

Recover sender address with Account.recover_message from signed message

web3_role_based_value_transfer_message_recover.py

from eth_account import Account
from web3 import Web3
from eth_account.messages import encode_defunct
from eth_utils.curried import to_hex
from web3py_ext import extend
w3 = Web3(Web3.HTTPProvider('https://public-en-kairos.node.kaia.io'))
def web3_role_based_value_transfer_message_recover():
user = Account.from_key_pair(
# role-based account address
'0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea',
# transaction role key of role-based account
'0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac'
)
message = to_hex(text="I♥KLAYTN")
msghash = encode_defunct(hexstr=message)
signature = Account.sign_message(msghash, user.key)
print(signature.signature.hex())
print(len(signature.signature.hex()))
recovered = w3.klay.recover_from_message(user.address, message, signature.signature.hex(), "latest")
print("\nsender", user.address, "\nrecovered", recovered) # recovered is an original address of the member key
web3_role_based_value_transfer_message_recover()

output

❯ python web3_public_value_transfer_message_recover.py
0xb7057812498a0a6f740fb218ce5ef945dce3b8437e5662fc707dad72e096b7036f98e96d48dd10de502a1d0ce0ae91e048357721dc7502a9aa2018561c06a97d1b
132
sender 0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA
recovered 0xe15cd70a41dfb05e7214004d7d054801b2a2f06b