From a69332fd89a5c1e293113b641fbe6b23cf279741 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 25 Jul 2019 11:28:57 -0400 Subject: [PATCH] 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. --- src/pubkey.cpp | 12 ++++++++++++ src/pubkey.h | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/pubkey.cpp b/src/pubkey.cpp index 956ff2b34ac..b7dfb6d83fc 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -352,6 +352,18 @@ void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) { if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || !pubkey.IsFullyValid()) pubkey = CPubKey(); } +void CExtPubKey::EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const +{ + memcpy(code, version, 4); + Encode(&code[4]); +} + +void CExtPubKey::DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) +{ + memcpy(version, code, 4); + Decode(&code[4]); +} + bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const { out.nDepth = nDepth + 1; CKeyID id = pubkey.GetID(); diff --git a/src/pubkey.h b/src/pubkey.h index 2453c92d92d..f37e24bc471 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -17,6 +17,7 @@ #include const unsigned int BIP32_EXTKEY_SIZE = 74; +const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE = 78; /** A reference to a CKey: the Hash160 of its serialized public key */ class CKeyID : public uint160 @@ -283,6 +284,7 @@ public: }; struct CExtPubKey { + unsigned char version[4]; unsigned char nDepth; unsigned char vchFingerprint[4]; unsigned int nChild; @@ -305,6 +307,8 @@ struct CExtPubKey { void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const; void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]); + void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const; + void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]); bool Derive(CExtPubKey& out, unsigned int nChild) const; };