udf_hex_to_algo

This commit is contained in:
drethereum 2023-11-30 10:42:56 -07:00
parent 229548e678
commit dae5fff71c
2 changed files with 33 additions and 0 deletions

View File

@ -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 %}

View File

@ -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 %}