funga-eth

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

dict.py (1143B)


      1 # standard imports
      2 import logging
      3 
      4 # external imports
      5 from hexathon import (
      6         strip_0x,
      7         add_0x,
      8         )
      9 
     10 # local imports
     11 #from . import keyapi
     12 from funga.error import UnknownAccountError
     13 from .interface import EthKeystore
     14 from funga.eth.encoding import private_key_to_address
     15 
     16 logg = logging.getLogger(__name__)
     17 
     18 
     19 class DictKeystore(EthKeystore):
     20 
     21     def __init__(self):
     22         super(DictKeystore, self).__init__()
     23         self.keys = {}
     24 
     25 
     26     def get(self, address, password=None):
     27         address_key = strip_0x(address).lower()
     28         if password != None:
     29             logg.debug('password ignored as dictkeystore doesnt do encryption')
     30         try:
     31             return self.keys[address_key]
     32         except KeyError:
     33             raise UnknownAccountError(address_key)
     34 
     35 
     36     def list(self):
     37         return list(self.keys.keys())
     38 
     39 
     40     def import_key(self, pk, password=None):
     41         address_hex = private_key_to_address(pk)
     42         address_hex_clean = strip_0x(address_hex).lower()
     43         self.keys[address_hex_clean] = pk.secret
     44         logg.debug('added key {}'.format(address_hex))
     45         return add_0x(address_hex)