@@ -159,21 +159,21 @@ bool CKey::Check(const unsigned char *vch) {
159159}
160160
161161void CKey::MakeNewKey (bool fCompressedIn ) {
162+ MakeKeyData ();
162163 do {
163- GetStrongRandBytes (keydata);
164- } while (!Check (keydata.data ()));
165- fValid = true ;
164+ GetStrongRandBytes (*keydata);
165+ } while (!Check (keydata->data ()));
166166 fCompressed = fCompressedIn ;
167167}
168168
169169bool CKey::Negate ()
170170{
171- assert (fValid );
172- return secp256k1_ec_seckey_negate (secp256k1_context_sign, keydata. data ());
171+ assert (keydata );
172+ return secp256k1_ec_seckey_negate (secp256k1_context_sign, keydata-> data ());
173173}
174174
175175CPrivKey CKey::GetPrivKey () const {
176- assert (fValid );
176+ assert (keydata );
177177 CPrivKey seckey;
178178 int ret;
179179 size_t seckeylen;
@@ -186,7 +186,7 @@ CPrivKey CKey::GetPrivKey() const {
186186}
187187
188188CPubKey CKey::GetPubKey () const {
189- assert (fValid );
189+ assert (keydata );
190190 secp256k1_pubkey pubkey;
191191 size_t clen = CPubKey::SIZE;
192192 CPubKey result;
@@ -212,7 +212,7 @@ bool SigHasLowR(const secp256k1_ecdsa_signature* sig)
212212}
213213
214214bool CKey::Sign (const uint256 &hash, std::vector<unsigned char >& vchSig, bool grind, uint32_t test_case) const {
215- if (!fValid )
215+ if (!keydata )
216216 return false ;
217217 vchSig.resize (CPubKey::SIGNATURE_SIZE);
218218 size_t nSigLen = CPubKey::SIGNATURE_SIZE;
@@ -253,7 +253,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
253253}
254254
255255bool CKey::SignCompact (const uint256 &hash, std::vector<unsigned char >& vchSig) const {
256- if (!fValid )
256+ if (!keydata )
257257 return false ;
258258 vchSig.resize (CPubKey::COMPACT_SIGNATURE_SIZE);
259259 int rec = -1 ;
@@ -301,10 +301,12 @@ bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint2
301301}
302302
303303bool CKey::Load (const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck =false ) {
304- if (!ec_seckey_import_der (secp256k1_context_sign, (unsigned char *)begin (), seckey.data (), seckey.size ()))
304+ MakeKeyData ();
305+ if (!ec_seckey_import_der (secp256k1_context_sign, (unsigned char *)begin (), seckey.data (), seckey.size ())) {
306+ ClearKeyData ();
305307 return false ;
308+ }
306309 fCompressed = vchPubKey.IsCompressed ();
307- fValid = true ;
308310
309311 if (fSkipCheck )
310312 return true ;
@@ -325,22 +327,21 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
325327 BIP32Hash (cc, nChild, 0 , begin (), vout.data ());
326328 }
327329 memcpy (ccChild.begin (), vout.data ()+32 , 32 );
328- memcpy (( unsigned char *) keyChild.begin (), begin (), 32 );
330+ keyChild.Set ( begin (), begin () + 32 , true );
329331 bool ret = secp256k1_ec_seckey_tweak_add (secp256k1_context_sign, (unsigned char *)keyChild.begin (), vout.data ());
330- keyChild.fCompressed = true ;
331- keyChild.fValid = ret;
332+ if (!ret) keyChild.ClearKeyData ();
332333 return ret;
333334}
334335
335336EllSwiftPubKey CKey::EllSwiftCreate (Span<const std::byte> ent32) const
336337{
337- assert (fValid );
338+ assert (keydata );
338339 assert (ent32.size () == 32 );
339340 std::array<std::byte, EllSwiftPubKey::size ()> encoded_pubkey;
340341
341342 auto success = secp256k1_ellswift_create (secp256k1_context_sign,
342343 UCharCast (encoded_pubkey.data ()),
343- keydata. data (),
344+ keydata-> data (),
344345 UCharCast (ent32.data ()));
345346
346347 // Should always succeed for valid keys (asserted above).
@@ -350,7 +351,7 @@ EllSwiftPubKey CKey::EllSwiftCreate(Span<const std::byte> ent32) const
350351
351352ECDHSecret CKey::ComputeBIP324ECDHSecret (const EllSwiftPubKey& their_ellswift, const EllSwiftPubKey& our_ellswift, bool initiating) const
352353{
353- assert (fValid );
354+ assert (keydata );
354355
355356 ECDHSecret output;
356357 // BIP324 uses the initiator as party A, and the responder as party B. Remap the inputs
@@ -359,7 +360,7 @@ ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, c
359360 UCharCast (output.data ()),
360361 UCharCast (initiating ? our_ellswift.data () : their_ellswift.data ()),
361362 UCharCast (initiating ? their_ellswift.data () : our_ellswift.data ()),
362- keydata. data (),
363+ keydata-> data (),
363364 initiating ? 0 : 1 ,
364365 secp256k1_ellswift_xdh_hash_function_bip324,
365366 nullptr );
0 commit comments