Skip to content

Commit

Permalink
Abstract chaincodes into CChainCode
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/key.cpp
#	src/key.h
  • Loading branch information
sipa authored and jonasschnelli committed May 2, 2015
1 parent 90c37bc commit 8cf1485
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
CKeyID id = key.GetPubKey().GetID();
memcpy(&out.vchFingerprint[0], &id, 4);
out.nChild = nChild;
return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
return key.Derive(out.key, out.chaincode.data, nChild, chaincode.data);
}

void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
Expand All @@ -161,7 +161,7 @@ void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
LockObject(out);
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
key.Set(&out[0], &out[32], true);
memcpy(vchChainCode, &out[32], 32);
memcpy(chaincode.data, &out[32], 32);
UnlockObject(out);
nDepth = 0;
nChild = 0;
Expand All @@ -174,7 +174,7 @@ CExtPubKey CExtKey::Neuter() const {
memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
ret.nChild = nChild;
ret.pubkey = key.GetPubKey();
memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
memcpy(&ret.chaincode.data[0], &chaincode.data[0], 32);
return ret;
}

Expand All @@ -183,7 +183,7 @@ void CExtKey::Encode(unsigned char code[74]) const {
memcpy(code+1, vchFingerprint, 4);
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
memcpy(code+9, vchChainCode, 32);
memcpy(code+9, chaincode.data, 32);
code[41] = 0;
assert(key.size() == 32);
memcpy(code+42, key.begin(), 32);
Expand All @@ -193,7 +193,7 @@ void CExtKey::Decode(const unsigned char code[74]) {
nDepth = code[0];
memcpy(vchFingerprint, code+1, 4);
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
memcpy(vchChainCode, code+9, 32);
memcpy(chaincode.data, code+9, 32);
key.Set(code+42, code+74, true);
}

Expand Down
8 changes: 3 additions & 5 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
#ifndef BITCOIN_KEY_H
#define BITCOIN_KEY_H

#include "pubkey.h"
#include "serialize.h"
#include "support/allocators/secure.h"
#include "uint256.h"

#include <stdexcept>
#include <vector>

class CPubKey;

struct CExtPubKey;

/**
* secp256k1:
Expand Down Expand Up @@ -157,13 +155,13 @@ struct CExtKey {
unsigned char nDepth;
unsigned char vchFingerprint[4];
unsigned int nChild;
unsigned char vchChainCode[32];
CChainCode chaincode;
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;
memcmp(&a.chaincode.data[0], &b.chaincode.data[0], 32) == 0 && a.key == b.key;
}

void Encode(unsigned char code[74]) const;
Expand Down
6 changes: 3 additions & 3 deletions src/pubkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void CExtPubKey::Encode(unsigned char code[74]) const {
memcpy(code+1, vchFingerprint, 4);
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
memcpy(code+9, vchChainCode, 32);
memcpy(code+9, chaincode.data, 32);
assert(pubkey.size() == 33);
memcpy(code+41, pubkey.begin(), 33);
}
Expand All @@ -84,7 +84,7 @@ void CExtPubKey::Decode(const unsigned char code[74]) {
nDepth = code[0];
memcpy(vchFingerprint, code+1, 4);
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
memcpy(vchChainCode, code+9, 32);
memcpy(chaincode.data, code+9, 32);
pubkey.Set(code+41, code+74);
}

Expand All @@ -93,5 +93,5 @@ bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
CKeyID id = pubkey.GetID();
memcpy(&out.vchFingerprint[0], &id, 4);
out.nChild = nChild;
return pubkey.Derive(out.pubkey, out.vchChainCode, nChild, vchChainCode);
return pubkey.Derive(out.pubkey, out.chaincode.data, nChild, chaincode.data);
}
36 changes: 33 additions & 3 deletions src/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ class CKeyID : public uint160
CKeyID(const uint160& in) : uint160(in) {}
};

struct CChainCode
{
unsigned char data[32];

void SetNull()
{
memset(data, 0, sizeof(data));
}

CChainCode()
{
SetNull();
}

bool IsNull() const
{
for (int i=0; i<32; i++)
if (data[i])
return false;
return true;
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(FLATDATA(data));
}
};

/** An encapsulated public key. */
class CPubKey
{
Expand Down Expand Up @@ -189,13 +219,13 @@ struct CExtPubKey {
unsigned char nDepth;
unsigned char vchFingerprint[4];
unsigned int nChild;
unsigned char vchChainCode[32];
CChainCode chaincode;
CPubKey pubkey;

friend bool operator==(const CExtPubKey& a, const CExtPubKey& b)
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;
memcmp(&a.chaincode.data[0], &b.chaincode.data[0], 32) == 0 && a.pubkey == b.pubkey;
}

void Encode(unsigned char code[74]) const;
Expand Down

0 comments on commit 8cf1485

Please sign in to comment.