diff --git a/itests/config.ini b/itests/config.ini index 7ff8d300..b634e375 100644 --- a/itests/config.ini +++ b/itests/config.ini @@ -18,7 +18,7 @@ rpc_pass= [server] rpc_host=0.0.0.0 rpc_port=8774 -price=100 +price=1000 [admin] rpc_host=0.0.0.0 diff --git a/itests/test.py b/itests/test.py index 3d065fbf..728b8378 100644 --- a/itests/test.py +++ b/itests/test.py @@ -246,6 +246,40 @@ def run(): print("Get balance response balance: " + str(get_balance_response.balance)) assert get_balance_response.balance == 0 + # Create a new signing profile + profile_name = 'bob' + create_signing_profile_response = admin_stub.CreateSigningProfile(squeak_admin_pb2.CreateSigningProfileRequest( + profile_name=profile_name, + )) + print("Get create signing profile response: " + str(create_signing_profile_response)) + profile_id = create_signing_profile_response.profile_id + + # Get the new squeak profile + get_squeak_profile_response = admin_stub.GetSqueakProfile(squeak_admin_pb2.GetSqueakProfileRequest( + profile_id=profile_id, + )) + print("Get squeak profile response: " + str(get_squeak_profile_response)) + assert get_squeak_profile_response.squeak_profile.profile_name == profile_name + + # Create a new squeak using the new profile + make_squeak_content = 'Hello from the profile on the server!' + make_squeak_response = admin_stub.MakeSqueak(squeak_admin_pb2.MakeSqueakRequest( + profile_id=profile_id, + content=make_squeak_content, + )) + print("Get make squeak response: " + str(make_squeak_response)) + make_squeak_hash = make_squeak_response.hash + assert len(make_squeak_hash) == 32 + + # Get the new squeak from the server + get_squeak_response = server_stub.GetSqueak(squeak_server_pb2.GetSqueakRequest(hash=make_squeak_hash)) + print("Get squeak response: " + str(get_squeak_response)) + get_squeak_response_squeak = squeak_from_msg(get_squeak_response.squeak) + CheckSqueak(get_response_squeak, skipDecryptionCheck=True) + assert get_hash(get_squeak_response_squeak) == make_squeak_hash + print("Squeak from make squeak request: " + str(get_squeak_response_squeak)) + assert get_squeak_response_squeak.GetDecryptedContentStr() == 'Hello from the profile on the server!' + # Close the channel time.sleep(10) for update in lnd_lightning_client.close_channel(channel_point): @@ -256,23 +290,7 @@ def run(): # Check the server balance get_balance_response = admin_stub.GetBalance(squeak_admin_pb2.GetBalanceRequest()) print("Get balance response balance: " + str(get_balance_response.balance)) - # assert get_balance_response.balance > 0 - - # Create a new signing profile - profile_name = 'bob' - create_signing_profile_response = admin_stub.CreateSigningProfile(squeak_admin_pb2.CreateSigningProfileRequest( - profile_name=profile_name, - )) - print("Get create signing profile response: " + str(create_signing_profile_response)) - profile_id = create_signing_profile_response.profile_id - # assert profile_id >= 0 - - # Get the new squeak profile - get_squeak_profile_response = admin_stub.GetSqueakProfile(squeak_admin_pb2.GetSqueakProfileRequest( - profile_id=profile_id, - )) - print("Get squeak profile response: " + str(get_squeak_profile_response)) - assert get_squeak_profile_response.squeak_profile.profile_name == profile_name + assert get_balance_response.balance == 1000 if __name__ == '__main__': diff --git a/squeakserver/admin/rpc/squeak_admin.proto b/squeakserver/admin/rpc/squeak_admin.proto index 415c8ab4..72a358ea 100644 --- a/squeakserver/admin/rpc/squeak_admin.proto +++ b/squeakserver/admin/rpc/squeak_admin.proto @@ -22,6 +22,10 @@ service SqueakAdmin { */ rpc GetSqueakProfile (GetSqueakProfileRequest) returns (GetSqueakProfileReply) {} + /** sqkadmin: `makesqueak` + */ + rpc MakeSqueak (MakeSqueakRequest) returns (MakeSqueakReply) {} + } message GetBalanceRequest { @@ -71,3 +75,19 @@ message SqueakProfile { /// Following bool following = 6; } + +message MakeSqueakRequest { + /// The profile id + int32 profile_id = 1; + + /// The content + string content = 2; + + /// The replyto hash + bytes replyto = 3; +} + +message MakeSqueakReply { + /// Hash of the created squeak. + bytes hash = 1; +} diff --git a/squeakserver/admin/squeak_admin_server_handler.py b/squeakserver/admin/squeak_admin_server_handler.py index 77b8bcc2..ac9428aa 100644 --- a/squeakserver/admin/squeak_admin_server_handler.py +++ b/squeakserver/admin/squeak_admin_server_handler.py @@ -16,6 +16,8 @@ from squeakserver.server.util import get_hash from squeakserver.server.squeak_profile import SqueakProfile +from squeakserver.node.squeak_maker import SqueakMaker + logger = logging.getLogger(__name__) @@ -41,12 +43,15 @@ class SqueakAdminServerHandler(object): def handle_create_signing_profile(self, profile_name): logger.info("Handle create signing profile with name: {}".format(profile_name)) signing_key = CSigningKey.generate() + logger.info("Created new signing key: {}".format(signing_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() squeak_profile = SqueakProfile( profile_id=None, profile_name=profile_name, - private_key=bytes(signing_key), + private_key=signing_key_bytes, address=str(address), sharing=False, following=False, @@ -59,3 +64,11 @@ class SqueakAdminServerHandler(object): logger.info("Handle get squeak profile with id: {}".format(profile_id)) squeak_profile = self.postgres_db.get_profile(profile_id) return squeak_profile + + def handle_make_squeak(self, profile_id, content_str, replyto_hash): + logger.info("Handle make squeak profile with id: {}".format(profile_id)) + squeak_profile = self.postgres_db.get_profile(profile_id) + squeak_maker = SqueakMaker(self.lightning_client) + squeak = squeak_maker.make_squeak(squeak_profile, content_str, replyto_hash) + inserted_squeak_hash = self.postgres_db.insert_squeak(squeak) + return inserted_squeak_hash diff --git a/squeakserver/admin/squeak_admin_server_servicer.py b/squeakserver/admin/squeak_admin_server_servicer.py index 2137ab30..536389d1 100644 --- a/squeakserver/admin/squeak_admin_server_servicer.py +++ b/squeakserver/admin/squeak_admin_server_servicer.py @@ -48,6 +48,15 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer): ) ) + def MakeSqueak(self, request, context): + profile_id = request.profile_id + content_str = request.content + replyto_hash = request.replyto + squeak_hash = self.handler.handle_make_squeak(profile_id, content_str, replyto_hash) + return squeak_admin_pb2.MakeSqueakReply( + hash=squeak_hash, + ) + def serve(self): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) squeak_admin_pb2_grpc.add_SqueakAdminServicer_to_server( diff --git a/squeakserver/node/__init__.py b/squeakserver/node/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/squeakserver/node/block_info.py b/squeakserver/node/block_info.py new file mode 100644 index 00000000..4fbee54f --- /dev/null +++ b/squeakserver/node/block_info.py @@ -0,0 +1,4 @@ +from collections import namedtuple + + +BlockInfo = namedtuple('BlockInfo', 'block_hash, block_height') diff --git a/squeakserver/node/hash.py b/squeakserver/node/hash.py new file mode 100644 index 00000000..e69de29b diff --git a/squeakserver/node/squeak_maker.py b/squeakserver/node/squeak_maker.py new file mode 100644 index 00000000..87aa1a61 --- /dev/null +++ b/squeakserver/node/squeak_maker.py @@ -0,0 +1,54 @@ +import logging +import time + +from squeak.core.signing import CSigningKey +from squeak.core import MakeSqueakFromStr + +from squeakserver.node.block_info import BlockInfo + + +logger = logging.getLogger(__name__) + + +class SqueakMaker: + + def __init__(self, lightning_client): + self.lightning_client = lightning_client + + def make_squeak(self, signing_profile, content_str, replyto_hash=None): + signing_key_str = signing_profile.private_key.decode() + signing_key = CSigningKey(signing_key_str) + logger.info("Creating squeak with signing key: {}".format(signing_key)) + logger.info("Creating squeak with replyto_hash: {}".format(replyto_hash)) + latest_block = self._get_latest_block() + block_height = latest_block.block_height + block_hash = latest_block.block_hash + timestamp = self._get_current_time_s() + logger.info("Creating squeak with block height: {}".format(block_height)) + logger.info("Creating squeak with block hash: {}".format(block_hash)) + if replyto_hash is None or len(replyto_hash) == 0: + return MakeSqueakFromStr( + signing_key, + content_str, + block_height, + block_hash, + timestamp, + ) + else: + return MakeSqueakFromStr( + signing_key, + content_str, + block_height, + block_hash, + timestamp, + replyto_hash, + ) + + def _get_latest_block(self): + get_info_response = self.lightning_client.get_info() + block_hash = bytes.fromhex(get_info_response.block_hash) + block_height = get_info_response.block_height + return BlockInfo(block_hash, block_height) + + def _get_current_time_s(self): + return int(time.time())