Skip to content

Commit 30a86a3

Browse files
committed
refactor: separation of extractSignatureBytes as helper function
Signed-off-by: kubikusik <jakubmical@student.agh.edu.pl>
1 parent 13dde58 commit 30a86a3

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

src/sdk/main/src/Transaction.cc

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@
7676

7777
namespace Hiero
7878
{
79+
80+
namespace
81+
{
82+
/**
83+
* Helper function - Extract the raw signature bytes from a protobuf SignaturePair.
84+
*/
85+
std::vector<std::byte> extractSignatureBytes(const proto::SignaturePair& pair)
86+
{
87+
const std::string* sigStr = nullptr;
88+
if (pair.has_ed25519())
89+
{
90+
sigStr = &pair.ed25519();
91+
}
92+
else if (pair.has_ecdsa_secp256k1())
93+
{
94+
sigStr = &pair.ecdsa_secp256k1();
95+
}
96+
else
97+
{
98+
throw IllegalStateException("Unknown signature type");
99+
}
100+
101+
std::vector<std::byte> sigBytes(sigStr->size());
102+
std::transform(sigStr->begin(), sigStr->end(), sigBytes.begin(), [](char c) { return static_cast<std::byte>(c); });
103+
return sigBytes;
104+
}
105+
} // anonymous namespace
106+
79107
//-----
80108
template<typename SdkRequestType>
81109
struct Transaction<SdkRequestType>::TransactionImpl
@@ -566,7 +594,6 @@ std::vector<std::vector<std::byte>> Transaction<SdkRequestType>::removeSignature
566594
throw IllegalStateException("Removing a signature from a Transaction requires "
567595
"the Transaction to be frozen");
568596
}
569-
570597
if (!keyAlreadySigned(publicKey))
571598
{
572599
throw IllegalStateException("The public key has not signed this transaction");
@@ -597,30 +624,9 @@ std::vector<std::vector<std::byte>> Transaction<SdkRequestType>::removeSignature
597624
for (int i = sigPairs->size() - 1; i >= 0; --i)
598625
{
599626
const auto& pair = sigPairs->at(i);
600-
601627
if (pair.pubkeyprefix() == pubKeyPrefixStr)
602628
{
603-
const std::string* sigStr = nullptr;
604-
if (pair.has_ed25519())
605-
{
606-
sigStr = &pair.ed25519();
607-
}
608-
else if (pair.has_ecdsa_secp256k1())
609-
{
610-
sigStr = &pair.ecdsa_secp256k1();
611-
}
612-
else
613-
{
614-
throw IllegalStateException("Unknown signature type");
615-
}
616-
617-
// Extract the bytes and save them to return to the caller
618-
std::vector<std::byte> sigBytes(sigStr->size());
619-
std::transform(
620-
sigStr->begin(), sigStr->end(), sigBytes.begin(), [](char c) { return static_cast<std::byte>(c); });
621-
622-
removedSignatures.push_back(std::move(sigBytes));
623-
629+
removedSignatures.push_back(extractSignatureBytes(pair));
624630
// Erase the signature directly from the Protobuf array
625631
sigPairs->erase(sigPairs->begin() + i);
626632
}
@@ -682,25 +688,7 @@ Transaction<SdkRequestType>::removeAllSignatures()
682688
auto it = prefixToKey.find(pair.pubkeyprefix());
683689
if (it != prefixToKey.end())
684690
{
685-
const std::string* sigStr = nullptr;
686-
if (pair.has_ed25519())
687-
{
688-
sigStr = &pair.ed25519();
689-
}
690-
else if (pair.has_ecdsa_secp256k1())
691-
{
692-
sigStr = &pair.ecdsa_secp256k1();
693-
}
694-
else
695-
{
696-
throw IllegalStateException("Unknown signature type");
697-
}
698-
699-
// Convert and store the extracted signature bytes
700-
std::vector<std::byte> sigBytes(sigStr->size());
701-
std::transform(
702-
sigStr->begin(), sigStr->end(), sigBytes.begin(), [](char c) { return static_cast<std::byte>(c); });
703-
691+
std::vector<std::byte> sigBytes = extractSignatureBytes(pair);
704692
removedByKey[it->second].push_back(std::move(sigBytes));
705693
}
706694
}

0 commit comments

Comments
 (0)