Implement dict keystore and tx executor from crypto dev signer

This commit is contained in:
nolash 2021-01-09 23:12:08 +01:00
parent a841de78fb
commit c07bc3f976
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 117 additions and 167 deletions

View File

@ -1 +1 @@
include **/data/GiftableToken.json **/data/GiftableToken.bin include **/data/GiftableToken.json **/data/GiftableToken.bin requirements.txt test_requirements.txt

View File

@ -18,9 +18,9 @@ from enum import Enum
# third-party imports # third-party imports
import web3 import web3
from eth_keys import keys
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.eth.transaction import EIP155Transaction from crypto_dev_signer.keystore import DictKeystore
from crypto_dev_signer.helper import TxExecutor
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
@ -55,113 +55,48 @@ if args.v:
block_last = args.w block_last = args.w
block_all = args.ww block_all = args.ww
class DictKeystore:
def __init__(self):
self.keys = {}
self.nonces = {}
def get(self, address, password=None):
return self.keys[signer_address]
def import_file(self, keystore_file):
f = open(keystore_file, 'r')
encrypted_key = f.read()
f.close()
private_key = w3.eth.account.decrypt(encrypted_key, '')
private_key_object = keys.PrivateKey(private_key)
signer_address = private_key_object.public_key.to_checksum_address()
self.keys[signer_address] = private_key
return signer_address
class TransactionRevertError(Exception):
pass
class EthCliHelper:
def __init__(self, w3, signer, chain_id, block=False, gas_price_helper=None):
self.w3 = w3
self.nonce = {}
self.signer = signer
self.block = bool(block)
self.chain_id = chain_id
self.tx_hashes = []
self.gas_helper = self.default_gas_helper
if gas_price_helper == None:
gas_price_helper = self.default_gas_price_helper
self.gas_price_helper = gas_price_helper
def default_gas_price_helper(self):
return w3.eth.gasPrice
def default_gas_helper(self, address, tx_data, args):
return 8000000
def sign_and_send(self, signer_address, tx_buildable, force_wait=False):
if self.nonce.get(signer_address) == None:
self.nonce[signer_address] = w3.eth.getTransactionCount(signer_address, 'pending')
tx = tx_buildable.buildTransaction({
'from': signer_address,
'chainId': self.chain_id,
'gasPrice': self.gas_price_helper(),
'nonce': self.nonce[signer_address],
})
tx['gas'] = self.gas_helper(signer_address, None, None)
logg.debug('from {} nonce {} tx {}'.format(signer_address, self.nonce[signer_address], tx))
chain_tx = EIP155Transaction(tx, self.nonce[signer_address], self.chain_id)
signature = self.signer.signTransaction(chain_tx)
chain_tx_serialized = chain_tx.rlp_serialize()
tx_hash = self.w3.eth.sendRawTransaction('0x' + chain_tx_serialized.hex())
self.tx_hashes.append(tx_hash)
self.nonce[signer_address] += 1
rcpt = None
if self.block or force_wait:
rcpt = self.wait_for(tx_hash)
logg.info('tx {} gas used: {}'.format(tx_hash.hex(), rcpt['gasUsed']))
return (tx_hash.hex(), rcpt)
def wait_for(self, tx_hash=None):
if tx_hash == None:
tx_hash = self.tx_hashes[len(self.tx_hashes)-1]
i = 1
while True:
try:
return w3.eth.getTransactionReceipt(tx_hash)
except web3.exceptions.TransactionNotFound:
logg.debug('poll #{} for {}'.format(i, tx_hash.hex()))
i += 1
time.sleep(1)
if rcpt['status'] == 0:
raise TransactionRevertError(tx_hash)
return rcpt
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p)) w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
signer_address = None signer_address = None
keystore = DictKeystore() keystore = DictKeystore()
if args.y != None: if args.y != None:
logg.debug('loading keystore file {}'.format(args.y)) logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_file(args.y) signer_address = keystore.import_keystore_file(args.y)
logg.debug('now have key for signer address {}'.format(signer_address)) logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore) signer = EIP155Signer(keystore)
chain_pair = args.i.split(':') chain_pair = args.i.split(':')
chain_id = int(chain_pair[1]) chain_id = int(chain_pair[1])
helper = EthCliHelper(w3, signer, chain_id, args.ww)
def gas_helper(signer_address, code, inputs):
return 8000000
def gas_price_helper():
return 20000000000
def translateTx(tx):
return {
'from': tx['from'],
'chainId': tx['chainId'],
'gas': tx['feeUnits'],
'gasPrice': tx['feePrice'],
'nonce': tx['nonce'],
}
nonce = w3.eth.getTransactionCount(signer_address, 'pending')
helper = TxExecutor(
signer_address,
signer,
w3.eth.sendRawTransaction,
w3.eth.getTransactionReceipt,
nonce,
chain_id,
fee_helper=gas_helper,
fee_price_helper=gas_price_helper,
block=args.ww,
)
def main(): def main():
@ -174,9 +109,13 @@ def main():
f.close() f.close()
c = w3.eth.contract(abi=abi, bytecode=bytecode) c = w3.eth.contract(abi=abi, bytecode=bytecode)
tx = c.constructor(args.n, args.s, args.d) (tx_hash, rcpt) = helper.sign_and_send(
[
(tx_hash, rcpt) = helper.sign_and_send(signer_address, tx, force_wait=True) translateTx,
c.constructor(args.n, args.s, args.d).buildTransaction
],
force_wait=True,
)
logg.debug('tx hash {} rcpt {}'.format(tx_hash, rcpt)) logg.debug('tx hash {} rcpt {}'.format(tx_hash, rcpt))
address = rcpt.contractAddress address = rcpt.contractAddress
@ -190,16 +129,31 @@ def main():
for a in args.minter: for a in args.minter:
if a == signer_address: if a == signer_address:
continue continue
tx = c.functions.addMinter(a) (tx_hash, rcpt) = helper.sign_and_send(
(tx_hash, rcpt) = helper.sign_and_send(signer_address, tx) [
translateTx,
c.functions.addMinter(a).buildTransaction,
],
)
if args.account != None: if args.account != None:
mint_total = len(args.account) * args.amount mint_total = len(args.account) * args.amount
tx = c.functions.mint(mint_total) tx = c.functions.mint(mint_total)
(tx_hash, rcpt) = helper.sign_and_send(signer_address, tx, True) (tx_hash, rcpt) = helper.sign_and_send(
[
translateTx,
c.functions.mint(mint_total).buildTransaction,
],
force_wait=True,
)
for a in args.account: for a in args.account:
tx = c.functions.transfer(a, args.amount) (tx_hash, rcpt) = helper.sign_and_send(
(tx_hash, rcpt) = helper.sign_and_send(signer_address, tx) [
translateTx,
c.functions.transfer(a, args.amount).buildTransaction,
],
)
if block_last: if block_last:
helper.wait_for() helper.wait_for()
@ -208,5 +162,6 @@ def main():
sys.exit(0) sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -18,6 +18,9 @@ import time
# third-party imports # third-party imports
import web3 import web3
from eth_keys import keys from eth_keys import keys
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore import DictKeystore
from crypto_dev_signer.helper import TxExecutor
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
@ -45,41 +48,51 @@ args = argparser.parse_args()
if args.v: if args.v:
logg.setLevel(logging.DEBUG) logg.setLevel(logging.DEBUG)
block_mode = 0 block_last = args.w
if args.ww: block_all = args.ww
logg.debug('set block after each tx')
block_mode = 2
elif args.w:
logg.debug('set block until last tx')
block_mode = 1
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p)) w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
private_key = None
signer_address = None signer_address = None
keystore = DictKeystore()
if args.y != None: if args.y != None:
logg.debug('loading keystore file {}'.format(args.y)) logg.debug('loading keystore file {}'.format(args.y))
f = open(args.y, 'r') signer_address = keystore.import_keystore_file(args.y)
encrypted_key = f.read()
f.close()
private_key = w3.eth.account.decrypt(encrypted_key, '')
private_key_object = keys.PrivateKey(private_key)
signer_address = private_key_object.public_key.to_checksum_address()
logg.debug('now have key for signer address {}'.format(signer_address)) logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
network_pair = args.i.split(':') chain_pair = args.i.split(':')
network_id = int(network_pair[1]) chain_id = int(chain_pair[1])
def gas_helper(signer_address, code, inputs):
return 8000000
def gas_price_helper():
return 20000000000
def translateTx(tx):
return {
'from': tx['from'],
'chainId': tx['chainId'],
'gas': tx['feeUnits'],
'gasPrice': tx['feePrice'],
'nonce': tx['nonce'],
}
nonce = w3.eth.getTransactionCount(signer_address, 'pending')
helper = TxExecutor(
signer_address,
signer,
w3.eth.sendRawTransaction,
w3.eth.getTransactionReceipt,
nonce,
chain_id,
fee_helper=gas_helper,
fee_price_helper=gas_price_helper,
block=args.ww,
)
def waitFor(tx_hash):
i = 1
while True:
try:
return w3.eth.getTransactionReceipt(tx_hash)
except web3.exceptions.TransactionNotFound:
logg.debug('poll #{} for {}'.format(i, tx_hash.hex()))
i += 1
time.sleep(1)
def main(): def main():
@ -100,49 +113,30 @@ def main():
if args.recipient != None: if args.recipient != None:
recipient = args.recipient recipient = args.recipient
tx = c.functions.mint(args.amount).buildTransaction({ (tx_hash, rcpt) = helper.sign_and_send(
'chainId': network_id, [
'gas': 60000, translateTx,
'gasPrice': gas_price, c.functions.mint(args.amount).buildTransaction,
'nonce': nonce, ],
}) )
signed_tx = w3.eth.account.sign_transaction(tx, private_key) logg.info('mint to {} tx {}'.format(signer_address, tx_hash)) #.hex()))
tx_hash_mint = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
last_tx = tx_hash_mint
logg.info('mint to {} tx {}'.format(signer_address, tx_hash_mint.hex()))
if block_mode == 2: (tx_hash, rcpt) = helper.sign_and_send(
rcpt = waitFor(tx_hash_mint) [
if rcpt['status'] == 0: translateTx,
logg.critical('mint failed: {}'.format(tx_hash_mint.hex())) c.functions.transfer(recipient, args.amount).buildTransaction,
sys.exit(1) ],
else: )
logg.info('mint succeeded. gas used: {}'.format(rcpt['gasUsed']))
nonce += 1 logg.info('transfer to {} tx {}'.format(recipient, tx_hash))
tx = c.functions.transfer(recipient, args.amount).buildTransaction({
'chainId': network_id,
'gas': 60000,
'gasPrice': gas_price,
'nonce': nonce,
})
signed_tx = w3.eth.account.sign_transaction(tx, private_key) if block_last:
tx_hash_mint = w3.eth.sendRawTransaction(signed_tx.rawTransaction) helper.wait_for()
last_tx = tx_hash_mint
logg.info('transfer to {} tx {}'.format(recipient, tx_hash_mint.hex()))
if block_mode > 0: print(tx_hash)
rcpt = waitFor(tx_hash_mint)
if rcpt['status'] == 0:
logg.error('trasnfer failed: {}'.format(tx_hash_mint.hex()))
sys.exit(1)
else:
logg.info('transfer succeeded. gas used: {}'.format(rcpt['gasUsed']))
print(last_tx.hex())
sys.exit(0) sys.exit(0)

View File

@ -28,6 +28,7 @@ packages =
giftable_erc20_token.runnable giftable_erc20_token.runnable
install_requires = install_requires =
web3==5.12.2 web3==5.12.2
crypto-dev-signer~=0.4.13b1
[options.package_data] [options.package_data]
* = * =