Bitcoin ABC  0.26.3
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 
12 bool ProofBuilder::addUTXO(COutPoint utxo, Amount amount, uint32_t height,
13  bool is_coinbase, CKey key) {
14  if (!key.IsValid()) {
15  return false;
16  }
17 
19  auto stake =
20  Stake(std::move(utxo), amount, height, is_coinbase, key.GetPubKey());
21  const uint256 h = stake.getHash(commitment);
23  if (!key.SignSchnorr(h, sig)) {
24  sig.fill(0);
25  }
26 
27  return stakes.emplace(std::move(stake), std::move(sig)).second;
28 }
29 
31  SchnorrSig proofSignature;
32  const LimitedProofId limitedProofId = getLimitedProofId();
33  if (!masterKey.SignSchnorr(limitedProofId, proofSignature)) {
34  proofSignature.fill(0);
35  }
36  std::vector<SignedStake> signedStakes;
37  signedStakes.reserve(stakes.size());
38 
39  while (!stakes.empty()) {
40  auto handle = stakes.extract(stakes.begin());
41  signedStakes.push_back(handle.value());
42  }
43 
45  std::move(signedStakes), payoutScriptPubKey,
46  std::move(proofSignature));
47 }
48 
50  CHashWriter ss(SER_GETHASH, 0);
51  ss << sequence;
52  ss << expirationTime;
53  ss << payoutScriptPubKey;
54 
55  WriteCompactSize(ss, stakes.size());
56  for (const auto &s : stakes) {
57  ss << s.getStake();
58  }
59 
60  return LimitedProofId(ss.GetHash());
61 }
62 
64  CHashWriter ss(SER_GETHASH, 0);
65  ss << getLimitedProofId();
66  ss << masterKey.GetPubKey();
67 
68  return ProofId(ss.GetHash());
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
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:94
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 outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
static RCUPtr make(Args &&...args)
Construct a new object that is owned by the pointer.
Definition: rcu.h:112
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
std::set< SignedStake, SignedStakeComparator > stakes
Definition: proofbuilder.h:28
ProofId getProofId() const
LimitedProofId getLimitedProofId() const
256-bit opaque blob.
Definition: uint256.h:127
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition: key.h:25
SchnorrSig sig
Definition: processor.cpp:491
@ SER_GETHASH
Definition: serialize.h:168
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1272
Definition: amount.h:19