Bitcoin ABC  0.24.7
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 
5 #include <avalanche/delegation.h>
6 
7 #include <avalanche/proof.h>
8 #include <avalanche/validation.h>
9 #include <hash.h>
10 #include <streams.h>
11 #include <util/strencodings.h>
12 #include <util/translation.h>
13 
14 namespace avalanche {
15 
16 bool 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 
35 template <typename L, typename F>
36 static bool reduceLevels(uint256 &hash, const std::vector<L> &levels, F f) {
37  for (const auto &l : levels) {
38  CHashWriter ss(SER_GETHASH, 0);
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 
51 template <typename L>
52 static 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 
73 bool Delegation::verify(DelegationState &state, CPubKey &auth) const {
74  uint256 hash = getProofId();
75  const CPubKey *pauth = &proofMaster;
76 
77  bool ret = reduceLevels(hash, levels, [&](const Level &l) {
78  if (!pauth->VerifySchnorr(hash, l.sig)) {
79  return state.Invalid(DelegationResult::INVALID_SIGNATURE,
80  "invalid-signature");
81  }
82 
83  // This key is valid, now up to the next delegation level.
84  pauth = &l.pubkey;
85  return true;
86  });
87 
88  auth = *pauth;
89  return ret;
90 }
91 
92 } // namespace avalanche
avalanche::reduceLevels
static bool reduceLevels(uint256 &hash, const std::vector< L > &levels, F f)
Definition: delegation.cpp:36
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
avalanche::Delegation
Definition: delegation.h:23
ParseHex
std::vector< uint8_t > ParseHex(const char *psz)
Definition: strencodings.cpp:87
delegation.h
avalanche::DelegationId
Definition: delegationid.h:14
streams.h
avalanche::Delegation::computeDelegationId
DelegationId computeDelegationId() const
Definition: delegation.cpp:67
avalanche
Definition: avalanche.h:11
avalanche::Delegation::Level
Definition: delegation.h:30
avalanche::Delegation::limitedProofid
LimitedProofId limitedProofid
Definition: delegation.h:24
CPubKey::VerifySchnorr
bool VerifySchnorr(const uint256 &hash, const std::array< uint8_t, SCHNORR_SIZE > &sig) const
Verify a Schnorr signature (=64 bytes).
Definition: pubkey.cpp:200
bilingual_str
Bilingual messages:
Definition: translation.h:17
IsHex
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.
Definition: strencodings.cpp:64
avalanche::Delegation::getProofId
ProofId getProofId() const
Definition: delegation.cpp:56
avalanche::ProofId
Definition: proofid.h:17
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:165
validation.h
strencodings.h
avalanche::DelegationState
Definition: validation.h:40
avalanche::Delegation::FromHex
static bool FromHex(Delegation &dg, const std::string &dgHex, bilingual_str &errorOut)
Definition: delegation.cpp:16
avalanche::Delegation::proofMaster
CPubKey proofMaster
Definition: delegation.h:25
avalanche::Delegation::Level::sig
SchnorrSig sig
Definition: delegation.h:32
avalanche::Delegation::verify
bool verify(DelegationState &state, CPubKey &auth) const
Definition: delegation.cpp:73
avalanche::Delegation::Level::pubkey
CPubKey pubkey
Definition: delegation.h:31
uint256
256-bit opaque blob.
Definition: uint256.h:127
avalanche::Delegation::getDelegatedPubkey
const CPubKey & getDelegatedPubkey() const
Definition: delegation.cpp:60
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
translation.h
proof.h
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
avalanche::LimitedProofId::computeProofId
ProofId computeProofId(const CPubKey &proofMaster) const
Definition: proofid.cpp:12
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
avalanche::Delegation::levels
std::vector< Level > levels
Definition: delegation.h:37
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:118
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11