@@ -7,7 +7,7 @@ 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+
1111
1212
1313/**
@@ -38,6 +38,9 @@ class ISocketComms extends EventEmitter {
3838
3939 this . socket = undefined
4040
41+ //this.aesOffer = null
42+ this . aesStream = null
43+
4144 this . _ros = undefined
4245 }
4346
@@ -80,49 +83,70 @@ class ISocketComms extends EventEmitter {
8083 op . run ( ) . then ( ( status ) => {
8184 debug ( status )
8285 debug ( 'authed' )
83- this . emit ( 'open' )
86+ //this.aesOffer = op.offer
87+ this . aesStream = op . stream
8488 this . authed = true
89+ this . emit ( 'open' )
8590 } ) . catch ( error => {
8691 this . authed = false
8792 debug ( 'auth error' , error )
8893 this . emit ( 'close' )
8994 } )
9095 }
9196
92- decrypt ( reply , sender ) {
97+ async decrypt ( reply , sender ) {
98+ if ( this . aesStream ) {
99+ debug ( 'decrypting quantum aes' )
100+ console . log ( reply , typeof reply )
101+ console . log ( typeof reply . data )
102+
103+ let buf = reply . data
104+
105+ if ( buf instanceof Blob ) {
106+ debug ( 'reply.data is a blob' )
107+ buf = await reply . data . arrayBuffer ( ) ;
108+ } else {
109+ //buf = reply.data
110+ }
111+
112+ const contentBSON = await this . aesStream . decrypt ( new Uint8Array ( buf ) )
113+ const content = Routines . BSON . parseObject ( new Routines . BSON . BaseParser ( contentBSON ) )
114+
115+ return content
116+
117+ } else {
118+ debug ( 'decrypting classic' )
93119 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
120+
121+ if ( replyObj . enc && replyObj . sig ) {
122+ let msg = new Message ( { } )
123+ msg . fromJSON ( replyObj )
124+
125+ let content = await msg . decrypt ( this . party . privateIdentity )
126+
127+ const senderPub = Routines . extractPublicKeys ( msg . enc )
128+ debug ( 'sender' , sender , '\tdiscover' , this . discoverRemoteIdentity )
129+ if ( this . discoverRemoteIdentity && ! sender ) {
130+ debug ( 'discovered remote identity' , senderPub )
131+ this . remoteIdentity = {
132+ key : {
133+ public : senderPub
134+ }
135+ }
136+ sender = this . remoteIdentity
137+ }
138+ debug ( `sender from - ${ msg . from } ` )
139+ debug ( `senderPub - ${ senderPub } ` )
140+
141+ if ( senderPub . box != sender . key . public . box || senderPub . sign != sender . key . public . sign ) {
142+ throw new Error ( 'TRUST - reply is not from expected remote' )
143+ }
144+
145+ debug ( 'decrypted data' )
146+ return content
147+ }
148+ }
149+
126150 }
127151
128152 onmessage ( message ) {
@@ -142,28 +166,37 @@ class ISocketComms extends EventEmitter {
142166 } )
143167 }
144168
145- send ( input ) {
146- debug ( 'send - ' , typeof input , input )
169+ async send ( input ) {
170+ debug ( 'send - ' , typeof input , input )
147171
148- if ( typeof input != 'object' ) {
149- input = JSON . parse ( input )
150- }
172+ if ( typeof input != 'object' ) {
173+ input = JSON . parse ( input )
174+ }
151175
152- const content = new Message ( { msg : input } )
176+ let content = null
153177
154- return content . encrypt ( this . party . _identity , this . remoteIdentity . key )
155- . then ( JSON . stringify )
156- . then ( this . socket . send . bind ( this . socket ) )
178+ if ( this . aesStream && input . id . indexOf ( 'auth' ) == - 1 ) {
179+ debug ( 'sending quantum aes' )
180+ const contentBSON = Routines . BSON . serializeBSONWithoutOptimiser ( input )
181+ content = await this . aesStream . encrypt ( contentBSON )
182+ } else {
157183
184+ debug ( 'sending classic' )
185+ const msg = new Message ( { msg : input } )
186+ await msg . encrypt ( this . party . _identity , this . remoteIdentity . key )
187+ content = JSON . stringify ( msg . toJSON ( ) )
188+ }
189+
190+ await this . socket . send ( content )
158191 }
159192
160193 get ros ( ) {
161- if ( ! this . _ros ) {
162- this . _ros = new RosShim ( this )
163- this . _ros . connect ( )
164- }
194+ if ( ! this . _ros ) {
195+ this . _ros = new RosShim ( this )
196+ this . _ros . connect ( )
197+ }
165198
166- return this . _ros
199+ return this . _ros
167200 }
168201}
169202
0 commit comments