From c669ed829fc4d78cca5a6fb73ab1e6d44abd5c56 Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 26 Mar 2021 13:08:36 +0100 Subject: [PATCH] Rehabilitate tests, rename camelcase sign methods to snake case --- crypto_dev_signer/eth/signer/defaultsigner.py | 16 ++++++---------- crypto_dev_signer/eth/transaction.py | 6 ++++-- crypto_dev_signer/helper/tx.py | 2 +- crypto_dev_signer/runnable/signer.py | 12 ++++++------ setup.py | 2 +- test/test_helper.py | 2 +- test/test_keystore_dict.py | 2 +- test/test_keystore_reference.py | 2 +- test/test_sign.py | 10 +++++----- 9 files changed, 26 insertions(+), 28 deletions(-) diff --git a/crypto_dev_signer/eth/signer/defaultsigner.py b/crypto_dev_signer/eth/signer/defaultsigner.py index 5019751..18b881d 100644 --- a/crypto_dev_signer/eth/signer/defaultsigner.py +++ b/crypto_dev_signer/eth/signer/defaultsigner.py @@ -4,12 +4,8 @@ import logging # external imports import sha3 import coincurve -#from eth_keys import KeyAPI -#from eth_keys.backends import NativeECCBackend -#keys = KeyAPI(NativeECCBackend) -#logg = logging.getLogger(__name__) -logg = logging.getLogger() +logg = logging.getLogger().getChild(__name__) class Signer: @@ -19,9 +15,8 @@ class Signer: self.keyGetter = keyGetter - def signTransaction(self, tx, password=None): - raise NotImplementedError - + def sign_transaction(self, tx, password=None): + return NotImplementedError class ReferenceSigner(Signer): @@ -31,7 +26,7 @@ class ReferenceSigner(Signer): super(ReferenceSigner, self).__init__(keyGetter) - def signTransaction(self, tx, password=None): + def sign_transaction(self, tx, password=None): s = tx.rlp_serialize() h = sha3.keccak_256() h.update(s) @@ -58,7 +53,7 @@ class ReferenceSigner(Signer): return z - def signEthereumMessage(self, address, message, password=None): + def sign_ethereum_message(self, address, message, password=None): #k = keys.PrivateKey(self.keyGetter.get(address, password)) #z = keys.ecdsa_sign(message_hash=g, private_key=k) @@ -82,6 +77,7 @@ class ReferenceSigner(Signer): return z + # TODO: generic sign should be moved to non-eth context def sign(self, address, message, password=None): pk = coincurve.PrivateKey(secret=self.keyGetter.get(address, password)) z = pk.sign_recoverable(hasher=None, message=message) diff --git a/crypto_dev_signer/eth/transaction.py b/crypto_dev_signer/eth/transaction.py index 4fed835..8fb9304 100644 --- a/crypto_dev_signer/eth/transaction.py +++ b/crypto_dev_signer/eth/transaction.py @@ -27,9 +27,11 @@ class EIP155Transaction: to = None data = None if tx['to'] != None: - to = binascii.unhexlify(strip_0x(tx['to'], allow_empty=True)) + #to = binascii.unhexlify(strip_0x(tx['to'], allow_empty=True)) + to = bytes.fromhex(strip_0x(tx['to'], allow_empty=True)) if tx['data'] != None: - data = binascii.unhexlify(strip_0x(tx['data'], allow_empty=True)) + #data = binascii.unhexlify(strip_0x(tx['data'], allow_empty=True)) + data = bytes.fromhex(strip_0x(tx['data'], allow_empty=True)) gas_price = None start_gas = None diff --git a/crypto_dev_signer/helper/tx.py b/crypto_dev_signer/helper/tx.py index e2ebec7..a031829 100644 --- a/crypto_dev_signer/helper/tx.py +++ b/crypto_dev_signer/helper/tx.py @@ -72,7 +72,7 @@ class TxExecutor: logg.debug('from {} nonce {} tx {}'.format(self.sender, self.nonce, tx)) chain_tx = EIP155Transaction(tx, self.nonce, self.chain_id) - signature = self.signer.signTransaction(chain_tx) + 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) diff --git a/crypto_dev_signer/runnable/signer.py b/crypto_dev_signer/runnable/signer.py index d62cfd2..b786524 100755 --- a/crypto_dev_signer/runnable/signer.py +++ b/crypto_dev_signer/runnable/signer.py @@ -106,7 +106,7 @@ def personal_sign_transaction(p): logg.debug('got {} to sign'.format(p[0])) #t = EIP155Transaction(p[0], p[0]['nonce'], 8995) t = EIP155Transaction(p[0], p[0]['nonce'], int(p[0]['chainId'])) - z = signer.signTransaction(t, p[1]) + z = signer.sign_transaction(t, p[1]) raw_signed_tx = t.rlp_serialize() o = { 'raw': '0x' + raw_signed_tx.hex(), @@ -116,8 +116,8 @@ def personal_sign_transaction(p): return o -# TODO: temporary workaround for platform, since personal_signTransaction is missing from web3.py -def eth_signTransaction(tx): +# TODO: temporary workaround for platform, since personal_sign_transaction is missing from web3.py +def eth_sign_transaction(tx): return personal_sign_transaction([tx[0], '']) @@ -126,14 +126,14 @@ def eth_sign(p): message_type = type(p[1]).__name__ if message_type != 'str': raise ValueError('invalid message format, must be {}, not {}'.format(message_type)) - z = signer.signEthereumMessage(p[0], p[1][2:]) + z = signer.sign_ethereum_message(p[0], p[1][2:]) return str(z) methods = { 'personal_newAccount': personal_new_account, - 'personal_signTransaction': personal_sign_transaction, - 'eth_signTransaction': eth_signTransaction, + 'personal_sign_transaction': personal_sign_transaction, + 'eth_sign_transaction': eth_sign_transaction, 'eth_sign': eth_sign, } diff --git a/setup.py b/setup.py index fec41da..da73111 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ f.close() setup( name="crypto-dev-signer", - version="0.4.14a8", + version="0.4.14a10", description="A signer and keystore daemon and library for cryptocurrency software development", author="Louis Holbrook", author_email="dev@holbrook.no", diff --git a/test/test_helper.py b/test/test_helper.py index 51a4439..0e46438 100644 --- a/test/test_helper.py +++ b/test/test_helper.py @@ -41,7 +41,7 @@ class MockEthTxBackend: def builder_two(self, tx): tx['value'] = 10243 tx['to'] = to_checksum_address('0x' + os.urandom(20).hex()) - tx['data'] = '' + tx['data'] = '0x' if tx.get('feePrice') != None: tx['gasPrice'] = tx['feePrice'] del tx['feePrice'] diff --git a/test/test_keystore_dict.py b/test/test_keystore_dict.py index 44c9e7d..d80c4f7 100644 --- a/test/test_keystore_dict.py +++ b/test/test_keystore_dict.py @@ -48,7 +48,7 @@ class TestDict(unittest.TestCase): def test_sign_message(self): s = ReferenceSigner(self.db) - z = s.signEthereumMessage(self.address_hex[2:], b'foo') + z = s.sign_ethereum_message(self.address_hex[2:], b'foo') logg.debug('zzz {}'.format(str(z))) diff --git a/test/test_keystore_reference.py b/test/test_keystore_reference.py index 06b382c..dd70027 100644 --- a/test/test_keystore_reference.py +++ b/test/test_keystore_reference.py @@ -12,7 +12,7 @@ from psycopg2 import sql from cryptography.fernet import Fernet, InvalidToken # local imports -from crypto_dev_signer.keystore.postgres import ReferenceKeystore +from crypto_dev_signer.keystore.reference import ReferenceKeystore from crypto_dev_signer.error import UnknownAccountError logging.basicConfig(level=logging.DEBUG) diff --git a/test/test_sign.py b/test/test_sign.py index af60030..334c334 100644 --- a/test/test_sign.py +++ b/test/test_sign.py @@ -68,14 +68,14 @@ class TestSign(unittest.TestCase): t = EIP155Transaction(tx_ints, 0) self.assertRegex(t.__class__.__name__, "Transaction") s = t.serialize() - self.assertEqual('{}'.format(s), "{'nonce': '0x00', 'gasPrice': '0x04a817c800', 'gas': '0x5208', 'to': '0x3535353535353535353535353535353535353535', 'value': '0x03e8', 'data': '0xdeadbeef', 'v': '0x01', 'r': '', 's': ''}") + self.assertEqual('{}'.format(s), "{'nonce': '0x', 'gasPrice': '0x04a817c800', 'gas': '0x5208', 'to': '0x3535353535353535353535353535353535353535', 'value': '0x03e8', 'data': '0xdeadbeef', 'v': '0x01', 'r': '0x', 's': '0x'}") r = t.rlp_serialize() self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080') t = EIP155Transaction(tx_hexs, 0) self.assertRegex(t.__class__.__name__, "Transaction") s = t.serialize() - self.assertEqual('{}'.format(s), "{'nonce': '0x00', 'gasPrice': '0x04a817c800', 'gas': '0x5208', 'to': '0x3535353535353535353535353535353535353535', 'value': '0x03e8', 'data': '0xdeadbeef', 'v': '0x01', 'r': '', 's': ''}") + self.assertEqual('{}'.format(s), "{'nonce': '0x', 'gasPrice': '0x04a817c800', 'gas': '0x5208', 'to': '0x3535353535353535353535353535353535353535', 'value': '0x03e8', 'data': '0xdeadbeef', 'v': '0x01', 'r': '0x', 's': '0x'}") r = t.rlp_serialize() self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080') @@ -84,13 +84,13 @@ class TestSign(unittest.TestCase): def test_sign_transaction(self): t = EIP155Transaction(tx_ints, 461, 8995) s = ReferenceSigner(self.pk_getter) - z = s.signTransaction(t) + z = s.sign_transaction(t) def test_sign_message(self): s = ReferenceSigner(self.pk_getter) - z = s.signEthereumMessage(tx_ints['from'], '666f6f') - z = s.signEthereumMessage(tx_ints['from'], b'foo') + z = s.sign_ethereum_message(tx_ints['from'], '666f6f') + z = s.sign_ethereum_message(tx_ints['from'], b'foo') logg.debug('zzz {}'.format(str(z)))