Skip to content

Mail and Instant Message(IM) Message Format

dnotestein edited this page Mar 7, 2014 · 6 revisions

All messages derive from message_header:

struct message_header
{
  uint32_t  size; //number of bytes in message
  uint32_t  type; //Type of message, used to properly unpack different message types.
                  //Also basis for message versioning.
};

struct message : public message_header
{
  std::vector<char> data;
  template<typename T> T as()const; //unpack message as type specified in message_header
...
};

Mail connections receive two types of messages in on_connection_message:

  1. encrypted_message::type: KH tries to decode these using message keys it knows. If it succeeds, it forward the decrypted message on further processing in bitchat_message_received. Profile's last_sync_time is updated with the message's timestamp (when message was sent, but for mail it is sender's local time plus server_time_offset).
  2. server_info_message::type: currently this is only used to update the server_time_offset from server reported time. This message also contains information such as server version, mirrors, and current_difficulty (all currently ignored).

bitchat_channel_impl::handle_message receives many types of messages, including encrypted messages. Encrypted messages are passed to channel_impl::handle_priv_msg which:

  1. validates the message's proof-of-work
  2. validates message was requested
  3. puts it on list of new_msgs to broadcast if it's less than 30 minutes old
  4. caches it in its _message_cache
  5. forwards to client_impl::handle_message which tries to decrypt it.

If the message can be decrypted in handle_message using message keys it knows, the decrypted message is forwarded to bitchat_message_received. Profile's last_sync_time is not updated in this case.

Decrypted messages contain their own embedded msg_type (distinct from the type in the originating encrypted message):

struct decrypted_message
{
  /** type of the decrypted, uncompressed message */
  fc::enum_type<fc::unsigned_int,private_message_type>  msg_type;
  std::vector<char>                                     data;                                                                
  fc::time_point_sec                                    sig_time;
  fc::optional<fc::ecc::compact_signature>              from_sig;
  fc::optional<fc::ecc::public_key>                     from_key;
  fc::optional<fc::ecc::private_key>                    decrypt_key;
 ...
};

bitchat_message_received caches the decrypted message in Profile's cache, unpacks the message based on it's type, and forwards unpacked messaged to an appropriate handler in the client.

Current decrypted message types:

  1. private_message_type::text_msg: instant message is passed to client's received_text(msg)
  2. private_message_type::email_msg: email message is passed to client's received_email(msg)
  3. private_message_type::contact_request_msg: request to authorize a user passed to client's received_request(msg). This involves exchange of extended_private_keys for enhanced transaction privacy and will also be used to establish "direct-connect" IM links.

Clone this wiki locally