UNPKG

better-near-auth

Version:

Sign in with NEAR (SIWN) plugin for Better Auth

748 lines 30.6 kB
import type { DBAdapter } from "better-auth/types"; import z from "zod"; import { type AccountId, type DualNetworkConfig, type ListAccountsResponseT, type ListedNearAccount, type Profile, type SubAccountConfig } from "./types.js"; export * from "./types.js"; export interface RelayerConfig { accountId?: string; privateKey?: string; privateKeys?: string[]; whitelistedContracts?: string[]; maxGasPerTransaction?: string; maxDepositPerTransaction?: string; } export interface SIWNPluginOptions { recipient?: string; recipients?: DualNetworkConfig<string>; requireFullAccessKey?: boolean; getNonce?: () => Promise<Uint8Array>; getProfile?: (accountId: AccountId) => Promise<Profile | null>; validateLimitedAccessKey?: (args: { accountId: AccountId; publicKey: string; recipient?: string; }) => Promise<boolean>; apiKey?: string; rpcUrl?: string | DualNetworkConfig<string>; relayer?: RelayerConfig | DualNetworkConfig<RelayerConfig>; secrets?: { parentKey?: string | DualNetworkConfig<string>; }; subAccount?: SubAccountConfig | DualNetworkConfig<SubAccountConfig>; } export declare const siwn: (options: SIWNPluginOptions) => { id: string; schema: { nearAccount: { fields: { userId: { type: "string"; references: { model: string; field: string; }; required: true; }; accountId: { type: "string"; required: true; }; network: { type: "string"; required: true; }; publicKey: { type: "string"; required: true; }; isPrimary: { type: "boolean"; defaultValue: false; }; createdAt: { type: "date"; required: true; }; }; }; relayedTransaction: { fields: { userId: { type: "string"; references: { model: string; field: string; }; }; txHash: { type: "string"; required: true; }; senderId: { type: "string"; required: true; }; receiverId: { type: "string"; required: true; }; network: { type: "string"; required: true; }; status: { type: "string"; required: true; }; gasUsed: { type: "string"; }; createdAt: { type: "date"; required: true; }; updatedAt: { type: "date"; }; }; }; relayerKey: { fields: { accountId: { type: "string"; required: true; }; encryptedPrivateKey: { type: "string"; required: true; }; iv: { type: "string"; required: true; }; publicKey: { type: "string"; required: true; }; network: { type: "string"; required: true; }; createdAt: { type: "date"; required: true; }; lastUsedAt: { type: "date"; }; }; }; }; hooks: { after: { matcher: (context: { path?: string; method?: string; }) => boolean; handler: (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ context: import("better-call").MiddlewareContext<import("better-call").MiddlewareOptions, { returned?: unknown | undefined; responseHeaders?: Headers | undefined; } & import("better-auth").PluginContext<import("better-auth/types").BetterAuthOptions> & import("better-auth").InfoContext & { options: import("better-auth/types").BetterAuthOptions; trustedOrigins: string[]; trustedProviders: string[]; isTrustedOrigin: (url: string, settings?: { allowRelativePaths: boolean; }) => boolean; oauthConfig: { skipStateCookieCheck?: boolean | undefined; storeStateStrategy: "database" | "cookie"; }; newSession: { session: { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; } & Record<string, any>; user: { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; } & Record<string, any>; } | null; session: { session: { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; } & Record<string, any>; user: { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; } & Record<string, any>; } | null; setNewSession: (session: { session: { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; } & Record<string, any>; user: { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; } & Record<string, any>; } | null) => void; socialProviders: import("@better-auth/core/oauth2").OAuthProvider[]; authCookies: import("better-auth/types").BetterAuthCookies; logger: ReturnType<(options?: import("@better-auth/core/env").Logger | undefined) => import("@better-auth/core/env").InternalLogger>; rateLimit: { enabled: boolean; window: number; max: number; storage: "memory" | "database" | "secondary-storage"; } & Omit<import("better-auth/types").BetterAuthRateLimitOptions, "enabled" | "window" | "max" | "storage">; adapter: DBAdapter<import("better-auth/types").BetterAuthOptions>; internalAdapter: import("better-auth").InternalAdapter<import("better-auth/types").BetterAuthOptions>; createAuthCookie: (cookieName: string, overrideAttributes?: Partial<import("better-call").CookieOptions> | undefined) => import("better-auth").BetterAuthCookie; secret: string; secretConfig: string | import("better-auth").SecretConfig; sessionConfig: { updateAge: number; expiresIn: number; freshAge: number; cookieRefreshCache: false | { enabled: true; updateAge: number; }; }; generateId: (options: { model: import("better-auth").ModelNames; size?: number | undefined; }) => string | false; secondaryStorage: import("better-auth").SecondaryStorage | undefined; password: { hash: (password: string) => Promise<string>; verify: (data: { password: string; hash: string; }) => Promise<boolean>; config: { minPasswordLength: number; maxPasswordLength: number; }; checkPassword: (userId: string, ctx: import("better-auth").GenericEndpointContext<import("better-auth/types").BetterAuthOptions>) => Promise<boolean>; }; tables: import("better-auth").BetterAuthDBSchema; runMigrations: () => Promise<void>; publishTelemetry: (event: { type: string; anonymousId?: string | undefined; payload: Record<string, any>; }) => Promise<void>; skipOriginCheck: boolean | string[]; skipCSRFCheck: boolean; runInBackground: (promise: Promise<unknown>) => void; runInBackgroundOrAwait: (promise: Promise<unknown> | void) => import("better-auth").Awaitable<unknown>; }>; }>; }[]; }; endpoints: { linkNearAccount: import("better-call").StrictEndpoint<"/near/link-account", { method: "POST"; body: z.ZodObject<{ signedMessage: z.ZodObject<{ accountId: z.ZodString; publicKey: z.ZodString; signature: z.ZodString; state: z.ZodOptional<z.ZodString>; }, z.z.core.$strip>; message: z.ZodString; recipient: z.ZodString; nonce: z.ZodString; accountId: z.ZodString; callbackUrl: z.ZodOptional<z.ZodString>; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; requireRequest: true; }, { success: boolean; accountId: string; network: "mainnet" | "testnet"; message: string; }>; unlinkNearAccount: import("better-call").StrictEndpoint<"/near/unlink-account", { method: "POST"; body: z.ZodObject<{ accountId: z.ZodString; network: z.ZodOptional<z.ZodEnum<{ mainnet: "mainnet"; testnet: "testnet"; }>>; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { success: boolean; accountId: string; network: "mainnet" | "testnet"; message: string; }>; listNearAccounts: import("better-call").StrictEndpoint<"/near/list-accounts", { method: "GET"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, ListAccountsResponseT>; setPrimaryNearAccount: import("better-call").StrictEndpoint<"/near/set-primary-account", { method: "POST"; body: z.ZodObject<{ accountId: z.ZodString; network: z.ZodOptional<z.ZodEnum<{ mainnet: "mainnet"; testnet: "testnet"; }>>; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { accounts: ListedNearAccount[]; activeAccount: ListedNearAccount | null; availableAccounts: ListedNearAccount[]; success: boolean; accountId: string; network: "mainnet" | "testnet"; message: string; }>; getSiwnNonce: import("better-call").StrictEndpoint<"/near/nonce", { method: "POST"; body: z.ZodObject<{ accountId: z.ZodString; networkId: z.ZodUnion<readonly [z.ZodLiteral<"mainnet">, z.ZodLiteral<"testnet">]>; }, z.z.core.$strip>; }, { nonce: string; }>; getSiwnProfile: import("better-call").StrictEndpoint<"/near/profile", { method: "POST"; body: z.ZodObject<{ accountId: z.ZodOptional<z.ZodString>; }, z.z.core.$strip>; }, { name?: string | undefined; description?: string | undefined; image?: { url?: string | undefined; ipfs_cid?: string | undefined; } | undefined; backgroundImage?: { url?: string | undefined; ipfs_cid?: string | undefined; } | undefined; linktree?: Record<string, string> | undefined; } | null>; verifySiwnMessage: import("better-call").StrictEndpoint<"/near/verify", { method: "POST"; body: z.ZodObject<{ signedMessage: z.ZodObject<{ accountId: z.ZodString; publicKey: z.ZodString; signature: z.ZodString; state: z.ZodOptional<z.ZodString>; }, z.z.core.$strip>; message: z.ZodString; recipient: z.ZodString; nonce: z.ZodString; accountId: z.ZodString; callbackUrl: z.ZodOptional<z.ZodString>; }, z.z.core.$strip>; requireRequest: true; }, { token: string; success: true; user: { id: string; accountId: string; network: "mainnet" | "testnet"; }; }>; relayNearTransaction: import("better-call").StrictEndpoint<"/near/relay", { method: "POST"; body: z.ZodObject<{ payload: z.ZodString; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { txHash: string; status: "pending" | "completed" | "failed"; }>; getRelayStatus: import("better-call").StrictEndpoint<"/near/relay-status/:txHash", { method: "GET"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { status: "pending" | "completed" | "failed"; gasUsed?: string | undefined; outcome?: unknown; }>; getRelayerInfo: import("better-call").StrictEndpoint<"/near/relayer-info", { method: "POST"; body: z.ZodObject<{ network: z.ZodOptional<z.ZodEnum<{ mainnet: "mainnet"; testnet: "testnet"; }>>; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { enabled: false; subAccountAvailable: false; } | { enabled: boolean; accountId: string; mode: "ephemeral" | "explicit"; network: "mainnet" | "testnet"; publicKey: string; balance: string; available: string; staked: string; storageUsage: string; storageBytes: number; hasContract: boolean; hasKey: boolean; createdAt: Date | undefined; lastUsedAt: Date | undefined; parentAccount: string | undefined; subAccountAvailable: boolean; }>; getRelayHistory: import("better-call").StrictEndpoint<"/near/relay-history", { method: "GET"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { transactions: { id: string; userId: string; txHash: string; senderId: string; receiverId: string; network: string; status: string; gasUsed: string | undefined; createdAt: string; updatedAt: string | undefined; }[]; }>; viewContract: import("better-call").StrictEndpoint<"/near/view", { method: "POST"; body: z.ZodObject<{ contractId: z.ZodString; methodName: z.ZodString; args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { result: unknown; }>; createSubAccount: import("better-call").StrictEndpoint<"/near/create-sub-account", { method: "POST"; body: z.ZodObject<{ subAccountName: z.ZodString; network: z.ZodOptional<z.ZodEnum<{ mainnet: "mainnet"; testnet: "testnet"; }>>; publicKey: z.ZodString; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { success: true; accountId: string; network: "mainnet" | "testnet"; publicKey: string; message: string; }>; checkSubAccountAvailability: import("better-call").StrictEndpoint<"/near/check-sub-account-availability", { method: "POST"; body: z.ZodObject<{ subAccountName: z.ZodString; network: z.ZodOptional<z.ZodEnum<{ mainnet: "mainnet"; testnet: "testnet"; }>>; }, z.z.core.$strip>; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; }, { available: boolean; accountId: string; parentAccount?: string | undefined; reason?: "taken" | "invalid" | "too-long" | "not-configured" | undefined; }>; }; }; //# sourceMappingURL=index.d.ts.map