From ec973dd19719541dbcd6f3a6facf6f5dd7cf439c Mon Sep 17 00:00:00 2001 From: josibake Date: Sat, 3 Aug 2024 14:58:28 +0200 Subject: [PATCH] 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. --- src/key.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/key.cpp b/src/key.cpp index 37b78d8e77..360a1f46d3 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -414,9 +414,9 @@ KeyPair::KeyPair(const CKey& key, const uint256* merkle_root) bool success = secp256k1_keypair_create(secp256k1_context_sign, keypair, UCharCast(key.data())); if (success && merkle_root) { secp256k1_xonly_pubkey pubkey; - if (!secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, keypair)) return; unsigned char pubkey_bytes[32]; - if (!secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, pubkey_bytes, &pubkey)) return; + assert(secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, keypair)); + assert(secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, pubkey_bytes, &pubkey)); uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root); success = secp256k1_keypair_xonly_tweak_add(secp256k1_context_static, keypair, tweak.data()); }