Bitcoin ABC  0.22.15
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 
11 namespace avalanche {
12 
13 template <typename L, typename F>
14 static bool reduceLevels(uint256 &hash, const std::vector<L> &levels, F f) {
15  for (const auto &l : levels) {
16  CHashWriter ss(SER_GETHASH, 0);
17  ss << hash;
18  ss << l.pubkey;
19  hash = ss.GetHash();
20 
21  if (!f(l)) {
22  return false;
23  }
24  }
25 
26  return true;
27 }
28 
29 template <typename L>
30 static bool reduceLevels(uint256 &hash, const std::vector<L> &levels) {
31  return reduceLevels(hash, levels, [](const L &) { return true; });
32 }
33 
35  uint256 hash = proofid;
36  reduceLevels(hash, levels);
37  return DelegationId(hash);
38 }
39 
40 bool Delegation::verify(DelegationState &state, const Proof &proof,
41  CPubKey &auth) const {
42  if (proof.getId() != proofid) {
44  }
45 
46  uint256 hash = proofid;
47  const CPubKey *pauth = &proof.getMaster();
48 
49  bool ret = reduceLevels(hash, levels, [&](const Level &l) {
50  if (!pauth->VerifySchnorr(hash, l.sig)) {
52  }
53 
54  // This key is valid, now up to the next delegation level.
55  pauth = &l.pubkey;
56  return true;
57  });
58 
59  auth = *pauth;
60  return ret;
61 }
62 
63 } // namespace avalanche
std::vector< Level > levels
Definition: delegation.h:34
static bool reduceLevels(uint256 &hash, const std::vector< L > &levels, F f)
Definition: delegation.cpp:14
const CPubKey & getMaster() const
Definition: proof.h:93
bool verify(DelegationState &state, const Proof &proof, CPubKey &auth) const
Definition: delegation.cpp:40
bool VerifySchnorr(const uint256 &hash, const std::array< uint8_t, SCHNORR_SIZE > &sig) const
Verify a Schnorr signature (=64 bytes).
Definition: pubkey.cpp:200
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:98
An encapsulated public key.
Definition: pubkey.h:31
DelegationId computeDelegationId() const
Definition: delegation.cpp:34
uint256 GetHash()
Definition: hash.h:118
const ProofId & getId() const
Definition: proof.h:96
256-bit opaque blob.
Definition: uint256.h:123
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99