better-auth-sign-in-with-typed-data 0.1.0
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/LICENSE +21 -0
- package/README.md +111 -0
- package/dist/client.d.ts +10 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +14 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +242 -0
- package/dist/schema.d.ts +33 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HiiiiD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# better-auth-sign-in-with-typed-data (SIWTD)
|
|
2
|
+
|
|
3
|
+
A better-auth plugin for logging in by signing EIP-712 typed data, instead of
|
|
4
|
+
an ERC-4361 (SIWE) plain-text message. Works with any EVM-style secp256k1
|
|
5
|
+
wallet, including VeChain wallets (VeWorld, Sync2/Connex).
|
|
6
|
+
|
|
7
|
+
Unlike the official `siwe` plugin, the typed-data schema (domain/types/
|
|
8
|
+
message shape) is entirely up to you — the plugin only requires a `nonce`
|
|
9
|
+
field inside the signed `message`, and a `chainId` in the signed `domain`.
|
|
10
|
+
There is no email/password support: wallet address is the sole identity.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun add better-auth-sign-in-with-typed-data viem
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Server
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { betterAuth } from "better-auth";
|
|
22
|
+
import { siwtd } from "better-auth-sign-in-with-typed-data";
|
|
23
|
+
|
|
24
|
+
export const auth = betterAuth({
|
|
25
|
+
plugins: [
|
|
26
|
+
siwtd({
|
|
27
|
+
domain: { name: "my-app.example.com", chainId: 1 },
|
|
28
|
+
getNonce: async () => crypto.randomUUID().replace(/-/g, ""),
|
|
29
|
+
}),
|
|
30
|
+
],
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Client
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { createAuthClient } from "better-auth/client";
|
|
38
|
+
import { siwtdClient } from "better-auth-sign-in-with-typed-data/client";
|
|
39
|
+
|
|
40
|
+
export const authClient = createAuthClient({
|
|
41
|
+
plugins: [siwtdClient()],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const { data: nonceData } = await authClient.siwtd.nonce();
|
|
45
|
+
|
|
46
|
+
const types = { Login: [{ name: "nonce", type: "string" }] } as const;
|
|
47
|
+
const message = { nonce: nonceData.nonce };
|
|
48
|
+
const domain = { name: "my-app.example.com", chainId: 1 };
|
|
49
|
+
|
|
50
|
+
const signature = await walletClient.signTypedData({
|
|
51
|
+
domain,
|
|
52
|
+
types,
|
|
53
|
+
primaryType: "Login",
|
|
54
|
+
message,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const { data } = await authClient.siwtd.verify({
|
|
58
|
+
domain,
|
|
59
|
+
types,
|
|
60
|
+
primaryType: "Login",
|
|
61
|
+
message,
|
|
62
|
+
signature,
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Options
|
|
67
|
+
|
|
68
|
+
| Option | Required | Description |
|
|
69
|
+
| --- | --- | --- |
|
|
70
|
+
| `domain` | yes | EIP-712 domain fields to check against the signed typed data. Only the fields you set here are enforced (`name`, `chainId`, `verifyingContract`, `version`). |
|
|
71
|
+
| `getNonce` | yes | Returns a fresh nonce (8+ alphanumeric characters) for each `/siwtd/nonce` call. |
|
|
72
|
+
| `ensLookup` | no | Resolve a display `name`/`avatar` for a wallet address on first login. |
|
|
73
|
+
| `schema` | no | Override the `walletAddress` table schema. |
|
|
74
|
+
|
|
75
|
+
## How verification works
|
|
76
|
+
|
|
77
|
+
1. Client requests a nonce from `/siwtd/nonce`; the server stores it.
|
|
78
|
+
2. Client signs an EIP-712 typed-data payload of its own design, as long as
|
|
79
|
+
the signed `message` includes that `nonce` and the signed `domain`
|
|
80
|
+
includes a `chainId`.
|
|
81
|
+
3. Client submits `{ domain, types, primaryType, message, signature }` to
|
|
82
|
+
`/siwtd/verify`.
|
|
83
|
+
4. The server atomically consumes the nonce (single use), checks the signed
|
|
84
|
+
`domain` against the plugin's configured `domain` option, and recovers
|
|
85
|
+
the signer's address from the signature via viem. The recovered address
|
|
86
|
+
is the only trusted source of wallet identity — any address-shaped field
|
|
87
|
+
inside `message` itself is ignored.
|
|
88
|
+
5. On success, the server finds or creates a `user` + `walletAddress` +
|
|
89
|
+
`account` record for that address/chain, creates a session, and returns
|
|
90
|
+
a session token.
|
|
91
|
+
|
|
92
|
+
## Testing
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
bun test
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Releasing
|
|
99
|
+
|
|
100
|
+
Versioning uses [changesets](https://github.com/changesets/changesets), but
|
|
101
|
+
nothing bumps automatically on every push to `master` — commits accumulate
|
|
102
|
+
until you're ready to cut a release:
|
|
103
|
+
|
|
104
|
+
1. On each PR that needs a release note, run `bunx changeset` and describe
|
|
105
|
+
the change; commit the generated file under `.changeset/`.
|
|
106
|
+
2. When ready to release, manually run the `Version` workflow (Actions tab
|
|
107
|
+
→ Version → Run workflow). It bumps the version, updates the changelog
|
|
108
|
+
from all pending changesets, commits straight to `master`, and pushes a
|
|
109
|
+
`vX.Y.Z` tag.
|
|
110
|
+
3. The tag push triggers the `Publish` workflow, which builds and publishes
|
|
111
|
+
to npm.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { siwtd } from "./index";
|
|
2
|
+
export declare const siwtdClient: () => {
|
|
3
|
+
id: "siwtd";
|
|
4
|
+
$InferServerPlugin: ReturnType<typeof siwtd>;
|
|
5
|
+
pathMethods: {
|
|
6
|
+
"/siwtd/nonce": "POST";
|
|
7
|
+
"/siwtd/verify": "POST";
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,eAAO,MAAM,WAAW;;wBAGI,UAAU,CAAC,OAAO,KAAK,CAAC;;;;;CAMnD,CAAC"}
|
package/dist/client.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { InferOptionSchema } from "better-auth/types";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import { schema, type WalletAddressSchema } from "./schema";
|
|
4
|
+
import type { ENSLookupArgs, ENSLookupResult } from "./types";
|
|
5
|
+
export interface SIWTDDomainOptions {
|
|
6
|
+
name?: string;
|
|
7
|
+
chainId?: number;
|
|
8
|
+
verifyingContract?: `0x${string}`;
|
|
9
|
+
version?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface SIWTDPluginOptions {
|
|
12
|
+
domain: SIWTDDomainOptions;
|
|
13
|
+
getNonce: () => Promise<string>;
|
|
14
|
+
ensLookup?: (args: ENSLookupArgs) => Promise<ENSLookupResult>;
|
|
15
|
+
schema?: InferOptionSchema<typeof schema>;
|
|
16
|
+
}
|
|
17
|
+
export declare const siwtd: (options: SIWTDPluginOptions) => {
|
|
18
|
+
id: "siwtd";
|
|
19
|
+
schema: WalletAddressSchema;
|
|
20
|
+
endpoints: {
|
|
21
|
+
getSiwtdNonce: import("better-call").StrictEndpoint<"/siwtd/nonce", {
|
|
22
|
+
method: "POST";
|
|
23
|
+
body: z.ZodOptional<z.ZodObject<{}, z.core.$strict>>;
|
|
24
|
+
}, {
|
|
25
|
+
nonce: string;
|
|
26
|
+
}>;
|
|
27
|
+
verifySiwtdMessage: import("better-call").StrictEndpoint<"/siwtd/verify", {
|
|
28
|
+
method: "POST";
|
|
29
|
+
body: z.ZodObject<{
|
|
30
|
+
domain: z.ZodRecord<z.ZodString, z.ZodAny>;
|
|
31
|
+
types: z.ZodRecord<z.ZodString, z.ZodAny>;
|
|
32
|
+
primaryType: z.ZodString;
|
|
33
|
+
message: z.ZodRecord<z.ZodString, z.ZodAny>;
|
|
34
|
+
signature: z.ZodString;
|
|
35
|
+
}, z.core.$strict>;
|
|
36
|
+
requireRequest: true;
|
|
37
|
+
}, {
|
|
38
|
+
token: string;
|
|
39
|
+
success: boolean;
|
|
40
|
+
user: {
|
|
41
|
+
id: string;
|
|
42
|
+
walletAddress: `0x${string}`;
|
|
43
|
+
chainId: number;
|
|
44
|
+
};
|
|
45
|
+
}>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAQ,MAAM,mBAAmB,CAAC;AAQjE,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,MAAM,EAAE,KAAK,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAiB,MAAM,SAAS,CAAC;AAE7E,MAAM,WAAW,kBAAkB;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iBAAiB,CAAC,EAAE,KAAK,MAAM,EAAE,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,kBAAkB;IAClC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,QAAQ,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;IAC9D,MAAM,CAAC,EAAE,iBAAiB,CAAC,OAAO,MAAM,CAAC,CAAC;CAC1C;AAkDD,eAAO,MAAM,KAAK,GAAI,SAAS,kBAAkB;;YAGC,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyOpE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { APIError, createAuthEndpoint, isAPIError } from "better-auth/api";
|
|
3
|
+
import { setSessionCookie } from "better-auth/cookies";
|
|
4
|
+
import { mergeSchema } from "better-auth/db";
|
|
5
|
+
import {
|
|
6
|
+
getAddress,
|
|
7
|
+
recoverTypedDataAddress
|
|
8
|
+
} from "viem";
|
|
9
|
+
import * as z from "zod";
|
|
10
|
+
import { schema } from "./schema";
|
|
11
|
+
var SIWTD_VERIFICATION_IDENTIFIER_PREFIX = "siwtd:";
|
|
12
|
+
var VERIFICATION_IDENTIFIER_MAX_LENGTH = 255;
|
|
13
|
+
var SIWTD_NONCE_MAX_LENGTH = VERIFICATION_IDENTIFIER_MAX_LENGTH - SIWTD_VERIFICATION_IDENTIFIER_PREFIX.length;
|
|
14
|
+
var SIWTD_NONCE_ALPHANUMERIC_REGEX = /^[a-zA-Z0-9]+$/;
|
|
15
|
+
var isValidSiwtdNonce = (nonce) => typeof nonce === "string" && nonce.length >= 8 && nonce.length <= SIWTD_NONCE_MAX_LENGTH && SIWTD_NONCE_ALPHANUMERIC_REGEX.test(nonce);
|
|
16
|
+
var getSiwtdNonceBodySchema = z.object({}).strict().optional();
|
|
17
|
+
var verifySiwtdBodySchema = z.object({
|
|
18
|
+
domain: z.record(z.string(), z.any()),
|
|
19
|
+
types: z.record(z.string(), z.any()),
|
|
20
|
+
primaryType: z.string().min(1),
|
|
21
|
+
message: z.record(z.string(), z.any()),
|
|
22
|
+
signature: z.string().min(1)
|
|
23
|
+
}).strict();
|
|
24
|
+
function domainMatches(expected, signed) {
|
|
25
|
+
if (expected.name !== undefined && expected.name !== signed.name)
|
|
26
|
+
return false;
|
|
27
|
+
if (expected.chainId !== undefined && expected.chainId !== signed.chainId)
|
|
28
|
+
return false;
|
|
29
|
+
if (expected.verifyingContract !== undefined && expected.verifyingContract.toLowerCase() !== signed.verifyingContract?.toLowerCase())
|
|
30
|
+
return false;
|
|
31
|
+
if (expected.version !== undefined && expected.version !== signed.version)
|
|
32
|
+
return false;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
function createPlaceholderEmail(address) {
|
|
36
|
+
return `${address.toLowerCase()}@siwtd.invalid`;
|
|
37
|
+
}
|
|
38
|
+
var siwtd = (options) => {
|
|
39
|
+
return {
|
|
40
|
+
id: "siwtd",
|
|
41
|
+
schema: mergeSchema(schema, options?.schema),
|
|
42
|
+
endpoints: {
|
|
43
|
+
getSiwtdNonce: createAuthEndpoint("/siwtd/nonce", {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: getSiwtdNonceBodySchema
|
|
46
|
+
}, async (ctx) => {
|
|
47
|
+
const nonce = await options.getNonce();
|
|
48
|
+
if (!isValidSiwtdNonce(nonce)) {
|
|
49
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
50
|
+
message: `SIWTD getNonce must return 8-${SIWTD_NONCE_MAX_LENGTH} alphanumeric characters.`,
|
|
51
|
+
code: "SIWTD_INVALID_NONCE"
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
await ctx.context.internalAdapter.createVerificationValue({
|
|
55
|
+
identifier: `${SIWTD_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`,
|
|
56
|
+
value: nonce,
|
|
57
|
+
expiresAt: new Date(Date.now() + 15 * 60 * 1000)
|
|
58
|
+
});
|
|
59
|
+
return ctx.json({ nonce });
|
|
60
|
+
}),
|
|
61
|
+
verifySiwtdMessage: createAuthEndpoint("/siwtd/verify", {
|
|
62
|
+
method: "POST",
|
|
63
|
+
body: verifySiwtdBodySchema,
|
|
64
|
+
requireRequest: true
|
|
65
|
+
}, async (ctx) => {
|
|
66
|
+
const { domain, types, primaryType, message, signature } = ctx.body;
|
|
67
|
+
const nonce = message.nonce;
|
|
68
|
+
if (typeof nonce !== "string" || !isValidSiwtdNonce(nonce)) {
|
|
69
|
+
throw new APIError("BAD_REQUEST", {
|
|
70
|
+
message: "message.nonce is required and must be a valid nonce."
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const verification = await ctx.context.internalAdapter.consumeVerificationValue(`${SIWTD_VERIFICATION_IDENTIFIER_PREFIX}${nonce}`);
|
|
75
|
+
if (!verification) {
|
|
76
|
+
throw new APIError("UNAUTHORIZED", {
|
|
77
|
+
message: "Invalid or expired nonce",
|
|
78
|
+
code: "UNAUTHORIZED_INVALID_OR_EXPIRED_NONCE"
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (!domainMatches(options.domain, domain)) {
|
|
82
|
+
throw new APIError("UNAUTHORIZED", {
|
|
83
|
+
message: "Signed domain does not match the expected domain",
|
|
84
|
+
code: "UNAUTHORIZED_DOMAIN_MISMATCH"
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const signedChainId = domain.chainId;
|
|
88
|
+
if (typeof signedChainId !== "number" || signedChainId <= 0) {
|
|
89
|
+
throw new APIError("UNAUTHORIZED", {
|
|
90
|
+
message: "Signed domain is missing a valid chainId",
|
|
91
|
+
code: "UNAUTHORIZED_INVALID_CHAIN_ID"
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
let recovered;
|
|
95
|
+
try {
|
|
96
|
+
recovered = await recoverTypedDataAddress({
|
|
97
|
+
domain,
|
|
98
|
+
types,
|
|
99
|
+
primaryType,
|
|
100
|
+
message,
|
|
101
|
+
signature
|
|
102
|
+
});
|
|
103
|
+
} catch {
|
|
104
|
+
throw new APIError("UNAUTHORIZED", {
|
|
105
|
+
message: "Invalid signature",
|
|
106
|
+
code: "UNAUTHORIZED_INVALID_SIGNATURE"
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const walletAddress = getAddress(recovered);
|
|
110
|
+
const expirationTime = message.expirationTime;
|
|
111
|
+
const notBefore = message.notBefore;
|
|
112
|
+
const now = Date.now();
|
|
113
|
+
if (typeof expirationTime === "string") {
|
|
114
|
+
const expiresAt = Date.parse(expirationTime);
|
|
115
|
+
if (!Number.isNaN(expiresAt) && now >= expiresAt) {
|
|
116
|
+
throw new APIError("UNAUTHORIZED", {
|
|
117
|
+
message: "Message has expired",
|
|
118
|
+
code: "UNAUTHORIZED_MESSAGE_EXPIRED"
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (typeof notBefore === "string") {
|
|
123
|
+
const notBeforeAt = Date.parse(notBefore);
|
|
124
|
+
if (!Number.isNaN(notBeforeAt) && now < notBeforeAt) {
|
|
125
|
+
throw new APIError("UNAUTHORIZED", {
|
|
126
|
+
message: "Message is not yet valid",
|
|
127
|
+
code: "UNAUTHORIZED_MESSAGE_NOT_YET_VALID"
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
let user = null;
|
|
132
|
+
const existingWalletAddress = await ctx.context.adapter.findOne({
|
|
133
|
+
model: "walletAddress",
|
|
134
|
+
where: [
|
|
135
|
+
{ field: "address", operator: "eq", value: walletAddress },
|
|
136
|
+
{ field: "chainId", operator: "eq", value: signedChainId }
|
|
137
|
+
]
|
|
138
|
+
});
|
|
139
|
+
if (existingWalletAddress) {
|
|
140
|
+
user = await ctx.context.adapter.findOne({
|
|
141
|
+
model: "user",
|
|
142
|
+
where: [
|
|
143
|
+
{
|
|
144
|
+
field: "id",
|
|
145
|
+
operator: "eq",
|
|
146
|
+
value: existingWalletAddress.userId
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
});
|
|
150
|
+
} else {
|
|
151
|
+
const anyWalletAddress = await ctx.context.adapter.findOne({
|
|
152
|
+
model: "walletAddress",
|
|
153
|
+
where: [
|
|
154
|
+
{ field: "address", operator: "eq", value: walletAddress }
|
|
155
|
+
]
|
|
156
|
+
});
|
|
157
|
+
if (anyWalletAddress) {
|
|
158
|
+
user = await ctx.context.adapter.findOne({
|
|
159
|
+
model: "user",
|
|
160
|
+
where: [
|
|
161
|
+
{
|
|
162
|
+
field: "id",
|
|
163
|
+
operator: "eq",
|
|
164
|
+
value: anyWalletAddress.userId
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (!user) {
|
|
171
|
+
const { name, avatar } = await options.ensLookup?.({ walletAddress }) ?? {};
|
|
172
|
+
user = await ctx.context.internalAdapter.createUser({
|
|
173
|
+
name: name ?? walletAddress,
|
|
174
|
+
email: createPlaceholderEmail(walletAddress),
|
|
175
|
+
image: avatar ?? ""
|
|
176
|
+
}, { method: "siwtd" });
|
|
177
|
+
await ctx.context.adapter.create({
|
|
178
|
+
model: "walletAddress",
|
|
179
|
+
data: {
|
|
180
|
+
userId: user.id,
|
|
181
|
+
address: walletAddress,
|
|
182
|
+
chainId: signedChainId,
|
|
183
|
+
isPrimary: true,
|
|
184
|
+
createdAt: new Date
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
await ctx.context.internalAdapter.createAccount({
|
|
188
|
+
userId: user.id,
|
|
189
|
+
providerId: "siwtd",
|
|
190
|
+
accountId: `${walletAddress}:${signedChainId}`,
|
|
191
|
+
createdAt: new Date,
|
|
192
|
+
updatedAt: new Date
|
|
193
|
+
});
|
|
194
|
+
} else if (!existingWalletAddress) {
|
|
195
|
+
await ctx.context.adapter.create({
|
|
196
|
+
model: "walletAddress",
|
|
197
|
+
data: {
|
|
198
|
+
userId: user.id,
|
|
199
|
+
address: walletAddress,
|
|
200
|
+
chainId: signedChainId,
|
|
201
|
+
isPrimary: false,
|
|
202
|
+
createdAt: new Date
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
await ctx.context.internalAdapter.createAccount({
|
|
206
|
+
userId: user.id,
|
|
207
|
+
providerId: "siwtd",
|
|
208
|
+
accountId: `${walletAddress}:${signedChainId}`,
|
|
209
|
+
createdAt: new Date,
|
|
210
|
+
updatedAt: new Date
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const session = await ctx.context.internalAdapter.createSession(user.id);
|
|
214
|
+
if (!session) {
|
|
215
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
216
|
+
message: "Failed to create session"
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
await setSessionCookie(ctx, { session, user });
|
|
220
|
+
return ctx.json({
|
|
221
|
+
token: session.token,
|
|
222
|
+
success: true,
|
|
223
|
+
user: {
|
|
224
|
+
id: user.id,
|
|
225
|
+
walletAddress,
|
|
226
|
+
chainId: signedChainId
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
} catch (error) {
|
|
230
|
+
if (isAPIError(error))
|
|
231
|
+
throw error;
|
|
232
|
+
throw new APIError("UNAUTHORIZED", {
|
|
233
|
+
message: "Something went wrong. Please try again later."
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
export {
|
|
241
|
+
siwtd
|
|
242
|
+
};
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const schema: {
|
|
2
|
+
walletAddress: {
|
|
3
|
+
fields: {
|
|
4
|
+
userId: {
|
|
5
|
+
type: "string";
|
|
6
|
+
required: true;
|
|
7
|
+
references: {
|
|
8
|
+
model: string;
|
|
9
|
+
field: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
address: {
|
|
13
|
+
type: "string";
|
|
14
|
+
required: true;
|
|
15
|
+
};
|
|
16
|
+
chainId: {
|
|
17
|
+
type: "number";
|
|
18
|
+
required: true;
|
|
19
|
+
};
|
|
20
|
+
isPrimary: {
|
|
21
|
+
type: "boolean";
|
|
22
|
+
required: true;
|
|
23
|
+
defaultValue: false;
|
|
24
|
+
};
|
|
25
|
+
createdAt: {
|
|
26
|
+
type: "date";
|
|
27
|
+
required: true;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export type WalletAddressSchema = typeof schema;
|
|
33
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../schema.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8BiB,CAAC;AAErC,MAAM,MAAM,mBAAmB,GAAG,OAAO,MAAM,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface WalletAddress {
|
|
2
|
+
id: string;
|
|
3
|
+
userId: string;
|
|
4
|
+
address: string;
|
|
5
|
+
chainId: number;
|
|
6
|
+
isPrimary: boolean;
|
|
7
|
+
createdAt: Date;
|
|
8
|
+
}
|
|
9
|
+
export interface ENSLookupArgs {
|
|
10
|
+
walletAddress: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ENSLookupResult {
|
|
13
|
+
name?: string;
|
|
14
|
+
avatar?: string;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,IAAI,CAAC;CAChB;AAED,MAAM,WAAW,aAAa;IAC7B,aAAa,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAe;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "better-auth-sign-in-with-typed-data",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A better-auth plugin for logging in by signing EIP-712 typed data instead of a SIWE plain-text message.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/HiiiiD/better-auth-sign-in-with-typed-data.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./client": {
|
|
23
|
+
"types": "./dist/client.d.ts",
|
|
24
|
+
"default": "./dist/client.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "bun test",
|
|
29
|
+
"build": "rm -rf dist && bun build ./index.ts ./client.ts --target=node --outdir=dist --external '*' && tsc -p tsconfig.build.json",
|
|
30
|
+
"prepublishOnly": "bun run build"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@better-auth/memory-adapter": "^1.7.5",
|
|
34
|
+
"@changesets/changelog-github": "^1.0.1",
|
|
35
|
+
"@changesets/cli": "^3.0.3",
|
|
36
|
+
"@types/bun": "latest"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"typescript": "^7"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@better-auth/core": "^1.7.5",
|
|
43
|
+
"better-auth": "^1.7.5",
|
|
44
|
+
"viem": "^2.56.8",
|
|
45
|
+
"zod": "^4.6.5"
|
|
46
|
+
}
|
|
47
|
+
}
|