@@ -56,8 +56,8 @@ module Simplex.Messaging.Agent
5656 deleteConnectionAsync ,
5757 deleteConnectionsAsync ,
5858 createConnection ,
59- setContactShortLink ,
60- deleteContactShortLink ,
59+ setConnShortLink ,
60+ deleteConnShortLink ,
6161 getConnShortLink ,
6262 deleteLocalInvShortLink ,
6363 changeConnectionUser ,
@@ -372,13 +372,13 @@ createConnection c userId enableNtfs = withAgentEnv c .::. newConn c userId enab
372372{-# INLINE createConnection #-}
373373
374374-- | Create or update user's contact connection short link
375- setContactShortLink :: AgentClient -> ConnId -> ConnInfo -> Maybe CRClientData -> AE (ConnShortLink 'CMContact )
376- setContactShortLink c = withAgentEnv c .:. setContactShortLink ' c
377- {-# INLINE setContactShortLink #-}
375+ setConnShortLink :: AgentClient -> ConnId -> SConnectionMode c -> ConnInfo -> Maybe CRClientData -> AE (ConnShortLink c )
376+ setConnShortLink c = withAgentEnv c .:: setConnShortLink ' c
377+ {-# INLINE setConnShortLink #-}
378378
379- deleteContactShortLink :: AgentClient -> ConnId -> AE ()
380- deleteContactShortLink c = withAgentEnv c . deleteContactShortLink ' c
381- {-# INLINE deleteContactShortLink #-}
379+ deleteConnShortLink :: AgentClient -> ConnId -> SConnectionMode c -> AE ()
380+ deleteConnShortLink c = withAgentEnv c .: deleteConnShortLink ' c
381+ {-# INLINE deleteConnShortLink #-}
382382
383383-- | Get and verify data from short link. For 1-time invitations it preserves the key to allow retries
384384getConnShortLink :: AgentClient -> UserId -> ConnShortLink c -> AE (ConnectionRequestUri c , ConnLinkData c )
@@ -833,26 +833,28 @@ newConn c userId enableNtfs cMode userData_ clientData pqInitKeys subMode = do
833833 (connId,) <$> newRcvConnSrv c userId connId enableNtfs cMode userData_ clientData pqInitKeys subMode srv
834834 `catchE` \ e -> withStore' c (`deleteConnRecord` connId) >> throwE e
835835
836- setContactShortLink' :: AgentClient -> ConnId -> ConnInfo -> Maybe CRClientData -> AM (ConnShortLink 'CMContact)
837- setContactShortLink' c connId userData clientData =
838- withConnLock c connId " setContactShortLink" $
839- withStore c (`getConn` connId) >>= \ case
840- SomeConn _ (ContactConnection _ rq) -> do
841- (lnkId, linkKey, d) <- prepareLinkData rq
842- addQueueLink c rq lnkId d
843- pure $ CSLContact SLSServer CCTContact (qServer rq) linkKey
844- _ -> throwE $ CMD PROHIBITED " setContactShortLink: not contact address"
836+ setConnShortLink' :: AgentClient -> ConnId -> SConnectionMode c -> ConnInfo -> Maybe CRClientData -> AM (ConnShortLink c )
837+ setConnShortLink' c connId cMode userData clientData =
838+ withConnLock c connId " setConnShortLink" $ do
839+ SomeConn _ conn <- withStore c (`getConn` connId)
840+ (rq, lnkId, sl, d) <- case (conn, cMode) of
841+ (ContactConnection _ rq, SCMContact ) -> prepareContactLinkData rq
842+ (RcvConnection _ rq, SCMInvitation ) -> prepareInvLinkData rq
843+ _ -> throwE $ CMD PROHIBITED " setConnShortLink: invalid connection or mode"
844+ addQueueLink c rq lnkId d
845+ pure sl
845846 where
846- prepareLinkData :: RcvQueue -> AM (SMP. LinkId , LinkKey , QueueLinkData )
847- prepareLinkData rq@ RcvQueue {server, sndId, e2ePrivKey, shortLink} = do
847+ prepareContactLinkData :: RcvQueue -> AM (RcvQueue , SMP. LinkId , ConnShortLink 'CMContact , QueueLinkData )
848+ prepareContactLinkData rq@ RcvQueue {server, sndId, e2ePrivKey, shortLink} = do
848849 g <- asks random
849850 AgentConfig {smpClientVRange = vr, smpAgentVRange} <- asks config
851+ let cslContact = CSLContact SLSServer CCTContact (qServer rq)
850852 case shortLink of
851853 Just ShortLinkCreds {shortLinkId, shortLinkKey, linkPrivSigKey, linkEncFixedData} -> do
852854 let (linkId, k) = SL. contactShortLinkKdf shortLinkKey
853- unless (shortLinkId == linkId) $ throwE $ INTERNAL " setContactShortLink : link ID is not derived from link"
855+ unless (shortLinkId == linkId) $ throwE $ INTERNAL " setConnShortLink : link ID is not derived from link"
854856 d <- liftError id $ SL. encryptUserData g k $ SL. encodeSignUserData linkPrivSigKey smpAgentVRange userData
855- pure (linkId, shortLinkKey, (linkEncFixedData, d))
857+ pure (rq, linkId, cslContact shortLinkKey, (linkEncFixedData, d))
856858 Nothing -> do
857859 sigKeys@ (_, privSigKey) <- atomically $ C. generateKeyPair @ 'C.Ed25519 g
858860 let qUri = SMPQueueUri vr $ SMPQueueAddress server sndId (C. publicKey e2ePrivKey) (Just QMContact )
@@ -862,14 +864,26 @@ setContactShortLink' c connId userData clientData =
862864 srvData <- liftError id $ SL. encryptLinkData g k linkData
863865 let slCreds = ShortLinkCreds linkId linkKey privSigKey (fst srvData)
864866 withStore' c $ \ db -> updateShortLinkCreds db rq slCreds
865- pure (linkId, linkKey, srvData)
866-
867- deleteContactShortLink' :: AgentClient -> ConnId -> AM ()
868- deleteContactShortLink' c connId =
869- withConnLock c connId " deleteContactShortLink" $
870- withStore c (`getConn` connId) >>= \ case
871- SomeConn _ (ContactConnection _ rq) -> deleteQueueLink c rq
872- _ -> throwE $ CMD PROHIBITED " deleteContactShortLink: not contact address"
867+ pure (rq, linkId, cslContact linkKey, srvData)
868+ prepareInvLinkData :: RcvQueue -> AM (RcvQueue , SMP. LinkId , ConnShortLink 'CMInvitation, QueueLinkData )
869+ prepareInvLinkData rq@ RcvQueue {shortLink} = case shortLink of
870+ Just ShortLinkCreds {shortLinkId, shortLinkKey, linkPrivSigKey, linkEncFixedData} -> do
871+ g <- asks random
872+ AgentConfig {smpAgentVRange} <- asks config
873+ let k = SL. invShortLinkKdf shortLinkKey
874+ d <- liftError id $ SL. encryptUserData g k $ SL. encodeSignUserData linkPrivSigKey smpAgentVRange userData
875+ let sl = CSLInvitation SLSServer (qServer rq) shortLinkId shortLinkKey
876+ pure (rq, shortLinkId, sl, (linkEncFixedData, d))
877+ Nothing -> throwE $ CMD PROHIBITED " setConnShortLink: no ShortLinkCreds in invitation"
878+
879+ deleteConnShortLink' :: AgentClient -> ConnId -> SConnectionMode c -> AM ()
880+ deleteConnShortLink' c connId cMode =
881+ withConnLock c connId " deleteConnShortLink" $ do
882+ SomeConn _ conn <- withStore c (`getConn` connId)
883+ case (conn, cMode) of
884+ (ContactConnection _ rq, SCMContact ) -> deleteQueueLink c rq
885+ (RcvConnection _ rq, SCMInvitation ) -> deleteQueueLink c rq
886+ _ -> throwE $ CMD PROHIBITED " deleteConnShortLink: not contact address"
873887
874888-- TODO [short links] remove 1-time invitation data and link ID from the server after the message is sent.
875889getConnShortLink' :: forall c . AgentClient -> UserId -> ConnShortLink c -> AM (ConnectionRequestUri c , ConnLinkData c )
0 commit comments