feat: Reimplement the OMEMOMessage protobuf schema

This commit is contained in:
2022-08-04 22:55:40 +02:00
parent 08ec093675
commit a82b5d0990
3 changed files with 242 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
import 'package:omemo_dart/src/helpers.dart';
import 'package:omemo_dart/src/protobuf/protobuf.dart';
class OmemoMessage {
const OmemoMessage(this.n, this.pn, this.dhPub, this.ciphertext);
factory OmemoMessage.fromBuffer(List<int> data) {
var i = 0;
// required uint32 n = 1;
if (data[0] != 8) {
throw Exception();
}
var decode = decodeVarint(data, 1);
final n = decode.n;
i += decode.length + 1;
// required uint32 pn = 2;
if (data[i] != 16) {
throw Exception();
}
decode = decodeVarint(data, i + 1);
final pn = decode.n;
i += decode.length + 1;
// required bytes dh_pub = 3;
if (data[i] != ((3 << 3) | 2)) {
throw Exception();
}
final dhPub = data.sublist(i + 2, i + 2 + data[i + 1]);
i += 2 + data[i + 1];
// optional bytes ciphertext = 4;
List<int>? ciphertext;
if (i < data.length) {
if (data[i] != ((4 << 3) | 2)) {
throw Exception();
}
ciphertext = data.sublist(i + 2, i + 2 + data[i + 1]);
}
return OmemoMessage(n, pn, dhPub, ciphertext);
}
final int n;
final int pn;
final List<int> dhPub;
final List<int>? ciphertext;
List<int> writeToBuffer() {
final data = concat([
[8],
encodeVarint(n),
[16],
encodeVarint(pn),
[((3 << 3) | 2), dhPub.length],
dhPub,
]);
if (ciphertext != null) {
return concat([
data,
[((4 << 3) | 2), ciphertext!.length],
ciphertext!,
]);
}
return data;
}
}