don't store default settings

This commit is contained in:
Paul Miller
2023-08-03 15:35:28 -05:00
committed by Tony Giorgio
parent 0295e7a7cf
commit 62cdd218bc
6 changed files with 128 additions and 130 deletions

View File

@@ -3,8 +3,8 @@ import { ParentComponent, createSignal } from "solid-js";
import { DIALOG_CONTENT, DIALOG_POSITIONER, OVERLAY } from "./DetailsModal"; import { DIALOG_CONTENT, DIALOG_POSITIONER, OVERLAY } from "./DetailsModal";
import { ModalCloseButton, SmallHeader } from "./layout"; import { ModalCloseButton, SmallHeader } from "./layout";
import { ExternalLink } from "./layout/ExternalLink"; import { ExternalLink } from "./layout/ExternalLink";
import { getExistingSettings } from "~/logic/mutinyWalletSetup";
import { useI18n } from "~/i18n/context"; import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
export function BetaWarningModal() { export function BetaWarningModal() {
const i18n = useI18n(); const i18n = useI18n();
@@ -34,9 +34,11 @@ export const WarningModal: ParentComponent<{
linkText: string; linkText: string;
title: string; title: string;
}> = (props) => { }> = (props) => {
const [state, _actions] = useMegaStore();
const [open, setOpen] = createSignal( const [open, setOpen] = createSignal(
localStorage.getItem("betaWarned") !== "true" && localStorage.getItem("betaWarned") !== "true" &&
getExistingSettings().network === "bitcoin" state.settings?.network === "bitcoin"
); );
function close() { function close() {

View File

@@ -19,7 +19,7 @@ export function DecryptDialog() {
e.preventDefault(); e.preventDefault();
setLoading(true); setLoading(true);
try { try {
await actions.setupMutinyWallet(undefined, password()); await actions.setup(password());
// If we get this far and the state stills wants a password that means the password was wrong // If we get this far and the state stills wants a password that means the password was wrong
if (state.needs_password) { if (state.needs_password) {

View File

@@ -3,8 +3,9 @@
import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm"; import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm";
export type Network = "bitcoin" | "testnet" | "regtest" | "signet"; export type Network = "bitcoin" | "testnet" | "regtest" | "signet";
export type MutinyWalletSettingStrings = { export type MutinyWalletSettingStrings = {
network?: Network; network?: string;
proxy?: string; proxy?: string;
esplora?: string; esplora?: string;
rgs?: string; rgs?: string;
@@ -15,114 +16,108 @@ export type MutinyWalletSettingStrings = {
scorer?: string; scorer?: string;
}; };
export function getExistingSettings(): MutinyWalletSettingStrings { const SETTINGS_KEYS = [
const network = {
localStorage.getItem("MUTINY_SETTINGS_network") || name: "network",
import.meta.env.VITE_NETWORK; storageKey: "USER_SETTINGS_network",
const proxy = default: import.meta.env.VITE_NETWORK
localStorage.getItem("MUTINY_SETTINGS_proxy") || },
import.meta.env.VITE_PROXY; {
const esplora = name: "proxy",
localStorage.getItem("MUTINY_SETTINGS_esplora") || storageKey: "USER_SETTINGS_proxy",
import.meta.env.VITE_ESPLORA; default: import.meta.env.VITE_PROXY
const rgs = },
localStorage.getItem("MUTINY_SETTINGS_rgs") || import.meta.env.VITE_RGS; {
const lsp = name: "esplora",
localStorage.getItem("MUTINY_SETTINGS_lsp") || import.meta.env.VITE_LSP; storageKey: "USER_SETTINGS_esplora",
const auth = default: import.meta.env.VITE_ESPLORA
localStorage.getItem("MUTINY_SETTINGS_auth") || },
import.meta.env.VITE_AUTH; {
const subscriptions = name: "rgs",
localStorage.getItem("MUTINY_SETTINGS_subscriptions") || storageKey: "USER_SETTINGS_rgs",
import.meta.env.VITE_SUBSCRIPTIONS; default: import.meta.env.VITE_RGS
const storage = },
localStorage.getItem("MUTINY_SETTINGS_storage") || {
import.meta.env.VITE_STORAGE; name: "lsp",
const scorer = storageKey: "USER_SETTINGS_lsp",
localStorage.getItem("MUTINY_SETTINGS_scorer") || default: import.meta.env.VITE_LSP
import.meta.env.VITE_SCORER; },
{
name: "auth",
storageKey: "USER_SETTINGS_auth",
default: import.meta.env.VITE_AUTH
},
{
name: "subscriptions",
storageKey: "USER_SETTINGS_subscriptions",
default: import.meta.env.VITE_SUBSCRIPTIONS
},
{
name: "storage",
storageKey: "USER_SETTINGS_storage",
default: import.meta.env.VITE_STORAGE
},
{
name: "scorer",
storageKey: "USER_SETTINGS_scorer",
default: import.meta.env.VITE_SCORER
}
];
return { function getItemOrDefault(
network, storageKey: string,
proxy, defaultValue: string
esplora, ): string | undefined {
rgs, const item = localStorage.getItem(storageKey);
lsp, if (item === "") {
auth, return undefined;
subscriptions, } else if (item === null) {
storage, return defaultValue;
scorer } else {
}; return item;
}
} }
export async function setAndGetMutinySettings( function setItemIfNotDefault(
settings?: MutinyWalletSettingStrings key: string,
): Promise<MutinyWalletSettingStrings> { override: string,
let { defaultValue: string
network, ) {
proxy, if (override === defaultValue) {
esplora, localStorage.removeItem(key);
rgs, } else {
lsp, localStorage.setItem(key, override);
auth, }
subscriptions, }
storage,
scorer
} = settings || {};
const existingSettings = getExistingSettings(); export async function getSettings() {
const settings = <MutinyWalletSettingStrings>{};
try { SETTINGS_KEYS.forEach(({ name, storageKey, default: defaultValue }) => {
network = network || existingSettings.network; const n = name as keyof MutinyWalletSettingStrings;
proxy = proxy || existingSettings.proxy; const item = getItemOrDefault(storageKey, defaultValue);
esplora = esplora || existingSettings.esplora; settings[n] = item as string;
rgs = rgs || existingSettings.rgs; });
lsp = lsp || existingSettings.lsp;
auth = auth || existingSettings.auth;
subscriptions = subscriptions || existingSettings.subscriptions;
storage = storage || existingSettings.storage;
scorer = scorer || existingSettings.scorer;
if (!network || !proxy || !esplora) { if (!settings.network || !settings.proxy || !settings.esplora) {
throw new Error( throw new Error(
"Missing a default setting for network, proxy, or esplora. Check your .env file to make sure it looks like .env.sample" "Missing a default setting for network, proxy, or esplora. Check your .env file to make sure it looks like .env.sample"
); );
} }
localStorage.setItem("MUTINY_SETTINGS_network", network); return settings;
localStorage.setItem("MUTINY_SETTINGS_proxy", proxy); }
localStorage.setItem("MUTINY_SETTINGS_esplora", esplora);
if (!rgs || !lsp) { export async function setSettings(newSettings: MutinyWalletSettingStrings) {
console.warn("RGS or LSP not set"); SETTINGS_KEYS.forEach(({ name, storageKey, default: defaultValue }) => {
} const n = name as keyof MutinyWalletSettingStrings;
const override = newSettings[n];
rgs && localStorage.setItem("MUTINY_SETTINGS_rgs", rgs); // If the value is in the newSettings, and it's not the default, set it in localstorage
lsp && localStorage.setItem("MUTINY_SETTINGS_lsp", lsp); // Also, "" is a valid value, so we only want to reject undefined
auth && localStorage.setItem("MUTINY_SETTINGS_auth", auth); if (override !== undefined) {
subscriptions && setItemIfNotDefault(storageKey, override, defaultValue);
localStorage.setItem(
"MUTINY_SETTINGS_subscriptions",
subscriptions
);
storage && localStorage.setItem("MUTINY_SETTINGS_storage", storage);
scorer && localStorage.setItem("MUTINY_SETTINGS_scorer", scorer);
return {
network,
proxy,
esplora,
rgs,
lsp,
auth,
subscriptions,
storage,
scorer
};
} catch (error) {
console.error(error);
throw error;
} }
});
} }
export async function checkForWasm() { export async function checkForWasm() {
@@ -167,10 +162,11 @@ export async function initializeWasm() {
} }
export async function setupMutinyWallet( export async function setupMutinyWallet(
settings?: MutinyWalletSettingStrings, settings: MutinyWalletSettingStrings,
password?: string password?: string
): Promise<MutinyWallet> { ): Promise<MutinyWallet> {
console.log("Starting setup..."); console.log("Starting setup...");
const { const {
network, network,
proxy, proxy,
@@ -181,7 +177,8 @@ export async function setupMutinyWallet(
subscriptions, subscriptions,
storage, storage,
scorer scorer
} = await setAndGetMutinySettings(settings); } = settings;
console.log("Initializing Mutiny Manager"); console.log("Initializing Mutiny Manager");
console.log("Using network", network); console.log("Using network", network);
console.log("Using proxy", proxy); console.log("Using proxy", proxy);
@@ -192,6 +189,7 @@ export async function setupMutinyWallet(
console.log("Using subscriptions address", subscriptions); console.log("Using subscriptions address", subscriptions);
console.log("Using storage address", storage); console.log("Using storage address", storage);
console.log("Using scorer address", scorer); console.log("Using scorer address", scorer);
const mutinyWallet = await new MutinyWallet( const mutinyWallet = await new MutinyWallet(
// Password // Password
password ? password : undefined, password ? password : undefined,

View File

@@ -75,10 +75,7 @@ export default function Swap() {
} }
const hasLsp = () => { const hasLsp = () => {
return ( return !!state.settings?.lsp;
!!localStorage.getItem("MUTINY_SETTINGS_lsp") ||
!!import.meta.env.VITE_LSP
);
}; };
const getPeers = async () => { const getPeers = async () => {

View File

@@ -2,8 +2,7 @@ import { createForm, url } from "@modular-forms/solid";
import { TextField } from "~/components/layout/TextField"; import { TextField } from "~/components/layout/TextField";
import { import {
MutinyWalletSettingStrings, MutinyWalletSettingStrings,
getExistingSettings, setSettings
setAndGetMutinySettings
} from "~/logic/mutinyWalletSetup"; } from "~/logic/mutinyWalletSetup";
import { import {
Button, Button,
@@ -20,19 +19,19 @@ import { ExternalLink } from "~/components/layout/ExternalLink";
import { BackLink } from "~/components/layout/BackLink"; import { BackLink } from "~/components/layout/BackLink";
import NavBar from "~/components/NavBar"; import NavBar from "~/components/NavBar";
import { useI18n } from "~/i18n/context"; import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
export function SettingsStringsEditor() { export function SettingsStringsEditor() {
const i18n = useI18n(); const i18n = useI18n();
const existingSettings = getExistingSettings(); const [state, _actions] = useMegaStore();
const [settingsForm, { Form, Field }] = const [settingsForm, { Form, Field }] =
createForm<MutinyWalletSettingStrings>({ createForm<MutinyWalletSettingStrings>({
initialValues: existingSettings initialValues: state.settings
}); });
async function handleSubmit(values: MutinyWalletSettingStrings) { async function handleSubmit(values: MutinyWalletSettingStrings) {
try { try {
const newSettings = { ...existingSettings, ...values }; await setSettings(values);
await setAndGetMutinySettings(newSettings);
window.location.reload(); window.location.reload();
} catch (e) { } catch (e) {
console.error(e); console.error(e);

View File

@@ -13,6 +13,7 @@ import { createStore } from "solid-js/store";
import { import {
MutinyWalletSettingStrings, MutinyWalletSettingStrings,
doubleInitDefense, doubleInitDefense,
getSettings,
initializeWasm, initializeWasm,
setupMutinyWallet setupMutinyWallet
} from "~/logic/mutinyWalletSetup"; } from "~/logic/mutinyWalletSetup";
@@ -51,12 +52,10 @@ export type MegaStore = [
readonly mutiny_plus: boolean; readonly mutiny_plus: boolean;
needs_password: boolean; needs_password: boolean;
load_stage: LoadStage; load_stage: LoadStage;
settings?: MutinyWalletSettingStrings;
}, },
{ {
setupMutinyWallet( setup(password?: string): Promise<void>;
settings?: MutinyWalletSettingStrings,
password?: string
): Promise<void>;
deleteMutinyWallet(): Promise<void>; deleteMutinyWallet(): Promise<void>;
setScanResult(scan_result: ParsedParams | undefined): void; setScanResult(scan_result: ParsedParams | undefined): void;
sync(): Promise<void>; sync(): Promise<void>;
@@ -95,7 +94,8 @@ export const Provider: ParentComponent = (props) => {
else return true; else return true;
}, },
needs_password: false, needs_password: false,
load_stage: "fresh" as LoadStage load_stage: "fresh" as LoadStage,
settings: undefined as MutinyWalletSettingStrings | undefined
}); });
const actions = { const actions = {
@@ -119,10 +119,7 @@ export const Provider: ParentComponent = (props) => {
console.error(e); console.error(e);
} }
}, },
async setupMutinyWallet( async setup(password?: string): Promise<void> {
settings?: MutinyWalletSettingStrings,
password?: string
): Promise<void> {
try { try {
// If we're already in an error state there should be no reason to continue // If we're already in an error state there should be no reason to continue
if (state.setup_error) { if (state.setup_error) {
@@ -139,11 +136,16 @@ export const Provider: ParentComponent = (props) => {
await initializeWasm(); await initializeWasm();
setState({ load_stage: "setup" }); setState({ load_stage: "setup" });
const settings = await getSettings();
const mutinyWallet = await setupMutinyWallet( const mutinyWallet = await setupMutinyWallet(
settings, settings,
password password
); );
// Give other components access to settings via the store
setState({ settings: settings });
// If we get this far then we don't need the password anymore // If we get this far then we don't need the password anymore
setState({ needs_password: false }); setState({ needs_password: false });
@@ -280,7 +282,7 @@ export const Provider: ParentComponent = (props) => {
console.log("running setup node manager..."); console.log("running setup node manager...");
actions actions
.setupMutinyWallet() .setup()
.then(() => console.log("node manager setup done")); .then(() => console.log("node manager setup done"));
// Setup an event listener to stop the mutiny wallet when the page unloads // Setup an event listener to stop the mutiny wallet when the page unloads