mirror of
https://github.com/XRPLF/rippled.git
synced 2026-08-23 07:10:53 +00:00
138 lines
4.1 KiB
C++
138 lines
4.1 KiB
C++
#include <test/jtx/batch.h>
|
|
|
|
#include <test/jtx/Account.h>
|
|
#include <test/jtx/Env.h>
|
|
#include <test/jtx/JTx.h>
|
|
#include <test/jtx/utility.h>
|
|
|
|
#include <xrpl/basics/Number.h>
|
|
#include <xrpl/basics/contract.h>
|
|
#include <xrpl/basics/strHex.h>
|
|
#include <xrpl/json/json_value.h>
|
|
#include <xrpl/json/to_string.h>
|
|
#include <xrpl/protocol/Batch.h>
|
|
#include <xrpl/protocol/PublicKey.h>
|
|
#include <xrpl/protocol/SField.h>
|
|
#include <xrpl/protocol/STAmount.h>
|
|
#include <xrpl/protocol/STObject.h>
|
|
#include <xrpl/protocol/STTx.h>
|
|
#include <xrpl/protocol/SecretKey.h>
|
|
#include <xrpl/protocol/Serializer.h>
|
|
#include <xrpl/protocol/Sign.h>
|
|
#include <xrpl/protocol/XRPAmount.h>
|
|
#include <xrpl/protocol/jss.h>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <utility>
|
|
|
|
namespace xrpl::test::jtx::batch {
|
|
|
|
XRPAmount
|
|
calcBatchFee(test::jtx::Env const& env, uint32_t const& numSigners, uint32_t const& txns)
|
|
{
|
|
XRPAmount const feeDrops = env.current()->fees().base;
|
|
return ((numSigners + 2) * feeDrops) + feeDrops * txns;
|
|
}
|
|
|
|
// Batch.
|
|
json::Value
|
|
outer(jtx::Account const& account, uint32_t seq, STAmount const& fee, std::uint32_t flags)
|
|
{
|
|
json::Value jv;
|
|
jv[jss::TransactionType] = jss::Batch;
|
|
jv[jss::Account] = account.human();
|
|
jv[jss::RawTransactions] = json::Value{json::ValueType::Array};
|
|
jv[jss::Sequence] = seq;
|
|
jv[jss::Flags] = flags;
|
|
jv[jss::Fee] = to_string(fee);
|
|
return jv;
|
|
}
|
|
|
|
void
|
|
Inner::operator()(Env& env, JTx& jt) const
|
|
{
|
|
auto const index = jt.jv[jss::RawTransactions].size();
|
|
json::Value& batchTransaction = jt.jv[jss::RawTransactions][index];
|
|
|
|
// Initialize the batch transaction
|
|
batchTransaction = json::Value{};
|
|
batchTransaction[jss::RawTransaction] = txn_;
|
|
}
|
|
|
|
void
|
|
Sig::operator()(Env& env, JTx& jt) const
|
|
{
|
|
auto const mySigners = signers;
|
|
std::optional<STObject> st;
|
|
try
|
|
{
|
|
// required to cast the STObject to STTx
|
|
jt.jv[jss::SigningPubKey] = "";
|
|
st = parse(jt.jv);
|
|
}
|
|
catch (ParseError const&)
|
|
{
|
|
env.test.log << pretty(jt.jv) << std::endl;
|
|
rethrow();
|
|
}
|
|
STTx const& stx = STTx{std::move(*st)};
|
|
auto& js = jt[sfBatchSigners.getJsonName()];
|
|
for (std::size_t i = 0; i < mySigners.size(); ++i)
|
|
{
|
|
auto const& e = mySigners[i];
|
|
auto& jo = js[i][sfBatchSigner.getJsonName()];
|
|
jo[jss::Account] = e.acct.human();
|
|
jo[jss::SigningPubKey] = strHex(e.sig.pk().slice());
|
|
|
|
Serializer msg;
|
|
serializeBatch(msg, stx.getFlags(), stx.getBatchTransactionIDs());
|
|
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
|
|
auto const sig = xrpl::sign(*publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
|
|
jo[sfTxnSignature.getJsonName()] = strHex(Slice{sig.data(), sig.size()});
|
|
}
|
|
}
|
|
|
|
void
|
|
Msig::operator()(Env& env, JTx& jt) const
|
|
{
|
|
auto const mySigners = signers;
|
|
std::optional<STObject> st;
|
|
try
|
|
{
|
|
// required to cast the STObject to STTx
|
|
jt.jv[jss::SigningPubKey] = "";
|
|
st = parse(jt.jv);
|
|
}
|
|
catch (ParseError const&)
|
|
{
|
|
env.test.log << pretty(jt.jv) << std::endl;
|
|
rethrow();
|
|
}
|
|
STTx const& stx = STTx{std::move(*st)};
|
|
auto& bs = jt[sfBatchSigners.getJsonName()];
|
|
auto const index = jt[sfBatchSigners.jsonName].size();
|
|
auto& bso = bs[index][sfBatchSigner.getJsonName()];
|
|
bso[jss::Account] = master.human();
|
|
bso[jss::SigningPubKey] = "";
|
|
auto& is = bso[sfSigners.getJsonName()];
|
|
for (std::size_t i = 0; i < mySigners.size(); ++i)
|
|
{
|
|
auto const& e = mySigners[i];
|
|
auto& iso = is[i][sfSigner.getJsonName()];
|
|
iso[jss::Account] = e.acct.human();
|
|
iso[jss::SigningPubKey] = strHex(e.sig.pk().slice());
|
|
|
|
Serializer msg;
|
|
serializeBatch(msg, stx.getFlags(), stx.getBatchTransactionIDs());
|
|
finishMultiSigningData(e.acct.id(), msg);
|
|
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
|
|
auto const sig = xrpl::sign(*publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
|
|
iso[sfTxnSignature.getJsonName()] = strHex(Slice{sig.data(), sig.size()});
|
|
}
|
|
}
|
|
|
|
} // namespace xrpl::test::jtx::batch
|