aboutsummaryrefslogtreecommitdiff
path: root/src/key.h
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2013-08-15 16:40:03 -0700
committerGavin Andresen <gavinandresen@gmail.com>2013-08-15 16:40:03 -0700
commit13d3b1c144756dfe31792209d09f5205f55f95bb (patch)
treef65ecbd27fc74735d1a1016e3c923e39d7b1590a /src/key.h
parent4323bfeafda4a5e0101710d94b518d41819a2325 (diff)
parenta59db19ee3b454680b3bb0aa881e601b9311a9ae (diff)
Merge pull request #2829 from sipa/bip32
BIP 32 derivation + test vectors
Diffstat (limited to 'src/key.h')
-rw-r--r--src/key.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/key.h b/src/key.h
index ce469ad298..75431e944f 100644
--- a/src/key.h
+++ b/src/key.h
@@ -161,6 +161,9 @@ public:
// Turn this public key into an uncompressed public key.
bool Decompress();
+
+ // Derive BIP32 child pubkey.
+ bool Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
};
@@ -201,6 +204,10 @@ public:
UnlockObject(vch);
}
+ friend bool operator==(const CKey &a, const CKey &b) {
+ return a.fCompressed == b.fCompressed && memcmp(&a.vch[0], &b.vch[0], 32);
+ }
+
// Initialize using begin and end iterators to byte data.
template<typename T>
void Set(const T pbegin, const T pend, bool fCompressedIn) {
@@ -251,6 +258,45 @@ public:
// 0x1D = second key with even y, 0x1E = second key with odd y,
// add 0x04 for compressed keys.
bool SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const;
+
+ // Derive BIP32 child key.
+ bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
+};
+
+struct CExtPubKey {
+ unsigned char nDepth;
+ unsigned char vchFingerprint[4];
+ unsigned int nChild;
+ unsigned char vchChainCode[32];
+ CPubKey pubkey;
+
+ friend bool operator==(const CExtPubKey &a, const CExtPubKey &b) {
+ return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
+ memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.pubkey == b.pubkey;
+ }
+
+ void Encode(unsigned char code[74]) const;
+ void Decode(const unsigned char code[74]);
+ bool Derive(CExtPubKey &out, unsigned int nChild) const;
+};
+
+struct CExtKey {
+ unsigned char nDepth;
+ unsigned char vchFingerprint[4];
+ unsigned int nChild;
+ unsigned char vchChainCode[32];
+ CKey key;
+
+ friend bool operator==(const CExtKey &a, const CExtKey &b) {
+ return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
+ memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
+ }
+
+ void Encode(unsigned char code[74]) const;
+ void Decode(const unsigned char code[74]);
+ bool Derive(CExtKey &out, unsigned int nChild) const;
+ CExtPubKey Neuter() const;
+ void SetMaster(const unsigned char *seed, unsigned int nSeedLen);
};
#endif