From a357208a77fde06dc58d3ee75f936e61032ea448 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 28 May 2019 18:46:50 -0400 Subject: [PATCH] cleaned up with passing tests --- lbrynet/wallet/account.py | 64 ++++------------------- lbrynet/wallet/database.py | 3 +- tests/integration/test_claim_commands.py | 4 +- tests/integration/test_resolve_command.py | 3 +- 4 files changed, 14 insertions(+), 60 deletions(-) diff --git a/lbrynet/wallet/account.py b/lbrynet/wallet/account.py index a0e337bce..da92358bd 100644 --- a/lbrynet/wallet/account.py +++ b/lbrynet/wallet/account.py @@ -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) diff --git a/lbrynet/wallet/database.py b/lbrynet/wallet/database.py index f6a4c293a..baa41abfc 100644 --- a/lbrynet/wallet/database.py +++ b/lbrynet/wallet/database.py @@ -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 = { diff --git a/tests/integration/test_claim_commands.py b/tests/integration/test_claim_commands.py index 285473c05..cb430704e 100644 --- a/tests/integration/test_claim_commands.py +++ b/tests/integration/test_claim_commands.py @@ -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] diff --git a/tests/integration/test_resolve_command.py b/tests/integration/test_resolve_command.py index 89edaa211..6ad6fd04d 100644 --- a/tests/integration/test_resolve_command.py +++ b/tests/integration/test_resolve_command.py @@ -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