trac-peer 0.1.51 → 0.1.52
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 +5 -4
- package/src/check.js +23 -0
- package/src/functions.js +17 -0
- package/src/index.js +62 -60
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trac-peer",
|
|
3
3
|
"main": "src/index.js",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.52",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"hypercore": "11.6.2",
|
|
8
|
-
"corestore": "7.4.1",
|
|
9
|
-
"autobase": "7.6.2",
|
|
10
7
|
"assert": "npm:bare-node-assert",
|
|
8
|
+
"autobase": "7.6.2",
|
|
11
9
|
"b4a": "1.6.7",
|
|
12
10
|
"bare-assert": "^1.0.2",
|
|
13
11
|
"bare-buffer": "^3.1.2",
|
|
@@ -35,10 +33,12 @@
|
|
|
35
33
|
"bare-worker": "^3.0.0",
|
|
36
34
|
"bare-zlib": "^1.2.5",
|
|
37
35
|
"bip39": "^3.1.0",
|
|
36
|
+
"blind-pairing": "^2.3.1",
|
|
38
37
|
"brittle": "3.0.0",
|
|
39
38
|
"buffer": "npm:bare-node-buffer",
|
|
40
39
|
"child_process": "npm:bare-node-child-process",
|
|
41
40
|
"console": "npm:bare-node-console",
|
|
41
|
+
"corestore": "7.4.1",
|
|
42
42
|
"crypto": "npm:bare-node-crypto",
|
|
43
43
|
"debounceify": "1.1.0",
|
|
44
44
|
"events": "npm:bare-node-events",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"http": "npm:bare-node-http",
|
|
49
49
|
"https": "npm:bare-node-https",
|
|
50
50
|
"hyperbee": "2.24.2",
|
|
51
|
+
"hypercore": "11.6.2",
|
|
51
52
|
"hypercore-crypto": "3.4.0",
|
|
52
53
|
"hyperdht": "^6.20.1",
|
|
53
54
|
"hyperswarm": "4.11.5",
|
package/src/check.js
CHANGED
|
@@ -72,6 +72,7 @@ class Check {
|
|
|
72
72
|
this._msg = this.compileMsg();
|
|
73
73
|
this._feature = this.compileFeature();
|
|
74
74
|
this._add_writer = this.compileAddWriter();
|
|
75
|
+
this._remove_writer = this.compileRemoveWriter();
|
|
75
76
|
this._auto_add_writers = this.compileSetAutoAddWriters();
|
|
76
77
|
this._key = this.compileKey();
|
|
77
78
|
this._chat_status = this.compileSetChatStatus();
|
|
@@ -322,6 +323,23 @@ class Check {
|
|
|
322
323
|
return res === true;
|
|
323
324
|
}
|
|
324
325
|
|
|
326
|
+
compileRemoveWriter (){
|
|
327
|
+
const schema = {
|
|
328
|
+
key: { type : "is_hex" },
|
|
329
|
+
hash : { type : "is_hex" },
|
|
330
|
+
nonce : { type : "string", min : 1, max : 256 },
|
|
331
|
+
value : {
|
|
332
|
+
$$type: "object",
|
|
333
|
+
msg : {
|
|
334
|
+
$$type : "object",
|
|
335
|
+
type : { type : "string", min : 1, max : 256 },
|
|
336
|
+
key: { type : "is_hex" }
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
return this.validator.compile(schema);
|
|
341
|
+
}
|
|
342
|
+
|
|
325
343
|
compileAddWriter (){
|
|
326
344
|
const schema = {
|
|
327
345
|
key: { type : "is_hex" },
|
|
@@ -349,6 +367,11 @@ class Check {
|
|
|
349
367
|
return res === true;
|
|
350
368
|
}
|
|
351
369
|
|
|
370
|
+
removeWriter(op){
|
|
371
|
+
const res = this._remove_writer(op);
|
|
372
|
+
return res === true;
|
|
373
|
+
}
|
|
374
|
+
|
|
352
375
|
compileFeature (){
|
|
353
376
|
const schema = {
|
|
354
377
|
key: { type : "string", min : 1, max : 256 },
|
package/src/functions.js
CHANGED
|
@@ -317,6 +317,23 @@ export async function addWriter(input, peer){
|
|
|
317
317
|
}
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
+
|
|
321
|
+
export async function removeWriter(input, peer){
|
|
322
|
+
const splitted = input.split(' ');
|
|
323
|
+
const parsed = peer.protocol_instance.parseArgs(input)
|
|
324
|
+
const nonce = peer.protocol_instance.generateNonce();
|
|
325
|
+
if(splitted[0] === '/remove_writer') {
|
|
326
|
+
const msg = { type: 'removeWriter', key: ''+parsed.key }
|
|
327
|
+
const signature = {
|
|
328
|
+
msg: msg
|
|
329
|
+
};
|
|
330
|
+
const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
|
|
331
|
+
if(peer.base.writable){
|
|
332
|
+
await peer.base.append({ op : 'remove_writer', type: 'removeWriter', key: ''+parsed.key, value: signature, hash: hash, nonce : nonce });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
320
337
|
export async function tx(input, peer){
|
|
321
338
|
const splitted = peer.protocol_instance.parseArgs(input);
|
|
322
339
|
if(splitted.command === undefined){
|
package/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import readline from 'readline';
|
|
|
9
9
|
import tty from 'tty'
|
|
10
10
|
import Corestore from 'corestore';
|
|
11
11
|
import w from 'protomux-wakeup';
|
|
12
|
+
import BlindPairing from 'blind-pairing'
|
|
12
13
|
const wakeup = new w();
|
|
13
14
|
import {
|
|
14
15
|
addWriter, addAdmin, setAutoAddWriters, setChatStatus, setMod, deleteMessage,
|
|
@@ -54,7 +55,7 @@ export class Peer extends ReadyResource {
|
|
|
54
55
|
this.options = options;
|
|
55
56
|
this.check = new Check();
|
|
56
57
|
this.dhtBootstrap = ['116.202.214.143:10001','116.202.214.149:10001', 'node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737'];
|
|
57
|
-
this.
|
|
58
|
+
this.invite = null;
|
|
58
59
|
this.validator = null;
|
|
59
60
|
this.validator_stream = null;
|
|
60
61
|
this.readline_instance = null;
|
|
@@ -249,6 +250,22 @@ export class Peer extends ReadyResource {
|
|
|
249
250
|
console.log(`Writer added: ${op.key}`);
|
|
250
251
|
}
|
|
251
252
|
}
|
|
253
|
+
} else if (op.type === 'removeWriter') {
|
|
254
|
+
if(false === this.check.removeWriter(op)) continue;
|
|
255
|
+
const str_msg = jsonStringify(op.value.msg);
|
|
256
|
+
const admin = await batch.get('admin');
|
|
257
|
+
if(null !== admin &&
|
|
258
|
+
op.value.msg.key === op.key &&
|
|
259
|
+
op.value.msg.type === 'removeWriter' &&
|
|
260
|
+
null === await batch.get('sh/'+op.hash)) {
|
|
261
|
+
const verified = _this.wallet.verify(op.hash, str_msg + op.nonce, admin.value);
|
|
262
|
+
if(true === verified){
|
|
263
|
+
const writerKey = b4a.from(op.key, 'hex');
|
|
264
|
+
await base.removeWriter(writerKey);
|
|
265
|
+
await batch.put('sh/'+op.hash, '');
|
|
266
|
+
console.log(`Writer removed: ${op.key}`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
252
269
|
} else if (op.type === 'setChatStatus') {
|
|
253
270
|
if(false === this.check.setChatStatus(op)) continue;
|
|
254
271
|
const str_msg = jsonStringify(op.value.msg);
|
|
@@ -568,18 +585,37 @@ export class Peer extends ReadyResource {
|
|
|
568
585
|
}
|
|
569
586
|
});
|
|
570
587
|
this.updater();
|
|
571
|
-
this.seenAutoAdd();
|
|
572
588
|
const auto_add_writers = await this.base.view.get('auto_add_writers');
|
|
573
589
|
if(!this.base.writable && null !== auto_add_writers && auto_add_writers.value === 'on'){
|
|
574
|
-
this.
|
|
575
|
-
op : 'auto-add-writer',
|
|
576
|
-
type : 'autoAddWriter',
|
|
577
|
-
key : this.writerLocalKey,
|
|
578
|
-
id : Math.random() + Date.now()
|
|
579
|
-
});
|
|
590
|
+
await this.pairBlind();
|
|
580
591
|
}
|
|
581
592
|
}
|
|
582
593
|
|
|
594
|
+
async pairBlind(){
|
|
595
|
+
const { invite, publicKey, discoveryKey } = BlindPairing.createInvite(b4a.from(this.writerLocalKey, 'hex'));
|
|
596
|
+
const _this = this;
|
|
597
|
+
|
|
598
|
+
const member = this.invite.addMember({
|
|
599
|
+
discoveryKey,
|
|
600
|
+
async onadd (candidate) {
|
|
601
|
+
candidate.open(publicKey)
|
|
602
|
+
candidate.confirm({ key: b4a.from(_this.writerLocalKey, 'hex') })
|
|
603
|
+
await _this.base.append({ type: 'autoAddWriter', key: _this.writerLocalKey });
|
|
604
|
+
}
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
await member.flushed();
|
|
608
|
+
|
|
609
|
+
const adding = this.invite.addCandidate({
|
|
610
|
+
invite: invite,
|
|
611
|
+
userData : b4a.from(_this.writerLocalKey, 'hex'),
|
|
612
|
+
async onadd (result) { }
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
await adding.pairing;
|
|
616
|
+
console.log('Writer added.');
|
|
617
|
+
}
|
|
618
|
+
|
|
583
619
|
async initContract(){
|
|
584
620
|
this.init_contract_starting = true;
|
|
585
621
|
this.protocol_instance = new this.protocol(this, this.base, this.options);
|
|
@@ -637,25 +673,28 @@ export class Peer extends ReadyResource {
|
|
|
637
673
|
_this.validator_stream = existing_stream;
|
|
638
674
|
_this.validator = validator.value.pub;
|
|
639
675
|
_this.validator_stream.on('close', () => {
|
|
640
|
-
_this.validator_stream
|
|
641
|
-
|
|
642
|
-
|
|
676
|
+
if(_this.validator_stream !== null && b4a.toString(_this.validator_stream.publicKey, 'hex') === validator.value.pub) {
|
|
677
|
+
_this.validator_stream = null;
|
|
678
|
+
_this.validator = null;
|
|
679
|
+
console.log('Validator stream closed', validator.value.pub);
|
|
680
|
+
}
|
|
643
681
|
});
|
|
644
682
|
console.log('Validator stream established', validator.value.pub);
|
|
645
683
|
} else {
|
|
646
684
|
_this.validator_stream = _this.msb.getSwarm().dht.connect(b4a.from(validator.value.pub, 'hex'));
|
|
647
|
-
_this.
|
|
648
|
-
_this.validator = validator.value.pub;
|
|
649
|
-
console.log('Validator stream established', validator.value.pub);
|
|
650
|
-
});
|
|
685
|
+
_this.validator = validator.value.pub;
|
|
651
686
|
_this.validator_stream.on('close', () => {
|
|
652
|
-
_this.validator_stream
|
|
653
|
-
|
|
654
|
-
|
|
687
|
+
if(_this.validator_stream !== null && b4a.toString(_this.validator_stream.publicKey, 'hex') === validator.value.pub) {
|
|
688
|
+
_this.validator_stream = null;
|
|
689
|
+
_this.validator = null;
|
|
690
|
+
console.log('Validator stream closed', validator.value.pub);
|
|
691
|
+
}
|
|
655
692
|
});
|
|
656
693
|
_this.validator_stream.on('error', (error) => {
|
|
657
|
-
_this.validator_stream
|
|
658
|
-
|
|
694
|
+
if(_this.validator_stream !== null && b4a.toString(_this.validator_stream.publicKey, 'hex') === validator.value.pub) {
|
|
695
|
+
_this.validator_stream = null;
|
|
696
|
+
_this.validator = null;
|
|
697
|
+
}
|
|
659
698
|
});
|
|
660
699
|
}
|
|
661
700
|
}
|
|
@@ -742,7 +781,9 @@ export class Peer extends ReadyResource {
|
|
|
742
781
|
};
|
|
743
782
|
|
|
744
783
|
this.swarm = new Hyperswarm({ keyPair, bootstrap: this.dhtBootstrap });
|
|
745
|
-
|
|
784
|
+
this.invite = new BlindPairing(this.swarm, {
|
|
785
|
+
poll: 5000
|
|
786
|
+
});
|
|
746
787
|
console.log(`Writer key: ${this.writerLocalKey}`)
|
|
747
788
|
|
|
748
789
|
this.swarm.on('connection', async (connection, peerInfo) => {
|
|
@@ -759,32 +800,6 @@ export class Peer extends ReadyResource {
|
|
|
759
800
|
|
|
760
801
|
connection.on('error', (error) => { });
|
|
761
802
|
|
|
762
|
-
connection.on('data', async (msg) => {
|
|
763
|
-
try{
|
|
764
|
-
msg = JSON.parse(msg);
|
|
765
|
-
if(msg.op && msg.op === 'auto-add-writer' && this.base.localWriter.isActive &&
|
|
766
|
-
this.writerLocalKey !== msg.key &&
|
|
767
|
-
false === this.base.activeWriters.has(b4a.from(msg.key, 'hex')) &&
|
|
768
|
-
this.seen_auto_add[msg.id] === undefined) {
|
|
769
|
-
if(this.base.writable){
|
|
770
|
-
await this.base.append(msg);
|
|
771
|
-
} else {
|
|
772
|
-
this.seen_auto_add[msg.id] = Date.now();
|
|
773
|
-
this.emit('announce', {
|
|
774
|
-
op : 'auto-add-writer',
|
|
775
|
-
type : 'autoAddWriter',
|
|
776
|
-
key : msg.key,
|
|
777
|
-
id : msg.id
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
} catch(e){ }
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
this.on('announce', async function(msg){
|
|
785
|
-
await connection.write(JSON.stringify(msg))
|
|
786
|
-
});
|
|
787
|
-
|
|
788
803
|
if (!this.isStreaming) {
|
|
789
804
|
this.emit('readyNode');
|
|
790
805
|
}
|
|
@@ -795,19 +810,6 @@ export class Peer extends ReadyResource {
|
|
|
795
810
|
}
|
|
796
811
|
}
|
|
797
812
|
|
|
798
|
-
async seenAutoAdd(){
|
|
799
|
-
while(true){
|
|
800
|
-
const ts = Date.now();
|
|
801
|
-
for(let seen in this.seen_auto_add){
|
|
802
|
-
if(ts - this.seen_auto_add[seen] > 30_000){
|
|
803
|
-
console.log('Wiping last seen', seen);
|
|
804
|
-
delete this.seen_auto_add[seen];
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
await this.sleep(1000);
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
|
|
811
813
|
nodeListener() {
|
|
812
814
|
this.on('readyNode', async () => {
|
|
813
815
|
if (!this.isStreaming) {
|