1+ const Joi = require ( '@hapi/joi' )
2+ const Hoek = require ( '@hapi/hoek' )
3+ const { Message, Routines} = require ( '@dataparty/crypto' )
4+ const debug = require ( 'debug' ) ( 'venue.middleware.pre.decrypt' )
5+
6+ const IMiddleware = require ( '../../../service/imiddleware' )
7+
8+ module . exports = class DecryptNaCl extends IMiddleware {
9+
10+ static get Name ( ) {
11+ return 'decrypt-nacl'
12+ }
13+
14+ static get Type ( ) {
15+ return 'pre'
16+ }
17+
18+ static get Description ( ) {
19+ return 'Decrypt inbound data'
20+ }
21+
22+ static get ConfigSchema ( ) {
23+ return Joi . boolean ( )
24+ }
25+
26+ static async start ( party ) {
27+
28+ }
29+
30+ static async run ( context , { Config} ) {
31+
32+ if ( ! Config ) { return }
33+
34+ if ( ! context . input || ! context . input . enc ) {
35+ throw new Error ( 'insecure message' )
36+ }
37+
38+ context . debug ( 'input' , context . input , typeof context . input )
39+
40+
41+ const msg = new Message ( context . input )
42+ context . debug ( 'privateIdentity' , context . party . privateIdentity . id )
43+
44+ const publicKeys = Routines . extractPublicKeys ( msg . enc )
45+
46+ context . debug ( 'sender' , publicKeys )
47+ context . debug ( typeof context . party . privateIdentity . key . private . box )
48+ context . debug ( context . input . enc )
49+
50+ //! check if sender is a known session
51+ // if known, context.setSenderKey & context.setActor( User )
52+ // verify (sender>user)+(user>sender) trusts
53+
54+ const jsonContent = await msg . decrypt ( context . party . privateIdentity )
55+
56+
57+ context . setSenderKey ( {
58+ type : 'nacl' ,
59+ public : publicKeys
60+ } )
61+
62+ //context.setInputSession(Hoek.reach(jsonContent, 'session'))
63+ context . setInput ( Hoek . reach ( jsonContent , 'data' ) )
64+ }
65+ }
0 commit comments