funga/crypto_dev_signer/keystore/dict.py

41 lines
1001 B
Python
Raw Normal View History

2021-01-09 22:05:24 +01:00
# standard imports
import logging
2021-03-17 15:34:51 +01:00
# external imports
from hexathon import strip_0x
2021-01-09 22:05:24 +01:00
# local imports
2021-03-17 15:34:51 +01:00
#from . import keyapi
2021-01-09 22:05:24 +01:00
from .interface import Keystore
from crypto_dev_signer.error import UnknownAccountError
2021-03-17 15:34:51 +01:00
from crypto_dev_signer.encoding import private_key_to_address
2021-01-09 22:05:24 +01:00
logg = logging.getLogger()
class DictKeystore(Keystore):
def __init__(self):
self.keys = {}
def get(self, address, password=None):
if password != None:
logg.debug('password ignored as dictkeystore doesnt do encryption')
try:
return self.keys[address]
except KeyError:
raise UnknownAccountError(address)
2021-03-17 15:34:51 +01:00
def list(self):
return list(self.keys.keys())
2021-01-09 22:05:24 +01:00
def import_key(self, pk, password=None):
2021-03-17 15:34:51 +01:00
address_hex = private_key_to_address(pk)
address_hex_clean = strip_0x(address_hex)
self.keys[address_hex_clean] = pk.secret
logg.debug('added key {}'.format(address_hex))
return address_hex_clean