Skip to content

Commit fa93ef5

Browse files
author
MarcoFalke
committed
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.
1 parent 5574881 commit fa93ef5

File tree

7 files changed

+11
-9
lines changed

7 files changed

+11
-9
lines changed

src/key.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,11 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
319319
return key.Derive(out.key, out.chaincode, _nChild, chaincode);
320320
}
321321

322-
void CExtKey::SetSeed(const unsigned char *seed, unsigned int nSeedLen) {
322+
void CExtKey::SetSeed(Span<const uint8_t> seed)
323+
{
323324
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
324325
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
325-
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(vout.data());
326+
CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
326327
key.Set(vout.data(), vout.data() + 32, true);
327328
memcpy(chaincode.begin(), vout.data() + 32, 32);
328329
nDepth = 0;

src/key.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class CKey
8585

8686
//! Simple read-only vector-like interface.
8787
unsigned int size() const { return (fValid ? keydata.size() : 0); }
88+
const unsigned char* data() const { return keydata.data(); }
8889
const unsigned char* begin() const { return keydata.data(); }
8990
const unsigned char* end() const { return keydata.data() + size(); }
9091

@@ -177,7 +178,7 @@ struct CExtKey {
177178
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
178179
bool Derive(CExtKey& out, unsigned int nChild) const;
179180
CExtPubKey Neuter() const;
180-
void SetSeed(const unsigned char* seed, unsigned int nSeedLen);
181+
void SetSeed(Span<const uint8_t> seed);
181182
};
182183

183184
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */

src/test/bip32_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void RunTest(const TestVector &test) {
124124
std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
125125
CExtKey key;
126126
CExtPubKey pubkey;
127-
key.SetSeed(seed.data(), seed.size());
127+
key.SetSeed(seed);
128128
pubkey = key.Neuter();
129129
for (const TestDerivation &derive : test.vDerive) {
130130
unsigned char data[74];

src/test/key_io_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
4646
privkey = DecodeSecret(exp_base58string);
4747
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
4848
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
49-
BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
49+
BOOST_CHECK_MESSAGE(Span<const uint8_t>{privkey} == Span<const uint8_t>{exp_payload}, "key mismatch:" + strTest);
5050

5151
// Private key must be invalid public key
5252
destination = DecodeDestination(exp_base58string);

src/wallet/rpcdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ RPCHelpMan dumpwallet()
797797
CKey seed;
798798
if (spk_man.GetKey(seed_id, seed)) {
799799
CExtKey masterKey;
800-
masterKey.SetSeed(seed.begin(), seed.size());
800+
masterKey.SetSeed(seed);
801801

802802
file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
803803
}

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
400400
CKey key;
401401
GetKey(meta.hd_seed_id, key);
402402
CExtKey masterKey;
403-
masterKey.SetSeed(key.begin(), key.size());
403+
masterKey.SetSeed(key);
404404
// Add to map
405405
CKeyID master_id = masterKey.key.GetPubKey().GetID();
406406
std::copy(master_id.begin(), master_id.begin() + 4, meta.key_origin.fingerprint);
@@ -1085,7 +1085,7 @@ void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata&
10851085
if (!GetKey(hd_chain.seed_id, seed))
10861086
throw std::runtime_error(std::string(__func__) + ": seed not found");
10871087

1088-
masterKey.SetSeed(seed.begin(), seed.size());
1088+
masterKey.SetSeed(seed);
10891089

10901090
// derive m/0'
10911091
// use hardened derivation (child keys >= 0x80000000 are hardened after bip32)

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3160,7 +3160,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
31603160

31613161
// Get the extended key
31623162
CExtKey master_key;
3163-
master_key.SetSeed(seed_key.begin(), seed_key.size());
3163+
master_key.SetSeed(seed_key);
31643164

31653165
for (bool internal : {false, true}) {
31663166
for (OutputType t : OUTPUT_TYPES) {

0 commit comments

Comments
 (0)