2018-07-12 18:14:47 +02:00
|
|
|
import logging
|
|
|
|
from binascii import hexlify, unhexlify
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from torba.baseaccount import BaseAccount
|
|
|
|
|
2018-06-26 23:27:24 +02:00
|
|
|
from lbryschema.claim import ClaimDict
|
|
|
|
from lbryschema.signer import SECP256k1, get_signer
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
from .transaction import Transaction
|
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
|
|
|
|
|
|
|
def generate_certificate():
|
|
|
|
secp256k1_private_key = get_signer(SECP256k1).generate().private_key.to_pem()
|
|
|
|
return ClaimDict.generate_certificate(secp256k1_private_key, curve=SECP256k1), secp256k1_private_key
|
|
|
|
|
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
def get_certificate_lookup(tx_or_hash, nout):
|
|
|
|
if isinstance(tx_or_hash, Transaction):
|
2018-07-15 07:20:44 +02:00
|
|
|
return '{}:{}'.format(tx_or_hash.id, nout)
|
2018-07-12 05:18:59 +02:00
|
|
|
else:
|
2018-07-16 02:53:18 +02:00
|
|
|
return '{}:{}'.format(tx_or_hash, nout)
|
2018-07-12 05:18:59 +02:00
|
|
|
|
|
|
|
|
2018-06-26 23:27:24 +02:00
|
|
|
class Account(BaseAccount):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-05 04:16:02 +02:00
|
|
|
super(Account, self).__init__(*args, **kwargs)
|
2018-06-26 23:27:24 +02:00
|
|
|
self.certificates = {}
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-12 18:14:47 +02:00
|
|
|
def add_certificate_private_key(self, tx_or_hash, nout, private_key):
|
|
|
|
lookup_key = get_certificate_lookup(tx_or_hash, nout)
|
2018-07-12 05:18:59 +02:00
|
|
|
assert lookup_key not in self.certificates, 'Trying to add a duplicate certificate.'
|
|
|
|
self.certificates[lookup_key] = private_key
|
2018-07-05 04:16:02 +02:00
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
def get_certificate_private_key(self, tx_or_hash, nout):
|
|
|
|
return self.certificates.get(get_certificate_lookup(tx_or_hash, nout))
|
2018-07-09 15:55:07 +02:00
|
|
|
|
2018-07-12 05:18:59 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def maybe_migrate_certificates(self):
|
2018-07-12 18:14:47 +02:00
|
|
|
failed, succeded, total = 0, 0, 0
|
2018-07-12 07:47:34 +02:00
|
|
|
for maybe_claim_id in self.certificates.keys():
|
2018-07-12 18:14:47 +02:00
|
|
|
total += 1
|
2018-07-12 07:47:34 +02:00
|
|
|
if ':' not in maybe_claim_id:
|
|
|
|
claims = yield self.ledger.network.get_claims_by_ids(maybe_claim_id)
|
2018-07-12 18:14:47 +02:00
|
|
|
claim = claims[maybe_claim_id]
|
|
|
|
txhash = unhexlify(claim['txid'])[::-1]
|
|
|
|
tx = yield self.ledger.get_transaction(txhash)
|
|
|
|
if tx is not None:
|
|
|
|
txo = tx.outputs[claim['nout']]
|
|
|
|
assert txo.script.is_claim_involved,\
|
|
|
|
"Certificate with claim_id {} doesn't point to a valid transaction."\
|
|
|
|
.format(maybe_claim_id)
|
|
|
|
tx_nout = '{txid}:{nout}'.format(**claim)
|
|
|
|
self.certificates[tx_nout] = self.certificates[maybe_claim_id]
|
|
|
|
del self.certificates[maybe_claim_id]
|
|
|
|
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
|
|
|
)
|
|
|
|
succeded += 1
|
|
|
|
else:
|
|
|
|
log.warning(
|
2018-07-15 07:20:44 +02:00
|
|
|
"Failed to migrate claim '%s', it's not associated with any of your addresses.",
|
|
|
|
maybe_claim_id
|
2018-07-12 18:14:47 +02:00
|
|
|
)
|
|
|
|
failed += 1
|
2018-07-15 07:20:44 +02:00
|
|
|
log.info('Checked: %s, Converted: %s, Failed: %s', total, succeded, failed)
|
2018-07-12 05:57:22 +02:00
|
|
|
|
2018-07-15 05:02:19 +02:00
|
|
|
def get_balance(self, confirmations=6, include_claims=False):
|
2018-07-09 15:55:07 +02:00
|
|
|
if include_claims:
|
2018-07-15 05:02:19 +02:00
|
|
|
return super(Account, self).get_balance(confirmations)
|
2018-07-09 15:55:07 +02:00
|
|
|
else:
|
|
|
|
return super(Account, self).get_balance(
|
2018-07-15 05:02:19 +02:00
|
|
|
confirmations, is_claim=0, is_update=0, is_support=0
|
2018-07-09 15:55:07 +02:00
|
|
|
)
|
2018-07-09 23:04:59 +02:00
|
|
|
|
|
|
|
def get_unspent_outputs(self, include_claims=False):
|
|
|
|
if include_claims:
|
|
|
|
return super(Account, self).get_unspent_outputs()
|
|
|
|
else:
|
|
|
|
return super(Account, self).get_unspent_outputs(
|
|
|
|
is_claim=0, is_update=0, is_support=0
|
|
|
|
)
|
2018-07-12 07:47:34 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, ledger, d): # type: (torba.baseledger.BaseLedger, Dict) -> BaseAccount
|
|
|
|
account = super(Account, cls).from_dict(ledger, d)
|
|
|
|
account.certificates = d['certificates']
|
|
|
|
return account
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
d = super(Account, self).to_dict()
|
|
|
|
d['certificates'] = self.certificates
|
|
|
|
return d
|