import { getKeys , OpenPGPKey } from 'pmcrypto' ; import { c } from 'ttag' ; import downloadFile from '@proton/shared/lib/helpers/downloadFile' ; import { KEY_FILE_EXTENSION } from '@proton/shared/lib/constants' ; import { Alert , FormModal } from '../../../components' ; const handleExport = ( name , publicKey ) => { const fingerprint = publicKey . getFingerprint ( ) ; const filename = [ 'publickey.' , name , '-' , fingerprint , KEY_FILE_EXTENSION ] . join ( '' ) ; const armoredPublicKey = publicKey . armor ( ) ; const blob = new Blob ( [ armoredPublicKey ] , { type : 'text/plain' } ) ; downloadFile ( blob , filename ) ; } ; interface Props { name : string ; fallbackPrivateKey : string ; publicKey ? : OpenPGPKey ; onSuccess ? : ( ) => void ; onClose ? : ( ) => void ; } const ExportPublicKeyModal = ( { name , fallbackPrivateKey , publicKey , onClose , ... rest } ) => { const handleSubmit = async ( ) => { if ( publicKey ) { handleExport ( name , publicKey ) ; } else { const [ key ] = await getKeys ( fallbackPrivateKey ) ; const publicKey = key . toPublic ( ) ; handleExport ( name , publicKey ) ; } onClose ?. ( ) ; } ; return ( < FormModal title = { c ( 'Title' ) . t `Export public key` } close = { c ( 'Action' ) . t `Close` } submit = { c ( 'Action' ) . t `Export` } onClose = { onClose } onSubmit = { handleSubmit } { ... rest } > < Alert className = "mb1" type = "info" > { c ( 'Info' ) . t `Give your public key to your friends, or publish it online, so that everyone can send you end-to-end encrypted email!` } < /Alert> < /FormModal>); } ; export default ExportPublicKeyModal ;