import * as Data from 'effect/Data'; import type { ParseError } from 'effect/ParseResult'; import * as Schema from 'effect/Schema'; import type { InvalidIdentityError } from '../identity/types.js'; import { InboxSenderAuthPolicy } from '../inboxes/types.js'; import { SignatureWithRecovery } from '../types.js'; export const EventAuthor = Schema.Struct({ accountAddress: Schema.String, signature: SignatureWithRecovery, }); export type EventAuthor = Schema.Schema.Type; export const SpaceMember = Schema.Struct({ accountAddress: Schema.String, role: Schema.Union(Schema.Literal('admin'), Schema.Literal('member')), }); export type SpaceMember = Schema.Schema.Type; export const SpaceInvitation = Schema.Struct({ inviteeAccountAddress: Schema.String, }); export type SpaceInvitation = Schema.Schema.Type; export const SpaceInbox = Schema.Struct({ inboxId: Schema.String, encryptionPublicKey: Schema.String, isPublic: Schema.Boolean, authPolicy: InboxSenderAuthPolicy, secretKey: Schema.String, }); export type SpaceInbox = Schema.Schema.Type; export const SpaceState = Schema.Struct({ id: Schema.String, invitations: Schema.Record({ key: Schema.String, value: SpaceInvitation }), members: Schema.Record({ key: Schema.String, value: SpaceMember }), removedMembers: Schema.Record({ key: Schema.String, value: SpaceMember }), inboxes: Schema.Record({ key: Schema.String, value: SpaceInbox }), lastEventHash: Schema.String, }); export type SpaceState = Schema.Schema.Type; export const CreateSpaceEvent = Schema.Struct({ transaction: Schema.Struct({ type: Schema.Literal('create-space'), id: Schema.String, creatorAccountAddress: Schema.String, }), author: EventAuthor, }); export type CreateSpaceEvent = Schema.Schema.Type; export const DeleteSpaceEvent = Schema.Struct({ transaction: Schema.Struct({ type: Schema.Literal('delete-space'), id: Schema.String, previousEventHash: Schema.String, }), author: EventAuthor, }); export type DeleteSpaceEvent = Schema.Schema.Type; export const CreateInvitationEvent = Schema.Struct({ transaction: Schema.Struct({ type: Schema.Literal('create-invitation'), id: Schema.String, inviteeAccountAddress: Schema.String, previousEventHash: Schema.String, }), author: EventAuthor, }); export type CreateInvitationEvent = Schema.Schema.Type; export const CreateSpaceInboxEvent = Schema.Struct({ transaction: Schema.Struct({ type: Schema.Literal('create-space-inbox'), id: Schema.String, spaceId: Schema.String, inboxId: Schema.String, encryptionPublicKey: Schema.String, secretKey: Schema.String, isPublic: Schema.Boolean, authPolicy: InboxSenderAuthPolicy, previousEventHash: Schema.String, }), author: EventAuthor, }); export type CreateSpaceInboxEvent = Schema.Schema.Type; export const AcceptInvitationEvent = Schema.Struct({ transaction: Schema.Struct({ id: Schema.String, type: Schema.Literal('accept-invitation'), previousEventHash: Schema.String, }), author: EventAuthor, }); export type AcceptInvitationEvent = Schema.Schema.Type; export const SpaceEvent = Schema.Union( CreateSpaceEvent, DeleteSpaceEvent, CreateInvitationEvent, AcceptInvitationEvent, CreateSpaceInboxEvent, ); export type SpaceEvent = Schema.Schema.Type; export const Author = Schema.Struct({ accountAddress: Schema.String, signaturePublicKey: Schema.String, signaturePrivateKey: Schema.String, encryptionPublicKey: Schema.String, }); export type Author = Schema.Schema.Type; export class VerifySignatureError extends Data.TaggedError('VerifySignatureError') {} export class InvalidEventError extends Data.TaggedError('InvalidEventError') {} export type ApplyError = ParseError | VerifySignatureError | InvalidEventError | InvalidIdentityError;