Skip to content

Commit

Permalink
Added signed transaction serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaven committed May 30, 2024
1 parent 1bc769a commit d4b0c56
Show file tree
Hide file tree
Showing 5 changed files with 574 additions and 24 deletions.
14 changes: 14 additions & 0 deletions include/EclipseMonitor/Eth/Transaction/DynamicFee.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ class DynFee :
);
}

Internal::Rlp::OutputContainerType RlpSerializeSigned() const
{
Validate();
if (get_r().size() == 0 || get_s().size() == 0)
{
throw Exception("The given transaction is not signed");
}
auto rlp =
Internal::Rlp::WriterGeneric::StaticDictWriter::Write(*this);
rlp.insert(rlp.begin(), GetTypeFlag());

return rlp;
}

/**
* @brief Get the type flag, which is 0x02 for dynamic fee transaction
*
Expand Down
75 changes: 75 additions & 0 deletions include/EclipseMonitor/Eth/Transaction/Ecdsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

#include <mbedTLScpp/BigNumber.hpp>
#include <mbedTLScpp/EcKey.hpp>
#include <mbedTLScpp/Hmac.hpp>
#include <mbedTLScpp/SecretVector.hpp>

Expand Down Expand Up @@ -856,6 +857,80 @@ EcdsaRawSign(
}


template<
typename _HashCtnT, bool _HashCtnSec,
typename _PkeyBigNumTraits,
typename _EcGroupTraits
>
inline
std::tuple<
uint8_t,
Internal::Tls::BigNum,
Internal::Tls::BigNum
>
EcdsaRawSign(
const Internal::Tls::ContCtnReadOnlyRef<_HashCtnT, _HashCtnSec>& hashCtnRef,
const Internal::Tls::BigNumber<_PkeyBigNumTraits>& privKeyNum,
const Internal::Tls::EcGroup<_EcGroupTraits>& curveGroup
)
{
auto privKeyBytes =
privKeyNum.template SecretBytes</*little endian=*/false>();

return EcdsaRawSign(
hashCtnRef,
Internal::Tls::CtnFullR(privKeyBytes),
privKeyNum,
curveGroup.BorrowA(),
curveGroup.BorrowGx(),
curveGroup.BorrowGy(),
curveGroup.BorrowN(),
curveGroup.BorrowP()
);
}


template<
typename _PkeyTraits
>
inline void SignTransaction(
DynFee& txn,
const Internal::Tls::EcKeyPair<
Internal::Tls::EcType::SECP256K1,
_PkeyTraits
>& keyPair
)
{
auto hash = txn.Hash();

auto signBigNum = EcdsaRawSign(
Internal::Tls::CtnFullR(hash),
keyPair.BorrowSecretNum(),
keyPair.BorrowGroup()
);

uint8_t v = std::get<0>(signBigNum);
if (v == 0)
{
txn.get_v().resize(0);
}
else
{
txn.get_v().resize(1);
txn.get_v()[0] = v;
}


txn.get_r() = Internal::Obj::Bytes(
std::get<1>(signBigNum).template Bytes</*_LitEndian=*/false>()
);

txn.get_s() = Internal::Obj::Bytes(
std::get<2>(signBigNum).template Bytes</*_LitEndian=*/false>()
);
}


} // namespace Transaction
} // namespace Eth
} // namespace EclipseMonitor
Expand Down
Loading

0 comments on commit d4b0c56

Please sign in to comment.