merge bitcoin#23409: Take Span in SetSeed

This commit is contained in:
Kittywhiskers Van Gogh 2021-11-01 13:59:15 +01:00 committed by PastaPastaPasta
parent de54b8784c
commit 984f58d4df
8 changed files with 12 additions and 10 deletions

View File

@ -55,7 +55,7 @@ void CHDChain::Debug(const std::string& strName) const
std::cout << "seed: " << HexStr(vchSeed).c_str() << std::endl; std::cout << "seed: " << HexStr(vchSeed).c_str() << std::endl;
CExtKey extkey; CExtKey extkey;
extkey.SetSeed(vchSeed.data(), vchSeed.size()); extkey.SetSeed(vchSeed);
std::cout << "extended private masterkey: " << EncodeExtKey(extkey).c_str() << std::endl; 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 changeKey; //key at m/purpose'/coin_type'/account'/change
CExtKey childKey; //key at m/purpose'/coin_type'/account'/change/address_index 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 // Use hardened derivation for purpose, coin_type and account
// (keys >= 0x80000000 are hardened after bip32) // (keys >= 0x80000000 are hardened after bip32)

View File

@ -298,10 +298,11 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
return key.Derive(out.key, out.chaincode, _nChild, chaincode); 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'}; 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); 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); key.Set(vout.data(), vout.data() + 32, true);
memcpy(chaincode.begin(), vout.data() + 32, 32); memcpy(chaincode.begin(), vout.data() + 32, 32);
nDepth = 0; nDepth = 0;

View File

@ -85,6 +85,7 @@ public:
//! Simple read-only vector-like interface. //! Simple read-only vector-like interface.
unsigned int size() const { return (fValid ? keydata.size() : 0); } 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* begin() const { return keydata.data(); }
const unsigned char* end() const { return keydata.data() + size(); } const unsigned char* end() const { return keydata.data() + size(); }
@ -160,7 +161,7 @@ struct CExtKey {
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]); void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
bool Derive(CExtKey& out, unsigned int nChild) const; bool Derive(CExtKey& out, unsigned int nChild) const;
CExtPubKey Neuter() 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. */ /** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */

View File

@ -91,7 +91,7 @@ static void RunTest(const TestVector &test) {
std::vector<unsigned char> seed = ParseHex(test.strHexMaster); std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
CExtKey key; CExtKey key;
CExtPubKey pubkey; CExtPubKey pubkey;
key.SetSeed(seed.data(), seed.size()); key.SetSeed(seed);
pubkey = key.Neuter(); pubkey = key.Neuter();
for (const TestDerivation &derive : test.vDerive) { for (const TestDerivation &derive : test.vDerive) {
unsigned char data[74]; unsigned char data[74];

View File

@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(bip39_vectors)
CExtKey key; CExtKey key;
CExtPubKey pubkey; CExtPubKey pubkey;
key.SetSeed(seed.data(), 64); key.SetSeed(seed);
pubkey = key.Neuter(); pubkey = key.Neuter();
// printf("CBitcoinExtKey: %s\n", EncodeExtKey(key).c_str()); // printf("CBitcoinExtKey: %s\n", EncodeExtKey(key).c_str());

View File

@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
privkey = DecodeSecret(exp_base58string); privkey = DecodeSecret(exp_base58string);
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + 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 // Private key must be invalid public key
destination = DecodeDestination(exp_base58string); destination = DecodeDestination(exp_base58string);

View File

@ -982,7 +982,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
file << "# HD seed: " << HexStr(vchSeed) << "\n\n"; file << "# HD seed: " << HexStr(vchSeed) << "\n\n";
CExtKey masterKey; CExtKey masterKey;
masterKey.SetSeed(&vchSeed[0], vchSeed.size()); masterKey.SetSeed(vchSeed);
file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n"; file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n";

View File

@ -323,7 +323,7 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
CExtKey masterKey; CExtKey masterKey;
SecureVector vchSeed = hdChainCurrent.GetSeed(); SecureVector vchSeed = hdChainCurrent.GetSeed();
masterKey.SetSeed(vchSeed.data(), vchSeed.size()); masterKey.SetSeed(vchSeed);
CKeyID master_id = masterKey.key.GetPubKey().GetID(); CKeyID master_id = masterKey.key.GetPubKey().GetID();
std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_storage.GetDatabase()); std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_storage.GetDatabase());