better-near-auth 0.3.3 → 0.3.4
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/package.json +6 -6
- package/src/client.ts +5 -16
- package/src/index.ts +4 -10
- package/src/types.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-near-auth",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Sign in with NEAR (SIWN) plugin for Better Auth",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -60,18 +60,18 @@
|
|
|
60
60
|
"typescript": "^5.9.3"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@hot-labs/near-connect": "^0.8.
|
|
63
|
+
"@hot-labs/near-connect": "^0.8.2",
|
|
64
64
|
"nanostores": "^1.1.0",
|
|
65
65
|
"near-kit": "^0.7.0",
|
|
66
66
|
"near-sign-verify": "^0.4.5",
|
|
67
|
-
"zod": "^4.
|
|
67
|
+
"zod": "^4.3.5"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@types/bun": "latest",
|
|
71
|
-
"@types/node": "^
|
|
72
|
-
"better-auth": "^1.4.
|
|
71
|
+
"@types/node": "^25.0.9",
|
|
72
|
+
"better-auth": "^1.4.13",
|
|
73
73
|
"typescript": "^5.9.3",
|
|
74
|
-
"vitest": "^
|
|
74
|
+
"vitest": "^4.0.17"
|
|
75
75
|
},
|
|
76
76
|
"files": [
|
|
77
77
|
"src/",
|
package/src/client.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { NearConnector } from "@hot-labs/near-connect";
|
|
2
|
-
import type {
|
|
2
|
+
import type { NearWalletBase } from "@hot-labs/near-connect";
|
|
3
|
+
|
|
4
|
+
type Account = Awaited<ReturnType<NearWalletBase["getAccounts"]>>[number];
|
|
3
5
|
import type { BetterAuthClientPlugin, BetterFetch, BetterFetchOption, BetterFetchResponse } from "better-auth/client";
|
|
4
6
|
import { atom } from "nanostores";
|
|
5
7
|
import { Near, fromHotConnect } from "near-kit";
|
|
@@ -22,7 +24,7 @@ export interface SIWNClientConfig {
|
|
|
22
24
|
export interface CachedNonceData {
|
|
23
25
|
nonce: string;
|
|
24
26
|
accountId: string;
|
|
25
|
-
publicKey
|
|
27
|
+
publicKey?: string | null;
|
|
26
28
|
networkId: string;
|
|
27
29
|
timestamp: number;
|
|
28
30
|
}
|
|
@@ -92,10 +94,9 @@ export const siwnClient = (config: SIWNClientConfig): SIWNClientPlugin => {
|
|
|
92
94
|
const accountId = accounts?.[0]?.accountId;
|
|
93
95
|
if (!accountId) return;
|
|
94
96
|
|
|
95
|
-
// Create Near instance with Hot Connect wallet adapter
|
|
96
97
|
nearInstance = new Near({
|
|
97
98
|
network,
|
|
98
|
-
wallet: fromHotConnect(connector),
|
|
99
|
+
wallet: fromHotConnect(connector as Parameters<typeof fromHotConnect>[0]),
|
|
99
100
|
});
|
|
100
101
|
|
|
101
102
|
// Update state with account info
|
|
@@ -217,10 +218,8 @@ export const siwnClient = (config: SIWNClientConfig): SIWNClientPlugin => {
|
|
|
217
218
|
throw error;
|
|
218
219
|
}
|
|
219
220
|
|
|
220
|
-
// Get nonce first
|
|
221
221
|
const nonceRequest: NonceRequestT = {
|
|
222
222
|
accountId,
|
|
223
|
-
publicKey: state.publicKey || "",
|
|
224
223
|
networkId: (state.networkId || network) as "mainnet" | "testnet"
|
|
225
224
|
};
|
|
226
225
|
|
|
@@ -330,18 +329,8 @@ export const siwnClient = (config: SIWNClientConfig): SIWNClientPlugin => {
|
|
|
330
329
|
|
|
331
330
|
const { accountId, networkId, publicKey } = state;
|
|
332
331
|
|
|
333
|
-
if (!publicKey) {
|
|
334
|
-
throw new Error("Failed to get public key from wallet");
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
nearState.set({
|
|
338
|
-
...state,
|
|
339
|
-
publicKey,
|
|
340
|
-
});
|
|
341
|
-
|
|
342
332
|
const nonceRequest: NonceRequestT = {
|
|
343
333
|
accountId,
|
|
344
|
-
publicKey: publicKey,
|
|
345
334
|
networkId: networkId as "mainnet" | "testnet"
|
|
346
335
|
};
|
|
347
336
|
|
package/src/index.ts
CHANGED
|
@@ -140,12 +140,10 @@ export const siwn = (options: SIWNPluginOptions) =>
|
|
|
140
140
|
...(options.validateMessage ? { validateMessage: options.validateMessage } : {}),
|
|
141
141
|
} as VerifyOptions;
|
|
142
142
|
|
|
143
|
-
// Verify the signature using near-sign-verify to extract publicKey
|
|
144
143
|
const result: VerificationResult = await verify(authToken, verifyOptions);
|
|
145
144
|
|
|
146
|
-
// Now retrieve the nonce using accountId, network, AND publicKey
|
|
147
145
|
const verification = await ctx.context.internalAdapter.findVerificationValue(
|
|
148
|
-
`siwn:${accountId}:${network}
|
|
146
|
+
`siwn:${accountId}:${network}`,
|
|
149
147
|
);
|
|
150
148
|
|
|
151
149
|
if (!verification || new Date() > verification.expiresAt) {
|
|
@@ -372,7 +370,7 @@ export const siwn = (options: SIWNPluginOptions) =>
|
|
|
372
370
|
body: NonceRequest,
|
|
373
371
|
},
|
|
374
372
|
async (ctx) => {
|
|
375
|
-
const { accountId, networkId
|
|
373
|
+
const { accountId, networkId } = ctx.body;
|
|
376
374
|
const network = getNetworkFromAccountId(accountId);
|
|
377
375
|
|
|
378
376
|
if (networkId !== network) {
|
|
@@ -384,12 +382,10 @@ export const siwn = (options: SIWNPluginOptions) =>
|
|
|
384
382
|
|
|
385
383
|
const nonce = options.getNonce ? await options.getNonce() : generateNonce();
|
|
386
384
|
|
|
387
|
-
// Store nonce as base64 string for database compatibility
|
|
388
385
|
const nonceString = bytesToBase64(nonce);
|
|
389
386
|
|
|
390
|
-
// Store nonce with accountId, network, and publicKey for unique identification
|
|
391
387
|
await ctx.context.internalAdapter.createVerificationValue({
|
|
392
|
-
identifier: `siwn:${accountId}:${network}
|
|
388
|
+
identifier: `siwn:${accountId}:${network}`,
|
|
393
389
|
value: nonceString!,
|
|
394
390
|
expiresAt: new Date(Date.now() + 15 * 60 * 1000),
|
|
395
391
|
});
|
|
@@ -479,13 +475,11 @@ export const siwn = (options: SIWNPluginOptions) =>
|
|
|
479
475
|
...(options.validateMessage ? { validateMessage: options.validateMessage } : {}),
|
|
480
476
|
} as VerifyOptions;
|
|
481
477
|
|
|
482
|
-
// Verify the signature using near-sign-verify to extract publicKey
|
|
483
478
|
const result: VerificationResult = await verify(authToken, verifyOptions);
|
|
484
479
|
|
|
485
|
-
// Now retrieve the nonce using accountId, network, AND publicKey
|
|
486
480
|
const verification =
|
|
487
481
|
await ctx.context.internalAdapter.findVerificationValue(
|
|
488
|
-
`siwn:${accountId}:${network}
|
|
482
|
+
`siwn:${accountId}:${network}`,
|
|
489
483
|
);
|
|
490
484
|
|
|
491
485
|
if (!verification || new Date() > verification.expiresAt) {
|
package/src/types.ts
CHANGED
|
@@ -36,7 +36,7 @@ export type Profile = z.infer<typeof profileSchema>;
|
|
|
36
36
|
|
|
37
37
|
export const NonceRequest = z.object({
|
|
38
38
|
accountId: accountIdSchema,
|
|
39
|
-
publicKey: z.string(),
|
|
39
|
+
publicKey: z.string().optional(),
|
|
40
40
|
networkId: z.union([z.literal("mainnet"), z.literal("testnet")])
|
|
41
41
|
});
|
|
42
42
|
export const VerifyRequest = z.object({
|