trac-peer 0.0.70 → 0.0.71

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,12 +1,13 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.0.70",
4
+ "version": "0.0.71",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.0.45",
8
8
  "b4a": "1.6.7",
9
9
  "bare-fs": "4.0.1",
10
+ "bare-readline": "^1.0.7",
10
11
  "bip39": "^3.1.0",
11
12
  "brittle": "3.0.0",
12
13
  "corestore": "7.0.22",
package/src/contract.js CHANGED
@@ -69,13 +69,15 @@ class Contract {
69
69
  this.storage = storage;
70
70
  this.root = op;
71
71
 
72
+ let _return = null;
73
+
72
74
  if(this.isFeature()) {
73
75
  if(this.features[this.op.type] !== undefined){
74
- await this.features[this.op.type]();
76
+ _return = await this.features[this.op.type]();
75
77
  }
76
78
  } else if(this.isMessage()) {
77
79
  if(this.message_handler !== undefined){
78
- await this.message_handler();
80
+ _return = await this.message_handler();
79
81
  }
80
82
  } else {
81
83
  if(this[this.op.type] !== undefined) {
@@ -84,7 +86,7 @@ class Contract {
84
86
  await this[this.op.type]();
85
87
  }
86
88
  } else {
87
- await this[this.op.type]();
89
+ _return = await this[this.op.type]();
88
90
  }
89
91
  }
90
92
  }
@@ -92,6 +94,8 @@ class Contract {
92
94
  this.address = null;
93
95
  this.is_message = false;
94
96
  this.is_feature = false;
97
+
98
+ return _return;
95
99
  }
96
100
 
97
101
  validateSchema(type, op) {
@@ -135,7 +139,7 @@ class Contract {
135
139
  if(key.startsWith('sh/') || key.startsWith('tx/') || key === 'msgl' || key.startsWith('kcin/') || key.startsWith('delm/') ||
136
140
  key.startsWith('umsg/') || key.startsWith('umsg/') || key.startsWith('msgl/') || key === 'admin' || key === 'auto_add_writers'
137
141
  || key.startsWith('nick/') || key.startsWith('mod/') || key === 'chat_status' || key.startsWith('mtd/') || key === 'delml' ||
138
- key === 'wlst' || key.startsWith('wl/'))
142
+ key === 'wlst' || key === 'txl' || key.startsWith('txi/') || key.startsWith('wl/'))
139
143
  throw Error('put(key,value): ' + key + 'is reserved');
140
144
  return await this.storage.put(key, value);
141
145
  }
package/src/index.js CHANGED
@@ -4,7 +4,7 @@ import Hyperswarm from 'hyperswarm';
4
4
  import ReadyResource from 'ready-resource';
5
5
  import b4a from 'b4a';
6
6
  import Hyperbee from 'hyperbee';
7
- import readline from 'readline';
7
+ import readline from 'bare-readline';
8
8
  import Corestore from 'corestore';
9
9
  import {createHash} from "node:crypto";
10
10
  import w from 'protomux-wakeup';
