summaryrefslogtreecommitdiffstats
path: root/vendor/go.mau.fi/libsignal/serialize/JSONSerializer.go
blob: 1245fc42bfe8be5b50c2a76b1895b9c98942f4b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package serialize

import (
	"encoding/json"

	groupRecord "go.mau.fi/libsignal/groups/state/record"
	"go.mau.fi/libsignal/logger"
	"go.mau.fi/libsignal/protocol"
	"go.mau.fi/libsignal/state/record"
)

// NewJSONSerializer will return a serializer for all Signal objects that will
// be responsible for converting objects to and from JSON bytes.
func NewJSONSerializer() *Serializer {
	serializer := NewSerializer()

	serializer.SignalMessage = &JSONSignalMessageSerializer{}
	serializer.PreKeySignalMessage = &JSONPreKeySignalMessageSerializer{}
	serializer.SignedPreKeyRecord = &JSONSignedPreKeyRecordSerializer{}
	serializer.PreKeyRecord = &JSONPreKeyRecordSerializer{}
	serializer.State = &JSONStateSerializer{}
	serializer.Session = &JSONSessionSerializer{}
	serializer.SenderKeyMessage = &JSONSenderKeyMessageSerializer{}
	serializer.SenderKeyDistributionMessage = &JSONSenderKeyDistributionMessageSerializer{}
	serializer.SenderKeyRecord = &JSONSenderKeySessionSerializer{}
	serializer.SenderKeyState = &JSONSenderKeyStateSerializer{}

	return serializer
}

// JSONSignalMessageSerializer is a structure for serializing signal messages into
// and from JSON.
type JSONSignalMessageSerializer struct{}

