chainlib-eth

Ethereum implementation of the chainlib interface
Info | Log | Files | Refs | README | LICENSE

test_sign.py (3466B)


      1 # standard imports
      2 import os
      3 import socket
      4 import unittest
      5 import unittest.mock
      6 import logging
      7 import json
      8 
      9 # external imports
     10 from funga.eth.transaction import EIP155Transaction
     11 from funga.eth.signer.defaultsigner import EIP155Signer
     12 from funga.eth.keystore.dict import DictKeystore
     13 
     14 # local imports
     15 import chainlib
     16 from chainlib.eth.connection import EthUnixSignerConnection
     17 from chainlib.eth.sign import sign_transaction
     18 from chainlib.eth.tx import TxFactory
     19 from chainlib.eth.address import (
     20         to_checksum_address,
     21         is_same_address,
     22         )
     23 from chainlib.jsonrpc import (
     24         jsonrpc_response,
     25         jsonrpc_error,
     26         )
     27 from hexathon import (
     28         add_0x,
     29         )
     30 from chainlib.chain import ChainSpec
     31 
     32 from tests.base import TestBase
     33 
     34 logging.basicConfig(level=logging.DEBUG)
     35 logg = logging.getLogger()
     36 
     37 keystore = DictKeystore() 
     38 alice = keystore.new()
     39 bob = keystore.new()
     40 
     41 
     42 class Mocket(socket.socket):
     43   
     44     req_id = None
     45     error = False
     46     tx = None
     47     signer = None
     48 
     49     def connect(self, v):
     50         return self
     51 
     52 
     53     def send(self, v):
     54         o = json.loads(v)
     55         logg.debug('mocket received {}'.format(v))
     56         Mocket.req_id = o['id']
     57         params = o['params'][0]
     58         from_address = to_checksum_address(params.get('from'))
     59         if not is_same_address(alice, from_address):
     60             logg.error('from {} does not match alice {}'.format(from_address, alice)) #params))
     61             Mocket.error = True
     62         to_address = to_checksum_address(params.get('to'))
     63         if not is_same_address(bob, to_address):
     64             logg.error('to {} does not match bob {}'.format(to_address, bob)) #params))
     65             Mocket.error = True
     66         if not Mocket.error:
     67             Mocket.tx = EIP155Transaction(params, params['nonce'], params['chainId'])
     68             logg.debug('mocket {}'.format(Mocket.tx))
     69         return len(v)
     70   
     71 
     72     def recv(self, c):
     73         if Mocket.req_id != None:
     74 
     75             o = None
     76             if Mocket.error:
     77                 o = jsonrpc_error(Mocket.req_id)
     78             else:
     79                 tx = Mocket.tx
     80                 r = Mocket.signer.sign_transaction_to_rlp(tx)
     81                 Mocket.tx = None
     82                 o = jsonrpc_response(Mocket.req_id, add_0x(r.hex()))
     83             Mocket.req_id = None
     84             return json.dumps(o).encode('utf-8')
     85 
     86         return b''
     87 
     88 
     89 class TestSign(TestBase):
     90 
     91     
     92     def setUp(self):
     93         super(TestSign, self).__init__()
     94         self.chain_spec = ChainSpec('evm', 'foo', 42)
     95 
     96 
     97         logg.debug('alice {}'.format(alice))
     98         logg.debug('bob {}'.format(bob))
     99 
    100         self.signer = EIP155Signer(keystore)
    101 
    102         Mocket.signer = self.signer
    103     
    104 
    105     def test_sign_build(self):
    106         with unittest.mock.patch('chainlib.connection.socket.socket', Mocket) as m:
    107             rpc = EthUnixSignerConnection('foo', chain_spec=self.chain_spec)
    108             f = TxFactory(self.chain_spec, signer=rpc)
    109             tx = f.template(alice, bob, use_nonce=True)
    110             tx = f.build(tx)
    111             logg.debug('tx result {}'.format(tx))
    112 
    113   
    114     def test_sign_rpc(self):
    115         with unittest.mock.patch('chainlib.connection.socket.socket', Mocket) as m:
    116             rpc = EthUnixSignerConnection('foo')
    117             f = TxFactory(self.chain_spec, signer=rpc)
    118             tx = f.template(alice, bob, use_nonce=True)
    119             tx_o = sign_transaction(tx)
    120             rpc.do(tx_o)
    121 
    122 
    123 if __name__ == '__main__':
    124     unittest.main()