encrypted nsec storage
This commit is contained in:
parent
87dbdef4e4
commit
9185213a50
@ -9,7 +9,6 @@ import NDK, {
|
||||
type NDKCacheAdapter,
|
||||
} from "@nostr-dev-kit/ndk";
|
||||
import NDKCacheAdapterDexie from "@nostr-dev-kit/ndk-cache-dexie";
|
||||
import { db } from "@/lib/ndk/db";
|
||||
|
||||
export default function NDKInstance(explicitRelayUrls: string[]) {
|
||||
const loaded = useRef(false);
|
||||
@ -81,7 +80,6 @@ export default function NDKInstance(explicitRelayUrls: string[]) {
|
||||
}
|
||||
|
||||
if (signer) {
|
||||
console.log("SIGNER", 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 { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import AddPassphrase from "./AddPassphrase";
|
||||
import { decryptMessage } from "@/lib/nostr";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export default function LoginModal() {
|
||||
const { loginWithNip07, loginWithSecret } = useNDK();
|
||||
const { loginWithPubkey, currentUser } = useCurrentUser();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [showExtensionLogin, setShowExtensionLogin] = useState(true);
|
||||
const [nsec, setNsec] = useState("");
|
||||
const [passphrase, setPassphrase] = useState("");
|
||||
const [encryptedNsec, setEncryptedNsec] = useState("");
|
||||
const modal = useModal();
|
||||
|
||||
useEffect(() => {
|
||||
const shouldReconnect = localStorage.getItem("shouldReconnect");
|
||||
const savedNsec = localStorage.getItem("nsec");
|
||||
const encryptedNsec_ = localStorage.getItem("encrypted-nsec");
|
||||
|
||||
const getConnected = async (shouldReconnect: string) => {
|
||||
let enabled: boolean | void = false;
|
||||
|
||||
if (typeof window.nostr === "undefined") {
|
||||
return;
|
||||
return setShowExtensionLogin(false);
|
||||
}
|
||||
|
||||
if (shouldReconnect === "true") {
|
||||
@ -53,12 +59,13 @@ export default function LoginModal() {
|
||||
}
|
||||
return enabled;
|
||||
};
|
||||
if (savedNsec) {
|
||||
handleLoginNsec(savedNsec);
|
||||
if (encryptedNsec_) {
|
||||
setEncryptedNsec(encryptedNsec_);
|
||||
} else if (shouldReconnect === "true") {
|
||||
getConnected(shouldReconnect);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (currentUser) {
|
||||
modal?.hide();
|
||||
@ -83,6 +90,27 @@ export default function LoginModal() {
|
||||
setIsLoading(false);
|
||||
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) {
|
||||
setIsLoading(true);
|
||||
console.log("loging in");
|
||||
@ -93,22 +121,40 @@ export default function LoginModal() {
|
||||
}
|
||||
console.log("LOGIN", user);
|
||||
await loginWithPubkey(nip19.decode(user.npub).data.toString());
|
||||
localStorage.setItem("nsec", nsec);
|
||||
|
||||
if (typeof window.webln !== "undefined") {
|
||||
await window.webln.enable();
|
||||
}
|
||||
console.log("connected ");
|
||||
setIsLoading(false);
|
||||
modal?.hide();
|
||||
modal?.swap(<AddPassphrase nsec={nsec} />);
|
||||
}
|
||||
|
||||
return (
|
||||
<Template title="Login" className="md:max-w-[400px]">
|
||||
<div className="flex flex-col gap-y-5">
|
||||
<Button onClick={() => void handleLogin()} loading={isLoading}>
|
||||
Connect with extension
|
||||
</Button>
|
||||
{showExtensionLogin && (
|
||||
<Button onClick={() => void handleLogin()} loading={isLoading}>
|
||||
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">
|
||||
<Label>Nsec</Label>
|
||||
<Input
|
||||
|
@ -209,9 +209,7 @@ async function generateEvent(
|
||||
} catch (e) {
|
||||
console.log("at catch", e);
|
||||
const signer = delegateSigner ?? ndk.signer!;
|
||||
console.log("Signer", signer);
|
||||
const user = await signer.user();
|
||||
console.log("User", user);
|
||||
const newEvent = new NDKEvent(ndk, {
|
||||
content: _value || "",
|
||||
kind: 1,
|
||||
|
Loading…
x
Reference in New Issue
Block a user