Remove dead code, logger names
This commit is contained in:
parent
8571447ce7
commit
397f240b6c
@ -1 +0,0 @@
|
|||||||
from .tx import EthTxExecutor
|
|
@ -1,58 +0,0 @@
|
|||||||
# standard imports
|
|
||||||
import logging
|
|
||||||
|
|
||||||
# local imports
|
|
||||||
from crypto_dev_signer.helper import TxExecutor
|
|
||||||
from crypto_dev_signer.error import NetworkError
|
|
||||||
|
|
||||||
logg = logging.getLogger()
|
|
||||||
logging.getLogger('web3').setLevel(logging.CRITICAL)
|
|
||||||
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
|
|
||||||
|
|
||||||
|
|
||||||
class EthTxExecutor(TxExecutor):
|
|
||||||
|
|
||||||
def __init__(self, w3, sender, signer, chain_id, verifier=None, block=False):
|
|
||||||
self.w3 = w3
|
|
||||||
nonce = self.w3.eth.getTransactionCount(sender, 'pending')
|
|
||||||
super(EthTxExecutor, self).__init__(sender, signer, self.translator, self.dispatcher, self.reporter, nonce, chain_id, self.fee_helper, self.fee_price_helper, verifier, block)
|
|
||||||
|
|
||||||
|
|
||||||
def fee_helper(self, tx):
|
|
||||||
estimate = self.w3.eth.estimateGas(tx)
|
|
||||||
if estimate < 21000:
|
|
||||||
estimate = 21000
|
|
||||||
logg.debug('estimate {} {}'.format(tx, estimate))
|
|
||||||
return estimate
|
|
||||||
|
|
||||||
|
|
||||||
def fee_price_helper(self):
|
|
||||||
return self.w3.eth.gasPrice
|
|
||||||
|
|
||||||
|
|
||||||
def dispatcher(self, tx):
|
|
||||||
error_object = None
|
|
||||||
try:
|
|
||||||
tx_hash = self.w3.eth.sendRawTransaction(tx)
|
|
||||||
except ValueError as e:
|
|
||||||
error_object = e.args[0]
|
|
||||||
logg.error('node could not intepret rlp {}'.format(tx))
|
|
||||||
if error_object != None:
|
|
||||||
raise NetworkError(error_object)
|
|
||||||
return tx_hash
|
|
||||||
|
|
||||||
|
|
||||||
def reporter(self, tx):
|
|
||||||
return self.w3.eth.getTransactionReceipt(tx)
|
|
||||||
|
|
||||||
|
|
||||||
def translator(self, tx):
|
|
||||||
if tx.get('feePrice') != None:
|
|
||||||
tx['gasPrice'] = tx['feePrice']
|
|
||||||
del tx['feePrice']
|
|
||||||
|
|
||||||
if tx.get('feeUnits') != None:
|
|
||||||
tx['gas'] = tx['feeUnits']
|
|
||||||
del tx['feeUnits']
|
|
||||||
|
|
||||||
return tx
|
|
@ -9,7 +9,7 @@ from hexathon import int_to_minbytes
|
|||||||
# local imports
|
# local imports
|
||||||
from crypto_dev_signer.eth.encoding import chain_id_to_v
|
from crypto_dev_signer.eth.encoding import chain_id_to_v
|
||||||
|
|
||||||
logg = logging.getLogger().getChild(__name__)
|
logg = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Signer:
|
class Signer:
|
||||||
|
@ -16,7 +16,7 @@ from crypto_dev_signer.eth.encoding import chain_id_to_v
|
|||||||
#from crypto_dev_signer.eth.rlp import rlp_encode
|
#from crypto_dev_signer.eth.rlp import rlp_encode
|
||||||
import rlp
|
import rlp
|
||||||
|
|
||||||
logg = logging.getLogger().getChild(__name__)
|
logg = logging.getLogger(__name__)
|
||||||
|
|
||||||
rlp_encode = rlp.encode
|
rlp_encode = rlp.encode
|
||||||
|
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
import logging
|
|
||||||
import re
|
|
||||||
|
|
||||||
from web3 import Web3 as Web3super
|
|
||||||
from web3 import WebsocketProvider, HTTPProvider
|
|
||||||
from .middleware import PlatformMiddleware
|
|
||||||
|
|
||||||
re_websocket = re.compile('^wss?://')
|
|
||||||
re_http = re.compile('^https?://')
|
|
||||||
|
|
||||||
logg = logging.getLogger(__file__)
|
|
||||||
|
|
||||||
|
|
||||||
def create_middleware(ipcpath):
|
|
||||||
PlatformMiddleware.ipcaddr = ipcpath
|
|
||||||
return PlatformMiddleware
|
|
||||||
|
|
||||||
|
|
||||||
# overrides the original Web3 constructor
|
|
||||||
#def Web3(blockchain_provider='ws://localhost:8546', ipcpath=None):
|
|
||||||
def Web3(provider, ipcpath=None):
|
|
||||||
w3 = Web3super(provider)
|
|
||||||
|
|
||||||
if ipcpath != None:
|
|
||||||
logg.info('using signer middleware with ipc {}'.format(ipcpath))
|
|
||||||
w3.middleware_onion.add(create_middleware(ipcpath))
|
|
||||||
|
|
||||||
w3.eth.personal = w3.geth.personal
|
|
||||||
return w3
|
|
@ -1,116 +0,0 @@
|
|||||||
# standard imports
|
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
import socket
|
|
||||||
import uuid
|
|
||||||
import json
|
|
||||||
|
|
||||||
logg = logging.getLogger(__file__)
|
|
||||||
|
|
||||||
|
|
||||||
def jsonrpc_request(method, params):
|
|
||||||
uu = uuid.uuid4()
|
|
||||||
return {
|
|
||||||
"jsonrpc": "2.0",
|
|
||||||
"id": str(uu),
|
|
||||||
"method": method,
|
|
||||||
"params": params,
|
|
||||||
}
|
|
||||||
|
|
||||||
class PlatformMiddleware:
|
|
||||||
|
|
||||||
# id for the request is not available, meaning we cannot easily short-circuit
|
|
||||||
# hack workaround
|
|
||||||
id_seq = -1
|
|
||||||
re_personal = re.compile('^personal_.*')
|
|
||||||
ipcaddr = None
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, make_request, w3):
|
|
||||||
self.w3 = w3
|
|
||||||
self.make_request = make_request
|
|
||||||
if self.ipcaddr == None:
|
|
||||||
raise AttributeError('ipcaddr not set')
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: understand what format input params come in
|
|
||||||
# single entry input gives a tuple on params, wtf...
|
|
||||||
# dict input comes as [{}] and fails if not passed on as an array
|
|
||||||
@staticmethod
|
|
||||||
def _translate_params(params):
|
|
||||||
#if params.__class__.__name__ == 'tuple':
|
|
||||||
# r = []
|
|
||||||
# for p in params:
|
|
||||||
# r.append(p)
|
|
||||||
# return r
|
|
||||||
|
|
||||||
if params.__class__.__name__ == 'list' and len(params) > 0:
|
|
||||||
return params[0]
|
|
||||||
|
|
||||||
return params
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: DRY
|
|
||||||
def __call__(self, method, suspect_params):
|
|
||||||
|
|
||||||
self.id_seq += 1
|
|
||||||
logg.debug('in middleware method {} params {} ipcpath {}'.format(method, suspect_params, self.ipcaddr))
|
|
||||||
|
|
||||||
if self.re_personal.match(method) != None:
|
|
||||||
params = PlatformMiddleware._translate_params(suspect_params)
|
|
||||||
# multiple providers is removed in web3.py 5.12.0
|
|
||||||
# https://github.com/ethereum/web3.py/issues/1701
|
|
||||||
# thus we need a workaround to use the same web3 instance
|
|
||||||
s = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0)
|
|
||||||
ipc_provider_workaround = s.connect(self.ipcaddr)
|
|
||||||
|
|
||||||
logg.info('redirecting method {} params {} original params {}'.format(method, params, suspect_params))
|
|
||||||
o = jsonrpc_request(method, params[0])
|
|
||||||
j = json.dumps(o)
|
|
||||||
logg.debug('send {}'.format(j))
|
|
||||||
s.send(j.encode('utf-8'))
|
|
||||||
r = s.recv(4096)
|
|
||||||
s.close()
|
|
||||||
logg.debug('got recv {}'.format(str(r)))
|
|
||||||
jr = json.loads(r)
|
|
||||||
jr['id'] = self.id_seq
|
|
||||||
#return str(json.dumps(jr))
|
|
||||||
return jr
|
|
||||||
|
|
||||||
elif method == 'eth_signTransaction':
|
|
||||||
params = PlatformMiddleware._translate_params(suspect_params)
|
|
||||||
s = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0)
|
|
||||||
ipc_provider_workaround = s.connect(self.ipcaddr)
|
|
||||||
logg.info('redirecting method {} params {} original params {}'.format(method, params, suspect_params))
|
|
||||||
o = jsonrpc_request(method, params[0])
|
|
||||||
j = json.dumps(o)
|
|
||||||
logg.debug('send {}'.format(j))
|
|
||||||
s.send(j.encode('utf-8'))
|
|
||||||
r = s.recv(4096)
|
|
||||||
s.close()
|
|
||||||
logg.debug('got recv {}'.format(str(r)))
|
|
||||||
jr = json.loads(r)
|
|
||||||
jr['id'] = self.id_seq
|
|
||||||
#return str(json.dumps(jr))
|
|
||||||
return jr
|
|
||||||
|
|
||||||
elif method == 'eth_sign':
|
|
||||||
params = PlatformMiddleware._translate_params(suspect_params)
|
|
||||||
s = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM, proto=0)
|
|
||||||
ipc_provider_workaround = s.connect(self.ipcaddr)
|
|
||||||
logg.info('redirecting method {} params {} original params {}'.format(method, params, suspect_params))
|
|
||||||
o = jsonrpc_request(method, params)
|
|
||||||
j = json.dumps(o)
|
|
||||||
logg.debug('send {}'.format(j))
|
|
||||||
s.send(j.encode('utf-8'))
|
|
||||||
r = s.recv(4096)
|
|
||||||
s.close()
|
|
||||||
logg.debug('got recv {}'.format(str(r)))
|
|
||||||
jr = json.loads(r)
|
|
||||||
jr['id'] = self.id_seq
|
|
||||||
return jr
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
r = self.make_request(method, suspect_params)
|
|
||||||
return r
|
|
@ -1 +0,0 @@
|
|||||||
from .tx import TxExecutor
|
|
@ -1,98 +0,0 @@
|
|||||||
# standard imports
|
|
||||||
import logging
|
|
||||||
import time
|
|
||||||
|
|
||||||
# third-party imports
|
|
||||||
from crypto_dev_signer.eth.transaction import EIP155Transaction
|
|
||||||
|
|
||||||
# local imports
|
|
||||||
from crypto_dev_signer.error import TransactionRevertError
|
|
||||||
|
|
||||||
logg = logging.getLogger()
|
|
||||||
|
|
||||||
|
|
||||||
class TxExecutor:
|
|
||||||
|
|
||||||
def __init__(self, sender, signer, translator, dispatcher, reporter, nonce, chain_id, fee_helper=None, fee_price_helper=None, verifier=None, block=False):
|
|
||||||
self.sender = sender
|
|
||||||
self.translator = translator
|
|
||||||
self.nonce = nonce
|
|
||||||
self.signer = signer
|
|
||||||
self.dispatcher = dispatcher
|
|
||||||
self.reporter = reporter
|
|
||||||
self.block = bool(block)
|
|
||||||
self.chain_id = chain_id
|
|
||||||
self.tx_hashes = []
|
|
||||||
if fee_helper == None:
|
|
||||||
fee_helper = self.noop_fee_helper
|
|
||||||
self.fee_helper = fee_helper
|
|
||||||
if fee_price_helper == None:
|
|
||||||
fee_price_helper = self.noop_fee_price_helper
|
|
||||||
self.fee_price_helper = fee_price_helper
|
|
||||||
if verifier == None:
|
|
||||||
verifier = self.noop_verifier
|
|
||||||
self.verifier = verifier
|
|
||||||
|
|
||||||
|
|
||||||
def noop_fee_helper(self, tx):
|
|
||||||
return 1
|
|
||||||
|
|
||||||
|
|
||||||
def noop_fee_price_helper(self):
|
|
||||||
return 1
|
|
||||||
|
|
||||||
|
|
||||||
def noop_verifier(self, rcpt):
|
|
||||||
return rcpt
|
|
||||||
|
|
||||||
|
|
||||||
def noop_translator(self, tx):
|
|
||||||
return tx
|
|
||||||
|
|
||||||
|
|
||||||
def sign_and_send(self, builder, force_wait=False):
|
|
||||||
|
|
||||||
fee_price = self.fee_price_helper()
|
|
||||||
|
|
||||||
tx_tpl = {
|
|
||||||
'from': self.sender,
|
|
||||||
'chainId': self.chain_id,
|
|
||||||
'feeUnits': 0, #fee_units,
|
|
||||||
'feePrice': fee_price,
|
|
||||||
'nonce': self.nonce,
|
|
||||||
}
|
|
||||||
|
|
||||||
tx = self.translator(tx_tpl)
|
|
||||||
for b in builder:
|
|
||||||
tx = b(tx)
|
|
||||||
|
|
||||||
tx['feeUnits'] = self.fee_helper(tx)
|
|
||||||
tx = self.translator(tx)
|
|
||||||
|
|
||||||
logg.debug('from {} nonce {} tx {}'.format(self.sender, self.nonce, tx))
|
|
||||||
|
|
||||||
chain_tx = EIP155Transaction(tx, self.nonce, self.chain_id)
|
|
||||||
signature = self.signer.sign_transaction(chain_tx)
|
|
||||||
chain_tx_serialized = chain_tx.rlp_serialize()
|
|
||||||
tx_hash = self.dispatcher('0x' + chain_tx_serialized.hex())
|
|
||||||
self.tx_hashes.append(tx_hash)
|
|
||||||
self.nonce += 1
|
|
||||||
rcpt = None
|
|
||||||
if self.block or force_wait:
|
|
||||||
rcpt = self.wait_for(tx_hash)
|
|
||||||
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 self.w3.eth.getTransactionReceipt(tx_hash)
|
|
||||||
return self.reporter(tx_hash)
|
|
||||||
except Exception:
|
|
||||||
logg.debug('poll #{} for {}'.format(i, tx_hash.hex()))
|
|
||||||
i += 1
|
|
||||||
time.sleep(1)
|
|
||||||
return self.verifier(rcpt)
|
|
@ -13,7 +13,7 @@ from .interface import Keystore
|
|||||||
from crypto_dev_signer.error import UnknownAccountError
|
from crypto_dev_signer.error import UnknownAccountError
|
||||||
from crypto_dev_signer.encoding import private_key_to_address
|
from crypto_dev_signer.encoding import private_key_to_address
|
||||||
|
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class DictKeystore(Keystore):
|
class DictKeystore(Keystore):
|
||||||
|
@ -34,7 +34,6 @@ class Keystore:
|
|||||||
|
|
||||||
|
|
||||||
def import_keystore_data(self, keystore_content, password=''):
|
def import_keystore_data(self, keystore_content, password=''):
|
||||||
#private_key = w3.eth.account.decrypt(keystore_content, password)
|
|
||||||
if type(keystore_content).__name__ == 'str':
|
if type(keystore_content).__name__ == 'str':
|
||||||
keystore_content = json.loads(keystore_content)
|
keystore_content = json.loads(keystore_content)
|
||||||
elif type(keystore_content).__name__ == 'bytes':
|
elif type(keystore_content).__name__ == 'bytes':
|
||||||
@ -45,6 +44,4 @@ class Keystore:
|
|||||||
|
|
||||||
def import_keystore_file(self, keystore_file, password=''):
|
def import_keystore_file(self, keystore_file, password=''):
|
||||||
private_key = keyfile.from_file(keystore_file, password)
|
private_key = keyfile.from_file(keystore_file, password)
|
||||||
#return self.import_keystore_data(keystore_content, password)
|
|
||||||
return self.import_raw_key(private_key)
|
return self.import_raw_key(private_key)
|
||||||
#return kes
|
|
||||||
|
@ -14,7 +14,7 @@ import sha3
|
|||||||
# local imports
|
# local imports
|
||||||
from crypto_dev_signer.encoding import private_key_to_address
|
from crypto_dev_signer.encoding import private_key_to_address
|
||||||
|
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger(__name__)
|
||||||
|
|
||||||
algo_keywords = [
|
algo_keywords = [
|
||||||
'aes-128-ctr',
|
'aes-128-ctr',
|
||||||
|
@ -21,7 +21,7 @@ from .interface import Keystore
|
|||||||
from crypto_dev_signer.error import UnknownAccountError
|
from crypto_dev_signer.error import UnknownAccountError
|
||||||
from crypto_dev_signer.encoding import private_key_to_address
|
from crypto_dev_signer.encoding import private_key_to_address
|
||||||
|
|
||||||
logg = logging.getLogger(__file__)
|
logg = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def to_bytes(x):
|
def to_bytes(x):
|
||||||
|
Loading…
Reference in New Issue
Block a user