forked from LBRYCommunity/lbry-sdk
cleaned up with passing tests
This commit is contained in:
parent
7e1f8a56d5
commit
a357208a77
4 changed files with 14 additions and 60 deletions
|
@ -42,57 +42,22 @@ class Account(BaseAccount):
|
|||
channel_pubkey_hash = self.ledger.public_key_to_address(public_key_bytes)
|
||||
self.channel_keys[channel_pubkey_hash] = private_key.to_pem().decode()
|
||||
|
||||
def get_channel_private_key(self, channel_pubkey_hash):
|
||||
def get_channel_private_key(self, public_key_bytes):
|
||||
channel_pubkey_hash = self.ledger.public_key_to_address(public_key_bytes)
|
||||
private_key_pem = self.channel_keys.get(channel_pubkey_hash)
|
||||
return self._get_private_key_object_from_pem(private_key_pem)
|
||||
if private_key_pem:
|
||||
return ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=sha256)
|
||||
|
||||
async def maybe_migrate_certificates(self):
|
||||
if not self.channel_keys:
|
||||
return
|
||||
|
||||
results = {
|
||||
'total': 0,
|
||||
'consolidated': 0,
|
||||
'migrate-success': 0,
|
||||
'migrate-failed': 0,
|
||||
'previous-success': 0,
|
||||
'previous-corrupted': 0
|
||||
}
|
||||
|
||||
new_channel_keys = {}
|
||||
|
||||
for maybe_outpoint in self.channel_keys:
|
||||
results['total'] += 1
|
||||
if ':' in maybe_outpoint:
|
||||
try:
|
||||
private_key_pem = self.channel_keys[maybe_outpoint]
|
||||
pubkey_hash = self._get_pubkey_address_from_private_key_pem(private_key_pem)
|
||||
|
||||
if pubkey_hash not in new_channel_keys and pubkey_hash not in self.channel_keys:
|
||||
new_channel_keys[pubkey_hash] = private_key_pem
|
||||
results['migrate-success'] += 1
|
||||
else:
|
||||
results['consolidated'] += 1
|
||||
except Exception as e:
|
||||
results['migrate-failed'] += 1
|
||||
log.warning("Failed to migrate certificate for %s, incorrect private key: %s",
|
||||
maybe_outpoint, str(e))
|
||||
else:
|
||||
try:
|
||||
pubkey_hash = self._get_pubkey_address_from_private_key_pem(self.channel_keys[maybe_outpoint])
|
||||
if pubkey_hash == maybe_outpoint:
|
||||
results['previous-success'] += 1
|
||||
else:
|
||||
results['previous-corrupted'] += 1
|
||||
except Exception as e:
|
||||
log.warning("Corrupt public:private key-pair: %s", str(e))
|
||||
results['previous-corrupted'] += 1
|
||||
|
||||
self.channel_keys = new_channel_keys
|
||||
|
||||
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
|
||||
self.wallet.save()
|
||||
log.info('verifying and possibly migrating certificates:')
|
||||
log.info(json.dumps(results, indent=2))
|
||||
|
||||
async def save_max_gap(self):
|
||||
if issubclass(self.address_generator, HierarchicalDeterministic):
|
||||
|
@ -167,12 +132,3 @@ class Account(BaseAccount):
|
|||
|
||||
async def release_all_outputs(self):
|
||||
await self.ledger.db.release_all_outputs(self)
|
||||
|
||||
def _get_pubkey_address_from_private_key_pem(self, private_key_pem):
|
||||
private_key = self._get_private_key_object_from_pem(private_key_pem)
|
||||
public_key_der = private_key.get_verifying_key().to_der()
|
||||
return self.ledger.public_key_to_address(public_key_der)
|
||||
|
||||
@staticmethod
|
||||
def _get_private_key_object_from_pem(private_key_pem):
|
||||
return ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=sha256)
|
||||
|
|
|
@ -63,10 +63,9 @@ class WalletDatabase(BaseDatabase):
|
|||
if txo.claim.is_signed:
|
||||
channel_ids.add(txo.claim.signing_channel_id)
|
||||
if txo.claim.is_channel and my_account is not None:
|
||||
channel_pubkey_hash = my_account.ledger.public_key_to_address(
|
||||
txo.private_key = my_account.get_channel_private_key(
|
||||
txo.claim.channel.public_key_bytes
|
||||
)
|
||||
txo.private_key = my_account.get_channel_private_key(channel_pubkey_hash)
|
||||
|
||||
if channel_ids:
|
||||
channels = {
|
||||
|
|
|
@ -299,8 +299,8 @@ class ChannelCommands(CommandTestCase):
|
|||
self.assertIsNone(txo.private_key)
|
||||
|
||||
# send the private key too
|
||||
channel_pubkey_address_hash = self.account.ledger.public_key_to_address(unhexlify(channel['public_key']))
|
||||
account2.add_channel_private_key('@featurechannel', self.account.channel_keys[channel_pubkey_address_hash])
|
||||
channel_public_key = self.account.get_channel_private_key(unhexlify(channel['public_key']))
|
||||
account2.add_channel_private_key(channel_public_key)
|
||||
|
||||
# now should have private key
|
||||
txo = (await account2.get_channels())[0]
|
||||
|
|
|
@ -333,8 +333,7 @@ def generate_signed_legacy(address: bytes, output: Output):
|
|||
claim.SerializeToString(),
|
||||
output.claim_hash[::-1]
|
||||
]))
|
||||
private_key = output.private_key
|
||||
signature = private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
|
||||
signature = output.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
|
||||
claim.publisherSignature.version = 1
|
||||
claim.publisherSignature.signatureType = 1
|
||||
claim.publisherSignature.signature = signature
|
||||
|
|
Loading…
Reference in a new issue