trac-peer 0.0.53 → 0.0.55

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.53",
4
+ "version": "0.0.55",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.1.1",
package/src/contract.js CHANGED
@@ -21,7 +21,8 @@ class Contract {
21
21
  if(typeof this.storage === "undefined" || this.storage === null) throw new Error('put(key,value): storage undefined');
22
22
  if(key.startsWith('sh/') || key.startsWith('tx/') || key === 'msgl' || key.startsWith('kcin/') || key.startsWith('delm/') ||
23
23
  key.startsWith('msg/') || key === 'admin' || key === 'auto_add_writers' || key.startsWith('nick/') || key.startsWith('mod/') ||
24
- key === 'chat_status' || key.startsWith('mtd/') || key === 'delml') throw Error('put(key,value): ' + key + 'is reserved');
24
+ key === 'chat_status' || key.startsWith('mtd/') || key === 'delml' || key === 'wlst' || key.startsWith('wl/'))
25
+ throw Error('put(key,value): ' + key + 'is reserved');
25
26
  return await this.storage.put(key, value);
26
27
  }
27
28
 
package/src/functions.js CHANGED
@@ -75,7 +75,7 @@ export function restoreManifest(parsedManifest) {
75
75
 
76
76
  if (Array.isArray(parsedManifest.signers)) {
77
77
  parsedManifest.signers = parsedManifest.signers.map(signer => {
78
- if(signer.namespace && signer.namespace.data &&signer.publicKey && signer.publicKey.data){
78
+ if(signer.namespace && signer.namespace.data &&signer.publicKey && signer.publicKey.data){
79
79
  return {
80
80
  ...signer,
81
81
  namespace: Buffer.from(signer.namespace.data),
@@ -103,6 +103,34 @@ export function jsonStringify(value){
103
103
  return null;
104
104
  }
105
105
 
106
+ export async function setWhitelistStatus(input, peer){
107
+ const splitted = yargs(input).parse();
108
+ const value = splitted.user;
109
+ const status = parseInt(splitted.status) === 1;
110
+ const nonce = Math.random() + '-' + Date.now();
111
+ const signature = { dispatch : {
112
+ type : 'setWhitelistStatus',
113
+ user: value,
114
+ status : status,
115
+ address : peer.wallet.publicKey
116
+ }};
117
+ const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
118
+ await peer.base.append({type: 'setWhitelistStatus', value: signature, hash : hash, nonce: nonce });
119
+ }
120
+
121
+ export async function enableWhitelist(input, peer){
122
+ const splitted = yargs(input).parse();
123
+ const value = splitted.enabled === 1;
124
+ const nonce = Math.random() + '-' + Date.now();
125
+ const signature = { dispatch : {
126
+ type : 'enableWhitelist',
127
+ enabled: value,
128
+ address : peer.wallet.publicKey
129
+ }};
130
+ const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
131
+ await peer.base.append({type: 'enableWhitelist', value: signature, hash : hash, nonce: nonce });
132
+ }
133
+
106
134
  export async function deleteMessage(input, peer){
107
135
  const splitted = yargs(input).parse();
108
136
  const value = splitted.id;
@@ -169,19 +197,19 @@ export async function postMessage(input, peer){
169
197
  const value = splitted.message;
170
198
  const nonce = Math.random() + '-' + Date.now();
171
199
  const signature = { dispatch : {
172
- type : 'msg',
173
- msg: value,
174
- address : peer.wallet.publicKey,
175
- attachments : [],
176
- deleted_by : null
177
- }};
200
+ type : 'msg',
201
+ msg: value,
202
+ address : peer.wallet.publicKey,
203
+ attachments : [],
204
+ deleted_by : null
205
+ }};
178
206
  const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
179
207
  await peer.base.append({type: 'msg', value: signature, hash : hash, nonce: nonce });
180
208
  }
181
209
 
182
210
  export async function setChatStatus(input, peer){
183
- const splitted = input.split(' ');
184
- const value = splitted[1];
211
+ const splitted = yargs(input).parse();
212
+ const value = splitted.enabled === 1 ? 'on' : 'off';
185
213
  const nonce = Math.random() + '-' + Date.now();
186
214
  if(value !== 'on' && value !== 'off') throw new Error('setChatStatus: use on and off values.');
187
215
  const msg = { type: 'setChatStatus', key: value }
@@ -193,8 +221,8 @@ export async function setChatStatus(input, peer){
193
221
  }
194
222
 
195
223
  export async function setAutoAddWriters(input, peer){
196
- const splitted = input.split(' ');
197
- const value = splitted[1];
224
+ const splitted = yargs(input).parse();
225
+ const value = splitted.enabled === 1 ? 'on' : 'off';
198
226
  const nonce = Math.random() + '-' + Date.now();
199
227
  if(value !== 'on' && value !== 'off') throw new Error('setAutoAddWriters: use on and off values.');
200
228
  const msg = { type: 'setAutoAddWriters', key: value }
@@ -206,27 +234,28 @@ export async function setAutoAddWriters(input, peer){
206
234
  }
207
235
 
208
236
  export async function addAdmin(input, peer){
209
- const splitted = input.split(' ');
210
- const publicKey = splitted[1];
237
+ const splitted = yargs(input).parse();
238
+ const publicKey = splitted.address;
211
239
  await peer.base.append({ type: 'addAdmin', key: publicKey });
212
240
  }
213
241
 
214
242
  export async function addWriter(input, peer){
215
243
  const splitted = input.split(' ');
244
+ const parsed = yargs(input).parse();
216
245
  const nonce = Math.random() + '-' + Date.now();
217
246
  if(splitted[0] === '/add_indexer'){
218
- const msg = { type: 'addIndexer', key: splitted[splitted.length - 1] }
247
+ const msg = { type: 'addIndexer', key: parsed.key }
219
248
  const signature = {
220
249
  msg: msg
221
250
  };
222
251
  const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
223
- peer.emit('announce', { op : 'append_writer', type: 'addIndexer', key: splitted[splitted.length - 1], value: signature, hash: hash, nonce: nonce });
252
+ peer.emit('announce', { op : 'append_writer', type: 'addIndexer', key: parsed.key, value: signature, hash: hash, nonce: nonce });
224
253
  } else if(splitted[0] === '/add_writer') {
225
- const msg = { type: 'addWriter', key: splitted[splitted.length - 1] }
254
+ const msg = { type: 'addWriter', key: parsed.key }
226
255
  const signature = {
227
256
  msg: msg
228
257
  };
229
258
  const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
230
- peer.emit('announce', { op : 'append_writer', type: 'addWriter', key: splitted[splitted.length - 1], value: signature, hash: hash, nonce : nonce });
259
+ peer.emit('announce', { op : 'append_writer', type: 'addWriter', key: parsed.key, value: signature, hash: hash, nonce : nonce });
231
260
  }
232
261
  }
package/src/index.js CHANGED
@@ -10,7 +10,8 @@ import {createHash} from "node:crypto";
10
10
  import w from 'protomux-wakeup';
11
11
  const wakeup = new w();
12
12
  import {addWriter, addAdmin, setAutoAddWriters, setChatStatus, setMod, deleteMessage,
13
- postMessage, jsonStringify, visibleLength, setNick, muteStatus} from "./functions.js";
13
+ enableWhitelist, postMessage, jsonStringify, visibleLength, setNick,
14
+ muteStatus, setWhitelistStatus} from "./functions.js";
14
15
  export {default as Protocol} from "./protocol.js";
15
16
  export {default as Contract} from "./contract.js";
16
17
  export {default as Feature} from "./feature.js";
@@ -100,11 +101,23 @@ export class Peer extends ReadyResource {
100
101
  op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
101
102
  op.nonce === undefined || op.hash === undefined) continue;
102
103
 
104
+ const admin = await batch.get('admin');
103
105
  let muted = false;
104
106
  const mute_status = await batch.get('mtd/'+op.value.dispatch.address);
105
107
  if(null !== mute_status){
106
108
  muted = mute_status.value;
107
109
  }
110
+ const whitelist_status = await batch.get('wlst');
111
+ if(null !== whitelist_status && true === whitelist_status.value) {
112
+ muted = true;
113
+ const whitelisted = await batch.get('wl/'+op.value.dispatch.address);
114
+ if(null !== whitelisted && true === whitelisted.value) {
115
+ muted = false;
116
+ }
117
+ }
118
+ if(null !== admin && admin.value === op.value.dispatch.address) {
119
+ muted = false;
120
+ }
108
121
  const str_value = jsonStringify(op.value);
109
122
  const chat_status = await batch.get('chat_status');
110
123
  const verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.dispatch.address);
@@ -357,6 +370,41 @@ export class Peer extends ReadyResource {
357
370
  }
358
371
  }
359
372
  await batch.put('sh/'+op.hash, '');
373
+ } else if(op.type === 'setWhitelistStatus') {
374
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.user === undefined ||
375
+ typeof op.value.dispatch.user !== "string" || op.value.dispatch.type === undefined ||
376
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
377
+ op.nonce === undefined || op.value.dispatch.status === undefined || typeof op.value.dispatch.status !== 'boolean' ||
378
+ op.hash === undefined) continue;
379
+
380
+ const admin = await batch.get('admin');
381
+ const str_value = jsonStringify(op.value);
382
+ if(null !== admin && null !== str_value &&
383
+ null === await batch.get('sh/'+op.hash)){
384
+ const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
385
+ if(verified) {
386
+ await batch.put('wl/'+op.value.dispatch.user, op.value.dispatch.status);
387
+ console.log(`Changed whitelist status ${op.value.dispatch.user} to ${op.value.dispatch.status}`);
388
+ }
389
+ }
390
+ await batch.put('sh/'+op.hash, '');
391
+ } else if(op.type === 'enableWhitelist') {
392
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.type === undefined ||
393
+ op.value.dispatch.address === undefined || typeof op.value.dispatch.address !== "string" ||
394
+ op.nonce === undefined || op.value.dispatch.enabled === undefined || typeof op.value.dispatch.enabled !== 'boolean' ||
395
+ op.hash === undefined) continue;
396
+
397
+ const admin = await batch.get('admin');
398
+ const str_value = jsonStringify(op.value);
399
+ if(null !== admin && null !== str_value &&
400
+ null === await batch.get('sh/'+op.hash)){
401
+ const verified = _this.wallet.verify(op.hash, str_value + op.nonce, admin.value);
402
+ if(verified) {
403
+ await batch.put('wlst', op.value.dispatch.enabled);
404
+ console.log(`Changed whitelist enabled ${op.value.dispatch.enabled}`);
405
+ }
406
+ }
407
+ await batch.put('sh/'+op.hash, '');
360
408
  }
361
409
  }
362
410
 
@@ -563,16 +611,18 @@ export class Peer extends ReadyResource {
563
611
  });
