Files
dragonx/src/zcash/Address.cpp
T
DanSandClaude Opus 5.5 c59b445ac1 zaddr: reject Sapling addresses that cannot receive funds
DecodePaymentAddress accepted any 43 bytes with a valid bech32 checksum.
An address whose diversifier has no valid g_d, or whose pk_d is not a
point in Jubjub's prime-order subgroup, can never receive a note, yet
z_validateaddress reported it as valid and z_sendmany queued the send.
The async operation then failed in TransactionBuilder::Build
("Invalid sapling note commitment!", surfaced as "Failed to build
transaction.") and took every other recipient in the batch with it.
A mining pool's payouts stalled for 10.5 hours on one such miner
address.

Add SaplingPaymentAddress::is_valid(), which runs
librustzcash_sapling_compute_cm: the same check the builder runs on
every output. DecodePaymentAddress now returns InvalidEncoding when it
fails. As a result:
- z_validateaddress returns isvalid:false for these addresses.
- z_sendmany fails up front with -8 "unknown address format: <addr>".
- Every other RPC and startup option that takes a zaddr rejects them
  through its existing invalid-address path.

The check is the builder's own, so it cannot reject an address the
builder would accept. Wallet-derived addresses are always valid, and
wallet.dat stores addresses in binary rather than decoding these
strings, so existing wallets are unaffected.

Tests: new gtest SaplingAddressValidity covers three real-world
unpayable addresses, constructed small-order, non-canonical, off-curve
and bad-diversifier cases, derived and hardcoded good addresses, and
is_valid() agreeing with SaplingNote::cm(). With the decoder check
reverted, 2 of the 4 tests fail. The full gtest suite passes (100/100),
and the change was verified end to end on regtest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:33:00 -06:00

125 lines
3.9 KiB
C++

// Copyright (c) 2016-2024 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
#include "Address.hpp"
#include "NoteEncryption.hpp"
#include "hash.h"
#include "prf.h"
#include "streams.h"
#include <librustzcash.h>
const unsigned char ZCASH_SAPLING_FVFP_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
{'Z', 'c', 'a', 's', 'h', 'S', 'a', 'p', 'l', 'i', 'n', 'g', 'F', 'V', 'F', 'P'};
const uint32_t SAPLING_BRANCH_ID = 0x76b809bb;
namespace libzcash {
//! Sapling
uint256 SaplingPaymentAddress::GetHash() const {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << *this;
return Hash(ss.begin(), ss.end());
}
bool SaplingPaymentAddress::is_valid() const {
// librustzcash_sapling_compute_cm is the exact check the transaction builder runs
// (via SaplingNote::cm()) on every output: it fails if the diversifier gives no valid
// g_d or if pk_d does not decode to a prime-order Jubjub point. The value and rcm play
// no part in that; zero is a canonical scalar, so rcm = 0 is accepted.
uint256 rcm;
uint256 cm;
return librustzcash_sapling_compute_cm(d.data(), pk_d.begin(), 0, rcm.begin(), cm.begin());
}
SaplingFullViewingKey SaplingExpandedSpendingKey::full_viewing_key() const {
uint256 ak;
uint256 nk;
librustzcash_ask_to_ak(ask.begin(), ak.begin());
librustzcash_nsk_to_nk(nsk.begin(), nk.begin());
return SaplingFullViewingKey(ak, nk, ovk);
}
SaplingExpandedSpendingKey SaplingSpendingKey::expanded_spending_key() const {
return SaplingExpandedSpendingKey(PRF_ask(*this), PRF_nsk(*this), PRF_ovk(*this));
}
SaplingFullViewingKey SaplingSpendingKey::full_viewing_key() const {
return expanded_spending_key().full_viewing_key();
}
SaplingIncomingViewingKey SaplingFullViewingKey::in_viewing_key() const {
uint256 ivk;
librustzcash_crh_ivk(ak.begin(), nk.begin(), ivk.begin());
return SaplingIncomingViewingKey(ivk);
}
bool SaplingFullViewingKey::is_valid() const {
uint256 ivk;
librustzcash_crh_ivk(ak.begin(), nk.begin(), ivk.begin());
return !ivk.IsNull();
}
uint256 SaplingFullViewingKey::GetFingerprint() const {
CBLAKE2bWriter ss(SER_GETHASH, 0, ZCASH_SAPLING_FVFP_PERSONALIZATION);
ss << *this;
return ss.GetHash();
}
SaplingSpendingKey SaplingSpendingKey::random() {
while (true) {
auto sk = SaplingSpendingKey(random_uint256());
if (sk.full_viewing_key().is_valid()) {
return sk;
}
}
}
boost::optional<SaplingPaymentAddress> SaplingIncomingViewingKey::address(diversifier_t d) const {
uint256 pk_d;
if (librustzcash_check_diversifier(d.data())) {
librustzcash_ivk_to_pkd(this->begin(), d.data(), pk_d.begin());
return SaplingPaymentAddress(d, pk_d);
} else {
return boost::none;
}
}
SaplingPaymentAddress SaplingSpendingKey::default_address() const {
// Iterates within default_diversifier to ensure a valid address is returned
auto addrOpt = full_viewing_key().in_viewing_key().address(default_diversifier(*this));
assert(addrOpt != boost::none);
return addrOpt.value();
}
}
class IsValidAddressForNetwork : public boost::static_visitor<bool> {
private:
uint32_t branchId;
public:
IsValidAddressForNetwork(uint32_t consensusBranchId) : branchId(consensusBranchId) {}
bool operator()(const libzcash::InvalidEncoding &addr) const {
return false;
}
bool operator()(const libzcash::SaplingPaymentAddress &addr) const {
if (SAPLING_BRANCH_ID == branchId)
return true;
else
return false;
}
};
bool IsValidPaymentAddress(const libzcash::PaymentAddress& zaddr, uint32_t consensusBranchId) {
return boost::apply_visitor(IsValidAddressForNetwork(consensusBranchId), zaddr);
}
bool IsValidViewingKey(const libzcash::ViewingKey& vk) {
return vk.which() != 0;
}