trac-peer 0.0.20 → 0.0.22
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 +1 -1
- package/src/contract.js +1 -0
- package/src/feature.js +2 -1
- package/src/functions.js +3 -5
- package/src/index.js +20 -8
- package/src/protocol.js +1 -1
package/package.json
CHANGED
package/src/contract.js
CHANGED
|
@@ -19,6 +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
23
|
return await this.storage.put(key, value);
|
|
23
24
|
}
|
|
24
25
|
}
|
package/src/feature.js
CHANGED
package/src/functions.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import BlindPairing from "blind-pairing";
|
|
2
|
-
|
|
3
1
|
export function resolveNumberString(number, decimals){
|
|
4
2
|
|
|
5
3
|
let splitted = number.split(".");
|
|
@@ -92,7 +90,7 @@ export async function setAutoAddWriters(input, peer){
|
|
|
92
90
|
const splitted = input.split(' ');
|
|
93
91
|
const value = splitted[1];
|
|
94
92
|
if(value !== 'on' && value !== 'off') throw new Error('setAutoAddWriters: use on and off values.');
|
|
95
|
-
const msg = { type: 'setAutoAddWriters', key: value }
|
|
93
|
+
const msg = { type: 'setAutoAddWriters', key: value, nonce : Math.random() + '-' + Date.now() }
|
|
96
94
|
const signature = {
|
|
97
95
|
msg: msg
|
|
98
96
|
};
|
|
@@ -110,7 +108,7 @@ export async function addAdmin(input, peer){
|
|
|
110
108
|
export async function addWriter(input, peer){
|
|
111
109
|
const splitted = input.split(' ');
|
|
112
110
|
if(splitted[0] === '/add_indexer'){
|
|
113
|
-
const msg = { type: 'addIndexer', key: splitted[splitted.length - 1] }
|
|
111
|
+
const msg = { type: 'addIndexer', key: splitted[splitted.length - 1], nonce : Math.random() + '-' + Date.now() }
|
|
114
112
|
const signature = {
|
|
115
113
|
msg: msg
|
|
116
114
|
};
|
|
@@ -118,7 +116,7 @@ export async function addWriter(input, peer){
|
|
|
118
116
|
signature['hash'] = hash;
|
|
119
117
|
peer.emit('announce', { op : 'append_writer', type: 'addIndexer', key: splitted[splitted.length - 1], value: signature });
|
|
120
118
|
} else if(splitted[0] === '/add_writer') {
|
|
121
|
-
const msg = { type: 'addWriter', key: splitted[splitted.length - 1] }
|
|
119
|
+
const msg = { type: 'addWriter', key: splitted[splitted.length - 1], nonce : Math.random() + '-' + Date.now() }
|
|
122
120
|
const signature = {
|
|
123
121
|
msg: msg
|
|
124
122
|
};
|
package/src/index.js
CHANGED
|
@@ -77,10 +77,10 @@ export class Peer extends ReadyResource {
|
|
|
77
77
|
const post_tx = await msb_view_session.get(op.key);
|
|
78
78
|
await msb_view_session.close();
|
|
79
79
|
if (null !== post_tx &&
|
|
80
|
-
null === await view.get(op.key) &&
|
|
80
|
+
null === await view.get('tx/'+op.key) &&
|
|
81
81
|
op.key === post_tx.value.tx &&
|
|
82
82
|
post_tx.value.ch === createHash('sha256').update(JSON.stringify(op.value.dispatch)).digest('hex')) {
|
|
83
|
-
await view.put(op.key, op.value);
|
|
83
|
+
await view.put('tx/'+op.key, op.value);
|
|
84
84
|
await _this.contract_instance.dispatch(op, node, view);
|
|
85
85
|
console.log(`${op.key} appended`);
|
|
86
86
|
}
|
|
@@ -89,43 +89,55 @@ export class Peer extends ReadyResource {
|
|
|
89
89
|
if(null !== admin &&
|
|
90
90
|
typeof op.value.dispatch === "object" &&
|
|
91
91
|
typeof op.value.dispatch.hash === "string" &&
|
|
92
|
-
typeof op.value.dispatch.value !== "undefined"
|
|
92
|
+
typeof op.value.dispatch.value !== "undefined" &&
|
|
93
|
+
null === await view.get('sh/'+op.value.dispatch.hash)){
|
|
93
94
|
const verified = _this.wallet.verify(op.value.dispatch.hash, JSON.stringify(op.value.dispatch.value), admin.value);
|
|
94
|
-
if(verified){
|
|
95
|
+
if(verified) {
|
|
95
96
|
await _this.contract_instance.dispatch(op, node, view);
|
|
96
97
|
}
|
|
98
|
+
await view.put('sh/'+op.value.dispatch.hash, '');
|
|
97
99
|
}
|
|
98
100
|
console.log(`Feature ${op.key} appended`);
|
|
99
101
|
} else if (op.type === 'addIndexer') {
|
|
100
102
|
const admin = await view.get('admin');
|
|
101
|
-
if(null !== admin &&
|
|
103
|
+
if(null !== admin &&
|
|
104
|
+
op.value.msg.key === op.key &&
|
|
105
|
+
op.value.msg.type === 'addIndexer' &&
|
|
106
|
+
null === await view.get('sh/'+op.op.value.hash)) {
|
|
102
107
|
const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
|
|
103
108
|
if(verified){
|
|
104
109
|
const writerKey = b4a.from(op.key, 'hex');
|
|
105
110
|
await base.addWriter(writerKey);
|
|
106
111
|
console.log(`Indexer added: ${op.key}`);
|
|
107
112
|
}
|
|
113
|
+
await view.put('sh/'+op.op.value.hash, '');
|
|
108
114
|
}
|
|
109
115
|
} else if (op.type === 'addWriter') {
|
|
110
116
|
const admin = await view.get('admin');
|
|
111
|
-
if(null !== admin &&
|
|
117
|
+
if(null !== admin &&
|
|
118
|
+
op.value.msg.key === op.key &&
|
|
119
|
+
op.value.msg.type === 'addWriter' &&
|
|
120
|
+
null === await view.get('sh/'+op.op.value.hash)) {
|
|
112
121
|
const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
|
|
113
122
|
if(verified){
|
|
114
123
|
const writerKey = b4a.from(op.key, 'hex');
|
|
115
124
|
await base.addWriter(writerKey, { isIndexer : false });
|
|
116
125
|
console.log(`Writer added: ${op.key}`);
|
|
117
126
|
}
|
|
127
|
+
await view.put('sh/'+op.op.value.hash, '');
|
|
118
128
|
}
|
|
119
129
|
} else if (op.type === 'setAutoAddWriters') {
|
|
120
130
|
const admin = await view.get('admin');
|
|
121
131
|
if(null !== admin && op.value.msg.key === op.key &&
|
|
122
132
|
op.value.msg.type === 'setAutoAddWriters' &&
|
|
123
|
-
(op.key === 'on' || op.key === 'off')
|
|
133
|
+
(op.key === 'on' || op.key === 'off') &&
|
|
134
|
+
null === await view.get('sh/'+op.op.value.hash)) {
|
|
124
135
|
const verified = _this.wallet.verify(op.value.hash, JSON.stringify(op.value.msg), admin.value);
|
|
125
136
|
if(verified){
|
|
126
137
|
await view.put('auto_add_writers', op.key);
|
|
127
138
|
console.log(`Set auto_add_writers: ${op.key}`);
|
|
128
139
|
}
|
|
140
|
+
await view.put('sh/'+op.op.value.hash, '');
|
|
129
141
|
}
|
|
130
142
|
} else if (op.type === 'autoAddWriter') {
|
|
131
143
|
const auto_add_writers = await view.get('auto_add_writers');
|
|
@@ -223,7 +235,7 @@ export class Peer extends ReadyResource {
|
|
|
223
235
|
delete this.protocol_instance.prepared_transactions_content[tx];
|
|
224
236
|
continue;
|
|
225
237
|
}
|
|
226
|
-
const msbsl = this.msb.base.view.core.signedLength;
|
|
238
|
+
const msbsl = this.msb.base.view.core.signedLength + 1;
|
|
227
239
|
const view_session = this.msb.base.view.checkout(msbsl);
|
|
228
240
|
const msb_tx = await view_session.get(tx);
|
|
229
241
|
await view_session.close();
|
package/src/protocol.js
CHANGED
|
@@ -30,7 +30,7 @@ class Protocol{
|
|
|
30
30
|
this.base.localWriter !== null &&
|
|
31
31
|
this.tokenized_input !== null)
|
|
32
32
|
{
|
|
33
|
-
this.nonce = Date.now();
|
|
33
|
+
this.nonce = Math.random() + '-' + Date.now();
|
|
34
34
|
const MSBwriter = writer;
|
|
35
35
|
const content_hash = createHash('sha256').update(JSON.stringify(obj)).digest('hex');
|
|
36
36
|
let tx = createHash('sha256').update(
|