sign-in-with-stacks 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{better-auth-CrxdUXaI.js → better-auth-CdqoUp9E.js} +11 -11
- package/dist/better-auth-CdqoUp9E.js.map +1 -0
- package/dist/{client-BywC0Cfs.d.ts → client-B0wBosYe.d.ts} +34 -12
- package/dist/{client-CIxO0mj2.js → client-DIS5YbyF.js} +4 -7
- package/dist/client-DIS5YbyF.js.map +1 -0
- package/dist/index.d.ts +16 -23
- package/dist/index.js +1 -2
- package/dist/plugins/better-auth/client.d.ts +1 -1
- package/dist/plugins/better-auth/client.js +1 -2
- package/dist/plugins/better-auth/index.d.ts +1 -1
- package/dist/plugins/better-auth/index.js +2 -2
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +2 -2
- package/dist/{src-C3f2LD6M.js → src-eUAWnT_0.js} +130 -89
- package/dist/src-eUAWnT_0.js.map +1 -0
- package/package.json +18 -20
- package/src/createSiwsMessage.ts +66 -25
- package/src/errors/base.ts +22 -25
- package/src/errors/siws.ts +1 -1
- package/src/generateSiwsNonce.ts +5 -17
- package/src/index.ts +1 -0
- package/src/parseSiwsMessage.ts +96 -32
- package/src/plugins/better-auth/client.ts +1 -0
- package/src/plugins/better-auth/plugin.ts +21 -10
- package/src/plugins/better-auth/schema.ts +0 -2
- package/src/types.ts +1 -1
- package/src/utils.ts +83 -17
- package/src/validateSiwsMessage.ts +55 -12
- package/src/verifySiwsMessage.ts +34 -24
- package/dist/better-auth-CrxdUXaI.js.map +0 -1
- package/dist/client-CIxO0mj2.js.map +0 -1
- package/dist/src-C3f2LD6M.js.map +0 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { n as PACKAGE_VERSION } from "./client-DIS5YbyF.js";
|
|
2
|
+
import { i as generateSiwsNonce, o as getAddress, r as parseSiwsMessage, s as isAddress, t as verifySiwsMessage } from "./src-eUAWnT_0.js";
|
|
3
3
|
import { createPlaceholderEmail } from "@better-auth/core/utils/email";
|
|
4
4
|
import { APIError, createAuthEndpoint, isAPIError } from "better-auth/api";
|
|
5
5
|
import { setSessionCookie } from "better-auth/cookies";
|
|
6
6
|
import { mergeSchema } from "better-auth/db";
|
|
7
|
-
import
|
|
8
|
-
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
// src/plugins/better-auth/schema.ts
|
|
9
9
|
const schema = { walletAddress: { fields: {
|
|
10
10
|
userId: {
|
|
11
11
|
type: "string",
|
|
@@ -34,12 +34,13 @@ const schema = { walletAddress: { fields: {
|
|
|
34
34
|
required: true
|
|
35
35
|
}
|
|
36
36
|
} } };
|
|
37
|
-
|
|
38
|
-
//#region src/plugins/better-auth/plugin.ts
|
|
37
|
+
// src/plugins/better-auth/plugin.ts
|
|
39
38
|
const SIWS_VERIFICATION_IDENTIFIER_PREFIX = "siws:";
|
|
40
39
|
const SIWS_NONCE_MAX_LENGTH = 250;
|
|
41
40
|
const SIWS_NONCE_ALPHANUMERIC_REGEX = /^[a-zA-Z0-9]+$/;
|
|
42
41
|
const SIWS_MAX_CHAIN_ID = 4294967295;
|
|
42
|
+
const SIWS_MESSAGE_MAX_LENGTH = 8192;
|
|
43
|
+
const SIWS_SIGNATURE_MAX_LENGTH = 1024;
|
|
43
44
|
const isValidSiwsNonce = (nonce) => typeof nonce === "string" && nonce.length >= 8 && nonce.length <= SIWS_NONCE_MAX_LENGTH && SIWS_NONCE_ALPHANUMERIC_REGEX.test(nonce);
|
|
44
45
|
const siwsMessageMismatchError = () => new APIError("UNAUTHORIZED", {
|
|
45
46
|
message: "Unauthorized: SIWS message does not match the expected domain, address, chain ID, or nonce",
|
|
@@ -82,8 +83,8 @@ const siws = (options) => {
|
|
|
82
83
|
verify: createAuthEndpoint("/siws/verify", {
|
|
83
84
|
method: "POST",
|
|
84
85
|
body: z.object({
|
|
85
|
-
message: z.string().min(1),
|
|
86
|
-
signature: z.string().min(1),
|
|
86
|
+
message: z.string().min(1).max(SIWS_MESSAGE_MAX_LENGTH),
|
|
87
|
+
signature: z.string().min(1).max(SIWS_SIGNATURE_MAX_LENGTH),
|
|
87
88
|
email: z.email().optional()
|
|
88
89
|
}).strict().refine((data) => options.anonymous !== false || !!data.email, {
|
|
89
90
|
message: "Email is required when the anonymous plugin option is disabled.",
|
|
@@ -256,7 +257,7 @@ const siws = (options) => {
|
|
|
256
257
|
session,
|
|
257
258
|
user
|
|
258
259
|
});
|
|
259
|
-
return ctx.json({
|
|
260
|
+
return await ctx.json({
|
|
260
261
|
token: session.token,
|
|
261
262
|
success: true,
|
|
262
263
|
user: {
|
|
@@ -278,7 +279,6 @@ const siws = (options) => {
|
|
|
278
279
|
options
|
|
279
280
|
};
|
|
280
281
|
};
|
|
281
|
-
//#endregion
|
|
282
282
|
export { siws as t };
|
|
283
283
|
|
|
284
|
-
//# sourceMappingURL=better-auth-
|
|
284
|
+
//# sourceMappingURL=better-auth-CdqoUp9E.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"better-auth-CdqoUp9E.js","names":[],"sources":["../src/plugins/better-auth/schema.ts","../src/plugins/better-auth/plugin.ts"],"sourcesContent":["import type { BetterAuthPluginDBSchema } from \"better-auth/db\";\n\nexport const schema = {\n walletAddress: {\n fields: {\n userId: {\n type: \"string\",\n references: {\n model: \"user\",\n field: \"id\",\n },\n required: true,\n index: true,\n },\n address: {\n type: \"string\",\n required: true,\n },\n chainId: {\n type: \"number\",\n required: true,\n bigint: true,\n },\n isPrimary: {\n type: \"boolean\",\n defaultValue: false,\n },\n createdAt: {\n type: \"date\",\n required: true,\n },\n },\n },\n} satisfies BetterAuthPluginDBSchema;\n","import { createPlaceholderEmail } from \"@better-auth/core/utils/email\";\nimport type { BetterAuthPlugin, InferOptionSchema, User } from \"better-auth\";\nimport { APIError, createAuthEndpoint, isAPIError } from \"better-auth/api\";\nimport { setSessionCookie } from \"better-auth/cookies\";\nimport { mergeSchema } from \"better-auth/db\";\nimport { z } from \"zod\";\nimport { generateSiwsNonce, verifySiwsMessage } from \"../../index.js\";\nimport { parseSiwsMessage } from \"../../parseSiwsMessage.js\";\nimport { getAddress, isAddress } from \"../../utils.js\";\nimport { PACKAGE_VERSION } from \"../../version.js\";\nimport { schema } from \"./schema.js\";\nimport type {\n ResolveProfileArgs,\n ResolveProfileResult,\n SIWSVerifyMessageArgs,\n WalletAddress,\n} from \"./types.js\";\n\ndeclare module \"@better-auth/core\" {\n interface BetterAuthPluginRegistry<AuthOptions, Options> {\n siws: {\n creator: typeof siws;\n };\n }\n}\n\nexport interface SIWSPluginOptions {\n // The domain name of your application (required for SIWS message generation)\n domain: string;\n // The email domain name for creating user accounts when not using anonymous mode. Defaults to a placeholder domain\n emailDomainName?: string | undefined;\n // Whether to allow anonymous sign-ins without requiring an email. Default is true\n anonymous?: boolean | undefined;\n // Function to generate a unique nonce for each sign-in attempt. You must implement this function to return a cryptographically secure random string. Must return a Promise<string>\n getNonce?: () => Promise<string>;\n // Function to verify the SIWS message signature. Defaults to the built-in Stacks verifier\n verifyMessage?:\n | ((args: SIWSVerifyMessageArgs) => Promise<boolean>)\n | undefined;\n // Function to resolve a display name and avatar for a new wallet user\n resolveProfile?:\n | ((args: ResolveProfileArgs) => Promise<ResolveProfileResult>)\n | undefined;\n // Custom schema for the plugin's walletAddress table\n schema?: InferOptionSchema<typeof schema> | undefined;\n}\n\nconst SIWS_VERIFICATION_IDENTIFIER_PREFIX = \"siws:\";\n// MySQL adapter schemas cap verification.identifier at 255 characters.\nconst VERIFICATION_IDENTIFIER_MAX_LENGTH = 255;\nconst SIWS_NONCE_MAX_LENGTH =\n VERIFICATION_IDENTIFIER_MAX_LENGTH -\n SIWS_VERIFICATION_IDENTIFIER_PREFIX.length;\nconst SIWS_NONCE_ALPHANUMERIC_REGEX = /^[a-zA-Z0-9]+$/;\nconst SIWS_MAX_CHAIN_ID = 4_294_967_295;\n// The verify endpoint parses the message before any authentication, so its\n// length is bounded. A SIWS message is usually under 1,000 characters; this\n// leaves room for a long statement and many resources. A Stacks RSV signature\n// is 130 hex characters; the limit leaves room for custom `verifyMessage`\n// formats.\nconst SIWS_MESSAGE_MAX_LENGTH = 8192;\nconst SIWS_SIGNATURE_MAX_LENGTH = 1024;\n\nconst isValidSiwsNonce = (nonce: string | undefined): nonce is string =>\n typeof nonce === \"string\" &&\n nonce.length >= 8 &&\n nonce.length <= SIWS_NONCE_MAX_LENGTH &&\n SIWS_NONCE_ALPHANUMERIC_REGEX.test(nonce);\n\nconst siwsMessageMismatchError = () =>\n new APIError(\"UNAUTHORIZED\", {\n message:\n \"Unauthorized: SIWS message does not match the expected domain, address, chain ID, or nonce\",\n status: 401,\n code: \"UNAUTHORIZED_SIWS_MESSAGE_MISMATCH\",\n });\n\nexport const siws = (options: SIWSPluginOptions) => {\n const verifyMessage =\n options.verifyMessage ??\n (async (args: SIWSVerifyMessageArgs) => {\n const { nonce } = parseSiwsMessage(args.message);\n return verifySiwsMessage({\n message: args.message,\n signature: args.signature,\n address: args.address,\n domain: options.domain,\n nonce,\n });\n });\n\n return {\n id: \"siws\",\n version: PACKAGE_VERSION,\n schema: mergeSchema(schema, options?.schema),\n endpoints: {\n nonce: createAuthEndpoint(\n \"/siws/nonce\",\n {\n method: \"POST\",\n body: z.object({}).strict().optional(),\n },\n async (ctx) => {\n const nonce = options.getNonce\n ? await options.getNonce()\n : generateSiwsNonce();\n\n if (!isValidSiwsNonce(nonce)) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `SIWS getNonce must return a nonce of 8-${SIWS_NONCE_MAX_LENGTH} alphanumeric characters.`,\n status: 500,\n code: \"SIWS_INVALID_NONCE\",\n });\n }\n\n // Store nonce with 15-minute expiration\n await ctx.context.internalAdapter.createVerificationValue({\n identifier: `${SIWS_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`,\n value: nonce,\n expiresAt: new Date(Date.now() + 15 * 60 * 1000),\n });\n\n return ctx.json({ nonce });\n },\n ),\n\n verify: createAuthEndpoint(\n \"/siws/verify\",\n {\n method: \"POST\",\n body: z\n .object({\n message: z.string().min(1).max(SIWS_MESSAGE_MAX_LENGTH),\n signature: z.string().min(1).max(SIWS_SIGNATURE_MAX_LENGTH),\n email: z.email().optional(),\n })\n .strict()\n .refine((data) => options.anonymous !== false || !!data.email, {\n message:\n \"Email is required when the anonymous plugin option is disabled.\",\n path: [\"email\"],\n }),\n requireRequest: true,\n },\n async (ctx) => {\n const { message, signature, email } = ctx.body;\n const isAnon = options.anonymous ?? true;\n\n if (!isAnon && !email) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Email is required when anonymous is disabled.\",\n status: 400,\n });\n }\n\n try {\n // The signed message is the source of truth for wallet identity:\n // address, chain ID, nonce, and time bounds are read from it rather\n // than from the request body.\n const parsedMessage = parseSiwsMessage(message);\n const { address, chainId, nonce } = parsedMessage;\n\n if (\n !address ||\n !isAddress(address) ||\n !isValidSiwsNonce(nonce) ||\n typeof chainId !== \"number\" ||\n !Number.isInteger(chainId) ||\n chainId < 1 ||\n chainId > SIWS_MAX_CHAIN_ID ||\n parsedMessage.domain !== options.domain\n ) {\n throw siwsMessageMismatchError();\n }\n\n // Canonicalize the signed address before using it as an identity:\n // c32check decoding folds case and the confusable characters\n // `O`/`0` and `I`/`L`/`1`, so equivalent spellings must resolve to\n // a single wallet record instead of creating duplicate users.\n const walletAddress = getAddress(address);\n\n const now = new Date();\n if (parsedMessage.expirationTime) {\n if (\n Number.isNaN(parsedMessage.expirationTime.getTime()) ||\n now >= parsedMessage.expirationTime\n ) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: SIWS message has expired\",\n status: 401,\n code: \"UNAUTHORIZED_SIWS_MESSAGE_EXPIRED\",\n });\n }\n }\n if (parsedMessage.notBefore) {\n if (\n Number.isNaN(parsedMessage.notBefore.getTime()) ||\n now < parsedMessage.notBefore\n ) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: SIWS message is not yet valid\",\n status: 401,\n code: \"UNAUTHORIZED_SIWS_MESSAGE_NOT_YET_VALID\",\n });\n }\n }\n\n // Atomically consume the single-use nonce before any signature\n // work. The first concurrent request wins; every racer gets null.\n // Expired nonces are treated as already consumed.\n const verification =\n await ctx.context.internalAdapter.consumeVerificationValue(\n `${SIWS_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`,\n );\n\n if (!verification) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: Invalid or expired nonce\",\n status: 401,\n code: \"UNAUTHORIZED_INVALID_OR_EXPIRED_NONCE\",\n });\n }\n\n // Verify SIWS message with enhanced parameters\n const verified = await verifyMessage({\n message,\n signature,\n address: walletAddress,\n chainId,\n });\n\n if (!verified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Unauthorized: Invalid SIWS signature\",\n status: 401,\n });\n }\n\n // Look for existing user by their wallet addresses\n let user: User | null = null;\n\n // Check if there's a wallet address record for this exact address+chainId combination\n const existingWalletAddress: WalletAddress | null =\n await ctx.context.adapter.findOne({\n model: \"walletAddress\",\n where: [\n { field: \"address\", operator: \"eq\", value: walletAddress },\n { field: \"chainId\", operator: \"eq\", value: chainId },\n ],\n });\n\n if (existingWalletAddress) {\n // Get the user associated with this wallet address\n user = await ctx.context.adapter.findOne({\n model: \"user\",\n where: [\n {\n field: \"id\",\n operator: \"eq\",\n value: existingWalletAddress.userId,\n },\n ],\n });\n } else {\n // No exact match found, check if this address exists on any other chain\n const anyWalletAddress: WalletAddress | null =\n await ctx.context.adapter.findOne({\n model: \"walletAddress\",\n where: [\n { field: \"address\", operator: \"eq\", value: walletAddress },\n ],\n });\n\n if (anyWalletAddress) {\n // Same address exists on different chain, get that user\n user = await ctx.context.adapter.findOne({\n model: \"user\",\n where: [\n {\n field: \"id\",\n operator: \"eq\",\n value: anyWalletAddress.userId,\n },\n ],\n });\n }\n }\n\n // Create new user if none exists\n if (!user) {\n const normalizedEmail = email?.toLowerCase();\n const walletEmail = options.emailDomainName\n ? `${walletAddress}@${options.emailDomainName}`\n : createPlaceholderEmail({\n identifier: walletAddress,\n namespace: \"siws\",\n });\n // SIWS proves wallet control, not email ownership: bind the caller\n // email only when unclaimed and atomically reserved, else keep\n // the wallet-derived address. Silent fallback avoids an\n // enumeration oracle.\n let userEmail = walletEmail;\n let emailClaimIdentifier: string | undefined;\n if (!isAnon && normalizedEmail) {\n const identifier = `siws-email-claim-${normalizedEmail}`;\n let reserved = false;\n try {\n reserved =\n await ctx.context.internalAdapter.reserveVerificationValue({\n identifier,\n value: walletAddress,\n expiresAt: new Date(Date.now() + 60_000),\n });\n } catch {\n reserved = false;\n }\n if (reserved) {\n emailClaimIdentifier = identifier;\n const existingUser =\n await ctx.context.internalAdapter.findUserByEmail(\n normalizedEmail,\n );\n if (!existingUser) {\n userEmail = normalizedEmail;\n }\n }\n }\n const { name, avatar } =\n (await options.resolveProfile?.({ walletAddress })) ?? {};\n\n const createSIWSUser = (newUserEmail: string) =>\n ctx.context.internalAdapter.createUser(\n {\n name: name ?? walletAddress,\n email: newUserEmail,\n image: avatar ?? \"\",\n },\n { method: \"siws\" },\n );\n\n try {\n user = await createSIWSUser(userEmail);\n } catch (error) {\n if (userEmail !== normalizedEmail || !normalizedEmail) {\n throw error;\n }\n const claimedUser =\n await ctx.context.internalAdapter.findUserByEmail(\n normalizedEmail,\n );\n if (!claimedUser) {\n throw error;\n }\n userEmail = walletEmail;\n user = await createSIWSUser(userEmail);\n } finally {\n if (emailClaimIdentifier) {\n await ctx.context.internalAdapter\n .consumeVerificationValue(emailClaimIdentifier)\n .catch(() => {});\n }\n }\n\n // Create wallet address record\n await ctx.context.adapter.create<WalletAddress>({\n model: \"walletAddress\",\n data: {\n userId: user.id,\n address: walletAddress,\n chainId,\n isPrimary: true, // First address is primary\n createdAt: new Date(),\n },\n });\n\n // Create account record for wallet authentication\n await ctx.context.internalAdapter.createAccount({\n userId: user.id,\n providerId: \"siws\",\n accountId: `${walletAddress}:${chainId}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n });\n } else {\n // User exists, but check if this specific address/chain combo exists\n if (!existingWalletAddress) {\n // Add this new chainId to existing user's addresses\n await ctx.context.adapter.create({\n model: \"walletAddress\",\n data: {\n userId: user.id,\n address: walletAddress,\n chainId,\n isPrimary: false, // Additional addresses are not primary by default\n createdAt: new Date(),\n },\n });\n\n // Create account record for this new wallet+chain combination\n await ctx.context.internalAdapter.createAccount({\n userId: user.id,\n providerId: \"siws\",\n accountId: `${walletAddress}:${chainId}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n });\n }\n }\n\n const session = await ctx.context.internalAdapter.createSession(\n user.id,\n );\n\n if (!session) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Internal Server Error\",\n status: 500,\n });\n }\n\n await setSessionCookie(ctx, { session, user });\n\n return await ctx.json({\n token: session.token,\n success: true,\n user: {\n id: user.id,\n walletAddress,\n chainId,\n },\n });\n } catch (error) {\n if (isAPIError(error)) {\n throw error;\n }\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Something went wrong. Please try again later.\",\n error: error instanceof Error ? error.message : \"Unknown error\",\n status: 401,\n });\n }\n },\n ),\n },\n options,\n } satisfies BetterAuthPlugin;\n};\n"],"mappings":";;;;;;;;AAEA,MAAa,SAAS,EACpB,eAAe,EACb,QAAQ;CACN,QAAQ;EACN,MAAM;EACN,YAAY;GACV,OAAO;GACP,OAAO;EACT;EACA,UAAU;EACV,OAAO;CACT;CACA,SAAS;EACP,MAAM;EACN,UAAU;CACZ;CACA,SAAS;EACP,MAAM;EACN,UAAU;EACV,QAAQ;CACV;CACA,WAAW;EACT,MAAM;EACN,cAAc;CAChB;CACA,WAAW;EACT,MAAM;EACN,UAAU;CACZ;AACF,EACF,EACF;;ACcA,MAAM,sCAAsC;AAG5C,MAAM,wBACJ;AAEF,MAAM,gCAAgC;AACtC,MAAM,oBAAoB;AAM1B,MAAM,0BAA0B;AAChC,MAAM,4BAA4B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G,cAAc,GAAG;OAC/B,2BAAW,IAAI,KAAK;OACpB,2BAAW,IAAI,KAAK;MACtB,CAAC;KACH;KAGF,MAAM,UAAU,MAAM,IAAI,QAAQ,gBAAgB,cAChD,KAAK,EACP;KAEA,IAAI,CAAC,SACH,MAAM,IAAI,SAAS,yBAAyB;MAC1C,SAAS;MACT,QAAQ;KACV,CAAC;KAGH,MAAM,iBAAiB,KAAK;MAAE;MAAS;KAAK,CAAC;KAE7C,OAAO,MAAM,IAAI,KAAK;MACpB,OAAO,QAAQ;MACf,SAAS;MACT,MAAM;OACJ,IAAI,KAAK;OACT;OACA;MACF;KACF,CAAC;IACH,SAAS,OAAO;KACd,IAAI,WAAW,KAAK,GAClB,MAAM;KAER,MAAM,IAAI,SAAS,gBAAgB;MACjC,SAAS;MACT,OAAO,iBAAiB,QAAQ,MAAM,UAAU;MAChD,QAAQ;KACV,CAAC;IACH;GACF,CACF;EACF;EACA;CACF;AACF"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
import { InferOptionSchema } from "better-auth";
|
|
3
|
-
//#region src/plugins/better-auth/schema.d.ts
|
|
4
3
|
declare const schema: {
|
|
5
4
|
walletAddress: {
|
|
6
5
|
fields: {
|
|
@@ -33,9 +32,6 @@ declare const schema: {
|
|
|
33
32
|
};
|
|
34
33
|
};
|
|
35
34
|
};
|
|
36
|
-
type WalletAddressSchema = typeof schema;
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/plugins/better-auth/types.d.ts
|
|
39
35
|
interface SIWSVerifyMessageArgs {
|
|
40
36
|
message: string;
|
|
41
37
|
signature: string;
|
|
@@ -49,8 +45,6 @@ interface ResolveProfileResult {
|
|
|
49
45
|
name: string;
|
|
50
46
|
avatar: string;
|
|
51
47
|
}
|
|
52
|
-
//#endregion
|
|
53
|
-
//#region src/plugins/better-auth/plugin.d.ts
|
|
54
48
|
declare module "@better-auth/core" {
|
|
55
49
|
interface BetterAuthPluginRegistry<AuthOptions, Options> {
|
|
56
50
|
siws: {
|
|
@@ -70,7 +64,38 @@ interface SIWSPluginOptions {
|
|
|
70
64
|
declare const siws: (options: SIWSPluginOptions) => {
|
|
71
65
|
id: "siws";
|
|
72
66
|
version: string;
|
|
73
|
-
schema:
|
|
67
|
+
schema: {
|
|
68
|
+
walletAddress: {
|
|
69
|
+
fields: {
|
|
70
|
+
userId: {
|
|
71
|
+
type: "string";
|
|
72
|
+
references: {
|
|
73
|
+
model: string;
|
|
74
|
+
field: string;
|
|
75
|
+
};
|
|
76
|
+
required: true;
|
|
77
|
+
index: true;
|
|
78
|
+
};
|
|
79
|
+
address: {
|
|
80
|
+
type: "string";
|
|
81
|
+
required: true;
|
|
82
|
+
};
|
|
83
|
+
chainId: {
|
|
84
|
+
type: "number";
|
|
85
|
+
required: true;
|
|
86
|
+
bigint: true;
|
|
87
|
+
};
|
|
88
|
+
isPrimary: {
|
|
89
|
+
type: "boolean";
|
|
90
|
+
defaultValue: false;
|
|
91
|
+
};
|
|
92
|
+
createdAt: {
|
|
93
|
+
type: "date";
|
|
94
|
+
required: true;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
};
|
|
74
99
|
endpoints: {
|
|
75
100
|
nonce: import("better-auth").StrictEndpoint<"/siws/nonce", {
|
|
76
101
|
method: "POST";
|
|
@@ -98,8 +123,6 @@ declare const siws: (options: SIWSPluginOptions) => {
|
|
|
98
123
|
};
|
|
99
124
|
options: SIWSPluginOptions;
|
|
100
125
|
};
|
|
101
|
-
//#endregion
|
|
102
|
-
//#region src/plugins/better-auth/client.d.ts
|
|
103
126
|
declare const siwsClient: () => {
|
|
104
127
|
id: "siws";
|
|
105
128
|
version: string;
|
|
@@ -109,6 +132,5 @@ declare const siwsClient: () => {
|
|
|
109
132
|
"/siws/verify": "POST";
|
|
110
133
|
};
|
|
111
134
|
};
|
|
112
|
-
//#endregion
|
|
113
135
|
export { SIWSPluginOptions as n, siws as r, siwsClient as t };
|
|
114
|
-
//# sourceMappingURL=client-
|
|
136
|
+
//# sourceMappingURL=client-B0wBosYe.d.ts.map
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//#endregion
|
|
5
|
-
//#region src/plugins/better-auth/client.ts
|
|
1
|
+
// src/version.ts
|
|
2
|
+
const PACKAGE_VERSION = "0.4.2";
|
|
3
|
+
// src/plugins/better-auth/client.ts
|
|
6
4
|
const siwsClient = () => {
|
|
7
5
|
return {
|
|
8
6
|
id: "siws",
|
|
@@ -14,7 +12,6 @@ const siwsClient = () => {
|
|
|
14
12
|
}
|
|
15
13
|
};
|
|
16
14
|
};
|
|
17
|
-
//#endregion
|
|
18
15
|
export { PACKAGE_VERSION as n, siwsClient as t };
|
|
19
16
|
|
|
20
|
-
//# sourceMappingURL=client-
|
|
17
|
+
//# sourceMappingURL=client-DIS5YbyF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-DIS5YbyF.js","names":["pkg.version"],"sources":["../package.json","../src/version.ts","../src/plugins/better-auth/client.ts"],"sourcesContent":["","import pkg from \"../package.json\" with { type: \"json\" };\n\nexport const PACKAGE_VERSION = pkg.version;\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport { PACKAGE_VERSION } from \"../../version.js\";\nimport type { siws } from \"./plugin.js\";\n\nexport const siwsClient = () => {\n return {\n id: \"siws\",\n version: PACKAGE_VERSION,\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Better Auth reads the server plugin's type from this placeholder.\n $InferServerPlugin: {} as ReturnType<typeof siws>,\n pathMethods: {\n \"/siws/nonce\": \"POST\",\n \"/siws/verify\": \"POST\",\n },\n } satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";ACEA,MAAa,kBAAkBA;;ACE/B,MAAa,mBAAmB;CAC9B,OAAO;EACL,IAAI;EACJ,SAAS;EAET,oBAAoB,CAAC;EACrB,aAAa;GACX,eAAe;GACf,gBAAgB;EAClB;CACF;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
//#region src/errors/base.d.ts
|
|
2
1
|
type BaseErrorParameters = {
|
|
3
2
|
cause?: BaseError | Error | undefined;
|
|
4
3
|
details?: string | undefined;
|
|
5
4
|
docsBaseUrl?: string | undefined;
|
|
6
5
|
docsPath?: string | undefined;
|
|
7
6
|
docsSlug?: string | undefined;
|
|
8
|
-
metaMessages?: string
|
|
7
|
+
metaMessages?: Array<string> | undefined;
|
|
9
8
|
name?: string | undefined;
|
|
10
9
|
};
|
|
11
10
|
declare class BaseError extends Error {
|
|
12
|
-
details: string;
|
|
11
|
+
details: string | undefined;
|
|
13
12
|
docsPath?: string | undefined;
|
|
14
|
-
metaMessages?: string
|
|
13
|
+
metaMessages?: Array<string> | undefined;
|
|
15
14
|
shortMessage: string;
|
|
16
15
|
name: string;
|
|
17
16
|
constructor(shortMessage: string, args?: BaseErrorParameters);
|
|
18
17
|
walk(): Error;
|
|
19
18
|
walk(fn: (err: unknown) => boolean): Error | null;
|
|
20
19
|
}
|
|
21
|
-
//#endregion
|
|
22
|
-
//#region src/errors/address.d.ts
|
|
23
20
|
type InvalidAddressErrorType = InvalidAddressError & {
|
|
24
21
|
name: "InvalidAddressError";
|
|
25
22
|
};
|
|
@@ -28,19 +25,15 @@ declare class InvalidAddressError extends BaseError {
|
|
|
28
25
|
address: string;
|
|
29
26
|
});
|
|
30
27
|
}
|
|
31
|
-
//#endregion
|
|
32
|
-
//#region src/errors/siws.d.ts
|
|
33
28
|
type SiwsInvalidMessageFieldErrorType = SiwsInvalidMessageFieldError & {
|
|
34
29
|
name: "SiwsInvalidMessageFieldError";
|
|
35
30
|
};
|
|
36
31
|
declare class SiwsInvalidMessageFieldError extends BaseError {
|
|
37
32
|
constructor({ field, metaMessages }: {
|
|
38
33
|
field: string;
|
|
39
|
-
metaMessages?: string
|
|
34
|
+
metaMessages?: Array<string> | undefined;
|
|
40
35
|
});
|
|
41
36
|
}
|
|
42
|
-
//#endregion
|
|
43
|
-
//#region src/types.d.ts
|
|
44
37
|
/**
|
|
45
38
|
* @description Combines members of an intersection into a readable type.
|
|
46
39
|
*
|
|
@@ -92,7 +85,7 @@ type SiwsMessage = {
|
|
|
92
85
|
/**
|
|
93
86
|
* A list of information or references to information the user wishes to have resolved as part of authentication by the relying party.
|
|
94
87
|
*/
|
|
95
|
-
resources?: string
|
|
88
|
+
resources?: Array<string> | undefined;
|
|
96
89
|
/**
|
|
97
90
|
* [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request.
|
|
98
91
|
*/
|
|
@@ -110,8 +103,6 @@ type SiwsMessage = {
|
|
|
110
103
|
*/
|
|
111
104
|
version: "1";
|
|
112
105
|
};
|
|
113
|
-
//#endregion
|
|
114
|
-
//#region src/createSiwsMessage.d.ts
|
|
115
106
|
type CreateSiwsMessageParameters = SiwsMessage;
|
|
116
107
|
type CreateSiwsMessageReturnType = string;
|
|
117
108
|
type CreateSiwsMessageErrorType = InvalidAddressErrorType | SiwsInvalidMessageFieldErrorType;
|
|
@@ -131,21 +122,26 @@ type CreateSiwsMessageErrorType = InvalidAddressErrorType | SiwsInvalidMessageFi
|
|
|
131
122
|
* @see https://github.com/stacksgov/sips/pull/70
|
|
132
123
|
*/
|
|
133
124
|
export declare function createSiwsMessage(parameters: CreateSiwsMessageParameters): CreateSiwsMessageReturnType;
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/generateSiwsNonce.d.ts
|
|
136
125
|
/**
|
|
137
|
-
* @description Generates random SIP-X nonce
|
|
126
|
+
* @description Generates a random SIP-X nonce from a cryptographically
|
|
127
|
+
* secure source.
|
|
138
128
|
*
|
|
139
129
|
* @example
|
|
140
|
-
* const nonce =
|
|
130
|
+
* const nonce = generateSiwsNonce()
|
|
141
131
|
*
|
|
142
132
|
* @see https://github.com/stacksgov/sips/pull/70
|
|
143
133
|
*
|
|
144
134
|
* @returns A randomly generated SIP-X nonce.
|
|
145
135
|
*/
|
|
146
136
|
export declare function generateSiwsNonce(): string;
|
|
147
|
-
|
|
148
|
-
|
|
137
|
+
/**
|
|
138
|
+
* @description Parses SIP-X formatted message into message fields object.
|
|
139
|
+
*
|
|
140
|
+
* @see https://github.com/stacksgov/sips/pull/70
|
|
141
|
+
*
|
|
142
|
+
* @returns SIP-X fields object
|
|
143
|
+
*/
|
|
144
|
+
export declare function parseSiwsMessage(message: string): Prettify<ExactPartial<SiwsMessage>>;
|
|
149
145
|
type ValidateSiwsMessageParameters = {
|
|
150
146
|
/**
|
|
151
147
|
* Stacks address to check against.
|
|
@@ -181,8 +177,6 @@ type ValidateSiwsMessageReturnType = boolean;
|
|
|
181
177
|
* @see https://github.com/stacksgov/sips/pull/70
|
|
182
178
|
*/
|
|
183
179
|
export declare function validateSiwsMessage(parameters: ValidateSiwsMessageParameters): ValidateSiwsMessageReturnType;
|
|
184
|
-
//#endregion
|
|
185
|
-
//#region src/verifySiwsMessage.d.ts
|
|
186
180
|
type VerifySiwsMessageParameters = Prettify<Pick<ValidateSiwsMessageParameters, "address" | "domain" | "nonce" | "scheme" | "time"> & {
|
|
187
181
|
/**
|
|
188
182
|
* SIP-X formatted message.
|
|
@@ -201,6 +195,5 @@ type VerifySiwsMessageReturnType = boolean;
|
|
|
201
195
|
* @returns Whether or not the signature is valid. {@link VerifySiwsMessageReturnType}
|
|
202
196
|
*/
|
|
203
197
|
export declare function verifySiwsMessage(parameters: VerifySiwsMessageParameters): VerifySiwsMessageReturnType;
|
|
204
|
-
//#endregion
|
|
205
198
|
export type { CreateSiwsMessageErrorType, CreateSiwsMessageParameters, CreateSiwsMessageReturnType, ValidateSiwsMessageParameters, ValidateSiwsMessageReturnType, VerifySiwsMessageParameters, VerifySiwsMessageReturnType };
|
|
206
199
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { createSiwsMessage, generateSiwsNonce, validateSiwsMessage, verifySiwsMessage };
|
|
1
|
+
export { a as createSiwsMessage, i as generateSiwsNonce, r as parseSiwsMessage, n as validateSiwsMessage, t as verifySiwsMessage } from "./src-eUAWnT_0.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as siwsClient } from "../../client-
|
|
1
|
+
import { t as siwsClient } from "../../client-B0wBosYe.js";
|
|
2
2
|
export { siwsClient };
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { siwsClient };
|
|
1
|
+
export { t as siwsClient } from "../../client-DIS5YbyF.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as SIWSPluginOptions, r as siws, t as siwsClient } from "../../client-
|
|
1
|
+
import { n as SIWSPluginOptions, r as siws, t as siwsClient } from "../../client-B0wBosYe.js";
|
|
2
2
|
export { SIWSPluginOptions, siws, siwsClient };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as siwsClient } from "../../client-
|
|
2
|
-
import { t as siws } from "../../better-auth-
|
|
1
|
+
import { t as siwsClient } from "../../client-DIS5YbyF.js";
|
|
2
|
+
import { t as siws } from "../../better-auth-CdqoUp9E.js";
|
|
3
3
|
export { siws, siwsClient };
|
package/dist/plugins/index.d.ts
CHANGED
package/dist/plugins/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as siwsClient } from "../client-
|
|
2
|
-
import { t as siws } from "../better-auth-
|
|
1
|
+
import { t as siwsClient } from "../client-DIS5YbyF.js";
|
|
2
|
+
import { t as siws } from "../better-auth-CdqoUp9E.js";
|
|
3
3
|
export { siws, siwsClient };
|