Bitcoin ABC  0.22.13
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 
9 namespace avalanche {
10 
12  const uint256 h = stake.getHash(proofid);
13 
14  std::array<uint8_t, 64> sig;
15  if (!key.SignSchnorr(h, sig)) {
16  sig.fill(0);
17  }
18 
19  return SignedStake(std::move(stake), std::move(sig));
20 }
21 
22 bool ProofBuilder::addUTXO(COutPoint utxo, Amount amount, uint32_t height,
23  bool is_coinbase, CKey key) {
24  if (!key.IsValid()) {
25  return false;
26  }
27 
28  stakes.emplace_back(
29  Stake(std::move(utxo), amount, height, is_coinbase, key.GetPubKey()),
30  std::move(key));
31  return true;
32 }
33 
35  const ProofId proofid = getProofId();
36 
37  std::vector<SignedStake> signedStakes;
38  signedStakes.reserve(stakes.size());
39 
40  for (auto &s : stakes) {
41  signedStakes.push_back(s.sign(proofid));
42  }
43 
44  stakes.clear();
45  return Proof(sequence, expirationTime, std::move(master),
46  std::move(signedStakes));
47 }
48 
50  CHashWriter ss(SER_GETHASH, 0);
51  ss << sequence;
52  ss << expirationTime;
53  ss << master;
54 
55  WriteCompactSize(ss, stakes.size());
56  for (const auto &s : stakes) {
57  ss << s.stake;
58  }
59 
60  return ProofId(ss.GetHash());
61 }
62 
64  CKey key;
65  key.MakeNewKey(true);
66 
67  ProofBuilder pb(0, std::numeric_limits<uint32_t>::max(), CPubKey());
68  pb.addUTXO(COutPoint(TxId(GetRandHash()), 0), (int64_t(score) * COIN) / 100,
69  0, false, std::move(key));
70  return pb.build();
71 }
72 
73 } // namespace avalanche
std::array< uint8_t, 64 > sig
Definition: processor.cpp:248
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1189
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
uint256 GetRandHash() noexcept
Definition: random.cpp:659
ProofId getProofId() const
Definition: amount.h:17
static Proof buildRandom(uint32_t score)
Builds a randomized (and therefore invalid) Proof.
uint256 getHash(const ProofId &proofid) const
Definition: proof.cpp:16
static constexpr Amount COIN
Definition: amount.h:153
An encapsulated public key.
Definition: pubkey.h:31
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint256 GetHash()
Definition: hash.h:139
256-bit opaque blob.
Definition: uint256.h:120
std::vector< StakeSigner > stakes
Definition: proofbuilder.h:30
SignedStake sign(const ProofId &proofid)
A TxId is the identifier of a transaction.
Definition: txid.h:14
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:120
An encapsulated secp256k1 private key.
Definition: key.h:25
bool SignSchnorr(const uint256 &hash, std::array< uint8_t, CPubKey::SCHNORR_SIZE > &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition: key.cpp:288
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:87