From 984f58d4df270b00b97346955e90a46f0a705fd6 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Mon, 1 Nov 2021 13:59:15 +0100 Subject: [PATCH] merge bitcoin#23409: Take Span in SetSeed --- src/hdchain.cpp | 4 ++-- src/key.cpp | 5 +++-- src/key.h | 3 ++- src/test/bip32_tests.cpp | 2 +- src/test/bip39_tests.cpp | 2 +- src/test/key_io_tests.cpp | 2 +- src/wallet/rpcdump.cpp | 2 +- src/wallet/scriptpubkeyman.cpp | 2 +- 8 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/hdchain.cpp b/src/hdchain.cpp index 43e7b397fe..811d8d2f73 100644 --- a/src/hdchain.cpp +++ b/src/hdchain.cpp @@ -55,7 +55,7 @@ void CHDChain::Debug(const std::string& strName) const std::cout << "seed: " << HexStr(vchSeed).c_str() << std::endl; CExtKey extkey; - extkey.SetSeed(vchSeed.data(), vchSeed.size()); + extkey.SetSeed(vchSeed); std::cout << "extended private masterkey: " << EncodeExtKey(extkey).c_str() << std::endl; @@ -167,7 +167,7 @@ void CHDChain::DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_ CExtKey changeKey; //key at m/purpose'/coin_type'/account'/change CExtKey childKey; //key at m/purpose'/coin_type'/account'/change/address_index - masterKey.SetSeed(vchSeed.data(), vchSeed.size()); + masterKey.SetSeed(vchSeed); // Use hardened derivation for purpose, coin_type and account // (keys >= 0x80000000 are hardened after bip32) diff --git a/src/key.cpp b/src/key.cpp index 04959dc74a..f97de1e8d9 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -298,10 +298,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 seed) +{ static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'}; std::vector> 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 f1b3099b2f..802134c6d9 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(); } @@ -160,7 +161,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 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 32329eb510..00e3160048 100644 --- a/src/test/bip32_tests.cpp +++ b/src/test/bip32_tests.cpp @@ -91,7 +91,7 @@ static void RunTest(const TestVector &test) { std::vector 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/bip39_tests.cpp b/src/test/bip39_tests.cpp index a26e98eace..c35b00c4ea 100644 --- a/src/test/bip39_tests.cpp +++ b/src/test/bip39_tests.cpp @@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(bip39_vectors) CExtKey key; CExtPubKey pubkey; - key.SetSeed(seed.data(), 64); + key.SetSeed(seed); pubkey = key.Neuter(); // printf("CBitcoinExtKey: %s\n", EncodeExtKey(key).c_str()); diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index f149b38b06..5d0ef030a3 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -47,7 +47,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{privkey} == Span{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 7dd486c71c..16246a25d6 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -982,7 +982,7 @@ UniValue dumpwallet(const JSONRPCRequest& request) file << "# HD seed: " << HexStr(vchSeed) << "\n\n"; CExtKey masterKey; - masterKey.SetSeed(&vchSeed[0], vchSeed.size()); + masterKey.SetSeed(vchSeed); file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n"; diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 0a6f234e7b..7260aae0b7 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -323,7 +323,7 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata() CExtKey masterKey; SecureVector vchSeed = hdChainCurrent.GetSeed(); - masterKey.SetSeed(vchSeed.data(), vchSeed.size()); + masterKey.SetSeed(vchSeed); CKeyID master_id = masterKey.key.GetPubKey().GetID(); std::unique_ptr batch = std::make_unique(m_storage.GetDatabase());