specs/x8.md

116 lines
5.0 KiB
Markdown
Raw Normal View History

2019-04-22 04:09:28 +00:00
---
sip: 3
title: Initial Message Payload Specification
2019-04-22 04:09:28 +00:00
status: Draft
type: Standard
author: Adam Babik <adam@status.im>, Oskar Thorén <oskar@status.im>
2019-04-22 04:09:28 +00:00
created: 2019-04-22
updated:
---
2019-04-22 04:22:25 +00:00
2019-08-27 11:33:02 +00:00
- [Wrapper](#wrapper)
- [Encoding](#encoding)
- [Upgradability](#upgradability)
2019-04-22 04:22:25 +00:00
2019-08-27 09:34:42 +00:00
This specification describes how the payload of each message in the Status Protocol looks.
2019-08-26 13:33:09 +00:00
The payload must be flexible enough to support messaging but also cases described in [Status Whitepaper](https://status.im/whitepaper.pdf) as well as various clients created using vastly different technologies.
2019-07-17 06:33:45 +00:00
# Wrapper
2019-07-17 06:33:45 +00:00
Payloads are wrapped in a [protobuf record](https://developers.google.com/protocol-buffers/)
record:
2019-08-27 09:34:42 +00:00
```protobuf
message StatusProtocolMessage {
bytes signature = 1;
bytes payload = 2;
}
2019-07-17 06:33:45 +00:00
```
2019-08-27 11:26:21 +00:00
`signature` is the bytes of the signed `SHA3-256` of the payload, signed with the key of the author of the message.
2019-07-17 06:33:45 +00:00
The signature is needed to validate authorship of the message, so that the message can be relayed to third parties.
2019-08-26 13:33:09 +00:00
If a signature is not present but an author is provided by a layer below, the message is to be relayed to third parties and its considered plausibly deniable.
# Encoding
2019-04-22 04:22:25 +00:00
2019-08-27 11:38:09 +00:00
The payload is encoded using [Transit format](https://github.com/cognitect/transit-format). Transit was chosen over JSON in order to reduce the bandwidth.
2019-04-22 04:22:25 +00:00
Example of a valid encoded payload:
```
["~#c4",["abc123","text/plain","~:public-group-user-message",154593077368201,1545930773682,["^ ","~:chat-id","testing-adamb","~:text","abc123"]]]
```
As you can see, the message is an array and each index value has its meaning:
* 0: `c4` is a decoder handler identification for the current payload format. Identifications allow to register handlers for many different types of payload
* 1: array which items correspond to the described payload fields above
For more details regarding serialization and deserialization please consult [transit format](https://github.com/cognitect/transit-format) specification.
2019-07-17 06:33:45 +00:00
# Message types
2019-08-27 11:38:09 +00:00
- [Message](#message)
2019-07-17 06:33:45 +00:00
## Message
The type `Message` represents a text message exchanged between clients.
2019-08-27 11:26:21 +00:00
- [Payload](#payload)
- [Content types](#content-types)
- [Message types](#message-types)
- [Clock vs Timestamp and message ordering](#clock-vs-timestamp-and-message-ordering)
- [Replies](#replies)
2019-07-17 06:33:45 +00:00
### Payload
2019-08-26 13:33:09 +00:00
Payload is a struct (a compound data type) with the following fields (order is important):
2019-07-17 06:33:45 +00:00
2019-08-27 11:41:25 +00:00
| Field | Name | Type |
| ----- | ---- | ---- |
| 1 | text | `string` |
| 2 | content type | `enum` (more in [Content types](#content-types)) |
| 3 | message type | `enum` (more in [Message types](#message-types)) |
| 4 | clock | `int64` |
| 5 | timestamp | `int64` |
| 6 | content | `struct { chat-id string, text string }` |
2019-07-17 06:33:45 +00:00
### Content types
2019-04-22 04:22:25 +00:00
2019-08-26 13:33:09 +00:00
Content types are required for a proper interpretation of incoming messages. Not each message is a plain text but may carry a different information.
2019-04-22 04:22:25 +00:00
The following content types MUST be supported:
* `text/plain` identifies a message which content is a plain text.
There are also other content types that MAY be implemented by the client:
2019-04-22 04:22:25 +00:00
* `sticker` TODO
* `status` TODO
* `command` TODO
* `command-request` TODO
* `emoji` TODO
2019-07-17 06:33:45 +00:00
### Message types
2019-04-22 04:22:25 +00:00
2019-08-26 13:33:09 +00:00
Message types are required to decide how a particular message is encrypted (more in [Whisper > Message encryption](#message-encryption)) and what metadata needs to be attached (more in [Whisper > Topic](#topic)) when passing a message to the transport layer.
2019-04-22 04:22:25 +00:00
The following messages types MUST be supported:
* `public-group-user-message` is a message to the public group
* `user-message` is a private message
* `group-user-message` is a message to the private group.
2019-07-17 06:33:45 +00:00
### Clock vs Timestamp and message ordering
2019-04-22 04:22:25 +00:00
`timestamp` MUST be Unix time calculated when the message is created. Because the peers in the Whisper network should have synchronized time, `timestamp` values should be fairly accurate among all Whisper network participants.
`clock` SHOULD be calculated using the algorithm of [Lamport timestamps](https://en.wikipedia.org/wiki/Lamport_timestamps). When there are messages available in a chat, `clock`'s value is calculated based on the last received message in a particular chat: `last-message-clock-value + 1`. If there are no messages, `clock` is initialized with `timestamp`'s value.
`clock` value is used for the message ordering. Due to the used algorithm and distributed nature of the system, we achieve casual ordering which might produce counterintuitive results in some edge cases. For example, when one joins a public chat and sends a message before receiving the exist messages, their message `clock` value might be lower and the message will end up in the past when the historical messages are fetched.
2019-07-17 06:33:45 +00:00
### Replies
2019-04-22 04:22:25 +00:00
TODO
2019-04-22 04:22:25 +00:00
# Upgradability
2019-04-22 04:22:25 +00:00
The current protocol format is hardly upgradable without breaking backward compatibility. Because Transit is used in this particular way described above, the only reliable option is to append a new field to the Transit record definition. It will be simply ignored by the old clients.