mirror of
https://github.com/LBRYFoundation/lbry-sdk.git
synced 2025-08-23 17:27:25 +00:00
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)
|
channel_pubkey_hash = self.ledger.public_key_to_address(public_key_bytes)
|
||||||
self.channel_keys[channel_pubkey_hash] = private_key.to_pem().decode()
|
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)
|
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):
|
async def maybe_migrate_certificates(self):
|
||||||
if not self.channel_keys:
|
if not self.channel_keys:
|
||||||
return
|
return
|
||||||
|
channel_keys = {}
|
||||||
results = {
|
for private_key_pem in self.channel_keys.values():
|
||||||
'total': 0,
|
private_key = ecdsa.SigningKey.from_pem(private_key_pem, hashfunc=sha256)
|
||||||
'consolidated': 0,
|
public_key_der = private_key.get_verifying_key().to_der()
|
||||||
'migrate-success': 0,
|
channel_keys[self.ledger.public_key_to_address(public_key_der)] = private_key_pem
|
||||||
'migrate-failed': 0,
|
self.channel_keys = channel_keys
|
||||||
'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
|
|
||||||
|
|
||||||
self.wallet.save()
|
self.wallet.save()
|
||||||
log.info('verifying and possibly migrating certificates:')
|
|
||||||
log.info(json.dumps(results, indent=2))
|
|
||||||
|
|
||||||
async def save_max_gap(self):
|
async def save_max_gap(self):
|
||||||
if issubclass(self.address_generator, HierarchicalDeterministic):
|
if issubclass(self.address_generator, HierarchicalDeterministic):
|
||||||
|
@ -167,12 +132,3 @@ class Account(BaseAccount):
|
||||||
|
|
||||||
async def release_all_outputs(self):
|
async def release_all_outputs(self):
|
||||||
await self.ledger.db.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:
|
if txo.claim.is_signed:
|
||||||
channel_ids.add(txo.claim.signing_channel_id)
|
channel_ids.add(txo.claim.signing_channel_id)
|
||||||
if txo.claim.is_channel and my_account is not None:
|
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.claim.channel.public_key_bytes
|
||||||
)
|
)
|
||||||
txo.private_key = my_account.get_channel_private_key(channel_pubkey_hash)
|
|
||||||
|
|
||||||
if channel_ids:
|
if channel_ids:
|
||||||
channels = {
|
channels = {
|
||||||
|
|
|
@ -299,8 +299,8 @@ class ChannelCommands(CommandTestCase):
|
||||||
self.assertIsNone(txo.private_key)
|
self.assertIsNone(txo.private_key)
|
||||||
|
|
||||||
# send the private key too
|
# send the private key too
|
||||||
channel_pubkey_address_hash = self.account.ledger.public_key_to_address(unhexlify(channel['public_key']))
|
channel_public_key = self.account.get_channel_private_key(unhexlify(channel['public_key']))
|
||||||
account2.add_channel_private_key('@featurechannel', self.account.channel_keys[channel_pubkey_address_hash])
|
account2.add_channel_private_key(channel_public_key)
|
||||||
|
|
||||||
# now should have private key
|
# now should have private key
|
||||||
txo = (await account2.get_channels())[0]
|
txo = (await account2.get_channels())[0]
|
||||||
|
|
|
@ -333,8 +333,7 @@ def generate_signed_legacy(address: bytes, output: Output):
|
||||||
claim.SerializeToString(),
|
claim.SerializeToString(),
|
||||||
output.claim_hash[::-1]
|
output.claim_hash[::-1]
|
||||||
]))
|
]))
|
||||||
private_key = output.private_key
|
signature = output.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
|
||||||
signature = private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
|
|
||||||
claim.publisherSignature.version = 1
|
claim.publisherSignature.version = 1
|
||||||
claim.publisherSignature.signatureType = 1
|
claim.publisherSignature.signatureType = 1
|
||||||
claim.publisherSignature.signature = signature
|
claim.publisherSignature.signature = signature
|
||||||
|
|
Loading…
Add table
Reference in a new issue