@@ -7,7 +7,9 @@ const {Message, Routines} = require('@dataparty/crypto')
77
88const AuthOp = require ( './op/auth-op' )
99const RosShim = require ( './ros-shim' )
10- const IParty = require ( '../party/iparty' )
10+ //const IParty = require('../party/iparty')
11+
12+ const { Routines} = require ( '@dataparty/crypto' )
1113
1214
1315/**
@@ -38,6 +40,9 @@ class ISocketComms extends EventEmitter {
3840
3941 this . socket = undefined
4042
43+ //this.aesOffer = null
44+ this . aesStream = null
45+
4146 this . _ros = undefined
4247 }
4348
@@ -80,49 +85,58 @@ class ISocketComms extends EventEmitter {
8085 op . run ( ) . then ( ( status ) => {
8186 debug ( status )
8287 debug ( 'authed' )
83- this . emit ( 'open' )
88+ //this.aesOffer = op.offer
89+ this . aesStream = op . stream
8490 this . authed = true
91+ this . emit ( 'open' )
8592 } ) . catch ( error => {
8693 this . authed = false
8794 debug ( 'auth error' , error )
8895 this . emit ( 'close' )
8996 } )
9097 }
9198
92- decrypt ( reply , sender ) {
99+ async decrypt ( reply , sender ) {
100+ if ( this . aesStream ) {
101+ debug ( 'decrypting quantum aes' )
102+
103+ const contentBSON = await this . aesStream . decrypt ( reply . data )
104+ const content = Routines . BSON . parseObject ( new Routines . BSON . BaseParser ( contentBSON ) )
105+
106+ return content
107+
108+ } else {
109+ debug ( 'decrypting classic' )
93110 const replyObj = JSON . parse ( reply . data )
94- let dataPromise = new Promise ( ( resolve , reject ) => {
95- if ( replyObj . enc && replyObj . sig ) {
96- let msg = new Message ( replyObj )
97-
98- return resolve ( msg . decrypt ( this . party . _identity ) . then ( content => {
99- const senderPub = Routines . extractPublicKeys ( msg . enc )
100- debug ( 'sender' , sender , '\tdiscover' , this . discoverRemoteIdentity )
101- if ( this . discoverRemoteIdentity && ! sender ) {
102- debug ( 'discovered remote identity' , senderPub )
103- this . remoteIdentity = {
104- key : {
105- public : senderPub
106- }
107- }
108- sender = this . remoteIdentity
109- }
110- debug ( `sender from - ${ msg . from } ` )
111- debug ( `senderPub - ${ senderPub } ` )
112-
113- if ( senderPub . box != sender . key . public . box || senderPub . sign != sender . key . public . sign ) {
114- return Promise . reject ( 'TRUST - reply is not from expected remote' )
115- }
116-
117- debug ( 'decrypted data' )
118- return content
119- } ) )
120- }
121-
122- reject ( Promise . reject ( 'TRUST - reply is not encrypted' ) )
123- } )
124-
125- return dataPromise
111+
112+ if ( replyObj . enc && replyObj . sig ) {
113+ let msg = new Message ( replyObj )
114+
115+ let content = await msg . decrypt ( this . party . privateIdentity ( ) )
116+
117+ const senderPub = Routines . extractPublicKeys ( msg . enc )
118+ debug ( 'sender' , sender , '\tdiscover' , this . discoverRemoteIdentity )
119+ if ( this . discoverRemoteIdentity && ! sender ) {
120+ debug ( 'discovered remote identity' , senderPub )
121+ this . remoteIdentity = {
122+ key : {
123+ public : senderPub
124+ }
125+ }
126+ sender = this . remoteIdentity
127+ }
128+ debug ( `sender from - ${ msg . from } ` )
129+ debug ( `senderPub - ${ senderPub } ` )
130+
131+ if ( senderPub . box != sender . key . public . box || senderPub . sign != sender . key . public . sign ) {
132+ throw new Error ( 'TRUST - reply is not from expected remote' )
133+ }
134+
135+ debug ( 'decrypted data' )
136+ return content
137+ }
138+ }
139+
126140 }
127141
128142 onmessage ( message ) {
@@ -142,28 +156,37 @@ class ISocketComms extends EventEmitter {
142156 } )
143157 }
144158
145- send ( input ) {
146- debug ( 'send - ' , typeof input , input )
159+ async send ( input ) {
160+ debug ( 'send - ' , typeof input , input )
147161
148- if ( typeof input != 'object' ) {
149- input = JSON . parse ( input )
150- }
162+ if ( typeof input != 'object' ) {
163+ input = JSON . parse ( input )
164+ }
165+
166+ let content = null
151167
152- const content = new Message ( { msg : input } )
168+ if ( this . aesStream ) {
169+ debug ( 'sending quantum aes' )
170+ const contentBSON = Routines . BSON . serializeBSONWithoutOptimiser ( input )
171+ content = await this . aesStream . encrypt ( contentBSON )
172+ } else {
153173
154- return content . encrypt ( this . party . _identity , this . remoteIdentity . key )
155- . then ( JSON . stringify )
156- . then ( this . socket . send . bind ( this . socket ) )
174+ debug ( 'sending classic' )
175+ const msg = new Message ( { msg : input } )
176+ await msg . encrypt ( this . party . _identity , this . remoteIdentity . key )
177+ content = JSON . stringify ( msg )
178+ }
157179
180+ await this . socket . send ( content )
158181 }
159182
160183 get ros ( ) {
161- if ( ! this . _ros ) {
162- this . _ros = new RosShim ( this )
163- this . _ros . connect ( )
164- }
184+ if ( ! this . _ros ) {
185+ this . _ros = new RosShim ( this )
186+ this . _ros . connect ( )
187+ }
165188
166- return this . _ros
189+ return this . _ros
167190 }
168191}
169192
0 commit comments