Skip to content

Commit a69332f

Browse files
committed
Store version bytes and be able to serialize them in CExtPubKey
CExtPubKey does not store the version bytes for the extended public key. We store these so that a CExtPubKey can be serialized and deserialized with the same version bytes.
1 parent 5fdaf6a commit a69332f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/pubkey.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,18 @@ void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
352352
if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || !pubkey.IsFullyValid()) pubkey = CPubKey();
353353
}
354354

355+
void CExtPubKey::EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
356+
{
357+
memcpy(code, version, 4);
358+
Encode(&code[4]);
359+
}
360+
361+
void CExtPubKey::DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
362+
{
363+
memcpy(version, code, 4);
364+
Decode(&code[4]);
365+
}
366+
355367
bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
356368
out.nDepth = nDepth + 1;
357369
CKeyID id = pubkey.GetID();

src/pubkey.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <vector>
1818

1919
const unsigned int BIP32_EXTKEY_SIZE = 74;
20+
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE = 78;
2021

2122
/** A reference to a CKey: the Hash160 of its serialized public key */
2223
class CKeyID : public uint160
@@ -283,6 +284,7 @@ class XOnlyPubKey
283284
};
284285

285286
struct CExtPubKey {
287+
unsigned char version[4];
286288
unsigned char nDepth;
287289
unsigned char vchFingerprint[4];
288290
unsigned int nChild;
@@ -305,6 +307,8 @@ struct CExtPubKey {
305307

306308
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
307309
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
310+
void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const;
311+
void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]);
308312
bool Derive(CExtPubKey& out, unsigned int nChild) const;
309313
};
310314

0 commit comments

Comments
 (0)