基于角色的账户密钥
AccountKeyRoleBased 表示基于角色的密钥。 如果账户有一个** AccountKeyRoleBased对象,且交易类型为except account update**,那么验证过程将根据每个角色进行,如下所示:
从web3py_ext导入extend,将 web3 扩展为 kaia web3
从 eth_account、web3py_ext 和 cytools 中导入必要的实用程序
使用 kairos testnet 端点创建 Web3 实例
此外,您还可以将提供商 URL 从 kairos 更改为 quicknode 。
从私人密钥加载 3 个账户
创建一个TxType.ACCOUNT_UPDATE类型的空交易。 您可以使用 empty_tx 工具来获取已填写默认字段的 tx。
使用 merge 工具,将 sender 和 keys 等附加字段合并到空 tx 中。
在这种情况下,要更新的账户密钥是基于ole-based密钥,每个角色的可能密钥类型是传统密钥、公共密钥或多重密钥。
您可以使用 compressed_key 工具来设置每个角色的密钥。 它将从密钥中提取压缩公钥
使用 fill_transaction 为交易对象添加更多参数,如 gas 限制...
您可以使用 to_pretty 工具打印格式化的 tx。
签署账户更新 tx 类型。 您必须使用已加载的 update_role 账户签署它
向网络发送*已签名的交易,并等待接收,直到交易在区块链中完全执行为止
web3_account_update_role_based.py
from web3py_ext import extendfrom web3 import Web3from eth_account import Accountfrom web3py_ext.klaytn_account.utils import compressed_keyfrom web3py_ext.klaytn_account.account_key import KeyTypefrom web3py_ext.transaction.transaction import ( empty_tx, fill_transaction, TxType)from web3py_ext.utils.klaytn_utils import ( to_pretty, bytes_to_hex_str)from cytoolz import mergew3 = Web3(Web3.HTTPProvider( 'https://public-en-kairos.node.kaia.io' ))def web3_account_update_role_based(): # Just for testing, user1 and user2 have the same key tx_role = Account.from_key("0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac"); update_role = Account.from_key("0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda"); fee_payer_role = Account.from_key("0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"); account_update_tx = empty_tx(TxType.ACCOUNT_UPDATE) account_update_tx = merge(account_update_tx, { 'from' : update_role.address, # 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea 'key' : { 'type': KeyType.ROLE_BASED, 'keys' : { 'roleTransactionKey': { 'type': KeyType.PUBLIC, 'key': compressed_key(tx_role) }, 'roleAccountUpdateKey': { 'type': KeyType.PUBLIC, 'key': compressed_key(update_role) }, 'roleFeePayerKey': { 'type': KeyType.PUBLIC, 'key': compressed_key(fee_payer_role) } } } }) account_update_tx = fill_transaction(account_update_tx, w3) print(to_pretty(account_update_tx)) # sign the kaia specific transaction type with web3py signed_tx = Account.sign_transaction(account_update_tx, update_role.key) print('\nrawTransaction:', bytes_to_hex_str(signed_tx.rawTransaction)) 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)web3_account_update_role_based()
output
❯ py web3_account_update_role_based.py{ "from": "0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA", "gas": 159000, "gasPrice": 50000000000, "nonce": 52, "chainId": 1001, "type": 32, "key": { "type": 5, "keys": { "roleTransactionKey": { "type": 2, "key": "0x03f26489914098c5da51f0f646e3000da4d6197217df082b4f7ce1530f0a0cbf2a" }, "roleAccountUpdateKey": { "type": 2, "key": "0x0263021199702b9fefca617bdcb2a9ed4a810dfa8d270d4e804a1e778450e63ec3" }, "roleFeePayerKey": { "type": 2, "key": "0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd" } } }}rawTransaction: 20f8da34850ba43b740083026d18945bd2fb3c21564c023a4a735935a2b7a238c4cceab86f05f86ca302a103f26489914098c5da51f0f646e3000da4d6197217df082b4f7ce1530f0a0cbf2aa302a10263021199702b9fefca617bdcb2a9ed4a810dfa8d270d4e804a1e778450e63ec3a302a103dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdf847f8458207f5a089c7e842c90cb3639a929b4d012b13b68ea680543ec31a05b53863580e93de4ba05ad477c3edf141c7f88a87ba12249500eff7532a448f33a6325d95c831f51d47tx hash: 0x523877191b60450bf48ef29db40ee7191a0c5525af3255bea191c02970c088c7 receipt: AttributeDict({'blockHash': HexBytes('0xf51f2bd1926578bf912353f6eea1b025271706eb83c438429184759dae902f5b'), 'blockNumber': 150554144, 'contractAddress': None, 'cumulativeGasUsed': 81000, 'effectiveGasPrice': 25000000000, 'from': '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA', 'gasUsed': 81000, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA', 'transactionHash': HexBytes('0x523877191b60450bf48ef29db40ee7191a0c5525af3255bea191c02970c088c7'), 'transactionIndex': 0, 'type': 0})