trac-peer 0.1.52 → 0.1.53

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +61 -32
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.1.52",
4
+ "version": "0.1.53",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "assert": "npm:bare-node-assert",
@@ -69,7 +69,7 @@
69
69
  "stream": "npm:bare-node-stream",
70
70
  "timers": "npm:bare-node-timers",
71
71
  "tls": "npm:bare-node-tls",
72
- "trac-wallet": "^0.0.42",
72
+ "trac-wallet": "^0.0.43",
73
73
  "tty": "npm:bare-node-tty",
74
74
  "url": "npm:bare-node-url",
75
75
  "util": "npm:bare-node-util",
package/src/index.js CHANGED
@@ -59,6 +59,7 @@ export class Peer extends ReadyResource {
59
59
  this.validator = null;
60
60
  this.validator_stream = null;
61
61
  this.readline_instance = null;
62
+ this.myInvite = null;
62
63
  this.enable_interactive_mode = options.enable_interactive_mode !== false;
63
64
  if(this.enable_interactive_mode !== false){
64
65
  try{
@@ -585,35 +586,6 @@ export class Peer extends ReadyResource {
585
586
  }
586
587
  });
587
588
  this.updater();
588
- const auto_add_writers = await this.base.view.get('auto_add_writers');
589
- if(!this.base.writable && null !== auto_add_writers && auto_add_writers.value === 'on'){
590
- await this.pairBlind();
591
- }
592
- }
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
589
  }
618
590
 
619
591
  async initContract(){
@@ -775,6 +747,8 @@ export class Peer extends ReadyResource {
775
747
  async _replicate() {
776
748
  if (!this.swarm) {
777
749
 
750
+ const _this = this;
751
+
778
752
  const keyPair = {
779
753
  publicKey: b4a.from(this.wallet.publicKey, 'hex'),
780
754
  secretKey: b4a.from(this.wallet.secretKey, 'hex')
@@ -787,15 +761,16 @@ export class Peer extends ReadyResource {
787
761
  console.log(`Writer key: ${this.writerLocalKey}`)
788
762
 
789
763
  this.swarm.on('connection', async (connection, peerInfo) => {
790
- const peerName = b4a.toString(connection.remotePublicKey, 'hex');
791
- this.connectedPeers.add(peerName);
764
+ const remotePublicKey = b4a.toString(connection.remotePublicKey, 'hex');
765
+
766
+ this.connectedPeers.add(remotePublicKey);
792
767
  wakeup.addStream(connection);
793
768
  this.store.replicate(connection);
794
769
  this.connectedNodes++;
795
770
 
796
771
  connection.on('close', () => {
797
772
  this.connectedNodes--;
798
- this.connectedPeers.delete(peerName);
773
+ this.connectedPeers.delete(remotePublicKey);
799
774
  });
800
775
 
801
776
  connection.on('error', (error) => { });
@@ -803,6 +778,60 @@ export class Peer extends ReadyResource {
803
778
  if (!this.isStreaming) {
804
779
  this.emit('readyNode');
805
780
  }
781
+
782
+ if(this.base.writable){
783
+ const auto_add_writers = await this.base.view.get('auto_add_writers');
784
+ if(auto_add_writers.value === 'on'){
785
+ try{
786
+ const { invite, publicKey, discoveryKey } = BlindPairing.createInvite(b4a.from(this.writerLocalKey, 'hex'));
787
+ connection.send(
788
+ b4a.from(jsonStringify({
789
+ invite : b4a.toString(invite, 'hex'),
790
+ publicKey : b4a.toString(publicKey, 'hex'),
791
+ discoveryKey : b4a.toString(discoveryKey, 'hex')
792
+ })));
793
+ }catch(e){}
794
+ }
795
+ }
796
+
797
+ connection.on('message', async (msg) => {
798
+ try{
799
+ const parsed = jsonParse(b4a.toString(msg));
800
+ if(false === this.base.writable && this.myInvite === null && parsed.invite !== undefined){
801
+ this.myInvite = parsed.invite
802
+ connection.send(b4a.from(jsonStringify({
803
+ inviteMyKey : this.writerLocalKey,
804
+ publicKey : parsed.publicKey,
805
+ discoveryKey : parsed.discoveryKey
806
+ })));
807
+ try{
808
+ const adding = _this.invite.addCandidate({
809
+ invite: b4a.from(parsed.invite, 'hex'),
810
+ userData : b4a.from(_this.writerLocalKey, 'hex'),
811
+ async onadd (result) { }
812
+ })
813
+ await adding.pairing;
814
+ this.myInvite = null
815
+ console.log('paired')
816
+ }catch(e){}
817
+ } else if(true === this.base.writable && parsed.inviteMyKey !== undefined){
818
+ const member = _this.invite.addMember({
819
+ discoveryKey : b4a.from(parsed.discoveryKey, 'hex'),
820
+ async onadd (candidate) {
821
+ try{
822
+ candidate.open(b4a.from(parsed.publicKey, 'hex'))
823
+ candidate.confirm({ key: b4a.from(parsed.inviteMyKey, 'hex') })
824
+ await _this.base.append({
825
+ type : 'autoAddWriter',
826
+ key : parsed.inviteMyKey
827
+ });
828
+ }catch(e){}
829
+ }
830
+ })
831
+ await member.flushed();
832
+ }
833
+ }catch(e){}
834
+ });
806
835
  });
807
836
 
808
837
  this.swarm.join(this.channel, { server: true, client: true });