Use SQLAlchemy

This commit is contained in:
nolash 2020-10-20 08:37:20 +02:00
parent 4eb2617198
commit f507765331
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 66 additions and 32 deletions

View File

@ -1,3 +1,5 @@
* 0.3.0
- Implement SQLAlchemy for db backend
* 0.2.6 * 0.2.6
- Upgrade confini - Upgrade confini
* 0.2.5 * 0.2.5

View File

@ -4,9 +4,11 @@ import base64
# third-party imports # third-party imports
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
import psycopg2 #import psycopg2
from psycopg2 import sql #from psycopg2 import sql
from psycopg2.extensions import make_dsn #from psycopg2.extensions import make_dsn
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
import sha3 import sha3
# local imports # local imports
@ -36,18 +38,28 @@ class ReferenceKeystore(Keystore):
] ]
def __init__(self, dsn, **kwargs): def __init__(self, dsn, **kwargs):
logg.debug('dsn {}'.format(dsn)) logg.debug('starting db session with dsn {}'.format(dsn))
self.conn = psycopg2.connect(make_dsn(dsn)) self.db_engine = create_engine(dsn)
self.cur = self.conn.cursor() self.db_session = sessionmaker(bind=self.db_engine)()
self.cur.execute(self.schema[0]) for s in self.schema:
self.db_session.execute(s)
self.db_session.commit()
self.symmetric_key = kwargs.get('symmetric_key') self.symmetric_key = kwargs.get('symmetric_key')
def __del__(self):
logg.debug('closing db session')
self.db_session.close()
def get(self, address, password=None): def get(self, address, password=None):
safe_address = strip_hex_prefix(address) safe_address = strip_hex_prefix(address)
s = sql.SQL('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = %s') s = text('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = :a')
self.cur.execute(s, [ safe_address ] ) r = self.db_session.execute(s, {
k = self.cur.fetchone()[0] 'a': safe_address,
},
)
k = r.first()[0]
return self._decrypt(k, password) return self._decrypt(k, password)
@ -55,11 +67,14 @@ class ReferenceKeystore(Keystore):
pubk = keyapi.private_key_to_public_key(pk) pubk = keyapi.private_key_to_public_key(pk)
address_hex = pubk.to_checksum_address() address_hex = pubk.to_checksum_address()
address_hex_clean = strip_hex_prefix(address_hex) address_hex_clean = strip_hex_prefix(address_hex)
logg.debug('importing address {}'.format(address_hex_clean))
c = self._encrypt(pk.to_bytes(), password) c = self._encrypt(pk.to_bytes(), password)
s = sql.SQL('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (%s, %s)') s = text('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (:a, :c)') #%s, %s)')
self.cur.execute(s, [ address_hex_clean, c.decode('utf-8') ]) self.db_session.execute(s, {
self.conn.commit() 'a': address_hex_clean,
'c': c.decode('utf-8'),
},
)
self.db_session.commit()
return address_hex return address_hex
@ -81,10 +96,3 @@ class ReferenceKeystore(Keystore):
def _decrypt(self, c, password): def _decrypt(self, c, password):
f = self._generate_encryption_engine(password) f = self._generate_encryption_engine(password)
return f.decrypt(c.encode('utf-8')) return f.decrypt(c.encode('utf-8'))
def __del__(self):
logg.debug('closing database')
self.conn.commit()
self.cur.close()
self.conn.close()

View File

@ -1,15 +1,42 @@
attrs==19.3.0
base58==2.0.1
bitarray==1.2.2
certifi==2020.6.20
cffi==1.14.1 cffi==1.14.1
confini==0.2.5 chardet==3.0.4
confini==0.2.1
cryptography==3.0 cryptography==3.0
cytoolz==0.10.1 cytoolz==0.10.1
eth-abi==2.1.1
eth-account==0.5.2
eth-hash==0.2.0 eth-hash==0.2.0
eth-keyfile==0.5.1
eth-keys==0.3.3 eth-keys==0.3.3
eth-rlp==0.1.2
eth-typing==2.2.1 eth-typing==2.2.1
eth-utils==1.9.0 eth-utils==1.9.0
hexbytes==0.2.1
idna==2.10
ipfshttpclient==0.6.0.post1
json-rpc==1.13.0 json-rpc==1.13.0
jsonschema==3.2.0
lru-dict==1.1.6
multiaddr==0.0.9
netaddr==0.8.0
parsimonious==0.8.1
protobuf==3.12.4
psycopg2==2.8.5 psycopg2==2.8.5
pycparser==2.20 pycparser==2.20
pycryptodome==3.9.8
pyrsistent==0.16.0
pysha3==1.0.2 pysha3==1.0.2
python-gnupg==0.4.6
requests==2.24.0
rlp==1.2.0 rlp==1.2.0
six==1.15.0 six==1.15.0
SQLAlchemy==1.3.19
toolz==0.10.0 toolz==0.10.0
urllib3==1.25.10
varint==1.0.2
web3==5.12.0
websockets==8.1

View File

@ -6,7 +6,7 @@ f.close()
setup( setup(
name="crypto-dev-signer", name="crypto-dev-signer",
version="0.2.6", version="0.3.0",
description="A signer and keystore daemon and library for cryptocurrency software development", description="A signer and keystore daemon and library for cryptocurrency software development",
author="Louis Holbrook", author="Louis Holbrook",
author_email="dev@holbrook.no", author_email="dev@holbrook.no",
@ -27,6 +27,7 @@ setup(
'rlp', 'rlp',
'json-rpc', 'json-rpc',
'confini==0.2.5', 'confini==0.2.5',
'sqlalchemy==1.3.19',
], ],
long_description=long_description, long_description=long_description,
long_description_content_type='text/markdown', long_description_content_type='text/markdown',

View File

@ -35,19 +35,15 @@ class TestDatabase(unittest.TestCase):
kw = { kw = {
'symmetric_key': self.symkey, 'symmetric_key': self.symkey,
} }
self.db = ReferenceKeystore('signer_test', **kw) self.db = ReferenceKeystore('postgres+psycopg2://postgres@localhost:5432/signer_test', **kw)
for ss in ReferenceKeystore.schema:
self.db.cur.execute(ss)
self.db.conn.commit()
self.address_hex = self.db.new('foo') self.address_hex = self.db.new('foo')
def tearDown(self): def tearDown(self):
self.db.conn = psycopg2.connect('dbname=signer_test') self.db.db_session.execute('DROP INDEX ethereum_address_idx;')
self.db.cur = self.db.conn.cursor() self.db.db_session.execute('DROP TABLE ethereum;')
self.db.cur.execute('DROP INDEX ethereum_address_idx;') self.db.db_session.commit()
self.db.cur.execute('DROP TABLE ethereum;')
self.db.conn.commit()
def test_get_key(self): def test_get_key(self):