Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BNS domains that resolve to addresses now searchable #57

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,8 @@
}
}
},
"defaultProject": "yellow-spyglass-client"
"defaultProject": "yellow-spyglass-client",
"cli": {
"analytics": false
}
}
8 changes: 6 additions & 2 deletions src/app/navigation/search-bar/search-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export let APP_SEARCH_BAR_ID = 0;
export class SearchBarComponent implements OnInit, AfterViewInit, AfterViewChecked {
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;

@Input() placeholder: string = 'Search by Address, Block or Alias';
@Input() placeholder: string = 'Search by Address, Block, BNS, or Alias';
@Input() toolbarTitle: string;

/** This input is used to turn off the autofocus logic. Home page search does not need autofocus, but app-bar search does. */
Expand Down Expand Up @@ -127,7 +127,11 @@ export class SearchBarComponent implements OnInit, AfterViewInit, AfterViewCheck
return this.invalidSearch.emit();
}

if (this._searchService.isValidAddress(value) || this._searchService.isValidBlock(value)) {
if (
this._searchService.isValidAddress(value) ||
this._searchService.isValidBlock(value) ||
this._searchService.isValidBNSDomain(value)
) {
return this._emitSearch(value, controlKey);
}

Expand Down
23 changes: 19 additions & 4 deletions src/app/pages/account/account.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { OnlineRepsService } from '@app/services/online-reps/online-reps.service
import { NavigationEnd, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { AliasService } from '@app/services/alias/alias.service';
import { APP_NAV_ITEMS, hashNavItem } from '../../navigation/nav-items';
import { APP_NAV_ITEMS, accountNavItem, hashNavItem } from '../../navigation/nav-items';
import { environment } from '../../../environments/environment';
import { DelegatorsTabService } from '@app/pages/account/tabs/delegators/delegators-tab.service';
import { InsightsTabService } from '@app/pages/account/tabs/insights/insights-tab.service';
Expand Down Expand Up @@ -64,12 +64,12 @@ export class AccountComponent implements OnDestroy {
private readonly _insightsTabService: InsightsTabService,
private readonly _delegatorsTabService: DelegatorsTabService
) {
this.routeListener = this._router.events.subscribe((route) => {
this.routeListener = this._router.events.subscribe(async (route) => {
if (route instanceof NavigationEnd) {
const splitUrl = this._router.url.replace('/history', '').split('/');
const path = splitUrl[splitUrl.length - 1];
const address = path.substring(0, 64);
this._searchAccount(address);
await this._searchAccount(address);
}
});

Expand Down Expand Up @@ -111,13 +111,28 @@ export class AccountComponent implements OnDestroy {
void this._router.navigate([`/${hashNavItem.route}/${hash}`], { replaceUrl: true });
}

/** Call this method whenever someone has enters a BNS domain, and it is resolved to a Banano address. */
private _redirectToAccountPage(account: string): void {
void this._router.navigate([`/${accountNavItem.route}/${account}`], { replaceUrl: true });
}

/** Given a ban address, searches for account. */
private _searchAccount(address): void {
private async _searchAccount(address): Promise<void> {
if (!address) {
return;
}

if (!address.startsWith('ban_')) {
const parts = address.split('.');
if (parts.length === 2) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use isValidBNSDomain utility function here?

//search in api
try {
const domain = await this.apiService.fetchBNSDomain(parts[0], parts[1]);
if (domain.domain?.resolved_address) {
return this._redirectToAccountPage(domain.domain?.resolved_address);
}
} catch (_) {}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get a quick comment here explaining what this does? If not a ban address, assume it's BNS?

this._redirectToHashPage(address);
}

Expand Down
11 changes: 11 additions & 0 deletions src/app/services/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ export class ApiService {
.toPromise();
}

/** Fetch/query BNS domain. */
async fetchBNSDomain(domain_name: string, tld: string): Promise<any> {
await this._hasPingedApi();
return this._http
.post<any>(`${this.httpApi}/v1/account/bns`, {
Copy link
Owner

@dev-ptera dev-ptera Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get some stronger typings here? The api is versioned so we should know what schema to expect.

Specifically looking to remove the any here.

domain_name,
tld,
})
.toPromise();
}

/** Fetches monitored representatives stats. */
async fetchMonitoredRepresentatives(): Promise<MonitoredRepDto[]> {
await this._hasPingedApi();
Expand Down
10 changes: 8 additions & 2 deletions src/app/services/search/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export class SearchService {
constructor(router: Router) {
this.searchEvents().subscribe((data: { search: string; openInNewWindow: boolean }) => {
if (data.openInNewWindow) {
if (data.search.startsWith('ban_')) {
if (data.search.startsWith('ban_') || this.isValidBNSDomain(data.search)) {
const origin = window.location.origin;
window.open(`${origin}/${APP_NAV_ITEMS.account.route}/${data.search}`, '_blank');
} else {
window.open(`${origin}/${APP_NAV_ITEMS.hash.route}/${data.search}`, '_blank');
}
} else {
if (data.search.startsWith('ban_')) {
if (data.search.startsWith('ban_') || this.isValidBNSDomain(data.search)) {
void router.navigate([`${APP_NAV_ITEMS.account.route}/${data.search}`]);
} else {
void router.navigate([`${APP_NAV_ITEMS.hash.route}/${data.search}`]);
Expand Down Expand Up @@ -47,4 +47,10 @@ export class SearchService {
isValidBlock(block: string): boolean {
return block && block.length === 64;
}

isValidBNSDomain(bns: string): boolean {
const parts = bns.split('.');
//later, can also check for illegal characters once that is more settled
return parts.length === 2 && parts[0].length <= 32;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be placed in the UtilService & then used here & in the AccountComponent?

}