@@ -17,6 +17,7 @@ import { createUpdate } from './handlers/createUpdate.js';
1717import { findAppIdentity } from './handlers/find-app-identity.js' ;
1818import { getAppIdentityBySessionToken } from './handlers/get-app-identity-by-session-token.js' ;
1919import { getAccountInbox } from './handlers/getAccountInbox.js' ;
20+ import { getAppOrConnectIdentity } from './handlers/getAppOrConnectIdentity.js' ;
2021import { type GetIdentityResult , getConnectIdentity } from './handlers/getConnectIdentity.js' ;
2122import { getLatestAccountInboxMessages } from './handlers/getLatestAccountInboxMessages.js' ;
2223import { getLatestSpaceInboxMessages } from './handlers/getLatestSpaceInboxMessages.js' ;
@@ -375,21 +376,47 @@ app.get('/whoami', async (req, res) => {
375376 }
376377} ) ;
377378
379+ app . get ( '/connect/identity' , async ( req , res ) => {
380+ console . log ( 'GET connect/identity' ) ;
381+ const accountAddress = req . query . accountAddress as string ;
382+ const identity = await getConnectIdentity ( { accountAddress } ) ;
383+ if ( ! identity ) {
384+ res . status ( 404 ) . send ( 'Identity not found' ) ;
385+ return ;
386+ }
387+ const outgoingMessage : Messages . ResponseIdentity = {
388+ accountAddress,
389+ signaturePublicKey : identity . signaturePublicKey ,
390+ encryptionPublicKey : identity . encryptionPublicKey ,
391+ accountProof : identity . accountProof ,
392+ keyProof : identity . keyProof ,
393+ } ;
394+ res . status ( 200 ) . send ( outgoingMessage ) ;
395+ } ) ;
396+
378397app . get ( '/identity' , async ( req , res ) => {
379398 console . log ( 'GET identity' ) ;
380399 const accountAddress = req . query . accountAddress as string ;
400+ const signaturePublicKey = req . query . signaturePublicKey as string ;
401+ const appId = req . query . appId as string ;
381402 if ( ! accountAddress ) {
382403 res . status ( 400 ) . send ( 'No accountAddress' ) ;
383404 return ;
384405 }
406+ if ( ! signaturePublicKey && ! appId ) {
407+ res . status ( 400 ) . send ( 'No signaturePublicKey or appId' ) ;
408+ return ;
409+ }
385410 try {
386- const identity = await getConnectIdentity ( { accountAddress } ) ;
411+ const params = signaturePublicKey ? { accountAddress, signaturePublicKey } : { accountAddress, appId } ;
412+ const identity = await getAppOrConnectIdentity ( params ) ;
387413 const outgoingMessage : Messages . ResponseIdentity = {
388414 accountAddress,
389415 signaturePublicKey : identity . signaturePublicKey ,
390416 encryptionPublicKey : identity . encryptionPublicKey ,
391417 accountProof : identity . accountProof ,
392418 keyProof : identity . keyProof ,
419+ appId : identity . appId ?? undefined ,
393420 } ;
394421 res . status ( 200 ) . send ( outgoingMessage ) ;
395422 } catch ( error ) {
@@ -469,7 +496,10 @@ app.post('/spaces/:spaceId/inboxes/:inboxId/messages', async (req, res) => {
469496 // Check if this public key corresponds to a user's identity
470497 let authorIdentity : GetIdentityResult ;
471498 try {
472- authorIdentity = await getConnectIdentity ( { connectSignaturePublicKey : authorPublicKey } ) ;
499+ authorIdentity = await getAppOrConnectIdentity ( {
500+ accountAddress : message . authorAccountAddress ,
501+ signaturePublicKey : authorPublicKey ,
502+ } ) ;
473503 } catch ( error ) {
474504 res . status ( 403 ) . send ( { error : 'Not authorized to post to this inbox' } ) ;
475505 return ;
@@ -552,7 +582,10 @@ app.post('/accounts/:accountAddress/inboxes/:inboxId/messages', async (req, res)
552582 // Check if this public key corresponds to a user's identity
553583 let authorIdentity : GetIdentityResult ;
554584 try {
555- authorIdentity = await getConnectIdentity ( { connectSignaturePublicKey : authorPublicKey } ) ;
585+ authorIdentity = await getAppOrConnectIdentity ( {
586+ accountAddress : message . authorAccountAddress ,
587+ signaturePublicKey : authorPublicKey ,
588+ } ) ;
556589 } catch ( error ) {
557590 res . status ( 403 ) . send ( { error : 'Not authorized to post to this inbox' } ) ;
558591 return ;
@@ -714,19 +747,15 @@ webSocketServer.on('connection', async (webSocket: CustomWebSocket, request: Req
714747 break ;
715748 }
716749 case 'create-space-event' : {
717- const getVerifiedIdentity = ( accountAddressToFetch : string ) => {
718- console . log (
719- 'TODO getVerifiedIdentity should work for app identities' ,
720- accountAddressToFetch ,
721- accountAddress ,
722- ) ;
750+ const getVerifiedIdentity = ( accountAddressToFetch : string , publicKey : string ) => {
723751 if ( accountAddressToFetch !== accountAddress ) {
724752 return Effect . fail ( new Identity . InvalidIdentityError ( ) ) ;
725753 }
726754
727755 return Effect . gen ( function * ( ) {
728756 const identity = yield * Effect . tryPromise ( {
729- try : ( ) => getConnectIdentity ( { accountAddress : accountAddressToFetch } ) ,
757+ try : ( ) =>
758+ getAppOrConnectIdentity ( { accountAddress : accountAddressToFetch , signaturePublicKey : publicKey } ) ,
730759 catch : ( ) => new Identity . InvalidIdentityError ( ) ,
731760 } ) ;
732761 return identity ;
@@ -825,7 +854,10 @@ webSocketServer.on('connection', async (webSocket: CustomWebSocket, request: Req
825854 throw new Error ( 'Invalid accountAddress' ) ;
826855 }
827856 const signer = Inboxes . recoverAccountInboxCreatorKey ( data ) ;
828- const signerAccount = await getConnectIdentity ( { connectSignaturePublicKey : signer } ) ;
857+ const signerAccount = await getAppOrConnectIdentity ( {
858+ accountAddress : data . accountAddress ,
859+ signaturePublicKey : signer ,
860+ } ) ;
829861 if ( signerAccount . accountAddress !== accountAddress ) {
830862 throw new Error ( 'Invalid signature' ) ;
831863 }
@@ -895,7 +927,10 @@ webSocketServer.on('connection', async (webSocket: CustomWebSocket, request: Req
895927 // Check that the update was signed by a valid identity
896928 // belonging to this accountAddress
897929 const signer = Messages . recoverUpdateMessageSigner ( data ) ;
898- const identity = await getConnectIdentity ( { connectSignaturePublicKey : signer } ) ;
930+ const identity = await getAppOrConnectIdentity ( {
931+ accountAddress : data . accountAddress ,
932+ signaturePublicKey : signer ,
933+ } ) ;
899934 if ( identity . accountAddress !== accountAddress ) {
900935 throw new Error ( 'Invalid signature' ) ;
901936 }
0 commit comments