Skip to content

Commit a574899

Browse files
theunijonasschnelli
authored andcommitted
chaincodes: abstract away more chaincode behavior
[squashme] replace struct CCainCode with a typedef uint256 ChainCode
1 parent 8cf1485 commit a574899

File tree

6 files changed

+25
-53
lines changed

6 files changed

+25
-53
lines changed

src/hash.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "hash.h"
66
#include "crypto/common.h"
77
#include "crypto/hmac_sha512.h"
8+
#include "pubkey.h"
89

910

1011
inline uint32_t ROTL32(uint32_t x, int8_t r)
@@ -71,15 +72,12 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
7172
return h1;
7273
}
7374

74-
void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
75+
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
7576
{
7677
unsigned char num[4];
7778
num[0] = (nChild >> 24) & 0xFF;
7879
num[1] = (nChild >> 16) & 0xFF;
7980
num[2] = (nChild >> 8) & 0xFF;
8081
num[3] = (nChild >> 0) & 0xFF;
81-
CHMAC_SHA512(chainCode, 32).Write(&header, 1)
82-
.Write(data, 32)
83-
.Write(num, 4)
84-
.Finalize(output);
82+
CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
8583
}

src/hash.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include <vector>
1616

17+
typedef uint256 ChainCode;
18+
1719
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
1820
class CHash256 {
1921
private:
@@ -159,6 +161,6 @@ uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL
159161

160162
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
161163

162-
void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
164+
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
163165

164166
#endif // BITCOIN_HASH_H

src/key.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
125125
return VerifyPubKey(vchPubKey);
126126
}
127127

128-
bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
128+
bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
129129
assert(IsValid());
130130
assert(IsCompressed());
131131
unsigned char out[64];
@@ -138,7 +138,7 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
138138
assert(begin() + 32 == end());
139139
BIP32Hash(cc, nChild, 0, begin(), out);
140140
}
141-
memcpy(ccChild, out+32, 32);
141+
memcpy(ccChild.begin(), out+32, 32);
142142
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
143143
bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
144144
UnlockObject(out);
@@ -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.chaincode.data, nChild, chaincode.data);
155+
return key.Derive(out.key, out.chaincode, nChild, chaincode);
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(chaincode.data, &out[32], 32);
164+
memcpy(chaincode.begin(), &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.chaincode.data[0], &chaincode.data[0], 32);
177+
ret.chaincode = chaincode;
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, chaincode.data, 32);
186+
memcpy(code+9, chaincode.begin(), 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(chaincode.data, code+9, 32);
196+
memcpy(chaincode.begin(), code+9, 32);
197197
key.Set(code+42, code+74, true);
198198
}
199199

src/key.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class CKey
136136
bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
137137

138138
//! Derive BIP32 child key.
139-
bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
139+
bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
140140

141141
/**
142142
* Verify thoroughly whether a private key and a public key match.
@@ -155,13 +155,13 @@ struct CExtKey {
155155
unsigned char nDepth;
156156
unsigned char vchFingerprint[4];
157157
unsigned int nChild;
158-
CChainCode chaincode;
158+
ChainCode chaincode;
159159
CKey key;
160160

161161
friend bool operator==(const CExtKey& a, const CExtKey& b)
162162
{
163163
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
164-
memcmp(&a.chaincode.data[0], &b.chaincode.data[0], 32) == 0 && a.key == b.key;
164+
a.chaincode == b.chaincode && a.key == b.key;
165165
}
166166

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

src/pubkey.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ bool CPubKey::Decompress() {
5454
return true;
5555
}
5656

57-
bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
57+
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
5858
assert(IsValid());
5959
assert((nChild >> 31) == 0);
6060
assert(begin() + 33 == end());
6161
unsigned char out[64];
6262
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
63-
memcpy(ccChild, out+32, 32);
63+
memcpy(ccChild.begin(), out+32, 32);
6464
CECKey key;
6565
bool ret = key.SetPubKey(begin(), size());
6666
ret &= key.TweakPublic(out);
@@ -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, chaincode.data, 32);
78+
memcpy(code+9, chaincode.begin(), 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(chaincode.data, code+9, 32);
87+
memcpy(chaincode.begin(), 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.chaincode.data, nChild, chaincode.data);
96+
return pubkey.Derive(out.pubkey, out.chaincode, nChild, chaincode);
9797
}

src/pubkey.h

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,7 @@ 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-
};
34+
typedef uint256 ChainCode;
6335

6436
/** An encapsulated public key. */
6537
class CPubKey
@@ -212,20 +184,20 @@ class CPubKey
212184
bool Decompress();
213185

214186
//! Derive BIP32 child pubkey.
215-
bool Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
187+
bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
216188
};
217189

218190
struct CExtPubKey {
219191
unsigned char nDepth;
220192
unsigned char vchFingerprint[4];
221193
unsigned int nChild;
222-
CChainCode chaincode;
194+
ChainCode chaincode;
223195
CPubKey pubkey;
224196

225197
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
226198
{
227199
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
228-
memcmp(&a.chaincode.data[0], &b.chaincode.data[0], 32) == 0 && a.pubkey == b.pubkey;
200+
a.chaincode == b.chaincode && a.pubkey == b.pubkey;
229201
}
230202

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

0 commit comments

Comments
 (0)