Skip to content

Conversation

nikgraf
Copy link
Collaborator

@nikgraf nikgraf commented Jan 30, 2025

Initially I wanted to use getVerifiedIdentity directly inside applyEvent, but then realized that on frontend we sometimes need to fetch from the API and on backend it's a DB call.

Passing in the entire list of identities was also an option. Didn't feel like a good idea to pass around the whole list from the store on frontend. I ended up adding a getVerifiedIdentity callback with this signature: (accountId: string) => Effect.Effect<PublicIdentity, InvalidIdentityError>

@nikgraf nikgraf self-assigned this Jan 30, 2025
@nikgraf nikgraf force-pushed the ng/verify-space-events-identities branch 2 times, most recently from 38b2862 to 53eb782 Compare January 31, 2025 09:39
@nikgraf nikgraf removed the request for review from fubhy February 3, 2025 13:22
@nikgraf nikgraf force-pushed the ng/verify-space-events-identities branch from 53eb782 to c689734 Compare February 3, 2025 13:24
Copy link
Member

@pcarranzav pcarranzav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still reviewing, but leaving this comment for now, will come back to the review in a while

SpaceEvents.applyEvent({
event,
state: JSON.parse(lastEvent.state),
getVerifiedIdentity: () => Effect.succeed(identity),
Copy link
Member

@pcarranzav pcarranzav Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds a bit risky, if we ever change applyEvent to query the identity for a different accountId then this will always return the same identity... I'd suggest passing a getVerifiedIdentity that checks if the accountId is the one we have, and fails otherwise

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's I case I didn't imagine, but makes sense. Let me know if the change I made is what you envisioned

@nikgraf nikgraf force-pushed the ng/verify-space-events-identities branch from 9684635 to f55d3a7 Compare February 4, 2025 16:47
Copy link
Member

@pcarranzav pcarranzav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions / details but LGTM


const result = await Effect.runPromiseExit(SpaceEvents.applyEvent({ event, state: JSON.parse(lastEvent.state) }));
const getVerifiedIdentity = (accountIdToFetch: string) => {
// applySpaceEvent is only allowed to be called by the account that is applying the event
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess technically we could remove this check, right? It's assuming the behavior of applySpaceEvent, in the future we could have a case where we need to check the identity for a different account mentioned in the event. (That being said I think it's okay to leave this as it is and change it in the future if needed)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right that technically we could remove this check.

At the moment this ensures that the server only applies an event created that this account created. This must not be enforced, but I sleep a bit better that we enforce it. At least until we explicitly only allow accounts to send events to a sync server they themselves created


export const createSpace = async ({ accountId, event, keyBox, keyId }: Params) => {
const result = await Effect.runPromiseExit(SpaceEvents.applyEvent({ event, state: undefined }));
const getVerifiedIdentity = (accountIdToFetch: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I see there's a few repetitions of this so maybe they could be split out into a utility function like this (in a separate file):

export const getGetVerifiedIdentity = (allowedAccountId: string) => {
  return function(accountIdToFetch: string) {
    // applySpaceEvent is only allowed to be called by the account that is applying the event
    if (accountIdToFetch !== allowedAccountId) {
      return Effect.fail(new Identity.InvalidIdentityError());
    }

    return Effect.gen(function* () {
      const identity = yield* Effect.tryPromise({
        try: () => getIdentity({ accountId: accountIdToFetch }),
        catch: () => new Identity.InvalidIdentityError(),
      });
      return identity;
    });
  }
}

Then you can pass getGetVerifiedIdentity(accountId) to applyEvent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, will do it in a follow up PR 👍

@nikgraf nikgraf merged commit 3ca1f1f into main Feb 4, 2025
4 checks passed
@nikgraf nikgraf deleted the ng/verify-space-events-identities branch February 4, 2025 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants