diff --git a/package.json b/package.json index affe03f..36ad2f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@utxos/sdk", - "version": "0.1.0", + "version": "0.1.1", "description": "UTXOS SDK - Web3 infrastructure platform for UTXO blockchains", "main": "./dist/index.cjs", "browser": "./dist/index.js", diff --git a/src/non-custodial/index.ts b/src/non-custodial/index.ts index f2afd8a..a95f6f2 100644 --- a/src/non-custodial/index.ts +++ b/src/non-custodial/index.ts @@ -571,6 +571,59 @@ export class Web3NonCustodialProvider { }; } + /** + * Sends OTP to email address + * @param email - The email address to send OTP to + * @returns Promise that resolves when OTP is sent + */ + async sendEmailOtp(email: string): Promise<{ error: Error | null }> { + const res = await fetch(this.appOrigin + "/api/auth/email/send-otp", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email, projectId: this.projectId }), + }); + + if (!res.ok) { + const data = await res.json(); + return { error: new Error(data.error) }; + } + + return { error: null }; + } + + /** + * Verifies OTP and stores JWT (same pattern as OAuth callback) + * @param email - The email address used to send OTP + * @param code - The 6-digit OTP code + * @returns User data on success, error on failure + */ + async verifyEmailOtp( + email: string, + code: string, + ): Promise< + | { data: Web3NonCustodialProviderUser; error: null } + | { data: null; error: Error } + > { + const res = await fetch(this.appOrigin + "/api/auth/email/verify-otp", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email, code, projectId: this.projectId }), + }); + + if (!res.ok) { + const data = await res.json(); + return { data: null, error: new Error(data.error) }; + } + + const { token } = await res.json(); + + // Store JWT same way as OAuth flow + await this.putInStorage(AUTH_KEY, { jwt: token }); + + // Return user data same way as getUser() + return this.getUser(); + } + private async putInStorage( key: string, data: ObjectType, diff --git a/src/types/core/index.ts b/src/types/core/index.ts index 6cd20ef..4deec2b 100644 --- a/src/types/core/index.ts +++ b/src/types/core/index.ts @@ -25,6 +25,16 @@ export type Web3ProjectBranding = { discordEnabled?: boolean; googleEnabled?: boolean; appleEnabled?: boolean; + emailEnabled?: boolean; +}; + +export type Web3ProjectWallet = { + id: string; + projectId: string; + key: string; + pubKeyHash: string; + stakeCredentialHash: string; + tags: string[]; }; export type Web3JWTBody = { @@ -48,7 +58,7 @@ export type Web3JWTBody = { username: string | null; }; -export type Web3AuthProvider = "google" | "discord" | "twitter" | "apple"; +export type Web3AuthProvider = "google" | "discord" | "twitter" | "apple" | "email"; export type SponsorshipTxParserPostRequestBody = { txHex: string;