Files
ObsidianDragon/src/util/address_validation.h
T
DanSandClaude Opus 4.8 579accd0c6 fix(ui): validate shielded addresses with the correct RPC
The Validate address dialog chose its RPC from the first character of
the input, then asked the transparent-only validateaddress for every
case. That RPC is Base58-only, so a valid Sapling (z) address came back
isvalid=false and was reported invalid.

- The input is now classified with the checksum-verified recognizers,
  and a shielded address is sent to z_validateaddress while a
  transparent (mining) address keeps validateaddress.
- Anything that is not an address - a typo, garbage, or a pasted
  viewing/spending key - is judged invalid on the spot and never sent to
  the node, so a key pasted into the field is not leaked to an RPC.
- On Lite there is no node to ask, so the dialog shows the checksum
  verdict alone and hides the "owned by this wallet" row.

Tests: validateRpcMethod maps shielded to z_validateaddress, transparent
to validateaddress, and a key or empty string to no RPC.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-12 21:42:23 -05:00

69 lines
3.6 KiB
C++

// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3
//
// address_validation.h — pure, offline checksum validation for wallet addresses.
// These verify the *encoding checksum* (Base58Check / Bech32), which is independent
// of the chain's version bytes / HRP, so a correct implementation never rejects a
// genuinely-valid address while still catching transcription errors. Used to make
// the send screen's "Valid address" label/gate reflect reality instead of a bare
// prefix+length heuristic. No network, no daemon — safe for both build variants.
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace dragonx {
namespace util {
// True if `s` decodes as Base58Check with a valid 4-byte double-SHA256 checksum
// (transparent R-addresses). Version-byte agnostic by design.
bool isValidBase58Check(const std::string& s);
// Decodes `s` as Base58Check; on success returns true and fills `payloadOut` with the
// decoded bytes EXCLUDING the trailing 4-byte checksum (i.e. version byte + data). Lets
// callers inspect the version byte / payload length (e.g. to tell a WIF from an address).
bool decodeBase58Check(const std::string& s, std::vector<std::uint8_t>& payloadOut);
// True if `s` is a valid Bech32 string (Sapling zs-addresses). The HRP is taken
// from the string itself and folded into the checksum, so no HRP is hardcoded.
bool isValidBech32(const std::string& s);
// Returns the (lower-cased) human-readable prefix of a valid Bech32 string, or "" if
// `s` is not valid Bech32. The HRP identifies the key/address type (e.g. "zivks").
std::string bech32Hrp(const std::string& s);
// True if `s` is a transparent (Base58Check) address — P2PKH *or* P2SH/multisig. Accepts any
// address whose payload is a 21-byte version+hash160, so it covers both the 'R…' (v60) and 'b…'
// (v85 script) forms on every DragonX network and rejects WIF keys / typos by checksum. Version-byte
// agnostic by design — a bare prefix check ('R' only) silently drops valid P2SH recipients.
bool isTransparentAddress(const std::string& s);
// True if `s` is a shielded Sapling payment address (HRP "zs" / "ztestsapling" / "zregtestsapling"),
// with a valid Bech32 checksum. Distinguishes a payment address from a viewing key (e.g. "zivks…").
bool isShieldedAddress(const std::string& s);
// True if `s` is a DragonX address of either kind (transparent or shielded) — "is this an address at all".
bool isDragonXAddress(const std::string& s);
// True if `s` is an address a payment can be sent to. DragonX is a private chain (-ac_private=1): consensus
// rejects any non-coinbase output to a transparent address, so only a shielded address can be paid — sends,
// payment requests and pool payouts included. A transparent address only ever receives mining rewards.
bool isValidRecipientAddress(const std::string& s);
// What sending to `s` would mean: a shielded address (payable), a mining address (transparent: valid, but
// it can only receive mining rewards, so a payment to it is rejected), or not an address.
enum class RecipientKind { Invalid, Shielded, MiningOnly };
RecipientKind classifyRecipient(const std::string& s);
// The daemon RPC that can validate an address of this kind: "z_validateaddress" for a shielded (Sapling)
// address, "validateaddress" for a transparent one (z_validateaddress reports isvalid=false for those, and
// validateaddress is Base58-only so it reports false for Sapling), nullptr for Invalid (not an address:
// answer client-side, send nothing to the node).
const char* validateRpcMethod(RecipientKind kind);
} // namespace util
} // namespace dragonx