Skip to content

Commit a1b7629

Browse files
agent: pass key and link ID when preparing group link (#1754)
* agent: pass key and link ID when preparing group link * binding --------- Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
1 parent 1e0093b commit a1b7629

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/Simplex/Messaging/Agent.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,11 @@ createConnection c nm userId enableNtfs checkNotices = withAgentEnv c .::. newCo
401401
{-# INLINE createConnection #-}
402402

403403
-- | Prepare connection link for contact mode (no network call).
404-
-- Returns root key pair (for signing OwnerAuth), the created link, and internal params.
404+
-- Caller provides root signing key pair and link entity ID.
405+
-- Returns the created link and internal params.
405406
-- The link address is fully determined at this point.
406-
prepareConnectionLink :: AgentClient -> UserId -> Maybe ByteString -> Bool -> Maybe CRClientData -> AE (C.KeyPairEd25519, CreatedConnLink 'CMContact, PreparedLinkParams)
407-
prepareConnectionLink c userId linkEntityId checkNotices = withAgentEnv c . prepareConnectionLink' c userId linkEntityId checkNotices
407+
prepareConnectionLink :: AgentClient -> UserId -> C.KeyPairEd25519 -> ByteString -> Bool -> Maybe CRClientData -> AE (CreatedConnLink 'CMContact, PreparedLinkParams)
408+
prepareConnectionLink c userId rootKey linkEntityId checkNotices = withAgentEnv c . prepareConnectionLink' c userId rootKey linkEntityId checkNotices
408409
{-# INLINE prepareConnectionLink #-}
409410

410411
-- | Create connection for prepared link (single network call).
@@ -918,23 +919,22 @@ newConn c nm userId enableNtfs checkNotices cMode linkData_ clientData pqInitKey
918919
`catchE` \e -> withStore' c (`deleteConnRecord` connId) >> throwE e
919920

920921
-- | Prepare connection link for contact mode (no network, no database).
921-
-- Generates all cryptographic material and returns the link that will be created.
922-
prepareConnectionLink' :: AgentClient -> UserId -> Maybe ByteString -> Bool -> Maybe CRClientData -> AM (C.KeyPairEd25519, CreatedConnLink 'CMContact, PreparedLinkParams)
923-
prepareConnectionLink' c userId linkEntityId checkNotices clientData = do
922+
-- Caller provides root signing key pair and link entity ID.
923+
prepareConnectionLink' :: AgentClient -> UserId -> C.KeyPairEd25519 -> ByteString -> Bool -> Maybe CRClientData -> AM (CreatedConnLink 'CMContact, PreparedLinkParams)
924+
prepareConnectionLink' c userId rootKey@(_, plpRootPrivKey) linkEntityId checkNotices clientData = do
924925
g <- asks random
925926
plpSrvWithAuth@(ProtoServerWithAuth srv _) <- getSMPServer c userId
926927
when checkNotices $ checkClientNotices c plpSrvWithAuth
927928
AgentConfig {smpClientVRange, smpAgentVRange} <- asks config
928929
plpNonce@(C.CbNonce corrId) <- atomically $ C.randomCbNonce g
929-
sigKeys@(_, plpRootPrivKey) <- atomically $ C.generateKeyPair g
930930
plpQueueE2EKeys@(e2ePubKey, _) <- atomically $ C.generateKeyPair g
931931
let sndId = SMP.EntityId $ B.take 24 $ C.sha3_384 corrId
932932
qUri = SMPQueueUri smpClientVRange $ SMPQueueAddress srv sndId e2ePubKey (Just QMContact)
933933
connReq = CRContactUri $ ConnReqUriData SSSimplex smpAgentVRange [qUri] clientData
934-
(plpLinkKey, plpSignedFixedData) = SL.encodeSignFixedData sigKeys smpAgentVRange connReq linkEntityId
934+
(plpLinkKey, plpSignedFixedData) = SL.encodeSignFixedData rootKey smpAgentVRange connReq (Just linkEntityId)
935935
ccLink = CCLink connReq $ Just $ CSLContact SLSServer CCTContact srv plpLinkKey
936936
params = PreparedLinkParams {plpNonce, plpQueueE2EKeys, plpLinkKey, plpRootPrivKey, plpSignedFixedData, plpSrvWithAuth}
937-
pure (sigKeys, ccLink, params)
937+
pure (ccLink, params)
938938

939939
-- | Create connection for prepared link (single network call).
940940
createConnectionForLink' :: AgentClient -> NetworkRequestMode -> UserId -> Bool -> CreatedConnLink 'CMContact -> PreparedLinkParams -> UserConnLinkData 'CMContact -> CR.InitialKeys -> SubscriptionMode -> AM ConnId

tests/AgentTests/FunctionalAPITests.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,10 +1653,11 @@ testPrepareCreateConnectionLink ps = withSmpServer ps $ withAgentClients2 $ \a b
16531653
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
16541654
userLinkData = UserContactLinkData userCtData
16551655
g <- C.newRandom
1656+
rootKey <- atomically $ C.generateKeyPair g
16561657
linkEntId <- atomically $ C.randomBytes 32 g
16571658
runRight $ do
1658-
((_rootPubKey, _rootPrivKey), ccLink@(CCLink connReq (Just shortLink)), preparedParams) <-
1659-
A.prepareConnectionLink a 1 (Just linkEntId) True Nothing
1659+
(ccLink@(CCLink connReq (Just shortLink)), preparedParams) <-
1660+
A.prepareConnectionLink a 1 rootKey linkEntId True Nothing
16601661
liftIO $ strDecode (strEncode shortLink) `shouldBe` Right shortLink
16611662
_ <- A.createConnectionForLink a NRMInteractive 1 True ccLink preparedParams userLinkData CR.IKPQOn SMSubscribe
16621663
(FixedLinkData {linkConnReq = connReq', linkEntityId}, ContactLinkData _ userCtData') <- getConnShortLink b 1 shortLink

0 commit comments

Comments
 (0)