lbry-sdk/torba/wallet.py

117 lines
3.6 KiB
Python
Raw Normal View History

2018-05-25 08:03:25 +02:00
import stat
import json
import os
import typing
from typing import Sequence, MutableSequence
2018-05-25 08:03:25 +02:00
if typing.TYPE_CHECKING:
from torba import baseaccount
from torba import baseledger
from torba import basemanager
2018-05-25 08:03:25 +02:00
class Wallet:
""" The primary role of Wallet is to encapsulate a collection
of accounts (seed/private keys) and the spending rules / settings
for the coins attached to those accounts. Wallets are represented
by physical files on the filesystem.
"""
def __init__(self, name: str = 'Wallet', accounts: MutableSequence['baseaccount.BaseAccount'] = None,
storage: 'WalletStorage' = None) -> None:
2018-05-25 08:03:25 +02:00
self.name = name
self.accounts = accounts or []
2018-05-25 08:03:25 +02:00
self.storage = storage or WalletStorage()
def add_account(self, account):
2018-05-25 08:03:25 +02:00
self.accounts.append(account)
def generate_account(self, ledger: 'baseledger.BaseLedger') -> 'baseaccount.BaseAccount':
return ledger.account_class.generate(ledger, self)
2018-05-25 08:03:25 +02:00
@classmethod
def from_storage(cls, storage: 'WalletStorage', manager: 'basemanager.BaseWalletManager') -> 'Wallet':
2018-05-25 08:03:25 +02:00
json_dict = storage.read()
wallet = cls(
2018-05-25 08:03:25 +02:00
name=json_dict.get('name', 'Wallet'),
storage=storage
)
account_dicts: Sequence[dict] = json_dict.get('accounts', [])
for account_dict in account_dicts:
ledger = manager.get_or_create_ledger(account_dict['ledger'])
ledger.account_class.from_dict(ledger, wallet, account_dict)
return wallet
2018-05-25 08:03:25 +02:00
def to_dict(self):
return {
'version': WalletStorage.LATEST_VERSION,
2018-05-25 08:03:25 +02:00
'name': self.name,
'accounts': [a.to_dict() for a in self.accounts]
}
def save(self):
self.storage.write(self.to_dict())
@property
def default_account(self):
for account in self.accounts:
return account
class WalletStorage:
2018-07-01 23:20:17 +02:00
LATEST_VERSION = 1
2018-05-25 08:03:25 +02:00
def __init__(self, path=None, default=None):
self.path = path
2018-07-12 04:37:15 +02:00
self._default = default or {
'version': self.LATEST_VERSION,
'name': 'My Wallet',
'accounts': []
}
2018-05-25 08:03:25 +02:00
def read(self):
if self.path and os.path.exists(self.path):
2018-07-12 04:37:15 +02:00
with open(self.path, 'r') as f:
2018-05-25 08:03:25 +02:00
json_data = f.read()
json_dict = json.loads(json_data)
if json_dict.get('version') == self.LATEST_VERSION and \
set(json_dict) == set(self._default):
return json_dict
else:
return self.upgrade(json_dict)
else:
2018-07-12 04:37:15 +02:00
return self._default.copy()
2018-05-25 08:03:25 +02:00
2018-07-12 04:37:15 +02:00
def upgrade(self, json_dict):
2018-05-25 08:03:25 +02:00
json_dict = json_dict.copy()
version = json_dict.pop('version', -1)
2018-07-12 04:37:15 +02:00
if version == -1:
2018-07-01 23:20:17 +02:00
pass
2018-07-12 04:37:15 +02:00
upgraded = self._default.copy()
2018-05-25 08:03:25 +02:00
upgraded.update(json_dict)
return json_dict
def write(self, json_dict):
json_data = json.dumps(json_dict, indent=4, sort_keys=True)
if self.path is None:
return json_data
temp_path = "%s.tmp.%s" % (self.path, os.getpid())
with open(temp_path, "w") as f:
f.write(json_data)
f.flush()
os.fsync(f.fileno())
if os.path.exists(self.path):
mode = os.stat(self.path).st_mode
else:
mode = stat.S_IREAD | stat.S_IWRITE
try:
os.rename(temp_path, self.path)
except Exception: # pylint: disable=broad-except
2018-05-25 08:03:25 +02:00
os.remove(self.path)
os.rename(temp_path, self.path)
os.chmod(self.path, mode)