aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-11-01 13:59:15 +0100
committerMarcoFalke <falke.marco@gmail.com>2021-11-01 14:20:56 +0100
commitfa93ef5a8aeae36304c792697a78af2d07fd9f41 (patch)
tree15a20252f07a2c95b9fb52be37a0369a6f5c4c80
parent5574881ce329f91cc5bbc2b9585860a45fde7c3c (diff)
downloadbitcoin-fa93ef5a8aeae36304c792697a78af2d07fd9f41.tar.xz
refactor: Take Span in SetSeed
This makes calling code less verbose and less fragile. Also, by adding the CKey::data() member function, it is now possible to call HexStr() with a CKey object.
-rw-r--r--src/key.cpp5
-rw-r--r--src/key.h3
-rw-r--r--src/test/bip32_tests.cpp2
-rw-r--r--src/test/key_io_tests.cpp2
-rw-r--r--src/wallet/rpcdump.cpp2
-rw-r--r--src/wallet/scriptpubkeyman.cpp4
-rw-r--r--src/wallet/wallet.cpp2
7 files changed, 11 insertions, 9 deletions
diff --git a/src/key.cpp b/src/key.cpp
index 39155e4311..2e42c0718d 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -319,10 +319,11 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
return key.Derive(out.key, out.chaincode, _nChild, chaincode);
}
-void CExtKey::SetSeed(const unsigned char *seed, unsigned int nSeedLen) {
+void CExtKey::SetSeed(Span<const uint8_t> seed)
+{
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
- CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(vout.data());
+ CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
key.Set(vout.data(), vout.data() + 32, true);
memcpy(chaincode.begin(), vout.data() + 32, 32);
nDepth = 0;
diff --git a/src/key.h b/src/key.h
index 9b94baa026..af8d2e72d8 100644
--- a/src/key.h
+++ b/src/key.h
@@ -85,6 +85,7 @@ public:
//! Simple read-only vector-like interface.
unsigned int size() const { return (fValid ? keydata.size() : 0); }
+ const unsigned char* data() const { return keydata.data(); }
const unsigned char* begin() const { return keydata.data(); }
const unsigned char* end() const { return keydata.data() + size(); }
@@ -177,7 +178,7 @@ struct CExtKey {
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
bool Derive(CExtKey& out, unsigned int nChild) const;
CExtPubKey Neuter() const;
- void SetSeed(const unsigned char* seed, unsigned int nSeedLen);
+ void SetSeed(Span<const uint8_t> seed);
};
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
diff --git a/src/test/bip32_tests.cpp b/src/test/bip32_tests.cpp
index a89868e1ef..0fa6b7784f 100644
--- a/src/test/bip32_tests.cpp
+++ b/src/test/bip32_tests.cpp
@@ -124,7 +124,7 @@ void RunTest(const TestVector &test) {
std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
CExtKey key;
CExtPubKey pubkey;
- key.SetSeed(seed.data(), seed.size());
+ key.SetSeed(seed);
pubkey = key.Neuter();
for (const TestDerivation &derive : test.vDerive) {
unsigned char data[74];
diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp
index 8629d13840..0361618c82 100644
--- a/src/test/key_io_tests.cpp
+++ b/src/test/key_io_tests.cpp
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
privkey = DecodeSecret(exp_base58string);
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
- BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
+ BOOST_CHECK_MESSAGE(Span<const uint8_t>{privkey} == Span<const uint8_t>{exp_payload}, "key mismatch:" + strTest);
// Private key must be invalid public key
destination = DecodeDestination(exp_base58string);
diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp
index 1f13b80f3e..5edd9f8f66 100644
--- a/src/wallet/rpcdump.cpp
+++ b/src/wallet/rpcdump.cpp
@@ -797,7 +797,7 @@ RPCHelpMan dumpwallet()
CKey seed;
if (spk_man.GetKey(seed_id, seed)) {
CExtKey masterKey;
- masterKey.SetSeed(seed.begin(), seed.size());
+ masterKey.SetSeed(seed);
file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
}
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp
index 619ebc8b4f..9173c790d4 100644
--- a/src/wallet/scriptpubkeyman.cpp
+++ b/src/wallet/scriptpubkeyman.cpp
@@ -400,7 +400,7 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
CKey key;
GetKey(meta.hd_seed_id, key);
CExtKey masterKey;
- masterKey.SetSeed(key.begin(), key.size());
+ masterKey.SetSeed(key);
// Add to map
CKeyID master_id = masterKey.key.GetPubKey().GetID();
std::copy(master_id.begin(), master_id.begin() + 4, meta.key_origin.fingerprint);
@@ -1085,7 +1085,7 @@ void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata&
if (!GetKey(hd_chain.seed_id, seed))
throw std::runtime_error(std::string(__func__) + ": seed not found");
- masterKey.SetSeed(seed.begin(), seed.size());
+ masterKey.SetSeed(seed);
// derive m/0'
// use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 4eb9d5560d..e917a15bcf 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -3160,7 +3160,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
// Get the extended key
CExtKey master_key;
- master_key.SetSeed(seed_key.begin(), seed_key.size());
+ master_key.SetSeed(seed_key);
for (bool internal : {false, true}) {
for (OutputType t : OUTPUT_TYPES) {