Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #610 from kjeom/refactorAccountManagement
Browse files Browse the repository at this point in the history
refactor account management examples
  • Loading branch information
jack authored May 8, 2024
2 parents 95f7704 + 3377b81 commit 02b75f8
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 118 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#-*- coding:utf-8 -*-
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-baobab.klaytn.net'))

def web3_account_update_legacy():
user = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8')

account_update_tx = empty_tx(TxType.ACCOUNT_UPDATE)
account_update_tx = merge(account_update_tx, {
'from' : user.address,
'key' : {
'type': KeyType.LEGACY,
'key': {},
}
})
account_update_tx = fill_transaction(account_update_tx, w3)
print(to_pretty(account_update_tx))

# sign the klaytn specific transaction type with web3py
signed_tx = Account.sign_transaction(account_update_tx, user.key)
print('\nrawTransaction:', bytes_to_hex_str(signed_tx.rawTransaction))

# temddp test
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_legacy()
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ def web3_account_update_pubkey():
print(to_pretty(account_update_tx))

# sign the klaytn specific transaction type with web3py
signed_tx = Account.sign_transaction(account_update_tx, user1.key)
signed_tx = Account.sign_transaction(account_update_tx, user2.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))

# temp test
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#-*- coding:utf-8 -*-
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-baobab.klaytn.net'))

def web3_account_update_multisig():
user1 = Account.from_key("0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a")
user2 = Account.from_key("0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8")
user3 = Account.from_key("0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac")

account_update_tx = empty_tx(TxType.ACCOUNT_UPDATE)
account_update_tx = merge(account_update_tx, {
'from' : user1.address, #0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e
'key' : {
'type': KeyType.MULTISIG,
'threshold': 2,
'keys': [
{
'weight':1,
'key': compressed_key(user1),
},
{
'weight':1,
'key': compressed_key(user2),
},
{
'weight':1,
'key': compressed_key(user3),
},
]
}
})
account_update_tx = fill_transaction(account_update_tx, w3)
print(to_pretty(account_update_tx))

# sign the klaytn specific transaction type with web3py
signed_tx = Account.sign_transaction(account_update_tx, user1.key)
signed_tx = Account.sign_transaction(signed_tx.rawTransaction, user2.key)
signed_tx = Account.sign_transaction(signed_tx.rawTransaction, user3.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_multisig()
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#-*- coding:utf-8 -*-
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-baobab.klaytn.net'))

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 klaytn 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()
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def web3_public_value_transfer_sign_recover():
msghash = encode_defunct(hexstr=message)
signature = Account.sign_message(msghash, user.key)

print(signature.signature.hex())
print(len(signature.signature.hex()))
recovered = w3.klay.recover_from_message(user.address, message, signature.signature.hex(), "latest")
recovered = w3.klay.recover_from_message(
user.address,
message, signature.signature.hex(),
"latest"
)
# recovered is an original address of the public key
print("\nsender", user.address, "\nrecovered", recovered)

web3_public_value_transfer_sign_recover()
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ def web3_multisig_value_transfer_sign_recover():
msghash = encode_defunct(hexstr=message)
signature = Account.sign_message(msghash, user.key)

print(signature.signature.hex())
print(len(signature.signature.hex()))
recovered = w3.klay.recover_from_message(user.address, message, signature.signature.hex(), "latest")
print("\nsender", user.address, "\nrecovered", recovered) # recovered is an original address of the member key
recovered = w3.klay.recover_from_message(
user.address,
message, signature.signature.hex(),
"latest"
)
# recovered is an original address of the member key
print("\nsender", user.address, "\nrecovered", recovered)

web3_multisig_value_transfer_sign_recover()
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ def web3_role_based_value_transfer_sign_recover():
msghash = encode_defunct(hexstr=message)
signature = Account.sign_message(msghash, user.key)

print(signature.signature.hex())
print(len(signature.signature.hex()))
recovered = w3.klay.recover_from_message(user.address, message, signature.signature.hex(), "latest")
print("\nsender", user.address, "\nrecovered", recovered) # recovered is an original address of the member key
recovered = w3.klay.recover_from_message(
user.address,
message, signature.signature.hex(),
"latest"
)
# recovered is an original address of the member key
print("\nsender", user.address, "\nrecovered", recovered)

web3_role_based_value_transfer_sign_recover()

0 comments on commit 02b75f8

Please sign in to comment.