Rehabilitate tests, rename camelcase sign methods to snake case

This commit is contained in:
nolash 2021-03-26 13:08:36 +01:00
parent 80cc54e3ba
commit c669ed829f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 26 additions and 28 deletions

View File

@ -4,12 +4,8 @@ import logging
# external imports # external imports
import sha3 import sha3
import coincurve import coincurve
#from eth_keys import KeyAPI
#from eth_keys.backends import NativeECCBackend
#keys = KeyAPI(NativeECCBackend) logg = logging.getLogger().getChild(__name__)
#logg = logging.getLogger(__name__)
logg = logging.getLogger()
class Signer: class Signer:
@ -19,9 +15,8 @@ class Signer:
self.keyGetter = keyGetter self.keyGetter = keyGetter
def signTransaction(self, tx, password=None): def sign_transaction(self, tx, password=None):
raise NotImplementedError return NotImplementedError
class ReferenceSigner(Signer): class ReferenceSigner(Signer):
@ -31,7 +26,7 @@ class ReferenceSigner(Signer):
super(ReferenceSigner, self).__init__(keyGetter) super(ReferenceSigner, self).__init__(keyGetter)
def signTransaction(self, tx, password=None): def sign_transaction(self, tx, password=None):
s = tx.rlp_serialize() s = tx.rlp_serialize()
h = sha3.keccak_256() h = sha3.keccak_256()
h.update(s) h.update(s)
@ -58,7 +53,7 @@ class ReferenceSigner(Signer):
return z 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)) #k = keys.PrivateKey(self.keyGetter.get(address, password))
#z = keys.ecdsa_sign(message_hash=g, private_key=k) #z = keys.ecdsa_sign(message_hash=g, private_key=k)
@ -82,6 +77,7 @@ class ReferenceSigner(Signer):
return z return z
# TODO: generic sign should be moved to non-eth context
def sign(self, address, message, password=None): def sign(self, address, message, password=None):
pk = coincurve.PrivateKey(secret=self.keyGetter.get(address, password)) pk = coincurve.PrivateKey(secret=self.keyGetter.get(address, password))
z = pk.sign_recoverable(hasher=None, message=message) z = pk.sign_recoverable(hasher=None, message=message)

View File

@ -27,9 +27,11 @@ class EIP155Transaction:
to = None to = None
data = None data = None
if tx['to'] != 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: 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 gas_price = None
start_gas = None start_gas = None

View File

@ -72,7 +72,7 @@ class TxExecutor:
logg.debug('from {} nonce {} tx {}'.format(self.sender, self.nonce, tx)) logg.debug('from {} nonce {} tx {}'.format(self.sender, self.nonce, tx))
chain_tx = EIP155Transaction(tx, self.nonce, self.chain_id) 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() chain_tx_serialized = chain_tx.rlp_serialize()
tx_hash = self.dispatcher('0x' + chain_tx_serialized.hex()) tx_hash = self.dispatcher('0x' + chain_tx_serialized.hex())
self.tx_hashes.append(tx_hash) self.tx_hashes.append(tx_hash)

View File

