From 7e1f8a56d5d859a61e0d5d17b37b862f5b65aaa6 Mon Sep 17 00:00:00 2001 From: hackrush Date: Thu, 23 May 2019 19:08:46 +0530 Subject: [PATCH] Changed txo.private_key to be an object --- lbrynet/extras/daemon/Daemon.py | 10 ++-------- lbrynet/wallet/account.py | 19 +++++++++++-------- lbrynet/wallet/transaction.py | 11 ++++------- tests/integration/test_claim_commands.py | 3 +-- tests/integration/test_resolve_command.py | 2 +- 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lbrynet/extras/daemon/Daemon.py b/lbrynet/extras/daemon/Daemon.py index 6949b4eaa..462c63641 100644 --- a/lbrynet/extras/daemon/Daemon.py +++ b/lbrynet/extras/daemon/Daemon.py @@ -1900,10 +1900,7 @@ class Daemon(metaclass=JSONRPCServerType): if not preview: await tx.sign([account]) await account.ledger.broadcast(tx) - channel_pubkey_hash = account.ledger.public_key_to_address( - txo.claim.channel.public_key_bytes - ) - account.add_channel_private_key(txo.claim_name, channel_pubkey_hash, txo.private_key) + account.add_channel_private_key(txo.private_key) self.default_wallet.save() await self.storage.save_claims([self._old_get_temp_claim_info( tx, txo, claim_address, claim, name, dewies_to_lbc(amount) @@ -2041,10 +2038,7 @@ class Daemon(metaclass=JSONRPCServerType): if not preview: await tx.sign([account]) await account.ledger.broadcast(tx) - channel_pubkey_hash = account.ledger.public_key_to_address( - new_txo.claim.channel.public_key_bytes - ) - account.add_channel_private_key(new_txo.claim_name, channel_pubkey_hash, new_txo.private_key) + account.add_channel_private_key(new_txo.private_key) self.default_wallet.save() await self.storage.save_claims([self._old_get_temp_claim_info( tx, new_txo, claim_address, new_txo.claim, new_txo.claim_name, dewies_to_lbc(amount) diff --git a/lbrynet/wallet/account.py b/lbrynet/wallet/account.py index b50575725..a0e337bce 100644 --- a/lbrynet/wallet/account.py +++ b/lbrynet/wallet/account.py @@ -1,4 +1,3 @@ -import hashlib import json import logging from hashlib import sha256 @@ -38,14 +37,14 @@ class Account(BaseAccount): super().apply(d) self.channel_keys.update(d.get('certificates', {})) - def add_channel_private_key(self, channel_name, channel_pubkey_hash, private_key): - if channel_pubkey_hash not in self.channel_keys: - self.channel_keys[channel_pubkey_hash] = private_key - else: - log.info("Public-Private key mapping for the channel %s already exists. Skipping...", channel_name) + 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() def get_channel_private_key(self, channel_pubkey_hash): - return self.channel_keys.get(channel_pubkey_hash) + private_key_pem = self.channel_keys.get(channel_pubkey_hash) + return self._get_private_key_object_from_pem(private_key_pem) async def maybe_migrate_certificates(self): if not self.channel_keys: @@ -170,6 +169,10 @@ class Account(BaseAccount): await self.ledger.db.release_all_outputs(self) def _get_pubkey_address_from_private_key_pem(self, private_key_pem): - private_key = ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=hashlib.sha256) + 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/transaction.py b/lbrynet/wallet/transaction.py index 7afc6b23f..b1430bbe3 100644 --- a/lbrynet/wallet/transaction.py +++ b/lbrynet/wallet/transaction.py @@ -143,8 +143,7 @@ class Output(BaseOutput): self.claim.signing_channel_hash, self.claim.to_message_bytes() ])) - private_key = ecdsa.SigningKey.from_pem(channel.private_key, hashfunc=hashlib.sha256) - self.claim.signature = private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256) + self.claim.signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256) self.script.generate() def clear_signature(self): @@ -152,14 +151,12 @@ class Output(BaseOutput): self.claim.clear_signature() def generate_channel_private_key(self): - private_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) - self.private_key = private_key.to_pem().decode() - self.claim.channel.public_key_bytes = private_key.get_verifying_key().to_der() + self.private_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) + self.claim.channel.public_key_bytes = self.private_key.get_verifying_key().to_der() self.script.generate() return self.private_key - def is_channel_private_key(self, private_key_pem): - private_key = ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=hashlib.sha256) + def is_channel_private_key(self, private_key): return self.claim.channel.public_key_bytes == private_key.get_verifying_key().to_der() @classmethod diff --git a/tests/integration/test_claim_commands.py b/tests/integration/test_claim_commands.py index 9e323bd11..285473c05 100644 --- a/tests/integration/test_claim_commands.py +++ b/tests/integration/test_claim_commands.py @@ -300,8 +300,7 @@ class ChannelCommands(CommandTestCase): # 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', channel_pubkey_address_hash, - self.account.channel_keys[channel_pubkey_address_hash]) + account2.add_channel_private_key('@featurechannel', self.account.channel_keys[channel_pubkey_address_hash]) # 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 3bf3c0759..89edaa211 100644 --- a/tests/integration/test_resolve_command.py +++ b/tests/integration/test_resolve_command.py @@ -333,7 +333,7 @@ def generate_signed_legacy(address: bytes, output: Output): claim.SerializeToString(), output.claim_hash[::-1] ])) - private_key = ecdsa.SigningKey.from_pem(output.private_key, hashfunc=hashlib.sha256) + private_key = output.private_key signature = private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256) claim.publisherSignature.version = 1 claim.publisherSignature.signatureType = 1