2020-09-21 19:10:20 +02:00
|
|
|
# standard imports
|
|
|
|
import os
|
2021-03-17 15:34:51 +01:00
|
|
|
import json
|
|
|
|
import logging
|
2021-01-09 20:25:47 +01:00
|
|
|
|
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
|
2021-07-20 17:46:19 +02:00
|
|
|
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
|
|
|
|
2020-08-06 14:11:43 +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
|
|
|
|
|
|
|
|
|
2020-08-06 14:11:43 +02:00
|
|
|
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):
|
2021-07-20 17:46:19 +02:00
|
|
|
pk = private_key_from_bytes(b)
|
2020-09-21 19:10:20 +02:00
|
|
|
return self.import_key(pk, password)
|
|
|
|
|
2020-08-06 14:11:43 +02:00
|
|
|
|
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
|
|
|
|
2021-01-09 20:25:47 +01: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'))
|
2021-01-09 20:25:47 +01:00
|
|
|
return self.import_raw_key(private_key, password)
|
2020-09-21 19:10:20 +02:00
|
|
|
|
2021-01-09 20:25:47 +01:00
|
|
|
def import_keystore_file(self, keystore_file, password=''):
|
2021-03-22 18:42:56 +01:00
|
|
|
private_key = keyfile.from_file(keystore_file, password)
|
|
|
|
return self.import_raw_key(private_key)
|