refactor(offline): useSigitMeta and remove async ops when parsing json
This commit is contained in:
parent
bcd57138ca
commit
7b2537e355
@ -1,11 +1,5 @@
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import {
|
import { DocSignatureEvent, Meta, SignedEventContent, FlatMeta } from '../types'
|
||||||
CreateSignatureEventContent,
|
|
||||||
DocSignatureEvent,
|
|
||||||
Meta,
|
|
||||||
SignedEventContent,
|
|
||||||
OpenTimestamp
|
|
||||||
} from '../types'
|
|
||||||
import { Mark } from '../types/mark'
|
import { Mark } from '../types/mark'
|
||||||
import {
|
import {
|
||||||
fromUnixTimestamp,
|
fromUnixTimestamp,
|
||||||
@ -17,53 +11,11 @@ import {
|
|||||||
} from '../utils'
|
} from '../utils'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { verifyEvent } from 'nostr-tools'
|
import { verifyEvent } from 'nostr-tools'
|
||||||
import { Event } from 'nostr-tools'
|
|
||||||
import store from '../store/store'
|
import store from '../store/store'
|
||||||
import { NostrController } from '../controllers'
|
import { NostrController } from '../controllers'
|
||||||
import { MetaParseError } from '../types/errors/MetaParseError'
|
import { MetaParseError } from '../types/errors/MetaParseError'
|
||||||
import { MARK_TYPE_CONFIG } from '../components/MarkTypeStrategy/MarkStrategy'
|
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<Omit<Event, 'pubkey' | 'created_at'>> {
|
|
||||||
// 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
|
* Custom use hook for parsing the Sigit Meta
|
||||||
* @param meta Sigit Meta
|
* @param meta Sigit Meta
|
||||||
@ -74,8 +26,8 @@ export const useSigitMeta = (meta: Meta): FlatMeta => {
|
|||||||
const [kind, setKind] = useState<number>()
|
const [kind, setKind] = useState<number>()
|
||||||
const [tags, setTags] = useState<string[][]>()
|
const [tags, setTags] = useState<string[][]>()
|
||||||
const [createdAt, setCreatedAt] = useState<number>()
|
const [createdAt, setCreatedAt] = useState<number>()
|
||||||
const [submittedBy, setSubmittedBy] = useState<`npub1${string}`>() // submittedBy, pubkey from nostr event
|
const [submittedBy, setSubmittedBy] = useState<string>() // submittedBy, pubkey from nostr event (hex)
|
||||||
const [exportedBy, setExportedBy] = useState<`npub1${string}`>() // pubkey from export signature nostr event
|
const [exportedBy, setExportedBy] = useState<string>() // pubkey from export signature nostr event (hex)
|
||||||
const [id, setId] = useState<string>()
|
const [id, setId] = useState<string>()
|
||||||
const [sig, setSig] = useState<string>()
|
const [sig, setSig] = useState<string>()
|
||||||
|
|
||||||
@ -108,18 +60,16 @@ export const useSigitMeta = (meta: Meta): FlatMeta => {
|
|||||||
;(async function () {
|
;(async function () {
|
||||||
try {
|
try {
|
||||||
if (meta.exportSignature) {
|
if (meta.exportSignature) {
|
||||||
const exportSignatureEvent = await parseNostrEvent(
|
const exportSignatureEvent = parseNostrEvent(meta.exportSignature)
|
||||||
meta.exportSignature
|
|
||||||
)
|
|
||||||
if (
|
if (
|
||||||
verifyEvent(exportSignatureEvent) &&
|
verifyEvent(exportSignatureEvent) &&
|
||||||
exportSignatureEvent.pubkey
|
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 } =
|
const { kind, tags, created_at, pubkey, id, sig, content } =
|
||||||
createSignatureEvent
|
createSignatureEvent
|
||||||
@ -129,12 +79,12 @@ export const useSigitMeta = (meta: Meta): FlatMeta => {
|
|||||||
setTags(tags)
|
setTags(tags)
|
||||||
// created_at in nostr events are stored in seconds
|
// created_at in nostr events are stored in seconds
|
||||||
setCreatedAt(fromUnixTimestamp(created_at))
|
setCreatedAt(fromUnixTimestamp(created_at))
|
||||||
setSubmittedBy(pubkey as `npub1${string}`)
|
setSubmittedBy(pubkey)
|
||||||
setId(id)
|
setId(id)
|
||||||
setSig(sig)
|
setSig(sig)
|
||||||
|
|
||||||
const { title, signers, viewers, fileHashes, markConfig, zipUrl } =
|
const { title, signers, viewers, fileHashes, markConfig, zipUrl } =
|
||||||
await parseCreateSignatureEventContent(content)
|
parseCreateSignatureEventContent(content)
|
||||||
|
|
||||||
setTitle(title)
|
setTitle(title)
|
||||||
setSigners(signers)
|
setSigners(signers)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Mark } from './mark'
|
import { Mark } from './mark'
|
||||||
import { Keys } from '../store/auth/types'
|
import { Keys } from '../store/auth/types'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
|
import { SigitStatus, SignStatus } from '../utils'
|
||||||
|
|
||||||
export enum UserRole {
|
export enum UserRole {
|
||||||
signer = 'Signer',
|
signer = 'Signer',
|
||||||
@ -35,11 +36,6 @@ export interface SignedEventContent {
|
|||||||
marks: Mark[]
|
marks: Mark[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Sigit {
|
|
||||||
fileUrl: string
|
|
||||||
meta: Meta
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface OpenTimestamp {
|
export interface OpenTimestamp {
|
||||||
nostrId: string
|
nostrId: string
|
||||||
value: string
|
value: string
|
||||||
@ -92,3 +88,43 @@ export interface SigitNotification {
|
|||||||
export function isSigitNotification(obj: unknown): obj is SigitNotification {
|
export function isSigitNotification(obj: unknown): obj is SigitNotification {
|
||||||
return typeof (obj as SigitNotification).metaUrl === 'string'
|
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<Omit<Event, 'pubkey' | 'created_at'>> {
|
||||||
|
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[]
|
||||||
|
}
|
||||||
|
@ -49,9 +49,9 @@ export interface SigitCardDisplayInfo {
|
|||||||
* @param raw Raw string for parsing
|
* @param raw Raw string for parsing
|
||||||
* @returns parsed Event
|
* @returns parsed Event
|
||||||
*/
|
*/
|
||||||
export const parseNostrEvent = async (raw: string): Promise<Event> => {
|
export const parseNostrEvent = (raw: string): Event => {
|
||||||
try {
|
try {
|
||||||
const event = await parseJson<Event>(raw)
|
const event = JSON.parse(raw) as Event
|
||||||
return event
|
return event
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new MetaParseError(MetaParseErrorType.PARSE_ERROR_EVENT, {
|
throw new MetaParseError(MetaParseErrorType.PARSE_ERROR_EVENT, {
|
||||||
@ -66,12 +66,13 @@ export const parseNostrEvent = async (raw: string): Promise<Event> => {
|
|||||||
* @param raw Raw string for parsing
|
* @param raw Raw string for parsing
|
||||||
* @returns parsed CreateSignatureEventContent
|
* @returns parsed CreateSignatureEventContent
|
||||||
*/
|
*/
|
||||||
export const parseCreateSignatureEventContent = async (
|
export const parseCreateSignatureEventContent = (
|
||||||
raw: string
|
raw: string
|
||||||
): Promise<CreateSignatureEventContent> => {
|
): CreateSignatureEventContent => {
|
||||||
try {
|
try {
|
||||||
const createSignatureEventContent =
|
const createSignatureEventContent = JSON.parse(
|
||||||
await parseJson<CreateSignatureEventContent>(raw)
|
raw
|
||||||
|
) as CreateSignatureEventContent
|
||||||
return createSignatureEventContent
|
return createSignatureEventContent
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new MetaParseError(
|
throw new MetaParseError(
|
||||||
@ -89,7 +90,7 @@ export const parseCreateSignatureEventContent = async (
|
|||||||
* @param meta Sigit metadata
|
* @param meta Sigit metadata
|
||||||
* @returns SigitCardDisplayInfo
|
* @returns SigitCardDisplayInfo
|
||||||
*/
|
*/
|
||||||
export const extractSigitCardDisplayInfo = async (meta: Meta) => {
|
export const extractSigitCardDisplayInfo = (meta: Meta) => {
|
||||||
if (!meta?.createSignature) return
|
if (!meta?.createSignature) return
|
||||||
|
|
||||||
const sigitInfo: SigitCardDisplayInfo = {
|
const sigitInfo: SigitCardDisplayInfo = {
|
||||||
@ -100,14 +101,14 @@ export const extractSigitCardDisplayInfo = async (meta: Meta) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const createSignatureEvent = await parseNostrEvent(meta.createSignature)
|
const createSignatureEvent = parseNostrEvent(meta.createSignature)
|
||||||
|
|
||||||
sigitInfo.isValid = verifyEvent(createSignatureEvent)
|
sigitInfo.isValid = verifyEvent(createSignatureEvent)
|
||||||
|
|
||||||
// created_at in nostr events are stored in seconds
|
// created_at in nostr events are stored in seconds
|
||||||
sigitInfo.createdAt = fromUnixTimestamp(createSignatureEvent.created_at)
|
sigitInfo.createdAt = fromUnixTimestamp(createSignatureEvent.created_at)
|
||||||
|
|
||||||
const createSignatureContent = await parseCreateSignatureEventContent(
|
const createSignatureContent = parseCreateSignatureEventContent(
|
||||||
createSignatureEvent.content
|
createSignatureEvent.content
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user