diff --git a/src/hooks/useSigitMeta.tsx b/src/hooks/useSigitMeta.tsx index 5c1159e..2026621 100644 --- a/src/hooks/useSigitMeta.tsx +++ b/src/hooks/useSigitMeta.tsx @@ -1,11 +1,5 @@ import { useEffect, useState } from 'react' -import { - CreateSignatureEventContent, - DocSignatureEvent, - Meta, - SignedEventContent, - OpenTimestamp -} from '../types' +import { DocSignatureEvent, Meta, SignedEventContent, FlatMeta } from '../types' import { Mark } from '../types/mark' import { fromUnixTimestamp, @@ -17,53 +11,11 @@ import { } from '../utils' import { toast } from 'react-toastify' import { verifyEvent } from 'nostr-tools' -import { Event } from 'nostr-tools' import store from '../store/store' import { NostrController } from '../controllers' import { MetaParseError } from '../types/errors/MetaParseError' import { MARK_TYPE_CONFIG } from '../components/MarkTypeStrategy/MarkStrategy' -/** - * Flattened interface that combines properties `Meta`, `CreateSignatureEventContent`, - * and `Event` (event's fields are made optional and pubkey and created_at replaced with our versions) - */ -export interface FlatMeta - extends Meta, - CreateSignatureEventContent, - Partial> { - // Remove pubkey and use submittedBy as `npub1${string}` - submittedBy?: `npub1${string}` - - // Optional field only present on exported sigits - // Exporting adds user's pubkey - exportedBy?: `npub1${string}` - - // Remove created_at and replace with createdAt - createdAt?: number - - // Validated create signature event - isValid: boolean - - // Decryption - encryptionKey: string | undefined - - // Parsed Document Signatures - parsedSignatureEvents: { - [signer: `npub1${string}`]: DocSignatureEvent - } - - // Calculated completion time - completedAt?: number - - // Calculated status fields - signedStatus: SigitStatus - signersStatus: { - [signer: `npub1${string}`]: SignStatus - } - - timestamps?: OpenTimestamp[] -} - /** * Custom use hook for parsing the Sigit Meta * @param meta Sigit Meta @@ -74,8 +26,8 @@ export const useSigitMeta = (meta: Meta): FlatMeta => { const [kind, setKind] = useState() const [tags, setTags] = useState() const [createdAt, setCreatedAt] = useState() - const [submittedBy, setSubmittedBy] = useState<`npub1${string}`>() // submittedBy, pubkey from nostr event - const [exportedBy, setExportedBy] = useState<`npub1${string}`>() // pubkey from export signature nostr event + const [submittedBy, setSubmittedBy] = useState() // submittedBy, pubkey from nostr event (hex) + const [exportedBy, setExportedBy] = useState() // pubkey from export signature nostr event (hex) const [id, setId] = useState() const [sig, setSig] = useState() @@ -108,18 +60,16 @@ export const useSigitMeta = (meta: Meta): FlatMeta => { ;(async function () { try { if (meta.exportSignature) { - const exportSignatureEvent = await parseNostrEvent( - meta.exportSignature - ) + const exportSignatureEvent = parseNostrEvent(meta.exportSignature) if ( verifyEvent(exportSignatureEvent) && exportSignatureEvent.pubkey ) { - setExportedBy(exportSignatureEvent.pubkey as `npub1${string}`) + setExportedBy(exportSignatureEvent.pubkey) } } - const createSignatureEvent = await parseNostrEvent(meta.createSignature) + const createSignatureEvent = parseNostrEvent(meta.createSignature) const { kind, tags, created_at, pubkey, id, sig, content } = createSignatureEvent @@ -129,12 +79,12 @@ export const useSigitMeta = (meta: Meta): FlatMeta => { setTags(tags) // created_at in nostr events are stored in seconds setCreatedAt(fromUnixTimestamp(created_at)) - setSubmittedBy(pubkey as `npub1${string}`) + setSubmittedBy(pubkey) setId(id) setSig(sig) const { title, signers, viewers, fileHashes, markConfig, zipUrl } = - await parseCreateSignatureEventContent(content) + parseCreateSignatureEventContent(content) setTitle(title) setSigners(signers) diff --git a/src/types/core.ts b/src/types/core.ts index f07dbf7..932ebf5 100644 --- a/src/types/core.ts +++ b/src/types/core.ts @@ -1,6 +1,7 @@ import { Mark } from './mark' import { Keys } from '../store/auth/types' import { Event } from 'nostr-tools' +import { SigitStatus, SignStatus } from '../utils' export enum UserRole { signer = 'Signer', @@ -35,11 +36,6 @@ export interface SignedEventContent { marks: Mark[] } -export interface Sigit { - fileUrl: string - meta: Meta -} - export interface OpenTimestamp { nostrId: string value: string @@ -92,3 +88,43 @@ export interface SigitNotification { export function isSigitNotification(obj: unknown): obj is SigitNotification { return typeof (obj as SigitNotification).metaUrl === 'string' } + +/** + * Flattened interface that combines properties `Meta`, `CreateSignatureEventContent`, + * and `Event` (event's fields are made optional and pubkey and created_at replaced with our versions) + */ +export interface FlatMeta + extends Meta, + CreateSignatureEventContent, + Partial> { + submittedBy?: string + + // Optional field only present on exported sigits + // Exporting adds user's pubkey + exportedBy?: string + + // Remove created_at and replace with createdAt + createdAt?: number + + // Validated create signature event + isValid: boolean + + // Decryption + encryptionKey: string | undefined + + // Parsed Document Signatures + parsedSignatureEvents: { + [signer: `npub1${string}`]: DocSignatureEvent + } + + // Calculated completion time + completedAt?: number + + // Calculated status fields + signedStatus: SigitStatus + signersStatus: { + [signer: `npub1${string}`]: SignStatus + } + + timestamps?: OpenTimestamp[] +} diff --git a/src/utils/meta.ts b/src/utils/meta.ts index 8052abf..75e1654 100644 --- a/src/utils/meta.ts +++ b/src/utils/meta.ts @@ -49,9 +49,9 @@ export interface SigitCardDisplayInfo { * @param raw Raw string for parsing * @returns parsed Event */ -export const parseNostrEvent = async (raw: string): Promise => { +export const parseNostrEvent = (raw: string): Event => { try { - const event = await parseJson(raw) + const event = JSON.parse(raw) as Event return event } catch (error) { throw new MetaParseError(MetaParseErrorType.PARSE_ERROR_EVENT, { @@ -66,12 +66,13 @@ export const parseNostrEvent = async (raw: string): Promise => { * @param raw Raw string for parsing * @returns parsed CreateSignatureEventContent */ -export const parseCreateSignatureEventContent = async ( +export const parseCreateSignatureEventContent = ( raw: string -): Promise => { +): CreateSignatureEventContent => { try { - const createSignatureEventContent = - await parseJson(raw) + const createSignatureEventContent = JSON.parse( + raw + ) as CreateSignatureEventContent return createSignatureEventContent } catch (error) { throw new MetaParseError( @@ -89,7 +90,7 @@ export const parseCreateSignatureEventContent = async ( * @param meta Sigit metadata * @returns SigitCardDisplayInfo */ -export const extractSigitCardDisplayInfo = async (meta: Meta) => { +export const extractSigitCardDisplayInfo = (meta: Meta) => { if (!meta?.createSignature) return const sigitInfo: SigitCardDisplayInfo = { @@ -100,14 +101,14 @@ export const extractSigitCardDisplayInfo = async (meta: Meta) => { } try { - const createSignatureEvent = await parseNostrEvent(meta.createSignature) + const createSignatureEvent = parseNostrEvent(meta.createSignature) sigitInfo.isValid = verifyEvent(createSignatureEvent) // created_at in nostr events are stored in seconds sigitInfo.createdAt = fromUnixTimestamp(createSignatureEvent.created_at) - const createSignatureContent = await parseCreateSignatureEventContent( + const createSignatureContent = parseCreateSignatureEventContent( createSignatureEvent.content )