From 1339b98281b235b6e4a5a15db041df835b37a686 Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 27 Aug 2022 16:35:20 +0900 Subject: [PATCH 01/14] Feature: Readable RegExp constructor --- frontend/src/app/shared/common.utils.ts | 69 ++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/frontend/src/app/shared/common.utils.ts b/frontend/src/app/shared/common.utils.ts index 7d206f4b5..6cbe35386 100644 --- a/frontend/src/app/shared/common.utils.ts +++ b/frontend/src/app/shared/common.utils.ts @@ -119,6 +119,7 @@ export function convertRegion(input, to: 'name' | 'abbreviated'): string { } } + export function haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number): number { const rlat1 = lat1 * Math.PI / 180; const rlon1 = lon1 * Math.PI / 180; @@ -135,4 +136,70 @@ export function haversineDistance(lat1: number, lon1: number, lat2: number, lon2 export function kmToMiles(km: number): number { return km * 0.62137119; -} \ No newline at end of file +} + +// all base58 characters +const BASE58_CHARS = '[a-km-zA-HJ-NP-Z1-9]'; +// all bech32 characters (after the separator) +const BECH32_CHARS = '[ac-hj-np-z02-9]'; +// All characters usable in bech32 human readable portion (before the 1 separator) +// Note: Technically the spec says "all US ASCII characters" but in practice only alphabet is used. +// Note: If HRP contains the separator (1) then the separator is "the last instance of separator" +const BECH32_HRP_CHARS = '[a-zA-Z0-9]'; +// Hex characters +const HEX_CHARS = '[a-fA-F0-9]'; +// A regex to say "A single 0 OR any number with no leading zeroes" +// (?: // Start a non-capturing group +// 0 // A single 0 +// | // OR +// [1-9][0-9]* // Any succession of numbers starting with 1-9 +// ) // End the non-capturing group. +const ZERO_INDEX_NUMBER_CHARS = '(?:0|[1-9][0-9]*)'; +export type RegexType = 'address' | 'blockhash' | 'transaction' | 'blockheight'; +export type Network = 'testnet' | 'signet' | 'liquid' | 'bisq' | 'mainnet'; +export function getRegex(type: RegexType, network: Network): RegExp { + let regex = '^'; // ^ = Start of string + switch (type) { + // Match a block height number + // [Testing Order]: any order is fine + case 'blockheight': + regex += ZERO_INDEX_NUMBER_CHARS; // block height is a 0 indexed number + break; + // Match a 32 byte block hash in hex. Assumes at least 32 bits of difficulty. + // [Testing Order]: Must always be tested before 'transaction' + case 'blockhash': + regex += '0{8}'; // Starts with exactly 8 zeroes in a row + regex += `${HEX_CHARS}{56}`; // Continues with exactly 56 hex letters/numbers + break; + // Match a 32 byte tx hash in hex. Contains optional output index specifier. + // [Testing Order]: Must always be tested after 'blockhash' + case 'transaction': + regex += `${HEX_CHARS}{64}`; // Exactly 64 hex letters/numbers + regex += '(?:'; // Start a non-capturing group + regex += ':'; // 1 instances of the symbol ":" + regex += ZERO_INDEX_NUMBER_CHARS; // A zero indexed number + regex += ')?'; // End the non-capturing group. This group appears 0 or 1 times + break; + case 'address': + // TODO + switch (network) { + case 'mainnet': + break; + case 'testnet': + break; + case 'signet': + break; + case 'liquid': + break; + case 'bisq': + break; + default: + throw new Error('Invalid Network (Unreachable error in TypeScript)'); + } + break; + default: + throw new Error('Invalid RegexType (Unreachable error in TypeScript)'); + } + regex += '$'; // $ = End of string + return new RegExp(regex); +} From c0d3f295eec2f483e8006b6f06de6fd8b3650a5b Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 28 Aug 2022 00:07:13 +0900 Subject: [PATCH 02/14] Finished Regex portion --- .../search-form/search-form.component.ts | 25 ++- frontend/src/app/shared/common.utils.ts | 191 ++++++++++++++---- 2 files changed, 178 insertions(+), 38 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index ab42fe1f7..8031195f0 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -9,6 +9,7 @@ import { ElectrsApiService } from '../../services/electrs-api.service'; import { RelativeUrlPipe } from '../../shared/pipes/relative-url/relative-url.pipe'; import { ApiService } from '../../services/api.service'; import { SearchResultsComponent } from './search-results/search-results.component'; +import { ADDRESS_REGEXES, getRegex } from '../../shared/common.utils'; @Component({ selector: 'app-search-form', @@ -38,6 +39,7 @@ export class SearchFormComponent implements OnInit { regexBlockhash = /^[0]{8}[a-fA-F0-9]{56}$/; regexTransaction = /^([a-fA-F0-9]{64})(:\d+)?$/; regexBlockheight = /^[0-9]{1,9}$/; + focus$ = new Subject(); click$ = new Subject(); @@ -58,8 +60,13 @@ export class SearchFormComponent implements OnInit { private elementRef: ElementRef, ) { } - ngOnInit(): void { - this.stateService.networkChanged$.subscribe((network) => this.network = network); + + ngOnInit() { + this.stateService.networkChanged$.subscribe((network) => { + this.network = network; + // TODO: Eventually change network type here from string to enum of consts + this.regexAddress = getRegex('address', network as any); + }); this.searchForm = this.formBuilder.group({ searchText: ['', Validators.required], @@ -203,6 +210,20 @@ export class SearchFormComponent implements OnInit { this.isSearching = true; if (!this.regexTransaction.test(searchText) && this.regexAddress.test(searchText)) { this.navigate('/address/', searchText); + } else if ( + // If the search text matches any other network besides this one + ADDRESS_REGEXES + .filter(([, network]) => network !== this.network) + .some(([regex]) => regex.test(searchText)) + ) { + // Gather all network matches as string[] + const networks = ADDRESS_REGEXES.filter(([regex, network]) => + network !== this.network && + regex.test(searchText) + ).map(([, network]) => network); + // ############################################### + // TODO: Create the search items for the drop down + // ############################################### } else if (this.regexBlockhash.test(searchText) || this.regexBlockheight.test(searchText)) { this.navigate('/block/', searchText); } else if (this.regexTransaction.test(searchText)) { diff --git a/frontend/src/app/shared/common.utils.ts b/frontend/src/app/shared/common.utils.ts index 6cbe35386..bbc9143c0 100644 --- a/frontend/src/app/shared/common.utils.ts +++ b/frontend/src/app/shared/common.utils.ts @@ -139,67 +139,186 @@ export function kmToMiles(km: number): number { } // all base58 characters -const BASE58_CHARS = '[a-km-zA-HJ-NP-Z1-9]'; +const BASE58_CHARS = `[a-km-zA-HJ-NP-Z1-9]`; + // all bech32 characters (after the separator) -const BECH32_CHARS = '[ac-hj-np-z02-9]'; -// All characters usable in bech32 human readable portion (before the 1 separator) -// Note: Technically the spec says "all US ASCII characters" but in practice only alphabet is used. -// Note: If HRP contains the separator (1) then the separator is "the last instance of separator" -const BECH32_HRP_CHARS = '[a-zA-Z0-9]'; +const BECH32_CHARS_LW = `[ac-hj-np-z02-9]`; +const BECH32_CHARS_UP = `[AC-HJ-NP-Z02-9]`; + // Hex characters -const HEX_CHARS = '[a-fA-F0-9]'; +const HEX_CHARS = `[a-fA-F0-9]`; + // A regex to say "A single 0 OR any number with no leading zeroes" -// (?: // Start a non-capturing group -// 0 // A single 0 -// | // OR -// [1-9][0-9]* // Any succession of numbers starting with 1-9 -// ) // End the non-capturing group. -const ZERO_INDEX_NUMBER_CHARS = '(?:0|[1-9][0-9]*)'; -export type RegexType = 'address' | 'blockhash' | 'transaction' | 'blockheight'; -export type Network = 'testnet' | 'signet' | 'liquid' | 'bisq' | 'mainnet'; -export function getRegex(type: RegexType, network: Network): RegExp { - let regex = '^'; // ^ = Start of string +// (?: // Start a non-capturing group +// 0 // A single 0 +// | // OR +// [1-9]\d* // Any succession of numbers starting with 1-9 +// ) // End the non-capturing group. +const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9]\d*)`; + +// Formatting of the address regex is for readability, +// We should ignore formatting it with automated formatting tools like prettier. +// +// prettier-ignore +const ADDRESS_CHARS = { + mainnet: { + base58: `[13]` // Starts with a single 1 or 3 + + BASE58_CHARS + + `{26,33}`, // Repeat the previous char 26-33 times. + // Version byte 0x00 (P2PKH) can be as short as 27 characters, up to 34 length + // P2SH must be 34 length + bech32: `(?:` + + `bc1` // Starts with bc1 + + BECH32_CHARS_LW + + `{6,100}` // As per bech32, 6 char checksum is minimum + + `|` + + `BC1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + testnet: { + base58: `[mn2]` // Starts with a single m, n, or 2 (P2PKH is m or n, 2 is P2SH) + + BASE58_CHARS + + `{33,34}`, // m|n is 34 length, 2 is 35 length (We match the first letter separately) + bech32: `(?:` + + `tb1` // Starts with bc1 + + BECH32_CHARS_LW + + `{6,100}` // As per bech32, 6 char checksum is minimum + + `|` + + `TB1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + signet: { + base58: `[mn2]` + + BASE58_CHARS + + `{33,34}`, + bech32: `(?:` + + `tb1` // Starts with tb1 + + BECH32_CHARS_LW + + `{6,100}` + + `|` + + `TB1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + liquid: { + base58: `[GHPQ]` // G|H is P2PKH, P|Q is P2SH + + BASE58_CHARS + + `{33}`, // All min-max lengths are 34 + bech32: `(?:` + + `(?:` // bech32 liquid starts with ex or lq + + `ex` + + `|` + + `lq` + + `)` + + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. + + `{6,100}` + + `|` + + `(?:` // Same as above but all upper case + + `EX` + + `|` + + `LQ` + + `)` + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + bisq: { + base58: `B1` // bisq base58 addrs start with B1 + + BASE58_CHARS + + `{33}`, // always length 35 + bech32: `(?:` + + `bbc1` // Starts with bbc1 + + BECH32_CHARS_LW + + `{6,100}` + + `|` + + `BBC1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, +} +type RegexTypeNoAddr = `blockhash` | `transaction` | `blockheight`; +export type RegexType = `address` | RegexTypeNoAddr; + +export const NETWORKS = [`testnet`, `signet`, `liquid`, `bisq`, `mainnet`] as const; +export type Network = typeof NETWORKS[number]; // Turn const array into union type + +export const ADDRESS_REGEXES: [RegExp, string][] = NETWORKS + .map(network => [getRegex('address', network), network]) + +export function getRegex(type: RegexTypeNoAddr): RegExp; +export function getRegex(type: 'address', network: Network): RegExp; +export function getRegex(type: RegexType, network?: Network): RegExp { + let regex = `^`; // ^ = Start of string switch (type) { // Match a block height number // [Testing Order]: any order is fine - case 'blockheight': + case `blockheight`: regex += ZERO_INDEX_NUMBER_CHARS; // block height is a 0 indexed number break; // Match a 32 byte block hash in hex. Assumes at least 32 bits of difficulty. - // [Testing Order]: Must always be tested before 'transaction' - case 'blockhash': - regex += '0{8}'; // Starts with exactly 8 zeroes in a row + // [Testing Order]: Must always be tested before `transaction` + case `blockhash`: + regex += `0{8}`; // Starts with exactly 8 zeroes in a row regex += `${HEX_CHARS}{56}`; // Continues with exactly 56 hex letters/numbers break; // Match a 32 byte tx hash in hex. Contains optional output index specifier. - // [Testing Order]: Must always be tested after 'blockhash' - case 'transaction': + // [Testing Order]: Must always be tested after `blockhash` + case `transaction`: regex += `${HEX_CHARS}{64}`; // Exactly 64 hex letters/numbers - regex += '(?:'; // Start a non-capturing group - regex += ':'; // 1 instances of the symbol ":" + regex += `(?:`; // Start a non-capturing group + regex += `:`; // 1 instances of the symbol ":" regex += ZERO_INDEX_NUMBER_CHARS; // A zero indexed number - regex += ')?'; // End the non-capturing group. This group appears 0 or 1 times + regex += `)?`; // End the non-capturing group. This group appears 0 or 1 times break; - case 'address': - // TODO + // Match any one of the many address types + // [Testing Order]: While possible that a bech32 address happens to be 64 hex + // characters in the future (current lengths are not 64), it is highly unlikely + // Order therefore, does not matter. + case `address`: + if (!network) { + throw new Error(`Must pass network when type is address`); + } + regex += `(?:`; // Start a non-capturing group (each network has multiple options) switch (network) { - case 'mainnet': + case `mainnet`: + regex += ADDRESS_CHARS.mainnet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.mainnet.bech32; break; - case 'testnet': + case `testnet`: + regex += ADDRESS_CHARS.testnet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.testnet.bech32; break; - case 'signet': + case `signet`: + regex += ADDRESS_CHARS.signet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.signet.bech32; break; - case 'liquid': + case `liquid`: + regex += ADDRESS_CHARS.liquid.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.liquid.bech32; break; - case 'bisq': + case `bisq`: + regex += ADDRESS_CHARS.bisq.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.bisq.bech32; break; default: - throw new Error('Invalid Network (Unreachable error in TypeScript)'); + throw new Error(`Invalid Network ${network} (Unreachable error in TypeScript)`); } + regex += `)`; // End the non-capturing group break; default: - throw new Error('Invalid RegexType (Unreachable error in TypeScript)'); + throw new Error(`Invalid RegexType ${type} (Unreachable error in TypeScript)`); } - regex += '$'; // $ = End of string + regex += `$`; // $ = End of string return new RegExp(regex); } From 0a51b752e62d82b6986c8177cc63a48991383fcb Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 28 Aug 2022 16:07:46 +0900 Subject: [PATCH 03/14] Improve types and add liquidtestnet for regex --- frontend/src/app/shared/common.utils.ts | 38 +++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/shared/common.utils.ts b/frontend/src/app/shared/common.utils.ts index bbc9143c0..288fc0362 100644 --- a/frontend/src/app/shared/common.utils.ts +++ b/frontend/src/app/shared/common.utils.ts @@ -160,7 +160,12 @@ const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9]\d*)`; // We should ignore formatting it with automated formatting tools like prettier. // // prettier-ignore -const ADDRESS_CHARS = { +const ADDRESS_CHARS: { + [k in Network]: { + base58: string; + bech32: string; + }; +} = { mainnet: { base58: `[13]` // Starts with a single 1 or 3 + BASE58_CHARS @@ -227,6 +232,28 @@ const ADDRESS_CHARS = { + `{6,100}` + `)`, }, + liquidtestnet: { + base58: `[89]` // ???(TODO: find version) is P2PKH, 8|9 is P2SH + + BASE58_CHARS + + `{33}`, // P2PKH is ???(TODO: find size), P2SH is 34 + bech32: `(?:` + + `(?:` // bech32 liquid testnet starts with tex or tlq + + `tex` // TODO: Why does mempool use this and not ert|el like in the elements source? + + `|` + + `tlq` // TODO: does this exist? + + `)` + + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. + + `{6,100}` + + `|` + + `(?:` // Same as above but all upper case + + `TEX` + + `|` + + `TLQ` + + `)` + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, bisq: { base58: `B1` // bisq base58 addrs start with B1 + BASE58_CHARS @@ -245,10 +272,10 @@ const ADDRESS_CHARS = { type RegexTypeNoAddr = `blockhash` | `transaction` | `blockheight`; export type RegexType = `address` | RegexTypeNoAddr; -export const NETWORKS = [`testnet`, `signet`, `liquid`, `bisq`, `mainnet`] as const; +export const NETWORKS = [`testnet`, `signet`, `liquid`, `liquidtestnet`, `bisq`, `mainnet`] as const; export type Network = typeof NETWORKS[number]; // Turn const array into union type -export const ADDRESS_REGEXES: [RegExp, string][] = NETWORKS +export const ADDRESS_REGEXES: [RegExp, Network][] = NETWORKS .map(network => [getRegex('address', network), network]) export function getRegex(type: RegexTypeNoAddr): RegExp; @@ -306,6 +333,11 @@ export function getRegex(type: RegexType, network?: Network): RegExp { regex += `|`; // OR regex += ADDRESS_CHARS.liquid.bech32; break; + case `liquidtestnet`: + regex += ADDRESS_CHARS.liquidtestnet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.liquidtestnet.bech32; + break; case `bisq`: regex += ADDRESS_CHARS.bisq.base58; regex += `|`; // OR From 3d900a38497d7d6b588abba8d06c2af2c8c5d663 Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 28 Aug 2022 16:28:42 +0900 Subject: [PATCH 04/14] Fix: Prevent regex clash with channel IDs --- frontend/src/app/shared/common.utils.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/frontend/src/app/shared/common.utils.ts b/frontend/src/app/shared/common.utils.ts index 288fc0362..8b914beda 100644 --- a/frontend/src/app/shared/common.utils.ts +++ b/frontend/src/app/shared/common.utils.ts @@ -149,12 +149,13 @@ const BECH32_CHARS_UP = `[AC-HJ-NP-Z02-9]`; const HEX_CHARS = `[a-fA-F0-9]`; // A regex to say "A single 0 OR any number with no leading zeroes" -// (?: // Start a non-capturing group -// 0 // A single 0 -// | // OR -// [1-9]\d* // Any succession of numbers starting with 1-9 -// ) // End the non-capturing group. -const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9]\d*)`; +// Capped at 13 digits so as to not be confused with lightning channel IDs (which are around 17 digits) +// (?: // Start a non-capturing group +// 0 // A single 0 +// | // OR +// [1-9][0-9]{0,12} // Any succession of numbers up to 13 digits starting with 1-9 +// ) // End the non-capturing group. +const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9][0-9]{0,12})`; // Formatting of the address regex is for readability, // We should ignore formatting it with automated formatting tools like prettier. From d825143b3562c4ab493f56ae67a4eadcfde7a469 Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 4 Sep 2022 21:31:02 +0900 Subject: [PATCH 05/14] Search for full address in separate network if matches --- .../search-form/search-form.component.ts | 26 +- frontend/src/app/shared/common.utils.ts | 1 - .../pipes/relative-url/relative-url.pipe.ts | 4 +- frontend/src/app/shared/regex.utils.ts | 224 ++++++++++++++++++ 4 files changed, 235 insertions(+), 20 deletions(-) create mode 100644 frontend/src/app/shared/regex.utils.ts diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index 8031195f0..a9e31221a 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -9,7 +9,7 @@ import { ElectrsApiService } from '../../services/electrs-api.service'; import { RelativeUrlPipe } from '../../shared/pipes/relative-url/relative-url.pipe'; import { ApiService } from '../../services/api.service'; import { SearchResultsComponent } from './search-results/search-results.component'; -import { ADDRESS_REGEXES, getRegex } from '../../shared/common.utils'; +import { findOtherNetworks, getRegex } from '../../shared/regex.utils'; @Component({ selector: 'app-search-form', @@ -208,22 +208,13 @@ export class SearchFormComponent implements OnInit { const searchText = result || this.searchForm.value.searchText.trim(); if (searchText) { this.isSearching = true; + + const otherNetworks = findOtherNetworks(searchText, this.network as any); if (!this.regexTransaction.test(searchText) && this.regexAddress.test(searchText)) { this.navigate('/address/', searchText); - } else if ( - // If the search text matches any other network besides this one - ADDRESS_REGEXES - .filter(([, network]) => network !== this.network) - .some(([regex]) => regex.test(searchText)) - ) { - // Gather all network matches as string[] - const networks = ADDRESS_REGEXES.filter(([regex, network]) => - network !== this.network && - regex.test(searchText) - ).map(([, network]) => network); - // ############################################### - // TODO: Create the search items for the drop down - // ############################################### + } else if (otherNetworks.length > 0) { + // Change the network to the first match + this.navigate('/address/', searchText, undefined, otherNetworks[0]); } else if (this.regexBlockhash.test(searchText) || this.regexBlockheight.test(searchText)) { this.navigate('/block/', searchText); } else if (this.regexTransaction.test(searchText)) { @@ -252,8 +243,9 @@ export class SearchFormComponent implements OnInit { } } - navigate(url: string, searchText: string, extras?: any): void { - this.router.navigate([this.relativeUrlPipe.transform(url), searchText], extras); + + navigate(url: string, searchText: string, extras?: any, swapNetwork?: string) { + this.router.navigate([this.relativeUrlPipe.transform(url, swapNetwork), searchText], extras); this.searchTriggered.emit(); this.searchForm.setValue({ searchText: '', diff --git a/frontend/src/app/shared/common.utils.ts b/frontend/src/app/shared/common.utils.ts index 8b914beda..e50ba13b7 100644 --- a/frontend/src/app/shared/common.utils.ts +++ b/frontend/src/app/shared/common.utils.ts @@ -119,7 +119,6 @@ export function convertRegion(input, to: 'name' | 'abbreviated'): string { } } - export function haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number): number { const rlat1 = lat1 * Math.PI / 180; const rlon1 = lon1 * Math.PI / 180; diff --git a/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts b/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts index d7fe612fe..83f5f20df 100644 --- a/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts +++ b/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts @@ -10,8 +10,8 @@ export class RelativeUrlPipe implements PipeTransform { private stateService: StateService, ) { } - transform(value: string): string { - let network = this.stateService.network; + transform(value: string, swapNetwork?: string): string { + let network = swapNetwork || this.stateService.network; if (this.stateService.env.BASE_MODULE === 'liquid' && network === 'liquidtestnet') { network = 'testnet'; } else if (this.stateService.env.BASE_MODULE !== 'mempool') { diff --git a/frontend/src/app/shared/regex.utils.ts b/frontend/src/app/shared/regex.utils.ts new file mode 100644 index 000000000..bac256c8d --- /dev/null +++ b/frontend/src/app/shared/regex.utils.ts @@ -0,0 +1,224 @@ +// all base58 characters +const BASE58_CHARS = `[a-km-zA-HJ-NP-Z1-9]`; + +// all bech32 characters (after the separator) +const BECH32_CHARS_LW = `[ac-hj-np-z02-9]`; +const BECH32_CHARS_UP = `[AC-HJ-NP-Z02-9]`; + +// Hex characters +const HEX_CHARS = `[a-fA-F0-9]`; + +// A regex to say "A single 0 OR any number with no leading zeroes" +// Capped at 13 digits so as to not be confused with lightning channel IDs (which are around 17 digits) +// (?: // Start a non-capturing group +// 0 // A single 0 +// | // OR +// [1-9][0-9]{0,12} // Any succession of numbers up to 13 digits starting with 1-9 +// ) // End the non-capturing group. +const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9][0-9]{0,12})`; + +// Formatting of the address regex is for readability, +// We should ignore formatting it with automated formatting tools like prettier. +// +// prettier-ignore +const ADDRESS_CHARS: { + [k in Network]: { + base58: string; + bech32: string; + }; +} = { + mainnet: { + base58: `[13]` // Starts with a single 1 or 3 + + BASE58_CHARS + + `{26,33}`, // Repeat the previous char 26-33 times. + // Version byte 0x00 (P2PKH) can be as short as 27 characters, up to 34 length + // P2SH must be 34 length + bech32: `(?:` + + `bc1` // Starts with bc1 + + BECH32_CHARS_LW + + `{6,100}` // As per bech32, 6 char checksum is minimum + + `|` + + `BC1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + testnet: { + base58: `[mn2]` // Starts with a single m, n, or 2 (P2PKH is m or n, 2 is P2SH) + + BASE58_CHARS + + `{33,34}`, // m|n is 34 length, 2 is 35 length (We match the first letter separately) + bech32: `(?:` + + `tb1` // Starts with bc1 + + BECH32_CHARS_LW + + `{6,100}` // As per bech32, 6 char checksum is minimum + + `|` + + `TB1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + signet: { + base58: `[mn2]` + + BASE58_CHARS + + `{33,34}`, + bech32: `(?:` + + `tb1` // Starts with tb1 + + BECH32_CHARS_LW + + `{6,100}` + + `|` + + `TB1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + liquid: { + base58: `[GHPQ]` // G|H is P2PKH, P|Q is P2SH + + BASE58_CHARS + + `{33}`, // All min-max lengths are 34 + bech32: `(?:` + + `(?:` // bech32 liquid starts with ex or lq + + `ex` + + `|` + + `lq` + + `)` + + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. + + `{6,100}` + + `|` + + `(?:` // Same as above but all upper case + + `EX` + + `|` + + `LQ` + + `)` + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + liquidtestnet: { + base58: `[89]` // ???(TODO: find version) is P2PKH, 8|9 is P2SH + + BASE58_CHARS + + `{33}`, // P2PKH is ???(TODO: find size), P2SH is 34 + bech32: `(?:` + + `(?:` // bech32 liquid testnet starts with tex or tlq + + `tex` // TODO: Why does mempool use this and not ert|el like in the elements source? + + `|` + + `tlq` // TODO: does this exist? + + `)` + + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. + + `{6,100}` + + `|` + + `(?:` // Same as above but all upper case + + `TEX` + + `|` + + `TLQ` + + `)` + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, + bisq: { + base58: `B1` // bisq base58 addrs start with B1 + + BASE58_CHARS + + `{33}`, // always length 35 + bech32: `(?:` + + `bbc1` // Starts with bbc1 + + BECH32_CHARS_LW + + `{6,100}` + + `|` + + `BBC1` // All upper case version + + BECH32_CHARS_UP + + `{6,100}` + + `)`, + }, +} +type RegexTypeNoAddr = `blockhash` | `transaction` | `blockheight`; +export type RegexType = `address` | RegexTypeNoAddr; + +export const NETWORKS = [`testnet`, `signet`, `liquid`, `liquidtestnet`, `bisq`, `mainnet`] as const; +export type Network = typeof NETWORKS[number]; // Turn const array into union type + +export const ADDRESS_REGEXES: [RegExp, Network][] = NETWORKS + .map(network => [getRegex('address', network), network]) + +export function findOtherNetworks(address: string, skipNetwork: Network): Network[] { + return ADDRESS_REGEXES.filter(([regex, network]) => + network !== skipNetwork && + regex.test(address) + ).map(([, network]) => network); +} + +export function getRegex(type: RegexTypeNoAddr): RegExp; +export function getRegex(type: 'address', network: Network): RegExp; +export function getRegex(type: RegexType, network?: Network): RegExp { + let regex = `^`; // ^ = Start of string + switch (type) { + // Match a block height number + // [Testing Order]: any order is fine + case `blockheight`: + regex += ZERO_INDEX_NUMBER_CHARS; // block height is a 0 indexed number + break; + // Match a 32 byte block hash in hex. Assumes at least 32 bits of difficulty. + // [Testing Order]: Must always be tested before `transaction` + case `blockhash`: + regex += `0{8}`; // Starts with exactly 8 zeroes in a row + regex += `${HEX_CHARS}{56}`; // Continues with exactly 56 hex letters/numbers + break; + // Match a 32 byte tx hash in hex. Contains optional output index specifier. + // [Testing Order]: Must always be tested after `blockhash` + case `transaction`: + regex += `${HEX_CHARS}{64}`; // Exactly 64 hex letters/numbers + regex += `(?:`; // Start a non-capturing group + regex += `:`; // 1 instances of the symbol ":" + regex += ZERO_INDEX_NUMBER_CHARS; // A zero indexed number + regex += `)?`; // End the non-capturing group. This group appears 0 or 1 times + break; + // Match any one of the many address types + // [Testing Order]: While possible that a bech32 address happens to be 64 hex + // characters in the future (current lengths are not 64), it is highly unlikely + // Order therefore, does not matter. + case `address`: + if (!network) { + throw new Error(`Must pass network when type is address`); + } + regex += `(?:`; // Start a non-capturing group (each network has multiple options) + switch (network) { + case `mainnet`: + regex += ADDRESS_CHARS.mainnet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.mainnet.bech32; + break; + case `testnet`: + regex += ADDRESS_CHARS.testnet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.testnet.bech32; + break; + case `signet`: + regex += ADDRESS_CHARS.signet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.signet.bech32; + break; + case `liquid`: + regex += ADDRESS_CHARS.liquid.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.liquid.bech32; + break; + case `liquidtestnet`: + regex += ADDRESS_CHARS.liquidtestnet.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.liquidtestnet.bech32; + break; + case `bisq`: + regex += ADDRESS_CHARS.bisq.base58; + regex += `|`; // OR + regex += ADDRESS_CHARS.bisq.bech32; + break; + default: + throw new Error(`Invalid Network ${network} (Unreachable error in TypeScript)`); + } + regex += `)`; // End the non-capturing group + break; + default: + throw new Error(`Invalid RegexType ${type} (Unreachable error in TypeScript)`); + } + regex += `$`; // $ = End of string + return new RegExp(regex); +} From 2c59992d3fbc28f64312802c622564b97a946cd6 Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 4 Sep 2022 21:53:52 +0900 Subject: [PATCH 06/14] Fix E2E error --- .../search-form/search-form.component.ts | 2 +- frontend/src/app/shared/common.utils.ts | 218 ------------------ 2 files changed, 1 insertion(+), 219 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index a9e31221a..18b4048ef 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -65,7 +65,7 @@ export class SearchFormComponent implements OnInit { this.stateService.networkChanged$.subscribe((network) => { this.network = network; // TODO: Eventually change network type here from string to enum of consts - this.regexAddress = getRegex('address', network as any); + this.regexAddress = getRegex('address', network as any || 'mainnet'); }); this.searchForm = this.formBuilder.group({ diff --git a/frontend/src/app/shared/common.utils.ts b/frontend/src/app/shared/common.utils.ts index e50ba13b7..87c952c31 100644 --- a/frontend/src/app/shared/common.utils.ts +++ b/frontend/src/app/shared/common.utils.ts @@ -136,221 +136,3 @@ export function haversineDistance(lat1: number, lon1: number, lat2: number, lon2 export function kmToMiles(km: number): number { return km * 0.62137119; } - -// all base58 characters -const BASE58_CHARS = `[a-km-zA-HJ-NP-Z1-9]`; - -// all bech32 characters (after the separator) -const BECH32_CHARS_LW = `[ac-hj-np-z02-9]`; -const BECH32_CHARS_UP = `[AC-HJ-NP-Z02-9]`; - -// Hex characters -const HEX_CHARS = `[a-fA-F0-9]`; - -// A regex to say "A single 0 OR any number with no leading zeroes" -// Capped at 13 digits so as to not be confused with lightning channel IDs (which are around 17 digits) -// (?: // Start a non-capturing group -// 0 // A single 0 -// | // OR -// [1-9][0-9]{0,12} // Any succession of numbers up to 13 digits starting with 1-9 -// ) // End the non-capturing group. -const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9][0-9]{0,12})`; - -// Formatting of the address regex is for readability, -// We should ignore formatting it with automated formatting tools like prettier. -// -// prettier-ignore -const ADDRESS_CHARS: { - [k in Network]: { - base58: string; - bech32: string; - }; -} = { - mainnet: { - base58: `[13]` // Starts with a single 1 or 3 - + BASE58_CHARS - + `{26,33}`, // Repeat the previous char 26-33 times. - // Version byte 0x00 (P2PKH) can be as short as 27 characters, up to 34 length - // P2SH must be 34 length - bech32: `(?:` - + `bc1` // Starts with bc1 - + BECH32_CHARS_LW - + `{6,100}` // As per bech32, 6 char checksum is minimum - + `|` - + `BC1` // All upper case version - + BECH32_CHARS_UP - + `{6,100}` - + `)`, - }, - testnet: { - base58: `[mn2]` // Starts with a single m, n, or 2 (P2PKH is m or n, 2 is P2SH) - + BASE58_CHARS - + `{33,34}`, // m|n is 34 length, 2 is 35 length (We match the first letter separately) - bech32: `(?:` - + `tb1` // Starts with bc1 - + BECH32_CHARS_LW - + `{6,100}` // As per bech32, 6 char checksum is minimum - + `|` - + `TB1` // All upper case version - + BECH32_CHARS_UP - + `{6,100}` - + `)`, - }, - signet: { - base58: `[mn2]` - + BASE58_CHARS - + `{33,34}`, - bech32: `(?:` - + `tb1` // Starts with tb1 - + BECH32_CHARS_LW - + `{6,100}` - + `|` - + `TB1` // All upper case version - + BECH32_CHARS_UP - + `{6,100}` - + `)`, - }, - liquid: { - base58: `[GHPQ]` // G|H is P2PKH, P|Q is P2SH - + BASE58_CHARS - + `{33}`, // All min-max lengths are 34 - bech32: `(?:` - + `(?:` // bech32 liquid starts with ex or lq - + `ex` - + `|` - + `lq` - + `)` - + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. - + `{6,100}` - + `|` - + `(?:` // Same as above but all upper case - + `EX` - + `|` - + `LQ` - + `)` - + BECH32_CHARS_UP - + `{6,100}` - + `)`, - }, - liquidtestnet: { - base58: `[89]` // ???(TODO: find version) is P2PKH, 8|9 is P2SH - + BASE58_CHARS - + `{33}`, // P2PKH is ???(TODO: find size), P2SH is 34 - bech32: `(?:` - + `(?:` // bech32 liquid testnet starts with tex or tlq - + `tex` // TODO: Why does mempool use this and not ert|el like in the elements source? - + `|` - + `tlq` // TODO: does this exist? - + `)` - + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. - + `{6,100}` - + `|` - + `(?:` // Same as above but all upper case - + `TEX` - + `|` - + `TLQ` - + `)` - + BECH32_CHARS_UP - + `{6,100}` - + `)`, - }, - bisq: { - base58: `B1` // bisq base58 addrs start with B1 - + BASE58_CHARS - + `{33}`, // always length 35 - bech32: `(?:` - + `bbc1` // Starts with bbc1 - + BECH32_CHARS_LW - + `{6,100}` - + `|` - + `BBC1` // All upper case version - + BECH32_CHARS_UP - + `{6,100}` - + `)`, - }, -} -type RegexTypeNoAddr = `blockhash` | `transaction` | `blockheight`; -export type RegexType = `address` | RegexTypeNoAddr; - -export const NETWORKS = [`testnet`, `signet`, `liquid`, `liquidtestnet`, `bisq`, `mainnet`] as const; -export type Network = typeof NETWORKS[number]; // Turn const array into union type - -export const ADDRESS_REGEXES: [RegExp, Network][] = NETWORKS - .map(network => [getRegex('address', network), network]) - -export function getRegex(type: RegexTypeNoAddr): RegExp; -export function getRegex(type: 'address', network: Network): RegExp; -export function getRegex(type: RegexType, network?: Network): RegExp { - let regex = `^`; // ^ = Start of string - switch (type) { - // Match a block height number - // [Testing Order]: any order is fine - case `blockheight`: - regex += ZERO_INDEX_NUMBER_CHARS; // block height is a 0 indexed number - break; - // Match a 32 byte block hash in hex. Assumes at least 32 bits of difficulty. - // [Testing Order]: Must always be tested before `transaction` - case `blockhash`: - regex += `0{8}`; // Starts with exactly 8 zeroes in a row - regex += `${HEX_CHARS}{56}`; // Continues with exactly 56 hex letters/numbers - break; - // Match a 32 byte tx hash in hex. Contains optional output index specifier. - // [Testing Order]: Must always be tested after `blockhash` - case `transaction`: - regex += `${HEX_CHARS}{64}`; // Exactly 64 hex letters/numbers - regex += `(?:`; // Start a non-capturing group - regex += `:`; // 1 instances of the symbol ":" - regex += ZERO_INDEX_NUMBER_CHARS; // A zero indexed number - regex += `)?`; // End the non-capturing group. This group appears 0 or 1 times - break; - // Match any one of the many address types - // [Testing Order]: While possible that a bech32 address happens to be 64 hex - // characters in the future (current lengths are not 64), it is highly unlikely - // Order therefore, does not matter. - case `address`: - if (!network) { - throw new Error(`Must pass network when type is address`); - } - regex += `(?:`; // Start a non-capturing group (each network has multiple options) - switch (network) { - case `mainnet`: - regex += ADDRESS_CHARS.mainnet.base58; - regex += `|`; // OR - regex += ADDRESS_CHARS.mainnet.bech32; - break; - case `testnet`: - regex += ADDRESS_CHARS.testnet.base58; - regex += `|`; // OR - regex += ADDRESS_CHARS.testnet.bech32; - break; - case `signet`: - regex += ADDRESS_CHARS.signet.base58; - regex += `|`; // OR - regex += ADDRESS_CHARS.signet.bech32; - break; - case `liquid`: - regex += ADDRESS_CHARS.liquid.base58; - regex += `|`; // OR - regex += ADDRESS_CHARS.liquid.bech32; - break; - case `liquidtestnet`: - regex += ADDRESS_CHARS.liquidtestnet.base58; - regex += `|`; // OR - regex += ADDRESS_CHARS.liquidtestnet.bech32; - break; - case `bisq`: - regex += ADDRESS_CHARS.bisq.base58; - regex += `|`; // OR - regex += ADDRESS_CHARS.bisq.bech32; - break; - default: - throw new Error(`Invalid Network ${network} (Unreachable error in TypeScript)`); - } - regex += `)`; // End the non-capturing group - break; - default: - throw new Error(`Invalid RegexType ${type} (Unreachable error in TypeScript)`); - } - regex += `$`; // $ = End of string - return new RegExp(regex); -} From 213800f563a2731a991cfbe2ce76b3ec419a92ad Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 19 Jul 2023 16:46:02 +0900 Subject: [PATCH 07/14] Merge error fix --- .../app/components/search-form/search-form.component.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index 18b4048ef..eb8adbd83 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -35,10 +35,10 @@ export class SearchFormComponent implements OnInit { } } - regexAddress = /^([a-km-zA-HJ-NP-Z1-9]{26,35}|[a-km-zA-HJ-NP-Z1-9]{80}|[A-z]{2,5}1[a-zA-HJ-NP-Z0-9]{39,59})$/; - regexBlockhash = /^[0]{8}[a-fA-F0-9]{56}$/; - regexTransaction = /^([a-fA-F0-9]{64})(:\d+)?$/; - regexBlockheight = /^[0-9]{1,9}$/; + regexAddress = getRegex('address', 'mainnet'); // Default to mainnet + regexBlockhash = getRegex('blockhash'); + regexTransaction = getRegex('transaction'); + regexBlockheight = getRegex('blockheight'); focus$ = new Subject(); click$ = new Subject(); From bd34d71d8b82bc784da1f6be3da17d3c076c7cfa Mon Sep 17 00:00:00 2001 From: natsee Date: Sat, 30 Dec 2023 19:19:07 +0100 Subject: [PATCH 08/14] Update regexes in regex.utils.ts --- .../search-form/search-form.component.ts | 18 ++- .../pipes/relative-url/relative-url.pipe.ts | 1 + frontend/src/app/shared/regex.utils.ts | 121 +++++++++++++----- 3 files changed, 105 insertions(+), 35 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index cb64df7f4..4ed6b1d64 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -38,11 +38,11 @@ export class SearchFormComponent implements OnInit { } regexAddress = getRegex('address', 'mainnet'); // Default to mainnet - regexBlockhash = getRegex('blockhash'); + regexBlockhash = getRegex('blockhash', 'mainnet'); regexTransaction = getRegex('transaction'); regexBlockheight = getRegex('blockheight'); - regexDate = /^(?:\d{4}[-/]\d{1,2}[-/]\d{1,2}(?: \d{1,2}:\d{2})?)$/; - regexUnixTimestamp = /^\d{10}$/; + regexDate = getRegex('date'); + regexUnixTimestamp = getRegex('timestamp'); focus$ = new Subject(); click$ = new Subject(); @@ -72,6 +72,7 @@ export class SearchFormComponent implements OnInit { this.network = network; // TODO: Eventually change network type here from string to enum of consts this.regexAddress = getRegex('address', network as any || 'mainnet'); + this.regexBlockhash = getRegex('blockhash', network as any || 'mainnet'); }); this.router.events.subscribe((e: NavigationStart) => { // Reset search focus when changing page @@ -181,8 +182,8 @@ export class SearchFormComponent implements OnInit { const lightningResults = result[1]; const matchesBlockHeight = this.regexBlockheight.test(searchText) && parseInt(searchText) <= this.stateService.latestBlockHeight; - const matchesDateTime = this.regexDate.test(searchText) && new Date(searchText).toString() !== 'Invalid Date'; - const matchesUnixTimestamp = this.regexUnixTimestamp.test(searchText); + const matchesDateTime = this.regexDate.test(searchText) && new Date(searchText).toString() !== 'Invalid Date' && new Date(searchText).getTime() <= Date.now() && new Date(searchText).getTime() >= 1231006505000; + const matchesUnixTimestamp = this.regexUnixTimestamp.test(searchText) && parseInt(searchText) <= Math.floor(Date.now() / 1000) && parseInt(searchText) >= 1231006505; // 1231006505 is the timestamp of the genesis block const matchesTxId = this.regexTransaction.test(searchText) && !this.regexBlockhash.test(searchText); const matchesBlockHash = this.regexBlockhash.test(searchText); const matchesAddress = !matchesTxId && this.regexAddress.test(searchText); @@ -237,7 +238,7 @@ export class SearchFormComponent implements OnInit { if (searchText) { this.isSearching = true; - const otherNetworks = findOtherNetworks(searchText, this.network as any); + const otherNetworks = findOtherNetworks(searchText, this.network as any || 'mainnet'); if (!this.regexTransaction.test(searchText) && this.regexAddress.test(searchText)) { this.navigate('/address/', searchText); } else if (otherNetworks.length > 0) { @@ -269,6 +270,11 @@ export class SearchFormComponent implements OnInit { } else if (this.regexDate.test(searchText) || this.regexUnixTimestamp.test(searchText)) { let timestamp: number; this.regexDate.test(searchText) ? timestamp = Math.floor(new Date(searchText).getTime() / 1000) : timestamp = searchText; + // Check if timestamp is too far in the future or before the genesis block + if (timestamp > Math.floor(Date.now() / 1000) || timestamp < 1231006505) { + this.isSearching = false; + return; + } this.apiService.getBlockDataFromTimestamp$(timestamp).subscribe( (data) => { this.navigate('/block/', data.hash); }, (error) => { console.log(error); this.isSearching = false; } diff --git a/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts b/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts index 83f5f20df..4211765df 100644 --- a/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts +++ b/frontend/src/app/shared/pipes/relative-url/relative-url.pipe.ts @@ -12,6 +12,7 @@ export class RelativeUrlPipe implements PipeTransform { transform(value: string, swapNetwork?: string): string { let network = swapNetwork || this.stateService.network; + if (network === 'mainnet') network = ''; if (this.stateService.env.BASE_MODULE === 'liquid' && network === 'liquidtestnet') { network = 'testnet'; } else if (this.stateService.env.BASE_MODULE !== 'mempool') { diff --git a/frontend/src/app/shared/regex.utils.ts b/frontend/src/app/shared/regex.utils.ts index bac256c8d..d0cd08f24 100644 --- a/frontend/src/app/shared/regex.utils.ts +++ b/frontend/src/app/shared/regex.utils.ts @@ -9,13 +9,16 @@ const BECH32_CHARS_UP = `[AC-HJ-NP-Z02-9]`; const HEX_CHARS = `[a-fA-F0-9]`; // A regex to say "A single 0 OR any number with no leading zeroes" -// Capped at 13 digits so as to not be confused with lightning channel IDs (which are around 17 digits) +// Capped at 9 digits so as to not be confused with lightning channel IDs (which are around 17 digits) // (?: // Start a non-capturing group // 0 // A single 0 // | // OR -// [1-9][0-9]{0,12} // Any succession of numbers up to 13 digits starting with 1-9 +// [1-9][0-9]{0,8} // Any succession of numbers up to 9 digits starting with 1-9 // ) // End the non-capturing group. -const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9][0-9]{0,12})`; +const ZERO_INDEX_NUMBER_CHARS = `(?:0|[1-9][0-9]{0,8})`; + +// Simple digits only regex +const NUMBER_CHARS = `[0-9]`; // Formatting of the address regex is for readability, // We should ignore formatting it with automated formatting tools like prettier. @@ -48,7 +51,7 @@ const ADDRESS_CHARS: { + BASE58_CHARS + `{33,34}`, // m|n is 34 length, 2 is 35 length (We match the first letter separately) bech32: `(?:` - + `tb1` // Starts with bc1 + + `tb1` // Starts with tb1 + BECH32_CHARS_LW + `{6,100}` // As per bech32, 6 char checksum is minimum + `|` @@ -76,18 +79,18 @@ const ADDRESS_CHARS: { + BASE58_CHARS + `{33}`, // All min-max lengths are 34 bech32: `(?:` - + `(?:` // bech32 liquid starts with ex or lq - + `ex` + + `(?:` // bech32 liquid starts with ex1 or lq1 + + `ex1` + `|` - + `lq` + + `lq1` + `)` + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. + `{6,100}` + `|` + `(?:` // Same as above but all upper case - + `EX` + + `EX1` + `|` - + `LQ` + + `LQ1` + `)` + BECH32_CHARS_UP + `{6,100}` @@ -99,39 +102,39 @@ const ADDRESS_CHARS: { + `{33}`, // P2PKH is ???(TODO: find size), P2SH is 34 bech32: `(?:` + `(?:` // bech32 liquid testnet starts with tex or tlq - + `tex` // TODO: Why does mempool use this and not ert|el like in the elements source? + + `tex1` // TODO: Why does mempool use this and not ert|el like in the elements source? + `|` - + `tlq` // TODO: does this exist? + + `tlq1` // TODO: does this exist? + `)` + BECH32_CHARS_LW // blech32 and bech32 are the same alphabet and protocol, different checksums. + `{6,100}` + `|` + `(?:` // Same as above but all upper case - + `TEX` + + `TEX1` + `|` - + `TLQ` + + `TLQ1` + `)` + BECH32_CHARS_UP + `{6,100}` + `)`, }, bisq: { - base58: `B1` // bisq base58 addrs start with B1 + base58: `(?:[bB][13]` // b or B at the start, followed by a single 1 or 3 + BASE58_CHARS - + `{33}`, // always length 35 + + `{26,33})`, bech32: `(?:` - + `bbc1` // Starts with bbc1 + + `[bB]bc1` // b or B at the start, followed by bc1 + BECH32_CHARS_LW - + `{6,100}` + + `{6,100}` + `|` - + `BBC1` // All upper case version + + `[bB]BC1` // b or B at the start, followed by BC1 + BECH32_CHARS_UP + `{6,100}` + `)`, }, } -type RegexTypeNoAddr = `blockhash` | `transaction` | `blockheight`; -export type RegexType = `address` | RegexTypeNoAddr; +type RegexTypeNoAddrNoBlockHash = | `transaction` | `blockheight` | `date` | `timestamp`; +export type RegexType = `address` | `blockhash` | RegexTypeNoAddrNoBlockHash; export const NETWORKS = [`testnet`, `signet`, `liquid`, `liquidtestnet`, `bisq`, `mainnet`] as const; export type Network = typeof NETWORKS[number]; // Turn const array into union type @@ -139,15 +142,15 @@ export type Network = typeof NETWORKS[number]; // Turn const array into union ty export const ADDRESS_REGEXES: [RegExp, Network][] = NETWORKS .map(network => [getRegex('address', network), network]) -export function findOtherNetworks(address: string, skipNetwork: Network): Network[] { - return ADDRESS_REGEXES.filter(([regex, network]) => - network !== skipNetwork && - regex.test(address) - ).map(([, network]) => network); +export function findOtherNetworks(address: string, skipNetwork: Network): {network: Network, address: string}[] { + return ADDRESS_REGEXES + .filter(([regex, network]) => network !== skipNetwork && regex.test(address)) + .map(([, network]) => ({ network, address })); } -export function getRegex(type: RegexTypeNoAddr): RegExp; +export function getRegex(type: RegexTypeNoAddrNoBlockHash): RegExp; export function getRegex(type: 'address', network: Network): RegExp; +export function getRegex(type: 'blockhash', network: Network): RegExp; export function getRegex(type: RegexType, network?: Network): RegExp { let regex = `^`; // ^ = Start of string switch (type) { @@ -156,11 +159,37 @@ export function getRegex(type: RegexType, network?: Network): RegExp { case `blockheight`: regex += ZERO_INDEX_NUMBER_CHARS; // block height is a 0 indexed number break; - // Match a 32 byte block hash in hex. Assumes at least 32 bits of difficulty. + // Match a 32 byte block hash in hex. // [Testing Order]: Must always be tested before `transaction` case `blockhash`: - regex += `0{8}`; // Starts with exactly 8 zeroes in a row - regex += `${HEX_CHARS}{56}`; // Continues with exactly 56 hex letters/numbers + if (!network) { + throw new Error(`Must pass network when type is blockhash`); + } + let leadingZeroes: number; + switch (network) { + case `mainnet`: + leadingZeroes = 8; // Assumes at least 32 bits of difficulty + break; + case `testnet`: + leadingZeroes = 8; // Assumes at least 32 bits of difficulty + break; + case `signet`: + leadingZeroes = 5; + break; + case `liquid`: + leadingZeroes = 8; // We are not interested in Liquid block hashes + break; + case `liquidtestnet`: + leadingZeroes = 8; // We are not interested in Liquid block hashes + break; + case `bisq`: + leadingZeroes = 8; // Assumes at least 32 bits of difficulty + break; + default: + throw new Error(`Invalid Network ${network} (Unreachable error in TypeScript)`); + } + regex += `0{${leadingZeroes}}`; + regex += `${HEX_CHARS}{${64 - leadingZeroes}}`; // Exactly 64 hex letters/numbers break; // Match a 32 byte tx hash in hex. Contains optional output index specifier. // [Testing Order]: Must always be tested after `blockhash` @@ -185,16 +214,28 @@ export function getRegex(type: RegexType, network?: Network): RegExp { regex += ADDRESS_CHARS.mainnet.base58; regex += `|`; // OR regex += ADDRESS_CHARS.mainnet.bech32; + regex += `|`; // OR + regex += `04${HEX_CHARS}{128}`; // Uncompressed pubkey + regex += `|`; // OR + regex += `(?:02|03)${HEX_CHARS}{64}`; // Compressed pubkey break; case `testnet`: regex += ADDRESS_CHARS.testnet.base58; regex += `|`; // OR regex += ADDRESS_CHARS.testnet.bech32; + regex += `|`; // OR + regex += `04${HEX_CHARS}{128}`; // Uncompressed pubkey + regex += `|`; // OR + regex += `(?:02|03)${HEX_CHARS}{64}`; // Compressed pubkey break; case `signet`: regex += ADDRESS_CHARS.signet.base58; regex += `|`; // OR regex += ADDRESS_CHARS.signet.bech32; + regex += `|`; // OR + regex += `04${HEX_CHARS}{128}`; // Uncompressed pubkey + regex += `|`; // OR + regex += `(?:02|03)${HEX_CHARS}{64}`; // Compressed pubkey break; case `liquid`: regex += ADDRESS_CHARS.liquid.base58; @@ -216,6 +257,28 @@ export function getRegex(type: RegexType, network?: Network): RegExp { } regex += `)`; // End the non-capturing group break; + // Match a date in the format YYYY-MM-DD (optional: HH:MM) + // [Testing Order]: any order is fine + case `date`: + regex += `(?:`; // Start a non-capturing group + regex += `${NUMBER_CHARS}{4}`; // Exactly 4 digits + regex += `[-/]`; // 1 instance of the symbol "-" or "/" + regex += `${NUMBER_CHARS}{1,2}`; // Exactly 4 digits + regex += `[-/]`; // 1 instance of the symbol "-" or "/" + regex += `${NUMBER_CHARS}{1,2}`; // Exactly 4 digits + regex += `(?:`; // Start a non-capturing group + regex += ` `; // 1 instance of the symbol " " + regex += `${NUMBER_CHARS}{1,2}`; // Exactly 4 digits + regex += `:`; // 1 instance of the symbol ":" + regex += `${NUMBER_CHARS}{1,2}`; // Exactly 4 digits + regex += `)?`; // End the non-capturing group. This group appears 0 or 1 times + regex += `)`; // End the non-capturing group + break; + // Match a unix timestamp + // [Testing Order]: any order is fine + case `timestamp`: + regex += `${NUMBER_CHARS}{10}`; // Exactly 10 digits + break; default: throw new Error(`Invalid RegexType ${type} (Unreachable error in TypeScript)`); } From 3e1b85e32ca7526f9772a495b14ef8bd6cfb3546 Mon Sep 17 00:00:00 2001 From: natsee Date: Sun, 31 Dec 2023 23:23:53 +0100 Subject: [PATCH 09/14] Add search addresses from cross networks feature --- .../search-form/search-form.component.ts | 48 ++++++++++--------- .../search-results.component.html | 16 +++++-- .../search-results.component.scss | 4 ++ .../search-results.component.ts | 2 +- frontend/src/app/shared/regex.utils.ts | 37 ++++++++++++++ 5 files changed, 79 insertions(+), 28 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index 4ed6b1d64..ed46f1e88 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -2,14 +2,14 @@ import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Output, ViewC import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { EventType, NavigationStart, Router } from '@angular/router'; import { AssetsService } from '../../services/assets.service'; -import { StateService } from '../../services/state.service'; +import { Env, StateService } from '../../services/state.service'; import { Observable, of, Subject, zip, BehaviorSubject, combineLatest } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap, catchError, map, startWith, tap } from 'rxjs/operators'; import { ElectrsApiService } from '../../services/electrs-api.service'; import { RelativeUrlPipe } from '../../shared/pipes/relative-url/relative-url.pipe'; import { ApiService } from '../../services/api.service'; import { SearchResultsComponent } from './search-results/search-results.component'; -import { findOtherNetworks, getRegex } from '../../shared/regex.utils'; +import { Network, findOtherNetworks, getRegex, getTargetUrl, needBaseModuleChange } from '../../shared/regex.utils'; @Component({ selector: 'app-search-form', @@ -19,7 +19,7 @@ import { findOtherNetworks, getRegex } from '../../shared/regex.utils'; }) export class SearchFormComponent implements OnInit { @Input() hamburgerOpen = false; - + env: Env; network = ''; assets: object = {}; isSearching = false; @@ -68,6 +68,7 @@ export class SearchFormComponent implements OnInit { } ngOnInit(): void { + this.env = this.stateService.env; this.stateService.networkChanged$.subscribe((network) => { this.network = network; // TODO: Eventually change network type here from string to enum of consts @@ -103,9 +104,6 @@ export class SearchFormComponent implements OnInit { const searchText$ = this.searchForm.get('searchText').valueChanges .pipe( map((text) => { - if (this.network === 'bisq' && text.match(/^(b)[^c]/i)) { - return text.substr(1); - } return text.trim(); }), tap((text) => { @@ -139,9 +137,6 @@ export class SearchFormComponent implements OnInit { ); }), map((result: any[]) => { - if (this.network === 'bisq') { - result[0] = result[0].map((address: string) => 'B' + address); - } return result; }), tap(() => { @@ -171,6 +166,7 @@ export class SearchFormComponent implements OnInit { blockHeight: false, txId: false, address: false, + otherNetworks: [], addresses: [], nodes: [], channels: [], @@ -186,10 +182,13 @@ export class SearchFormComponent implements OnInit { const matchesUnixTimestamp = this.regexUnixTimestamp.test(searchText) && parseInt(searchText) <= Math.floor(Date.now() / 1000) && parseInt(searchText) >= 1231006505; // 1231006505 is the timestamp of the genesis block const matchesTxId = this.regexTransaction.test(searchText) && !this.regexBlockhash.test(searchText); const matchesBlockHash = this.regexBlockhash.test(searchText); - const matchesAddress = !matchesTxId && this.regexAddress.test(searchText); + let matchesAddress = !matchesTxId && this.regexAddress.test(searchText); + const otherNetworks = findOtherNetworks(searchText, this.network as any || 'mainnet'); - if (matchesAddress && this.network === 'bisq') { - searchText = 'B' + searchText; + // Add B prefix to addresses in Bisq network + if (!matchesAddress && this.network === 'bisq' && getRegex('address', 'mainnet').test(searchText)) { + searchText = 'B' + searchText; + matchesAddress = !matchesTxId && this.regexAddress.test(searchText); } if (matchesDateTime && searchText.indexOf('/') !== -1) { @@ -205,7 +204,8 @@ export class SearchFormComponent implements OnInit { txId: matchesTxId, blockHash: matchesBlockHash, address: matchesAddress, - addresses: addressPrefixSearchResults, + addresses: matchesAddress && addressPrefixSearchResults.length === 1 && searchText === addressPrefixSearchResults[0] ? [] : addressPrefixSearchResults, // If there is only one address and it matches the search text, don't show it in the dropdown + otherNetworks: otherNetworks, nodes: lightningResults.nodes, channels: lightningResults.channels, }; @@ -230,6 +230,8 @@ export class SearchFormComponent implements OnInit { this.navigate('/lightning/node/', result.public_key); } else if (result.short_id) { this.navigate('/lightning/channel/', result.id); + } else if (result.network) { + this.navigate('/address/', result.address, undefined, result.network); } } @@ -238,12 +240,8 @@ export class SearchFormComponent implements OnInit { if (searchText) { this.isSearching = true; - const otherNetworks = findOtherNetworks(searchText, this.network as any || 'mainnet'); if (!this.regexTransaction.test(searchText) && this.regexAddress.test(searchText)) { this.navigate('/address/', searchText); - } else if (otherNetworks.length > 0) { - // Change the network to the first match - this.navigate('/address/', searchText, undefined, otherNetworks[0]); } else if (this.regexBlockhash.test(searchText)) { this.navigate('/block/', searchText); } else if (this.regexBlockheight.test(searchText)) { @@ -288,11 +286,15 @@ export class SearchFormComponent implements OnInit { navigate(url: string, searchText: string, extras?: any, swapNetwork?: string) { - this.router.navigate([this.relativeUrlPipe.transform(url, swapNetwork), searchText], extras); - this.searchTriggered.emit(); - this.searchForm.setValue({ - searchText: '', - }); - this.isSearching = false; + if (needBaseModuleChange(this.env.BASE_MODULE as 'liquid' | 'bisq' | 'mempool', swapNetwork as Network)) { + window.location.href = getTargetUrl(swapNetwork as Network, searchText, this.env); + } else { + this.router.navigate([this.relativeUrlPipe.transform(url, swapNetwork), searchText], extras); + this.searchTriggered.emit(); + this.searchForm.setValue({ + searchText: '', + }); + this.isSearching = false; + } } } diff --git a/frontend/src/app/components/search-form/search-results/search-results.component.html b/frontend/src/app/components/search-form/search-results/search-results.component.html index d4f68edbd..adc92b0bf 100644 --- a/frontend/src/app/components/search-form/search-results/search-results.component.html +++ b/frontend/src/app/components/search-form/search-results/search-results.component.html @@ -1,4 +1,4 @@ -