@@ -88,9 +88,17 @@ export class Peer extends ReadyResource {
88
88
  if (null !== str_dispatch &&
89
89
  null === await batch.get('tx/'+op.key) &&
90
90
  post_tx.value.tx === op.key &&
91
- post_tx.value.ch === createHash('sha256').update(str_dispatch).digest('hex')) {
91
+ post_tx.value.ch === createHash('sha256').update(str_dispatch).digest('hex') &&
92
+ false !== await _this.contract_instance.execute(op, node, batch)) {
93
+ let len = await batch.get('txl');
94
+ if(null === len) {
95
+ len = 0;
96
+ } else {
97
+ len = len.value;
98
+ }
99
+ await batch.put('txi/'+op.key, len);
100
+ await batch.put('txl', len + 1);
92
101
  await batch.put('tx/'+op.key, op.value);
93
- await _this.contract_instance.execute(op, node, batch);
94
102
  console.log(`${op.key} appended. Signed length:`, _this.base.view.core.signedLength);
95
103
  }
96
104
  } else if(op.type === 'msg') {
@@ -116,10 +124,15 @@ export class Peer extends ReadyResource {
116
124
  const str_value = jsonStringify(op.value);
117
125
  const chat_status = await batch.get('chat_status');
118
126
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
119
- if(false === muted && true === whitelisted && null !== str_value && verified &&
120
- null !== chat_status && chat_status.value === 'on' &&
127
+ if(true === verified &&
128
+ false === muted &&
129
+ true === whitelisted &&
130
+ null !== str_value &&
131
+ null !== chat_status &&
121
132
  null === await batch.get('sh/'+op.hash) &&
122
- Buffer.byteLength(str_value) <= 10_2400){
133
+ Buffer.byteLength(str_value) <= 10_2400 &&
134
+ chat_status.value === 'on' &&
135
+ false !== await _this.contract_instance.execute(op, node, batch)){
123
136
  let len = await batch.get('msgl');
124
137
  if(null === len) {
125
138
  len = 0;
@@ -136,11 +149,10 @@ export class Peer extends ReadyResource {
136
149
  await batch.put('umsg/'+user_len, 'msg/'+len);
137
150
  await batch.put('msgl', len + 1);
138
151
  await batch.put('umsgl/'+op.value.dispatch.address, user_len + 1);
139
- await _this.contract_instance.execute(op, node, batch);
152
+ await batch.put('sh/'+op.hash, '');
140
153
  const nick = await batch.get('nick/'+op.value.dispatch.address);
141
154
  console.log(`#${len + 1} | ${nick !== null ? nick.value : op.value.dispatch.address}: ${op.value.dispatch.msg}`);
142
155
  }
143
- await batch.put('sh/'+op.hash, '');
144
156
  } else if (op.type === 'feature') {
145
157
  if(false === this.check.feature(op)) continue;
146
158
  const str_dispatch_value = jsonStringify(op.value.dispatch.value);
@@ -148,12 +160,12 @@ export class Peer extends ReadyResource {
148
160
  if(null !== admin &&
149
161
  null === await batch.get('sh/'+op.value.dispatch.hash)){
150
162
  const verified = _this.wallet.verify(op.value.dispatch.hash, str_dispatch_value + op.value.dispatch.nonce, admin.value);
151
- if(verified) {
163
+ if(true === verified) {
152
164
  await _this.contract_instance.execute(op, node, batch);
165
+ await batch.put('sh/'+op.value.dispatch.hash, '');
153
166
  console.log(`Feature ${op.key} appended`);
154
167
  }
155
168
  }
156
- await batch.put('sh/'+op.value.dispatch.hash, '');
157
169
  } else if (op.type === 'addIndexer') {
158
170
  if(false === this.check.addIndexer(op)) continue;
159
171
  const str_msg = jsonStringify(op.value.msg);
@@ -163,13 +175,13 @@ export class Peer extends ReadyResource {
163
175
  op.value.msg.type === 'addIndexer' &&
164
176
  null === await batch.get('sh/'+op.hash)) {
165
177
  const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
166
- if(verified){
178
+ if(true === verified){
167
179
  const writerKey = b4a.from(op.key, 'hex');
168
180
  await base.addWriter(writerKey);
181
+ await batch.put('sh/'+op.hash, '');
169
182
  console.log(`Indexer added: ${op.key}`);
170
183
  }
171
184
  }
172
- await batch.put('sh/'+op.hash, '');
173
185
  } else if (op.type === 'addWriter') {
174
186
  if(false === this.check.addWriter(op)) continue;
175
187
  const str_msg = jsonStringify(op.value.msg);
@@ -179,13 +191,13 @@ export class Peer extends ReadyResource {
179
191
  op.value.msg.type === 'addWriter' &&
180
192
  null === await batch.get('sh/'+op.hash)) {
181
193
  const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
182
- if(verified){
194
+ if(true === verified){
183
195
  const writerKey = b4a.from(op.key, 'hex');
184
196
  await base.addWriter(writerKey, { isIndexer : false });
197
+ await batch.put('sh/'+op.hash, '');
185
198
  console.log(`Writer added: ${op.key}`);
186
199
  }
187
200
  }
188
- await batch.put('sh/'+op.hash, '');
189
201
  } else if (op.type === 'setChatStatus') {
190
202
  if(false === this.check.setChatStatus(op)) continue;
191
203
  const str_msg = jsonStringify(op.value.msg);
@@ -195,12 +207,12 @@ export class Peer extends ReadyResource {
195
207
  (op.key === 'on' || op.key === 'off') &&
196
208
  null === await batch.get('sh/'+op.hash)) {
197
209
  const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
198
- if(verified){
210
+ if(true === verified){
199
211
  await batch.put('chat_status', op.key);
212
+ await batch.put('sh/'+op.hash, '');
200
213
  console.log(`Set chat_status: ${op.key}`);
201
214
  }
202
215
  }
203
- await batch.put('sh/'+op.hash, '');
204
216
  } else if (op.type === 'setAutoAddWriters') {
205
217
  if(false === this.check.setAutoAddWriters(op)) continue;
206
218
  const str_msg = jsonStringify(op.value.msg);
@@ -210,12 +222,12 @@ export class Peer extends ReadyResource {
210
222
  (op.key === 'on' || op.key === 'off') &&
211
223
  null === await batch.get('sh/'+op.hash)) {
212
224
  const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
213
- if(verified){
225
+ if(true === verified){
214
226
  await batch.put('auto_add_writers', op.key);
227
+ await batch.put('sh/'+op.hash, '');
215
228
  console.log(`Set auto_add_writers: ${op.key}`);
216
229
  }
217
230
  }
218
- await batch.put('sh/'+op.hash, '');
219
231
  } else if (op.type === 'autoAddWriter') {
220
232
  if(false === this.check.key(op)) continue;
221
233
  const auto_add_writers = await batch.get('auto_add_writers');
@@ -238,12 +250,12 @@ export class Peer extends ReadyResource {
238
250
  if(null !== admin && null !== str_value &&
239
251
  null === await batch.get('sh/'+op.hash)){
240
252
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
241
- if(verified) {
253
+ if(true === verified) {
242
254
  await batch.put('admin', op.value.dispatch.admin);
255
+ await batch.put('sh/'+op.hash, '');
243
256
  console.log(`Changed admin ${admin.value} to ${op.value.dispatch.admin}`);
244
257
  }
245
258
  }
246
- await batch.put('sh/'+op.hash, '');
247
259
  } else if(op.type === 'setNick') {
248
260
  if(false === this.check.nick(op)) continue;
249
261
  const taken = await batch.get('kcin/'+op.value.dispatch.nick);
@@ -263,8 +275,11 @@ export class Peer extends ReadyResource {
263
275
  }
264
276
  }
265
277
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
266
- if(null === taken && null !== str_value && ( verified || mod_verified || admin_verified ) &&
267
- null !== chat_status && chat_status.value === 'on' &&
278
+ if(null === taken &&
279
+ null !== str_value &&
280
+ ( true === verified || true === mod_verified || true === admin_verified ) &&
281
+ null !== chat_status &&
282
+ chat_status.value === 'on' &&
268
283
  null === await batch.get('sh/'+op.hash) &&
269
284
  Buffer.byteLength(str_value) <= 256 &&
270
285
  visibleLength(op.value.dispatch.nick) <= 32){
@@ -275,9 +290,9 @@ export class Peer extends ReadyResource {
275
290
  }
276
291
  await batch.put('nick/'+op.value.dispatch.address, op.value.dispatch.nick);
277
292
  await batch.put('kcin/'+op.value.dispatch.nick, op.value.dispatch.address);
293
+ await batch.put('sh/'+op.hash, '');
278
294
  console.log(`Changed nick to ${op.value.dispatch.nick} (${op.value.dispatch.address})`);
279
295
  }
280
- await batch.put('sh/'+op.hash, '');
281
296
  } else if(op.type === 'muteStatus') {
282
297
  if(false === this.check.mute(op)) continue;
283
298
  const admin = await batch.get('admin');
@@ -293,12 +308,12 @@ export class Peer extends ReadyResource {
293
308
  }
294
309
  }
295
310
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
296
- if(verified || mod_verified) {
311
+ if(true === verified || true === mod_verified) {
297
312
  await batch.put('mtd/'+op.value.dispatch.user, op.value.dispatch.muted);
313
+ await batch.put('sh/'+op.hash, '');
298
314
  console.log(`Changed mute status ${op.value.dispatch.user} to ${op.value.dispatch.muted}`);
299
315
  }
300
316
  }
301
- await batch.put('sh/'+op.hash, '');
302
317
  } else if(op.type === 'deleteMessage') {
303
318
  if(false === this.check.deleteMessage(op)) continue;
304
319
  const admin = await batch.get('admin');
@@ -318,7 +333,7 @@ export class Peer extends ReadyResource {
318
333
  user_verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
319
334
  }
320
335
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
321
- if(verified || mod_verified || user_verified) {
336
+ if(true === verified || true === mod_verified || true === user_verified) {
322
337
  message.value.msg = null;
323
338
  message.value.attachments = [];
324
339
  message.value.deleted_by = verified ? admin.value : op.value.dispatch.address;
@@ -331,11 +346,11 @@ export class Peer extends ReadyResource {
331
346
  await batch.put('msg/'+op.value.dispatch.id, message);
332
347
  await batch.put('delm/'+len, op.value.dispatch.id);
333
348
  await batch.put('delml', len + 1);
349
+ await batch.put('sh/'+op.hash, '');
334
350
  console.log(`Deleted message ${op.value.dispatch.id} by user ${message.value.address}`);
335
351
  }
336
352
  }
337
353
  }
338
- await batch.put('sh/'+op.hash, '');
339
354
  } else if(op.type === 'setMod') {
340
355
  if(false === this.check.mod(op)) continue;
341
356
  const admin = await batch.get('admin');
@@ -343,12 +358,12 @@ export class Peer extends ReadyResource {
343
358
  if(null !== admin && null !== str_value &&
344
359
  null === await batch.get('sh/'+op.hash)){
345
360
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
346
- if(verified) {
361
+ if(true === verified) {
347
362
  await batch.put('mod/'+op.value.dispatch.user, op.value.dispatch.mod);
363
+ await batch.put('sh/'+op.hash, '');
348
364
  console.log(`Changed mod status ${op.value.dispatch.user} to ${op.value.dispatch.mod}`);
349
365
  }
350
366
  }
351
- await batch.put('sh/'+op.hash, '');
352
367
  } else if(op.type === 'setWhitelistStatus') {
353
368
  if(false === this.check.whitelistStatus(op)) continue;
354
369
  const admin = await batch.get('admin');
@@ -356,12 +371,12 @@ export class Peer extends ReadyResource {
356
371
  if(null !== admin && null !== str_value &&
357
372
  null === await batch.get('sh/'+op.hash)){
358
373
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
359
- if(verified) {
374
+ if(true === verified) {
360
375
  await batch.put('wl/'+op.value.dispatch.user, op.value.dispatch.status);
376
+ await batch.put('sh/'+op.hash, '');
361
377
  console.log(`Changed whitelist status ${op.value.dispatch.user} to ${op.value.dispatch.status}`);
362
378
  }
363
379
  }
364
- await batch.put('sh/'+op.hash, '');
365
380
  } else if(op.type === 'enableWhitelist') {
366
381
  if(false === this.check.enableWhitelist(op)) continue;
367
382
  const admin = await batch.get('admin');
@@ -369,12 +384,12 @@ export class Peer extends ReadyResource {
369
384
  if(null !== admin && null !== str_value &&
370
385
  null === await batch.get('sh/'+op.hash)){
371
386
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
372
- if(verified) {
387
+ if(true === verified) {
373
388
  await batch.put('wlst', op.value.dispatch.enabled);
389
+ await batch.put('sh/'+op.hash, '');
374
390
  console.log(`Changed whitelist enabled ${op.value.dispatch.enabled}`);
375
391
  }
376
392
  }
377
- await batch.put('sh/'+op.hash, '');
378
393
  }
379
394
  }
380
395