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

Search - add error handling to SRC20 Search #593

Open
babalicious-io opened this issue Jan 3, 2025 · 0 comments
Open

Search - add error handling to SRC20 Search #593

babalicious-io opened this issue Jan 3, 2025 · 0 comments

Comments

@babalicious-io
Copy link
Collaborator

Add error handling to SRC20 Search (SRC20Search.tsx) for:

  • CPID
  • address
  • tx hash

The error handling code from StampSearch.tsx should be able to work for token search as well.
CPID search kicks in if a/A+5chars (6chars total) so that should not conflict with search for tokens starting with A (5chars total).
I'm unsure how the SRC20 autosuggest vs StampSearch's EnterSearch will work with the error handling/messages...

StampSearch.tsx - line 38-145 ↓

const validateBitcoinAddress = async (address: string) => {
    // First check format
    const isValidFormat = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,62}$/.test(address);
    if (!isValidFormat) return false;

    try {
      const response = await fetch(
        `https://blockchain.info/rawaddr/${address}`,
      );
      if (!response.ok) return false;
      const data = await response.json();
      return data.n_tx > 0;
    } catch (error) {
      console.log("Stamp Search Error=========>", error);
      return false;
    }
  };

  const validateBitcoinTx = async (txHash: string) => {
    try {
      const response = await fetch(`https://blockchain.info/rawtx/${txHash}`);
      if (!response.ok) return false;

      const data = await response.json();
      // Check if response has basic tx properties
      return !!(data && data.hash && data.ver);
    } catch (error) {
      console.log("Stamp Search Error=========>", error);
      return false;
    }
  };

  // Helper function to validate if string contains only hex chars
  const isHexString = (str: string): boolean => {
    return /^[a-fA-F0-9]+$/.test(str);
  };

  const handleSearch = async () => {
    const query = searchTerm.trim();
    if (!query) return;

    try {
      setError("");

      // Handle potential tx hash (any hex string > 16 chars that's not a CPID)
      if (isHexString(query) && query.length > 16 && !query.startsWith("A")) {
        // Check format and blockchain validity
        if (query.length !== 64 || !(await validateBitcoinTx(query))) {
          setError(
            `NO TRANSACTION FOUND\n${query}\nThe transaction hash isn't valid`,
          );
          return;
        }

        try {
          const stampResponse = await fetch(`/api/v2/stamps/${query}`);
          const responseData = await stampResponse.json();

          if (
            stampResponse.ok && responseData.data && responseData.data.stamp
          ) {
            globalThis.location.href = `/stamp/${query}`;
            return;
          }
        } catch (error) {
          console.log("Stamp Search Error=========>", error);
        }

        // Not a stamp but valid tx - open blockchain explorer
        globalThis.open(
          `https://www.blockchain.com/explorer/transactions/btc/${query}`,
          "_blank",
        );
        return;
      }

      // Check for Bitcoin address formats
      if (/^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,62}$/.test(query)) {
        const isValidAndActive = await validateBitcoinAddress(query);
        if (isValidAndActive) {
          globalThis.location.href = `/wallet/${query}`;
          return;
        }
        setError(
          `NO ADDY FOUND\n${query}\nThe Bitcoin address doesn't seem to exist`,
        );
        return;
      }

      // Check for CPID format (starts with 'A' or 'a' followed by at least 5 numeric chars)
      if (/^[Aa]\d{5,}$/.test(query)) {
        try {
          const response = await fetch(`/api/v2/stamps/${query.toUpperCase()}`);
          const responseData = await response.json();

          if (response.ok && responseData.data && responseData.data.stamp) {
            globalThis.location.href = `/stamp/${query.toUpperCase()}`;
            return;
          }
        } catch (error) {
          console.log("Stamp Search Error=========>", error);
        }

        setError(
          `NO STAMP FOUND\n${query}\nThe CPID doesn't seem to be valid`,
        );
        return;
      }
itttm127 added a commit that referenced this issue Jan 6, 2025
reinamora137 pushed a commit that referenced this issue Jan 6, 2025
reinamora137 pushed a commit that referenced this issue Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant