Skip to content

Commit

Permalink
feat: save wallet to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Sep 1, 2024
1 parent ec0adc1 commit 7fb8d89
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@ import { ScanQR } from "./components/ScanQR";

export default function Home() {
const [wallet, setWallet] = React.useState<Wallet>();
const [loadedSavedWallet, setLoadedSavedWallet] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const [copied, setCopied] = React.useState<"nwcUrl" | "lightningAddress">();
const [balance, setBalance] = React.useState(0);
React.useEffect(() => {
const savedWalletJSON = window.localStorage.getItem("wallet");
if (savedWalletJSON) {
const _wallet = JSON.parse(savedWalletJSON);
setWallet(_wallet);
setLoadedSavedWallet(true);
(async () => {
const client = new nwc.NWCClient({
nostrWalletConnectUrl: _wallet.connectionSecret,
});
const _balance = await client.getBalance();
setBalance(_balance.balance);
})();
}
}, []);
async function onSubmit() {
setLoading(true);
try {
Expand All @@ -25,6 +41,9 @@ export default function Home() {
throw new Error(error);
}
setWallet(wallet);
if (wallet) {
window.localStorage.setItem("wallet", JSON.stringify(wallet));
}
} catch (error) {
console.error(error);
alert("Something went wrong: " + error);
Expand Down Expand Up @@ -91,7 +110,35 @@ export default function Home() {
)}
{wallet && (
<>
<p>New Wallet Created!</p>
<p>
{loadedSavedWallet ? (
<>
Welcome back,{" "}
<span className="font-semibold">
{wallet.lightningAddress}
</span>
</>
) : (
"New Wallet Created!"
)}
</p>
{loadedSavedWallet && (
<button
className="btn btn-secondary btn-sm"
onClick={() => {
if (
confirm(
"Are you sure you wish to log out? if you haven't saved your connection secret, any funds in this wallet will be lost."
)
) {
window.localStorage.removeItem("wallet");
window.location.reload();
}
}}
>
Log out
</button>
)}
<p className="text-xs max-w-xs text-center">
{"Make sure to copy it and save it somewhere safe."}
</p>
Expand Down

0 comments on commit 7fb8d89

Please sign in to comment.