diff --git a/squeaknode/core/profiles.py b/squeaknode/core/profiles.py index 84fa8034..1afb5c5c 100644 --- a/squeaknode/core/profiles.py +++ b/squeaknode/core/profiles.py @@ -19,6 +19,8 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from typing import Optional + from bitcoin.base58 import Base58ChecksumError from bitcoin.wallet import CBitcoinAddressError from squeak.core.signing import CSigningKey @@ -27,9 +29,12 @@ from squeak.core.signing import CSqueakAddress from squeaknode.core.squeak_profile import SqueakProfile -def create_signing_profile(profile_name: str) -> SqueakProfile: +def create_signing_profile(profile_name: str, private_key: Optional[str] = None) -> SqueakProfile: validate_profile_name(profile_name) - signing_key = CSigningKey.generate() + if private_key is None: + signing_key = CSigningKey.generate() + else: + signing_key = CSigningKey(private_key) verifying_key = signing_key.get_verifying_key() address = CSqueakAddress.from_verifying_key(verifying_key) signing_key_str = str(signing_key) @@ -42,20 +47,6 @@ def create_signing_profile(profile_name: str) -> SqueakProfile: ) -def import_signing_profile(profile_name: str, private_key: str) -> SqueakProfile: - validate_profile_name(profile_name) - signing_key = CSigningKey(private_key) - verifying_key = signing_key.get_verifying_key() - address = CSqueakAddress.from_verifying_key(verifying_key) - signing_key_str = str(signing_key) - signing_key_bytes = signing_key_str.encode() - return SqueakProfile( - profile_name=profile_name, - private_key=signing_key_bytes, - address=str(address), - ) - - def create_contact_profile(profile_name: str, squeak_address: str) -> SqueakProfile: validate_profile_name(profile_name) try: diff --git a/squeaknode/node/squeak_controller.py b/squeaknode/node/squeak_controller.py index 2c1fa0bc..0d072ceb 100644 --- a/squeaknode/node/squeak_controller.py +++ b/squeaknode/node/squeak_controller.py @@ -45,7 +45,6 @@ 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 import_signing_profile from squeaknode.core.received_offer import ReceivedOffer from squeaknode.core.received_payment import ReceivedPayment from squeaknode.core.received_payment_summary import ReceivedPaymentSummary @@ -257,7 +256,7 @@ class SqueakController: return profile_id def import_signing_profile(self, profile_name: str, private_key: str) -> int: - squeak_profile = import_signing_profile( + squeak_profile = create_signing_profile( profile_name, private_key, ) diff --git a/tests/core/test_profiles.py b/tests/core/test_profiles.py index 8940dc2a..e8a9f1c9 100644 --- a/tests/core/test_profiles.py +++ b/tests/core/test_profiles.py @@ -23,7 +23,6 @@ import pytest from squeaknode.core.profiles import create_contact_profile from squeaknode.core.profiles import create_signing_profile -from squeaknode.core.profiles import import_signing_profile @pytest.fixture @@ -54,7 +53,7 @@ def test_create_signing_profile_empty_name(): def test_import_signing_profile(profile_name, private_key, address): - profile = import_signing_profile(profile_name, private_key) + profile = create_signing_profile(profile_name, private_key) assert profile.profile_name == profile_name assert profile.private_key == private_key.encode() diff --git a/tests/db/test_squeak_db.py b/tests/db/test_squeak_db.py index 9a915dcb..6e7c938d 100644 --- a/tests/db/test_squeak_db.py +++ b/tests/db/test_squeak_db.py @@ -24,6 +24,8 @@ from sqlalchemy import create_engine from squeaknode.core.squeaks import get_hash from squeaknode.db.squeak_db import SqueakDb +from tests.utils import gen_contact_profile +from tests.utils import gen_signing_profile from tests.utils import gen_squeak_with_block_header @@ -59,6 +61,32 @@ def block_header(squeak_with_block_header): yield block_header +@pytest.fixture +def signing_profile_name(): + yield "fake_signing_profile_name" + + +@pytest.fixture +def contact_profile_name(): + yield "fake_contact_profile_name" + + +@pytest.fixture +def signing_profile(signing_profile_name, signing_key): + yield gen_signing_profile( + signing_profile_name, + str(signing_key), + ) + + +@pytest.fixture +def contact_profile(contact_profile_name, address): + yield gen_contact_profile( + contact_profile_name, + str(address), + ) + + def test_insert_get_squeak(squeak_db, squeak, block_header): squeak_hash = squeak_db.insert_squeak(squeak, block_header) retrieved_squeak = squeak_db.get_squeak(squeak_hash) @@ -126,3 +154,56 @@ def test_get_missing_squeak_secret_key( retrieved_secret_key = squeak_db.get_squeak_secret_key(squeak_hash) assert retrieved_secret_key is None + + +def test_get_timeline_squeak_entries( + squeak_db, + signing_key, + signing_profile, + contact_profile, +): + squeak_1, header_1 = gen_squeak_with_block_header(signing_key, 5001) + squeak_2, header_2 = gen_squeak_with_block_header(signing_key, 5002) + squeak_3, header_3 = gen_squeak_with_block_header(signing_key, 5003) + squeak_4, header_4 = gen_squeak_with_block_header(signing_key, 5004) + squeak_5, header_5 = gen_squeak_with_block_header(signing_key, 5005) + + squeak_hash_1 = squeak_db.insert_squeak(squeak_1, header_1) + squeak_hash_2 = squeak_db.insert_squeak(squeak_2, header_2) + squeak_hash_3 = squeak_db.insert_squeak(squeak_3, header_3) + squeak_hash_4 = squeak_db.insert_squeak(squeak_4, header_4) + squeak_hash_5 = squeak_db.insert_squeak(squeak_5, header_5) + + assert squeak_hash_1 is not None + assert squeak_hash_2 is not None + assert squeak_hash_3 is not None + assert squeak_hash_4 is not None + assert squeak_hash_5 is not None + + # Insert the contact profile and ensure that it is followed. + profile_id = squeak_db.insert_profile(contact_profile) + squeak_db.set_profile_following(profile_id, True) + + # TODO: get_timeline_squeak_entries only returns followed squeaks. + timeline_squeak_entries = squeak_db.get_timeline_squeak_entries( + limit=2, + last_entry=None, + ) + + assert len(timeline_squeak_entries) == 2 + + +def test_get_signing_profile(squeak_db, signing_key, signing_profile): + profile_id = squeak_db.insert_profile(signing_profile) + retrieved_profile = squeak_db.get_profile(profile_id) + + assert retrieved_profile.profile_name == signing_profile.profile_name + assert retrieved_profile.private_key == signing_profile.private_key + + +def test_get_contact_profile(squeak_db, address, contact_profile): + profile_id = squeak_db.insert_profile(contact_profile) + retrieved_profile = squeak_db.get_profile(profile_id) + + assert retrieved_profile.profile_name == contact_profile.profile_name + assert retrieved_profile.address == contact_profile.address diff --git a/tests/utils.py b/tests/utils.py index c0581af9..ca610394 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -26,6 +26,8 @@ from bitcoin.core import CBlockHeader from squeak.core.signing import CSigningKey from squeak.core.signing import CSqueakAddress +from squeaknode.core.profiles import create_contact_profile +from squeaknode.core.profiles import create_signing_profile from squeaknode.core.squeaks import HASH_LENGTH from squeaknode.core.squeaks import make_squeak_with_block @@ -83,3 +85,17 @@ def gen_squeak_with_block_header(signing_key, block_height, replyto_hash=None): block_height=block_height, ) return squeak, block_info + + +def gen_signing_profile(profile_name, signing_key): + return create_signing_profile( + profile_name, + signing_key, + ) + + +def gen_contact_profile(profile_name, address): + return create_contact_profile( + profile_name, + address, + )