diff --git a/src/avalanche/proofbuilder.cpp b/src/avalanche/proofbuilder.cpp --- a/src/avalanche/proofbuilder.cpp +++ b/src/avalanche/proofbuilder.cpp @@ -64,12 +64,9 @@ } Proof ProofBuilder::buildRandom(uint32_t score) { - CKey key; - key.MakeNewKey(true); - ProofBuilder pb(0, std::numeric_limits::max(), CPubKey()); pb.addUTXO(COutPoint(TxId(GetRandHash()), 0), (int64_t(score) * COIN) / 100, - 0, false, std::move(key)); + 0, false, CKey::MakeCompressedKey()); return pb.build(); } diff --git a/src/avalanche/test/delegation_tests.cpp b/src/avalanche/test/delegation_tests.cpp --- a/src/avalanche/test/delegation_tests.cpp +++ b/src/avalanche/test/delegation_tests.cpp @@ -31,8 +31,7 @@ } BOOST_AUTO_TEST_CASE(verify_random) { - CKey key; - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const Proof p = buildRandomProof(123456, key.GetPubKey()); DelegationBuilder dgb(p); @@ -43,15 +42,13 @@ CheckDelegation(dg, p, p.getMaster()); } - CKey l1key; - l1key.MakeNewKey(true); + auto l1key = CKey::MakeCompressedKey(); BOOST_CHECK(!dgb.addLevel(l1key, key.GetPubKey())); dgb.addLevel(key, l1key.GetPubKey()); CheckDelegation(dgb.build(), p, l1key.GetPubKey()); - CKey l2key; - l2key.MakeNewKey(true); + auto l2key = CKey::MakeCompressedKey(); BOOST_CHECK(!dgb.addLevel(key, l2key.GetPubKey())); BOOST_CHECK(!dgb.addLevel(l2key, l2key.GetPubKey())); diff --git a/src/avalanche/test/orphanproofpool_tests.cpp b/src/avalanche/test/orphanproofpool_tests.cpp --- a/src/avalanche/test/orphanproofpool_tests.cpp +++ b/src/avalanche/test/orphanproofpool_tests.cpp @@ -22,12 +22,11 @@ static std::shared_ptr makeProof(const size_t nStakes) { const Amount v = 5 * COIN; const int height = 1234; - CKey key; - key.MakeNewKey(true); ProofBuilder pb(0, 0, CPubKey()); for (size_t i = 0; i < nStakes; i++) { TxId txid(GetRandHash()); - pb.addUTXO(COutPoint(txid, 0), v, height, false, key); + pb.addUTXO(COutPoint(txid, 0), v, height, false, + CKey::MakeCompressedKey()); } return std::make_shared(pb.build()); } diff --git a/src/avalanche/test/peermanager_tests.cpp b/src/avalanche/test/peermanager_tests.cpp --- a/src/avalanche/test/peermanager_tests.cpp +++ b/src/avalanche/test/peermanager_tests.cpp @@ -459,8 +459,7 @@ avalanche::PeerManager pm; ProofBuilder pb(0, 0, CPubKey()); - CKey key; - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); COutPoint utxo(TxId(GetRandHash()), 0); Amount amount = 1 * COIN; @@ -523,8 +522,7 @@ } BOOST_AUTO_TEST_CASE(proof_conflict) { - CKey key; - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); TxId txid1(GetRandHash()); @@ -598,8 +596,7 @@ BOOST_AUTO_TEST_CASE(orphan_proofs) { avalanche::PeerManager pm; - CKey key; - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); COutPoint outpoint1 = COutPoint(TxId(GetRandHash()), 0); diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -69,10 +69,12 @@ std::unique_ptr m_processor; + // The master private key we delegate to. CKey masterpriv; AvalancheTestingSetup() - : TestChain100Setup(), config(GetConfig()), masterpriv() { + : TestChain100Setup(), config(GetConfig()), + masterpriv(CKey::MakeCompressedKey()) { // Deterministic randomness for tests. auto connman = std::make_unique(config, 0x1337, 0x1337); m_connman = connman.get(); @@ -87,9 +89,6 @@ m_processor = Processor::MakeProcessor(*m_node.args, *m_node.chain, m_node.connman.get(), error); BOOST_CHECK(m_processor); - - // The master private key we delegate to. - masterpriv.MakeNewKey(true); } ~AvalancheTestingSetup() { diff --git a/src/avalanche/test/proof_tests.cpp b/src/avalanche/test/proof_tests.cpp --- a/src/avalanche/test/proof_tests.cpp +++ b/src/avalanche/test/proof_tests.cpp @@ -38,10 +38,8 @@ } BOOST_AUTO_TEST_CASE(proofbuilder) { - CKey key; - // Master key. - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const CPubKey master = key.GetPubKey(); const uint64_t sequence = InsecureRandBits(64); @@ -328,8 +326,7 @@ CCoinsView coinsDummy; CCoinsViewCache coins(&coinsDummy); - CKey key; - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const CPubKey pubkey = key.GetPubKey(); const Amount value = 12345 * COIN; @@ -398,10 +395,8 @@ // Mismatching key { - CKey altkey; - altkey.MakeNewKey(true); runCheck(ProofValidationResult::DESTINATION_MISMATCH, pkh_outpoint, - value, height, false, altkey); + value, height, false, CKey::MakeCompressedKey()); } // No stake diff --git a/src/avalanche/test/util.cpp b/src/avalanche/test/util.cpp --- a/src/avalanche/test/util.cpp +++ b/src/avalanche/test/util.cpp @@ -16,8 +16,7 @@ namespace avalanche { Proof buildRandomProof(uint32_t score, const CPubKey &master) { - CKey key; - key.MakeNewKey(true); + auto key = CKey::MakeCompressedKey(); const COutPoint o(TxId(GetRandHash()), 0); const Amount v = (int64_t(score) * COIN) / 100; diff --git a/src/key.h b/src/key.h --- a/src/key.h +++ b/src/key.h @@ -61,6 +61,10 @@ // Important: vch must be 32 bytes in length to not break serialization keydata.resize(32); } + //! Produce a valid compressed key + static CKey MakeCompressedKey(); + //! Produce a valid uncompressed key + static CKey MakeUncompressedKey(); friend bool operator==(const CKey &a, const CKey &b) { return a.fCompressed == b.fCompressed && a.size() == b.size() && diff --git a/src/key.cpp b/src/key.cpp --- a/src/key.cpp +++ b/src/key.cpp @@ -456,3 +456,17 @@ secp256k1_context_destroy(ctx); } } + +static CKey validKey(bool compressed) { + CKey ret; + ret.MakeNewKey(compressed); + return ret; +} + +CKey CKey::MakeCompressedKey() { + return validKey(true); +} + +CKey CKey::MakeUncompressedKey() { + return validKey(false); +}