Bitcoin ABC 0.30.5
P2P Digital Currency
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,
17 bilingual_str &errorOut) {
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
58}
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
73bool Delegation::verify(DelegationState &state, CPubKey &auth) const {
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
bool VerifySchnorr(const uint256 &hash, const std::array< uint8_t, SCHNORR_SIZE > &sig) const
Verify a Schnorr signature (=64 bytes).
Definition: pubkey.cpp:199
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
Definition: delegation.cpp:56
static bool FromHex(Delegation &dg, const std::string &dgHex, bilingual_str &errorOut)
Definition: delegation.cpp:16
bool verify(DelegationState &state, CPubKey &auth) const
Definition: delegation.cpp:73
std::vector< Level > levels
Definition: delegation.h:45
LimitedProofId limitedProofid
Definition: delegation.h:32
const CPubKey & getDelegatedPubkey() const
Definition: delegation.cpp:60
DelegationId computeDelegationId() const
Definition: delegation.cpp:67
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)
Definition: delegation.cpp:36
@ SER_NETWORK
Definition: serialize.h:152
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...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
template std::vector< std::byte > ParseHex(std::string_view)
bool IsHex(std::string_view str)
Returns true if each character in str is a hex character, and has an even number of hex digits.
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11