/** @jsx h */ import { Component, h } from "https://esm.sh/preact@10.17.1"; 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 "../lib/nostr-ts/key.ts"; import { InMemoryAccountContext } from "../lib/nostr-ts/nostr.ts"; import { emitFunc, EventEmitter } from "../event-bus.ts"; export type SignInEvent = { type: "signin"; privateKey?: PrivateKey; // undefined means sign in with extentions } | { type: "editSignInPrivateKey"; privateKey: 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); } 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; } type Props = { eventBus: EventEmitter; } & SignInModel; export type SignInModel = { privateKey: string; warningString?: string; }; type State = { state: "newAccount" | "enterPrivateKey" }; export class SignIn extends Component { render() { const props = this.props; if (this.state.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

{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}
); } } const on_CreateAccount_clicked = (setState: (state: State) => void) => () => { setState({ state: "newAccount" }); }; const on_SignIn_clicked = (privateKey: PrivateKey | Error, emit: emitFunc) => () => { if (privateKey instanceof PrivateKey) { emit({ type: "signin", privateKey: privateKey, }); } }; const on_EditSignInPrivateKey_clicked = (emit: emitFunc) => (e: h.JSX.TargetedEvent) => { emit({ type: "editSignInPrivateKey", privateKey: e.currentTarget.value, }); }; const on_BackToSignInPage_click = (setState: (state: State) => void) => () => { setState({ state: "enterPrivateKey" }); };