Skip to content

Commit ca14ea1

Browse files
author
Brite
committed
fix error coment
1 parent a4a01a7 commit ca14ea1

4 files changed

Lines changed: 67 additions & 20 deletions

File tree

.github/workflows/contract-ci.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ name: Contract CI
22

33
on:
44
push:
5-
branches: [ main, develop ]
5+
branches: [main, develop]
66
paths:
7-
- 'apps/onchain/**'
8-
- '.github/workflows/contract-ci.yml'
7+
- "apps/onchain/**"
8+
- ".github/workflows/contract-ci.yml"
99
pull_request:
10-
branches: [ main, develop ]
10+
branches: [main, develop]
1111
paths:
12-
- 'apps/onchain/**'
13-
- '.github/workflows/contract-ci.yml'
12+
- "apps/onchain/**"
13+
- ".github/workflows/contract-ci.yml"
1414

1515
jobs:
1616
test:
@@ -28,8 +28,12 @@ jobs:
2828
uses: dtolnay/rust-toolchain@stable
2929
with:
3030
toolchain: stable
31+
components: rustfmt, clippy
3132
targets: wasm32-unknown-unknown
3233

34+
- name: Add wasm target
35+
run: rustup target add wasm32-unknown-unknown
36+
3337
- name: Cache dependencies
3438
uses: Swatinem/rust-cache@v2
3539
with:

apps/backend/src/gateways/escrow.gateway.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,27 @@ interface EscrowEventData {
1313
[key: string]: unknown;
1414
}
1515

16+
interface JwtPayload {
17+
sub?: string;
18+
userId?: string;
19+
id?: string;
20+
}
21+
22+
interface ClientAuthPayload {
23+
token?: string;
24+
}
25+
1626
@WebSocketGateway({
1727
namespace: '/events',
1828
cors: {
19-
origin: process.env.FRONTEND_URL?.split(',') || ['http://localhost:3001'],
29+
origin: (typeof process !== 'undefined' &&
30+
process.env.FRONTEND_URL?.split(',')) || ['http://localhost:3001'],
2031
credentials: true,
2132
},
2233
})
2334
export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
2435
@WebSocketServer()
25-
server: Server;
36+
server!: Server;
2637

2738
private readonly logger = new Logger(EventsGateway.name);
2839
private readonly userSocketMap = new Map<string, Set<string>>();
@@ -31,11 +42,23 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
3142

3243
constructor(private readonly jwtService: JwtService) {}
3344

45+
private extractToken(client: Socket): string | undefined {
46+
const authPayload = client.handshake.auth as ClientAuthPayload | undefined;
47+
const authorizationHeader = client.handshake.headers.authorization as
48+
| string
49+
| undefined;
50+
51+
if (authPayload?.token) {
52+
return authPayload.token;
53+
}
54+
55+
const parts = authorizationHeader?.split(' ') ?? [];
56+
return parts[1];
57+
}
58+
3459
async handleConnection(client: Socket): Promise<void> {
3560
try {
36-
const token =
37-
(client.handshake.auth?.token as string) ||
38-
(client.handshake.headers.authorization as string)?.split(' ')[1];
61+
const token = this.extractToken(client);
3962

4063
if (!token) {
4164
this.logger.warn(
@@ -45,12 +68,27 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
4568
return;
4669
}
4770

48-
const decoded = this.jwtService.verify(token) as {
49-
sub?: string;
50-
userId?: string;
51-
id?: string;
52-
};
53-
const userId = decoded?.sub || decoded?.userId || decoded?.id;
71+
let decoded: unknown;
72+
try {
73+
decoded = this.jwtService.verify(token);
74+
} catch {
75+
this.logger.warn(`Connection rejected: invalid token (${client.id})`);
76+
client.disconnect();
77+
return;
78+
}
79+
80+
if (
81+
!decoded ||
82+
typeof decoded !== 'object' ||
83+
!('sub' in decoded || 'userId' in decoded || 'id' in decoded)
84+
) {
85+
this.logger.warn(`Connection rejected: invalid token (${client.id})`);
86+
client.disconnect();
87+
return;
88+
}
89+
90+
const payload = decoded as JwtPayload;
91+
const userId = payload.sub || payload.userId || payload.id;
5492

5593
if (!userId) {
5694
this.logger.warn(`Connection rejected: invalid token (${client.id})`);
@@ -201,6 +239,10 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
201239
return (this.userSocketMap.get(userId)?.size || 0) > 0;
202240
}
203241

242+
isHealthy(): boolean {
243+
return this.server !== undefined;
244+
}
245+
204246
private emitToEscrowRoom(
205247
eventName: string,
206248
escrowId: string,

apps/backend/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"noImplicitAny": false,
2121
"strictBindCallApply": false,
2222
"noFallthroughCasesInSwitch": false,
23+
"types": ["node"],
2324
"lib": ["es2021"]
2425
}
2526
}

apps/onchain/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ pub enum Error {
399399
const DEFAULT_FEE_BPS: i128 = 50;
400400
const BPS_DENOMINATOR: i128 = 10000;
401401
const FEE_TIERS: &[(i128, i128)] = &[
402-
(1000, 50), // 0-1,000 XLM => 50 bps
403-
(5000, 30), // 1,001-5,000 XLM => 30 bps
404-
(10000, 20), // 5,001-10,000 XLM => 20 bps
402+
(1000, 50), // 0-1,000 XLM => 50 bps
403+
(5000, 30), // 1,001-5,000 XLM => 30 bps
404+
(10000, 20), // 5,001-10,000 XLM => 20 bps
405405
(i128::MAX, 10), // 10,001+ XLM => 10 bps
406406
];
407407
const MAX_BATCH_SIZE: u32 = 20;

0 commit comments

Comments
 (0)