564
612
 
565
613
  console.log('Node started. Available commands:');
566
- console.log('- /add_admin: Only once on bootstrap node! Enter a wallet public key to assign admin rights to it.');
567
- console.log('- /add_indexer: Only admin. Enter a peer writer key as argument to get included as indexer for this network.');
568
- console.log('- /add_writer: Only admin. Enter a peer writer key as argument to get included as writer.');
569
- console.log('- /set_auto_add_writers: Only admin. Use "on" or "off" as 2nd parameter to allow/disallow peers automatically being added as writers.');
570
- console.log('- /set_chat_status: Only admin. Use "on" or "off" as 2nd parameter to enable/disable the built-in chat system.');
571
- console.log('- /post: Post a message like \'/post --message "Hello"\'. Chat must be enabled.');
572
- console.log('- /set_nick: Change your nickname like this \'/set_nick --nick "Peter"\'. Chat must be enabled. Can be edited by admin and mods using the optional --user <address> flag..');
573
- console.log('- /mute_status: Only admin and mods. Mute or unmute a user by their address like this \'/mute_status --user "<address>" --muted 1\'.');
574
- console.log('- /set_mod: Only admin. Set a user as mod like this \'/set_mod --user "<address>" --mod 1\'.');
575
- console.log('- /delete_message: Delete a messages like \'/delete_message --id 1\'. Chat must be enabled.');
614
+ console.log('- /add_admin: Works only once and only on bootstrap node! Enter a wallet address to assign admin rights: \'/add_admin --address "<address>"\'.');
615
+ console.log('- /add_indexer: Only admin. Enter a peer writer key to get included as indexer for this network: \'/add_indexer --key "<key>"\'.');
616
+ console.log('- /add_writer: Only admin. Enter a peer writer key to get included as writer for this network: \'/add_writer --key "<key>"\'.');
617
+ console.log('- /set_auto_add_writers: Only admin. Allow any peer to join as writer automatically: \'/set_auto_add_writers --enabled 1\'');
618
+ console.log('- /set_chat_status: Only admin. Enable/disable the built-in chat system: \'/set_chat_status --enabled 1\'');
619
+ console.log('- /post: Post a message: \'/post --message "Hello"\'. Chat must be enabled.');
620
+ console.log('- /set_nick: Change your nickname like this \'/set_nick --nick "Peter"\'. Chat must be enabled. Can be edited by admin and mods using the optional --user <address> flag.');
621
+ console.log('- /mute_status: Only admin and mods. Mute or unmute a user by their address: \'/mute_status --user "<address>" --muted 1\'.');
622
+ console.log('- /set_mod: Only admin. Set a user as mod: \'/set_mod --user "<address>" --mod 1\'.');
623
+ console.log('- /delete_message: Delete a messages: \'/delete_message --id 1\'. Chat must be enabled.');
624
+ console.log('- /enable_whitelist: Only admin. Enable/disable chat whitelists: \'/enable_whitelist --enabled 1\'.');
625
+ console.log('- /set_whitelist_status: Only admin. Enable/disable chat whitelists: \'/set_whitelist_status --user "<address>" --status 1\'.');
576
626
  console.log('- /dag: check system properties such as writer key, DAG, etc.');
