15
15
#include < vector>
16
16
17
17
18
- /* *
18
+ /* *
19
19
* secp256k1:
20
20
* const unsigned int PRIVATE_KEY_SIZE = 279;
21
21
* const unsigned int PUBLIC_KEY_SIZE = 65;
@@ -45,6 +45,8 @@ class CKey
45
45
// ! The actual byte data
46
46
unsigned char vch[32 ];
47
47
48
+ static_assert (sizeof (vch) == 32 , " vch must be 32 bytes in length to not break serialization" );
49
+
48
50
// ! Check whether the 32-byte array pointed to be vch is valid keydata.
49
51
bool static Check (const unsigned char * vch);
50
52
@@ -70,20 +72,19 @@ class CKey
70
72
71
73
friend bool operator ==(const CKey& a, const CKey& b)
72
74
{
73
- return a.fCompressed == b.fCompressed && a.size () == b.size () &&
74
- memcmp (&a.vch [0 ], &b.vch [0 ], a.size ()) == 0 ;
75
+ return a.fCompressed == b.fCompressed &&
76
+ a.size () == b.size () &&
77
+ memcmp (&a.vch [0 ], &b.vch [0 ], a.size ()) == 0 ;
75
78
}
76
79
77
80
// ! Initialize using begin and end iterators to byte data.
78
81
template <typename T>
79
82
void Set (const T pbegin, const T pend, bool fCompressedIn )
80
83
{
81
- if (pend - pbegin != 32 ) {
84
+ if (pend - pbegin != sizeof (vch) ) {
82
85
fValid = false ;
83
- return ;
84
- }
85
- if (Check (&pbegin[0 ])) {
86
- memcpy (vch, (unsigned char *)&pbegin[0 ], 32 );
86
+ } else if (Check (&pbegin[0 ])) {
87
+ memcpy (vch, (unsigned char *)&pbegin[0 ], sizeof (vch));
87
88
fValid = true ;
88
89
fCompressed = fCompressedIn ;
89
90
} else {
@@ -92,7 +93,7 @@ class CKey
92
93
}
93
94
94
95
// ! Simple read-only vector-like interface.
95
- unsigned int size () const { return (fValid ? 32 : 0 ); }
96
+ unsigned int size () const { return (fValid ? sizeof (vch) : 0 ); }
96
97
const unsigned char * begin () const { return vch; }
97
98
const unsigned char * end () const { return vch + size (); }
98
99
@@ -110,7 +111,7 @@ class CKey
110
111
111
112
/* *
112
113
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
113
- * This is expensive.
114
+ * This is expensive.
114
115
*/
115
116
CPrivKey GetPrivKey () const ;
116
117
@@ -157,8 +158,11 @@ struct CExtKey {
157
158
158
159
friend bool operator ==(const CExtKey& a, const CExtKey& b)
159
160
{
160
- return a.nDepth == b.nDepth && memcmp (&a.vchFingerprint [0 ], &b.vchFingerprint [0 ], 4 ) == 0 && a.nChild == b.nChild &&
161
- a.chaincode == b.chaincode && a.key == b.key ;
161
+ return a.nDepth == b.nDepth &&
162
+ memcmp (&a.vchFingerprint [0 ], &b.vchFingerprint [0 ], sizeof (vchFingerprint)) == 0 &&
163
+ a.nChild == b.nChild &&
164
+ a.chaincode == b.chaincode &&
165
+ a.key == b.key ;
162
166
}
163
167
164
168
void Encode (unsigned char code[BIP32_EXTKEY_SIZE]) const ;
0 commit comments