// Serialize will take a signal message structure and convert it to JSON bytes.
func (j *JSONSignalMessageSerializer) Serialize(signalMessage *protocol.SignalMessageStructure) []byte {
	serialized, err := json.Marshal(*signalMessage)
	if err != nil {
		logger.Error("Error serializing signal message: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a signal message structure.
func (j *JSONSignalMessageSerializer) Deserialize(serialized []byte) (*protocol.SignalMessageStructure, error) {
	var signalMessage protocol.SignalMessageStructure
	err := json.Unmarshal(serialized, &signalMessage)
	if err != nil {
		logger.Error("Error deserializing signal message: ", err)
		return nil, err
	}

	return &signalMessage, nil
}

// JSONPreKeySignalMessageSerializer is a structure for serializing prekey signal messages
// into and from JSON.
type JSONPreKeySignalMessageSerializer struct{}

// Serialize will take a prekey signal message structure and convert it to JSON bytes.
func (j *JSONPreKeySignalMessageSerializer) Serialize(signalMessage *protocol.PreKeySignalMessageStructure) []byte {
	serialized, err := json.Marshal(signalMessage)
	if err != nil {
		logger.Error("Error serializing prekey signal message: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a prekey signal message structure.
func (j *JSONPreKeySignalMessageSerializer) Deserialize(serialized []byte) (*protocol.PreKeySignalMessageStructure, error) {
	var preKeySignalMessage protocol.PreKeySignalMessageStructure
	err := json.Unmarshal(serialized, &preKeySignalMessage)
	if err != nil {
		logger.Error("Error deserializing prekey signal message: ", err)
		return nil, err
	}

	return &preKeySignalMessage, nil
}

// JSONSignedPreKeyRecordSerializer is a structure for serializing signed prekey records
// into and from JSON.
type JSONSignedPreKeyRecordSerializer struct{}

// Serialize will take a signed prekey record structure and convert it to JSON bytes.
func (j *JSONSignedPreKeyRecordSerializer) Serialize(signedPreKey *record.SignedPreKeyStructure) []byte {
	serialized, err := json.Marshal(signedPreKey)
	if err != nil {
		logger.Error("Error serializing signed prekey record: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a signed prekey record structure.
func (j *JSONSignedPreKeyRecordSerializer) Deserialize(serialized []byte) (*record.SignedPreKeyStructure, error) {
	var signedPreKeyStructure record.SignedPreKeyStructure
	err := json.Unmarshal(serialized, &signedPreKeyStructure)
	if err != nil {
		logger.Error("Error deserializing signed prekey record: ", err)
		return nil, err
	}

	return &signedPreKeyStructure, nil
}

// JSONPreKeyRecordSerializer is a structure for serializing prekey records
// into and from JSON.
type JSONPreKeyRecordSerializer struct{}

// Serialize will take a prekey record structure and convert it to JSON bytes.
func (j *JSONPreKeyRecordSerializer) Serialize(preKey *record.PreKeyStructure) []byte {
	serialized, err := json.Marshal(preKey)
	if err != nil {
		logger.Error("Error serializing prekey record: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a prekey record structure.
func (j *JSONPreKeyRecordSerializer) Deserialize(serialized []byte) (*record.PreKeyStructure, error) {
	var preKeyStructure record.PreKeyStructure
	err := json.Unmarshal(serialized, &preKeyStructure)
	if err != nil {
		logger.Error("Error deserializing prekey record: ", err)
		return nil, err
	}

	return &preKeyStructure, nil
}

// JSONStateSerializer is a structure for serializing session states into
// and from JSON.
type JSONStateSerializer struct{}

// Serialize will take a session state structure and convert it to JSON bytes.
func (j *JSONStateSerializer) Serialize(state *record.StateStructure) []byte {
	serialized, err := json.Marshal(state)
	if err != nil {
		logger.Error("Error serializing session state: ", err)
	}
	logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a session state structure.
func (j *JSONStateSerializer) Deserialize(serialized []byte) (*record.StateStructure, error) {
	var stateStructure record.StateStructure
	err := json.Unmarshal(serialized, &stateStructure)
	if err != nil {
		logger.Error("Error deserializing session state: ", err)
		return nil, err
	}

	return &stateStructure, nil
}

// JSONSessionSerializer is a structure for serializing session records into
// and from JSON.
type JSONSessionSerializer struct{}

// Serialize will take a session structure and convert it to JSON bytes.
func (j *JSONSessionSerializer) Serialize(session *record.SessionStructure) []byte {
	serialized, err := json.Marshal(session)
	if err != nil {
		logger.Error("Error serializing session: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a session structure, which can be
// used to create a new Session Record object.
func (j *JSONSessionSerializer) Deserialize(serialized []byte) (*record.SessionStructure, error) {
	var sessionStructure record.SessionStructure
	err := json.Unmarshal(serialized, &sessionStructure)
	if err != nil {
		logger.Error("Error deserializing session: ", err)
		return nil, err
	}

	return &sessionStructure, nil
}

// JSONSenderKeyDistributionMessageSerializer is a structure for serializing senderkey
// distribution records to and from JSON.
type JSONSenderKeyDistributionMessageSerializer struct{}

// Serialize will take a senderkey distribution message and convert it to JSON bytes.
func (j *JSONSenderKeyDistributionMessageSerializer) Serialize(message *protocol.SenderKeyDistributionMessageStructure) []byte {
	serialized, err := json.Marshal(message)
	if err != nil {
		logger.Error("Error serializing senderkey distribution message: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a message structure, which can be
// used to create a new SenderKey Distribution object.
func (j *JSONSenderKeyDistributionMessageSerializer) Deserialize(serialized []byte) (*protocol.SenderKeyDistributionMessageStructure, error) {
	var msgStructure protocol.SenderKeyDistributionMessageStructure
	err := json.Unmarshal(serialized, &msgStructure)
	if err != nil {
		logger.Error("Error deserializing senderkey distribution message: ", err)
		return nil, err
	}

	return &msgStructure, nil
}

// JSONSenderKeyMessageSerializer is a structure for serializing senderkey
// messages to and from JSON.
type JSONSenderKeyMessageSerializer struct{}

// Serialize will take a senderkey message and convert it to JSON bytes.
func (j *JSONSenderKeyMessageSerializer) Serialize(message *protocol.SenderKeyMessageStructure) []byte {
	serialized, err := json.Marshal(message)
	if err != nil {
		logger.Error("Error serializing senderkey distribution message: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a message structure, which can be
// used to create a new SenderKey message object.
func (j *JSONSenderKeyMessageSerializer) Deserialize(serialized []byte) (*protocol.SenderKeyMessageStructure, error) {
	var msgStructure protocol.SenderKeyMessageStructure
	err := json.Unmarshal(serialized, &msgStructure)
	if err != nil {
		logger.Error("Error deserializing senderkey message: ", err)
		return nil, err
	}

	return &msgStructure, nil
}

// JSONSenderKeyStateSerializer is a structure for serializing group session states into
// and from JSON.
type JSONSenderKeyStateSerializer struct{}

// Serialize will take a session state structure and convert it to JSON bytes.
func (j *JSONSenderKeyStateSerializer) Serialize(state *groupRecord.SenderKeyStateStructure) []byte {
	serialized, err := json.Marshal(state)
	if err != nil {
		logger.Error("Error serializing session state: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a session state structure.
func (j *JSONSenderKeyStateSerializer) Deserialize(serialized []byte) (*groupRecord.SenderKeyStateStructure, error) {
	var stateStructure groupRecord.SenderKeyStateStructure
	err := json.Unmarshal(serialized, &stateStructure)
	if err != nil {
		logger.Error("Error deserializing session state: ", err)
		return nil, err
	}

	return &stateStructure, nil
}

// JSONSenderKeySessionSerializer is a structure for serializing session records into
// and from JSON.
type JSONSenderKeySessionSerializer struct{}

// Serialize will take a session structure and convert it to JSON bytes.
func (j *JSONSenderKeySessionSerializer) Serialize(session *groupRecord.SenderKeyStructure) []byte {
	serialized, err := json.Marshal(session)
	if err != nil {
		logger.Error("Error serializing session: ", err)
	}
	// logger.Debug("Serialize result: ", string(serialized))

	return serialized
}

// Deserialize will take in JSON bytes and return a session structure, which can be
// used to create a new Session Record object.
func (j *JSONSenderKeySessionSerializer) Deserialize(serialized []byte) (*groupRecord.SenderKeyStructure, error) {
	var sessionStructure groupRecord.SenderKeyStructure
	err := json.Unmarshal(serialized, &sessionStructure)
	if err != nil {
		logger.Error("Error deserializing session: ", err)
		return nil, err
	}

	return &sessionStructure, nil
}