577
627
  console.log('- /get_keys: prints your public and private keys. Be careful and never share your private key!');
578
628
  console.log('- /exit: Exit the program');
@@ -593,26 +643,34 @@ export class Peer extends ReadyResource {
593
643
  console.log("Secret Key: ", this.wallet.secretKey);
594
644
  break;
595
645
  default:
596
- if (input.startsWith('/add_indexer') || input.startsWith('/add_writer')) {
597
- await addWriter(input, this);
598
- } else if (input.startsWith('/add_admin')) {
599
- await addAdmin(input, this);
600
- } else if (input.startsWith('/set_auto_add_writers')) {
601
- await setAutoAddWriters(input, this);
602
- } else if (input.startsWith('/set_chat_status')) {
603
- await setChatStatus(input, this);
604
- } else if (input.startsWith('/post')) {
605
- await postMessage(input, this);
606
- } else if (input.startsWith('/set_nick')) {
607
- await setNick(input, this);
608
- } else if (input.startsWith('/mute_status')) {
609
- await muteStatus(input, this);
610
- } else if (input.startsWith('/set_mod')) {
611
- await setMod(input, this);
612
- } else if (input.startsWith('/delete_message')) {
613
- await deleteMessage(input, this);
614
- } else {
615
- this.protocol_instance.execute(input);
646
+ try {
647
+ if (input.startsWith('/add_indexer') || input.startsWith('/add_writer')) {
648
+ await addWriter(input, this);
649
+ } else if (input.startsWith('/add_admin')) {
650
+ await addAdmin(input, this);
651
+ } else if (input.startsWith('/set_auto_add_writers')) {
652
+ await setAutoAddWriters(input, this);
653
+ } else if (input.startsWith('/set_chat_status')) {
654
+ await setChatStatus(input, this);
655
+ } else if (input.startsWith('/post')) {
656
+ await postMessage(input, this);
657
+ } else if (input.startsWith('/set_nick')) {
658
+ await setNick(input, this);
659
+ } else if (input.startsWith('/mute_status')) {
660
+ await muteStatus(input, this);
661
+ } else if (input.startsWith('/set_mod')) {
662
+ await setMod(input, this);
663
+ } else if (input.startsWith('/delete_message')) {
664
+ await deleteMessage(input, this);
665
+ } else if (input.startsWith('/enable_whitelist')) {
666
+ await enableWhitelist(input, this);
667
+ } else if (input.startsWith('/set_whitelist_status')) {
668
+ await setWhitelistStatus(input, this);
669
+ } else {
670
+ this.protocol_instance.execute(input);
671
+ }
672
+ } catch(e) {
673
+ console.log('Command failed:', e.message);
616
674
  }
617
675
  }
618
676
  rl.prompt();