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 extend from web3py_ext to extend web3 to kaia web3

Import necessary utils from web3 and eth_account

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 address and private key using Account.from_key_pair which has the role to send transaction

Create an empty transaction of type TxType.VALUE_TRANSFER. You can use empty_tx util to get a tx with default fields filled.

Create a transaction with from, to and value fields to transfer.

from: the sender's address, to: target address to receive kaia.

value field means how many kaia you want to send and you can use the Web3.to_peb utils to convert kaia to peb unit.

Use fill_transaction to add rest of the fields of the transaction like gas limit, Nonce, etc. You can print all the fields after this line if you want to see

Sign the transaction with the defined account

Sending the signed transaction to the network and waiting receipt until it is completely executed in the blockchain

Recover sender address with Account.recover_transaction from signed transaction

web3_role_based_value_transfer_sign_recover.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3py_ext.transaction.transaction import (
empty_tx,
fill_transaction,
TxType
)
from cytoolz import merge
w3 = Web3(Web3.HTTPProvider('https://public-en-kairos.node.kaia.io'))
def web3_tx_sign_recover_role_based():
txRoleUser = Account.from_key_pair(
'0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea',
'0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac'
)
value_transfer_tx = empty_tx(TxType.VALUE_TRANSFER)
value_transfer_tx = merge(value_transfer_tx, {
'from' : txRoleUser.address,
'to' : txRoleUser.address,
'value' : Web3.to_peb(10, "klay"),
})
value_transfer_tx = fill_transaction(value_transfer_tx, w3)
signed_tx = Account.sign_transaction(value_transfer_tx, txRoleUser.key)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('tx hash: ', tx_hash, 'receipt: ', tx_receipt)
recovered = w3.klay.recover_from_transaction(signed_tx.rawTransaction.hex(), "latest")
print("\nsender", txRoleUser.address, "\nrecovered", recovered)
web3_tx_sign_recover_role_based()

output

❯ py web3_multisig_value_transfer_sign_recover.py
tx hash: 0x63b7dc24acdea79fc3b63059072f744f43917a989b7aead7e69d54be9e338544 receipt: AttributeDict({'blockHash': HexBytes('0x7a4ddd2165b84a8279333daaae641140bc8c64fa91d632828bcc0a7cf94e5aca'), 'blockNumber': 150557802, 'contractAddress': None, 'cumulativeGasUsed': 21000, 'effectiveGasPrice': 25000000000, 'from': '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA', 'gasUsed': 21000, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA', 'transactionHash': HexBytes('0x63b7dc24acdea79fc3b63059072f744f43917a989b7aead7e69d54be9e338544'), 'transactionIndex': 0, 'type': 0})
sender 0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA
recovered 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea