'use strict' ; import { Router , RequestHandler , Request , Response , NextFunction } from 'express' ; import { setupMetaverseAPI , finishMetaverseAPI } from '@Route-Tools/middleware' ; import { accountFromAuthToken } from '@Route-Tools/middleware' ; import multer from 'multer' ; import { Logger } from '@Tools/Logging' ; import { convertBinKeyToPEM } from '@Route-Tools/Util' ; import { Accounts } from '@Entities/Accounts' ; import { IsNotNullOrEmpty } from '@Tools/Misc' ; const procPutUserPublicKey = async ( req , resp , next ) => { if ( req . vAuthAccount ) { if ( req . files ) { try { const publicKeyBin = ( req . files as any ) . public_key [ 0 ] . buffer ; if ( IsNotNullOrEmpty ( publicKeyBin ) ) { const fieldsToUpdate = { 'sessionPublicKey' : convertBinKeyToPEM ( publicKeyBin ) } ; await Accounts . updateEntityFields ( req . vAuthAccount , fieldsToUpdate ) ; } else { req . vRestResp . respondFailure ( 'badly formed public key' ) ; } ; } catch ( e ) { Logger . error ( 'procPutUserPublicKey: exception converting: ' + e ) ; req . vRestResp . respondFailure ( 'exception converting public key' ) ; } } else { Logger . error ( 'procPutUserPublicKey: no files part of body' ) ; req . vRestResp . respondFailure ( 'no public key supplied' ) ; } ; } else { req . vRestResp . respondFailure ( req . vAccountError ?? 'Not logged in' ) ; } ; next ( ) ; } ; export const name = '/api/v1/user/public_key' ; export const router = Router ( ) ; const multiStorage = multer . memoryStorage ( ) ; const uploader = multer ( { storage : multiStorage , } ) ; const multiParser = uploader . fields ( [ { name : 'public_key' } ] ) ; router . put ( '/api/v1/user/public_key' , [ setupMetaverseAPI , accountFromAuthToken , multiParser , procPutUserPublicKey , finishMetaverseAPI ] ) ;