-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path[action].js
More file actions
614 lines (519 loc) · 22.3 KB
/
Copy path[action].js
File metadata and controls
614 lines (519 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Wallet endpoints — dispatched by req.query.action.
// GET /api/auth/wallets → handleListWallets (action undefined)
// POST /api/auth/wallets → handleLinkWallet (action undefined)
// GET /api/auth/wallets/check → handleCheck (action === 'check')
// POST /api/auth/wallets/nonce → handleNonce (action === 'nonce')
// POST /api/auth/wallets/nonce-solana → handleNonceSolana (action === 'nonce-solana')
// POST /api/auth/wallets/link-solana → handleLinkSolana (action === 'link-solana')
// DELETE /api/auth/wallets/<address> → handleUnlinkWallet (action === <address>)
//
// Dispatcher convention: undefined action → index (GET/POST), reserved literal
// actions handle nonce/link/check flows, and anything else is treated as an
// address for DELETE. _link-nonces.js remains a separate helper (underscore-
// prefixed, not file-routed by Vercel).
import { verifyMessage, getAddress } from 'ethers';
import { z } from 'zod';
import { sql } from '../../_lib/db.js';
import { getSessionUser } from '../../_lib/auth.js';
import { logAudit } from '../../_lib/audit.js';
import { cors, json, method, readJson, wrap, error, rateLimited } from '../../_lib/http.js';
import { limits, clientIp } from '../../_lib/rate-limit.js';
import { requireCsrf } from '../../_lib/csrf.js';
import { parse } from '../../_lib/validate.js';
import { parseSiweMessage } from '../../_lib/siwe.js';
import { parseSiwsMessage, verifySiwsSignature } from '../../_lib/siws.js';
import { env } from '../../_lib/env.js';
import { issueNonce, consumeNonce, NONCE_TTL_SEC } from './_link-nonces.js';
const SOLANA_ADDRESS_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
const ALLOWED_SOLANA_CHAINS = new Set(['mainnet', 'devnet', 'testnet']);
const linkBody = z.object({
message: z.string().min(64).max(4000),
signature: z.string().regex(/^0x[a-fA-F0-9]+$/),
});
const nonceBody = z.object({
address: z.string().regex(/^0x[a-fA-F0-9]{40}$/),
chainId: z.number().int().positive(),
});
export default wrap(async (req, res) => {
// action arrives via the route table (?action=$1) when a subpath was hit.
// Behind the Cloud Run rewrite the bare index path keeps its ORIGINAL
// pathname on req.url (see server/index.mjs), so the URL fallback must map
// the endpoint's own directory segment ("wallets") back to "no action";
// otherwise GET/POST /api/auth/wallets dispatches to the DELETE-only
// unlink branch and 405s.
let action = req.query?.action;
if (action === undefined) {
const last = new URL(req.url, 'http://x').pathname.split('/').filter(Boolean).pop();
action = last === 'wallets' ? undefined : last;
}
// /api/auth/wallets — list (GET) or link (POST)
if (action === undefined || action === '' || action === null) {
return handleIndex(req, res);
}
// /api/auth/wallets/check — cheap pre-check: is this address already linked to the session user?
if (action === 'check') {
return handleCheck(req, res);
}
// /api/auth/wallets/nonce — issue link nonce (EVM/SIWE)
if (action === 'nonce') {
return handleNonce(req, res);
}
// /api/auth/wallets/nonce-solana — issue SIWS link message
if (action === 'nonce-solana') {
return handleNonceSolana(req, res);
}
// /api/auth/wallets/link-solana — verify SIWS + insert Solana wallet row
if (action === 'link-solana') {
return handleLinkSolana(req, res);
}
// /api/auth/wallets/primary — mark one of the caller's wallets as primary
if (action === 'primary') {
return handleSetPrimary(req, res);
}
// /api/auth/wallets/<address> — unlink wallet (action carries the address)
return handleUnlinkWallet(req, res, action);
});
// ── Index: list (GET) and link (POST) ──────────────────────────────────────
// Link new wallets to authenticated user + list existing wallets.
async function handleIndex(req, res) {
if (cors(req, res, { methods: 'GET,POST,OPTIONS', credentials: true })) return;
const session = await getSessionUser(req);
if (!session) return error(res, 401, 'unauthorized', 'sign in required');
if (req.method === 'GET') {
return handleListWallets(session.id, res);
} else if (req.method === 'POST') {
return handleLinkWallet(session.id, req, res);
} else if (!method(req, res, ['GET', 'POST'])) return;
}
async function handleListWallets(userId, res) {
const rows = await sql`
select address, chain_id, chain_type, created_at, is_primary
from user_wallets
where user_id = ${userId}
order by created_at asc
`;
return json(res, 200, {
wallets: rows.map((w) => ({
address: w.address,
chain_id: w.chain_id,
chain_type: w.chain_type,
created_at: w.created_at,
is_primary: w.is_primary,
})),
});
}
async function handleLinkWallet(userId, req, res) {
const rl = await limits.walletLink(userId);
if (!rl.success) return rateLimited(res, rl);
const body = parse(linkBody, await readJson(req));
// 1. Parse SIWE message.
const fields = parseSiweMessage(body.message);
if (!fields) return error(res, 400, 'invalid_message', 'malformed SIWE message');
// 2. Domain + URI must match this deployment.
const appOrigin = env.APP_ORIGIN;
const appHost = new URL(appOrigin).host;
const vercelHost = process.env.VERCEL_URL || null;
const isLocalDev =
process.env.VERCEL_ENV !== 'production' && process.env.VERCEL_ENV !== 'preview';
const allowedHosts = new Set([appHost, vercelHost].filter(Boolean));
const domainOk =
allowedHosts.has(fields.domain) || (isLocalDev && /^localhost(:\d+)?$/.test(fields.domain));
if (!domainOk) return error(res, 400, 'invalid_domain', `domain must be ${appHost}`);
try {
const u = new URL(fields.uri);
const allowedOrigins = new Set(
[appOrigin, vercelHost ? `https://${vercelHost}` : null].filter(Boolean),
);
const originOk =
allowedOrigins.has(u.origin) ||
(isLocalDev && /^https?:\/\/localhost(:\d+)?$/.test(u.origin));
if (!originOk) return error(res, 400, 'invalid_uri', 'uri origin mismatch');
} catch {
return error(res, 400, 'invalid_uri', 'uri not a valid URL');
}
// 3. Temporal checks.
const now = Date.now();
if (fields.expirationTime && Date.parse(fields.expirationTime) < now) {
return error(res, 400, 'expired', 'message expired');
}
if (fields.notBefore && Date.parse(fields.notBefore) > now) {
return error(res, 400, 'not_yet_valid', 'message not yet valid');
}
// 4. Verify nonce was issued to this user and burn it.
const nonceData = await consumeNonce(fields.nonce, userId);
if (!nonceData) {
return error(res, 400, 'invalid_nonce', 'unknown, expired, or invalid nonce');
}
// 5. Verify signature recovers the address claimed in the message.
let recovered;
try {
recovered = verifyMessage(body.message, body.signature);
} catch {
return error(res, 401, 'invalid_signature', 'signature verification failed');
}
let claimed;
try {
claimed = getAddress(fields.address);
} catch {
return error(res, 400, 'invalid_address', 'address not checksummed correctly');
}
if (recovered.toLowerCase() !== claimed.toLowerCase()) {
return error(res, 401, 'invalid_signature', 'signer does not match address');
}
const addrLower = claimed.toLowerCase();
const chainId = fields.chainId || null;
// 6. Check if this address is already linked to this user (idempotent).
const existing = await sql`
select id from user_wallets
where user_id = ${userId} and address = ${addrLower}
`;
if (existing.length > 0) {
return json(res, 200, { wallet: { address: claimed, chain_id: chainId } });
}
// 7. Check if this address is already linked to a different user.
const conflict = await sql`
select user_id from user_wallets
where address = ${addrLower}
`;
if (conflict.length > 0) {
return error(
res,
409,
'address_in_use',
'this address is already linked to another account',
);
}
// 8. Insert the new wallet.
await sql`
insert into user_wallets (user_id, address, chain_id, is_primary)
values (${userId}, ${addrLower}, ${chainId}, false)
`;
logAudit({
userId,
action: 'link_wallet',
resourceId: addrLower,
meta: { chain_id: chainId },
req,
});
return json(res, 201, { wallet: { address: claimed, chain_id: chainId } });
}
// ── Check ──────────────────────────────────────────────────────────────────
// GET /api/auth/wallets/check?chain_type=<solana|evm>&address=<addr>
// Returns { linked: bool } — true iff the address is already linked to the
// session user with a matching chain_type. Used by the Solana deploy flow
// to skip the SIWS prompt when the wallet is already linked. Unauthenticated
// callers get { linked: false } (200) — the address simply isn't linked to
// "the current session" when there is no session.
const checkAddressRe = /^[A-Za-z0-9]{1,128}$/;
async function handleCheck(req, res) {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
const session = await getSessionUser(req);
if (!session) return json(res, 200, { linked: false });
const chainType = String(req.query?.chain_type || '').toLowerCase();
const address = String(req.query?.address || '');
if (!chainType || !address || !checkAddressRe.test(address)) {
return error(res, 400, 'invalid_request', 'chain_type and address required');
}
// EVM addresses normalize to lowercase; Solana base58 is case-sensitive.
const addrLookup = chainType === 'solana' ? address : address.toLowerCase();
const rows = await sql`
select 1 from user_wallets
where user_id = ${session.id}
and address = ${addrLookup}
and chain_type = ${chainType}
limit 1
`;
return json(res, 200, { linked: rows.length > 0 });
}
// ── Nonce ──────────────────────────────────────────────────────────────────
// Issue a nonce + EIP-4361 (SIWE) message for wallet linking.
// Caller must already be authenticated; the resulting message ties the wallet
// signature to the active session's user.
async function handleNonce(req, res) {
if (cors(req, res, { methods: 'GET,POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET', 'POST'])) return;
const session = await getSessionUser(req);
if (!session) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.walletLink(session.id);
if (!rl.success) return rateLimited(res, rl);
const appOrigin = env.APP_ORIGIN;
const domain = new URL(appOrigin).host;
const nonce = await issueNonce(session.id);
// GET: bare per-user link nonce for the connect-button component, which
// builds the SIWE message client-side. The security of linking rests on this
// nonce being bound to the current session (handleLinkWallet validates it via
// consumeNonce(nonce, session.id)) and living in a store separate from the
// login nonce pool — so a captured *login* signature can't be replayed here.
if (req.method === 'GET') {
return json(res, 200, { nonce, domain, uri: appOrigin, ttl: NONCE_TTL_SEC });
}
const { address, chainId } = parse(nonceBody, await readJson(req));
const issuedAt = new Date().toISOString();
const expirationTime = new Date(Date.now() + NONCE_TTL_SEC * 1000).toISOString();
const message = [
`${domain} wants you to sign in with your Ethereum account:`,
address,
``,
`Link this wallet to three.ws account ${session.email}`,
``,
`URI: ${appOrigin}`,
`Version: 1`,
`Chain ID: ${chainId}`,
`Nonce: ${nonce}`,
`Issued At: ${issuedAt}`,
`Expiration Time: ${expirationTime}`,
].join('\n');
return json(res, 200, { nonce, message, ttl: NONCE_TTL_SEC });
}
// ── Solana: nonce ──────────────────────────────────────────────────────────
// Issue a nonce + SIWS link message tying the wallet signature to the active
// session's user. Mirrors handleNonce for EVM but builds a Sign-In with
// Solana (CAIP-122 / SIP-0) message with a "Link this wallet" statement.
const nonceSolanaBody = z.object({
address: z.string().regex(SOLANA_ADDRESS_RE),
chainId: z.string().optional(),
});
async function handleNonceSolana(req, res) {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
const session = await getSessionUser(req);
if (!session) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.walletLink(session.id);
if (!rl.success) return rateLimited(res, rl);
const { address, chainId } = parse(nonceSolanaBody, await readJson(req));
const chain = chainId && ALLOWED_SOLANA_CHAINS.has(chainId) ? chainId : 'mainnet';
const nonce = await issueNonce(session.id);
const appOrigin = env.APP_ORIGIN;
const domain = new URL(appOrigin).host;
const issuedAt = new Date().toISOString();
const expirationTime = new Date(Date.now() + NONCE_TTL_SEC * 1000).toISOString();
const message = [
`${domain} wants you to sign in with your Solana account:`,
address,
``,
`Link this wallet to three.ws account ${session.email}`,
``,
`URI: ${appOrigin}`,
`Version: 1`,
`Chain ID: ${chain}`,
`Nonce: ${nonce}`,
`Issued At: ${issuedAt}`,
`Expiration Time: ${expirationTime}`,
].join('\n');
return json(res, 200, { nonce, message, ttl: NONCE_TTL_SEC });
}
// ── Solana: link ───────────────────────────────────────────────────────────
// Verify a SIWS message+signature signed by `address`, then insert (or no-op)
// a user_wallets row with chain_type='solana' for the session user.
const linkSolanaBody = z.object({
message: z.string().min(32).max(4000),
signature: z.string().min(1).max(256),
// When true and the wallet is already linked to a different account, the
// signature proves ownership of the keypair and the row is moved to the
// session user atomically. The default keeps the safe "409 first, confirm
// second" UX: the caller must opt in to a takeover.
takeover: z.boolean().optional(),
});
async function handleLinkSolana(req, res) {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
const session = await getSessionUser(req);
if (!session) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.walletLink(session.id);
if (!rl.success) return rateLimited(res, rl);
const body = parse(linkSolanaBody, await readJson(req));
const fields = parseSiwsMessage(body.message);
if (!fields) return error(res, 400, 'invalid_message', 'malformed SIWS message');
// Domain + URI must match this deployment.
const appOrigin = env.APP_ORIGIN;
const appHost = new URL(appOrigin).host;
const vercelHost = process.env.VERCEL_URL || null;
const isLocalDev =
process.env.VERCEL_ENV !== 'production' && process.env.VERCEL_ENV !== 'preview';
const allowedHosts = new Set([appHost, vercelHost].filter(Boolean));
const domainOk =
allowedHosts.has(fields.domain) || (isLocalDev && /^localhost(:\d+)?$/.test(fields.domain));
if (!domainOk) return error(res, 400, 'invalid_domain', `domain must be ${appHost}`);
try {
const u = new URL(fields.uri);
const allowedOrigins = new Set(
[appOrigin, vercelHost ? `https://${vercelHost}` : null].filter(Boolean),
);
const originOk =
allowedOrigins.has(u.origin) ||
(isLocalDev && /^https?:\/\/localhost(:\d+)?$/.test(u.origin));
if (!originOk) return error(res, 400, 'invalid_uri', 'uri origin mismatch');
} catch {
return error(res, 400, 'invalid_uri', 'uri not a valid URL');
}
if (fields.chainId && !ALLOWED_SOLANA_CHAINS.has(fields.chainId)) {
return error(res, 400, 'invalid_chain', 'unknown Solana chain ID');
}
const now = Date.now();
if (fields.expirationTime && Date.parse(fields.expirationTime) < now) {
return error(res, 400, 'expired', 'message expired');
}
if (fields.notBefore && Date.parse(fields.notBefore) > now) {
return error(res, 400, 'not_yet_valid', 'message not yet valid');
}
// Burn the per-user link nonce.
const nonceData = await consumeNonce(fields.nonce, session.id);
if (!nonceData) {
return error(res, 400, 'invalid_nonce', 'unknown, expired, or invalid nonce');
}
let valid;
try {
valid = verifySiwsSignature(body.message, body.signature, fields.address);
} catch {
return error(res, 401, 'invalid_signature', 'signature verification failed');
}
if (!valid) return error(res, 401, 'invalid_signature', 'signer does not match address');
const addr = fields.address;
const chain = fields.chainId || 'mainnet';
// Idempotent: already linked to this user → success.
const [existing] = await sql`
select id from user_wallets
where user_id = ${session.id} and address = ${addr}
limit 1
`;
if (existing) {
return json(res, 200, { wallet: { address: addr, chain_type: 'solana', chain_id: chain } });
}
// Conflict: linked to a different user.
const [conflict] = await sql`
select user_id from user_wallets where address = ${addr} limit 1
`;
if (conflict) {
// Signature already verified above — the caller controls the keypair.
// With explicit takeover consent, move the wallet row to the session
// user atomically. Without it, surface 409 so the UI can confirm.
if (!body.takeover) {
return error(
res,
409,
'address_in_use',
'this address is already linked to another account',
{ takeover_available: true },
);
}
await sql.transaction([
sql`delete from user_wallets where address = ${addr}`,
sql`insert into user_wallets (user_id, address, chain_type, is_primary)
values (${session.id}, ${addr}, 'solana', false)`,
]);
logAudit({
userId: conflict.user_id,
action: 'unlink_wallet_transferred',
resourceId: addr,
meta: { to_user_id: session.id },
req,
});
logAudit({
userId: session.id,
action: 'link_wallet_solana_takeover',
resourceId: addr,
meta: { from_user_id: conflict.user_id },
req,
});
return json(res, 200, {
wallet: { address: addr, chain_type: 'solana', chain_id: chain },
transferred: true,
});
}
await sql`
insert into user_wallets (user_id, address, chain_type, is_primary)
values (${session.id}, ${addr}, 'solana', false)
`;
logAudit({ userId: session.id, action: 'link_wallet_solana', resourceId: addr, req });
return json(res, 201, { wallet: { address: addr, chain_type: 'solana', chain_id: chain } });
}
// ── Set primary ────────────────────────────────────────────────────────────
// Mark one of the caller's wallets as primary; demote every other row in
// the same transaction so exactly one wallet is primary at a time.
//
// Body: { address }. EVM addresses are stored lowercased; Solana base58 is
// case-sensitive — we try the literal address first, then the lowercased
// form as a fallback for EVM callers who send a mixed-case checksum.
async function handleSetPrimary(req, res) {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
const session = await getSessionUser(req);
if (!session) return error(res, 401, 'unauthorized', 'sign in required');
if (!(await requireCsrf(req, res, session.id))) return;
const body = await readJson(req).catch(() => null);
const rawAddress = body?.address;
if (!rawAddress || typeof rawAddress !== 'string') {
return error(res, 400, 'validation_error', 'address required');
}
const candidates = [rawAddress, rawAddress.toLowerCase()].filter(
(v, i, a) => a.indexOf(v) === i,
);
const [wallet] = await sql`
select id, address, is_primary
from user_wallets
where user_id = ${session.id}
and address = any(${candidates}::text[])
limit 1
`;
if (!wallet) return error(res, 404, 'not_found', 'wallet not found');
if (!wallet.is_primary) {
await sql.transaction([
sql`update user_wallets set is_primary = false where user_id = ${session.id} and is_primary = true`,
sql`update user_wallets set is_primary = true where id = ${wallet.id}`,
]);
logAudit({
userId: session.id,
action: 'set_primary_wallet',
resourceId: wallet.address,
req,
});
}
return json(res, 200, { primary: { address: wallet.address } });
}
// ── Unlink ─────────────────────────────────────────────────────────────────
// Unlink a wallet from authenticated user.
// Refuse if it's the only wallet AND user has no email+password.
async function handleUnlinkWallet(req, res, address) {
if (cors(req, res, { methods: 'DELETE,OPTIONS', credentials: true })) return;
if (!method(req, res, ['DELETE'])) return;
const session = await getSessionUser(req);
if (!session) return error(res, 401, 'unauthorized', 'sign in required');
if (!(await requireCsrf(req, res, session.id))) return;
if (!address) return error(res, 400, 'missing_address', 'address required');
// EVM addresses are stored lowercased; Solana base58 is case-sensitive — try
// the literal address first, then the lowercased form (same approach as
// handleSetPrimary).
const candidates = [address, address.toLowerCase()].filter((v, i, a) => a.indexOf(v) === i);
// 1. Check that this wallet belongs to the user.
const [wallet] = await sql`
select id, address from user_wallets
where user_id = ${session.id} and address = any(${candidates}::text[])
limit 1
`;
if (!wallet) return error(res, 404, 'not_found', 'wallet not found');
// 2. Check if this is the only wallet.
const [count] = await sql`
select count(*) as n from user_wallets
where user_id = ${session.id}
`;
const walletCount = count.n;
if (walletCount === 1) {
// 3. If only wallet, check if user has password-based auth.
const [user] = await sql`
select password_hash from users where id = ${session.id} limit 1
`;
if (!user.password_hash) {
return error(
res,
400,
'cannot_remove_last_wallet',
'cannot remove the last wallet if account has no password',
);
}
}
// 4. Delete the wallet.
await sql`delete from user_wallets where id = ${wallet.id} and user_id = ${session.id}`;
logAudit({ userId: session.id, action: 'unlink_wallet', resourceId: wallet.address, req });
return json(res, 200, { removed: true });
}