The Open Network: obtain address (wallets/pools) without mytonctrl

#!/bin/bash

# Function to read address file and extract addr and workchain
read_addr_file() {
    local file_path="$1"
    local addr=$(xxd -p -l 32 "$file_path" | tr -d '\n')
    local workchain=$(od -An -t d4 -j 32 -N 4 "$file_path" | tr -d ' ')
    echo "$addr $workchain"
}

# Function to convert addr_full to addr_b64
addr_full_to_addr_b64() {
    local addr_full="$1"
    local bounceable="$2"
    local testnet="$3"

    IFS=':' read -r workchain addr <<< "$addr_full"

    if [ ${#addr} -ne 64 ]; then
        echo "Invalid length of hexadecimal address" >&2
        exit 1
    fi

    local prefix
    if [ "$bounceable" = true ] && [ "$testnet" = false ]; then
        prefix="11"
    elif [ "$bounceable" = false ] && [ "$testnet" = false ]; then
        prefix="51"
    elif [ "$bounceable" = true ] && [ "$testnet" = true ]; then
        prefix="91"
    else
        prefix="D1"
    fi

    local workchain_byte=$(printf "%02x" $((workchain & 255)))
    local data="${prefix}${workchain_byte}${addr}"
    
    # Calculate CRC16-XMODEM
    local crc=$(crc16_xmodem "$data")
    
    local result=$(echo -n "${data}${crc}" | xxd -r -p | base64 | tr '+/' '-_' | tr -d '=')
    echo "$result"
}

# Main function
main() {
    local file_path="/home/ubuntu/.local/share/mytoncore/wallets/validator_wallet_001.addr"
    
    # Get addr and workchain
    read -r addr workchain <<< $(read_addr_file "$file_path")
    
    # Construct addr_full
    local addr_full="${workchain}:${addr}"
    
    # Get addr_b64 (bounceable)
    local addr_b64=$(addr_full_to_addr_b64 "$addr_full" true false)
    
    # Print results
    echo "$addr_b64"
}

# Run the main function
main

# Function to calculate CRC16-XMODEM
crc16_xmodem() {
    local data="$1"
    local crc=0
    for (( i=0; i<${#data}; i+=2 )); do
        byte=$((16#${data:$i:2}))
        crc=$((crc ^ (byte << 8)))
        for (( j=0; j<8; j++ )); do
            if (( crc & 0x8000 )); then
                crc=$(( (crc << 1) ^ 0x1021 ))
            else
                crc=$((crc << 1))
            fi
            crc=$((crc & 0xFFFF))
        done
    done
    printf "%04x" $crc
}