2020-08-08 10:45:37 +02:00
|
|
|
# standard imports
|
2020-08-05 18:14:25 +02:00
|
|
|
import logging
|
|
|
|
import base64
|
|
|
|
|
2021-03-17 15:34:51 +01:00
|
|
|
# external 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
|
2021-04-09 16:05:16 +02:00
|
|
|
from hexathon import (
|
|
|
|
strip_0x,
|
|
|
|
add_0x,
|
|
|
|
)
|
2020-08-05 19:47:38 +02:00
|
|
|
|
2020-08-08 10:45:37 +02:00
|
|
|
# local imports
|
2020-08-08 11:33:15 +02:00
|
|
|
from .interface import Keystore
|
2021-03-17 15:34:51 +01:00
|
|
|
#from . import keyapi
|
2020-12-25 09:21:09 +01:00
|
|
|
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
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2021-08-21 09:32:46 +02:00
|
|
|
logg = logging.getLogger(__name__)
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
|
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):
|
2021-04-09 16:05:16 +02:00
|
|
|
safe_address = strip_0x(address).lower()
|
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()
|
2021-04-09 16:05:16 +02:00
|
|
|
raise UnknownAccountError(safe_address)
|
2021-02-28 08:39:15 +01:00
|
|
|
self.db_session.commit()
|
2021-04-09 16:05:16 +02:00
|
|
|
a = self._decrypt(k, password)
|
|
|
|
return a
|
2020-08-05 19:47:38 +02:00
|
|
|
|
|
|
|
|
2020-09-20 10:00:59 +02:00
|
|
|
def import_key(self, pk, password=None):
|
2021-03-17 15:34:51 +01:00
|
|
|
address_hex = private_key_to_address(pk)
|
2021-04-09 16:05:16 +02:00
|
|
|
address_hex_clean = strip_0x(address_hex).lower()
|
2021-03-17 15:34:51 +01:00
|
|
|
|
|
|
|
c = self._encrypt(pk.secret, 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))
|
2021-04-09 16:05:16 +02:00
|
|
|
return add_0x(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'))
|