stampchain-io / btc_stamps

Bitcoin Stamps Indexer
https://stampchain.io
GNU Affero General Public License v3.0
9 stars 2 forks source link

Attempt additional base64 decoding on cursed stamps #264

Open reinamora137 opened 1 month ago

reinamora137 commented 1 month ago

There are several where the decoding could be successful on cursed for instances where stamp_base64 is malformed

todo:

strip chars before and including a , (some with base64,) strip chars before and including a : (some with stamp:) strip invalid base64 chars

currently these decode to: application/octet-stream

this function was added in stamp.py to strip non base64, pre-checks will be needed to strip the others beforehand.

#  NOTE: this can be used for cursed, but will change the txlist hash
def remove_non_base64_chars(base64_string):
    """
    Removes any non-base64 characters from the given base64 string.

    Args:
        base64_string (str): The base64 string to remove non-base64 characters from.

    Returns:
        str: The base64 string with non-base64 characters removed.
    """
    return ''.join(c for c in base64_string if c in base64.b64encode(base64.b64decode(base64_string)).decode())
reinamora137 commented 1 month ago

this is a little complex with how the current functions are arranged setting the ident based upon the base64 initial decoding. These functions should do the trick when those are arranged properly.


def strip_chars_before_and_including_comma(s):
    """
    Strips characters before and including a comma from the given string.

    Args:
        s (str): The string to strip characters from.

    Returns:
        str: The string with characters before and including a comma stripped.
    """
    return s[s.find(',') + 1:] if ',' in s else s

def strip_chars_before_and_including_colon(s):
    """
    Strips characters before and including a colon from the given string.

    Args:
        s (str): The string to strip characters from.

    Returns:
        str: The string with characters before and including a colon stripped.
    """
    return s[s.find(':') + 1:] if ':' in s else s

def remove_non_base64_chars(base64_string):
    """
    Removes any non-base64 characters from the given base64 string.

    Args:
        base64_string (str): The base64 string to remove non-base64 characters from.

    Returns:
        str: The base64 string with non-base64 characters removed.
    """
    return ''.join(c for c in base64_string if c in base64.b64encode(base64.b64decode(base64_string)).decode())

def post_process_base64_strings(s):
    """
    Processes the given string by stripping certain characters and removing non-base64 characters.
    This is for cursed stamps thath previously decoded as octet-stream

    Args:
        s (str): The string to process.

    Returns:
        str: The processed string.
    """
    s = strip_chars_before_and_including_comma(s)
    s = strip_chars_before_and_including_colon(s)
    return remove_non_base64_chars(s)