trac-peer 0.0.98 → 0.1.0
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 +4 -1
- package/src/check.js +13 -2
- package/src/index.js +20 -9
- package/src/protocol.js +4 -4
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
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"trac-wallet": "^0.0.34",
|
|
@@ -25,6 +25,9 @@
|
|
|
25
25
|
"safety-catch": "1.0.2",
|
|
26
26
|
"sodium-native": "^4.3.3",
|
|
27
27
|
"xache": "1.2.0",
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
28
31
|
"bare-assert": "^1.0.2",
|
|
29
32
|
"bare-buffer": "^3.1.2",
|
|
30
33
|
"bare-console": "^6.0.1",
|
package/src/check.js
CHANGED
|
@@ -379,7 +379,15 @@ class Check {
|
|
|
379
379
|
value : {
|
|
380
380
|
$$type: "object",
|
|
381
381
|
tx : { type : "is_hex" },
|
|
382
|
-
ch : { type : "is_hex" }
|
|
382
|
+
ch : { type : "is_hex" },
|
|
383
|
+
ws : { type : "is_hex" },
|
|
384
|
+
wn : { type : "string", min : 1 },
|
|
385
|
+
wp : { type : "is_hex" },
|
|
386
|
+
is : { type : "is_hex" },
|
|
387
|
+
in : { type : "string", min : 1 },
|
|
388
|
+
ipk : { type : "is_hex" },
|
|
389
|
+
w : { type : "is_hex" },
|
|
390
|
+
i : { type : "is_hex" }
|
|
383
391
|
}
|
|
384
392
|
};
|
|
385
393
|
return this.validator.compile(schema);
|
|
@@ -416,7 +424,10 @@ class Check {
|
|
|
416
424
|
$$type: "object",
|
|
417
425
|
dispatch : { type : "object" },
|
|
418
426
|
msbsl : { type : "number", integer : true, min : 0 },
|
|
419
|
-
ipk : { type : "is_hex" }
|
|
427
|
+
ipk : { type : "is_hex" },
|
|
428
|
+
wp : { type : "is_hex" },
|
|
429
|
+
hash : { type : "is_hex" },
|
|
430
|
+
nonce: { type : "string", min : 1 }
|
|
420
431
|
}
|
|
421
432
|
};
|
|
422
433
|
return this.validator.compile(schema);
|
package/src/index.js
CHANGED
|
@@ -81,18 +81,26 @@ export class Peer extends ReadyResource {
|
|
|
81
81
|
const op = node.value;
|
|
82
82
|
if (op.type === 'tx') {
|
|
83
83
|
if(false === this.check.tx(op)) continue;
|
|
84
|
+
while (_this.msb.base.view.core.signedLength < op.value.msbsl) {
|
|
85
|
+
await new Promise( (resolve, reject) => {
|
|
86
|
+
_this.msb.base.view.core.once('append', resolve);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
84
89
|
const msb_view_session = _this.msb.base.view.checkout(op.value.msbsl);
|
|
85
90
|
const post_tx = await msb_view_session.get(op.key);
|
|
86
91
|
await msb_view_session.close();
|
|
87
92
|
if(false === this.check.postTx(post_tx)) continue;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
const content_hash = await _this.createHash('sha256', jsonStringify(op.value.dispatch))
|
|
94
|
+
if (op.key === post_tx.value.tx &&
|
|
95
|
+
null === await batch.get('tx/'+post_tx.value.tx) &&
|
|
96
|
+
post_tx.value.ch === content_hash &&
|
|
97
|
+
_this.wallet.verify(post_tx.value.ws, post_tx.value.tx + post_tx.value.wn, op.value.wp) &&
|
|
98
|
+
_this.wallet.verify(post_tx.value.is, post_tx.value.tx + post_tx.value.in, op.value.ipk) &&
|
|
99
|
+
_this.wallet.verify(op.value.hash, post_tx.value.tx + post_tx.value.ch + op.value.nonce, post_tx.value.ipk) &&
|
|
92
100
|
post_tx.value.tx === await _this.protocol_instance.generateTx(
|
|
93
101
|
_this.bootstrap, _this.msb.bootstrap,
|
|
94
|
-
post_tx.value.w, post_tx.value.i,
|
|
95
|
-
|
|
102
|
+
post_tx.value.w, post_tx.value.i, post_tx.value.ipk,
|
|
103
|
+
post_tx.value.ch, post_tx.value.in
|
|
96
104
|
) &&
|
|
97
105
|
false !== await _this.contract_instance.execute(op, node, batch)) {
|
|
98
106
|
let len = await batch.get('txl');
|
|
@@ -101,10 +109,10 @@ export class Peer extends ReadyResource {
|
|
|
101
109
|
} else {
|
|
102
110
|
len = len.value;
|
|
103
111
|
}
|
|
104
|
-
await batch.put('txi/'+len,
|
|
112
|
+
await batch.put('txi/'+len, post_tx.value.tx);
|
|
105
113
|
await batch.put('txl', len + 1);
|
|
106
|
-
await batch.put('tx/'+
|
|
107
|
-
console.log(`${
|
|
114
|
+
await batch.put('tx/'+post_tx.value.tx, op.value);
|
|
115
|
+
console.log(`${post_tx.value.tx} appended. Signed length:`, _this.base.view.core.signedLength, 'tx length', len + 1);
|
|
108
116
|
}
|
|
109
117
|
} else if(op.type === 'msg') {
|
|
110
118
|
if(false === this.check.msg(op)) continue;
|
|
@@ -500,6 +508,9 @@ export class Peer extends ReadyResource {
|
|
|
500
508
|
msb_tx['dispatch'] = this.protocol_instance.prepared_transactions_content[tx];
|
|
501
509
|
msb_tx['msbsl'] = msbsl;
|
|
502
510
|
msb_tx['ipk'] = this.wallet.publicKey;
|
|
511
|
+
msb_tx['wp'] = msb_tx.value.wp !== undefined ? msb_tx.value.wp : null;
|
|
512
|
+
msb_tx['nonce'] = Math.random() + '-' + Date.now();
|
|
513
|
+
msb_tx['hash'] = this.wallet.sign(tx + await this.createHash('sha256', jsonStringify(msb_tx['dispatch'])) + msb_tx['nonce']);
|
|
503
514
|
delete this.tx_pool[tx];
|
|
504
515
|
delete this.protocol_instance.prepared_transactions_content[tx];
|
|
505
516
|
await this.base.append({ type: 'tx', key: tx, value: msb_tx });
|
package/src/protocol.js
CHANGED
|
@@ -70,8 +70,7 @@ class Protocol{
|
|
|
70
70
|
this.features[key] = feature;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
async generateTx(bootstrap, msb_bootstrap, validator_writer_key, local_writer_key, local_public_key,
|
|
74
|
-
const content_hash = await this.peer.createHash('sha256', jsonStringify(content));
|
|
73
|
+
async generateTx(bootstrap, msb_bootstrap, validator_writer_key, local_writer_key, local_public_key, content_hash, nonce){
|
|
75
74
|
let tx = bootstrap + '-' +
|
|
76
75
|
msb_bootstrap + '-' +
|
|
77
76
|
validator_writer_key + '-' +
|
|
@@ -84,13 +83,14 @@ class Protocol{
|
|
|
84
83
|
|
|
85
84
|
async broadcastTransaction(validator_writer_key, obj){
|
|
86
85
|
if((this.peer.wallet.publicKey !== null &&
|
|
87
|
-
|
|
86
|
+
this.peer.wallet.secretKey !== null) &&
|
|
88
87
|
this.base.localWriter !== null &&
|
|
89
88
|
this.tokenized_input !== null)
|
|
90
89
|
{
|
|
91
90
|
this.nonce = Math.random() + '-' + Date.now();
|
|
92
91
|
const content_hash = await this.peer.createHash('sha256', JSON.stringify(obj));
|
|
93
|
-
let tx = await this.generateTx(this.peer.bootstrap, this.peer.msb.bootstrap, validator_writer_key,
|
|
92
|
+
let tx = await this.generateTx(this.peer.bootstrap, this.peer.msb.bootstrap, validator_writer_key,
|
|
93
|
+
this.peer.writerLocalKey, this.peer.wallet.publicKey, content_hash, this.nonce);
|
|
94
94
|
const signature = this.peer.wallet.sign(tx + this.nonce);
|
|
95
95
|
this.peer.emit('tx', {
|
|
96
96
|
op: 'pre-tx',
|