chainlib-eth

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

src.py (1209B)


      1 # standard imports
      2 import logging
      3 import json
      4 
      5 # external imports
      6 from potaahto.symbols import snake_and_camel
      7 from hexathon import (
      8         uniform,
      9         strip_0x,
     10         )
     11 
     12 # local imports
     13 from chainlib.src import (
     14         Src as BaseSrc,
     15         SrcItem,
     16         )
     17 
     18 logg = logging.getLogger(__name__)
     19 
     20 
     21 class Src(BaseSrc):
     22 
     23     @classmethod
     24     def src_normalize(self, v):
     25         src = snake_and_camel(v)
     26         if isinstance(src.get('v'), str):
     27             try:
     28                 src['v'] = int(src['v'])
     29             except ValueError:
     30                 src['v'] = int(src['v'], 16)
     31         return src
     32 
     33 
     34     def normal(self, v, typ=SrcItem.AUTO):
     35         if typ == SrcItem.SRC:
     36             return self.src_normalize(v)
     37 
     38         if typ == SrcItem.HASH:
     39             v = strip_0x(v, pad=False)
     40             v = uniform(v, compact_value=True)
     41         elif typ == SrcItem.ADDRESS:
     42             v = strip_0x(v, pad=False)
     43             v = uniform(v, compact_value=True)
     44         elif typ == SrcItem.PAYLOAD:
     45             v = strip_0x(v, pad=False, allow_empty=True)
     46             v = uniform(v, compact_value=False, allow_empty=True)
     47 
     48         return v
     49 
     50 
     51     def __repr__(self):
     52         return json.dumps(self.src)