trac-peer 0.1.5 → 0.1.6
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 +14 -16
- package/src/functions.js +12 -2
- package/src/index.js +4 -4
- package/src/protocol.js +58 -4
package/package.json
CHANGED
package/src/contract.js
CHANGED
|
@@ -9,13 +9,11 @@ class Contract {
|
|
|
9
9
|
this.features = {};
|
|
10
10
|
this.schemata = {};
|
|
11
11
|
this.message_handler = null;
|
|
12
|
-
this.root = null;
|
|
13
12
|
this.address = null;
|
|
14
13
|
this.validator_address = null;
|
|
15
14
|
this.tx = null;
|
|
16
15
|
this.op = null;
|
|
17
16
|
this.value = null;
|
|
18
|
-
this.node = null;
|
|
19
17
|
|
|
20
18
|
this.enter_execute_schema = this.protocol.peer.check.validator.compile({
|
|
21
19
|
value : {
|
|
@@ -28,7 +26,7 @@ class Contract {
|
|
|
28
26
|
});
|
|
29
27
|
|
|
30
28
|
this.tx_schema = this.protocol.peer.check.validator.compile({
|
|
31
|
-
key : { type : "
|
|
29
|
+
key : { type : "is_hex" },
|
|
32
30
|
value : {
|
|
33
31
|
$$type: "object",
|
|
34
32
|
value : {
|
|
@@ -54,7 +52,7 @@ class Contract {
|
|
|
54
52
|
});
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
async execute(op,
|
|
55
|
+
async execute(op, storage){
|
|
58
56
|
|
|
59
57
|
this.address = null;
|
|
60
58
|
this.validator_address = null;
|
|
@@ -63,9 +61,7 @@ class Contract {
|
|
|
63
61
|
this.tx = null;
|
|
64
62
|
this.op = null;
|
|
65
63
|
this.value = null;
|
|
66
|
-
this.node = null;
|
|
67
64
|
this.storage = null;
|
|
68
|
-
this.root = null;
|
|
69
65
|
|
|
70
66
|
if(true !== this.enter_execute_schema(op)) return false;
|
|
71
67
|
|
|
@@ -84,15 +80,14 @@ class Contract {
|
|
|
84
80
|
this.tx = op.type === 'tx' ? op.key : null;
|
|
85
81
|
this.op = op.value.dispatch;
|
|
86
82
|
this.value = this.op.value !== undefined ? this.op.value : null;
|
|
87
|
-
this.node = node;
|
|
88
83
|
this.storage = storage;
|
|
89
|
-
this.root = op;
|
|
90
84
|
|
|
91
85
|
let _return = null;
|
|
92
86
|
|
|
93
87
|
if(this.isFeature()) {
|
|
94
88
|
if(this.features[this.op.type] !== undefined){
|
|
95
|
-
|
|
89
|
+
// feature returns aren't handled by peer, so not necessary to track
|
|
90
|
+
await this.features[this.op.type]();
|
|
96
91
|
}
|
|
97
92
|
} else if(this.isMessage()) {
|
|
98
93
|
if(this.message_handler !== undefined){
|
|
@@ -119,9 +114,7 @@ class Contract {
|
|
|
119
114
|
this.tx = null;
|
|
120
115
|
this.op = null;
|
|
121
116
|
this.value = null;
|
|
122
|
-
this.node = null;
|
|
123
117
|
this.storage = null;
|
|
124
|
-
this.root = null;
|
|
125
118
|
|
|
126
119
|
return _return;
|
|
127
120
|
}
|
|
@@ -155,7 +148,16 @@ class Contract {
|
|
|
155
148
|
return this.is_message;
|
|
156
149
|
}
|
|
157
150
|
|
|
151
|
+
isReservedKey(key){
|
|
152
|
+
return key.startsWith('sh/') || key.startsWith('tx/') || key === 'msgl' || key.startsWith('kcin/') || key.startsWith('delm/') ||
|
|
153
|
+
key.startsWith('umsg/') || key.startsWith('umsg/') || key.startsWith('msgl/') || key === 'admin' || key === 'auto_add_writers'
|
|
154
|
+
|| key.startsWith('nick/') || key.startsWith('mod/') || key === 'chat_status' || key.startsWith('mtd/') || key === 'delml' ||
|
|
155
|
+
key === 'wlst' || key === 'txl' || key.startsWith('txi/') || key.startsWith('wl/');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
158
|
async del(key){
|
|
159
|
+
if(typeof this.storage === "undefined" || this.storage === null) throw new Error('del(key): storage undefined');
|
|
160
|
+
if(this.isReservedKey(key)) throw Error('del(key): ' + key + 'is reserved');
|
|
159
161
|
await this.storage.del(key);
|
|
160
162
|
}
|
|
161
163
|
|
|
@@ -168,11 +170,7 @@ class Contract {
|
|
|
168
170
|
|
|
169
171
|
async put(key, value){
|
|
170
172
|
if(typeof this.storage === "undefined" || this.storage === null) throw new Error('put(key,value): storage undefined');
|
|
171
|
-
if(
|
|
172
|
-
key.startsWith('umsg/') || key.startsWith('umsg/') || key.startsWith('msgl/') || key === 'admin' || key === 'auto_add_writers'
|
|
173
|
-
|| key.startsWith('nick/') || key.startsWith('mod/') || key === 'chat_status' || key.startsWith('mtd/') || key === 'delml' ||
|
|
174
|
-
key === 'wlst' || key === 'txl' || key.startsWith('txi/') || key.startsWith('wl/'))
|
|
175
|
-
throw Error('put(key,value): ' + key + 'is reserved');
|
|
173
|
+
if(this.isReservedKey(key)) throw Error('put(key,value): ' + key + 'is reserved');
|
|
176
174
|
return await this.storage.put(key, value);
|
|
177
175
|
}
|
|
178
176
|
|
package/src/functions.js
CHANGED
|
@@ -296,8 +296,18 @@ export async function addWriter(input, peer){
|
|
|
296
296
|
|
|
297
297
|
export async function tx(input, peer){
|
|
298
298
|
const splitted = peer.protocol_instance.parseArgs(input);
|
|
299
|
-
if(splitted.validator === undefined || splitted.command === undefined){
|
|
299
|
+
if(splitted.sim === undefined && (splitted.validator === undefined || splitted.command === undefined)){
|
|
300
300
|
throw new Error('Missing option. Please use --validator and --command flags.');
|
|
301
|
+
} else if(splitted.sim !== undefined && splitted.command === undefined){
|
|
302
|
+
throw new Error('Missing option. Please use the --command flag when simulating a tx.');
|
|
301
303
|
}
|
|
302
|
-
|
|
304
|
+
let res = false;
|
|
305
|
+
try{
|
|
306
|
+
if(splitted.sim !== undefined && parseInt(splitted.sim) === 1){
|
|
307
|
+
peer.protocol_instance.sim = true;
|
|
308
|
+
}
|
|
309
|
+
res = await peer.protocol_instance.tx(splitted);
|
|
310
|
+
} catch(e){ console.log(e) }
|
|
311
|
+
peer.protocol_instance.sim = false;
|
|
312
|
+
return res;
|
|
303
313
|
}
|
package/src/index.js
CHANGED
|
@@ -100,7 +100,7 @@ export class Peer extends ReadyResource {
|
|
|
100
100
|
post_tx.value.w, post_tx.value.i, post_tx.value.ipk,
|
|
101
101
|
post_tx.value.ch, post_tx.value.in
|
|
102
102
|
) &&
|
|
103
|
-
false !== await _this.contract_instance.execute(op,
|
|
103
|
+
false !== await _this.contract_instance.execute(op, batch)) {
|
|
104
104
|
let len = await batch.get('txl');
|
|
105
105
|
if(null === len) {
|
|
106
106
|
len = 0;
|
|
@@ -143,7 +143,7 @@ export class Peer extends ReadyResource {
|
|
|
143
143
|
null === await batch.get('sh/'+op.hash) &&
|
|
144
144
|
b4a.byteLength(str_value) <= 10_2400 &&
|
|
145
145
|
chat_status.value === 'on' &&
|
|
146
|
-
false !== await _this.contract_instance.execute(op,
|
|
146
|
+
false !== await _this.contract_instance.execute(op, batch)){
|
|
147
147
|
let len = await batch.get('msgl');
|
|
148
148
|
if(null === len) {
|
|
149
149
|
len = 0;
|
|
@@ -172,7 +172,7 @@ export class Peer extends ReadyResource {
|
|
|
172
172
|
null === await batch.get('sh/'+op.value.dispatch.hash)){
|
|
173
173
|
const verified = _this.wallet.verify(op.value.dispatch.hash, str_dispatch_value + op.value.dispatch.nonce, admin.value);
|
|
174
174
|
if(true === verified) {
|
|
175
|
-
await _this.contract_instance.execute(op,
|
|
175
|
+
await _this.contract_instance.execute(op, batch);
|
|
176
176
|
await batch.put('sh/'+op.value.dispatch.hash, '');
|
|
177
177
|
console.log(`Feature ${op.key} appended`);
|
|
178
178
|
}
|
|
@@ -659,7 +659,7 @@ export class Peer extends ReadyResource {
|
|
|
659
659
|
console.log('- /set_whitelist_status | Only admin. Add/remove users to/from the chat whitelist: \'/set_whitelist_status --user "<address>" --status 1\'.');
|
|
660
660
|
console.log(' ');
|
|
661
661
|
console.log('- System Commands:');
|
|
662
|
-
console.log('- /tx | Perform a contract transaction. The validator flag broadcasts the transaction to the desired validator. The command flag contains contract commands in json format: \'/tx --validator "<validator writer key>" --command "<string, content depends on the protocol>"\'');
|
|
662
|
+
console.log('- /tx | Perform a contract transaction. The validator flag broadcasts the transaction to the desired validator. The command flag contains contract commands in json format: \'/tx --validator "<validator writer key>" --command "<string, content depends on the protocol>"\'. To simulate a tx, you must leave out the \'--validator\' flag and instead use \'--sim 1\' instead.');
|
|
663
663
|
console.log('- /dag | check system properties such as writer key, DAG, etc.');
|
|
664
664
|
console.log('- /get_keys | prints your public and private keys. Be careful and never share your private key!');
|
|
665
665
|
console.log('- /exit | Exit the program');
|
package/src/protocol.js
CHANGED
|
@@ -17,6 +17,7 @@ class Protocol{
|
|
|
17
17
|
this.nonce = 0;
|
|
18
18
|
this.prepared_transactions_content = {};
|
|
19
19
|
this.features = {};
|
|
20
|
+
this.sim = false;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
safeBigInt(value) {
|
|
@@ -84,10 +85,36 @@ class Protocol{
|
|
|
84
85
|
return await this.peer.createHash('sha256', await this.peer.createHash('sha256', tx));
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
async simulateTransaction(obj){
|
|
89
|
+
const storage = new SimStorage(this.peer);
|
|
90
|
+
const null_hex = '0000000000000000000000000000000000000000000000000000000000000000';
|
|
91
|
+
let nonce = Math.random() + '-' + Date.now();
|
|
92
|
+
const content_hash = await this.peer.createHash('sha256', JSON.stringify(obj));
|
|
93
|
+
let tx = await this.generateTx(this.peer.bootstrap, this.peer.msb.bootstrap, null_hex,
|
|
94
|
+
this.peer.writerLocalKey, this.peer.wallet.publicKey, content_hash, nonce);
|
|
95
|
+
const op = {
|
|
96
|
+
type : 'tx',
|
|
97
|
+
key : tx,
|
|
98
|
+
value : {
|
|
99
|
+
dispatch : obj,
|
|
100
|
+
value : {
|
|
101
|
+
ipk : this.peer.wallet.publicKey,
|
|
102
|
+
wp : null_hex
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return await this.peer.contract_instance.execute(op, storage);
|
|
107
|
+
}
|
|
108
|
+
|
|
87
109
|
async broadcastTransaction(validator_writer_key, obj){
|
|
110
|
+
if(true === this.sim) {
|
|
111
|
+
return await this.simulateTransaction(obj);
|
|
112
|
+
}
|
|
88
113
|
if(this.peer.wallet.publicKey !== null &&
|
|
89
114
|
this.peer.wallet.secretKey !== null &&
|
|
90
|
-
this.base.localWriter !== null
|
|
115
|
+
this.base.localWriter !== null &&
|
|
116
|
+
obj.type !== undefined &&
|
|
117
|
+
obj.value !== undefined)
|
|
91
118
|
{
|
|
92
119
|
this.nonce = Math.random() + '-' + Date.now();
|
|
93
120
|
const content_hash = await this.peer.createHash('sha256', JSON.stringify(obj));
|
|
@@ -110,6 +137,7 @@ class Protocol{
|
|
|
110
137
|
} else {
|
|
111
138
|
throw Error('broadcastTransaction(writer, obj): Cannot prepare transaction. Please make sure inputs and local writer are set.');
|
|
112
139
|
}
|
|
140
|
+
return true;
|
|
113
141
|
}
|
|
114
142
|
|
|
115
143
|
async tokenizeInput(input){
|
|
@@ -121,9 +149,7 @@ class Protocol{
|
|
|
121
149
|
}
|
|
122
150
|
}
|
|
123
151
|
|
|
124
|
-
async tx(subject){
|
|
125
|
-
throw new Error('Not implemented: Protocol.tx(subject)');
|
|
126
|
-
}
|
|
152
|
+
async tx(subject){ }
|
|
127
153
|
|
|
128
154
|
async customCommand(input){ }
|
|
129
155
|
|
|
@@ -145,4 +171,32 @@ class Protocol{
|
|
|
145
171
|
}
|
|
146
172
|
}
|
|
147
173
|
|
|
174
|
+
class SimStorage{
|
|
175
|
+
constructor(peer) {
|
|
176
|
+
this.peer = peer;
|
|
177
|
+
this.values = {};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async del(key){
|
|
181
|
+
if(this.peer.contract_instance.isReservedKey(key)) throw Error('del(key): ' + key + 'is reserved');
|
|
182
|
+
delete this.values[key];
|
|
183
|
+
return this.peer.contract_instance.emptyPromise();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async get(key){
|
|
187
|
+
if(this.values[key] !== undefined) {
|
|
188
|
+
return this.values[key];
|
|
189
|
+
}
|
|
190
|
+
let result = await this.peer.base.view.get(key);
|
|
191
|
+
if(null === result) return null;
|
|
192
|
+
return result.value;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async put(key, value){
|
|
196
|
+
if(this.peer.contract_instance.isReservedKey(key)) throw Error('put(key,value): ' + key + 'is reserved');
|
|
197
|
+
this.values[key] = value;
|
|
198
|
+
return this.peer.contract_instance.emptyPromise();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
148
202
|
export default Protocol;
|