trac-peer 0.0.58 → 0.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.0.58",
4
+ "version": "0.0.60",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.1.1",
package/src/check.js CHANGED
@@ -5,14 +5,350 @@ class Check {
5
5
  constructor() {
6
6
  this._node = null;
7
7
  this._tx = null;
8
+ this._post_tx = null;
9
+ this._msg = null;
10
+ this._feature = null;
11
+ this._add_writer = null;
12
+ this._key = null;
13
+ this._nick = null;
14
+ this._mute = null;
15
+ this._delete_message = null;
16
+ this._mod = null;
17
+ this._whitelist_status = null;
18
+ this._enable_whitelist = null;
8
19
  this.validator = new Validator();
9
20
  }
10
21
 
22
+ async compileEnableWhitelist (){
23
+ /*
24
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.type === undefined ||
25
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
26
+ op.nonce === undefined || op.value.dispatch.enabled === undefined || typeof op.value.dispatch.enabled !== 'boolean' ||
27
+ op.hash === undefined) continue;
28
+ */
29
+ const schema = {
30
+ nonce: { type : "string", min : 1 },
31
+ hash: { type : "string", hex : null },
32
+ value : {
33
+ $$type: "object",
34
+ dispatch : {
35
+ $$type : "object",
36
+ enabled : { type : "boolean" },
37
+ type : { type : "string", min : 1 },
38
+ address : { type : "string", hex : null }
39
+ }
40
+ }
41
+ };
42
+ return this.validator.compile(schema);
43
+ }
44
+
45
+ async enableWhitelist(op){
46
+ if(this._enable_whitelist === null) {
47
+ this._enable_whitelist = await this.compileEnableWhitelist();
48
+ }
49
+ return this._enable_whitelist(op);
50
+ }
51
+
52
+ async compileWhitelistStatus (){
53
+ /*
54
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
55
+ typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
56
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
57
+ op.nonce === undefined || op.value.dispatch.status === undefined || typeof op.value.dispatch.status !== 'boolean' ||
58
+ op.hash === undefined) continue;
59
+ */
60
+ const schema = {
61
+ nonce: { type : "string", min : 1 },
62
+ hash: { type : "string", hex : null },
63
+ value : {
64
+ $$type: "object",
65
+ dispatch : {
66
+ $$type : "object",
67
+ status : { type : "boolean" },
68
+ type : { type : "string", min : 1 },
69
+ address : { type : "string", hex : null },
70
+ user : { type : "string", hex : null }
71
+ }
72
+ }
73
+ };
74
+ return this.validator.compile(schema);
75
+ }
76
+
77
+ async whitelistStatus(op){
78
+ if(this._whitelist_status === null) {
79
+ this._whitelist_status = await this.compileWhitelistStatus();
80
+ }
81
+ return this._whitelist_status(op);
82
+ }
83
+
84
+ async compileMod (){
85
+ /*
86
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
87
+ typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
88
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
89
+ op.nonce === undefined || op.value.dispatch.mod === undefined || typeof op.value.dispatch.mod !== 'boolean' ||
90
+ op.hash === undefined) continue;
91
+ */
92
+ const schema = {
93
+ nonce: { type : "string", min : 1 },
94
+ hash: { type : "string", hex : null },
95
+ value : {
96
+ $$type: "object",
97
+ dispatch : {
98
+ $$type : "object",
99
+ mod : { type : "boolean" },
100
+ type : { type : "string", min : 1 },
101
+ address : { type : "string", hex : null },
102
+ user : { type : "string", hex : null }
103
+ }
104
+ }
105
+ };
106
+ return this.validator.compile(schema);
107
+ }
108
+
109
+ async mod(op){
110
+ if(this._mod === null) {
111
+ this._mod = await this.compileMod();
112
+ }
113
+ return this._mod(op);
114
+ }
115
+
116
+ async compileDeleteMessage (){
117
+ /*
118
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.id === undefined ||
119
+ typeof op.value.dispatch.id !== "number" || op.value.dispatch.type === undefined ||
120
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
121
+ op.nonce === undefined || op.hash === undefined || op.value.dispatch.deleted_by === undefined) continue;
122
+ */
123
+ const schema = {
124
+ nonce: { type : "string", min : 1 },
125
+ hash: { type : "string", hex : null },
126
+ value : {
127
+ $$type: "object",
128
+ dispatch : {
129
+ $$type : "object",
130
+ id : { type : "number", integer: true, positive: true },
131
+ type : { type : "string", min : 1 },
132
+ address : { type : "string", hex : null },
133
+ deleted_by : { type : "string", hex : null, nullable : true }
134
+ }
135
+ }
136
+ };
137
+ return this.validator.compile(schema);
138
+ }
139
+
140
+ async deleteMessage(op){
141
+ if(this._delete_message === null) {
142
+ this._delete_message = await this.compileDeleteMessage();
143
+ }
144
+ return this._delete_message(op);
145
+ }
146
+
147
+ async compileMute (){
148
+ /*
149
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
150
+ typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
151
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
152
+ op.nonce === undefined || op.value.dispatch.muted === undefined || typeof op.value.dispatch.muted !== 'boolean' ||
153
+ op.hash === undefined) continue;
154
+ */
155
+ const schema = {
156
+ nonce: { type : "string", min : 1 },
157
+ hash: { type : "string", hex : null },
158
+ value : {
159
+ $$type: "object",
160
+ dispatch : {
161
+ $$type : "object",
162
+ muted : { type : "boolean" },
163
+ type : { type : "string", min : 1 },
164
+ address : { type : "string", hex : null },
165
+ user : { type : "string", hex : null }
166
+ }
167
+ }
168
+ };
169
+ return this.validator.compile(schema);
170
+ }
171
+
172
+ async mute(op){
173
+ if(this._mute === null) {
174
+ this._mute = await this.compileMute();
175
+ }
176
+ return this._mute(op);
177
+ }
178
+
179
+ async compileNick (){
180
+ /*
181
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.nick === undefined ||
182
+ typeof op.value.dispatch.nick !== "string" || op.value.dispatch.type === undefined ||
183
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
184
+ op.value.dispatch.initiator === undefined || typeof op.value.dispatch.initiator !== "string" ||
185
+ op.nonce === undefined || op.hash === undefined) continue;
186
+ */
187
+ const schema = {
188
+ nonce: { type : "string", min : 1 },
189
+ hash: { type : "string", hex : null },
190
+ value : {
191
+ $$type: "object",
192
+ dispatch : {
193
+ $$type : "object",
194
+ nick : { type : "string", min : 1 },
195
+ type : { type : "string", min : 1 },
196
+ address : { type : "string", hex : null },
197
+ initiator : { type : "string", hex : null }
198
+ }
199
+ }
200
+ };
201
+ return this.validator.compile(schema);
202
+ }
203
+
204
+ async nick(op){
205
+ if(this._nick === null) {
206
+ this._nick = await this.compileNick();
207
+ }
208
+ return this._nick(op);
209
+ }
210
+
211
+ async compileKey() {
212
+ const schema = {
213
+ key: { type : 'string', hex: null }
214
+ };
215
+ return this.validator.compile(schema);
216
+ }
217
+
218
+ async key(op){
219
+ if(this._key === null) {
220
+ this._key = await this.compileKey();
221
+ }
222
+ return this._key(op);
223
+ }
224
+
225
+ async setStatus(op){
226
+ // currently same as addWriter
227
+ return await this.addWriter(op);
228
+ }
229
+
230
+ async compileAddWriter (){
231
+ /*
232
+ if(op.key === undefined || op.value === undefined || op.hash === undefined ||
233
+ op.value.msg === undefined || op.value.msg.key === undefined ||
234
+ op.value.msg.type === undefined || op.nonce === undefined) continue;
235
+ */
236
+ const schema = {
237
+ key: { type : "string", hex : null },
238
+ hash : { type : "string", hex : null },
239
+ nonce : { type : "string", min : 1 },
240
+ value : {
241
+ $$type: "object",
242
+ msg : {
243
+ $$type : "object",
244
+ type : { type : "string", min : 1 },
245
+ key: { type : "string", hex : null },
246
+ }
247
+ }
248
+ };
249
+ return this.validator.compile(schema);
250
+ }
251
+
252
+ async addIndexer(op){
253
+ // currently same as addWriter
254
+ return await this.addWriter(op);
255
+ }
256
+
257
+ async addWriter(op){
258
+ if(this._add_writer === null) {
259
+ this._add_writer = await this.compileAddWriter();
260
+ }
261
+ return this._add_writer(op);
262
+ }
263
+
264
+ async compileFeature (){
265
+ /*
266
+ if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined ||
267
+ op.value.dispatch.hash === undefined || op.value.dispatch.value === undefined ||
268
+ op.value.dispatch.nonce === undefined) continue;
269
+ */
270
+ const schema = {
271
+ key: { type : "string", min : 1 },
272
+ value : {
273
+ $$type: "object",
274
+ dispatch : {
275
+ $$type : "object",
276
+ value : { type : "object" },
277
+ nonce: { type : "string", min : 1 },
278
+ hash: { type : "string", hex : null }
279
+ }
280
+ }
281
+ };
282
+ return this.validator.compile(schema);
283
+ }
284
+
285
+ async feature(op){
286
+ if(this._feature === null) {
287
+ this._feature = await this.compileFeature();
288
+ }
289
+ return this._feature(op);
290
+ }
291
+
292
+ async compileMsg (){
293
+ /*
294
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.attachments === undefined ||
295
+ !Array.isArray(op.value.dispatch.attachments) || op.value.dispatch.msg === undefined ||
296
+ typeof op.value.dispatch.msg !== "string" || op.value.dispatch.type === undefined ||
297
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
298
+ op.nonce === undefined || op.hash === undefined) continue;
299
+ */
300
+ const schema = {
301
+ nonce: { type : "string", min : 1 },
302
+ hash: { type : "string", hex : null },
303
+ value : {
304
+ $$type: "object",
305
+ dispatch : {
306
+ $$type : "object",
307
+ attachments : { type : "array", items : "string" },
308
+ msg : { type : "string", min : 1 },
309
+ type : { type : "string", min : 1 },
310
+ address : { type : "string", hex : null }
311
+ }
312
+ }
313
+ };
314
+ return this.validator.compile(schema);
315
+ }
316
+
317
+ async msg(op){
318
+ if(this._msg === null) {
319
+ this._msg = await this.compileMsg();
320
+ }
321
+ return this._msg(op);
322
+ }
323
+
324
+ async compilePostTx() {
325
+ // if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined) continue;
326
+ const schema = {
327
+ value : {
328
+ $$type: "object",
329
+ tx : { type : "string", hex : null },
330
+ ch : { type : "string", hex : null }
331
+ }
332
+ };
333
+ return this.validator.compile(schema);
334
+ }
335
+
336
+ async postTx(post_tx){
337
+ if(this._post_tx === null) {
338
+ this._post_tx = await this.compilePostTx();
339
+ }
340
+ return this._post_tx(post_tx);
341
+ }
342
+
11
343
  async compileNode() {
12
344
  const schema = {
345
+ from : {
346
+ $$type: "object",
347
+ key : { type : 'string', hex : null, instanceof: Buffer }
348
+ },
13
349
  value: {
14
350
  $$type: "object",
15
- type: "string"
351
+ type: { type : "string", min : 1 }
16
352
  }
17
353
  };
18
354
  return this.validator.compile(schema);
@@ -20,7 +356,7 @@ class Check {
20
356
 
21
357
  async node(node){
22
358
  if(this._node === null) {
23
- this._node = this.compileNode();
359
+ this._node = await this.compileNode();
24
360
  }
25
361
  return this._node(node);
26
362
  }
@@ -28,11 +364,11 @@ class Check {
28
364
  async compileTx() {
29
365
  // if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined) continue;
30
366
  const schema = {
31
- key: "hex",
367
+ key: { type : "string", hex : null },
32
368
  value : {
33
- $$type : "object",
34
- dispatch : "object",
35
- msbsl : "integer"
369
+ $$type: "object",
370
+ dispatch : { type : "object" },
371
+ msbsl : { type : "number", integer : true, positive: true }
36
372
  }
37
373
  };
38
374
  return this.validator.compile(schema);
@@ -40,7 +376,7 @@ class Check {
40
376
 
41
377
  async tx(op){
42
378
  if(this._tx === null) {
43
- this._tx = this.compileTx();
379
+ this._tx = await this.compileTx();
44
380
  }
45
381
  return this._tx(op);
46
382
  }
package/src/contract.js CHANGED
@@ -1,13 +1,89 @@
1
+ import Validator from 'fastest-validator';
2
+
1
3
  class Contract {
2
4
 
3
5
  constructor(protocol, options = {}) {
4
6
  this.protocol = protocol;
5
7
  this.storage = null;
6
8
  this.options = options;
9
+ this.is_feature = false;
10
+ this.is_message = false;
11
+ this.features = {};
12
+ this.schemata = {};
13
+ this.message_handler = null;
14
+ this.root = null;
15
+ this.validator = new Validator();
16
+ }
17
+
18
+ async execute(op, node, storage){
19
+ if(op.key === undefined ||
20
+ op.value === undefined ||
21
+ op.value.dispatch === undefined ||
22
+ op.value.dispatch.type === undefined) return await this.emptyPromise();
23
+
24
+ this.address = null;
25
+
26
+ if(op.type !== 'feature' && op.type !== 'msg'){
27
+ if(op.value.value === undefined || op.value.value.ipk === undefined) return await this.emptyPromise();
28
+ this.address = op.value.value.ipk;
29
+ } else {
30
+ if(op.value.dispatch.address === undefined) return this.emptyPromise();
31
+ if(op.type === 'feature') this.is_feature = true;
32
+ if(op.type === 'msg') this.is_message = true;
33
+ this.address = op.value.dispatch.address;
34
+ }
35
+
36
+ this.tx = op.key;
37
+ this.op = op.value.dispatch;
38
+ this.node = node;
39
+ this.storage = storage;
40
+ this.root = op;
41
+
42
+ if(this.isFeature()) {
43
+ await this.features[this.op.type].func();
44
+ } else if(this.isMessage()) {
45
+ if(typeof this.message_handler === "function"){
46
+ this.message_handler();
47
+ }
48
+ } else {
49
+ if(typeof this[this.op.type] === "function") {
50
+ if(true === this.validateSchema(this.op.type, this.op)) {
51
+ this[this.op.type]();
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ validateSchema(type, op) {
58
+ return this.schemata[type](op);
59
+ }
60
+
61
+ addSchema(type, schema){
62
+ this.schemata[type] = this.validator.compile(schema);
63
+ }
64
+
65
+ addFeature(type, func) {
66
+ this.features[type] = func;
67
+ }
68
+
69
+ messageHandler(func){
70
+ this.message_handler = func;
71
+ }
72
+
73
+ getRoot() {
74
+ return this.root;
75
+ }
76
+
77
+ isFeature(){
78
+ return this.is_feature;
79
+ }
80
+
81
+ isMessage(){
82
+ return this.is_message;
7
83
  }
8
84
 
9
- async dispatch(op, node, storage) {
10
- throw Error('Not implemented: Contract.dispatch(op, node, signed_storage, storage)');
85
+ async dispatch() {
86
+ throw Error('Not implemented: Contract.dispatch()');
11
87
  }
12
88
 
13
89
  async get(key){
package/src/functions.js CHANGED
@@ -194,6 +194,7 @@ export async function setNick(input, peer){
194
194
 
195
195
  export async function postMessage(input, peer){
196
196
  const splitted = yargs(input).parse();
197
+ if(typeof splitted.message === "boolean" || splitted.message === undefined) throw new Error('Empty message not allowed');
197
198
  const value = splitted.message;
198
199
  const nonce = Math.random() + '-' + Date.now();
199
200
  const signature = { dispatch : {
package/src/index.js CHANGED
@@ -84,46 +84,39 @@ export class Peer extends ReadyResource {
84
84
  const msb_view_session = _this.msb.base.view.checkout(op.value.msbsl);
85
85
  const post_tx = await msb_view_session.get(op.key);
86
86
  await msb_view_session.close();
87
+ if(false === await this.check.postTx(post_tx)) continue;
87
88
  if (null !== str_dispatch &&
88
- null !== post_tx &&
89
89
  null === await batch.get('tx/'+op.key) &&
90
- post_tx.value !== undefined &&
91
- post_tx.value.tx !== undefined &&
92
- post_tx.value.ch !== undefined &&
93
90
  op.key === post_tx.value.tx &&
94
91
  post_tx.value.ch === createHash('sha256').update(str_dispatch).digest('hex')) {
95
92
  await batch.put('tx/'+op.key, op.value);
96
- await _this.contract_instance.dispatch(op, node, batch);
97
- console.log(`${op.key} appended`);
93
+ await _this.contract_instance.execute(op, node, batch);
94
+ console.log(`${op.key} appended. Signed length:`, _this.base.view.core.signedLength);
98
95
  }
99
96
  } else if(op.type === 'msg') {
100
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.attachments === undefined ||
101
- !Array.isArray(op.value.dispatch.attachments) || op.value.dispatch.msg === undefined ||
102
- typeof op.value.dispatch.msg !== "string" || op.value.dispatch.type === undefined ||
103
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
104
- op.nonce === undefined || op.hash === undefined) continue;
105
-
97
+ if(false === await this.check.msg(op)) continue;
106
98
  const admin = await batch.get('admin');
107
99
  let muted = false;
108
- const mute_status = await batch.get('mtd/'+op.value.dispatch.address);
109
- if(null !== mute_status){
110
- muted = mute_status.value;
111
- }
100
+ let whitelisted = true;
112
101
  const whitelist_status = await batch.get('wlst');
113
102
  if(null !== whitelist_status && true === whitelist_status.value) {
114
- muted = true;
115
- const whitelisted = await batch.get('wl/'+op.value.dispatch.address);
116
- if(null !== whitelisted && true === whitelisted.value) {
117
- muted = false;
103
+ const _whitelisted = await batch.get('wl/'+op.value.dispatch.address);
104
+ if(null === _whitelisted || false === _whitelisted.value) {
105
+ whitelisted = false;
118
106
  }
119
107
  }
108
+ const mute_status = await batch.get('mtd/'+op.value.dispatch.address);
109
+ if(null !== mute_status){
110
+ muted = mute_status.value;
111
+ }
120
112
  if(null !== admin && admin.value === op.value.dispatch.address) {
121
113
  muted = false;
114
+ whitelisted = true;
122
115
  }
123
116
  const str_value = jsonStringify(op.value);
124
117
  const chat_status = await batch.get('chat_status');
125
118
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
126
- if(false === muted && null !== str_value && verified &&
119
+ if(false === muted && true === whitelisted && null !== str_value && verified &&
127
120
  null !== chat_status && chat_status.value === 'on' &&
128
121
  null === await batch.get('sh/'+op.hash) &&
129
122
  Buffer.byteLength(str_value) <= 10_2400){
@@ -135,35 +128,26 @@ export class Peer extends ReadyResource {
135
128
  }
136
129
  await batch.put('msg/'+len, op.value.dispatch);
137
130
  await batch.put('msgl', len + 1);
138
- await _this.contract_instance.dispatch(op, node, batch);
131
+ await _this.contract_instance.execute(op, node, batch);
139
132
  const nick = await batch.get('nick/'+op.value.dispatch.address);
140
133
  console.log(`#${len + 1} | ${nick !== null ? nick.value : op.value.dispatch.address}: ${op.value.dispatch.msg}`);
141
134
  }
142
135
  await batch.put('sh/'+op.hash, '');
143
136
  } else if (op.type === 'feature') {
144
- if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined ||
145
- op.value.dispatch.hash === undefined || op.value.dispatch.value === undefined ||
146
- op.value.dispatch.nonce === undefined) continue;
147
-
137
+ if(false === await this.check.feature(op)) continue;
148
138
  const str_dispatch_value = jsonStringify(op.value.dispatch.value);
149
139
  const admin = await batch.get('admin');
150
140
  if(null !== admin &&
151
- typeof op.value.dispatch === "object" &&
152
- typeof op.value.dispatch.hash === "string" &&
153
- typeof op.value.dispatch.value !== "undefined" &&
154
141
  null === await batch.get('sh/'+op.value.dispatch.hash)){
155
142
  const verified = _this.wallet.verify(op.value.dispatch.hash, str_dispatch_value + op.value.dispatch.nonce, admin.value);
156
143
  if(verified) {
157
- await _this.contract_instance.dispatch(op, node, batch);
144
+ await _this.contract_instance.execute(op, node, batch);
158
145
  console.log(`Feature ${op.key} appended`);
159
146
  }
160
147
  }
161
148
  await batch.put('sh/'+op.value.dispatch.hash, '');
162
149
  } else if (op.type === 'addIndexer') {
163
- if(op.key === undefined || op.value === undefined || op.hash === undefined ||
164
- op.value.msg === undefined || op.value.msg.key === undefined ||
165
- op.value.msg.type === undefined || op.nonce === undefined) continue;
166
-
150
+ if(false === await this.check.addIndexer(op)) continue;
167
151
  const str_msg = jsonStringify(op.value.msg);
168
152
  const admin = await batch.get('admin');
169
153
  if(null !== admin &&
@@ -179,10 +163,7 @@ export class Peer extends ReadyResource {
179
163
  }
180
164
  await batch.put('sh/'+op.hash, '');
181
165
  } else if (op.type === 'addWriter') {
182
- if(op.key === undefined || op.hash === undefined || op.value === undefined ||
183
- op.value.msg === undefined || op.value.msg.key === undefined ||
184
- op.value.msg.type === undefined || op.nonce === undefined) continue;
185
-
166
+ if(false === await this.check.addWriter(op)) continue;
186
167
  const str_msg = jsonStringify(op.value.msg);
187
168
  const admin = await batch.get('admin');
188
169
  if(null !== admin &&
@@ -198,10 +179,7 @@ export class Peer extends ReadyResource {
198
179
  }
199
180
  await batch.put('sh/'+op.hash, '');
200
181
  } else if (op.type === 'setChatStatus') {
201
- if(op.key === undefined || op.value === undefined || op.hash === undefined ||
202
- op.value.msg === undefined || op.value.msg.key === undefined ||
203
- op.nonce === undefined || op.value.msg.type === undefined) continue;
204
-
182
+ if(false === await this.check.setStatus(op)) continue;
205
183
  const str_msg = jsonStringify(op.value.msg);
206
184
  const admin = await batch.get('admin');
207
185
  if(null !== admin && op.value.msg.key === op.key &&
@@ -216,10 +194,7 @@ export class Peer extends ReadyResource {
216
194
  }
217
195
  await batch.put('sh/'+op.hash, '');
218
196
  } else if (op.type === 'setAutoAddWriters') {
219
- if(op.key === undefined || op.value === undefined || op.hash === undefined ||
220
- op.value.msg === undefined || op.value.msg.key === undefined ||
221
- op.nonce === undefined || op.value.msg.type === undefined) continue;
222
-
197
+ if(false === await this.check.setStatus(op)) continue;
223
198
  const str_msg = jsonStringify(op.value.msg);
224
199
  const admin = await batch.get('admin');
225
200
  if(null !== admin && op.value.msg.key === op.key &&
@@ -234,7 +209,7 @@ export class Peer extends ReadyResource {
234
209
  }
235
210
  await batch.put('sh/'+op.hash, '');
236
211
  } else if (op.type === 'autoAddWriter') {
237
- if(op.key === undefined) continue;
212
+ if(false === await this.check.key(op)) continue;
238
213
  const auto_add_writers = await batch.get('auto_add_writers');
239
214
  if(null !== auto_add_writers && auto_add_writers.value === 'on'){
240
215
  const writerKey = b4a.from(op.key, 'hex');
@@ -242,19 +217,14 @@ export class Peer extends ReadyResource {
242
217
  }
243
218
  console.log(`Writer auto added: ${op.key}`);
244
219
  } else if (op.type === 'addAdmin') {
245
- if(op.key === undefined) continue;
220
+ if(false === await this.check.key(op)) continue;
246
221
  const bootstrap = Buffer(node.from.key).toString('hex')
247
222
  if(null === await batch.get('admin') && bootstrap === _this.bootstrap){
248
223
  await batch.put('admin', op.key);
249
224
  console.log(`Admin added: ${op.key}`);
250
225
  }
251
226
  } else if(op.type === 'setNick') {
252
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.nick === undefined ||
253
- typeof op.value.dispatch.nick !== "string" || op.value.dispatch.type === undefined ||
254
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
255
- op.value.dispatch.initiator === undefined || typeof op.value.dispatch.initiator !== "string" ||
256
- op.nonce === undefined || op.hash === undefined) continue;
257
-
227
+ if(false === await this.check.nick(op)) continue;
258
228
  const taken = await batch.get('kcin/'+op.value.dispatch.nick);
259
229
  const chat_status = await batch.get('chat_status');
260
230
  const str_value = jsonStringify(op.value);
@@ -288,12 +258,7 @@ export class Peer extends ReadyResource {
288
258
  }
289
259
  await batch.put('sh/'+op.hash, '');
290
260
  } else if(op.type === 'muteStatus') {
291
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
292
- typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
293
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
294
- op.nonce === undefined || op.value.dispatch.muted === undefined || typeof op.value.dispatch.muted !== 'boolean' ||
295
- op.hash === undefined) continue;
296
-
261
+ if(false === await this.check.mute(op)) continue;
297
262
  const admin = await batch.get('admin');
298
263
  const str_value = jsonStringify(op.value);
299
264
  if(null !== admin && null !== str_value &&
@@ -314,11 +279,7 @@ export class Peer extends ReadyResource {
314
279
  }
315
280
  await batch.put('sh/'+op.hash, '');
316
281
  } else if(op.type === 'deleteMessage') {
317
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.id === undefined ||
318
- typeof op.value.dispatch.id !== "number" || op.value.dispatch.type === undefined ||
319
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
320
- op.nonce === undefined || op.hash === undefined || op.value.dispatch.deleted_by === undefined) continue;
321
-
282
+ if(false === await this.check.deleteMessage(op)) continue;
322
283
  const admin = await batch.get('admin');
323
284
  const str_value = jsonStringify(op.value);
324
285
  if(null !== admin && null !== str_value &&
@@ -355,12 +316,7 @@ export class Peer extends ReadyResource {
355
316
  }
356
317
  await batch.put('sh/'+op.hash, '');
357
318
  } else if(op.type === 'setMod') {
358
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
359
- typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
360
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
361
- op.nonce === undefined || op.value.dispatch.mod === undefined || typeof op.value.dispatch.mod !== 'boolean' ||
362
- op.hash === undefined) continue;
363
-
319
+ if(false === await this.check.mod(op)) continue;
364
320
  const admin = await batch.get('admin');
365
321
  const str_value = jsonStringify(op.value);
366
322
  if(null !== admin && null !== str_value &&
@@ -373,12 +329,7 @@ export class Peer extends ReadyResource {
373
329
  }
374
330
  await batch.put('sh/'+op.hash, '');
375
331
  } else if(op.type === 'setWhitelistStatus') {
376
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
377
- typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
378
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
379
- op.nonce === undefined || op.value.dispatch.status === undefined || typeof op.value.dispatch.status !== 'boolean' ||
380
- op.hash === undefined) continue;
381
-
332
+ if(false === await this.check.whitelistStatus(op)) continue;
382
333
  const admin = await batch.get('admin');
383
334
  const str_value = jsonStringify(op.value);
384
335
  if(null !== admin && null !== str_value &&
@@ -391,11 +342,7 @@ export class Peer extends ReadyResource {
391
342
  }
392
343
  await batch.put('sh/'+op.hash, '');
393
344
  } else if(op.type === 'enableWhitelist') {
394
- if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.type === undefined ||
395
- op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
396
- op.nonce === undefined || op.value.dispatch.enabled === undefined || typeof op.value.dispatch.enabled !== 'boolean' ||
397
- op.hash === undefined) continue;
398
-
345
+ if(false === await this.check.enableWhitelist(op)) continue;
399
346
  const admin = await batch.get('admin');
400
347
  const str_value = jsonStringify(op.value);
401
348
  if(null !== admin && null !== str_value &&