2018-08-08 20:41:29 -04:00
|
|
|
import json
|
2018-07-12 12:14:47 -04:00
|
|
|
import logging
|
2019-03-11 09:52:35 -04:00
|
|
|
from hashlib import sha256
|
2019-03-18 19:34:01 -04:00
|
|
|
from string import hexdigits
|
2018-07-12 12:14:47 -04:00
|
|
|
|
2019-05-19 02:14:33 +05:30
|
|
|
import ecdsa
|
|
|
|
|
2019-05-04 17:22:32 -04:00
|
|
|
from torba.client.baseaccount import BaseAccount, HierarchicalDeterministic
|
2018-07-11 23:18:59 -04:00
|
|
|
|
2018-06-26 17:27:24 -04:00
|
|
|
|
2018-07-12 12:14:47 -04:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2018-06-26 17:27:24 -04:00
|
|
|
|
2019-03-18 19:34:01 -04: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 17:27:24 -04:00
|
|
|
class Account(BaseAccount):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-21 18:34:59 -04:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-03-24 16:55:04 -04:00
|
|
|
self.channel_keys = {}
|
2018-07-04 22:16:02 -04:00
|
|
|
|
2019-03-11 09:52:35 -04:00
|
|
|
@property
|
|
|
|
def hash(self) -> bytes:
|
|
|
|
h = sha256(json.dumps(self.to_dict(False)).encode())
|
2019-03-24 16:55:04 -04:00
|
|
|
for cert in sorted(self.channel_keys.keys()):
|
2019-03-11 09:52:35 -04:00
|
|
|
h.update(cert.encode())
|
|
|
|
return h.digest()
|
|
|
|
|
2019-03-12 15:31:54 -04:00
|
|
|
def apply(self, d: dict):
|
|
|
|
super().apply(d)
|
2019-03-24 16:55:04 -04:00
|
|
|
self.channel_keys.update(d.get('certificates', {}))
|
2019-03-12 15:31:54 -04:00
|
|
|
|
2019-05-23 19:08:46 +05:30
|
|
|
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-04 22:16:02 -04:00
|
|
|
|
2019-05-28 18:46:50 -04: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 19:08:46 +05:30
|
|
|
private_key_pem = self.channel_keys.get(channel_pubkey_hash)
|
2019-05-28 18:46:50 -04:00
|
|
|
if private_key_pem:
|
|
|
|
return ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=sha256)
|
2018-07-09 09:55:07 -04:00
|
|
|
|
2018-10-15 17:16:43 -04:00
|
|
|
async def maybe_migrate_certificates(self):
|
2019-03-24 16:55:04 -04:00
|
|
|
if not self.channel_keys:
|
2018-08-08 20:41:29 -04:00
|
|
|
return
|
2019-05-28 18:46:50 -04:00
|
|
|
channel_keys = {}
|
|
|
|
for private_key_pem in self.channel_keys.values():
|
|
|
|
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
|
|
|
|
self.channel_keys = channel_keys
|
2018-08-08 20:41:29 -04:00
|
|
|
self.wallet.save()
|
2018-07-11 23:57:22 -04:00
|
|
|
|
2018-10-15 17:16:43 -04:00
|
|
|
async def save_max_gap(self):
|
2019-05-04 17:22:32 -04:00
|
|
|
if issubclass(self.address_generator, HierarchicalDeterministic):
|
|
|
|
gap = await self.get_max_gap()
|
|
|
|
self.receiving.gap = max(20, gap['max_receiving_gap'] + 1)
|
|
|
|
self.change.gap = max(6, gap['max_change_gap'] + 1)
|
|
|
|
self.wallet.save()
|
2018-10-09 22:39:29 -03:00
|
|
|
|
2018-10-08 14:09:37 -04:00
|
|
|
def get_balance(self, confirmations=0, include_claims=False, **constraints):
|
2018-07-16 23:32:37 -04:00
|
|
|
if not include_claims:
|
|
|
|
constraints.update({'is_claim': 0, 'is_update': 0, 'is_support': 0})
|
2018-07-21 18:34:59 -04:00
|
|
|
return super().get_balance(confirmations, **constraints)
|
2018-07-16 23:32:37 -04:00
|
|
|
|
2018-08-06 00:28:11 -04:00
|
|
|
@classmethod
|
2019-03-24 22:20:17 -04:00
|
|
|
def get_private_key_from_seed(cls, ledger, seed: str, password: str):
|
2018-08-06 00:28:11 -04:00
|
|
|
return super().get_private_key_from_seed(
|
|
|
|
ledger, seed, password or 'lbryum'
|
|
|
|
)
|
|
|
|
|
2018-07-12 01:47:34 -04:00
|
|
|
@classmethod
|
2018-08-08 20:41:29 -04:00
|
|
|
def from_dict(cls, ledger, wallet, d: dict) -> 'Account':
|
|
|
|
account = super().from_dict(ledger, wallet, d)
|
2019-03-24 16:55:04 -04:00
|
|
|
account.channel_keys = d.get('certificates', {})
|
2018-07-12 01:47:34 -04:00
|
|
|
return account
|
|
|
|
|
2019-03-24 16:55:04 -04:00
|
|
|
def to_dict(self, include_channel_keys=True):
|
2018-07-21 18:34:59 -04:00
|
|
|
d = super().to_dict()
|
2019-03-24 16:55:04 -04:00
|
|
|
if include_channel_keys:
|
|
|
|
d['certificates'] = self.channel_keys
|
2018-07-12 01:47:34 -04:00
|
|
|
return d
|
2018-07-31 22:59:51 -04:00
|
|
|
|
2018-10-15 17:16:43 -04:00
|
|
|
async def get_details(self, **kwargs):
|
|
|
|
details = await super().get_details(**kwargs)
|
2019-03-24 16:55:04 -04:00
|
|
|
details['certificates'] = len(self.channel_keys)
|
2018-08-30 00:04:25 -04:00
|
|
|
return details
|
|
|
|
|
2018-10-09 20:46:41 -04:00
|
|
|
@staticmethod
|
2019-03-24 16:55:04 -04:00
|
|
|
def constraint_spending_utxos(constraints):
|
2018-10-09 20:46:41 -04:00
|
|
|
constraints.update({'is_claim': 0, 'is_update': 0, 'is_support': 0})
|
|
|
|
|
|
|
|
def get_utxos(self, **constraints):
|
2019-03-24 16:55:04 -04:00
|
|
|
self.constraint_spending_utxos(constraints)
|
2018-10-09 20:46:41 -04:00
|
|
|
return super().get_utxos(**constraints)
|
|
|
|
|
|
|
|
def get_utxo_count(self, **constraints):
|
2019-03-24 16:55:04 -04:00
|
|
|
self.constraint_spending_utxos(constraints)
|
2018-10-09 20:46:41 -04:00
|
|
|
return super().get_utxo_count(**constraints)
|
|
|
|
|
|
|
|
def get_claims(self, **constraints):
|
|
|
|
return self.ledger.db.get_claims(account=self, **constraints)
|
|
|
|
|
|
|
|
def get_claim_count(self, **constraints):
|
|
|
|
return self.ledger.db.get_claim_count(account=self, **constraints)
|
|
|
|
|
2019-03-25 18:30:43 -04:00
|
|
|
def get_streams(self, **constraints):
|
|
|
|
return self.ledger.db.get_streams(account=self, **constraints)
|
|
|
|
|
|
|
|
def get_stream_count(self, **constraints):
|
|
|
|
return self.ledger.db.get_stream_count(account=self, **constraints)
|
|
|
|
|
2018-10-09 20:46:41 -04:00
|
|
|
def get_channels(self, **constraints):
|
|
|
|
return self.ledger.db.get_channels(account=self, **constraints)
|
|
|
|
|
|
|
|
def get_channel_count(self, **constraints):
|
|
|
|
return self.ledger.db.get_channel_count(account=self, **constraints)
|
2018-11-20 18:21:53 -05:00
|
|
|
|
2019-03-24 16:55:04 -04:00
|
|
|
def get_supports(self, **constraints):
|
|
|
|
return self.ledger.db.get_supports(account=self, **constraints)
|
|
|
|
|
|
|
|
def get_support_count(self, **constraints):
|
|
|
|
return self.ledger.db.get_support_count(account=self, **constraints)
|
|
|
|
|
2019-01-04 02:49:29 -05:00
|
|
|
async def release_all_outputs(self):
|
|
|
|
await self.ledger.db.release_all_outputs(self)
|