contract-registry

Ethereum Smart Contract key-value registry
Info | Log | Files | Refs

encoding.py (594B)


      1 # standard imports
      2 import logging
      3 
      4 # external imports
      5 from hexathon import strip_0x
      6 
      7 logg = logging.getLogger(__name__)
      8 
      9 
     10 def to_text(b):
     11         b = b[:b.find(0)]
     12         # TODO: why was this part of this method previously?
     13         # if len(b) % 2 > 0:
     14         #     b = b'\x00' + b
     15         return b.decode('utf-8')
     16 
     17 
     18 def from_text(txt):
     19     return '0x{:0<64s}'.format(txt.encode('utf-8').hex())
     20 
     21 
     22 def from_identifier(b):
     23     return to_text(b)
     24 
     25 
     26 def from_identifier_hex(hx):
     27     b = bytes.fromhex(strip_0x(hx))
     28     return from_identifier(b)
     29 
     30 
     31 def to_identifier(txt):
     32     return from_text(txt)