CryptoCurrenciesFormat

Verifies that a crypto address is valid and only returns the address if it is valid.

Type
python
Pack
Cryptocurrency

Source

from hashlib import sha256

import demistomock as demisto
from CommonServerPython import *  # noqa: E402 lgtm [py/polluting-import]

DIGITS58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"


def decode_base58(address, length) -> bytes:
    n = 0
    for char in address:
        n = n * 58 + DIGITS58.index(char)
    return n.to_bytes(length, "big")


def verify_is_bitcoin(address) -> bytes | bool:
    try:
        bitcoin_bytes = decode_base58(address, 25)
        """Check if the last four bytes are equal to the
        first four bytes of a double SHA-256 digest of the previous 21 bytes.
        Source: https://rosettacode.org/wiki/Bitcoin/address_validation#Python """
        return bitcoin_bytes[-4:] == sha256(sha256(bitcoin_bytes[:-4]).digest()).digest()[:4]
    except Exception:
        return False


def main():
    address_list = argToList(demisto.args().get("input"))

    list_results = [f"bitcoin:{address}" if verify_is_bitcoin(address) else "" for address in address_list]

    if list_results:
        demisto.results(list_results)
    else:
        demisto.results("")


if __name__ in ("__main__", "builtin", "builtins"):
    main()

README

Verifies that a cryptocurrency address is valid and only returns the address if it is valid.

Script Data


Name Description
Script Type python3
Tags  
Cortex XSOAR Version 5.0.0

Supported cryptocurrencies

Inputs


Argument Name Description
input The Cryptocurrency address to check.

Outputs


There are no outputs for this script.

Script Example

!CryptoCurrenciesFormat address=1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i

Human Readable Output

1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i