test_cli.py (3477B)
1 # standard imports 2 import unittest 3 import logging 4 import os 5 6 # external imports 7 from hexathon import strip_0x 8 from pathlib import Path 9 10 import sys 11 path_root = Path('/home/vincent/ida/grassroots/funga-eth/funga/eth/keystore') 12 sys.path.append(str(path_root)) 13 print(sys.path) 14 15 # local imports 16 from funga.eth.signer import EIP155Signer 17 from funga.eth.keystore.dict import DictKeystore 18 19 20 from funga.eth.cli.handle import SignRequestHandler 21 from funga.eth.transaction import EIP155Transaction 22 23 logging.basicConfig(level=logging.DEBUG) 24 logg = logging.getLogger() 25 26 script_dir = os.path.dirname(os.path.realpath(__file__)) 27 data_dir = os.path.join(script_dir, 'testdata') 28 29 30 class TestCli(unittest.TestCase): 31 32 def setUp(self): 33 # pk = bytes.fromhex('5087503f0a9cc35b38665955eb830c63f778453dd11b8fa5bd04bc41fd2cc6d6') 34 # pk_getter = pkGetter(pk) 35 self.keystore = DictKeystore() 36 SignRequestHandler.keystore = self.keystore 37 self.signer = EIP155Signer(self.keystore) 38 SignRequestHandler.signer = self.signer 39 self.handler = SignRequestHandler() 40 41 def test_new_account(self): 42 q = { 43 'id': 0, 44 'method': 'personal_newAccount', 45 'params': [''], 46 } 47 (rpc_id, result) = self.handler.process_input(q) 48 self.assertTrue(self.keystore.get(result)) 49 50 def test_sign_tx(self): 51 keystore_file = os.path.join(data_dir, 52 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72') 53 sender = self.keystore.import_keystore_file(keystore_file) 54 tx_hexs = { 55 'nonce': '0x', 56 'from': sender, 57 'gasPrice': "0x04a817c800", 58 'gas': "0x5208", 59 'to': '0x3535353535353535353535353535353535353535', 60 'value': "0x03e8", 61 'data': "0xdeadbeef", 62 'chainId': 8995, 63 } 64 tx = EIP155Transaction(tx_hexs, 42, 8995) 65 tx_s = tx.serialize() 66 67 # TODO: move to serialization wrapper for tests 68 tx_s['chainId'] = tx_s['v'] 69 tx_s['from'] = sender 70 71 # eth_signTransaction wraps personal_signTransaction, so here we test both already 72 q = { 73 'id': 0, 74 'method': 'eth_signTransaction', 75 'params': [tx_s], 76 } 77 (rpc_id, result) = self.handler.process_input(q) 78 logg.debug('result {}'.format(result)) 79 80 self.assertEqual(strip_0x(result), 81 'f86c2a8504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef82466aa0b7c1bbf52f736ada30fe253c7484176f44d6fd097a9720dc85ae5bbc7f060e54a07afee2563b0cf6d00333df51cc62b0d13c63108b2bce54ce2ad24e26ce7b4f25') 82 83 84 85 def test_sign_msg(self): 86 keystore_file = os.path.join(data_dir, 87 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72') 88 sender = self.keystore.import_keystore_file(keystore_file) 89 q = { 90 'id': 0, 91 'method': 'eth_sign', 92 'params': [sender, '0xdeadbeef'], 93 } 94 (rpc_id, result) = self.handler.process_input(q) 95 logg.debug('result msg {}'.format(result)) 96 self.assertEqual(strip_0x(result), 97 '50320dda75190a121b7b5979de66edadafd02bdfbe4f6d49552e79c01410d2464aae35e385c0e5b61663ff7b44ef65fa0ac7ad8a57472cf405db399b9dba3e1600') 98 99 100 if __name__ == '__main__': 101 unittest.main()