merge bitcoin#23595: Add ParseHex<std::byte>() helper

includes:
- facd1fb911abfc595a3484ee53397eff515d4c40

continuation of cf4522f8 in dash#5901
This commit is contained in:
Kittywhiskers Van Gogh 2021-11-08 14:27:35 +01:00
parent c2defe7210
commit aa945c2c1d
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
9 changed files with 14 additions and 13 deletions

View File

@ -348,11 +348,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(Span<const uint8_t> seed) void CExtKey::SetSeed(Span<const std::byte> 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.data(), seed.size()).Finalize(vout.data()); CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(UCharCast(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

@ -91,7 +91,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 std::byte* data() const { return reinterpret_cast<const std::byte*>(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(); }
@ -188,7 +188,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(Span<const uint8_t> seed); void SetSeed(Span<const std::byte> 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

@ -120,11 +120,12 @@ const std::vector<std::string> TEST5 = {
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHL" "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHL"
}; };
void RunTest(const TestVector &test) { void RunTest(const TestVector& test)
std::vector<unsigned char> seed = ParseHex(test.strHexMaster); {
std::vector<std::byte> seed{ParseHex<std::byte>(test.strHexMaster)};
CExtKey key; CExtKey key;
CExtPubKey pubkey; CExtPubKey pubkey;
key.SetSeed(seed); key.SetSeed(MakeByteSpan(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

@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
continue; continue;
} }
std::string exp_base58string = test[0].get_str(); std::string exp_base58string = test[0].get_str();
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str()); const std::vector<std::byte> exp_payload{ParseHex<std::byte>(test[1].get_str())};
const UniValue &metadata = test[2].get_obj(); const UniValue &metadata = test[2].get_obj();
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); bool isPrivkey = find_value(metadata, "isPrivkey").get_bool();
SelectParams(find_value(metadata, "chain").get_str()); SelectParams(find_value(metadata, "chain").get_str());

View File

@ -140,7 +140,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); masterKey.SetSeed(MakeByteSpan(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

@ -1007,7 +1007,7 @@ RPCHelpMan dumpwallet()
file << "# HD seed: " << HexStr(vchSeed) << "\n\n"; file << "# HD seed: " << HexStr(vchSeed) << "\n\n";
CExtKey masterKey; CExtKey masterKey;
masterKey.SetSeed(vchSeed); masterKey.SetSeed(MakeByteSpan(vchSeed));
file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n"; file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n";

View File

@ -350,7 +350,7 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
CExtKey masterKey; CExtKey masterKey;
SecureVector vchSeed = hdChainCurrent.GetSeed(); SecureVector vchSeed = hdChainCurrent.GetSeed();
masterKey.SetSeed(vchSeed); masterKey.SetSeed(MakeByteSpan(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());

View File

@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(bip39_vectors)
CExtKey key; CExtKey key;
CExtPubKey pubkey; CExtPubKey pubkey;
key.SetSeed(seed); key.SetSeed(MakeByteSpan(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

@ -5866,7 +5866,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
// Get the extended key // Get the extended key
CExtKey master_key; CExtKey master_key;
master_key.SetSeed(seed_key); master_key.SetSeed(MakeByteSpan(seed_key));
for (bool internal : {false, true}) { for (bool internal : {false, true}) {
{ // OUTPUT_TYPE is only one: LEGACY { // OUTPUT_TYPE is only one: LEGACY