funga/tests/test_sign.py

100 lines
2.8 KiB
Python
Raw Normal View History

2021-10-10 09:46:27 +02:00
# standard imports
2020-08-04 23:41:31 +02:00
import unittest
import logging
2021-10-10 09:46:27 +02:00
import json
2020-08-04 23:41:31 +02:00
from rlp import encode as rlp_encode
2021-10-10 09:46:27 +02:00
from funga.eth.signer import EIP155Signer
from funga.eth.transaction import EIP155Transaction
2020-08-04 23:41:31 +02:00
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
2020-10-26 09:02:29 +01:00
tx_ints = {
'nonce': 0,
2020-08-04 23:41:31 +02:00
'from': "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
'gasPrice': "20000000000",
2020-10-26 09:02:29 +01:00
'gas': "21000",
2020-08-04 23:41:31 +02:00
'to': '0x3535353535353535353535353535353535353535',
'value': "1000",
'data': "deadbeef",
}
2020-10-26 09:02:29 +01:00
tx_hexs = {
'nonce': '0x0',
2020-10-26 09:02:29 +01:00
'from': "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
'gasPrice': "0x4a817c800",
2020-10-26 09:02:29 +01:00
'gas': "0x5208",
'to': '0x3535353535353535353535353535353535353535',
'value': "0x3e8",
'data': "deadbeef",
2020-10-26 09:02:29 +01:00
}
2020-08-06 11:07:18 +02:00
class pkGetter:
def __init__(self, pk):
self.pk = pk
def get(self, address, password=None):
return self.pk
2020-08-04 23:41:31 +02:00
class TestSign(unittest.TestCase):
pk = None
nonce = -1
2020-08-06 11:07:18 +02:00
pk_getter = None
2020-08-04 23:41:31 +02:00
def getNonce(self):
self.nonce += 1
return self.nonce
def setUp(self):
self.pk = bytes.fromhex('5087503f0a9cc35b38665955eb830c63f778453dd11b8fa5bd04bc41fd2cc6d6')
2020-08-06 11:07:18 +02:00
self.pk_getter = pkGetter(self.pk)
2020-08-04 23:41:31 +02:00
def tearDown(self):
logg.info('teardown empty')
2020-08-06 11:07:18 +02:00
# TODO: verify rlp tx output
2020-08-04 23:41:31 +02:00
def test_serialize_transaction(self):
2020-10-26 09:02:29 +01:00
t = EIP155Transaction(tx_ints, 0)
self.assertRegex(t.__class__.__name__, "Transaction")
s = t.serialize()
2021-10-10 09:46:27 +02:00
self.assertDictEqual(s, {'nonce': '0x', 'gasPrice': '0x04a817c800', 'gas': '0x5208', 'to': '0x3535353535353535353535353535353535353535', 'value': '0x03e8', 'data': '0xdeadbeef', 'v': '0x01', 'r': '0x', 's': '0x'})
2020-10-26 09:02:29 +01:00
r = t.rlp_serialize()
self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080')
t = EIP155Transaction(tx_hexs, 0)
2020-08-04 23:41:31 +02:00
self.assertRegex(t.__class__.__name__, "Transaction")
2020-08-06 11:07:18 +02:00
s = t.serialize()
2021-10-10 09:46:27 +02:00
#o = json.loads(s)
self.assertDictEqual(s, {'nonce': '0x', 'gasPrice': '0x04a817c800', 'gas': '0x5208', 'to': '0x3535353535353535353535353535353535353535', 'value': '0x03e8', 'data': '0xdeadbeef', 'v': '0x01', 'r': '0x', 's': '0x'})
2020-08-06 11:07:18 +02:00
r = t.rlp_serialize()
2020-10-26 09:02:29 +01:00
self.assertEqual(r.hex(), 'ea808504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef018080')
2020-08-06 11:07:18 +02:00
2020-08-04 23:41:31 +02:00
def test_sign_transaction(self):
2020-10-26 09:02:29 +01:00
t = EIP155Transaction(tx_ints, 461, 8995)
2021-10-10 09:46:27 +02:00
s = EIP155Signer(self.pk_getter)
z = s.sign_transaction(t)
2020-08-04 23:41:31 +02:00
2020-12-19 08:47:21 +01:00
def test_sign_message(self):
2021-10-10 09:46:27 +02:00
s = EIP155Signer(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)))
2020-12-19 08:47:21 +01:00
2020-08-04 23:41:31 +02:00
if __name__ == '__main__':
unittest.main()