Skip to main content

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 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

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_public_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_pubkey():
user = Account.from_key_pair(
'0xe15cd70a41dfb05e7214004d7d054801b2a2f06b',
'0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8'
)
value_transfer_tx = empty_tx(TxType.VALUE_TRANSFER)
value_transfer_tx = merge(value_transfer_tx, {
'from' : user.address,
'to' : user.address,
'value' : Web3.to_peb(10, "klay"),
})
value_transfer_tx = fill_transaction(value_transfer_tx, w3)
signed_tx = Account.sign_transaction(value_transfer_tx, user.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", user.address, "\nrecovered", recovered)
web3_tx_sign_recover_pubkey()

output

❯ python txTypeLegacyTransaction.py
tx hash: 0x26b8f66ac07e666163c65396fb24a5940ccc5bdc84a1de5301550f7fe096e2d3 receipt: AttributeDict({'blockHash': HexBytes('0x1731103ca6b336d0b1f6731632e7339f710f2316b5efb63882108bd0a5207f45'), 'blockNumber': 150557786, 'contractAddress': None, 'cumulativeGasUsed': 21000, 'effectiveGasPrice': 25000000000, 'from': '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', 'gasUsed': 21000, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', 'transactionHash': HexBytes('0x26b8f66ac07e666163c65396fb24a5940ccc5bdc84a1de5301550f7fe096e2d3'), 'transactionIndex': 0, 'type': 0})
sender 0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b
recovered 0xe15cd70a41dfb05e7214004d7d054801b2a2f06b