/
message.js
153 lines (123 loc) · 3.2 KB
/
message.js
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
import { Buffer } from 'buffer';
import { ChannelMessage as PChannelMessage } from '../messages';
import { BanError, now } from '../utils';
import Chain from './chain';
export const HASH_SIZE = 32;
export default class Message {
constructor(options) {
const {
sodium, parents, height, chain, timestamp = now(), body, signature,
} = options;
if (!sodium) {
throw new Error('Missing required `sodium` option');
}
this.sodium = sodium;
parents.forEach((hash) => {
Message.checkHash(hash, 'Invalid parent hash');
});
if (height < 0) {
throw new BanError('Invalid height');
}
if (signature.length !== sodium.crypto_sign_BYTES) {
throw new BanError('Invalid signature length');
}
if (!body.root) {
try {
JSON.parse(body.json);
} catch (e) {
throw new BanError('Invalid JSON content. ' + e.message);
}
}
this.parents = parents;
this.height = height;
this.chain = chain;
this.timestamp = timestamp;
this.body = body;
this.signature = signature;
this.hash = Buffer.alloc(HASH_SIZE);
sodium.crypto_generichash(this.hash, this.serializeData());
this.debugHash = this.hash.toString('hex').slice(0, 8);
this.cachedJSON = null;
}
get isRoot() {
return !!this.body.root;
}
get json() {
if (this.isRoot) {
return undefined;
}
if (!this.cachedJSON) {
this.cachedJSON = JSON.parse(this.body.json);
}
return this.cachedJSON;
}
getAuthor() {
return {
displayPath: this.chain.getDisplayPath(),
publicKeys: this.chain.getPublicKeys(),
};
}
verify(channel) {
const sodium = this.sodium;
const leafKey = this.chain.getLeafKey(channel, this.timestamp);
return sodium.crypto_sign_verify_detached(
this.signature,
Message.tbs(this.serializeTBS()),
leafKey);
}
serialize() {
return {
tbs: this.serializeTBS(),
signature: this.signature,
};
}
serializeData() {
return PChannelMessage.encode(this.serialize()).finish();
}
serializeTBS() {
return {
parents: this.parents,
height: this.height,
chain: this.chain.serialize(),
timestamp: this.timestamp,
body: this.body,
};
}
static deserialize(decoded, options) {
const chain = Chain.deserialize(decoded.tbs.chain, options);
return new Message({
sodium: options.sodium,
parents: decoded.tbs.parents,
height: decoded.tbs.height.toNumber(),
chain,
timestamp: decoded.tbs.timestamp,
body: decoded.tbs.body,
signature: decoded.signature,
});
}
static deserializeData(data, options) {
return Message.deserialize(PChannelMessage.decode(data), options);
}
// Helpers
static root() {
return { root: {} };
}
static json(value) {
return { json: JSON.stringify(value) };
}
static checkHash(hash, message) {
if (!hash || hash.length !== HASH_SIZE) {
throw new BanError(message);
}
}
static tbs({ chain, timestamp, body, parents, height }) {
return PChannelMessage.TBS.encode({
chain,
timestamp,
body,
parents,
height,
}).finish();
}
}
Message.HASH_SIZE = HASH_SIZE;