add Nostr-login package #217

Merged
enes merged 20 commits from nostr-login-9-30 into staging 2024-10-09 08:54:33 +00:00
4 changed files with 26 additions and 0 deletions
Showing only changes of commit aa4637dd0d - Show all commits

View File

@ -3,6 +3,12 @@ import { SignedEvent } from '../../types'
import { LoginMethodStrategy } from './loginMethodStrategy' import { LoginMethodStrategy } from './loginMethodStrategy'
import { WindowNostr } from 'nostr-tools/nip07' import { WindowNostr } from 'nostr-tools/nip07'
/**
* Login Method Strategy when using nostr-login package.
*
* This class extends {@link LoginMethodStrategy base strategy} and implements all login method operations
* @see {@link LoginMethodStrategy}
*/
export class NostrLoginStrategy extends LoginMethodStrategy { export class NostrLoginStrategy extends LoginMethodStrategy {
private nostr: WindowNostr private nostr: WindowNostr

View File

@ -12,6 +12,12 @@ import { AuthState, LoginMethod } from '../../store/auth/types'
import { LoginMethodStrategy } from './loginMethodStrategy' import { LoginMethodStrategy } from './loginMethodStrategy'
import { verifySignedEvent } from '../../utils/nostr' import { verifySignedEvent } from '../../utils/nostr'
/**
* Login Method Strategy when using dev private key login.
*
* This class extends {@link LoginMethodStrategy base strategy} and implements all login method operations
* @see {@link LoginMethodStrategy}
*/
export class PrivateKeyStrategy extends LoginMethodStrategy { export class PrivateKeyStrategy extends LoginMethodStrategy {
async nip04Encrypt(receiver: string, content: string): Promise<string> { async nip04Encrypt(receiver: string, content: string): Promise<string> {
const keys = (store.getState().auth as AuthState).keyPair const keys = (store.getState().auth as AuthState).keyPair

View File

@ -8,6 +8,13 @@ import { LoginMethod } from '../../store/auth/types'
import { NostrLoginStrategy } from './NostrLoginStrategy' import { NostrLoginStrategy } from './NostrLoginStrategy'
import { PrivateKeyStrategy } from './PrivateKeyStrategy' import { PrivateKeyStrategy } from './PrivateKeyStrategy'
/**
* This class is a context provider and helper class. This MUST be instantiated and used as an entry point for any of the {@link LoginMethodOperations LoginMethodOperations}
* @constructor Takes {@link LoginMethod LoginMethod} as an argument and sets the correct strategy
*
* @see {@link LoginMethod}
* @see {@link LoginMethodOperations}
*/
export class LoginMethodContext implements LoginMethodOperations { export class LoginMethodContext implements LoginMethodOperations {
private strategy: LoginMethodStrategy private strategy: LoginMethodStrategy

View File

@ -2,6 +2,9 @@
import { EventTemplate, UnsignedEvent } from 'nostr-tools' import { EventTemplate, UnsignedEvent } from 'nostr-tools'
import { SignedEvent } from '../../types/nostr' import { SignedEvent } from '../../types/nostr'
/**
* This interface holds all operations that are dependant on the login method and is used as the basis for the login strategies.
*/
export interface LoginMethodOperations { export interface LoginMethodOperations {
nip04Encrypt(receiver: string, content: string): Promise<string> nip04Encrypt(receiver: string, content: string): Promise<string>
nip04Decrypt(sender: string, content: string): Promise<string> nip04Decrypt(sender: string, content: string): Promise<string>
@ -10,6 +13,10 @@ export interface LoginMethodOperations {
signEvent(event: UnsignedEvent | EventTemplate): Promise<SignedEvent> signEvent(event: UnsignedEvent | EventTemplate): Promise<SignedEvent>
} }
/**
* This is the fallback class that provides base implementation for the {@link LoginMethodOperations login method operations} . Only used to throw errors in case when the LoginMethod is missing (login context not set).
* @see {@link LoginMethodOperations}
*/
export class LoginMethodStrategy implements LoginMethodOperations { export class LoginMethodStrategy implements LoginMethodOperations {
async nip04Encrypt(_receiver: string, _content: string): Promise<string> { async nip04Encrypt(_receiver: string, _content: string): Promise<string> {
throw new Error('Login method strategy is undefined') throw new Error('Login method strategy is undefined')