Skip to content

Commit 829d3e0

Browse files
committed
Merge bitcoin/bitcoin#23199: refactor: use {Read,Write}BE32 helpers for BIP32 nChild (de)serialization
7fc487a refactor: use `{Read,Write}BE32` helpers for BIP32 nChild (de)serialization (Sebastian Falbesoner) Pull request description: This small refactoring PR replaces manual bit-fiddling (de)serialization of the BIP32 child number (nChild) by the helpers `ReadBE32`/`WriteBE32`. Note that those were first introduced in #4100, almost one year _after_ the BIP32 derivation implementation has been merged (#2829, eb2c999). ACKs for top commit: sipa: utACK 7fc487a laanwj: Code review ACK 7fc487a Tree-SHA512: bbe3e411fb0429fa74c8a5705a91f4d6ed704dac9d6623ecb633563f22acf8e21f3189a16f1d0cf1aeedfc56a5b695df54ae51e9577e34eb6d7dc335de2da6de
2 parents 01129ca + 7fc487a commit 829d3e0

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed

src/hash.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vData
7575
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
7676
{
7777
unsigned char num[4];
78-
num[0] = (nChild >> 24) & 0xFF;
79-
num[1] = (nChild >> 16) & 0xFF;
80-
num[2] = (nChild >> 8) & 0xFF;
81-
num[3] = (nChild >> 0) & 0xFF;
78+
WriteBE32(num, nChild);
8279
CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
8380
}
8481

src/key.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ CExtPubKey CExtKey::Neuter() const {
343343
void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
344344
code[0] = nDepth;
345345
memcpy(code+1, vchFingerprint, 4);
346-
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
347-
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
346+
WriteBE32(code+5, nChild);
348347
memcpy(code+9, chaincode.begin(), 32);
349348
code[41] = 0;
350349
assert(key.size() == 32);
@@ -354,7 +353,7 @@ void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
354353
void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
355354
nDepth = code[0];
356355
memcpy(vchFingerprint, code+1, 4);
357-
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
356+
nChild = ReadBE32(code+5);
358357
memcpy(chaincode.begin(), code+9, 32);
359358
key.Set(code+42, code+BIP32_EXTKEY_SIZE, true);
360359
if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || code[41] != 0) key = CKey();

src/pubkey.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
337337
void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
338338
code[0] = nDepth;
339339
memcpy(code+1, vchFingerprint, 4);
340-
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
341-
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
340+
WriteBE32(code+5, nChild);
342341
memcpy(code+9, chaincode.begin(), 32);
343342
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
344343
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
@@ -347,7 +346,7 @@ void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
347346
void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
348347
nDepth = code[0];
349348
memcpy(vchFingerprint, code+1, 4);
350-
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
349+
nChild = ReadBE32(code+5);
351350
memcpy(chaincode.begin(), code+9, 32);
352351
pubkey.Set(code+41, code+BIP32_EXTKEY_SIZE);
353352
if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || !pubkey.IsFullyValid()) pubkey = CPubKey();

0 commit comments

Comments
 (0)