/** @jsx h */ import { h } from "https://esm.sh/preact@10.11.3"; import { tw } from "https://esm.sh/twind@0.16.16"; import { GetLocalStorageAccountContext, Nip7ExtensionContext } from "./account-context.ts"; import { ButtonClass, CenterClass, DividerClass } from "./components/tw.ts"; import KeyView from "./key-view.tsx"; import { PrivateKey } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/key.ts"; import { InMemoryAccountContext, NostrAccountContext, } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts"; import { EventBus, EventEmitter } from "../event-bus.ts"; export type SignInEvent = { type: "signin"; privateKey?: PrivateKey; // undefined means sign in with extentions } | { type: "editSignInPrivateKey"; privateKey: string; } | { type: "createNewAccount"; } | { type: "backToSignInPage"; }; type Props = { eventBus: EventEmitter; } & SignInModel; export type SignInModel = { state: "newAccount" | "enterPrivateKey"; privateKey: string; warningString?: string; }; const AlbyURL = "https://getalby.com/"; type SignInState = "none" | "nip07" | "local"; export function getSignInState(): SignInState { const state = localStorage.getItem("SignInState"); if (state === null) { return "none"; } return state as SignInState; } export function setSignInState(state: SignInState) { localStorage.setItem("SignInState", state); } //////////////////////// // Check Login Status // //////////////////////// export async function getCurrentSignInCtx() { if (getSignInState() === "nip07") { const albyCtx = await Nip7ExtensionContext.New(); if (albyCtx instanceof Error) { return albyCtx; } if (albyCtx === undefined) { setSignInState("none"); } return albyCtx; } if (getSignInState() === "local") { const ctx = GetLocalStorageAccountContext(); if (ctx instanceof Error) { throw ctx; } if (ctx === undefined) { console.log("GetLocalStorageAccountContext is undefined"); setSignInState("none"); } return ctx; } return undefined; } export async function signInWithExtension() { const albyCtx = await Nip7ExtensionContext.New(); if (albyCtx instanceof Error) { return albyCtx; } if (albyCtx === undefined) { open(AlbyURL); } else { try { const enabled = await albyCtx.enable(); if (enabled) { setSignInState("nip07"); } else { console.error("User rejected Alby login"); } } catch (e) { console.log(e); return "You rejected Alby login. Refresh page to enable it again."; } } return albyCtx; } export function signInWithPrivateKey(privateKey: PrivateKey) { const ctx = InMemoryAccountContext.New(privateKey); if (ctx instanceof Error) { throw ctx; } localStorage.setItem("MPK", privateKey.hex); setSignInState("local"); return ctx; } export function SignIn(props: Props) { if (props.state == "newAccount") { const privateKey = PrivateKey.Generate(); return (
); } let privateKey = PrivateKey.FromHex(props.privateKey); if (privateKey instanceof Error) { privateKey = PrivateKey.FromBech32(props.privateKey); } return (
Logo

Welcome to Blowater

{ props.eventBus.emit({ type: "editSignInPrivateKey", privateKey: e.currentTarget.value, }); }} placeholder="Input your private key here" type="text" class={tw`w-full px-4 py-2 focus-visible:outline-none rounded-lg mt-8`} /> {privateKey instanceof Error ? (

Private Key has to be 64 letters hex-decimal or 63 letters nsec string

) : undefined}
Or you can
{props.warningString ?

{props.warningString}

: undefined}
); }