#!/usr/bin/env python

# https://www.dnspython.org/
import dns.resolver

import sys

# Not yet known from most software. https://www.iana.org/assignments/dns-parameters/dns-parameters.xml#dns-parameters-4
WALLET = 262

def decode(wallet_rr):
    result = []
    bytes = wallet_rr.to_wire()
    index = 0
    while True:
        if index >= len(bytes):
            break
        length = int(bytes[index])
        new_index = index+length+1
        if new_index > len(bytes):
            raise Exception("Missing data at the end of the WALLET RR")
        result.append(bytes[index+1:new_index].decode())
        index = new_index
    if index != len(bytes):
        raise Exception("Extra data at the end of the WALLET RR")
    if len(result) != 2:
        raise Exception("A WALLET RR must be two and only two strings")
    return {"code": result[0], "address": result[1]}

if len(sys.argv) < 2:
    raise Exception("Usage: %s domain-name ..." % sys.argv[0])
for domain in sys.argv[1:]:
    print(domain)
    answers = dns.resolver.resolve(domain, WALLET)
    for rdata in answers:
        value = decode(rdata)
        print("Code: %s ; Address: %s" % (value["code"], value["address"]))
    print("")
