Merge pull request #5325 from mempool/mononaut/subnet-route-restrictions

Restrict accelerator routes to mainnet
This commit is contained in:
wiz 2024-07-12 18:59:21 +09:00 committed by GitHub
commit 1b6fd29c82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 4 deletions

View File

@ -50,7 +50,7 @@ const routes: Routes = [
}, },
{ {
path: 'acceleration', path: 'acceleration',
data: { networks: ['bitcoin'] }, data: { networks: ['bitcoin'], networkSpecific: true, onlySubnet: [''] },
component: StartComponent, component: StartComponent,
children: [ children: [
{ {
@ -61,7 +61,7 @@ const routes: Routes = [
}, },
{ {
path: 'acceleration/list/:page', path: 'acceleration/list/:page',
data: { networks: ['bitcoin'] }, data: { networks: ['bitcoin'], networkSpecific: true, onlySubnet: [''] },
component: AccelerationsListComponent, component: AccelerationsListComponent,
}, },
{ {
@ -140,7 +140,7 @@ const routes: Routes = [
}, },
{ {
path: 'acceleration/fees', path: 'acceleration/fees',
data: { networks: ['bitcoin'] }, data: { networks: ['bitcoin'], networkSpecific: true, onlySubnet: [''] },
component: AccelerationFeesGraphComponent, component: AccelerationFeesGraphComponent,
}, },
{ {

View File

@ -3,6 +3,7 @@ import { Router, NavigationEnd, ActivatedRouteSnapshot } from '@angular/router';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
import { filter, map } from 'rxjs/operators'; import { filter, map } from 'rxjs/operators';
import { StateService } from './state.service'; import { StateService } from './state.service';
import { RelativeUrlPipe } from '../shared/pipes/relative-url/relative-url.pipe';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -30,15 +31,30 @@ export class NavigationService {
constructor( constructor(
private stateService: StateService, private stateService: StateService,
private router: Router, private router: Router,
private relativeUrlPipe: RelativeUrlPipe,
) { ) {
this.router.events.pipe( this.router.events.pipe(
filter(event => event instanceof NavigationEnd), filter(event => event instanceof NavigationEnd),
map(() => this.router.routerState.snapshot.root), map(() => this.router.routerState.snapshot.root),
).subscribe((state) => { ).subscribe((state) => {
this.updateSubnetPaths(state); if (this.enforceSubnetRestrictions(state)) {
this.updateSubnetPaths(state);
}
}); });
} }
enforceSubnetRestrictions(root: ActivatedRouteSnapshot): boolean {
let route = root;
while (route) {
if (route.data.onlySubnet && !route.data.onlySubnet.includes(this.stateService.network)) {
this.router.navigate([this.relativeUrlPipe.transform('')]);
return false;
}
route = route.firstChild;
}
return true;
}
// For each network (bitcoin/liquid), find and save the longest url path compatible with the current route // For each network (bitcoin/liquid), find and save the longest url path compatible with the current route
updateSubnetPaths(root: ActivatedRouteSnapshot): void { updateSubnetPaths(root: ActivatedRouteSnapshot): void {
let path = ''; let path = '';