-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathencoder.ts
128 lines (107 loc) · 3.63 KB
/
encoder.ts
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
// @ts-strict-ignore
import { Timestamp, SyncProtoBuf } from '@actual-app/crdt';
import * as encryption from '../encryption';
import { SyncError } from '../errors';
import * as prefs from '../prefs';
import { Message } from './index';
function coerceBuffer(value) {
// The web encryption APIs give us back raw Uint8Array... but our
// encryption code assumes we can work with it as a buffer. This is
// a leaky abstraction and ideally the our abstraction over the web
// encryption APIs should do this.
if (!Buffer.isBuffer(value)) {
return Buffer.from(value);
}
return value;
}
export async function encode(
groupId: string,
fileId: string,
since: Timestamp | string,
messages: Message[],
): Promise<Uint8Array> {
const { encryptKeyId } = prefs.getPrefs();
const requestPb = new SyncProtoBuf.SyncRequest();
for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
const envelopePb = new SyncProtoBuf.MessageEnvelope();
envelopePb.setTimestamp(msg.timestamp.toString());
const messagePb = new SyncProtoBuf.Message();
messagePb.setDataset(msg.dataset);
messagePb.setRow(msg.row);
messagePb.setColumn(msg.column);
messagePb.setValue(msg.value as string);
const binaryMsg = messagePb.serializeBinary();
if (encryptKeyId) {
const encrypted = new SyncProtoBuf.EncryptedData();
let result;
try {
result = await encryption.encrypt(binaryMsg, encryptKeyId);
} catch (e) {
throw new SyncError('encrypt-failure', {
isMissingKey: e.message === 'missing-key',
});
}
encrypted.setData(result.value);
encrypted.setIv(Buffer.from(result.meta.iv, 'base64'));
encrypted.setAuthtag(Buffer.from(result.meta.authTag, 'base64'));
envelopePb.setContent(encrypted.serializeBinary());
envelopePb.setIsencrypted(true);
} else {
envelopePb.setContent(binaryMsg);
}
requestPb.addMessages(envelopePb);
}
requestPb.setGroupid(groupId);
requestPb.setFileid(fileId);
requestPb.setKeyid(encryptKeyId);
requestPb.setSince(since.toString());
return requestPb.serializeBinary();
}
export async function decode(
data: Uint8Array,
): Promise<{ messages: Message[]; merkle: { hash: number } }> {
const { encryptKeyId } = prefs.getPrefs();
const responsePb = SyncProtoBuf.SyncResponse.deserializeBinary(data);
const merkle = JSON.parse(responsePb.getMerkle());
const list = responsePb.getMessagesList();
const messages = [];
for (let i = 0; i < list.length; i++) {
const envelopePb = list[i];
const timestamp = Timestamp.parse(envelopePb.getTimestamp());
const encrypted = envelopePb.getIsencrypted();
let msg;
if (encrypted) {
const binary = SyncProtoBuf.EncryptedData.deserializeBinary(
envelopePb.getContent() as Uint8Array,
);
let decrypted;
try {
decrypted = await encryption.decrypt(coerceBuffer(binary.getData()), {
keyId: encryptKeyId,
algorithm: 'aes-256-gcm',
iv: coerceBuffer(binary.getIv()),
authTag: coerceBuffer(binary.getAuthtag()),
});
} catch (e) {
console.log(e);
throw new SyncError('decrypt-failure', {
isMissingKey: e.message === 'missing-key',
});
}
msg = SyncProtoBuf.Message.deserializeBinary(decrypted);
} else {
msg = SyncProtoBuf.Message.deserializeBinary(
envelopePb.getContent() as Uint8Array,
);
}
messages.push({
timestamp,
dataset: msg.getDataset(),
row: msg.getRow(),
column: msg.getColumn(),
value: msg.getValue(),
});
}
return { messages, merkle };
}