Skip to content

Commit

Permalink
add import nostr profile
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Feb 12, 2024
1 parent 2454c67 commit 8063c2a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 159 deletions.
16 changes: 1 addition & 15 deletions src/components/Activity.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TagItem } from "@mutinywallet/mutiny-wasm";
import { A, cache, createAsync, revalidate } from "@solidjs/router";
import { cache, createAsync, revalidate } from "@solidjs/router";
import { createEffect, createSignal, For, Match, Show, Switch } from "solid-js";

import shuffle from "~/assets/icons/shuffle.svg";
Expand All @@ -9,7 +9,6 @@ import {
HackActivityType,
NiceP
} from "~/components";
import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
import { timeAgo } from "~/utils";

Expand Down Expand Up @@ -143,25 +142,12 @@ export function UnifiedActivityItem(props: {
}
/>
</button>
{/* <pre>{JSON.stringify(props.item, null, 2)}</pre> */}
</>
// <ActivityItem
// // This is actually the ActivityType enum but wasm is hard
// kind={props.item.kind as unknown as HackActivityType}
// labels={props.item.labels}
// contacts={props.item.contacts}
// // FIXME: is this something we can put into node logic?
// amount={props.item.amount_sats || 0}
// date={props.item.last_updated}
// positive={props.item.inbound}
// onClick={click}
// />
);
}

