Skip to content

Commit ec973dd

Browse files
committed
refactor: remove un-tested early returns
Replace early returns in KeyPair::KeyPair() with asserts. The if statements imply there is an error we are handling, but keypair_xonly_pub and xonly_pubkey_serialize can only fail if the keypair object is malformed, i.e., it was created with a bad secret key. Since we check that the keypair was created successfully before attempting to extract the public key, using asserts more accurately documents what we expect here and removes untested branches from the code.
1 parent 72a5822 commit ec973dd

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ KeyPair::KeyPair(const CKey& key, const uint256* merkle_root)
414414
bool success = secp256k1_keypair_create(secp256k1_context_sign, keypair, UCharCast(key.data()));
415415
if (success && merkle_root) {
416416
secp256k1_xonly_pubkey pubkey;
417-
if (!secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, keypair)) return;
418417
unsigned char pubkey_bytes[32];
419-
if (!secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, pubkey_bytes, &pubkey)) return;
418+
assert(secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, keypair));
419+
assert(secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, pubkey_bytes, &pubkey));
420420
uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root);
421421
success = secp256k1_keypair_xonly_tweak_add(secp256k1_context_static, keypair, tweak.data());
422422
}

0 commit comments

Comments
 (0)