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:
        pass


#
#  singleton
#
facebook = Facebook()

keystore.py

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

    def save_keys(self, key_map: dict) -> bool:
        pass

    def load_keys(self) -> dict:
        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:
        pass

    def upload_data(self, data: bytes, msg: InstantMessage) -> str:
        pass

    def download_data(self, url: str, msg: InstantMessage) -> bytes:
        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
    user = User(identifier)
    facebook.cache_user(user)
    return user

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')

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.3.11.tar.gz (17.8 kB view hashes)

Uploaded Source

Built Distribution

dimp-0.3.11-py3-none-any.whl (37.1 kB view hashes)

Uploaded Python 3

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