Changed txo.private_key to be an object

This commit is contained in:
hackrush 2019-05-23 19:08:46 +05:30 committed by Lex Berezhny
parent 37665f2df7
commit 7e1f8a56d5
5 changed files with 19 additions and 26 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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]

View file

@ -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