Bitcoin ABC  0.24.10
P2P Digital Currency
proofbuilder.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 <random.h>
8 #include <util/system.h>
9 
10 namespace avalanche {
11 
13  const uint256 h = stake.getHash(commitment);
14 
16  if (!key.SignSchnorr(h, sig)) {
17  sig.fill(0);
18  }
19 
20  return SignedStake(std::move(stake), std::move(sig));
21 }
22 
23 bool ProofBuilder::addUTXO(COutPoint utxo, Amount amount, uint32_t height,
24  bool is_coinbase, CKey key) {
25  if (!key.IsValid()) {
26  return false;
27  }
28 
29  return stakes
30  .emplace(Stake(std::move(utxo), amount, height, is_coinbase,
31  key.GetPubKey()),
32  std::move(key))
33  .second;
34 }
35 
37  SchnorrSig proofSignature;
38  const LimitedProofId limitedProofId = getLimitedProofId();
39  if (!masterKey.SignSchnorr(limitedProofId, proofSignature)) {
40  proofSignature.fill(0);
41  }
42 
43  const ProofId proofid = getProofId();
44 
45  const StakeCommitment commitment(proofid, expirationTime,
47 
48  std::vector<SignedStake> signedStakes;
49  signedStakes.reserve(stakes.size());
50 
51  while (!stakes.empty()) {
52  auto handle = stakes.extract(stakes.begin());
53  signedStakes.push_back(handle.value().sign(commitment));
54  }
55 
56  return std::make_shared<Proof>(
58  std::move(signedStakes), payoutScriptPubKey, std::move(proofSignature));
59 }
60 
62  CHashWriter ss(SER_GETHASH, 0);
63  ss << sequence;
64  ss << expirationTime;
65 
66  if (!Proof::useLegacy(gArgs)) {
67  ss << payoutScriptPubKey;
68  }
69 
70  WriteCompactSize(ss, stakes.size());
71  for (const auto &s : stakes) {
72  ss << s.stake;
73  }
74 
75  return LimitedProofId(ss.GetHash());
76 }
77 
79  CHashWriter ss(SER_GETHASH, 0);
80  ss << getLimitedProofId();
81  ss << masterKey.GetPubKey();
82 
83  return ProofId(ss.GetHash());
84 }
85 
86 } // 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::ProofBuilder::StakeSigner::key
CKey key
Definition: proofbuilder.h:25
avalanche::Stake::getHash
uint256 getHash(const StakeCommitment &commitment) const
Definition: proof.cpp:42
avalanche::ProofBuilder::StakeSigner::sign
SignedStake sign(const StakeCommitment &commitment)
Definition: proofbuilder.cpp:12
avalanche
Definition: avalanche.h:11
avalanche::ProofBuilder::expirationTime
int64_t expirationTime
Definition: proofbuilder.h:19
avalanche::ProofBuilder::getLimitedProofId
LimitedProofId getLimitedProofId() const
Definition: proofbuilder.cpp:61
avalanche::SignedStake
Definition: proof.h:85
avalanche::ProofId
Definition: proofid.h:17
avalanche::ProofBuilder::addUTXO
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
Definition: proofbuilder.cpp:23
proofbuilder.h
avalanche::ProofBuilder::StakeSigner::stake
Stake stake
Definition: proofbuilder.h:24
avalanche::ProofBuilder::build
ProofRef build()
Definition: proofbuilder.cpp:36
avalanche::Stake
Definition: proof.h:50
sig
SchnorrSig sig
Definition: processor.cpp:322
random.h
avalanche::StakeCommitment
Definition: proof.h:43
WriteCompactSize
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1178
avalanche::ProofBuilder::getProofId
ProofId getProofId() const
Definition: proofbuilder.cpp:78
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:94
SchnorrSig
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition: key.h:25
avalanche::ProofBuilder::payoutScriptPubKey
CScript payoutScriptPubKey
Definition: proofbuilder.h:21
avalanche::Proof::useLegacy
static bool useLegacy()
Definition: proof.cpp:53
uint256
256-bit opaque blob.
Definition: uint256.h:127
avalanche::LimitedProofId
Definition: proofid.h:28
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
Amount
Definition: amount.h:19
system.h
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
gArgs
ArgsManager gArgs
Definition: system.cpp:76
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:118
avalanche::ProofBuilder::stakes
std::set< StakeSigner, StakeSignerComparator > stakes
Definition: proofbuilder.h:38
avalanche::ProofBuilder::sequence
uint64_t sequence
Definition: proofbuilder.h:18
avalanche::ProofBuilder::masterKey
CKey masterKey
Definition: proofbuilder.h:20