trac-peer 0.0.39 → 0.0.41

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.39",
4
+ "version": "0.0.41",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.1.1",
@@ -9,20 +9,21 @@
9
9
  "bare-fs": "4.0.1",
10
10
  "bip39": "^3.1.0",
11
11
  "brittle": "3.0.0",
12
- "corestore": "7.0.22",
12
+ "corestore": "7.0.23",
13
13
  "debounceify": "1.1.0",
14
- "trac-wallet": "^0.0.3",
15
14
  "hyperbee": "2.23.0",
16
15
  "hypercore": "11.0.48",
17
16
  "hypercore-crypto": "3.4.0",
18
17
  "hyperdht": "6.19.0",
19
18
  "hyperswarm": "4.8.4",
20
19
  "is-options": "1.0.2",
20
+ "minimist-string": "^1.0.2",
21
21
  "pear-interface": "1.0.0",
22
22
  "protomux-wakeup": "^2.2.1",
23
23
  "ready-resource": "^1.0.0",
24
24
  "safety-catch": "1.0.2",
25
25
  "sodium-native": "^4.3.3",
26
+ "trac-wallet": "^0.0.4",
26
27
  "xache": "1.2.0"
27
28
  },
28
29
  "publishConfig": {
package/src/contract.js CHANGED
@@ -19,7 +19,9 @@ class Contract {
19
19
 
20
20
  async put(key, value){
21
21
  if(typeof this.storage === "undefined" || this.storage === null) throw new Error('put(key,value): storage undefined');
22
- if(key.startsWith('sh/') || key.startsWith('tx/') || key.startsWith('msg/') || key === 'admin') throw Error('put(key,value): ' + key + 'is reserved');
22
+ if(key.startsWith('sh/') || key.startsWith('tx/') || key === 'msgl' ||
23
+ key.startsWith('msg/') || key === 'admin' || key === 'auto_add_writers' ||
24
+ key === 'chat_status') throw Error('put(key,value): ' + key + 'is reserved');
23
25
  return await this.storage.put(key, value);
24
26
  }
25
27
 
package/src/functions.js CHANGED
@@ -1,3 +1,5 @@
1
+ import parseSentence from 'minimist-string';
2
+
1
3
  export function resolveNumberString(number, decimals){
2
4
  number = number + '';
3
5
  decimals = isNaN(decimals) ? 18 : parseInt(decimals);
@@ -88,6 +90,42 @@ export function restoreManifest(parsedManifest) {
88
90
  return parsedManifest;
89
91
  }
90
92
 
93
+ export function jsonStringify(value){
94
+ try {
95
+ return JSON.stringify(value);
96
+ } catch(e){
97
+ console.log(e);
98
+ }
99
+ return null;
100
+ }
101
+
102
+ export async function postMessage(input, peer){
103
+ const splitted = parseSentence(input);
104
+ const value = JSON.parse(splitted.msg);
105
+ const nonce = Math.random() + '-' + Date.now();
106
+ const signature = {
107
+ type : 'msg',
108
+ msg: value,
109
+ address : this.wallet.publicKey,
110
+ attachments : []
111
+ };
112
+ const hash = peer.wallet.sign(JSON.stringify(signature) + nonce);
113
+ await peer.base.append({type: 'msg', value: { dispatch : signature }, hash : hash, nonce: nonce });
114
+ }
115
+
116
+ export async function setChatStatus(input, peer){
117
+ const splitted = input.split(' ');
118
+ const value = splitted[1];
119
+ const nonce = Math.random() + '-' + Date.now();
120
+ if(value !== 'on' && value !== 'off') throw new Error('setChatStatus: use on and off values.');
121
+ const msg = { type: 'setChatStatus', key: value }
122
+ const signature = {
123
+ msg: msg
124
+ };
125
+ const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
126
+ await peer.base.append({type: 'setChatStatus', key: value, value: signature, hash : hash, nonce: nonce });
127
+ }
128
+
91
129
  export async function setAutoAddWriters(input, peer){
92
130
  const splitted = input.split(' ');
93
131
  const value = splitted[1];
package/src/index.js CHANGED
@@ -9,7 +9,7 @@ import Corestore from 'corestore';
9
9
  import {createHash} from "node:crypto";
10
10
  import w from 'protomux-wakeup';
11
11
  const wakeup = new w();
12
- import {addWriter, addAdmin, setAutoAddWriters} from "./functions.js";
12
+ import {addWriter, addAdmin, setAutoAddWriters, setChatStatus, postMessage, jsonStringify} from "./functions.js";
13
13
  export {default as Protocol} from "./protocol.js";
14
14
  export {default as Contract} from "./contract.js";
15
15
  export {default as Feature} from "./feature.js";
@@ -77,61 +77,76 @@ export class Peer extends ReadyResource {
77
77
  if (op.type === 'tx') {
78
78
  if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined) continue;
79
79
 
80
+ const str_dispatch = jsonStringify(op.value.dispatch);
80
81
  const msb_view_session = _this.msb.base.view.checkout(op.value.msbsl);
81
82
  const post_tx = await msb_view_session.get(op.key);
82
83
  await msb_view_session.close();
83
- if (null !== post_tx &&
84
+ if (null !== str_dispatch &&
85
+ null !== post_tx &&
84
86
  null === await batch.get('tx/'+op.key) &&
85
87
  post_tx.value &&
86
88
  post_tx.value.tx &&
87
89
  op.key === post_tx.value.tx &&
88
- post_tx.value.ch === createHash('sha256').update(JSON.stringify(op.value.dispatch)).digest('hex')) {
90
+ post_tx.value.ch === createHash('sha256').update(str_dispatch).digest('hex')) {
89
91
  await batch.put('tx/'+op.key, op.value);
90
92
  await _this.contract_instance.dispatch(op, node, batch);
91
93
  console.log(`${op.key} appended`);
92
94
  }
93
95
  } else if(op.type === 'msg') {
94
- if(op.key === undefined || op.value === undefined || op.value.msg === undefined ||
95
- op.value.address === undefined || op.nonce === undefined || op.hash === undefined) continue;
96
-
97
- const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, op.value.address);
98
- if(verified &&
96
+ if(op.value === undefined || op.value.dispatch === undefined || op.value.dispatch.attachments === undefined ||
97
+ !Array.isArray(op.value.dispatch.attachments) || op.value.dispatch.msg === undefined || op.value.dispatch.type === undefined ||
98
+ op.value.dispatch.address === undefined || op.nonce === undefined || op.hash === undefined) continue;
99
+
100
+ const str_value = jsonStringify(op.value);
101
+ const chat_status = await batch.get('chat_status');
102
+ const verified = _this.wallet.verify(op.hash, str_value + op.nonce, op.value.address);
103
+ if(null !== str_value && verified &&
104
+ null !== chat_status && chat_status.value === 'on' &&
99
105
  null === await batch.get('sh/'+op.hash) &&
100
- Buffer.byteLength(JSON.stringify(op.value)) <= 4_096){
101
-
102
- await batch.put('msg/'+0, op.value);
106
+ Buffer.byteLength(str_value) <= 10_2400){
107
+ let len = await batch.get('msgl');
108
+ if(null === len) {
109
+ len = 0;
110
+ } else {
111
+ len = len.value;
112
+ }
113
+ await batch.put('msg/'+len, op.value.dispatch);
114
+ await batch.put('msgl', len + 1);
115
+ await _this.contract_instance.dispatch(op, node, batch);
116
+ console.log(`#${len + 1} | ${op.value.dispatch.address}: ${op.value.dispatch.msg}`);
103
117
  }
104
118
  await batch.put('sh/'+op.hash, '');
105
- console.log(`Message ${op.key} appended`);
106
119
  } else if (op.type === 'feature') {
107
120
  if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined ||
108
121
  op.value.dispatch.hash === undefined || op.value.dispatch.value === undefined ||
109
122
  op.value.dispatch.nonce === undefined) continue;
110
123
 
124
+ const str_dispatch_value = jsonStringify(op.value.dispatch.value);
111
125
  const admin = await batch.get('admin');
112
126
  if(null !== admin &&
113
127
  typeof op.value.dispatch === "object" &&
114
128
  typeof op.value.dispatch.hash === "string" &&
115
129
  typeof op.value.dispatch.value !== "undefined" &&
116
130
  null === await batch.get('sh/'+op.value.dispatch.hash)){
117
- const verified = _this.wallet.verify(op.value.dispatch.hash, JSON.stringify(op.value.dispatch.value) + op.value.dispatch.nonce, admin.value);
131
+ const verified = _this.wallet.verify(op.value.dispatch.hash, str_dispatch_value + op.value.dispatch.nonce, admin.value);
118
132
  if(verified) {
119
133
  await _this.contract_instance.dispatch(op, node, batch);
134
+ console.log(`Feature ${op.key} appended`);
120
135
  }
121
136
  }
122
137
  await batch.put('sh/'+op.value.dispatch.hash, '');
123
- console.log(`Feature ${op.key} appended`);
124
138
  } else if (op.type === 'addIndexer') {
125
139
  if(op.key === undefined || op.value === undefined || op.hash === undefined ||
126
140
  op.value.msg === undefined || op.value.msg.key === undefined ||
127
141
  op.value.msg.type === undefined || op.nonce === undefined) continue;
128
142
 
143
+ const str_msg = jsonStringify(op.value.msg);
129
144
  const admin = await batch.get('admin');
130
145
  if(null !== admin &&
131
146
  op.value.msg.key === op.key &&
132
147
  op.value.msg.type === 'addIndexer' &&
133
148
  null === await batch.get('sh/'+op.hash)) {
134
- const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, admin.value);
149
+ const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
135
150
  if(verified){
136
151
  const writerKey = b4a.from(op.key, 'hex');
137
152
  await base.addWriter(writerKey);
@@ -144,12 +159,13 @@ export class Peer extends ReadyResource {
144
159
  op.value.msg === undefined || op.value.msg.key === undefined ||
145
160
  op.value.msg.type === undefined || op.nonce === undefined) continue;
146
161
 
162
+ const str_msg = jsonStringify(op.value.msg);
147
163
  const admin = await batch.get('admin');
148
164
  if(null !== admin &&
149
165
  op.value.msg.key === op.key &&
150
166
  op.value.msg.type === 'addWriter' &&
151
167
  null === await batch.get('sh/'+op.hash)) {
152
- const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, admin.value);
168
+ const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
153
169
  if(verified){
154
170
  const writerKey = b4a.from(op.key, 'hex');
155
171
  await base.addWriter(writerKey, { isIndexer : false });
@@ -157,17 +173,36 @@ export class Peer extends ReadyResource {
157
173
  }
158
174
  }
159
175
  await batch.put('sh/'+op.hash, '');
176
+ } else if (op.type === 'setChatStatus') {
177
+ if(op.key === undefined || op.value === undefined || op.hash === undefined ||
178
+ op.value.msg === undefined || op.value.msg.key === undefined ||
179
+ op.nonce === undefined || op.value.msg.type === undefined) continue;
180
+
181
+ const str_msg = jsonStringify(op.value.msg);
182
+ const admin = await batch.get('admin');
183
+ if(null !== admin && op.value.msg.key === op.key &&
184
+ op.value.msg.type === 'setChatStatus' &&
185
+ (op.key === 'on' || op.key === 'off') &&
186
+ null === await batch.get('sh/'+op.hash)) {
187
+ const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
188
+ if(verified){
189
+ await batch.put('chat_status', op.key);
190
+ console.log(`Set chat_status: ${op.key}`);
191
+ }
192
+ }
193
+ await batch.put('sh/'+op.hash, '');
160
194
  } else if (op.type === 'setAutoAddWriters') {
161
195
  if(op.key === undefined || op.value === undefined || op.hash === undefined ||
162
196
  op.value.msg === undefined || op.value.msg.key === undefined ||
163
197
  op.nonce === undefined || op.value.msg.type === undefined) continue;
164
198
 
199
+ const str_msg = jsonStringify(op.value.msg);
165
200
  const admin = await batch.get('admin');
166
201
  if(null !== admin && op.value.msg.key === op.key &&
167
202
  op.value.msg.type === 'setAutoAddWriters' &&
168
203
  (op.key === 'on' || op.key === 'off') &&
169
204
  null === await batch.get('sh/'+op.hash)) {
170
- const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, admin.value);
205
+ const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
171
206
  if(verified){
172
207
  await batch.put('auto_add_writers', op.key);
173
208
  console.log(`Set auto_add_writers: ${op.key}`);
@@ -395,12 +430,14 @@ export class Peer extends ReadyResource {
395
430
  });
396
431
 
397
432
  console.log('Node started. Available commands:');
398
- console.log('- /add_admin: only once on bootstrap node: enter a wallet public key to assign admin rights to it. Admin rights are required to set indexers and writers.');
399
- console.log('- /add_indexer: enter a peer writer key as argument to get included as indexer for this network.');
400
- console.log('- /add_writer: enter a peer writer key as argument to get included as writer.');
401
- console.log('- /set_auto_add_writers: use "on" or "off" as 2nd parameter to allow/disallow peers automatically being added as writers.');
433
+ console.log('- /add_admin: Only once on bootstrap node! Enter a wallet public key to assign admin rights to it.');
434
+ console.log('- /add_indexer: Only admin. Enter a peer writer key as argument to get included as indexer for this network.');
435
+ console.log('- /add_writer: Only admin. Enter a peer writer key as argument to get included as writer.');
436
+ console.log('- /set_auto_add_writers: Only admin. Use "on" or "off" as 2nd parameter to allow/disallow peers automatically being added as writers.');
437
+ console.log('- /set_status: Only admin. Use "on" or "off" as 2nd parameter to enable/disable the built-in chat system.');
438
+ console.log('- /post_message: Post a message in json format. Chat must be enabled.');
402
439
  console.log('- /dag: check system properties such as writer key, DAG, etc.');
403
- console.log('- /get_keys: prints the signing key pair');
440
+ console.log('- /get_keys: prints your key pair. Be careful and never share!');
404
441
  console.log('- /exit: Exit the program');
405
442
  this.protocol_instance.printOptions();
406
443
 
@@ -425,6 +462,10 @@ export class Peer extends ReadyResource {
425
462
  await addAdmin(input, this);
426
463
  } else if (input.startsWith('/set_auto_add_writers')) {
427
464
  await setAutoAddWriters(input, this);
465
+ } else if (input.startsWith('/set_chat_status')) {
466
+ await setChatStatus(input, this);
467
+ } else if (input.startsWith('/post_message')) {
468
+ await postMessage(input, this);
428
469
  } else {
429
470
  this.protocol_instance.execute(input);
430
471
  }