Signup flow

This commit is contained in:
2023-07-20 12:33:00 +01:00
parent a1605e31d5
commit c74bbae5c7
24 changed files with 389 additions and 77 deletions

View File

@ -1,7 +1,17 @@
import { bytesToHex } from "@noble/curves/abstract/utils";
import { schnorr } from "@noble/curves/secp256k1";
import { ExternalStore } from "@snort/shared";
import { EventPublisher, Nip7Signer, PrivateKeySigner } from "@snort/system";
export enum LoginType {
Nip7 = "nip7",
PrivateKey = "private-key"
}
export interface LoginSession {
type: LoginType;
pubkey: string;
privateKey?: string;
follows: string[];
}
@ -13,19 +23,49 @@ export class LoginStore extends ExternalStore<LoginSession | undefined> {
const json = window.localStorage.getItem("session");
if (json) {
this.#session = JSON.parse(json);
if (this.#session) {
this.#session.type ??= LoginType.Nip7;
}
}
}
loginWithPubkey(pk: string) {
loginWithPubkey(pk: string, type = LoginType.Nip7) {
this.#session = {
type,
pubkey: pk,
follows: [],
};
window.localStorage.setItem("session", JSON.stringify(this.#session));
this.notifyChange();
this.#save();
}
loginWithPrivateKey(key: string) {
this.#session = {
type: LoginType.PrivateKey,
pubkey: bytesToHex(schnorr.getPublicKey(key)),
privateKey: key,
follows: [],
};
this.#save();
}
takeSnapshot() {
return this.#session ? { ...this.#session } : undefined;
}
#save() {
window.localStorage.setItem("session", JSON.stringify(this.#session));
this.notifyChange();
}
}
export function getPublisher(session: LoginSession) {
switch (session?.type) {
case LoginType.Nip7: {
return new EventPublisher(new Nip7Signer(), session.pubkey);
}
case LoginType.PrivateKey: {
return new EventPublisher(new PrivateKeySigner(session.privateKey!), session.pubkey);
}
}
}