Skip to content

Commit b1eae99

Browse files
achow101vijaydasmp
authored andcommitted
Merge bitcoin#28455: refactor: share and use GenerateRandomKey helper
fa1d495 refactor: share and use `GenerateRandomKey` helper (Sebastian Falbesoner) Pull request description: Making the `GeneratingRandomKey` helper (recently introduced in PR bitcoin#28433, commit b6934fd) available to other modules via key.{h.cpp} allows us to create random private keys directly at CKey instantiation, in contrast to the currently needed two-step process of creating an (invalid) CKey instance first and then having to call `MakeNewKey(...)`. This is mostly used in unit tests and a few instances in the wallet. ACKs for top commit: Sjors: re-ACK fa1d495 achow101: ACK fa1d495 sipa: utACK fa1d495 kristapsk: cr utACK fa1d495 stratospher: ACK fa1d495. Tree-SHA512: 6fec73f33efe5bd77ca7d3c2fc06725d96f789f229294c39377e682ff222cfc7990b77c92e0bfd4cb6cf891d007ab1f86d395907511f06e87044bae37652a2fd
1 parent 3fa318c commit b1eae99

14 files changed

+46
-74
lines changed

src/bench/ellswift.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ static void EllSwiftCreate(benchmark::Bench& bench)
1111
{
1212
ECC_Start();
1313

14-
CKey key;
15-
key.MakeNewKey(true);
16-
14+
CKey key = GenerateRandomKey();
1715
uint256 entropy = GetRandHash();
1816

1917
bench.batch(1).unit("pubkey").run([&] {

src/key.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, c
340340
return output;
341341
}
342342

343+
CKey GenerateRandomKey(bool compressed) noexcept
344+
{
345+
CKey key;
346+
key.MakeNewKey(/*fCompressed=*/compressed);
347+
return key;
348+
}
349+
343350
bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
344351
if (nDepth == std::numeric_limits<unsigned char>::max()) return false;
345352
out.nDepth = nDepth + 1;
@@ -391,8 +398,7 @@ void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
391398
}
392399

393400
bool ECC_InitSanityCheck() {
394-
CKey key;
395-
key.MakeNewKey(true);
401+
CKey key = GenerateRandomKey();
396402
CPubKey pubkey = key.GetPubKey();
397403
return key.VerifyPubKey(pubkey);
398404
}

src/key.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class CKey
168168
bool initiating) const;
169169
};
170170

171+
CKey GenerateRandomKey(bool compressed = true) noexcept;
172+
171173
struct CExtKey {
172174
unsigned char nDepth;
173175
unsigned char vchFingerprint[4];

src/net.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <node/eviction.h>
2222
#include <fs.h>
2323
#include <i2p.h>
24+
#include <key.h>
2425
#include <memusage.h>
2526
#include <net_permissions.h>
2627
#include <netaddress.h>
@@ -1057,13 +1058,6 @@ class V2MessageMap
10571058

10581059
const V2MessageMap V2_MESSAGE_MAP;
10591060

1060-
CKey GenerateRandomKey() noexcept
1061-
{
1062-
CKey key;
1063-
key.MakeNewKey(/*fCompressed=*/true);
1064-
return key;
1065-
}
1066-
10671061
std::vector<uint8_t> GenerateRandomGarbage() noexcept
10681062
{
10691063
std::vector<uint8_t> ret;

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
8181
}
8282

8383
auto build_address = [wallet]() {
84-
CKey key;
85-
key.MakeNewKey(true);
84+
CKey key = GenerateRandomKey();
8685
CTxDestination dest = PKHash(key.GetPubKey());
8786

8887
return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest)));

src/test/blockfilter_index_tests.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
161161
LOCK(cs_main);
162162
tip = m_node.chainman->ActiveChain().Tip();
163163
}
164-
CKey coinbase_key_A, coinbase_key_B;
165-
coinbase_key_A.MakeNewKey(true);
166-
coinbase_key_B.MakeNewKey(true);
164+
CKey coinbase_key_A = GenerateRandomKey();
165+
CKey coinbase_key_B = GenerateRandomKey();
167166
CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
168167
CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
169168
std::vector<std::shared_ptr<CBlock>> chainA, chainB;

