From f09d9b23787f1ce9b375e453512d639a4d793938 Mon Sep 17 00:00:00 2001 From: enes Date: Tue, 8 Oct 2024 20:14:44 +0200 Subject: [PATCH] refactor(ts): remove type assertion --- src/controllers/NostrController.ts | 11 +++++------ src/hooks/useSigitMeta.tsx | 3 +-- .../LoginMethodStrategy/PrivateKeyStrategy.ts | 12 ++++++------ src/utils/misc.ts | 3 +-- src/utils/nostr.ts | 18 ++++++++---------- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/controllers/NostrController.ts b/src/controllers/NostrController.ts index 15274f5..c3d2908 100644 --- a/src/controllers/NostrController.ts +++ b/src/controllers/NostrController.ts @@ -1,7 +1,6 @@ import { EventTemplate, UnsignedEvent } from 'nostr-tools' import { WindowNostr } from 'nostr-tools/nip07' import { EventEmitter } from 'tseep' -import { AuthState } from '../store/auth/types' import store from '../store/store' import { SignedEvent } from '../types' import { LoginMethodContext } from '../services/LoginMethodStrategy/loginMethodContext' @@ -37,7 +36,7 @@ export class NostrController extends EventEmitter { */ nip44Encrypt = async (receiver: string, content: string) => { // Retrieve the current login method from the application's redux state. - const loginMethod = (store.getState().auth as AuthState).loginMethod + const loginMethod = store.getState().auth.loginMethod const context = new LoginMethodContext(loginMethod) // Handle encryption when the login method is via an extension. @@ -54,7 +53,7 @@ export class NostrController extends EventEmitter { */ nip44Decrypt = async (sender: string, content: string) => { // Retrieve the current login method from the application's redux state. - const loginMethod = (store.getState().auth as AuthState).loginMethod + const loginMethod = store.getState().auth.loginMethod const context = new LoginMethodContext(loginMethod) // Handle decryption @@ -70,14 +69,14 @@ export class NostrController extends EventEmitter { signEvent = async ( event: UnsignedEvent | EventTemplate ): Promise => { - const loginMethod = (store.getState().auth as AuthState).loginMethod + const loginMethod = store.getState().auth.loginMethod const context = new LoginMethodContext(loginMethod) return await context.signEvent(event) } nip04Encrypt = async (receiver: string, content: string): Promise => { - const loginMethod = (store.getState().auth as AuthState).loginMethod + const loginMethod = store.getState().auth.loginMethod const context = new LoginMethodContext(loginMethod) return await context.nip04Encrypt(receiver, content) @@ -91,7 +90,7 @@ export class NostrController extends EventEmitter { * @returns A promise that resolves to the decrypted content. */ nip04Decrypt = async (sender: string, content: string): Promise => { - const loginMethod = (store.getState().auth as AuthState).loginMethod + const loginMethod = store.getState().auth.loginMethod const context = new LoginMethodContext(loginMethod) return await context.nip04Decrypt(sender, content) diff --git a/src/hooks/useSigitMeta.tsx b/src/hooks/useSigitMeta.tsx index 22a6a6d..c794496 100644 --- a/src/hooks/useSigitMeta.tsx +++ b/src/hooks/useSigitMeta.tsx @@ -18,7 +18,6 @@ import { toast } from 'react-toastify' import { verifyEvent } from 'nostr-tools' import { Event } from 'nostr-tools' import store from '../store/store' -import { AuthState } from '../store/auth/types' import { NostrController } from '../controllers' import { MetaParseError } from '../types/errors/MetaParseError' @@ -143,7 +142,7 @@ export const useSigitMeta = (meta: Meta): FlatMeta => { if (meta.keys) { const { sender, keys } = meta.keys // Retrieve the user's public key from the state - const usersPubkey = (store.getState().auth as AuthState).usersPubkey! + const usersPubkey = store.getState().auth.usersPubkey! const usersNpub = hexToNpub(usersPubkey) // Check if the user's public key is in the keys object diff --git a/src/services/LoginMethodStrategy/PrivateKeyStrategy.ts b/src/services/LoginMethodStrategy/PrivateKeyStrategy.ts index 230cd5c..c92ceb6 100644 --- a/src/services/LoginMethodStrategy/PrivateKeyStrategy.ts +++ b/src/services/LoginMethodStrategy/PrivateKeyStrategy.ts @@ -8,7 +8,7 @@ import { } from 'nostr-tools' import { SignedEvent } from '../../types' import store from '../../store/store' -import { AuthState, LoginMethod } from '../../store/auth/types' +import { LoginMethod } from '../../store/auth/types' import { LoginMethodStrategy } from './loginMethodStrategy' import { verifySignedEvent } from '../../utils/nostr' @@ -20,7 +20,7 @@ import { verifySignedEvent } from '../../utils/nostr' */ export class PrivateKeyStrategy extends LoginMethodStrategy { async nip04Encrypt(receiver: string, content: string): Promise { - const keys = (store.getState().auth as AuthState).keyPair + const keys = store.getState().auth.keyPair if (!keys) { throw new Error( @@ -36,7 +36,7 @@ export class PrivateKeyStrategy extends LoginMethodStrategy { } async nip04Decrypt(sender: string, content: string): Promise { - const keys = (store.getState().auth as AuthState).keyPair + const keys = store.getState().auth.keyPair if (!keys) { throw new Error( @@ -52,7 +52,7 @@ export class PrivateKeyStrategy extends LoginMethodStrategy { } async nip44Encrypt(receiver: string, content: string): Promise { - const keys = (store.getState().auth as AuthState).keyPair + const keys = store.getState().auth.keyPair // Check if the private and public key pair is available. if (!keys) { @@ -78,7 +78,7 @@ export class PrivateKeyStrategy extends LoginMethodStrategy { } async nip44Decrypt(sender: string, content: string): Promise { - const keys = (store.getState().auth as AuthState).keyPair + const keys = store.getState().auth.keyPair // Check if the private and public key pair is available. if (!keys) { @@ -104,7 +104,7 @@ export class PrivateKeyStrategy extends LoginMethodStrategy { } async signEvent(event: UnsignedEvent | EventTemplate): Promise { - const keys = (store.getState().auth as AuthState).keyPair + const keys = store.getState().auth.keyPair if (!keys) { return Promise.reject( diff --git a/src/utils/misc.ts b/src/utils/misc.ts index c84231b..3942a5b 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -10,7 +10,6 @@ import { } from 'nostr-tools' import { toast } from 'react-toastify' import { NostrController } from '../controllers' -import { AuthState } from '../store/auth/types' import store from '../store/store' import { CreateSignatureEventContent, Meta } from '../types' import { hexToNpub, unixNow } from './nostr' @@ -232,7 +231,7 @@ export const extractZipUrlAndEncryptionKey = async (meta: Meta) => { const zipUrl = createSignatureContent.zipUrl // Retrieve the user's public key from the state - const usersPubkey = (store.getState().auth as AuthState).usersPubkey! + const usersPubkey = store.getState().auth.usersPubkey! const usersNpub = hexToNpub(usersPubkey) // Return null if the metadata does not contain keys diff --git a/src/utils/nostr.ts b/src/utils/nostr.ts index d6be22e..8699ae9 100644 --- a/src/utils/nostr.ts +++ b/src/utils/nostr.ts @@ -27,8 +27,7 @@ import { updateProcessedGiftWraps, updateUserAppData as updateUserAppDataAction } from '../store/actions' -import { AuthState, Keys } from '../store/auth/types' -import { RelaysState } from '../store/relays/types' +import { Keys } from '../store/auth/types' import store from '../store/store' import { Meta, ProfileMetadata, SignedEvent, UserAppData } from '../types' import { getDefaultRelayMap } from './relays' @@ -41,7 +40,7 @@ import { SIGIT_BLOSSOM } from './const.ts' * Generates a `d` tag for userAppData */ const getDTagForUserAppData = async (): Promise => { - const isLoggedIn = store.getState().auth?.loggedIn + const isLoggedIn = store.getState().auth.loggedIn const pubkey = store.getState().auth?.usersPubkey if (!isLoggedIn || !pubkey) { @@ -360,7 +359,7 @@ export const getUsersAppData = async (): Promise => { const relays: string[] = [] // Retrieve the user's public key and relay map from the Redux store - const usersPubkey = (store.getState().auth as AuthState).usersPubkey! + const usersPubkey = store.getState().auth.usersPubkey! const relayMap = store.getState().relays?.map // Check if relayMap is undefined in the Redux store @@ -572,7 +571,7 @@ export const updateUsersAppData = async (meta: Meta) => { }) } - const usersPubkey = (store.getState().auth as AuthState).usersPubkey! + const usersPubkey = store.getState().auth.usersPubkey! // encrypt content for storing in kind 30078 event const nostrController = NostrController.getInstance() @@ -619,8 +618,7 @@ export const updateUsersAppData = async (meta: Meta) => { if (!signedEvent) return null - const relayMap = - (store.getState().relays as RelaysState).map || getDefaultRelayMap() + const relayMap = store.getState().relays.map || getDefaultRelayMap() const writeRelays = Object.keys(relayMap).filter((key) => relayMap[key].write) const publishResult = await Promise.race([ @@ -878,8 +876,8 @@ export const subscribeForSigits = async (pubkey: string) => { } const processReceivedEvent = async (event: Event, difficulty: number = 5) => { - const processedEvents = (store.getState().userAppData as UserAppData) - .processedGiftWraps + const processedEvents = store.getState().userAppData?.processedGiftWraps || [] + if (processedEvents.includes(event.id)) return store.dispatch(updateProcessedGiftWraps([...processedEvents, event.id])) @@ -930,7 +928,7 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => { */ export const sendNotification = async (receiver: string, meta: Meta) => { // Retrieve the user's public key from the state - const usersPubkey = (store.getState().auth as AuthState).usersPubkey! + const usersPubkey = store.getState().auth.usersPubkey! // Create an unsigned event object with the provided metadata const unsignedEvent: UnsignedEvent = {