from libcrypto import HDWallet, Wallet, generate_mnemonic, validate_mnemonic


def derive_wallet(hd_wallet: HDWallet, path: str) -> Wallet:
    node = hd_wallet.derive_from_path(path)

    if node.private_key is None:
        raise RuntimeError("Derived node does not contain a private key.")

    return Wallet(node.private_key)


def main() -> None:
    mnemonic = generate_mnemonic(12)

    print("Mnemonic:")
    print(mnemonic)

    if not validate_mnemonic(mnemonic):
        raise RuntimeError("Generated mnemonic is not valid.")

    hd_wallet = HDWallet.from_mnemonic(mnemonic, passphrase="", network="mainnet")

    derivations = {
        "Bitcoin BIP44 account 0 address 0": "m/44'/0'/0'/0/0",
        "Bitcoin BIP84 account 0 address 0": "m/84'/0'/0'/0/0",
        "Ethereum BIP44 account 0 address 0": "m/44'/60'/0'/0/0",
        "TRON BIP44 account 0 address 0": "m/44'/195'/0'/0/0",
    }

    for label, path in derivations.items():
        wallet = derive_wallet(hd_wallet, path)

        print(f"\n{label}")
        print("Path:", path)

        if "Bitcoin" in label:
            print("P2PKH:", wallet.get_address("bitcoin", "p2pkh"))
            print("P2WPKH:", wallet.get_address("bitcoin", "p2wpkh"))
            print("P2TR:", wallet.get_address("bitcoin", "p2tr"))

        if "Ethereum" in label:
            print("Ethereum:", wallet.get_address("ethereum"))

        if "TRON" in label:
            print("TRON:", wallet.get_address("tron"))
            print("TRON Hex:", wallet.get_address("tron", "hex"))


if __name__ == "__main__":
    main()