Files
pallectrum/electrum/storage.py

283 lines
10 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import threading
import stat
import hashlib
import base64
import zlib
from enum import IntEnum
2019-02-25 20:22:23 +01:00
from . import ecc
2019-04-26 18:52:26 +02:00
from .util import profiler, InvalidPassword, WalletFileException, bfh, standardize_path
from .plugin import run_hook, plugin_loaders
2019-02-19 11:56:46 +01:00
from .json_db import JsonDB
2019-04-26 18:52:26 +02:00
from .logging import Logger
def get_derivation_used_for_hw_device_encryption():
return ("m"
"/4541509'" # ascii 'ELE' as decimal ("BIP43 purpose")
"/1112098098'") # ascii 'BIE2' as decimal
class StorageEncryptionVersion(IntEnum):
PLAINTEXT = 0
USER_PASSWORD = 1
XPUB_PASSWORD = 2
2018-07-23 19:57:41 +02:00
class StorageReadWriteError(Exception): pass
2019-04-26 18:52:26 +02:00
class WalletStorage(Logger):
2019-02-19 11:56:46 +01:00
2019-02-25 20:22:23 +01:00
def __init__(self, path, *, manual_upgrades=False):
2019-04-26 18:52:26 +02:00
Logger.__init__(self)
self.lock = threading.RLock()
2019-02-08 12:59:06 +01:00
self.path = standardize_path(path)
2019-02-07 13:56:11 +01:00
self._file_exists = self.path and os.path.exists(self.path)
2018-07-23 19:57:41 +02:00
2019-02-19 11:56:46 +01:00
DB_Class = JsonDB
2019-04-26 18:52:26 +02:00
self.logger.info(f"wallet path {self.path}")
2019-02-19 11:56:46 +01:00
self.pubkey = None
self._test_read_write_permissions(self.path)
2019-02-19 11:56:46 +01:00
if self.file_exists():
with open(self.path, "r", encoding='utf-8') as f:
self.raw = f.read()
self._encryption_version = self._init_encryption_version()
if not self.is_encrypted():
2019-02-25 20:22:23 +01:00
self.db = DB_Class(self.raw, manual_upgrades=manual_upgrades)
2019-02-28 15:50:37 +01:00
self.load_plugins()
2019-02-19 11:56:46 +01:00
else:
self._encryption_version = StorageEncryptionVersion.PLAINTEXT
2019-02-19 11:56:46 +01:00
# avoid new wallets getting 'upgraded'
2019-02-25 20:22:23 +01:00
self.db = DB_Class('', manual_upgrades=False)
2019-02-19 11:56:46 +01:00
@classmethod
def _test_read_write_permissions(cls, path):
# note: There might already be a file at 'path'.
# Make sure we do NOT overwrite/corrupt that!
temp_path = "%s.tmptest.%s" % (path, os.getpid())
echo = "fs r/w test"
try:
# test READ permissions for actual path
if os.path.exists(path):
with open(path, "r", encoding='utf-8') as f:
f.read(1) # read 1 byte
# test R/W sanity for "similar" path
with open(temp_path, "w", encoding='utf-8') as f:
f.write(echo)
with open(temp_path, "r", encoding='utf-8') as f:
echo2 = f.read()
os.remove(temp_path)
except Exception as e:
raise StorageReadWriteError(e) from e
if echo != echo2:
raise StorageReadWriteError('echo sanity-check failed')
def load_plugins(self):
wallet_type = self.db.get('wallet_type')
if wallet_type in plugin_loaders:
plugin_loaders[wallet_type]()
2019-02-19 11:56:46 +01:00
def put(self, key,value):
self.db.put(key, value)
2018-07-23 19:57:41 +02:00
2019-02-19 11:56:46 +01:00
def get(self, key, default=None):
return self.db.get(key, default)
2018-07-23 19:57:41 +02:00
@profiler
def write(self):
with self.lock:
2018-07-23 19:57:41 +02:00
self._write()
def _write(self):
if threading.currentThread().isDaemon():
2019-04-26 18:52:26 +02:00
self.logger.warning('daemon thread cannot write db')
2018-07-23 19:57:41 +02:00
return
if not self.db.modified():
2018-07-23 19:57:41 +02:00
return
2019-02-19 11:56:46 +01:00
self.db.commit()
s = self.encrypt_before_writing(self.db.dump())
2018-07-23 19:57:41 +02:00
temp_path = "%s.tmp.%s" % (self.path, os.getpid())
with open(temp_path, "w", encoding='utf-8') as f:
f.write(s)
f.flush()
os.fsync(f.fileno())
2019-02-07 13:56:11 +01:00
mode = os.stat(self.path).st_mode if self.file_exists() else stat.S_IREAD | stat.S_IWRITE
2019-09-10 09:25:42 +02:00
# assert that wallet file does not exist, to prevent wallet corruption (see issue #5082)
2019-02-08 08:27:23 +01:00
if not self.file_exists():
assert not os.path.exists(self.path)
2018-11-30 04:08:02 +01:00
os.replace(temp_path, self.path)
2018-07-23 19:57:41 +02:00
os.chmod(self.path, mode)
2019-02-07 13:56:11 +01:00
self._file_exists = True
2019-04-26 18:52:26 +02:00
self.logger.info(f"saved {self.path}")
self.db.set_modified(False)
2018-07-23 19:57:41 +02:00
def file_exists(self):
2019-02-07 13:56:11 +01:00
return self._file_exists
2018-07-23 19:57:41 +02:00
def is_past_initial_decryption(self):
"""Return if storage is in a usable state for normal operations.
The value is True exactly
if encryption is disabled completely (self.is_encrypted() == False),
or if encryption is enabled but the contents have already been decrypted.
"""
2019-03-04 02:08:23 +01:00
try:
return bool(self.db.data)
except AttributeError:
return False
2017-03-06 08:33:35 +01:00
def is_encrypted(self):
"""Return if storage encryption is currently enabled."""
return self.get_encryption_version() != StorageEncryptionVersion.PLAINTEXT
def is_encrypted_with_user_pw(self):
return self.get_encryption_version() == StorageEncryptionVersion.USER_PASSWORD
def is_encrypted_with_hw_device(self):
return self.get_encryption_version() == StorageEncryptionVersion.XPUB_PASSWORD
def get_encryption_version(self):
"""Return the version of encryption used for this storage.
0: plaintext / no encryption
ECIES, private key derived from a password,
1: password is provided by user
2: password is derived from an xpub; used with hw wallets
"""
return self._encryption_version
def _init_encryption_version(self):
2017-03-06 08:33:35 +01:00
try:
magic = base64.b64decode(self.raw)[0:4]
if magic == b'BIE1':
return StorageEncryptionVersion.USER_PASSWORD
elif magic == b'BIE2':
return StorageEncryptionVersion.XPUB_PASSWORD
else:
return StorageEncryptionVersion.PLAINTEXT
2017-03-06 08:33:35 +01:00
except:
return StorageEncryptionVersion.PLAINTEXT
2017-03-06 08:33:35 +01:00
@staticmethod
def get_eckey_from_password(password):
secret = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), b'', iterations=1024)
ec_key = ecc.ECPrivkey.from_arbitrary_size_secret(secret)
2017-03-06 08:33:35 +01:00
return ec_key
def _get_encryption_magic(self):
v = self._encryption_version
if v == StorageEncryptionVersion.USER_PASSWORD:
return b'BIE1'
elif v == StorageEncryptionVersion.XPUB_PASSWORD:
return b'BIE2'
else:
raise WalletFileException('no encryption magic for version: %s' % v)
2017-03-06 08:33:35 +01:00
def decrypt(self, password):
ec_key = self.get_eckey_from_password(password)
if self.raw:
enc_magic = self._get_encryption_magic()
s = zlib.decompress(ec_key.decrypt_message(self.raw, enc_magic))
else:
s = None
self.pubkey = ec_key.get_public_key_hex()
2017-08-09 11:06:17 +02:00
s = s.decode('utf8')
2019-02-25 20:22:23 +01:00
self.db = JsonDB(s, manual_upgrades=True)
2019-02-28 15:50:37 +01:00
self.load_plugins()
2017-03-06 08:33:35 +01:00
2018-07-23 19:57:41 +02:00
def encrypt_before_writing(self, plaintext: str) -> str:
s = plaintext
if self.pubkey:
s = bytes(s, 'utf8')
c = zlib.compress(s)
enc_magic = self._get_encryption_magic()
public_key = ecc.ECPubkey(bfh(self.pubkey))
s = public_key.encrypt_message(c, enc_magic)
s = s.decode('utf8')
return s
def check_password(self, password):
"""Raises an InvalidPassword exception on invalid password"""
if not self.is_encrypted():
return
if self.pubkey and self.pubkey != self.get_eckey_from_password(password).get_public_key_hex():
raise InvalidPassword()
def set_keystore_encryption(self, enable):
self.put('use_encryption', enable)
def set_password(self, password, enc_version=None):
"""Set a password to be used for encrypting this storage."""
if enc_version is None:
enc_version = self._encryption_version
if password and enc_version != StorageEncryptionVersion.PLAINTEXT:
ec_key = self.get_eckey_from_password(password)
self.pubkey = ec_key.get_public_key_hex()
self._encryption_version = enc_version
2017-03-06 08:33:35 +01:00
else:
self.pubkey = None
self._encryption_version = StorageEncryptionVersion.PLAINTEXT
# make sure next storage.write() saves changes
self.db.set_modified(True)
2017-03-06 08:33:35 +01:00
2016-08-19 09:57:29 +02:00
def requires_upgrade(self):
if not self.is_past_initial_decryption():
raise Exception("storage not yet decrypted!")
2019-02-19 11:56:46 +01:00
return self.db.requires_upgrade()
2016-08-19 09:57:29 +02:00
def is_ready_to_be_used_by_wallet(self):
return not self.requires_upgrade() and self.db._called_after_upgrade_tasks
2016-08-19 09:57:29 +02:00
def upgrade(self):
2019-02-19 11:56:46 +01:00
self.db.upgrade()
2016-08-19 09:57:29 +02:00
self.write()
2019-02-19 11:56:46 +01:00
def requires_split(self):
return self.db.requires_split()
def split_accounts(self):
out = []
result = self.db.split_accounts()
for data in result:
path = self.path + '.' + data['suffix']
storage = WalletStorage(path)
storage.db.data = data
storage.db._called_after_upgrade_tasks = False
2019-02-19 11:56:46 +01:00
storage.db.upgrade()
storage.write()
out.append(path)
return out
def get_action(self):
action = run_hook('get_action', self)
return action