Skip to content

Commit 8cf1485

Browse files
sipajonasschnelli
authored andcommitted
Abstract chaincodes into CChainCode
# Conflicts: # src/key.cpp # src/key.h
1 parent 90c37bc commit 8cf1485

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/key.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
152152
CKeyID id = key.GetPubKey().GetID();
153153
memcpy(&out.vchFingerprint[0], &id, 4);
154154
out.nChild = nChild;
155-
return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
155+
return key.Derive(out.key, out.chaincode.data, nChild, chaincode.data);
156156
}
157157

158158
void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
@@ -161,7 +161,7 @@ void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
161161
LockObject(out);
162162
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
163163
key.Set(&out[0], &out[32], true);
164-
memcpy(vchChainCode, &out[32], 32);
164+
memcpy(chaincode.data, &out[32], 32);
165165
UnlockObject(out);
166166
nDepth = 0;
167167
nChild = 0;
@@ -174,7 +174,7 @@ CExtPubKey CExtKey::Neuter() const {
174174
memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
175175
ret.nChild = nChild;
176176
ret.pubkey = key.GetPubKey();
177-
memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
177+
memcpy(&ret.chaincode.data[0], &chaincode.data[0], 32);
178178
return ret;
179179
}
180180

@@ -183,7 +183,7 @@ void CExtKey::Encode(unsigned char code[74]) const {
183183
memcpy(code+1, vchFingerprint, 4);
184184
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
185185
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
186-
memcpy(code+9, vchChainCode, 32);
186+
memcpy(code+9, chaincode.data, 32);
187187
code[41] = 0;
188188
assert(key.size() == 32);
189189
memcpy(code+42, key.begin(), 32);
@@ -193,7 +193,7 @@ void CExtKey::Decode(const unsigned char code[74]) {
193193
nDepth = code[0];
194194
memcpy(vchFingerprint, code+1, 4);
195195
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
196-
memcpy(vchChainCode, code+9, 32);
196+
memcpy(chaincode.data, code+9, 32);
197197
key.Set(code+42, code+74, true);
198198
}
199199

src/key.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
#ifndef BITCOIN_KEY_H
77
#define BITCOIN_KEY_H
88

9+
#include "pubkey.h"
910
#include "serialize.h"
1011
#include "support/allocators/secure.h"
1112
#include "uint256.h"
1213

1314
#include <stdexcept>
1415
#include <vector>
1516

16-
class CPubKey;
17-
18-
struct CExtPubKey;
1917

2018
/**
2119
* secp256k1:
@@ -157,13 +155,13 @@ struct CExtKey {
157155
unsigned char nDepth;
158156
unsigned char vchFingerprint[4];
159157
unsigned int nChild;
160-
unsigned char vchChainCode[32];
158+
CChainCode chaincode;
161159
CKey key;
162160

163161
friend bool operator==(const CExtKey& a, const CExtKey& b)
164162
{
165163
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
166-
memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
164+
memcmp(&a.chaincode.data[0], &b.chaincode.data[0], 32) == 0 && a.key == b.key;
167165
}
168166

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

src/pubkey.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void CExtPubKey::Encode(unsigned char code[74]) const {
7575
memcpy(code+1, vchFingerprint, 4);
7676
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
7777
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
78-
memcpy(code+9, vchChainCode, 32);
78+
memcpy(code+9, chaincode.data, 32);
7979
assert(pubkey.size() == 33);
8080
memcpy(code+41, pubkey.begin(), 33);
8181
}
@@ -84,7 +84,7 @@ void CExtPubKey::Decode(const unsigned char code[74]) {
8484
nDepth = code[0];
8585
memcpy(vchFingerprint, code+1, 4);
8686
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
87-
memcpy(vchChainCode, code+9, 32);
87+
memcpy(chaincode.data, code+9, 32);
8888
pubkey.Set(code+41, code+74);
8989
}
9090

@@ -93,5 +93,5 @@ bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
9393
CKeyID id = pubkey.GetID();
9494
memcpy(&out.vchFingerprint[0], &id, 4);
9595
out.nChild = nChild;
96-
return pubkey.Derive(out.pubkey, out.vchChainCode, nChild, vchChainCode);
96+
return pubkey.Derive(out.pubkey, out.chaincode.data, nChild, chaincode.data);
9797
}

src/pubkey.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ class CKeyID : public uint160
3131
CKeyID(const uint160& in) : uint160(in) {}
3232
};
3333

34+
struct CChainCode
35+
{
36+
unsigned char data[32];
37+
38+
void SetNull()
39+
{
40+
memset(data, 0, sizeof(data));
41+
}
42+
43+
CChainCode()
44+
{
45+
SetNull();
46+
}
47+
48+
bool IsNull() const
49+
{
50+
for (int i=0; i<32; i++)
51+
if (data[i])
52+
return false;
53+
return true;
54+
}
55+
56+
ADD_SERIALIZE_METHODS;
57+
58+
template <typename Stream, typename Operation>
59+
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
60+
READWRITE(FLATDATA(data));
61+
}
62+
};
63+
3464
/** An encapsulated public key. */
3565
class CPubKey
3666
{
@@ -189,13 +219,13 @@ struct CExtPubKey {
189219
unsigned char nDepth;
190220
unsigned char vchFingerprint[4];
191221
unsigned int nChild;
192-
unsigned char vchChainCode[32];
222+
CChainCode chaincode;
193223
CPubKey pubkey;
194224

195-
friend bool operator==(const CExtPubKey& a, const CExtPubKey& b)
225+
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
196226
{
197227
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
198-
memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.pubkey == b.pubkey;
228+
memcmp(&a.chaincode.data[0], &b.chaincode.data[0], 32) == 0 && a.pubkey == b.pubkey;
199229
}
200230

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

0 commit comments

Comments
 (0)