@ -106,7 +106,7 @@ def personal_sign_transaction(p):
logg.debug('got {} to sign'.format(p[0])) logg.debug('got {} to sign'.format(p[0]))
#t = EIP155Transaction(p[0], p[0]['nonce'], 8995) #t = EIP155Transaction(p[0], p[0]['nonce'], 8995)
t = EIP155Transaction(p[0], p[0]['nonce'], int(p[0]['chainId'])) 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() raw_signed_tx = t.rlp_serialize()
o = { o = {
'raw': '0x' + raw_signed_tx.hex(), 'raw': '0x' + raw_signed_tx.hex(),
@ -116,8 +116,8 @@ def personal_sign_transaction(p):
return o return o
# TODO: temporary workaround for platform, since personal_signTransaction is missing from web3.py # TODO: temporary workaround for platform, since personal_sign_transaction is missing from web3.py
def eth_signTransaction(tx): def eth_sign_transaction(tx):
return personal_sign_transaction([tx[0], '']) return personal_sign_transaction([tx[0], ''])
@ -126,14 +126,14 @@ def eth_sign(p):
message_type = type(p[1]).__name__ message_type = type(p[1]).__name__
if message_type != 'str': if message_type != 'str':
raise ValueError('invalid message format, must be {}, not {}'.format(message_type)) 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) return str(z)
methods = { methods = {
'personal_newAccount': personal_new_account, 'personal_newAccount': personal_new_account,
'personal_signTransaction': personal_sign_transaction, 'personal_sign_transaction': personal_sign_transaction,
'eth_signTransaction': eth_signTransaction, 'eth_sign_transaction': eth_sign_transaction,
'eth_sign': eth_sign, 'eth_sign': eth_sign,
} }

View File

@ -24,7 +24,7 @@ f.close()
setup( setup(
name="crypto-dev-signer", name="crypto-dev-signer",
version="0.4.14a8", version="0.4.14a10",
description="A signer and keystore daemon and library for cryptocurrency software development", description="A signer and keystore daemon and library for cryptocurrency software development",
author="Louis Holbrook", author="Louis Holbrook",
author_email="dev@holbrook.no", author_email="dev@holbrook.no",

View File

@ -41,7 +41,7 @@ class MockEthTxBackend:
def builder_two(self, tx): def builder_two(self, tx):
tx['value'] = 10243 tx['value'] = 10243
tx['to'] = to_checksum_address('0x' + os.urandom(20).hex()) tx['to'] = to_checksum_address('0x' + os.urandom(20).hex())
tx['data'] = '' tx['data'] = '0x'
if tx.get('feePrice') != None: if tx.get('feePrice') != None:
tx['gasPrice'] = tx['feePrice'] tx['gasPrice'] = tx['feePrice']
del tx['feePrice'] del tx['feePrice']

View File

@ -48,7 +48,7 @@ class TestDict(unittest.TestCase):
def test_sign_message(self): def test_sign_message(self):
s = ReferenceSigner(self.db) 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))) logg.debug('zzz {}'.format(str(z)))

View File

@ -12,7 +12,7 @@ from psycopg2 import sql
from cryptography.fernet import Fernet, InvalidToken from cryptography.fernet import Fernet, InvalidToken
# local imports # 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 from crypto_dev_signer.error import UnknownAccountError
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)

View File

@ -68,14 +68,14 @@ class TestSign(unittest.TestCase):
t = EIP155Transaction(tx_ints, 0) t = EIP155Transaction(tx_ints, 0)
self.assertRegex(t.__class__.__name__, "Transaction") self.assertRegex(t.__class__.__name__, "Transaction")
s = t.serialize() 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() r = t.rlp_serialize()
self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080') self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080')
t = EIP155Transaction(tx_hexs, 0) t = EIP155Transaction(tx_hexs, 0)
self.assertRegex(t.__class__.__name__, "Transaction") self.assertRegex(t.__class__.__name__, "Transaction")
s = t.serialize() 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() r = t.rlp_serialize()
self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080') self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080')
@ -84,13 +84,13 @@ class TestSign(unittest.TestCase):
def test_sign_transaction(self): def test_sign_transaction(self):
t = EIP155Transaction(tx_ints, 461, 8995) t = EIP155Transaction(tx_ints, 461, 8995)
s = ReferenceSigner(self.pk_getter) s = ReferenceSigner(self.pk_getter)
z = s.signTransaction(t) z = s.sign_transaction(t)
def test_sign_message(self): def test_sign_message(self):
s = ReferenceSigner(self.pk_getter) s = ReferenceSigner(self.pk_getter)
z = s.signEthereumMessage(tx_ints['from'], '666f6f') z = s.sign_ethereum_message(tx_ints['from'], '666f6f')
z = s.signEthereumMessage(tx_ints['from'], b'foo') z = s.sign_ethereum_message(tx_ints['from'], b'foo')
logg.debug('zzz {}'.format(str(z))) logg.debug('zzz {}'.format(str(z)))