Bitcoin ABC  0.24.11
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
CKey::SignSchnorr
bool SignSchnorr(const uint256 &hash, SchnorrSig &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition: key.cpp:288
avalanche::Delegation
Definition: delegation.h:23
avalanche::DelegationId
Definition: delegationid.h:14
proofid.h
avalanche
Definition: avalanche.h:11
avalanche::DelegationBuilder::build
Delegation build() const
Definition: delegationbuilder.cpp:61
delegationbuilder.h
avalanche::DelegationBuilder::dgid
DelegationId dgid
Definition: delegationbuilder.h:22
pubkey.h
avalanche::DelegationBuilder::limitedProofid
LimitedProofId limitedProofid
Definition: delegationbuilder.h:21
avalanche::DelegationBuilder::levels
std::vector< Delegation::Level > levels
Definition: delegationbuilder.h:24
avalanche::DelegationBuilder
Definition: delegationbuilder.h:20
avalanche::LimitedProofId
Definition: proofid.h:28
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
avalanche::Proof
Definition: proof.h:102
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
key.h
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
avalanche::DelegationBuilder::DelegationBuilder
DelegationBuilder(const LimitedProofId &ltdProofId, const CPubKey &proofMaster, const DelegationId &delegationId)
Definition: delegationbuilder.cpp:15
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
proof.h
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
avalanche::Delegation::levels
std::vector< Level > levels
Definition: delegation.h:37
avalanche::DelegationBuilder::addLevel
bool addLevel(const CKey &delegatorKey, const CPubKey &delegatedPubKey)
Definition: delegationbuilder.cpp:40
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:118