Make signer test pass

This commit is contained in:
nolash 2021-10-10 09:55:15 +02:00
parent 91b94f5ddf
commit e606384ed2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 13 additions and 108 deletions

View File

@ -4,29 +4,26 @@ import json
import logging
# local imports
from funga.keystore import Keystore
from funga.eth.keystore import keyfile
from funga.eth.encoding import private_key_from_bytes
from funga.keystore import Keystore
logg = logging.getLogger(__name__)
def native_keygen(self):
def native_keygen(*args, **kwargs):
return os.urandom(32)
class EthKeystore(Keystore):
def __init__(self, private_key_generator=native_keygen):
super(Keystore, self).__init__(
private_key_generator=private_key_generator,
private_key_parser=private_key_from_bytes,
keystore_parser=keyfile.from_some,
)
super(EthKeystore, self).__init__(private_key_generator, private_key_from_bytes, keyfile.from_some)
def new(self, password=None):
return self.import_raw_key(b, password)
b = self.private_key_generator()
return self.import_raw_key(b, password=password)
def import_raw_key(self, b, password=None):

View File

@ -5,8 +5,6 @@ import base64
# external imports
from cryptography.fernet import Fernet
#import psycopg2
#from psycopg2 import sql
#from psycopg2.extensions import make_dsn
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
import sha3
@ -16,10 +14,10 @@ from hexathon import (
)
# local imports
from .interface import Keystore
from .interface import EthKeystore
#from . import keyapi
from crypto_dev_signer.error import UnknownAccountError
from crypto_dev_signer.encoding import private_key_to_address
from funga.error import UnknownAccountError
from funga.eth.encoding import private_key_to_address
logg = logging.getLogger(__name__)
@ -28,7 +26,7 @@ def to_bytes(x):
return x.encode('utf-8')
class ReferenceKeystore(Keystore):
class SQLKeystore(EthKeystore):
schema = [
"""CREATE TABLE IF NOT EXISTS ethereum (
@ -42,6 +40,7 @@ class ReferenceKeystore(Keystore):
]
def __init__(self, dsn, **kwargs):
super(SQLKeystore, self).__init__()
logg.debug('starting db session with dsn {}'.format(dsn))
self.db_engine = create_engine(dsn)
self.db_session = sessionmaker(bind=self.db_engine)()

View File

@ -1,91 +0,0 @@
# 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()

View File

@ -12,8 +12,8 @@ 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
from funga.eth.keystore.sql import SQLKeystore
from funga.error import UnknownAccountError
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
@ -37,7 +37,7 @@ class TestDatabase(unittest.TestCase):
kw = {
'symmetric_key': self.symkey,
}
self.db = ReferenceKeystore('postgres+psycopg2://postgres@localhost:5432/signer_test', **kw)
self.db = SQLKeystore('postgres+psycopg2://postgres@localhost:5432/signer_test', **kw)
self.address_hex = self.db.new('foo')
#self.address_hex = add_0x(address_hex)