funga-eth

Ethereum implementation of the funga keystore and signer
Info | Log | Files | Refs | README | LICENSE

interface.py (1560B)


      1 # standard imports
      2 import os
      3 import json
      4 import logging
      5 
      6 # local imports
      7 from funga.eth.keystore import keyfile
      8 from funga.eth.encoding import private_key_from_bytes
      9 from funga.keystore import Keystore
     10 
     11 logg = logging.getLogger(__name__)
     12 
     13 
     14 def native_keygen(*args, **kwargs):
     15     return os.urandom(32)
     16 
     17 
     18 class EthKeystore(Keystore):
     19 
     20     def __init__(self, private_key_generator=native_keygen):
     21         super(EthKeystore, self).__init__(private_key_generator, private_key_from_bytes, keyfile.from_some)
     22             
     23 
     24     def new(self, password=None):
     25         b = self.private_key_generator()
     26         return self.import_raw_key(b, password=password)
     27 
     28 
     29     def import_raw_key(self, b, password=None):
     30         pk = private_key_from_bytes(b)
     31         return self.import_key(pk, password)
     32 
     33 
     34     def import_key(self, pk, password=None):
     35         raise NotImplementedError
     36 
     37 
     38     def import_keystore_data(self, keystore_content, password=''):
     39         if type(keystore_content).__name__ == 'str':
     40             keystore_content = json.loads(keystore_content)
     41         elif type(keystore_content).__name__ == 'bytes':
     42             logg.debug('bytes {}'.format(keystore_content))
     43             keystore_content = json.loads(keystore_content.decode('utf-8'))
     44         private_key = keyfile.from_dict(keystore_content, password.encode('utf-8'))
     45         return self.import_raw_key(private_key, password)
     46 
     47 
     48     def import_keystore_file(self, keystore_file, password=''):
     49         private_key = keyfile.from_file(keystore_file, password)
     50         return self.import_raw_key(private_key)