@@ -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' ;
@@ -318,6 +319,20 @@ app.post('/connect/app-identity', async (req, res) => {
318319 res . status ( 401 ) . send ( 'Unauthorized' ) ;
319320 return ;
320321 }
322+ if (
323+ ! Identity . verifyIdentityOwnership (
324+ accountAddress ,
325+ message . signaturePublicKey ,
326+ message . accountProof ,
327+ message . keyProof ,
328+ CHAIN ,
329+ RPC_URL ,
330+ )
331+ ) {
332+ console . log ( 'Ownership proof is invalid' ) ;
333+ res . status ( 401 ) . send ( 'Unauthorized' ) ;
334+ return ;
335+ }
321336 const sessionToken = bytesToHex ( randomBytes ( 32 ) ) ;
322337 const sessionTokenExpires = new Date ( Date . now ( ) + 1000 * 60 * 60 * 24 * 30 ) ; // 30 days
323338 const appIdentity = await createAppIdentity ( {
@@ -361,21 +376,47 @@ app.get('/whoami', async (req, res) => {
361376 }
362377} ) ;
363378
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+
364397app . get ( '/identity' , async ( req , res ) => {
365398 console . log ( 'GET identity' ) ;
366399 const accountAddress = req . query . accountAddress as string ;
400+ const signaturePublicKey = req . query . signaturePublicKey as string ;
401+ const appId = req . query . appId as string ;
367402 if ( ! accountAddress ) {
368403 res . status ( 400 ) . send ( 'No accountAddress' ) ;
369404 return ;
370405 }
406+ if ( ! signaturePublicKey && ! appId ) {
407+ res . status ( 400 ) . send ( 'No signaturePublicKey or appId' ) ;
408+ return ;
409+ }
371410 try {
372- const identity = await getConnectIdentity ( { accountAddress } ) ;
411+ const params = signaturePublicKey ? { accountAddress, signaturePublicKey } : { accountAddress, appId } ;
412+ const identity = await getAppOrConnectIdentity ( params ) ;
373413 const outgoingMessage : Messages . ResponseIdentity = {
374414 accountAddress,
375415 signaturePublicKey : identity . signaturePublicKey ,
376416 encryptionPublicKey : identity . encryptionPublicKey ,
377417 accountProof : identity . accountProof ,
378418 keyProof : identity . keyProof ,
419+ appId : identity . appId ?? undefined ,
379420 } ;
380421 res . status ( 200 ) . send ( outgoingMessage ) ;
381422 } catch ( error ) {
@@ -455,7 +496,10 @@ app.post('/spaces/:spaceId/inboxes/:inboxId/messages', async (req, res) => {
455496 // Check if this public key corresponds to a user's identity
456497 let authorIdentity : GetIdentityResult ;
457498 try {
458- authorIdentity = await getConnectIdentity ( { connectSignaturePublicKey : authorPublicKey } ) ;
499+ authorIdentity = await getAppOrConnectIdentity ( {
500+ accountAddress : message . authorAccountAddress ,
501+ signaturePublicKey : authorPublicKey ,
502+ } ) ;
459503 } catch ( error ) {
460504 res . status ( 403 ) . send ( { error : 'Not authorized to post to this inbox' } ) ;
461505 return ;
@@ -538,7 +582,10 @@ app.post('/accounts/:accountAddress/inboxes/:inboxId/messages', async (req, res)
538582 // Check if this public key corresponds to a user's identity
539583 let authorIdentity : GetIdentityResult ;
540584 try {
541- authorIdentity = await getConnectIdentity ( { connectSignaturePublicKey : authorPublicKey } ) ;
585+ authorIdentity = await getAppOrConnectIdentity ( {
586+ accountAddress : message . authorAccountAddress ,
587+ signaturePublicKey : authorPublicKey ,
588+ } ) ;
542589 } catch ( error ) {
543590 res . status ( 403 ) . send ( { error : 'Not authorized to post to this inbox' } ) ;
544591 return ;
@@ -700,19 +747,15 @@ webSocketServer.on('connection', async (webSocket: CustomWebSocket, request: Req
700747 break ;
701748 }
702749 case 'create-space-event' : {
703- const getVerifiedIdentity = ( accountAddressToFetch : string ) => {
704- console . log (
705- 'TODO getVerifiedIdentity should work for app identities' ,
706- accountAddressToFetch ,
707- accountAddress ,
708- ) ;
750+ const getVerifiedIdentity = ( accountAddressToFetch : string , publicKey : string ) => {
709751 if ( accountAddressToFetch !== accountAddress ) {
710752 return Effect . fail ( new Identity . InvalidIdentityError ( ) ) ;
711753 }
712754
713755 return Effect . gen ( function * ( ) {
714756 const identity = yield * Effect . tryPromise ( {
715- try : ( ) => getConnectIdentity ( { accountAddress : accountAddressToFetch } ) ,
757+ try : ( ) =>
758+ getAppOrConnectIdentity ( { accountAddress : accountAddressToFetch , signaturePublicKey : publicKey } ) ,
716759 catch : ( ) => new Identity . InvalidIdentityError ( ) ,
717760 } ) ;
718761 return identity ;
@@ -811,7 +854,10 @@ webSocketServer.on('connection', async (webSocket: CustomWebSocket, request: Req
811854 throw new Error ( 'Invalid accountAddress' ) ;
812855 }
813856 const signer = Inboxes . recoverAccountInboxCreatorKey ( data ) ;
814- const signerAccount = await getConnectIdentity ( { connectSignaturePublicKey : signer } ) ;
857+ const signerAccount = await getAppOrConnectIdentity ( {
858+ accountAddress : data . accountAddress ,
859+ signaturePublicKey : signer ,
860+ } ) ;
815861 if ( signerAccount . accountAddress !== accountAddress ) {
816862 throw new Error ( 'Invalid signature' ) ;
817863 }
@@ -881,7 +927,10 @@ webSocketServer.on('connection', async (webSocket: CustomWebSocket, request: Req
881927 // Check that the update was signed by a valid identity
882928 // belonging to this accountAddress
883929 const signer = Messages . recoverUpdateMessageSigner ( data ) ;
884- const identity = await getConnectIdentity ( { connectSignaturePublicKey : signer } ) ;
930+ const identity = await getAppOrConnectIdentity ( {
931+ accountAddress : data . accountAddress ,
932+ signaturePublicKey : signer ,
933+ } ) ;
885934 if ( identity . accountAddress !== accountAddress ) {
886935 throw new Error ( 'Invalid signature' ) ;
887936 }
0 commit comments