Skip to main content

Decentralized Instant Messaging Protocol

Project description

Decentralized Instant Messaging Protocol (Python)

license Version PRs Welcome Platform

Talk is cheap, show you the codes!

Dependencies

pip3 install dimp

Common Extensions

facebook.py

class Facebook(Barrack):
    """ Access database to load/save user's private key, meta and profiles """

    def save_private_key(self, private_key: PrivateKey, identifier: ID) -> bool:
        # TODO: save to local/persistent storage
        pass

    def save_profile(self, profile: Profile) -> bool:
        if not self.verify_profile(profile):
            return False
        # TODO: save to local storage
        pass

    def verify_profile(self, profile: Profile) -> bool:
        if profile is None:
            return False
        elif profile.valid:
            # already verified
            return True
        identifier = profile.identifier
        meta = None
        if identifier.type.is_communicator():
            # verify with account's meta.key
            meta = self.meta(identifier=identifier)
        elif identifier.type.is_group():
            # verify with group owner's meta.key
            group = self.group(identifier=identifier)
            if group is not None:
                meta = self.meta(identifier=group.owner)
        if meta is not None:
            return profile.verify(public_key=meta.key)

    #
    #   ISocialNetworkDataSource (Entity factories)
    #
    def account(self, identifier: ID) -> Account:
        account = super().account(identifier=identifier)
        if account is not None:
            return account
        # check meta
        meta = self.meta(identifier=identifier)
        if meta is not None:
            # create account with type
            if identifier.type.is_station():
                account = Server(identifier=identifier)
            elif identifier.type.is_person():
                account = Account(identifier=identifier)
            self.cache_account(account=account)
            return account

    def user(self, identifier: ID) -> User:
        user = super().user(identifier=identifier)
        if user is not None:
            return user
        # check meta and private key
        meta = self.meta(identifier=identifier)
        key = self.private_key_for_signature(identifier=identifier)
        if meta is not None and key is not None:
            # create user
            user = User(identifier=identifier)
            self.cache_user(user=user)
            return user

    def group(self, identifier: ID) -> Group:
        group = super().group(identifier=identifier)
        if group is not None:
            return group
        # check meta
        meta = self.meta(identifier=identifier)
        if meta is not None:
            # create group with type
            group = Group(identifier=identifier)
            self.cache_group(group=group)
            return group


#
#  singleton
#
facebook = Facebook()

keystore.py

class KeyStore(KeyCache):
    """ For reuse symmetric key """

    def save_keys(self, key_map: dict) -> bool:
        # TODO: save to local cache
        pass

    def load_keys(self) -> dict:
        # TODO: load from local cache
        pass


#
#  singleton
#
keystore = KeyStore()

messanger.py

class Messanger(Transceiver, ITransceiverDelegate):
    """ Transform and send message """

    def __init__(self):
        super().__init__()
        self.delegate = self

    #
    #  ITransceiverDelegate
    #
    def send_package(self, data: bytes, handler: ICompletionHandler) -> bool:
        # TODO: send out data
        pass

    def upload_data(self, data: bytes, msg: InstantMessage) -> str:
        # TODO: upload onto FTP server
        pass

    def download_data(self, url: str, msg: InstantMessage) -> bytes:
        # TODO: download from FTP server
        pass


#
#  singleton
#
messanger = Messanger()
messanger.barrack = facebook
messanger.key_cache = keystore

User Account

register.py

def register(username: str) -> User:
    # 1. generate private key
    sk = PrivateKey({'algorithm': 'RSA'})

    # 2. generate meta with username(as seed) and private key
    meta = Meta.generate(private_key=sk, seed=username)

    # 3. generate ID with network type by meta
    identifier = meta.generate_identifier(network=network)

    # 4. save private key and meta info
    facebook.save_private_key(private_key=sk, identifier=identifier)
    facebook.save_meta(meta=meta, identifier=identifier)

    # 5. create user with ID
    return facebook.user(identifier)

Messaging

send.py

def pack(content: Content, sender: ID, receiver: ID) -> ReliableMessage:
    msg = InstantMessage.new(content=content, sender=sender, receiver=receiver)
    return messanger.encrypt_sign(msg)


if __name__ == '__main__':
    moki = ID("moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk");
    hulk = ID("hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj");
    content = TextContent.new(text='Hello world!')
    msg = pack(content=content, sender=moki, receiver=hulk)

    # TODO: send out the request data
    request = json.dumps(msg) + '\n'
    data = request.encode('utf-8')

receive.py

def receive(pack: bytes) -> InstantMessage:
    msg = json.loads(pack.decode('utf-8'))
    r_msg = ReliableMessage(msg)
    i_msg = messanger.verify_decrypt(r_msg)
    # TODO: process message content
    pass

Copyright © 2019 Albert Moky

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dimp-0.4.6.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

dimp-0.4.6-py3-none-any.whl (34.3 kB view details)

Uploaded Python 3

File details

Details for the file dimp-0.4.6.tar.gz.

File metadata

  • Download URL: dimp-0.4.6.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for dimp-0.4.6.tar.gz
Algorithm Hash digest
SHA256 1cc7bd46247a1cfe59e7108d0baa0c50ef53adb9c00d378e785fdac25d5418b5
MD5 f31a0b906ca9b4c7d7af816d14adf1cf
BLAKE2b-256 9e155e95182c93bf1bb1fcb917535d54e66a7c100c0df918e3f1ada20a78fde0

See more details on using hashes here.

File details

Details for the file dimp-0.4.6-py3-none-any.whl.

File metadata

  • Download URL: dimp-0.4.6-py3-none-any.whl
  • Upload date:
  • Size: 34.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for dimp-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f9b3094b175fe38bfe5c124da5a513986c08a3cdfe5c418090fa81440d1e6f1e
MD5 29b7bcfbb4f9e5df6dad15394db7c251
BLAKE2b-256 a0576629ea3595bdba9482a55c0bc72fe23b01420f51bdd17bdc819c786d3e16

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page