funga/crypto_dev_signer/keystore/dict.py

45 lines
1.1 KiB
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,
add_0x,
)
2021-03-17 15:34:51 +01:00
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
2021-08-21 09:32:46 +02:00
logg = logging.getLogger(__name__)
2021-01-09 22:05:24 +01:00
class DictKeystore(Keystore):
def __init__(self):
self.keys = {}
def get(self, address, password=None):
address_key = strip_0x(address).lower()
2021-01-09 22:05:24 +01:00
if password != None:
logg.debug('password ignored as dictkeystore doesnt do encryption')
try:
return self.keys[address_key]
2021-01-09 22:05:24 +01:00
except KeyError:
raise UnknownAccountError(address_key)
2021-01-09 22:05:24 +01:00
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).lower()
2021-03-17 15:34:51 +01:00
self.keys[address_hex_clean] = pk.secret
logg.debug('added key {}'.format(address_hex))
return add_0x(address_hex)