2018-08-09 02:41:29 +02:00
|
|
|
import json
|
2018-07-12 18:14:47 +02:00
|
|
|
import logging
|
2019-07-31 01:18:00 +02:00
|
|
|
from functools import partial
|
2019-03-11 14:52:35 +01:00
|
|
|
from hashlib import sha256
|
2019-03-19 00:34:01 +01:00
|
|
|
from string import hexdigits
|
2018-07-12 18:14:47 +02:00
|
|
|
|
2019-05-18 22:44:33 +02:00
|
|
|
import ecdsa
|
2019-12-10 00:44:50 +01:00
|
|
|
from lbry.wallet.constants import CLAIM_TYPES, TXO_TYPES
|
2019-05-18 22:44:33 +02:00
|
|
|
|
2019-12-31 20:52:57 +01:00
|
|
|
from lbry.wallet.client.baseaccount import BaseAccount, HierarchicalDeterministic
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-06-26 23:27:24 +02:00
|
|
|
|
2018-07-12 18:14:47 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2018-06-26 23:27:24 +02:00
|
|
|
|
2019-03-19 00:34:01 +01:00
|
|
|
def validate_claim_id(claim_id):
|
|
|
|
if not len(claim_id) == 40:
|
|
|
|
raise Exception("Incorrect claimid length: %i" % len(claim_id))
|
|
|
|
if isinstance(claim_id, bytes):
|
|
|
|
claim_id = claim_id.decode('utf-8')
|
|
|
|
if set(claim_id).difference(hexdigits):
|
|
|
|
raise Exception("Claim id is not hex encoded")
|
|
|
|
|
|
|
|
|
2018-06-26 23:27:24 +02:00
|
|
|
class Account(BaseAccount):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-22 00:34:59 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-03-24 21:55:04 +01:00
|
|
|
self.channel_keys = {}
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2019-03-11 14:52:35 +01:00
|
|
|
@property
|
|
|
|
def hash(self) -> bytes:
|
2019-10-16 07:18:39 +02:00
|
|
|
h = sha256(json.dumps(self.to_dict(include_channel_keys=False)).encode())
|
2019-03-24 21:55:04 +01:00
|
|
|
for cert in sorted(self.channel_keys.keys()):
|
2019-03-11 14:52:35 +01:00
|
|
|
h.update(cert.encode())
|
|
|
|
return h.digest()
|
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
def merge(self, d: dict):
|
|
|
|
super().merge(d)
|
2019-03-24 21:55:04 +01:00
|
|
|
self.channel_keys.update(d.get('certificates', {}))
|
2019-03-12 20:31:54 +01:00
|
|
|
|
2019-05-23 15:38:46 +02:00
|
|
|
def add_channel_private_key(self, private_key):
|
|
|
|
public_key_bytes = private_key.get_verifying_key().to_der()
|
|
|
|
channel_pubkey_hash = self.ledger.public_key_to_address(public_key_bytes)
|
|
|
|
self.channel_keys[channel_pubkey_hash] = private_key.to_pem().decode()
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2019-05-29 00:46:50 +02:00
|
|
|
def get_channel_private_key(self, public_key_bytes):
|
|
|
|
channel_pubkey_hash = self.ledger.public_key_to_address(public_key_bytes)
|
2019-05-23 15:38:46 +02:00
|
|
|
private_key_pem = self.channel_keys.get(channel_pubkey_hash)
|
2019-05-29 00:46:50 +02:00
|
|
|
if private_key_pem:
|
|
|
|
return ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=sha256)
|
2018-07-09 15:55:07 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def maybe_migrate_certificates(self):
|
2019-03-24 21:55:04 +01:00
|
|
|
if not self.channel_keys:
|
2018-08-09 02:41:29 +02:00
|
|
|
return
|
2019-05-29 00:46:50 +02:00
|
|
|
channel_keys = {}
|
|
|
|
for private_key_pem in self.channel_keys.values():
|
2019-06-23 18:55:24 +02:00
|
|
|
if not isinstance(private_key_pem, str):
|
|
|
|
continue
|
|
|
|
if "-----BEGIN EC PRIVATE KEY-----" not in private_key_pem:
|
|
|
|
continue
|
2019-05-29 00:46:50 +02:00
|
|
|
private_key = ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=sha256)
|
|
|
|
public_key_der = private_key.get_verifying_key().to_der()
|
|
|
|
channel_keys[self.ledger.public_key_to_address(public_key_der)] = private_key_pem
|
2019-10-18 18:57:19 +02:00
|
|
|
if self.channel_keys != channel_keys:
|
|
|
|
self.channel_keys = channel_keys
|
|
|
|
self.wallet.save()
|
2018-07-12 05:57:22 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def save_max_gap(self):
|
2019-05-04 23:22:32 +02:00
|
|
|
if issubclass(self.address_generator, HierarchicalDeterministic):
|
|
|
|
gap = await self.get_max_gap()
|
2019-10-18 19:12:09 +02:00
|
|
|
gap_changed = False
|
|
|
|
new_receiving_gap = max(20, gap['max_receiving_gap'] + 1)
|
|
|
|
if self.receiving.gap != new_receiving_gap:
|
|
|
|
self.receiving.gap = new_receiving_gap
|
|
|
|
gap_changed = True
|
|
|
|
new_change_gap = max(6, gap['max_change_gap'] + 1)
|
|
|
|
if self.change.gap != new_change_gap:
|
|
|
|
self.change.gap = new_change_gap
|
|
|
|
gap_changed = True
|
|
|
|
if gap_changed:
|
|
|
|
self.wallet.save()
|
2018-10-10 03:39:29 +02:00
|
|
|
|
2018-10-08 20:09:37 +02:00
|
|
|
def get_balance(self, confirmations=0, include_claims=False, **constraints):
|
2018-07-17 05:32:37 +02:00
|
|
|
if not include_claims:
|
2019-12-10 00:44:50 +01:00
|
|
|
constraints.update({'txo_type__in': (0, TXO_TYPES['purchase'])})
|
2018-07-22 00:34:59 +02:00
|
|
|
return super().get_balance(confirmations, **constraints)
|
2018-07-17 05:32:37 +02:00
|
|
|
|
2019-10-14 01:32:10 +02:00
|
|
|
async def get_detailed_balance(self, confirmations=0, reserved_subtotals=False):
|
2019-07-31 01:18:00 +02:00
|
|
|
tips_balance, supports_balance, claims_balance = 0, 0, 0
|
|
|
|
get_total_balance = partial(self.get_balance, confirmations=confirmations, include_claims=True)
|
|
|
|
total = await get_total_balance()
|
|
|
|
if reserved_subtotals:
|
2019-11-13 23:50:35 +01:00
|
|
|
claims_balance = await get_total_balance(txo_type__in=CLAIM_TYPES)
|
2019-07-31 01:18:00 +02:00
|
|
|
for amount, spent, from_me, to_me, height in await self.get_support_summary():
|
|
|
|
if confirmations > 0 and not 0 < height <= self.ledger.headers.height - (confirmations - 1):
|
|
|
|
continue
|
|
|
|
if not spent and to_me:
|
|
|
|
if from_me:
|
|
|
|
supports_balance += amount
|
|
|
|
else:
|
|
|
|
tips_balance += amount
|
|
|
|
reserved = claims_balance + supports_balance + tips_balance
|
|
|
|
else:
|
|
|
|
reserved = await self.get_balance(
|
2019-09-13 15:16:17 +02:00
|
|
|
confirmations=confirmations, include_claims=True, txo_type__gt=0
|
2019-07-31 01:18:00 +02:00
|
|
|
)
|
|
|
|
return {
|
2019-10-14 01:32:10 +02:00
|
|
|
'total': total,
|
|
|
|
'available': total - reserved,
|
|
|
|
'reserved': reserved,
|
2019-07-31 01:18:00 +02:00
|
|
|
'reserved_subtotals': {
|
2019-10-14 01:32:10 +02:00
|
|
|
'claims': claims_balance,
|
|
|
|
'supports': supports_balance,
|
|
|
|
'tips': tips_balance
|
2019-07-31 01:18:00 +02:00
|
|
|
} if reserved_subtotals else None
|
|
|
|
}
|
|
|
|
|
2018-08-06 06:28:11 +02:00
|
|
|
@classmethod
|
2019-03-25 03:20:17 +01:00
|
|
|
def get_private_key_from_seed(cls, ledger, seed: str, password: str):
|
2018-08-06 06:28:11 +02:00
|
|
|
return super().get_private_key_from_seed(
|
|
|
|
ledger, seed, password or 'lbryum'
|
|
|
|
)
|
|
|
|
|
2018-07-12 07:47:34 +02:00
|
|
|
@classmethod
|
2018-08-09 02:41:29 +02:00
|
|
|
def from_dict(cls, ledger, wallet, d: dict) -> 'Account':
|
|
|
|
account = super().from_dict(ledger, wallet, d)
|
2019-03-24 21:55:04 +01:00
|
|
|
account.channel_keys = d.get('certificates', {})
|
2018-07-12 07:47:34 +02:00
|
|
|
return account
|
|
|
|
|
2019-10-16 07:18:39 +02:00
|
|
|
def to_dict(self, encrypt_password: str = None, include_channel_keys: bool = True):
|
|
|
|
d = super().to_dict(encrypt_password)
|
2019-03-24 21:55:04 +01:00
|
|
|
if include_channel_keys:
|
|
|
|
d['certificates'] = self.channel_keys
|
2018-07-12 07:47:34 +02:00
|
|
|
return d
|
2018-08-01 04:59:51 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def get_details(self, **kwargs):
|
|
|
|
details = await super().get_details(**kwargs)
|
2019-03-24 21:55:04 +01:00
|
|
|
details['certificates'] = len(self.channel_keys)
|
2018-08-30 06:04:25 +02:00
|
|
|
return details
|
|
|
|
|
2019-08-12 06:40:05 +02:00
|
|
|
def get_transaction_history(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_transaction_history(wallet=self.wallet, accounts=[self], **constraints)
|
2018-10-10 02:46:41 +02:00
|
|
|
|
2019-08-12 06:40:05 +02:00
|
|
|
def get_transaction_history_count(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_transaction_history_count(wallet=self.wallet, accounts=[self], **constraints)
|
2018-10-10 02:46:41 +02:00
|
|
|
|
|
|
|
def get_claims(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_claims(wallet=self.wallet, accounts=[self], **constraints)
|
2018-10-10 02:46:41 +02:00
|
|
|
|
|
|
|
def get_claim_count(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_claim_count(wallet=self.wallet, accounts=[self], **constraints)
|
2018-10-10 02:46:41 +02:00
|
|
|
|
2019-03-25 23:30:43 +01:00
|
|
|
def get_streams(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_streams(wallet=self.wallet, accounts=[self], **constraints)
|
2019-03-25 23:30:43 +01:00
|
|
|
|
|
|
|
def get_stream_count(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_stream_count(wallet=self.wallet, accounts=[self], **constraints)
|
2019-03-25 23:30:43 +01:00
|
|
|
|
2018-10-10 02:46:41 +02:00
|
|
|
def get_channels(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_channels(wallet=self.wallet, accounts=[self], **constraints)
|
2018-10-10 02:46:41 +02:00
|
|
|
|
|
|
|
def get_channel_count(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_channel_count(wallet=self.wallet, accounts=[self], **constraints)
|
2018-11-21 00:21:53 +01:00
|
|
|
|
2019-11-13 23:50:35 +01:00
|
|
|
def get_collections(self, **constraints):
|
|
|
|
return self.ledger.get_collections(wallet=self.wallet, accounts=[self], **constraints)
|
|
|
|
|
|
|
|
def get_collection_count(self, **constraints):
|
|
|
|
return self.ledger.get_collection_count(wallet=self.wallet, accounts=[self], **constraints)
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
def get_supports(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_supports(wallet=self.wallet, accounts=[self], **constraints)
|
2019-03-24 21:55:04 +01:00
|
|
|
|
|
|
|
def get_support_count(self, **constraints):
|
2019-09-20 06:05:37 +02:00
|
|
|
return self.ledger.get_support_count(wallet=self.wallet, accounts=[self], **constraints)
|
2019-03-24 21:55:04 +01:00
|
|
|
|
2019-07-30 05:34:12 +02:00
|
|
|
def get_support_summary(self):
|
|
|
|
return self.ledger.db.get_supports_summary(account_id=self.id)
|
|
|
|
|
2019-01-04 08:49:29 +01:00
|
|
|
async def release_all_outputs(self):
|
|
|
|
await self.ledger.db.release_all_outputs(self)
|