forked from MindBlockLabs/mindBlock_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonce.service.ts
More file actions
70 lines (59 loc) · 1.9 KB
/
Copy pathnonce.service.ts
File metadata and controls
70 lines (59 loc) · 1.9 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
import { Injectable, BadRequestException } from '@nestjs/common';
@Injectable()
export class NonceService {
private nonces = new Map<
string,
{ walletAddress: string; expiresAt: number; used: boolean }
>();
public storeNonce(nonce: string, walletAddress: string, expiresAt: number): void {
this.nonces.set(nonce, {
walletAddress,
expiresAt,
used: false,
});
console.log(`[NonceService] Stored nonce: ${nonce}. Total: ${this.nonces.size}`);
}
public getNonce(nonce: string) {
return this.nonces.get(nonce);
}
public markAsUsed(nonce: string): void {
const data = this.nonces.get(nonce);
if (data) {
data.used = true;
this.nonces.set(nonce, data);
}
}
public verifyAndUseNonce(nonce: string, walletAddress: string): void {
console.log(`[NonceService] Verifying nonce: ${nonce} for wallet ${walletAddress}`);
const nonceData = this.nonces.get(nonce);
if (!nonceData) {
const known = Array.from(this.nonces.keys()).join(', ');
console.error(`[NonceService] Nonce NOT FOUND: ${nonce}. Map size: ${this.nonces.size}. Known nonces: ${known}`);
throw new BadRequestException('Invalid nonce');
}
if (nonceData.used) {
throw new BadRequestException('Nonce already used');
}
if (Date.now() > nonceData.expiresAt) {
throw new BadRequestException('Nonce expired');
}
if (nonceData.walletAddress !== walletAddress) {
throw new BadRequestException('Nonce wallet address mismatch');
}
// Mark as used
this.markAsUsed(nonce);
}
public cleanupExpiredNonces(): void {
const now = Date.now();
let count = 0;
for (const [nonce, data] of this.nonces.entries()) {
if (now > data.expiresAt) {
this.nonces.delete(nonce);
count++;
}
}
if (count > 0) {
console.log(`[NonceService] Cleaned up ${count} expired nonces.`);
}
}
}