2020-08-08 10:45:37 +02:00
|
|
|
# standard imports
|
2020-08-05 18:14:25 +02:00
|
|
|
import logging
|
|
|
|
import base64
|
|
|
|
|
2020-08-08 10:45:37 +02:00
|
|
|
# third-party imports
|
2020-08-05 18:14:25 +02:00
|
|
|
from cryptography.fernet import Fernet
|
2020-10-20 08:37:20 +02:00
|
|
|
#import psycopg2
|
|
|
|
#from psycopg2 import sql
|
|
|
|
#from psycopg2.extensions import make_dsn
|
|
|
|
from sqlalchemy import create_engine, text
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
2020-08-05 19:47:38 +02:00
|
|
|
import sha3
|
|
|
|
|
2020-08-08 10:45:37 +02:00
|
|
|
# local imports
|
2020-08-08 11:33:15 +02:00
|
|
|
from .interface import Keystore
|
2020-09-21 19:10:20 +02:00
|
|
|
from crypto_dev_signer.common import strip_hex_prefix
|
|
|
|
from . import keyapi
|
2020-12-25 09:21:09 +01:00
|
|
|
from crypto_dev_signer.error import UnknownAccountError
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
logg = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
2020-08-05 19:51:22 +02:00
|
|
|
def to_bytes(x):
|
|
|
|
return x.encode('utf-8')
|
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2020-08-06 11:07:18 +02:00
|
|
|
class ReferenceKeystore(Keystore):
|
|
|
|
|
|
|
|
schema = [
|
2020-09-19 14:23:23 +02:00
|
|
|
"""CREATE TABLE IF NOT EXISTS ethereum (
|
2020-08-06 11:07:18 +02:00
|
|
|
id SERIAL NOT NULL PRIMARY KEY,
|
|
|
|
key_ciphertext VARCHAR(256) NOT NULL,
|
|
|
|
wallet_address_hex CHAR(40) NOT NULL
|
|
|
|
);
|
|
|
|
""",
|
2020-09-19 14:23:23 +02:00
|
|
|
"""CREATE UNIQUE INDEX IF NOT EXISTS ethereum_address_idx ON ethereum ( wallet_address_hex );
|
2020-08-06 11:07:18 +02:00
|
|
|
""",
|
|
|
|
]
|
|
|
|
|
2020-10-17 11:06:52 +02:00
|
|
|
def __init__(self, dsn, **kwargs):
|
2020-10-20 08:37:20 +02:00
|
|
|
logg.debug('starting db session with dsn {}'.format(dsn))
|
|
|
|
self.db_engine = create_engine(dsn)
|
|
|
|
self.db_session = sessionmaker(bind=self.db_engine)()
|
|
|
|
for s in self.schema:
|
|
|
|
self.db_session.execute(s)
|
2021-02-28 08:39:15 +01:00
|
|
|
self.db_session.commit()
|
2020-08-05 19:47:38 +02:00
|
|
|
self.symmetric_key = kwargs.get('symmetric_key')
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
|
2020-10-20 08:37:20 +02:00
|
|
|
def __del__(self):
|
|
|
|
logg.debug('closing db session')
|
|
|
|
self.db_session.close()
|
|
|
|
|
|
|
|
|
2020-08-05 19:47:38 +02:00
|
|
|
def get(self, address, password=None):
|
2020-08-06 11:07:18 +02:00
|
|
|
safe_address = strip_hex_prefix(address)
|
2020-10-20 08:37:20 +02:00
|
|
|
s = text('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = :a')
|
|
|
|
r = self.db_session.execute(s, {
|
|
|
|
'a': safe_address,
|
|
|
|
},
|
|
|
|
)
|
2020-12-25 09:21:09 +01:00
|
|
|
try:
|
|
|
|
k = r.first()[0]
|
|
|
|
except TypeError:
|
2021-02-28 08:39:15 +01:00
|
|
|
self.db_session.rollback()
|
2020-12-25 09:21:09 +01:00
|
|
|
raise UnknownAccountError(address)
|
2021-02-28 08:39:15 +01:00
|
|
|
self.db_session.commit()
|
2020-08-05 19:47:38 +02:00
|
|
|
return self._decrypt(k, password)
|
|
|
|
|
|
|
|
|
2020-09-20 10:00:59 +02:00
|
|
|
def import_key(self, pk, password=None):
|
2020-08-05 21:30:08 +02:00
|
|
|
pubk = keyapi.private_key_to_public_key(pk)
|
|
|
|
address_hex = pubk.to_checksum_address()
|
|
|
|
address_hex_clean = strip_hex_prefix(address_hex)
|
2020-08-05 19:47:38 +02:00
|
|
|
c = self._encrypt(pk.to_bytes(), password)
|
2020-10-20 08:37:20 +02:00
|
|
|
s = text('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (:a, :c)') #%s, %s)')
|
|
|
|
self.db_session.execute(s, {
|
|
|
|
'a': address_hex_clean,
|
|
|
|
'c': c.decode('utf-8'),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.db_session.commit()
|
2020-10-26 09:02:29 +01:00
|
|
|
logg.info('added private key for address {}'.format(address_hex_clean))
|
2020-08-05 21:30:08 +02:00
|
|
|
return address_hex
|
2020-08-05 19:47:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _encrypt(self, private_key, password):
|
|
|
|
f = self._generate_encryption_engine(password)
|
|
|
|
return f.encrypt(private_key)
|
|
|
|
|
|
|
|
|
|
|
|
def _generate_encryption_engine(self, password):
|
|
|
|
h = sha3.keccak_256()
|
|
|
|
h.update(self.symmetric_key)
|
|
|
|
if password != None:
|
2020-08-05 19:51:22 +02:00
|
|
|
password_bytes = to_bytes(password)
|
|
|
|
h.update(password_bytes)
|
2020-08-05 19:47:38 +02:00
|
|
|
g = h.digest()
|
|
|
|
return Fernet(base64.b64encode(g))
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
|
2020-08-05 19:47:38 +02:00
|
|
|
def _decrypt(self, c, password):
|
|
|
|
f = self._generate_encryption_engine(password)
|
|
|
|
return f.decrypt(c.encode('utf-8'))
|