trac-peer 0.0.33 → 0.0.34

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.33",
4
+ "version": "0.0.34",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.0.45",
package/src/contract.js CHANGED
@@ -19,7 +19,7 @@ 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 === 'admin') throw Error('put(key,value): ' + key + 'is reserved');
22
+ if(key.startsWith('sh/') || key.startsWith('tx/') || key.startsWith('msg/') || key === 'admin') throw Error('put(key,value): ' + key + 'is reserved');
23
23
  return await this.storage.put(key, value);
24
24
  }
25
25
 
package/src/feature.js CHANGED
@@ -20,14 +20,15 @@ class Feature {
20
20
  }
21
21
 
22
22
  async append(key, value){
23
- const hash = this.peer.wallet.sign(JSON.stringify(value));
23
+ const nonce = Math.random() + '-' + Date.now();
24
+ const hash = this.peer.wallet.sign(JSON.stringify(value) + nonce);
24
25
  await this.peer.base.append({ type: 'feature', key: this.key + '_' + key, value : {
25
26
  dispatch : {
26
27
  type : this.key + '_feature',
27
28
  key : key,
28
29
  hash : hash,
29
30
  value : value,
30
- nonce : Math.random() + '-' + Date.now()
31
+ nonce : nonce
31
32
  }
32
33
  }});
33
34
  }
package/src/functions.js CHANGED
@@ -91,14 +91,14 @@ export function restoreManifest(parsedManifest) {
91
91
  export async function setAutoAddWriters(input, peer){
92
92
  const splitted = input.split(' ');
93
93
  const value = splitted[1];
94
+ const nonce = Math.random() + '-' + Date.now();
94
95
  if(value !== 'on' && value !== 'off') throw new Error('setAutoAddWriters: use on and off values.');
95
- const msg = { type: 'setAutoAddWriters', key: value, nonce : Math.random() + '-' + Date.now() }
96
+ const msg = { type: 'setAutoAddWriters', key: value }
96
97
  const signature = {
97
98
  msg: msg
98
99
  };
99
- const hash = peer.wallet.sign(JSON.stringify(msg));
100
- signature['hash'] = hash;
101
- await peer.base.append({type: 'setAutoAddWriters', key: value, value: signature });
100
+ const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
101
+ await peer.base.append({type: 'setAutoAddWriters', key: value, value: signature, hash : hash, nonce: nonce });
102
102
  }
103
103
 
104
104
  export async function addAdmin(input, peer){
@@ -109,21 +109,20 @@ export async function addAdmin(input, peer){
109
109
 
110
110
  export async function addWriter(input, peer){
111
111
  const splitted = input.split(' ');
112
+ const nonce = Math.random() + '-' + Date.now();
112
113
  if(splitted[0] === '/add_indexer'){
113
- const msg = { type: 'addIndexer', key: splitted[splitted.length - 1], nonce : Math.random() + '-' + Date.now() }
114
+ const msg = { type: 'addIndexer', key: splitted[splitted.length - 1] }
114
115
  const signature = {
115
116
  msg: msg
116
117
  };
117
- const hash = peer.wallet.sign(JSON.stringify(msg));
118
- signature['hash'] = hash;
119
- peer.emit('announce', { op : 'append_writer', type: 'addIndexer', key: splitted[splitted.length - 1], value: signature });
118
+ const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
119
+ peer.emit('announce', { op : 'append_writer', type: 'addIndexer', key: splitted[splitted.length - 1], value: signature, hash: hash, nonce: nonce });
120
120
  } else if(splitted[0] === '/add_writer') {
121
- const msg = { type: 'addWriter', key: splitted[splitted.length - 1], nonce : Math.random() + '-' + Date.now() }
121
+ const msg = { type: 'addWriter', key: splitted[splitted.length - 1] }
122
122
  const signature = {
123
123
  msg: msg
124
124
  };
125
- const hash = peer.wallet.sign(JSON.stringify(msg));
126
- signature['hash'] = hash;
127
- peer.emit('announce', { op : 'append_writer', type: 'addWriter', key: splitted[splitted.length - 1], value: signature });
125
+ const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
126
+ peer.emit('announce', { op : 'append_writer', type: 'addWriter', key: splitted[splitted.length - 1], value: signature, hash: hash, nonce : nonce });
128
127
  }
129
128
  }
package/src/index.js CHANGED
@@ -90,9 +90,23 @@ export class Peer extends ReadyResource {
90
90
  await _this.contract_instance.dispatch(op, node, batch);
91
91
  console.log(`${op.key} appended`);
92
92
  }
93
+ } 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 &&
99
+ 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);
103
+ }
104
+ await batch.put('sh/'+op.hash, '');
105
+ console.log(`Message ${op.key} appended`);
93
106
  } else if (op.type === 'feature') {
94
107
  if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined ||
95
- op.value.dispatch.hash === undefined || op.value.dispatch.value === undefined) continue;
108
+ op.value.dispatch.hash === undefined || op.value.dispatch.value === undefined ||
109
+ op.value.dispatch.nonce === undefined) continue;
96
110
 
