diff --git a/macros/streamline/configs.yaml.sql b/macros/streamline/configs.yaml.sql index 2851024..61c310e 100644 --- a/macros/streamline/configs.yaml.sql +++ b/macros/streamline/configs.yaml.sql @@ -181,5 +181,16 @@ sql: | {{ fsc_utils.create_udf_bech32() | indent(4) }} +- name: {{ schema }}.udf_hex_to_algorand + signature: + - [input, STRING] + return_type: TEXT + options: | + LANGUAGE PYTHON + RUNTIME_VERSION = '3.8' + HANDLER = 'transform_hex_to_algorand' + sql: | + {{ fsc_utils.create_udf_hex_to_algorand() | indent(4) }} + {% endmacro %} diff --git a/macros/streamline/functions.py.sql b/macros/streamline/functions.py.sql index b6c82ed..52087cc 100644 --- a/macros/streamline/functions.py.sql +++ b/macros/streamline/functions.py.sql @@ -261,4 +261,26 @@ def transform_bech32(input, hrp=''): return hrp + '1' + ''.join([CHARSET[d] for d in data5bit + checksum]) +{% endmacro %} + +{% macro create_udf_hex_to_algorand() %} + +import hashlib +import base64 + +def transform_hex_to_algorand(input): + if input is None or not input.startswith('0x'): + return 'Invalid input' + + input = input[2:] + public_key_bytes = bytearray.fromhex(input) + + sha512_256_hash = hashlib.new('sha512_256', public_key_bytes).digest() + + checksum = sha512_256_hash[-4:] + + algorand_address = base64.b32encode(public_key_bytes + checksum).decode('utf-8').rstrip('=') + + return algorand_address + {% endmacro %} \ No newline at end of file