src/test/compress_tests.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ BOOST_AUTO_TEST_CASE(compress_amounts)
6565
BOOST_AUTO_TEST_CASE(compress_script_to_ckey_id)
6666
{
6767
// case CKeyID
68-
CKey key;
69-
key.MakeNewKey(true);
68+
CKey key = GenerateRandomKey();
7069
CPubKey pubkey = key.GetPubKey();
7170

7271
CScript script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG;
@@ -101,8 +100,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_cscript_id)
101100

102101
BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id)
103102
{
104-
CKey key;
105-
key.MakeNewKey(true); // case compressed PubKeyID
103+
CKey key = GenerateRandomKey(); // case compressed PubKeyID
106104

107105
CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // COMPRESSED_PUBLIC_KEY_SIZE (33)
108106
BOOST_CHECK_EQUAL(script.size(), 35U);
@@ -119,8 +117,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id)
119117

120118
BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id)
121119
{
122-
CKey key;
123-
key.MakeNewKey(false); // case uncompressed PubKeyID
120+
CKey key = GenerateRandomKey(/*compressed=*/false); // case uncompressed PubKeyID
124121
CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // PUBLIC_KEY_SIZE (65)
125122
BOOST_CHECK_EQUAL(script.size(), 67U); // 1 char code + 65 char pubkey + OP_CHECKSIG
126123

src/test/script_standard_tests.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_success)
9696

9797
BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
9898
{
99-
CKey key;
100-
CPubKey pubkey;
101-
key.MakeNewKey(true);
102-
pubkey = key.GetPubKey();
99+
CKey key = GenerateRandomKey();
100+
CPubKey pubkey = key.GetPubKey();
103101

104102
CScript s;
105103
std::vector<std::vector<unsigned char> > solutions;
@@ -147,10 +145,8 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
147145

148146
BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
149147
{
150-
CKey key;
151-
CPubKey pubkey;
152-
key.MakeNewKey(true);
153-
pubkey = key.GetPubKey();
148+
CKey key = GenerateRandomKey();
149+
CPubKey pubkey = key.GetPubKey();
154150

155151
CScript s;
156152
CTxDestination address;

src/test/script_tests.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,9 @@ sign_multisig(const CScript& scriptPubKey, const CKey& key, const CTransaction&
987987
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
988988
{
989989
ScriptError err;
990-
CKey key1, key2, key3;
991-
key1.MakeNewKey(true);
992-
key2.MakeNewKey(false);
993-
key3.MakeNewKey(true);
990+
CKey key1 = GenerateRandomKey();
991+
CKey key2 = GenerateRandomKey(/*compressed=*/false);
992+
CKey key3 = GenerateRandomKey();
994993

995994
CScript scriptPubKey12;
996995
scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
@@ -1017,11 +1016,10 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
10171016
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
10181017
{
10191018
ScriptError err;
1020-
CKey key1, key2, key3, key4;
1021-
key1.MakeNewKey(true);
1022-
key2.MakeNewKey(false);
1023-
key3.MakeNewKey(true);
1024-
key4.MakeNewKey(false);
1019+
CKey key1 = GenerateRandomKey();
1020+
CKey key2 = GenerateRandomKey(/*compressed=*/false);
1021+
CKey key3 = GenerateRandomKey();
1022+
CKey key4 = GenerateRandomKey(/*compressed=*/false);
10251023

10261024
CScript scriptPubKey23;
10271025
scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
@@ -1101,8 +1099,7 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
11011099
std::vector<CPubKey> pubkeys;
11021100
for (int i = 0; i < 3; i++)
11031101
{
1104-
CKey key;
1105-
key.MakeNewKey(i%2 == 1);
1102+
CKey key = GenerateRandomKey(/*compressed=*/i%2 == 1);
11061103
keys.push_back(key);
11071104
pubkeys.push_back(key.GetPubKey());
11081105
BOOST_CHECK(keystore.AddKey(key));

src/test/sigopcount_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount)
4646
std::vector<CPubKey> keys;
4747
for (int i = 0; i < 3; i++)
4848
{
49-
CKey k;
50-
k.MakeNewKey(true);
49+
CKey k = GenerateRandomKey();
5150
keys.push_back(k.GetPubKey());
5251
}
5352
CScript s2 = GetScriptForMultisig(1, keys);

0 commit comments

Comments
 (0)