funga/crypto_dev_signer/keystore/interface.py

48 lines
1.4 KiB
Python
Raw Normal View History

2020-09-21 19:10:20 +02:00
# standard imports
import os
2021-03-17 15:34:51 +01:00
import json
import logging
2020-09-21 19:10:20 +02:00
# local imports
2021-03-17 15:34:51 +01:00
from crypto_dev_signer.keystore import keyfile
from crypto_dev_signer.encoding import private_key_from_bytes
2020-09-21 19:10:20 +02:00
2021-03-17 15:34:51 +01:00
logg = logging.getLogger(__name__)
2020-09-21 19:10:20 +02:00
class Keystore:
def get(self, address, password=None):
raise NotImplementedError
2020-09-21 19:10:20 +02:00
2021-03-17 15:34:51 +01:00
def list(self):
raise NotImplementedError
def new(self, password=None):
2020-09-21 19:10:20 +02:00
b = os.urandom(32)
return self.import_raw_key(b, password)
def import_raw_key(self, b, password=None):
pk = private_key_from_bytes(b)
2020-09-21 19:10:20 +02:00
return self.import_key(pk, password)
2020-09-20 10:00:59 +02:00
def import_key(self, pk, password=None):
raise NotImplementedError
2020-09-21 19:10:20 +02:00
def import_keystore_data(self, keystore_content, password=''):
2021-03-17 15:34:51 +01:00
if type(keystore_content).__name__ == 'str':
keystore_content = json.loads(keystore_content)
elif type(keystore_content).__name__ == 'bytes':
logg.debug('bytes {}'.format(keystore_content))
keystore_content = json.loads(keystore_content.decode('utf-8'))
private_key = keyfile.from_dict(keystore_content, password.encode('utf-8'))
return self.import_raw_key(private_key, password)
2020-09-21 19:10:20 +02:00
def import_keystore_file(self, keystore_file, password=''):
private_key = keyfile.from_file(keystore_file, password)
return self.import_raw_key(private_key)