Dao Ke Dao (道可道) -- Message Module (Python)
This document introduces a common Message Module for decentralized instant messaging.
Copyright © 2018-2019 Albert Moky
0. Envelope
Message Envelope
{
"sender" : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
"receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
"time" : 1545405083
}
1. Content
{
"type" : 0x01,
"sn" : 412968873,
"text" : "Hey guy!"
}
Message Content Type
class ContentType(IntEnum):
TEXT = 0x01
FILE = 0x10
IMAGE = 0x12
AUDIO = 0x14
VIDEO = 0x16
PAGE = 0x20
QUOTE = 0x37
MONEY = 0x40
COMMAND = 0x88
HISTORY = 0x89
FORWARD = 0xFF
2. Message
When the user want to send out a message, the client needs TWO steps before sending it:
- Encrypt the Instant Message to Secure Message;
- Sign the Secure Message to Reliable Message.
Accordingly, when the client received a message, it needs TWO steps to extract the content:
- Verify the Reliable Message to Secure Message;
- 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 |
+-------------+ | key/keys | | key/keys |
+------------+ | signature |
+--------------+
Algorithm:
data = password.encrypt(content)
key = receiver.public_key.encrypt(password)
signature = sender.private_key.sign(data)
Instant Message
{
"sender" : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
"receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
"time" : 1545405083,
"content" : {
"type" : 0x01,
"sn" : 412968873,
"text" : "Hey guy!"
}
}
content -> JsON string: {"sn":412968873,"text":"Hey guy!","type":1}
Secure Message
{
"sender" : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
"receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
"time" : 1545405083,
"data" : "9cjCKG99ULCCxbL2mkc/MgF1saeRqJaCc+S12+HCqmsuF7TWK61EwTQWZSKskUeF",
"key" : "WH/wAcu+HfpaLq+vRblNnYufkyjTm4FgYyzW3wBDeRtXs1TeDmRxKVu7nQI/sdIALGLXrY+O5mlRfhU8f8TuIBilZUlX/eIUpL4uSDYKVLaRG9pOcrCHKevjUpId9x/8KBEiMIL5LB0Vo7sKrvrqosCnIgNfHbXMKvMzwcqZEU8="
}
Reliable Message
{
"sender" : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
"receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
"time" : 1545405083,
"data" : "9cjCKG99ULCCxbL2mkc/MgF1saeRqJaCc+S12+HCqmsuF7TWK61EwTQWZSKskUeF",
"key" : "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)