Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/delegationbuilder.cpp
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/delegationbuilder.h> | #include <avalanche/delegationbuilder.h> | ||||
#include <key.h> | #include <key.h> | ||||
namespace avalanche { | namespace avalanche { | ||||
DelegationBuilder::DelegationBuilder(const Proof &p) | DelegationBuilder::DelegationBuilder(const Proof &p) | ||||
: proofid(p.getId()), dgid(proofid) { | : proofid(p.getId()), dgid(proofid) { | ||||
levels.push_back({p.getMaster(), {}}); | levels.push_back({p.getMaster(), {}}); | ||||
} | } | ||||
bool DelegationBuilder::importDelegation(const Delegation &d) { | bool DelegationBuilder::importDelegation(const Delegation &d, | ||||
const CPubKey &pubkey) { | |||||
if (d.getProofId() != proofid) { | if (d.getProofId() != proofid) { | ||||
return false; | return false; | ||||
} | } | ||||
if (levels.size() > 1) { | if (levels.size() > 1) { | ||||
// We already imported a delegation | // We already imported a delegation | ||||
return false; | return false; | ||||
} | } | ||||
if (!d.levels.size()) { | if (!d.levels.size()) { | ||||
return true; | return true; | ||||
} | } | ||||
dgid = d.getId(); | |||||
for (auto &l : d.levels) { | for (auto &l : d.levels) { | ||||
levels.back().sig = l.sig; | levels.back().sig = l.sig; | ||||
levels.push_back({l.pubkey, {}}); | levels.push_back({l.pubkey, {}}); | ||||
if (l.pubkey == pubkey) { | |||||
break; | |||||
} | |||||
} | } | ||||
// Compute the delegation id. The one from the imported delegation might | |||||
// differ if some levels got stripped. | |||||
dgid = build().computeDelegationId(); | |||||
return true; | return true; | ||||
} | } | ||||
bool DelegationBuilder::addLevel(const CKey &key, const CPubKey &master) { | bool DelegationBuilder::addLevel(const CKey &key, const CPubKey &master) { | ||||
// Ensures that the private key provided is the one we need. | // Ensures that the private key provided is the one we need. | ||||
if (levels.back().pubkey != key.GetPubKey()) { | if (levels.back().pubkey != key.GetPubKey()) { | ||||
return false; | return false; | ||||
} | } | ||||
Show All 25 Lines |