更新賬戶
基礎
從web3py_ext導入extend,將 web3 擴展為 kaia web3
從 eth_account、web3py_ext 和 cytools 中導入必要的實用程序
使用指定的 kairos 測試網 URL 創建 Web3 實例
此外,您還可以將提供商 URL 從 kairos 更改為 quicknode 。
從私人密鑰加載交易密鑰,將其用作交易角色密鑰。 您可以用它來簽署交易
從私人密鑰加載更新密鑰,將其用作更新角色密鑰。 您可以用它來簽署該賬戶的賬戶更新交易。 這意味著只有擁有此密鑰的人才能更新此賬戶。
從私人密鑰中加載繳費人密鑰,將其用作繳費人角色密鑰。 您可以用它作為付費方簽署交易。
創建一個TxType.ACCOUNT_UPDATE類型的空交易。 您可以使用 empty_tx 工具來獲取已填寫默認字段的 tx。
使用 merge 工具,將 sender 和 keys 等附加字段合併到空 tx 中。
在這種情況下,要更新的賬戶密鑰是基於role-based密鑰,每個角色都有** pubkey**類型。 每個角色都可以使用pubkey或多重身份驗證。
您可以使用 compressed_key 工具來設置每個角色的密鑰。 它將從密鑰中提取壓縮公鑰
使用 fill_transaction 為交易對象添加更多參數,如 gas 限制...
您可以使用 to_pretty 工具打印格式化的 tx。
簽署賬戶更新 tx 類型。 您應使用 update role key 簽名,因為賬戶 "0x5bd2...cea "已是基於角色的賬戶,以更新該賬戶
從簽名交易中恢復發件人地址
如果您想查看 RLP 編碼的簽名 tx,請使用 decode_transaction 工具
向網絡發送*已簽名的交易,並等待接收,直到交易在區塊鏈中完全執行為止
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 mergefrom eth_utils.address import to_checksum_addressw3 = Web3(Web3.HTTPProvider( 'https://public-en-kairos.node.kaia.io' ))def web3_account_update_role_based(): transaction_key = Account.from_key('0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac') update_key = Account.from_key('0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda') fee_payer_key = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8') account_update_tx = empty_tx(TxType.ACCOUNT_UPDATE) account_update_tx = merge(account_update_tx, { 'from' : to_checksum_address("0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea"), 'key' : { "type": KeyType.ROLE_BASED, "keys": { "roleTransactionKey": { "type":KeyType.PUBLIC, "key": compressed_key(transaction_key) }, "roleAccountUpdateKey": { "type": KeyType.PUBLIC, "key": compressed_key(update_key) }, "roleFeePayerKey": { "type": KeyType.PUBLIC, "key": compressed_key(fee_payer_key) } } } }) 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_key.key) print('\nrawTransaction:', bytes_to_hex_str(signed_tx.rawTransaction)) recovered_tx = Account.recover_transaction(signed_tx.rawTransaction) print("\nrecovered sender address: ", recovered_tx) decoded_tx = Account.decode_transaction(signed_tx.rawTransaction) print("\ndecoded transaction:", to_pretty(decoded_tx)) 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()
❯ python 8-account_update_role_based.py{ "from": "0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA", "gas": 159000, "gasPrice": 50000000000, "nonce": 40, "chainId": 1001, "type": 32, "key": { "type": 5, "keys": { "roleTransactionKey": { "type": 2, "key": "0x03f26489914098c5da51f0f646e3000da4d6197217df082b4f7ce1530f0a0cbf2a" }, "roleAccountUpdateKey": { "type": 2, "key": "0x0263021199702b9fefca617bdcb2a9ed4a810dfa8d270d4e804a1e778450e63ec3" }, "roleFeePayerKey": { "type": 2, "key": "0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd" } } }}rawTransaction: 20f8da28850ba43b740083026d18945bd2fb3c21564c023a4a735935a2b7a238c4cceab86f05f86ca302a103f26489914098c5da51f0f646e3000da4d6197217df082b4f7ce1530f0a0cbf2aa302a10263021199702b9fefca617bdcb2a9ed4a810dfa8d270d4e804a1e778450e63ec3a302a103dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdf847f8458207f5a0985bec3e261c26aae4753692e485df1f2610d15a49c6d1a211aa722ebe07f83aa030de040be157e4ba1ee085adfa104ad52e6c5be2133e08eba32c23e8fdcef2f3recovered sender address: 0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEAdecoded transaction: { "type": 32, "nonce": 40, "gasPrice": 50000000000, "gas": 159000, "from": "0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA", "signatures": [ { "v": 2037, "r": 68913966555219307694792023212600507140724440249606323106025362523748232067130, "s": 22103284707653811363718459097471289368017860785927799915329973403830188962547 } ], "key": { "type": 5, "keys": { "roleTransactionKey": { "type": 2, "key": "0x03f26489914098c5da51f0f646e3000da4d6197217df082b4f7ce1530f0a0cbf2a" }, "roleAccountUpdateKey": { "type": 2, "key": "0x0263021199702b9fefca617bdcb2a9ed4a810dfa8d270d4e804a1e778450e63ec3" }, "roleFeePayerKey": { "type": 2, "key": "0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd" } } }, "chainId": 1001}tx hash: 0xd3315a9b59096514bb518a624effa314850f816e3b77633fd47660949518888a receipt: AttributeDict({'blockHash': HexBytes('0xcd0b362591ab28e4c5c423561ea525ad7f19e1fbc177aeef640aece7d53fc83d'), 'blockNumber': 147156811, 'contractAddress': None, 'cumulativeGasUsed': 271841, 'effectiveGasPrice': 25000000000, 'from': '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA', 'gasUsed': 81000, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA', 'transactionHash': HexBytes('0xd3315a9b59096514bb518a624effa314850f816e3b77633fd47660949518888a'), 'transactionIndex': 1, 'type': 0})