Skip to content

Commit

Permalink
Fix to store encrypted fields in postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Feb 24, 2024
1 parent 9403f24 commit a005eee
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,6 @@ class EncryptedBinary(TypeDecorator):

cache_ok = True
key, impl = _get_encryption_key(LargeBinary)
# if DATABASE_ENCRYPTION_ENABLED:
# if not DB_FIELD_ENCRYPTION_KEY:
# _logger.warning("Database encryption is enabled but value DB_FIELD_ENCRYPTION_KEY not found in "
# "environment. Please supply 32 bytes key as hexadecimal string.")
# if DB_FIELD_ENCRYPTION_KEY:
# key = bytes().fromhex(DB_FIELD_ENCRYPTION_KEY)

def process_bind_param(self, value, dialect):
if value is None or self.key is None or not (DB_FIELD_ENCRYPTION_KEY or DB_FIELD_ENCRYPTION_PASSWORD):
Expand All @@ -182,16 +176,7 @@ class EncryptedString(TypeDecorator):
FieldType for encrypted String storage using EAS encryption
"""

# impl = String
cache_ok = True
# key = None
# if DATABASE_ENCRYPTION_ENABLED:
# if not DB_FIELD_ENCRYPTION_KEY:
# _logger.warning("Database encryption is enabled but value DB_FIELD_ENCRYPTION_KEY not found in "
# "environment. Please supply 32 bytes key as hexadecimal string.")
# if DB_FIELD_ENCRYPTION_KEY:
# impl = LargeBinary
# key = bytes().fromhex(DB_FIELD_ENCRYPTION_KEY)
key, impl = _get_encryption_key(String)

def process_bind_param(self, value, dialect):
Expand All @@ -204,6 +189,8 @@ def process_bind_param(self, value, dialect):
def process_result_value(self, value, dialect):
if value is None or self.key is None or not (DB_FIELD_ENCRYPTION_KEY or DB_FIELD_ENCRYPTION_PASSWORD):
return value
if value.startswith('\\x'):
value = bytes.fromhex(value[2:])
return aes_decrypt(value, self.key).decode('utf8')


Expand Down Expand Up @@ -306,7 +293,7 @@ class DbKey(Base):
address_index = Column(BigInteger, doc="Index of address in HD key structure address level")
public = Column(LargeBinary(65), index=True, doc="Bytes representation of public key")
private = Column(EncryptedBinary(48), doc="Bytes representation of private key")
wif = Column(EncryptedString(128), index=True, doc="Public or private WIF (Wallet Import Format) representation")
wif = Column(EncryptedString(260), index=True, doc="Public or private WIF (Wallet Import Format) representation")
compressed = Column(Boolean, default=True, doc="Is key compressed or not. Default is True")
key_type = Column(String(10), default='bip32', doc="Type of key: single, bip32 or multisig. Default is bip32")
address = Column(String(100), index=True,
Expand Down

0 comments on commit a005eee

Please sign in to comment.