mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-28 20:43:33 +02:00
allow switching verification methods without reload
This commit is contained in:
@@ -47,6 +47,9 @@ const webRtcRecentConnections = new LocalStorageEntry(
|
|||||||
// posting
|
// posting
|
||||||
const addClientTag = new BooleanLocalStorageEntry("add-client-tag", ENABLE_CLIENT_TAG);
|
const addClientTag = new BooleanLocalStorageEntry("add-client-tag", ENABLE_CLIENT_TAG);
|
||||||
|
|
||||||
|
// performance
|
||||||
|
const verifyEventMethod = new LocalStorageEntry("verify-event-method", "wasm"); // wasm, internal, none
|
||||||
|
|
||||||
const localSettings = {
|
const localSettings = {
|
||||||
idbMaxEvents,
|
idbMaxEvents,
|
||||||
wasmPersistForDays,
|
wasmPersistForDays,
|
||||||
@@ -56,6 +59,7 @@ const localSettings = {
|
|||||||
webRtcSignalingRelays,
|
webRtcSignalingRelays,
|
||||||
webRtcRecentConnections,
|
webRtcRecentConnections,
|
||||||
addClientTag,
|
addClientTag,
|
||||||
|
verifyEventMethod,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (import.meta.env.DEV) {
|
if (import.meta.env.DEV) {
|
||||||
|
@@ -1,20 +1,18 @@
|
|||||||
import { NostrEvent, VerifiedEvent, verifiedSymbol, verifyEvent } from "nostr-tools";
|
import { NostrEvent, VerifiedEvent, verifiedSymbol, verifyEvent as internalVerifyEvent } from "nostr-tools";
|
||||||
import { setNostrWasm, verifyEvent as wasmVerifyEvent } from "nostr-tools/wasm";
|
import { setNostrWasm, verifyEvent as wasmVerifyEvent } from "nostr-tools/wasm";
|
||||||
import { logger } from "../helpers/debug";
|
import { logger } from "../helpers/debug";
|
||||||
|
import localSettings from "./local-settings";
|
||||||
const localStorageKey = "verify-event-method";
|
|
||||||
|
|
||||||
const log = logger.extend("VerifyEvent");
|
const log = logger.extend("VerifyEvent");
|
||||||
let selectedMethod = "wasm";
|
let verifyEventMethod: typeof internalVerifyEvent;
|
||||||
let verifyEventMethod: typeof verifyEvent;
|
let alwaysVerifyMethod: typeof internalVerifyEvent;
|
||||||
let alwaysVerify: typeof verifyEvent;
|
|
||||||
|
|
||||||
export function fakeVerifyEvent(event: NostrEvent): event is VerifiedEvent {
|
export function fakeVerifyEvent(event: NostrEvent): event is VerifiedEvent {
|
||||||
return (event[verifiedSymbol] = true);
|
return (event[verifiedSymbol] = true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadWithTimeout() {
|
function loadWithTimeout() {
|
||||||
return new Promise<typeof verifyEvent>((res, rej) => {
|
return new Promise<typeof internalVerifyEvent>((res, rej) => {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
log("Timeout");
|
log("Timeout");
|
||||||
rej(new Error("Timeout"));
|
rej(new Error("Timeout"));
|
||||||
@@ -33,34 +31,41 @@ function loadWithTimeout() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
export default function verifyEvent(event: NostrEvent) {
|
||||||
selectedMethod = localStorage.getItem(localStorageKey) ?? "wasm";
|
return verifyEventMethod(event);
|
||||||
|
}
|
||||||
switch (selectedMethod) {
|
export function alwaysVerify(event: NostrEvent) {
|
||||||
case "wasm":
|
return alwaysVerifyMethod(event);
|
||||||
if (!("WebAssembly" in window)) throw new Error("WebAssembly not supported");
|
|
||||||
log("Loading WebAssembly module");
|
|
||||||
verifyEventMethod = alwaysVerify = await loadWithTimeout();
|
|
||||||
log("Loaded");
|
|
||||||
break;
|
|
||||||
case "none":
|
|
||||||
log("Using fake verify event method");
|
|
||||||
verifyEventMethod = fakeVerifyEvent;
|
|
||||||
alwaysVerify = verifyEvent;
|
|
||||||
break;
|
|
||||||
case "internal":
|
|
||||||
default:
|
|
||||||
log("Using internal nostr-tools");
|
|
||||||
verifyEventMethod = alwaysVerify = verifyEvent;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to initialize event verification method, disabling");
|
|
||||||
console.log(error);
|
|
||||||
|
|
||||||
localStorage.setItem(localStorageKey, "none");
|
|
||||||
verifyEventMethod = alwaysVerify = verifyEvent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { alwaysVerify, selectedMethod };
|
async function updateVerifyMethod() {
|
||||||
export default verifyEventMethod;
|
try {
|
||||||
|
switch (localSettings.verifyEventMethod.value) {
|
||||||
|
case "wasm":
|
||||||
|
if (!("WebAssembly" in window)) throw new Error("WebAssembly not supported");
|
||||||
|
log("Loading WebAssembly module");
|
||||||
|
verifyEventMethod = alwaysVerifyMethod = await loadWithTimeout();
|
||||||
|
log("Loaded");
|
||||||
|
break;
|
||||||
|
case "none":
|
||||||
|
log("Using fake verify event method");
|
||||||
|
verifyEventMethod = fakeVerifyEvent;
|
||||||
|
alwaysVerifyMethod = internalVerifyEvent;
|
||||||
|
break;
|
||||||
|
case "internal":
|
||||||
|
default:
|
||||||
|
log("Using internal nostr-tools");
|
||||||
|
verifyEventMethod = alwaysVerifyMethod = internalVerifyEvent;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to initialize event verification method, disabling");
|
||||||
|
console.log(error);
|
||||||
|
|
||||||
|
localSettings.verifyEventMethod.next("none");
|
||||||
|
verifyEventMethod = alwaysVerifyMethod = internalVerifyEvent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
localSettings.verifyEventMethod.subscribe(updateVerifyMethod);
|
||||||
|
await updateVerifyMethod();
|
||||||
|
@@ -9,20 +9,17 @@ import {
|
|||||||
FormErrorMessage,
|
FormErrorMessage,
|
||||||
Select,
|
Select,
|
||||||
Button,
|
Button,
|
||||||
Text,
|
|
||||||
Heading,
|
Heading,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { useLocalStorage } from "react-use";
|
|
||||||
|
|
||||||
import { safeUrl } from "../../../helpers/parse";
|
import { safeUrl } from "../../../helpers/parse";
|
||||||
import { selectedMethod } from "../../../services/verify-event";
|
|
||||||
import VerticalPageLayout from "../../../components/vertical-page-layout";
|
import VerticalPageLayout from "../../../components/vertical-page-layout";
|
||||||
import useSettingsForm from "../use-settings-form";
|
import useSettingsForm from "../use-settings-form";
|
||||||
|
import useSubject from "../../../hooks/use-subject";
|
||||||
|
import localSettings from "../../../services/local-settings";
|
||||||
|
|
||||||
function VerifyEventSettings() {
|
function VerifyEventSettings() {
|
||||||
const [verifyEventMethod, setVerifyEventMethod] = useLocalStorage<string>("verify-event-method", "internal", {
|
const verifyEventMethod = useSubject(localSettings.verifyEventMethod);
|
||||||
raw: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -30,7 +27,11 @@ function VerifyEventSettings() {
|
|||||||
<FormLabel htmlFor="verifyEventMethod" mb="0">
|
<FormLabel htmlFor="verifyEventMethod" mb="0">
|
||||||
Verify event method
|
Verify event method
|
||||||
</FormLabel>
|
</FormLabel>
|
||||||
<Select value={verifyEventMethod} onChange={(e) => setVerifyEventMethod(e.target.value)} maxW="sm">
|
<Select
|
||||||
|
value={verifyEventMethod}
|
||||||
|
onChange={(e) => localSettings.verifyEventMethod.next(e.target.value)}
|
||||||
|
maxW="sm"
|
||||||
|
>
|
||||||
<option value="wasm">WebAssembly</option>
|
<option value="wasm">WebAssembly</option>
|
||||||
<option value="internal">Internal</option>
|
<option value="internal">Internal</option>
|
||||||
<option value="none">None</option>
|
<option value="none">None</option>
|
||||||
@@ -38,17 +39,6 @@ function VerifyEventSettings() {
|
|||||||
<FormHelperText>Default: All events signatures are checked</FormHelperText>
|
<FormHelperText>Default: All events signatures are checked</FormHelperText>
|
||||||
<FormHelperText>WebAssembly: Events signatures are checked in a separate thread</FormHelperText>
|
<FormHelperText>WebAssembly: Events signatures are checked in a separate thread</FormHelperText>
|
||||||
<FormHelperText>None: Only Profiles, Follows, and replaceable event signatures are checked</FormHelperText>
|
<FormHelperText>None: Only Profiles, Follows, and replaceable event signatures are checked</FormHelperText>
|
||||||
|
|
||||||
{selectedMethod !== verifyEventMethod && (
|
|
||||||
<>
|
|
||||||
<Text color="blue.500" mt="2">
|
|
||||||
NOTE: You must reload the app for this setting to take effect
|
|
||||||
</Text>
|
|
||||||
<Button colorScheme="primary" size="sm" onClick={() => location.reload()}>
|
|
||||||
Reload App
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user