本文へスキップ
このページは英語からの機械翻訳を使用しており、誤りや不明瞭な表現が含まれている可能性があります。最も正確な情報については、オリジナルの英語版をご覧ください。頻繁な更新のため、一部のコンテンツはオリジナルの英語になっている可能性があります。Crowdinでの取り組みに参加して、このページの翻訳改善にご協力ください。 (Crowdin translation page, Contributing guide)

パブリック・アカウント・キー

AccountKeyPublicは、1つの公開鍵を持つアカウントに使用される。 アカウントにAccountKeyPublicオブジェクトがある場合、トランザクションの検証処理は以下のように行われる:

web3py_ext から extend をインポートして、web3 を kaia web3 に拡張します

必要なutilsをweb3web3py_extからインポートする。

指定されたkairosテストネットURLでWeb3インスタンスを作成する

また、プロバイダーのURLをkairosからquicknodeに変更することもできます。

秘密鍵から2つのアカウントを読み込む

TxType.ACCOUNT_UPDATE型の空のトランザクションを作成しています。 empty_txユーティリティを使えば、デフォルトのフィールドが埋められたtxを得ることができる。

mergeユーティリティを使用して、senderkeysのような追加フィールドを空のtxにマージする。

この場合、更新するアカウント・キーはpublicキーである。

キーを設定するには、compressed_keyユーティリティを使用できます。 圧縮された公開鍵を鍵から抽出する。

fill_transactionを使用して、トランザクション・オブジェクトにガス・リミットなどのパラメータを追加する。

to_prettyユーティリティを使えば、フォーマットされたtxをプリントすることができる。

アカウント更新TXタイプに署名する。 ロードされたアカウントで署名する必要があります。

**署名されたトランザクションをネットワークに送信し、ブロックチェーンで完全に実行されるまで受信を待つ。

web3_account_update_pubkey.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3py_ext.klaytn_account.utils import compressed_key
from web3py_ext.klaytn_account.account_key import KeyType
from 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 merge
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
def web3_account_update_pubkey():
user1 = Account.from_key('0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac')
user2 = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8')
account_update_tx = empty_tx(TxType.ACCOUNT_UPDATE)
account_update_tx = merge(account_update_tx, {
'from' : user1.address,
'key' : {
'type': KeyType.PUBLIC,
'key' : compressed_key(user2)
}
})
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, user2.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_pubkey()

output

❯ web3_account_update_pubkey.py
# {
# "from":"0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b",
# "gas":159000,
# "gasPrice":50000000000,
# "nonce":123,
# "chainId":1001,
# "type":32,
# "key":{
# "type":2,
# "key":"0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd"
# }.
# }
# rawTransaction:20f88d7b850ba43b740083026d1894e15cd70a41dfb05e7214004d7d054801b2a2f06ba302a103dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdf847f8458207f6a0f75f66b00c25910bfa28b70bd9b84be7d992af41803b8127b70db8d277b0d916a055594b060f8efba08cdc9e103e47000f6f83f191ea25a2a49661cc01fc449f2c
# tx ハッシュ 0x300f72798b62b8ff59287d21f186735ec95edaaf90c78ee153a7ab7ad31311fd receipt: AttributeDict({'blockHash':HexBytes('0xf76ff73748e352ae26e28dbd9485fc30f101999de67800acaa65e1b5b743e1ce'), 'blockNumber':150554008, 'contractAddress':なし, 'cumulativeGasUsed': 234641, 'effectiveGasPrice': 25000000000, 'from': '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', 'gasUsed':41000, 'logs': [], 'logsBloom':HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),'status':1, 'to': '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b', 'transactionHash':HexBytes('0x300f72798b62b8ff59287d21f186735ec95edaaf90c78ee153a7ab7ad31311fd'), 'transactionIndex':1, 'type': 0})

ページを改善してください。