encrypted nsec storage
This commit is contained in:
parent
87dbdef4e4
commit
9185213a50
@ -9,7 +9,6 @@ import NDK, {
|
|||||||
type NDKCacheAdapter,
|
type NDKCacheAdapter,
|
||||||
} from "@nostr-dev-kit/ndk";
|
} from "@nostr-dev-kit/ndk";
|
||||||
import NDKCacheAdapterDexie from "@nostr-dev-kit/ndk-cache-dexie";
|
import NDKCacheAdapterDexie from "@nostr-dev-kit/ndk-cache-dexie";
|
||||||
import { db } from "@/lib/ndk/db";
|
|
||||||
|
|
||||||
export default function NDKInstance(explicitRelayUrls: string[]) {
|
export default function NDKInstance(explicitRelayUrls: string[]) {
|
||||||
const loaded = useRef(false);
|
const loaded = useRef(false);
|
||||||
@ -81,7 +80,6 @@ export default function NDKInstance(explicitRelayUrls: string[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (signer) {
|
if (signer) {
|
||||||
console.log("SIGNER", signer);
|
|
||||||
_setSigner(signer);
|
_setSigner(signer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
70
components/Modals/AddPassphrase.tsx
Normal file
70
components/Modals/AddPassphrase.tsx
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
"use client";
|
||||||
|
import { useState, useRef, useEffect } from "react";
|
||||||
|
import Template from "./Template";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { useModal } from "@/app/_providers/modal/provider";
|
||||||
|
import { nip19 } from "nostr-tools";
|
||||||
|
// import { useKeys } from "@/app/_providers/keysProvider";
|
||||||
|
import { useNDK } from "@/app/_providers/ndk";
|
||||||
|
import useCurrentUser from "@/lib/hooks/useCurrentUser";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { encryptMessage } from "@/lib/nostr";
|
||||||
|
|
||||||
|
export default function AddPassphrase({ nsec }: { nsec: string }) {
|
||||||
|
const { signer } = useNDK();
|
||||||
|
const { currentUser } = useCurrentUser();
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [passphrase, setPassphrase] = useState("");
|
||||||
|
|
||||||
|
const modal = useModal();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (currentUser) {
|
||||||
|
console.log("Current signer", currentUser.ndk?.signer);
|
||||||
|
console.log("Current signer alt", signer);
|
||||||
|
}
|
||||||
|
}, [currentUser]);
|
||||||
|
|
||||||
|
async function handleAddPassphrase() {
|
||||||
|
setIsLoading(true);
|
||||||
|
console.log("loging in");
|
||||||
|
const encryptedNsec = encryptMessage(nsec, passphrase);
|
||||||
|
if (encryptedNsec) {
|
||||||
|
localStorage.setItem("encrypted-nsec", encryptedNsec);
|
||||||
|
}
|
||||||
|
setIsLoading(false);
|
||||||
|
modal?.hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Template title="Login" className="md:max-w-[400px]">
|
||||||
|
<div className="flex flex-col gap-y-5">
|
||||||
|
<div className="">
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
Wan't to make it easier to log in next time? Add a passphrase that
|
||||||
|
we'll use to encrypt and save your nsec.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-3">
|
||||||
|
<Label>Passphrase</Label>
|
||||||
|
<Input
|
||||||
|
value={passphrase}
|
||||||
|
onChange={(e) => setPassphrase(e.target.value)}
|
||||||
|
placeholder="passphrase..."
|
||||||
|
type="password"
|
||||||
|
className="text-[16px]"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant={"outline"}
|
||||||
|
onClick={() => void handleAddPassphrase()}
|
||||||
|
loading={isLoading}
|
||||||
|
className="w-full"
|
||||||
|
>
|
||||||
|
Save passphrase
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Template>
|
||||||
|
);
|
||||||
|
}
|
@ -9,23 +9,29 @@ import { useNDK } from "@/app/_providers/ndk";
|
|||||||
import useCurrentUser from "@/lib/hooks/useCurrentUser";
|
import useCurrentUser from "@/lib/hooks/useCurrentUser";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
|
import AddPassphrase from "./AddPassphrase";
|
||||||
|
import { decryptMessage } from "@/lib/nostr";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
export default function LoginModal() {
|
export default function LoginModal() {
|
||||||
const { loginWithNip07, loginWithSecret } = useNDK();
|
const { loginWithNip07, loginWithSecret } = useNDK();
|
||||||
const { loginWithPubkey, currentUser } = useCurrentUser();
|
const { loginWithPubkey, currentUser } = useCurrentUser();
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [showExtensionLogin, setShowExtensionLogin] = useState(true);
|
||||||
const [nsec, setNsec] = useState("");
|
const [nsec, setNsec] = useState("");
|
||||||
|
const [passphrase, setPassphrase] = useState("");
|
||||||
|
const [encryptedNsec, setEncryptedNsec] = useState("");
|
||||||
const modal = useModal();
|
const modal = useModal();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const shouldReconnect = localStorage.getItem("shouldReconnect");
|
const shouldReconnect = localStorage.getItem("shouldReconnect");
|
||||||
const savedNsec = localStorage.getItem("nsec");
|
const encryptedNsec_ = localStorage.getItem("encrypted-nsec");
|
||||||
|
|
||||||
const getConnected = async (shouldReconnect: string) => {
|
const getConnected = async (shouldReconnect: string) => {
|
||||||
let enabled: boolean | void = false;
|
let enabled: boolean | void = false;
|
||||||
|
|
||||||
if (typeof window.nostr === "undefined") {
|
if (typeof window.nostr === "undefined") {
|
||||||
return;
|
return setShowExtensionLogin(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldReconnect === "true") {
|
if (shouldReconnect === "true") {
|
||||||
@ -53,12 +59,13 @@ export default function LoginModal() {
|
|||||||
}
|
}
|
||||||
return enabled;
|
return enabled;
|
||||||
};
|
};
|
||||||
if (savedNsec) {
|
if (encryptedNsec_) {
|
||||||
handleLoginNsec(savedNsec);
|
setEncryptedNsec(encryptedNsec_);
|
||||||
} else if (shouldReconnect === "true") {
|
} else if (shouldReconnect === "true") {
|
||||||
getConnected(shouldReconnect);
|
getConnected(shouldReconnect);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
modal?.hide();
|
modal?.hide();
|
||||||
@ -83,6 +90,27 @@ export default function LoginModal() {
|
|||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
modal?.hide();
|
modal?.hide();
|
||||||
}
|
}
|
||||||
|
async function handleLoginPassphrase() {
|
||||||
|
if (!encryptedNsec || passphrase) return;
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
const decryptedNsec = decryptMessage(encryptedNsec, passphrase);
|
||||||
|
if (!decryptedNsec) {
|
||||||
|
setIsLoading(false);
|
||||||
|
toast.error("An error has occured");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const user = await loginWithSecret(decryptedNsec);
|
||||||
|
if (!user) {
|
||||||
|
throw new Error("NO auth");
|
||||||
|
}
|
||||||
|
await loginWithPubkey(nip19.decode(user.npub).data.toString());
|
||||||
|
if (typeof window.webln !== "undefined") {
|
||||||
|
await window.webln.enable();
|
||||||
|
}
|
||||||
|
setIsLoading(false);
|
||||||
|
modal?.hide();
|
||||||
|
}
|
||||||
async function handleLoginNsec(nsec_?: string) {
|
async function handleLoginNsec(nsec_?: string) {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
console.log("loging in");
|
console.log("loging in");
|
||||||
@ -93,22 +121,40 @@ export default function LoginModal() {
|
|||||||
}
|
}
|
||||||
console.log("LOGIN", user);
|
console.log("LOGIN", user);
|
||||||
await loginWithPubkey(nip19.decode(user.npub).data.toString());
|
await loginWithPubkey(nip19.decode(user.npub).data.toString());
|
||||||
localStorage.setItem("nsec", nsec);
|
|
||||||
|
|
||||||
if (typeof window.webln !== "undefined") {
|
if (typeof window.webln !== "undefined") {
|
||||||
await window.webln.enable();
|
await window.webln.enable();
|
||||||
}
|
}
|
||||||
console.log("connected ");
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
modal?.hide();
|
modal?.swap(<AddPassphrase nsec={nsec} />);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Template title="Login" className="md:max-w-[400px]">
|
<Template title="Login" className="md:max-w-[400px]">
|
||||||
<div className="flex flex-col gap-y-5">
|
<div className="flex flex-col gap-y-5">
|
||||||
<Button onClick={() => void handleLogin()} loading={isLoading}>
|
{showExtensionLogin && (
|
||||||
Connect with extension
|
<Button onClick={() => void handleLogin()} loading={isLoading}>
|
||||||
</Button>
|
Connect with extension
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{!!encryptedNsec && (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<Label>Passphrase</Label>
|
||||||
|
<Input
|
||||||
|
value={nsec}
|
||||||
|
onChange={(e) => setPassphrase(e.target.value)}
|
||||||
|
placeholder="passphrase..."
|
||||||
|
className="text-[16px]"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant={"outline"}
|
||||||
|
onClick={() => void handleLoginPassphrase()}
|
||||||
|
loading={isLoading}
|
||||||
|
className="w-full"
|
||||||
|
>
|
||||||
|
Login with Passphrase
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
<Label>Nsec</Label>
|
<Label>Nsec</Label>
|
||||||
<Input
|
<Input
|
||||||
|
@ -209,9 +209,7 @@ async function generateEvent(
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("at catch", e);
|
console.log("at catch", e);
|
||||||
const signer = delegateSigner ?? ndk.signer!;
|
const signer = delegateSigner ?? ndk.signer!;
|
||||||
console.log("Signer", signer);
|
|
||||||
const user = await signer.user();
|
const user = await signer.user();
|
||||||
console.log("User", user);
|
|
||||||
const newEvent = new NDKEvent(ndk, {
|
const newEvent = new NDKEvent(ndk, {
|
||||||
content: _value || "",
|
content: _value || "",
|
||||||
kind: 1,
|
kind: 1,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user