Skip to content

Commit b64cc91

Browse files
committed
Updated verify-signatures middleware to handle testnet and mainnet
1 parent 0a117d6 commit b64cc91

File tree

9 files changed

+63
-56
lines changed

9 files changed

+63
-56
lines changed

docker-compose.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ services:
2626
interval: 30s
2727
timeout: 10s
2828
retries: 5
29-
# ports:
30-
# - '5672:5672'
29+
ports:
30+
- '5672:5672'
3131

3232
redis:
3333
container_name: redis
@@ -39,8 +39,8 @@ services:
3939
interval: 30s
4040
timeout: 10s
4141
retries: 5
42-
# ports:
43-
# - '6379:6379'
42+
ports:
43+
- '6379:6379'
4444

4545
volumes:
4646
relay-storage:

package-lock.json

+12-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
},
2525
"dependencies": {
2626
"@aws-sdk/client-s3": "^3.504.0",
27-
"@ltonetwork/http-message-signatures": "^0.1.11",
28-
"@ltonetwork/lto": "^0.15.16",
27+
"@ltonetwork/http-message-signatures": "^0.1.12",
28+
"@ltonetwork/lto": "^0.16.6",
2929
"@nestjs/axios": "^3.0.0",
3030
"@nestjs/common": "^9.0.0",
3131
"@nestjs/core": "^9.0.0",
3232
"@nestjs/platform-express": "^9.0.0",
3333
"@nestjs/swagger": "^6.3.0",
3434
"amqplib": "^0.10.3",
3535
"any-bucket": "^0.1.6",
36-
"axios": "^1.6.8",
36+
"axios": "^1.7.7",
3737
"boolean": "^3.2.0",
3838
"connection-string": "^4.3.6",
3939
"convict": "^6.2.4",

src/app.module.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,16 @@ import { DispatcherModule } from './dispatcher/dispatcher.module';
77
import { QueueModule } from './queue/queue.module';
88
import { InboxModule } from './inbox/inbox.module';
99
import { VerifySignatureMiddleware } from './common/http-signature/verify-signature.middleware';
10-
import { InboxController } from './inbox/inbox.controller';
1110

1211
export const AppModuleConfig = {
1312
imports: [ConfigModule, RabbitMQModule, QueueModule, DispatcherModule, InboxModule],
1413
controllers: [AppController],
1514
providers: [AppService],
1615
};
1716

18-
// @Module(AppModuleConfig)
19-
// export class AppModule {
20-
// configure(consumer: MiddlewareConsumer) {
21-
// consumer.apply(VerifySignatureMiddleware).forRoutes(InboxController);
22-
// }
23-
// }
24-
2517
@Module(AppModuleConfig)
2618
export class AppModule {
2719
configure(consumer: MiddlewareConsumer) {
28-
consumer
29-
.apply(VerifySignatureMiddleware)
30-
.forRoutes({ path: 'inboxes/:address/:hash', method: RequestMethod.DELETE });
20+
consumer.apply(VerifySignatureMiddleware).forRoutes({ path: 'inboxes/*', method: RequestMethod.ALL });
3121
}
3222
}

src/common/config/config.service.ts

-4
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ export class ConfigService {
169169
return this.config.get(`lto.${this.networkName(network)}.node`);
170170
}
171171

172-
getNetworkId(): string {
173-
return this.config.get('lto.networkId');
174-
}
175-
176172
getDidResolver(network: 'mainnet' | 'testnet' | 'L' | 'T'): string {
177173
return this.config.get(`lto.${this.networkName(network)}.did_resolver`);
178174
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { Injectable, NestMiddleware } from '@nestjs/common';
22
import { NextFunction, Request, Response } from 'express';
3-
import { LTO } from '@ltonetwork/lto';
3+
import { LTO, getNetwork } from '@ltonetwork/lto';
44
import { verify } from '@ltonetwork/http-message-signatures';
5-
import { ConfigService } from '../config/config.service';
65

6+
export const lto = new LTO();
77
@Injectable()
88
export class VerifySignatureMiddleware implements NestMiddleware {
9-
private readonly lto: LTO;
9+
private lto: LTO;
1010

11-
constructor(private readonly config: ConfigService) {
12-
const networkID = this.config.getNetworkId();
13-
this.lto = new LTO(networkID);
11+
constructor() {
12+
this.lto = new LTO();
1413
}
1514

16-
async verify(req: Request, res: Response): Promise<boolean> {
15+
async verifyRequest(req: Request, res: Response): Promise<boolean> {
1716
try {
18-
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
19-
req.url = fullUrl;
17+
const path = req.path;
18+
const walletAddress = path.match(/3[^\/]*/)?.[0];
19+
const network = getNetwork(walletAddress);
20+
21+
//switch to testnet if address is testnet
22+
if (network == 'T') this.lto = new LTO(network);
23+
2024
const account = await verify(req, this.lto);
2125
req['signer'] = account;
2226
} catch (err) {
@@ -27,7 +31,7 @@ export class VerifySignatureMiddleware implements NestMiddleware {
2731
}
2832

2933
async use(req: Request, res: Response, next: NextFunction): Promise<void> {
30-
if ('signature' in req.headers && !(await this.verify(req, res))) return;
34+
if ('signature' in req.headers && !(await this.verifyRequest(req, res))) return;
3135
next();
3236
}
3337
}

src/config/schema.ts

-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ export default {
126126
},
127127
},
128128
lto: {
129-
networkId: {
130-
default: 'L',
131-
env: 'NETWORK_ID',
132-
},
133129
testnet: {
134130
node: {
135131
doc: 'LTO testnet node url',

src/inbox/inbox.controller.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,28 @@ export class InboxController {
2727
@ApiParam({ name: 'address', description: 'Address to get inbox for' })
2828
@ApiQuery({ name: 'type', description: 'Type of messages to get', required: false })
2929
@ApiProduces('application/json')
30-
async list(@Param('address') address: string, @Query('type') type?: string): Promise<MessageSummery[]> {
30+
async list(
31+
@Signer() signer: Account,
32+
@Param('address') address: string,
33+
@Query('type') type?: string,
34+
): Promise<MessageSummery[]> {
35+
if (signer.address !== address) {
36+
throw new ForbiddenException({ message: 'Unauthorized: Invalid signature for this address' });
37+
}
3138
return this.inbox.list(address, type);
3239
}
3340

3441
@Get('/:address/:hash')
3542
@ApiProduces('application/json')
36-
async get(@Param('address') address: string, @Param('hash') hash: string): Promise<Message> {
43+
async get(
44+
@Param('address') address: string,
45+
@Param('hash') hash: string,
46+
@Signer() signer: Account,
47+
): Promise<Message> {
48+
if (signer.address !== address) {
49+
throw new ForbiddenException({ message: 'Unauthorized: Invalid signature for this address' });
50+
}
51+
3752
if (!(await this.inbox.has(address, hash))) {
3853
throw new NotFoundException({ message: 'Message not found' });
3954
}

yarn.lock

+12-8
Original file line numberDiff line numberDiff line change
@@ -1234,13 +1234,15 @@
12341234
"@jridgewell/resolve-uri" "^3.0.3"
12351235
"@jridgewell/sourcemap-codec" "^1.4.10"
12361236

1237-
"@ltonetwork/http-message-signatures@^0.1.11":
1238-
version "0.1.11"
1239-
resolved "https://registry.npmjs.org/@ltonetwork/http-message-signatures/-/http-message-signatures-0.1.11.tgz"
1240-
integrity sha512-8pFdYvR1oL5W1tQwYhryvOImE89dwRAdalnYvM4L7LrN/SYYI6ub/oLDtgpsqwZXtoX5SOP8bh2l79Un06BMVQ==
1237+
"@ltonetwork/http-message-signatures@^0.1.12":
1238+
version "0.1.12"
1239+
resolved "https://registry.npmjs.org/@ltonetwork/http-message-signatures/-/http-message-signatures-0.1.12.tgz"
1240+
integrity sha512-bk7BemlROclcdXu5ISTKjPeRstJoqWefh6Bvr1q6XaX+KyKgLuKeCSYOKRyBBJ8P/E+tq9toBUIlMRLvW5J9KQ==
12411241

1242-
"@ltonetwork/lto@^0.15.16":
1243-
version "0.15.16"
1242+
"@ltonetwork/lto@^0.16.6":
1243+
version "0.16.6"
1244+
resolved "https://registry.npmjs.org/@ltonetwork/lto/-/lto-0.16.6.tgz"
1245+
integrity sha512-TFCgPdN5UPUpXz1OKdM66WBnzXxQdwpvWczp0dvaFhJgZ2WpeXIMVIcwZRJtyApEJD7xhW5SOatDlfWs6YMTfQ==
12441246
dependencies:
12451247
"@noble/curves" "^1.0.0"
12461248
"@noble/hashes" "^1.3.0"
@@ -2280,8 +2282,10 @@ async@^3.2.3:
22802282
asynckit@^0.4.0:
22812283
version "0.4.0"
22822284

2283-
axios@^1.3.1, axios@^1.6.8:
2284-
version "1.7.2"
2285+
axios@^1.3.1, axios@^1.7.7:
2286+
version "1.7.7"
2287+
resolved "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz"
2288+
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
22852289
dependencies:
22862290
follow-redirects "^1.15.6"
22872291
form-data "^4.0.0"

0 commit comments

Comments
 (0)