Add function to get private key from profile (#1619)

This commit is contained in:
Jonathan Zernik 2021-10-16 19:35:24 -07:00 committed by GitHub
parent 0e85b37f02
commit fd6ad232e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 6 deletions

View file

@ -68,3 +68,11 @@ def validate_profile_name(profile_name: str) -> None:
raise Exception(
"Profile name cannot be empty.",
)
def get_profile_private_key(profile: SqueakProfile) -> bytes:
if profile.private_key is None:
raise Exception("Profile: {} does not have a private key.".format(
profile,
))
return profile.private_key

View file

@ -45,6 +45,7 @@ from squeaknode.core.peer_address import PeerAddress
from squeaknode.core.peers import create_saved_peer
from squeaknode.core.profiles import create_contact_profile
from squeaknode.core.profiles import create_signing_profile
from squeaknode.core.profiles import get_profile_private_key
from squeaknode.core.received_offer import ReceivedOffer
from squeaknode.core.received_payment import ReceivedPayment
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
@ -320,13 +321,9 @@ class SqueakController:
profile = self.get_squeak_profile(profile_id)
if profile is None:
raise Exception("Profile with id: {} does not exist.".format(
profile_id
profile_id,
))
if profile.private_key is None:
raise Exception("Profile with id: {} does not have a private key.".format(
profile_id
))
return profile.private_key
return get_profile_private_key(profile)
def create_peer(self, peer_name: str, peer_address: PeerAddress):
squeak_peer = create_saved_peer(

View file

@ -54,6 +54,11 @@ def signing_key():
yield CSigningKey.generate()
@pytest.fixture
def signing_key_bytes(signing_key):
yield str(signing_key).encode()
@pytest.fixture
def address(signing_key):
verifying_key = signing_key.get_verifying_key()

View file

@ -23,6 +23,7 @@ import pytest
from squeaknode.core.profiles import create_contact_profile
from squeaknode.core.profiles import create_signing_profile
from squeaknode.core.profiles import get_profile_private_key
@pytest.fixture
@ -71,3 +72,15 @@ def test_create_contact_profile_invalid_address(profile_name, invalid_address_st
with pytest.raises(Exception) as excinfo:
create_contact_profile(profile_name, invalid_address_str)
assert "Invalid squeak address" in str(excinfo.value)
def test_get_profile_private_key(signing_profile, signing_key_bytes):
private_key = get_profile_private_key(signing_profile)
assert private_key == signing_key_bytes
def test_get_profile_private_key_missing(contact_profile):
with pytest.raises(Exception) as excinfo:
get_profile_private_key(contact_profile)
assert "does not have a private key" in str(excinfo.value)