Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
delegation.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6
7#include <avalanche/proof.h>
9#include <hash.h>
10#include <streams.h>
11#include <util/strencodings.h>
12#include <util/translation.h>
13
14namespace avalanche {
15
16bool Delegation::FromHex(Delegation &dg, const std::string &dgHex,
18 if (!IsHex(dgHex)) {
19 errorOut = _("Delegation must be an hexadecimal string.");
20 return false;
21 }
22
24
25 try {
26 ss >> dg;
27 } catch (std::exception &e) {
28 errorOut = strprintf(_("Delegation has invalid format: %s"), e.what());
29 return false;
30 }
31
32 return true;
33}
34
35template <typename L, typename F>
36static bool reduceLevels(uint256 &hash, const std::vector<L> &levels, F f) {
37 for (const auto &l : levels) {
38 HashWriter ss{};
39 ss << hash;
40 ss << l.pubkey;
41 hash = ss.GetHash();
42
43 if (!f(l)) {
44 return false;
45 }
46 }
47
48 return true;
49}
50
51template <typename L>
52static bool reduceLevels(uint256 &hash, const std::vector<L> &levels) {
53 return reduceLevels(hash, levels, [](const L &) { return true; });
54}
55
59
61 if (!levels.empty()) {
62 return levels.back().pubkey;
63 }
64 return proofMaster;
65}
66
68 uint256 hash = getProofId();
69 reduceLevels(hash, levels);
70 return DelegationId(hash);
71}
72
74 uint256 hash = getProofId();
75 const CPubKey *pauth = &proofMaster;
76
77 if (levels.size() > MAX_DELEGATION_LEVELS) {
79 "too-many-levels");
80 }
81
82 bool ret = reduceLevels(hash, levels, [&](const Level &l) {
83 if (!pauth->VerifySchnorr(hash, l.sig)) {
84 return state.Invalid(DelegationResult::INVALID_SIGNATURE,
85 "invalid-signature");
86 }
87
88 // This key is valid, now up to the next delegation level.
89 pauth = &l.pubkey;
90 return true;
91 });
92
93 auth = *pauth;
94 return ret;
95}
96
97} // namespace avalanche
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:177
An encapsulated public key.
Definition pubkey.h:31
A writer stream (for serialization) that computes a 256-bit hash.
Definition hash.h:99
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition validation.h:101
ProofId getProofId() const
static bool FromHex(Delegation &dg, const std::string &dgHex, bilingual_str &errorOut)
bool verify(DelegationState &state, CPubKey &auth) const
std::vector< Level > levels
Definition delegation.h:45
LimitedProofId limitedProofid
Definition delegation.h:32
const CPubKey & getDelegatedPubkey() const
DelegationId computeDelegationId() const
256-bit opaque blob.
Definition uint256.h:129
constexpr size_t MAX_DELEGATION_LEVELS
The maximum number of delegation levels we are willing to verify.
Definition delegation.h:26
static bool reduceLevels(uint256 &hash, const std::vector< L > &levels, F f)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
@ SER_NETWORK
Definition serialize.h:152
std::vector< uint8_t > ParseHex(const char *psz)
bool IsHex(const std::string &str)
Returns true if each character in str is a hex character, and has an even number of hex digits.
ProofId computeProofId(const CPubKey &proofMaster) const
Definition proofid.cpp:12
Bilingual messages:
Definition translation.h:17
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
bilingual_str _(const char *psz)
Translation function.
Definition translation.h:68
static const int PROTOCOL_VERSION
network protocol versioning
Definition version.h:11