97
111
  const admin = await batch.get('admin');
98
112
  if(null !== admin &&
@@ -100,63 +114,66 @@ export class Peer extends ReadyResource {
100
114
  typeof op.value.dispatch.hash === "string" &&
101
115
  typeof op.value.dispatch.value !== "undefined" &&
102
116
  null === await batch.get('sh/'+op.value.dispatch.hash)){
103
- const verified = _this.wallet.verify(op.value.dispatch.hash, JSON.stringify(op.value.dispatch.value), admin.value);
117
+ const verified = _this.wallet.verify(op.value.dispatch.hash, JSON.stringify(op.value.dispatch.value) + op.value.dispatch.nonce, admin.value);
104
118
  if(verified) {
105
119
  await _this.contract_instance.dispatch(op, node, batch);
106
120
  }
107
- await batch.put('sh/'+op.value.dispatch.hash, '');
108
121
  }
122
+ await batch.put('sh/'+op.value.dispatch.hash, '');
109
123
  console.log(`Feature ${op.key} appended`);
110
124
  } else if (op.type === 'addIndexer') {
111
- if(op.key === undefined || op.value === undefined || op.value.hash === undefined ||
112
- op.value.msg === undefined || op.value.msg.key === undefined || op.value.msg.type === undefined) continue;
125
+ if(op.key === undefined || op.value === undefined || op.hash === undefined ||
126
+ op.value.msg === undefined || op.value.msg.key === undefined ||
127
+ op.value.msg.type === undefined || op.nonce === undefined) continue;
113
128
 
114
129
  const admin = await batch.get('admin');
115
130
  if(null !== admin &&
116
131
  op.value.msg.key === op.key &&
117
132
  op.value.msg.type === 'addIndexer' &&
118
- null === await batch.get('sh/'+op.value.hash)) {
119
- const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
133
+ null === await batch.get('sh/'+op.hash)) {
134
+ const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, admin.value);
120
135
  if(verified){
121
136
  const writerKey = b4a.from(op.key, 'hex');
122
137
  await base.addWriter(writerKey);
123
138
  console.log(`Indexer added: ${op.key}`);
124
139
  }
125
- await batch.put('sh/'+op.value.hash, '');
126
140
  }
141
+ await batch.put('sh/'+op.hash, '');
127
142
  } else if (op.type === 'addWriter') {
128
- if(op.key === undefined || op.value === undefined || op.value.hash === undefined ||
129
- op.value.msg === undefined || op.value.msg.key === undefined || op.value.msg.type === undefined) continue;
143
+ if(op.key === undefined || op.hash === undefined || op.value === undefined ||
144
+ op.value.msg === undefined || op.value.msg.key === undefined ||
145
+ op.value.msg.type === undefined || op.nonce === undefined) continue;
130
146
 
131
147
  const admin = await batch.get('admin');
132
148
  if(null !== admin &&
133
149
  op.value.msg.key === op.key &&
134
150
  op.value.msg.type === 'addWriter' &&
135
- null === await batch.get('sh/'+op.value.hash)) {
136
- const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
151
+ null === await batch.get('sh/'+op.hash)) {
152
+ const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, admin.value);
137
153
  if(verified){
138
154
  const writerKey = b4a.from(op.key, 'hex');
139
155
  await base.addWriter(writerKey, { isIndexer : false });
140
156
  console.log(`Writer added: ${op.key}`);
141
157
  }
142
- await batch.put('sh/'+op.value.hash, '');
143
158
  }
159
+ await batch.put('sh/'+op.hash, '');
144
160
  } else if (op.type === 'setAutoAddWriters') {
145
- if(op.key === undefined || op.value === undefined || op.value.hash === undefined ||
146
- op.value.msg === undefined || op.value.msg.key === undefined || op.value.msg.type === undefined) continue;
161
+ if(op.key === undefined || op.value === undefined || op.hash === undefined ||
162
+ op.value.msg === undefined || op.value.msg.key === undefined ||
163
+ op.nonce === undefined || op.value.msg.type === undefined) continue;
147
164
 
148
165
  const admin = await batch.get('admin');
149
166
  if(null !== admin && op.value.msg.key === op.key &&
150
167
  op.value.msg.type === 'setAutoAddWriters' &&
151
168
  (op.key === 'on' || op.key === 'off') &&
152
- null === await batch.get('sh/'+op.value.hash)) {
153
- const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
169
+ null === await batch.get('sh/'+op.hash)) {
170
+ const verified = _this.wallet.verify(op.hash, JSON.stringify(op.value.msg) + op.nonce, admin.value);
154
171
  if(verified){
155
172
  await batch.put('auto_add_writers', op.key);
156
173
  console.log(`Set auto_add_writers: ${op.key}`);
157
174
  }
158
- await batch.put('sh/'+op.value.hash, '');
159
175
  }
176
+ await batch.put('sh/'+op.hash, '');
160
177
  } else if (op.type === 'autoAddWriter') {
161
178
  if(op.key === undefined) continue;
162
179
  const auto_add_writers = await batch.get('auto_add_writers');