Skip to main content

Dao Ke Dao (道可道) -- Message Module (Python)

License PRs Welcome Platform Issues Repo Size Tags Version

Watchers Forks Stars Followers

This document introduces a common Message Module for decentralized instant messaging.

Features

Envelope

Message Envelope

/* example */
{
    "sender"   : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
    "receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
    "time"     : 1545405083
}

Content

/* example */
{
    "type"     : "1",       // message type
    "sn"       : 412968873, // serial number (message ID in conversation)
    "time"     : 1545405083,
    
    "text"     : "Hey guy!"
}

Content Type

def i2s(value: int) -> str:
    return '%d' % value


class ContentType:

    ANY = i2s(0x00)        # 0000 0000 (Undefined)

    TEXT = i2s(0x01)       # 0000 0001

    FILE = i2s(0x10)       # 0001 0000
    IMAGE = i2s(0x12)      # 0001 0010
    AUDIO = i2s(0x14)      # 0001 0100
    VIDEO = i2s(0x16)      # 0001 0110

    # Web Page
    PAGE = i2s(0x20)       # 0010 0000

    # Name Card
    NAME_CARD = i2s(0x33)  # 0011 0011

    # Quote a message before and reply it with text
    QUOTE = i2s(0x37)      # 0011 0111

    MONEY = i2s(0x40)          # 0100 0000
    TRANSFER = i2s(0x41)       # 0100 0001
    LUCKY_MONEY = i2s(0x42)    # 0100 0010
    CLAIM_PAYMENT = i2s(0x48)  # 0100 1000 (Claim for Payment)
    SPLIT_BILL = i2s(0x49)     # 0100 1001 (Split the Bill)

    COMMAND = i2s(0x88)        # 1000 1000
    HISTORY = i2s(0x89)        # 1000 1001 (Entity History Command)

    # Application Customized
    APPLICATION = i2s(0xA0)       # 1010 0000 (Application 0nly, Reserved)
    # APPLICATION_1 = i2s(0xA1)   # 1010 0001 (Reserved)
    # ...                         # 1010 ???? (Reserved)
    # APPLICATION_15 = i2s(0xAF)  # 1010 1111 (Reserved)

    # CUSTOMIZED_0 = i2s(0xC0)    # 1100 0000 (Reserved)
    # CUSTOMIZED_1 = i2s(0xC1)    # 1100 0001 (Reserved)
    # ...                         # 1100 ???? (Reserved)
    ARRAY = i2s(0xCA)             # 1100 1010 (Content Array)
    # ...                         # 1100 ???? (Reserved)
    CUSTOMIZED = i2s(0xCC)        # 1100 1100 (Customized Content)
    # ...                         # 1100 ???? (Reserved)
    COMBINE_FORWARD = i2s(0xCF)   # 1100 1111 (Combine and Forward)

    # Top-Secret message forward by proxy (MTA)
    FORWARD = i2s(0xFF)           # 1111 1111

Message

When the user want to send out a message, the client needs TWO steps before sending it:

  1. Encrypt the Instant Message to Secure Message;
  2. Sign the Secure Message to Reliable Message.

Accordingly, when the client received a message, it needs TWO steps to extract the content:

  1. Verify the Reliable Message to Secure Message;
  2. Decrypt the Secure Message to Instant Message.
    Message Transforming
    ~~~~~~~~~~~~~~~~~~~~

    Instant Message  <-->  Secure Message  <-->  Reliable Message
    +-------------+        +------------+        +--------------+
    |  sender     |        |  sender    |        |  sender      |
    |  receiver   |        |  receiver  |        |  receiver    |
    |  time       |        |  time      |        |  time        |
    |             |        |            |        |              |
    |  content    |        |  data      |        |  data        |
    +-------------+        |  keys      |        |  keys        |
                           +------------+        |  signature   |
                                                 +--------------+
    Algorithm:
        data      = password.encrypt(content)
        key       = receiver.public_key.encrypt(password)
        signature = sender.private_key.sign(data)

Instant Message

/* example */
{
    //-------- head (envelope) --------
    "sender"   : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
    "receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
    "time"     : 1545405083,
    
    //-------- body (content) ---------
    "content"  : {
        "type"  : "1",       // message type
        "sn"    : 412968873, // serial number (ID)
        "time"  : 1545405083,
        
        "text"  : "Hey guy!"
    }
}

content -> JsON string: {"sn":412968873,"text":"Hey guy!","type":1}

Secure Message

