Bitcoin ABC  0.29.2
P2P Digital Currency
delegationbuilder.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>
8 #include <avalanche/proofid.h>
9 #include <pubkey.h>
10 
11 #include <key.h>
12 
13 namespace avalanche {
14 
16  const CPubKey &proofMaster,
17  const DelegationId &delegationId)
18  : limitedProofid(ltdProofId), dgid(delegationId) {
19  levels.push_back({proofMaster, {}});
20 }
21 
23  const CPubKey &proofMaster)
24  : DelegationBuilder(ltdProofId, proofMaster,
25  DelegationId(ltdProofId.computeProofId(proofMaster))) {}
26 
28  : DelegationBuilder(p.getLimitedId(), p.getMaster(),
29  DelegationId(p.getId())) {}
30 
32  : DelegationBuilder(dg.getLimitedProofId(), dg.getProofMaster(),
33  dg.getId()) {
34  for (auto &l : dg.levels) {
35  levels.back().sig = l.sig;
36  levels.push_back({l.pubkey, {}});
37  }
38 }
39 
40 bool DelegationBuilder::addLevel(const CKey &delegatorKey,
41  const CPubKey &delegatedPubKey) {
42  // Ensures that the private key provided is the one we need.
43  if (levels.back().pubkey != delegatorKey.GetPubKey()) {
44  return false;
45  }
46 
47  CHashWriter ss(SER_GETHASH, 0);
48  ss << dgid;
49  ss << delegatedPubKey;
50  auto hash = ss.GetHash();
51 
52  if (!delegatorKey.SignSchnorr(hash, levels.back().sig)) {
53  return false;
54  }
55 
56  dgid = DelegationId(hash);
57  levels.push_back({delegatedPubKey, {}});
58  return true;
59 }
60 
62  std::vector<Delegation::Level> dglvls;
63  for (size_t i = 1; i < levels.size(); i++) {
64  dglvls.push_back({levels[i].pubkey, levels[i - 1].sig});
65  }
66 
67  return Delegation(limitedProofid, levels[0].pubkey, dgid,
68  std::move(dglvls));
69 }
70 
71 } // namespace avalanche
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:122
An encapsulated secp256k1 private key.
Definition: key.h:28
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
bool SignSchnorr(const uint256 &hash, SchnorrSig &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition: key.cpp:288
An encapsulated public key.
Definition: pubkey.h:31
std::vector< Delegation::Level > levels
bool addLevel(const CKey &delegatorKey, const CPubKey &delegatedPubKey)
DelegationBuilder(const LimitedProofId &ltdProofId, const CPubKey &proofMaster, const DelegationId &delegationId)
std::vector< Level > levels
Definition: delegation.h:45
@ SER_GETHASH
Definition: serialize.h:154