export function CombinedActivity() {
const [state, _actions] = useMegaStore();
const i18n = useI18n();

const [detailsOpen, setDetailsOpen] = createSignal(false);
const [detailsKind, setDetailsKind] = createSignal<HackActivityType>();
Expand Down
14 changes: 6 additions & 8 deletions src/logic/mutinyWalletSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* @refresh reload */

import { Capacitor } from "@capacitor/core";
import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm";
import { SecureStoragePlugin } from "capacitor-secure-storage-plugin";

Expand Down Expand Up @@ -260,13 +259,12 @@ export async function setupMutinyWallet(

let nsec;
// get nsec from secure storage
if (Capacitor.isNativePlatform()) {
try {
const value = await SecureStoragePlugin.get({ key: "nsec" });
nsec = value.value;
} catch (e) {
console.log("No nsec stored");
}
// TODO: might have to check Capacitor.isNativePlatform but I think it's fine
try {
const value = await SecureStoragePlugin.get({ key: "nsec" });
nsec = value.value;
} catch (e) {
console.log("No nsec stored");
}

// if we didn't get an nsec from storage, try to use extension
Expand Down
4 changes: 2 additions & 2 deletions src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
Chat,
Feedback,
Gift as GiftReceive,
ImportProfile,
Main,
NewProfile,
NotFound,
OldMain,
Profile,
Receive,
RequestRoute,
Expand Down Expand Up @@ -160,10 +160,10 @@ export function Router() {
<Route path="/" component={Main} />
<Route path="/setup" component={Setup} />
<Route path="/newprofile" component={NewProfile} />
<Route path="/importprofile" component={ImportProfile} />
<Route path="/profile" component={Profile} />
<Route path="/scratchpad" component={Scratchpad} />
<Route path="/chat/:id" component={Chat} />
<Route path="/oldhome" component={OldMain} />
<Route path="/feedback" component={Feedback} />
<Route path="/gift" component={GiftReceive} />
<Route path="/receive" component={Receive} />
Expand Down
79 changes: 79 additions & 0 deletions src/routes/ImportProfile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { MutinyWallet } from "@mutinywallet/mutiny-wasm";
import { useNavigate } from "@solidjs/router";
import { SecureStoragePlugin } from "capacitor-secure-storage-plugin";
import { createSignal, Show } from "solid-js";

import {
Button,
ButtonLink,
DefaultMain,
InfoBox,
SimpleInput
} from "~/components";

export function ImportProfile() {
const [nsec, setNsec] = createSignal("");
const [saving, setSaving] = createSignal(false);
const [error, setError] = createSignal<string | undefined>();

const navigate = useNavigate();

function handleSkip() {
localStorage.setItem("profile_setup_stage", "skipped");
navigate("/");
}

async function saveNsec() {
setSaving(true);
setError(undefined);
const trimmedNsec = nsec().trim();
try {
const npub = await MutinyWallet.nsec_to_npub(trimmedNsec);
if (!npub) {
throw new Error("Invalid nsec");
}
await SecureStoragePlugin.set({ key: "nsec", value: trimmedNsec });
// TODO: right now we need a reload to set the nsec
window.location.href = "/";
} catch (e) {
console.error(e);
setError("Invalid nsec");
}
setSaving(false);
}

return (
<DefaultMain>
<div class="mx-auto flex max-w-[20rem] flex-1 flex-col items-center gap-4">
<div class="flex-1" />
<h1 class="text-3xl font-semibold">Import nostr profile</h1>
<p class="text-center text-xl font-light text-neutral-200">
Login with an existing nostr account.
<br />
</p>
<div class="flex-1" />
<SimpleInput
value={nsec()}
onInput={(e) => setNsec(e.currentTarget.value)}
placeholder={`Nostr private key (starts with "nsec")`}
/>
<Button layout="full" onClick={saveNsec} loading={saving()}>
Import
</Button>
<Show when={error()}>
<InfoBox accent="red">{error()}</InfoBox>
</Show>
<div class="flex-1" />
<div class="flex flex-col items-center">
<ButtonLink href="/newprofile" intent="text">
Create new nostr profile
</ButtonLink>
<Button onClick={handleSkip} intent="text">
Skip for now
</Button>
</div>
<div class="flex-1" />
</div>
</DefaultMain>
);
}
4 changes: 2 additions & 2 deletions src/routes/NewProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export function NewProfile() {
</Button>
<div class="flex-1" />
<div class="flex flex-col items-center">
<ButtonLink href="#" intent="text">
Import existing
<ButtonLink href="/importprofile" intent="text">
Import existing nostr profile
</ButtonLink>
<Button onClick={handleSkip} intent="text">
Skip for now
Expand Down
114 changes: 0 additions & 114 deletions src/routes/OldMain.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/routes/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export function Setup() {
);

// Let setup know to create a new wallet this time through setup
actions.setShouldCreateNewWallet(true);
await actions.setup();
await actions.setup(undefined, true);

if (!profileSetupStage && !hasNip07) {
navigate("/newprofile");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export * from "./Send";
export * from "./Swap";
export * from "./SwapLightning";
export * from "./Search";
export * from "./OldMain";
export * from "./Profile";
export * from "./Chat";
export * from "./Setup";
export * from "./NewProfile";
export * from "./Request";
export * from "./Scratchpad";
export * from "./ImportProfile";
25 changes: 10 additions & 15 deletions src/state/megaStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ type MegaStore = [
testflightPromptDismissed: boolean;
should_zap_hodl: boolean;
federations?: MutinyFederationIdentity[];
shouldCreateNewWallet?: boolean;
},
{
setup(password?: string): Promise<void>;
setup(
password?: string,
shouldCreateNewWallet?: boolean
): Promise<void>;
deleteMutinyWallet(): Promise<void>;
setScanResult(scan_result: ParsedParams | undefined): void;
sync(): Promise<void>;
Expand All @@ -101,7 +103,6 @@ type MegaStore = [
toggleHodl(): void;
dropMutinyWallet(): void;
refreshFederations(): Promise<void>;
setShouldCreateNewWallet(shouldCreateNewWallet: boolean): void;
}
];

Expand Down Expand Up @@ -144,8 +145,7 @@ export const Provider: ParentComponent = (props) => {
should_zap_hodl: localStorage.getItem("should_zap_hodl") === "true",
testflightPromptDismissed:
localStorage.getItem("testflightPromptDismissed") === "true",
federations: undefined as MutinyFederationIdentity[] | undefined,
shouldCreateNewWallet: false
federations: undefined as MutinyFederationIdentity[] | undefined
});

const actions = {
Expand All @@ -165,7 +165,10 @@ export const Provider: ParentComponent = (props) => {
console.error(e);
}
},
async setup(password?: string): Promise<void> {
async setup(
password?: string,
shouldCreateNewWallet?: boolean
): Promise<void> {
try {
// If we're already in an error state there should be no reason to continue
if (state.setup_error) {
Expand Down Expand Up @@ -194,7 +197,7 @@ export const Provider: ParentComponent = (props) => {
setState({ load_stage: "checking_for_existing_wallet" });
const existing = await MutinyWallet.has_node_manager();

if (!existing && !state.shouldCreateNewWallet) {
if (!existing && !shouldCreateNewWallet) {
navigate("/setup");
return;
}
Expand Down Expand Up @@ -249,11 +252,6 @@ export const Provider: ParentComponent = (props) => {
console.error(
"Unable to run post setup, no mutiny_wallet is set"
);
setState({
setup_error: new Error(
"Attempted post-setup without a Mutiny Wallet initialized."
)
});
return;
}

Expand All @@ -277,9 +275,6 @@ export const Provider: ParentComponent = (props) => {
}
console.timeEnd("post_setup");
},
setShouldCreateNewWallet(shouldCreateNewWallet: boolean) {
setState({ shouldCreateNewWallet });
},
async deleteMutinyWallet(): Promise<void> {
try {
setState((prevState) => ({
Expand Down

0 comments on commit 8063c2a

Please sign in to comment.