2018-08-09 02:41:29 +02:00
|
|
|
import json
|
2018-07-12 18:14:47 +02:00
|
|
|
import logging
|
2018-11-30 18:46:53 +01:00
|
|
|
import binascii
|
2019-03-24 21:55:04 +01:00
|
|
|
import typing
|
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
|
|
|
|
2018-11-04 07:24:41 +01:00
|
|
|
from torba.client.baseaccount import BaseAccount
|
|
|
|
from torba.client.basetransaction import TXORef
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
from lbrynet.wallet import ledger
|
|
|
|
|
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):
|
2019-03-24 21:55:04 +01:00
|
|
|
ledger: 'ledger.MainNetLedger'
|
2018-06-26 23:27:24 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
h = sha256(json.dumps(self.to_dict(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-03-12 20:31:54 +01:00
|
|
|
def apply(self, d: dict):
|
|
|
|
super().apply(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-03-24 21:55:04 +01:00
|
|
|
def add_channel_private_key(self, ref: TXORef, private_key):
|
|
|
|
assert ref.id not in self.channel_keys, 'Trying to add a duplicate channel private key.'
|
|
|
|
self.channel_keys[ref.id] = private_key
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
def get_channel_private_key(self, ref: TXORef):
|
|
|
|
return self.channel_keys.get(ref.id)
|
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
|
|
|
|
|
|
|
|
addresses = {}
|
|
|
|
results = {
|
|
|
|
'total': 0,
|
|
|
|
'not-a-claim-tx': 0,
|
|
|
|
'migrate-success': 0,
|
|
|
|
'migrate-failed': 0,
|
|
|
|
'previous-success': 0,
|
|
|
|
'previous-corrupted': 0
|
|
|
|
}
|
2018-11-30 18:46:53 +01:00
|
|
|
double_hex_encoded_to_pop = []
|
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
for maybe_claim_id in list(self.channel_keys):
|
2018-11-30 18:46:53 +01:00
|
|
|
if ':' not in maybe_claim_id:
|
|
|
|
try:
|
|
|
|
validate_claim_id(maybe_claim_id)
|
|
|
|
continue
|
|
|
|
except Exception:
|
|
|
|
try:
|
|
|
|
maybe_claim_id_bytes = maybe_claim_id
|
|
|
|
if isinstance(maybe_claim_id_bytes, str):
|
|
|
|
maybe_claim_id_bytes = maybe_claim_id_bytes.encode()
|
|
|
|
decoded_double_hex = binascii.unhexlify(maybe_claim_id_bytes).decode()
|
|
|
|
validate_claim_id(decoded_double_hex)
|
2019-03-24 21:55:04 +01:00
|
|
|
if decoded_double_hex in self.channel_keys:
|
2018-11-30 18:46:53 +01:00
|
|
|
log.warning("don't know how to migrate certificate %s", decoded_double_hex)
|
|
|
|
else:
|
|
|
|
log.info("claim id was double hex encoded, fixing it")
|
|
|
|
double_hex_encoded_to_pop.append((maybe_claim_id, decoded_double_hex))
|
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for double_encoded_claim_id, correct_claim_id in double_hex_encoded_to_pop:
|
2019-03-24 21:55:04 +01:00
|
|
|
self.channel_keys[correct_claim_id] = self.channel_keys.pop(double_encoded_claim_id)
|
2018-08-09 02:41:29 +02:00
|
|
|
|
2019-03-24 21:55:04 +01:00
|
|
|
for maybe_claim_id in list(self.channel_keys):
|
2018-08-09 02:41:29 +02:00
|
|
|
results['total'] += 1
|
2018-07-12 07:47:34 +02:00
|
|
|
if ':' not in maybe_claim_id:
|
2018-11-30 00:56:55 +01:00
|
|
|
try:
|
|
|
|
validate_claim_id(maybe_claim_id)
|
|
|
|
except Exception as e:
|
|
|
|
log.warning(
|
|
|
|
"Failed to migrate claim '%s': %s",
|
|
|
|
maybe_claim_id, str(e)
|
|
|
|
)
|
|
|
|
results['migrate-failed'] += 1
|
|
|
|
continue
|
2018-10-15 23:16:43 +02:00
|
|
|
claims = await self.ledger.network.get_claims_by_ids(maybe_claim_id)
|
2018-08-27 06:46:42 +02:00
|
|
|
if maybe_claim_id not in claims:
|
|
|
|
log.warning(
|
|
|
|
"Failed to migrate claim '%s', server did not return any claim information.",
|
|
|
|
maybe_claim_id
|
|
|
|
)
|
|
|
|
results['migrate-failed'] += 1
|
|
|
|
continue
|
2018-07-12 18:14:47 +02:00
|
|
|
claim = claims[maybe_claim_id]
|
2018-08-17 16:35:56 +02:00
|
|
|
tx = None
|
|
|
|
if claim:
|
2018-10-15 23:16:43 +02:00
|
|
|
tx = await self.ledger.db.get_transaction(txid=claim['txid'])
|
2018-08-17 16:35:56 +02:00
|
|
|
else:
|
|
|
|
log.warning(maybe_claim_id)
|
2018-07-12 18:14:47 +02:00
|
|
|
if tx is not None:
|
|
|
|
txo = tx.outputs[claim['nout']]
|
2018-08-09 02:41:29 +02:00
|
|
|
if not txo.script.is_claim_involved:
|
|
|
|
results['not-a-claim-tx'] += 1
|
|
|
|
raise ValueError(
|
|
|
|
"Certificate with claim_id {} doesn't point to a valid transaction."
|
|
|
|
.format(maybe_claim_id)
|
|
|
|
)
|
2018-07-12 18:14:47 +02:00
|
|
|
tx_nout = '{txid}:{nout}'.format(**claim)
|
2019-03-24 21:55:04 +01:00
|
|
|
self.channel_keys[tx_nout] = self.channel_keys[maybe_claim_id]
|
|
|
|
del self.channel_keys[maybe_claim_id]
|
2018-07-12 18:14:47 +02:00
|
|
|
log.info(
|
2018-07-15 07:20:44 +02:00
|
|
|
"Migrated certificate with claim_id '%s' ('%s') to a new look up key %s.",
|
|
|
|
maybe_claim_id, txo.script.values['claim_name'], tx_nout
|
2018-07-12 18:14:47 +02:00
|
|
|
)
|
2018-08-09 02:41:29 +02:00
|
|
|
results['migrate-success'] += 1
|
2018-07-12 18:14:47 +02:00
|
|
|
else:
|
2018-08-17 16:35:56 +02:00
|
|
|
if claim:
|
|
|
|
addresses.setdefault(claim['address'], 0)
|
|
|
|
addresses[claim['address']] += 1
|
|
|
|
log.warning(
|
|
|
|
"Failed to migrate claim '%s', it's not associated with any of your addresses.",
|
|
|
|
maybe_claim_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
log.warning(
|
|
|
|
"Failed to migrate claim '%s', it appears abandoned.",
|
|
|
|
maybe_claim_id
|
|
|
|
)
|
2018-08-09 02:41:29 +02:00
|
|
|
results['migrate-failed'] += 1
|
2018-08-04 03:39:48 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
txid, nout = maybe_claim_id.split(':')
|
2018-10-15 23:16:43 +02:00
|
|
|
tx = await self.ledger.db.get_transaction(txid=txid)
|
2018-10-18 19:00:27 +02:00
|
|
|
if not tx:
|
2018-11-06 23:41:21 +01:00
|
|
|
log.warning(
|
|
|
|
"Claim migration failed to find a transaction for outpoint %s", maybe_claim_id
|
|
|
|
)
|
2018-10-18 19:00:27 +02:00
|
|
|
results['previous-corrupted'] += 1
|
|
|
|
continue
|
2018-08-04 03:39:48 +02:00
|
|
|
if tx.outputs[int(nout)].script.is_claim_involved:
|
2018-08-09 02:41:29 +02:00
|
|
|
results['previous-success'] += 1
|
2018-08-04 03:39:48 +02:00
|
|
|
else:
|
2018-08-09 02:41:29 +02:00
|
|
|
results['previous-corrupted'] += 1
|
2018-08-04 03:39:48 +02:00
|
|
|
except Exception:
|
|
|
|
log.exception("Couldn't verify certificate with look up key: %s", maybe_claim_id)
|
2018-08-09 02:41:29 +02:00
|
|
|
results['previous-corrupted'] += 1
|
|
|
|
|
|
|
|
self.wallet.save()
|
|
|
|
log.info('verifying and possibly migrating certificates:')
|
|
|
|
log.info(json.dumps(results, indent=2))
|
|
|
|
if addresses:
|
|
|
|
log.warning('failed for addresses:')
|
|
|
|
log.warning(json.dumps(
|
|
|
|
[{'address': a, 'number of certificates': c} for a, c in addresses.items()],
|
|
|
|
indent=2
|
|
|
|
))
|
2018-07-12 05:57:22 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def save_max_gap(self):
|
|
|
|
gap = await self.get_max_gap()
|
2018-10-10 03:39:29 +02:00
|
|
|
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-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:
|
|
|
|
constraints.update({'is_claim': 0, 'is_update': 0, 'is_support': 0})
|
2018-07-22 00:34:59 +02:00
|
|
|
return super().get_balance(confirmations, **constraints)
|
2018-07-17 05:32:37 +02:00
|
|
|
|
2018-08-06 06:28:11 +02:00
|
|
|
@classmethod
|
2019-03-25 00:45:54 +01:00
|
|
|
def get_private_key_from_seed(cls, ledger: 'ledger.MainNetLedger', 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-03-24 21:55:04 +01:00
|
|
|
def to_dict(self, include_channel_keys=True):
|
2018-07-22 00:34:59 +02:00
|
|
|
d = super().to_dict()
|
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
|
|
|
|
|
2018-09-18 23:18:02 +02:00
|
|
|
def get_claim(self, claim_id=None, txid=None, nout=None):
|
2018-10-03 18:00:21 +02:00
|
|
|
if claim_id is not None:
|
|
|
|
return self.ledger.db.get_claims(account=self, claim_id=claim_id)
|
|
|
|
elif txid is not None and nout is not None:
|
|
|
|
return self.ledger.db.get_claims(**{'account': self, 'txo.txid': txid, 'txo.position': nout})
|
2018-09-21 15:47:06 +02:00
|
|
|
|
2018-10-10 02:46:41 +02:00
|
|
|
@staticmethod
|
2019-03-24 21:55:04 +01:00
|
|
|
def constraint_spending_utxos(constraints):
|
2018-10-10 02:46:41 +02:00
|
|
|
constraints.update({'is_claim': 0, 'is_update': 0, 'is_support': 0})
|
|
|
|
|
|
|
|
def get_utxos(self, **constraints):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.constraint_spending_utxos(constraints)
|
2018-10-10 02:46:41 +02:00
|
|
|
return super().get_utxos(**constraints)
|
|
|
|
|
|
|
|
def get_utxo_count(self, **constraints):
|
2019-03-24 21:55:04 +01:00
|
|
|
self.constraint_spending_utxos(constraints)
|
2018-10-10 02:46:41 +02: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)
|
|
|
|
|
|
|
|
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-21 00:21:53 +01:00
|
|
|
|
2019-03-24 21:55:04 +01: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 08:49:29 +01:00
|
|
|
async def release_all_outputs(self):
|
|
|
|
await self.ledger.db.release_all_outputs(self)
|