/**
 *  Algorithm:
 *      string = json(content);
 *      PW     = random();
 *      data   = encrpyt(string, PW);      // Symmetric
 *      key    = encrypt(PW, receiver.PK); // Asymmetric
 */
{
    //-------- head (envelope) --------
    "sender"   : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
    "receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
    "time"     : 1545405083,
    
    //-------- body (content) ---------
    "data"     : "9cjCKG99ULCCxbL2mkc/MgF1saeRqJaCc+S12+HCqmsuF7TWK61EwTQWZSKskUeF",
    "keys"     : {
        "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj" : "WH/wAcu+HfpaLq+vRblNnYufkyjTm4FgYyzW3wBDeRtXs1TeDmRxKVu7nQI/sdIALGLXrY+O5mlRfhU8f8TuIBilZUlX/eIUpL4uSDYKVLaRG9pOcrCHKevjUpId9x/8KBEiMIL5LB0Vo7sKrvrqosCnIgNfHbXMKvMzwcqZEU8="
    }
}

Reliable Message

/**
 *  Algorithm:
 *      signature = sign(data, sender.SK);
 */
{
    //-------- head (envelope) --------
    "sender"   : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
    "receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
    "time"     : 1545405083,
    
    //-------- body (content) ---------
    "data"      : "9cjCKG99ULCCxbL2mkc/MgF1saeRqJaCc+S12+HCqmsuF7TWK61EwTQWZSKskUeF",
    "keys"      : {
        "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj" : "WH/wAcu+HfpaLq+vRblNnYufkyjTm4FgYyzW3wBDeRtXs1TeDmRxKVu7nQI/sdIALGLXrY+O5mlRfhU8f8TuIBilZUlX/eIUpL4uSDYKVLaRG9pOcrCHKevjUpId9x/8KBEiMIL5LB0Vo7sKrvrqosCnIgNfHbXMKvMzwcqZEU8="
    },
    "signature" : "Yo+hchWsQlWHtc8iMGS7jpn/i9pOLNq0E3dTNsx80QdBboTLeKoJYAg/lI+kZL+g7oWJYpD4qKemOwzI+9pxdMuZmPycG+0/VM3HVSMcguEOqOH9SElp/fYVnm4aSjAJk2vBpARzMT0aRNp/jTFLawmMDuIlgWhBfXvH7bT7rDI="
}

(All data encode with BASE64 algorithm as default)


Copyright © 2018-2026 Albert Moky Followers

Download files

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

Source Distribution

dkd-2.4.3.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dkd-2.4.3-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file dkd-2.4.3.tar.gz.

File metadata

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

File hashes

Hashes for dkd-2.4.3.tar.gz
Algorithm Hash digest
SHA256 07c00bff927cb2c33f7a351e5673cfc0793e4b877d5318a97da7ff094e5b7000
MD5 1411f30b890404e57eb87e130e1ab2a2
BLAKE2b-256 0f7e916e1512680c81efb3e0b6673ebf80693e9c4fd25fb4bf329682cc44b069

See more details on using hashes here.

File details

Details for the file dkd-2.4.3-py3-none-any.whl.

File metadata

  • Download URL: dkd-2.4.3-py3-none-any.whl
  • Upload date:
  • Size: 21.4 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/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for dkd-2.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 409479df70029cd72370a0801dc054ad30e76ef5f8ca1e6cde8357b462afc2ad
MD5 1a5001c608562767b6592cb7c23103e5
BLAKE2b-256 9629c2c4e361f4bf28447eed33e4cde117177851b54a80e4c40b29f1d1c3a371

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.4.3 This release

2 files

2.4.2

2 files

2.4.1

2 files

2.4.0

2 files

2.3.4

2 files

2.3.3

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

2 files

2.2.2

2 files

2.2.1

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.12.8

2 files

0.12.7

2 files

0.12.6

2 files

0.12.4

2 files

0.12.2

2 files

0.12.1

2 files

0.12.0

2 files

0.11.6

2 files

0.11.5

2 files

0.11.2

2 files

0.11.1

2 files

0.10.15

2 files

0.10.14

2 files

0.10.13

2 files

0.10.12

2 files

0.10.11

2 files

0.10.10

2 files

0.8.0

2 files

0.7.7

2 files

0.7.6

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.9

2 files

0.6.8

2 files

0.6.7

2 files

0.6.6

2 files

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.16

2 files

0.5.15

2 files

0.5.7

2 files

0.5.6

2 files

0.5.3

2 files

0.5.1

2 files

0.5.0

2 files

0.4.7

2 files

0.4.6

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.3.5

2 files

0.3.3

2 files

0.3.1

2 files

0.2.7

2 files

0.2.6

2 files

0.2.4

2 files

0.2.3

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page