Use SQLAlchemy
This commit is contained in:
parent
4eb2617198
commit
f507765331
@ -1,3 +1,5 @@
|
||||
* 0.3.0
|
||||
- Implement SQLAlchemy for db backend
|
||||
* 0.2.6
|
||||
- Upgrade confini
|
||||
* 0.2.5
|
||||
|
@ -4,9 +4,11 @@ import base64
|
||||
|
||||
# third-party imports
|
||||
from cryptography.fernet import Fernet
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
from psycopg2.extensions import make_dsn
|
||||
#import psycopg2
|
||||
#from psycopg2 import sql
|
||||
#from psycopg2.extensions import make_dsn
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
import sha3
|
||||
|
||||
# local imports
|
||||
@ -36,18 +38,28 @@ class ReferenceKeystore(Keystore):
|
||||
]
|
||||
|
||||
def __init__(self, dsn, **kwargs):
|
||||
logg.debug('dsn {}'.format(dsn))
|
||||
self.conn = psycopg2.connect(make_dsn(dsn))
|
||||
self.cur = self.conn.cursor()
|
||||
self.cur.execute(self.schema[0])
|
||||
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)
|
||||
self.db_session.commit()
|
||||
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):
|
||||
safe_address = strip_hex_prefix(address)
|
||||
s = sql.SQL('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = %s')
|
||||
self.cur.execute(s, [ safe_address ] )
|
||||
k = self.cur.fetchone()[0]
|
||||
s = text('SELECT key_ciphertext FROM ethereum WHERE wallet_address_hex = :a')
|
||||
r = self.db_session.execute(s, {
|
||||
'a': safe_address,
|
||||
},
|
||||
)
|
||||
k = r.first()[0]
|
||||
return self._decrypt(k, password)
|
||||
|
||||
|
||||
@ -55,11 +67,14 @@ class ReferenceKeystore(Keystore):
|
||||
pubk = keyapi.private_key_to_public_key(pk)
|
||||
address_hex = pubk.to_checksum_address()
|
||||
address_hex_clean = strip_hex_prefix(address_hex)
|
||||
logg.debug('importing address {}'.format(address_hex_clean))
|
||||
c = self._encrypt(pk.to_bytes(), password)
|
||||
s = sql.SQL('INSERT INTO ethereum (wallet_address_hex, key_ciphertext) VALUES (%s, %s)')
|
||||
self.cur.execute(s, [ address_hex_clean, c.decode('utf-8') ])
|
||||
self.conn.commit()
|
||||
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()
|
||||
return address_hex
|
||||
|
||||
|
||||
@ -81,10 +96,3 @@ class ReferenceKeystore(Keystore):
|
||||
def _decrypt(self, c, password):
|
||||
f = self._generate_encryption_engine(password)
|
||||
return f.decrypt(c.encode('utf-8'))
|
||||
|
||||
|
||||
def __del__(self):
|
||||
logg.debug('closing database')
|
||||
self.conn.commit()
|
||||
self.cur.close()
|
||||
self.conn.close()
|
||||
|
@ -1,15 +1,42 @@
|
||||
attrs==19.3.0
|
||||
base58==2.0.1
|
||||
bitarray==1.2.2
|
||||
certifi==2020.6.20
|
||||
cffi==1.14.1
|
||||
confini==0.2.5
|
||||
chardet==3.0.4
|
||||
confini==0.2.1
|
||||
cryptography==3.0
|
||||
cytoolz==0.10.1
|
||||
eth-abi==2.1.1
|
||||
eth-account==0.5.2
|
||||
eth-hash==0.2.0
|
||||
eth-keyfile==0.5.1
|
||||
eth-keys==0.3.3
|
||||
eth-rlp==0.1.2
|
||||
eth-typing==2.2.1
|
||||
eth-utils==1.9.0
|
||||
hexbytes==0.2.1
|
||||
idna==2.10
|
||||
ipfshttpclient==0.6.0.post1
|
||||
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
|
||||
pycparser==2.20
|
||||
pycryptodome==3.9.8
|
||||
pyrsistent==0.16.0
|
||||
pysha3==1.0.2
|
||||
python-gnupg==0.4.6
|
||||
requests==2.24.0
|
||||
rlp==1.2.0
|
||||
six==1.15.0
|
||||
SQLAlchemy==1.3.19
|
||||
toolz==0.10.0
|
||||
urllib3==1.25.10
|
||||
varint==1.0.2
|
||||
web3==5.12.0
|
||||
websockets==8.1
|
||||
|
3
setup.py
3
setup.py
@ -6,7 +6,7 @@ f.close()
|
||||
|
||||
setup(
|
||||
name="crypto-dev-signer",
|
||||
version="0.2.6",
|
||||
version="0.3.0",
|
||||
description="A signer and keystore daemon and library for cryptocurrency software development",
|
||||
author="Louis Holbrook",
|
||||
author_email="dev@holbrook.no",
|
||||
@ -27,6 +27,7 @@ setup(
|
||||
'rlp',
|
||||
'json-rpc',
|
||||
'confini==0.2.5',
|
||||
'sqlalchemy==1.3.19',
|
||||
],
|
||||
long_description=long_description,
|
||||
long_description_content_type='text/markdown',
|
||||
|
@ -35,19 +35,15 @@ class TestDatabase(unittest.TestCase):
|
||||
kw = {
|
||||
'symmetric_key': self.symkey,
|
||||
}
|
||||
self.db = ReferenceKeystore('signer_test', **kw)
|
||||
for ss in ReferenceKeystore.schema:
|
||||
self.db.cur.execute(ss)
|
||||
self.db.conn.commit()
|
||||
self.db = ReferenceKeystore('postgres+psycopg2://postgres@localhost:5432/signer_test', **kw)
|
||||
self.address_hex = self.db.new('foo')
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
self.db.conn = psycopg2.connect('dbname=signer_test')
|
||||
self.db.cur = self.db.conn.cursor()
|
||||
self.db.cur.execute('DROP INDEX ethereum_address_idx;')
|
||||
self.db.cur.execute('DROP TABLE ethereum;')
|
||||
self.db.conn.commit()
|
||||
self.db.db_session.execute('DROP INDEX ethereum_address_idx;')
|
||||
self.db.db_session.execute('DROP TABLE ethereum;')
|
||||
self.db.db_session.commit()
|
||||
|
||||
|
||||
|
||||
def test_get_key(self):
|
||||
|
Loading…
Reference in New Issue
Block a user