Add private key bytes to private key converter, address default on keyfile decode
This commit is contained in:
91
tests/test_helper.py
Normal file
91
tests/test_helper.py
Normal file
@@ -0,0 +1,91 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
import logging
|
||||
import os
|
||||
|
||||
# local imports
|
||||
from crypto_dev_signer.keystore.dict import DictKeystore
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner
|
||||
from crypto_dev_signer.helper import TxExecutor
|
||||
#from crypto_dev_signer.eth.helper import EthTxExecutor
|
||||
from crypto_dev_signer.encoding import to_checksum_address
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
script_dir = os.path.realpath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
class MockEthTxBackend:
|
||||
|
||||
def dispatcher(self, tx):
|
||||
logg.debug('sender {}'.format(tx))
|
||||
return os.urandom(32)
|
||||
|
||||
def reporter(self, tx):
|
||||
logg.debug('reporter {}'.format(tx))
|
||||
|
||||
def verifier(self, rcpt):
|
||||
logg.debug('reporter {}'.format(rcpt))
|
||||
|
||||
def fee_price_helper(self):
|
||||
return 21
|
||||
|
||||
def fee_helper(self, tx):
|
||||
logg.debug('fee helper tx {}'.format(tx))
|
||||
return 2
|
||||
|
||||
def builder(self, tx):
|
||||
return tx
|
||||
|
||||
def builder_two(self, tx):
|
||||
tx['value'] = 10243
|
||||
tx['to'] = to_checksum_address('0x' + os.urandom(20).hex())
|
||||
tx['data'] = '0x'
|
||||
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
|
||||
|
||||
|
||||
class TestHelper(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
logg.debug('setup')
|
||||
self.db = DictKeystore()
|
||||
|
||||
keystore_filename = 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72'
|
||||
keystore_filepath = os.path.join(script_dir, 'testdata', keystore_filename)
|
||||
|
||||
self.address_hex = self.db.import_keystore_file(keystore_filepath, '')
|
||||
self.signer = ReferenceSigner(self.db)
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_helper(self):
|
||||
backend = MockEthTxBackend()
|
||||
executor = TxExecutor(self.address_hex, self.signer, backend.builder, backend.dispatcher, backend.reporter, 666, 13, backend.fee_helper, backend.fee_price_helper, backend.verifier)
|
||||
|
||||
tx_ish = {'from': self.address_hex}
|
||||
executor.sign_and_send([backend.builder_two])
|
||||
|
||||
|
||||
# def test_eth_helper(self):
|
||||
# backend = MockEthTxBackend()
|
||||
# w3 = web3.Web3(web3.Web3.HTTPProvider('http://localhost:8545'))
|
||||
# executor = EthTxExecutor(w3, self.address_hex, self.signer, 1337)
|
||||
#
|
||||
# tx_ish = {'from': self.address_hex}
|
||||
# #executor.sign_and_send([backend.builder, backend.builder_two])
|
||||
# with self.assertRaises(ValueError):
|
||||
# executor.sign_and_send([backend.builder_two])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
64
tests/test_keystore_dict.py
Normal file
64
tests/test_keystore_dict.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# standard imports
|
||||
import unittest
|
||||
import logging
|
||||
import base64
|
||||
import os
|
||||
|
||||
# external imports
|
||||
from hexathon import (
|
||||
strip_0x,
|
||||
add_0x,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from crypto_dev_signer.keystore.dict import DictKeystore
|
||||
from crypto_dev_signer.error import UnknownAccountError
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
script_dir = os.path.realpath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
class TestDict(unittest.TestCase):
|
||||
|
||||
address_hex = None
|
||||
db = None
|
||||
|
||||
def setUp(self):
|
||||
logg.debug('setup')
|
||||
self.db = DictKeystore()
|
||||
|
||||
keystore_filepath = os.path.join(script_dir, 'testdata', 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72')
|
||||
|
||||
address_hex = self.db.import_keystore_file(keystore_filepath, '')
|
||||
self.address_hex = add_0x(address_hex)
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_get_key(self):
|
||||
logg.debug('getting {}'.format(strip_0x(self.address_hex)))
|
||||
pk = self.db.get(strip_0x(self.address_hex), '')
|
||||
|
||||
self.assertEqual(self.address_hex.lower(), '0x00a329c0648769a73afac7f9381e08fb43dbea72')
|
||||
|
||||
bogus_account = os.urandom(20).hex()
|
||||
with self.assertRaises(UnknownAccountError):
|
||||
self.db.get(bogus_account, '')
|
||||
|
||||
|
||||
def test_sign_message(self):
|
||||
s = ReferenceSigner(self.db)
|
||||
z = s.sign_ethereum_message(strip_0x(self.address_hex), b'foo')
|
||||
logg.debug('zzz {}'.format(str(z)))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
64
tests/test_keystore_reference.py
Normal file
64
tests/test_keystore_reference.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# standard imports
|
||||
import unittest
|
||||
import logging
|
||||
import base64
|
||||
import os
|
||||
|
||||
# external imports
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
from cryptography.fernet import Fernet, InvalidToken
|
||||
|
||||
# local imports
|
||||
from crypto_dev_signer.keystore.reference import ReferenceKeystore
|
||||
from crypto_dev_signer.error import UnknownAccountError
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class TestDatabase(unittest.TestCase):
|
||||
|
||||
conn = None
|
||||
cur = None
|
||||
symkey = None
|
||||
address_hex = None
|
||||
db = None
|
||||
|
||||
def setUp(self):
|
||||
logg.debug('setup')
|
||||
# arbitrary value
|
||||
symkey_hex = 'E92431CAEE69313A7BE9E443C4ABEED9BF8157E9A13553B4D5D6E7D51B5021D9'
|
||||
self.symkey = bytes.fromhex(symkey_hex)
|
||||
self.address_hex = '9FA61f0E52A5C51b43f0d32404625BC436bb7041'
|
||||
|
||||
kw = {
|
||||
'symmetric_key': self.symkey,
|
||||
}
|
||||
self.db = ReferenceKeystore('postgres+psycopg2://postgres@localhost:5432/signer_test', **kw)
|
||||
self.address_hex = self.db.new('foo')
|
||||
#self.address_hex = add_0x(address_hex)
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
self.db.db_session.execute('DROP INDEX ethereum_address_idx;')
|
||||
self.db.db_session.execute('DROP TABLE ethereum;')
|
||||
self.db.db_session.commit()
|
||||
|
||||
|
||||
|
||||
def test_get_key(self):
|
||||
logg.debug('getting {}'.format(self.address_hex))
|
||||
self.db.get(self.address_hex, 'foo')
|
||||
with self.assertRaises(InvalidToken):
|
||||
self.db.get(self.address_hex, 'bar')
|
||||
|
||||
bogus_account = '0x' + os.urandom(20).hex()
|
||||
with self.assertRaises(UnknownAccountError):
|
||||
self.db.get(bogus_account, 'bar')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
98
tests/test_sign.py
Normal file
98
tests/test_sign.py
Normal file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import unittest
|
||||
import logging
|
||||
|
||||
from rlp import encode as rlp_encode
|
||||
|
||||
from crypto_dev_signer.eth.signer import ReferenceSigner
|
||||
from crypto_dev_signer.eth.transaction import EIP155Transaction
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
tx_ints = {
|
||||
'nonce': 0,
|
||||
'from': "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
|
||||
'gasPrice': "20000000000",
|
||||
'gas': "21000",
|
||||
'to': '0x3535353535353535353535353535353535353535',
|
||||
'value': "1000",
|
||||
'data': "deadbeef",
|
||||
}
|
||||
|
||||
tx_hexs = {
|
||||
'nonce': '0x0',
|
||||
'from': "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
|
||||
'gasPrice': "0x4a817c800",
|
||||
'gas': "0x5208",
|
||||
'to': '0x3535353535353535353535353535353535353535',
|
||||
'value': "0x3e8",
|
||||
'data': "deadbeef",
|
||||
}
|
||||
|
||||
class pkGetter:
|
||||
|
||||
def __init__(self, pk):
|
||||
self.pk = pk
|
||||
|
||||
def get(self, address, password=None):
|
||||
return self.pk
|
||||
|
||||
|
||||
class TestSign(unittest.TestCase):
|
||||
|
||||
pk = None
|
||||
nonce = -1
|
||||
pk_getter = None
|
||||
|
||||
|
||||
def getNonce(self):
|
||||
self.nonce += 1
|
||||
return self.nonce
|
||||
|
||||
|
||||
def setUp(self):
|
||||
self.pk = bytes.fromhex('5087503f0a9cc35b38665955eb830c63f778453dd11b8fa5bd04bc41fd2cc6d6')
|
||||
self.pk_getter = pkGetter(self.pk)
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
logg.info('teardown empty')
|
||||
|
||||
|
||||
|
||||
# TODO: verify rlp tx output
|
||||
def test_serialize_transaction(self):
|
||||
t = EIP155Transaction(tx_ints, 0)
|
||||
self.assertRegex(t.__class__.__name__, "Transaction")
|
||||
s = t.serialize()
|
||||
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': '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')
|
||||
|
||||
|
||||
|
||||
def test_sign_transaction(self):
|
||||
t = EIP155Transaction(tx_ints, 461, 8995)
|
||||
s = ReferenceSigner(self.pk_getter)
|
||||
z = s.sign_transaction(t)
|
||||
|
||||
|
||||
def test_sign_message(self):
|
||||
s = ReferenceSigner(self.pk_getter)
|
||||
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)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user