1#include <xrpl/basics/Blob.h>
2#include <xrpl/basics/Expected.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/StringUtilities.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/basics/safe_cast.h>
9#include <xrpl/basics/strHex.h>
10#include <xrpl/beast/utility/Zero.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/protocol/AccountID.h>
14#include <xrpl/protocol/Batch.h>
15#include <xrpl/protocol/HashPrefix.h>
16#include <xrpl/protocol/MPTIssue.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/PublicKey.h>
19#include <xrpl/protocol/Rules.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/SOTemplate.h>
22#include <xrpl/protocol/STAccount.h>
23#include <xrpl/protocol/STAmount.h>
24#include <xrpl/protocol/STArray.h>
25#include <xrpl/protocol/STBase.h>
26#include <xrpl/protocol/STObject.h>
27#include <xrpl/protocol/STTx.h>
28#include <xrpl/protocol/STVector256.h>
29#include <xrpl/protocol/SecretKey.h>
30#include <xrpl/protocol/SeqProxy.h>
31#include <xrpl/protocol/Serializer.h>
32#include <xrpl/protocol/Sign.h>
33#include <xrpl/protocol/TxFlags.h>
34#include <xrpl/protocol/TxFormats.h>
35#include <xrpl/protocol/jss.h>
37#include <boost/container/flat_set.hpp>
38#include <boost/format/format_fwd.hpp>
39#include <boost/format/free_funcs.hpp>
61 if (format ==
nullptr)
63 Throw<std::runtime_error>(
64 "Invalid transaction type " +
83 Throw<std::runtime_error>(
"Transaction length invalid");
86 Throw<std::runtime_error>(
"Transaction contains an object terminator");
98 set(format->getSOTemplate());
106 LogicError(
"Transaction type was mutated during assembly");
120 return emplace(n, buf, std::move(*
this));
127 return STI_TRANSACTION;
141boost::container::flat_set<AccountID>
144 boost::container::flat_set<AccountID> list;
146 for (
auto const& it : *
this)
148 if (
auto sacc =
dynamic_cast<STAccount const*
>(&it))
150 XRPL_ASSERT(!sacc->isDefault(),
"xrpl::STTx::getMentionedAccounts : account is set");
151 if (!sacc->isDefault())
152 list.insert(sacc->value());
154 else if (
auto samt =
dynamic_cast<STAmount const*
>(&it))
156 auto const& issuer = samt->getIssuer();
227 target.setFieldVL(sfTxnSignature, sig);
245 Blob const& signingPubKey = sigObject.
getFieldVL(sfSigningPubKey);
252 return Unexpected(
"Internal signature check failure.");
258 if (
auto const ret =
checkSign(rules, *
this); !ret)
264 if (
auto const ret =
checkSign(rules, counterSig); !ret)
265 return Unexpected(
"Counterparty: " + ret.error());
275 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::checkBatchSign : not a batch transaction");
278 JLOG(
debugLog().fatal()) <<
"not a batch transaction";
279 return Unexpected(
"Not a batch transaction.");
282 for (
auto const& signer : signers)
284 Blob const& signingPubKey = signer.getFieldVL(sfSigningPubKey);
295 JLOG(
debugLog().error()) <<
"Batch signature check failed: " << e.
what();
297 return Unexpected(
"Internal batch signature check failure.");
322 ret[jss::tx] = dataBin;
341 "INSERT OR REPLACE INTO Transactions "
342 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
365 static boost::format bfTrans(
"('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
369 XRPL_ASSERT(format,
"xrpl::STTx::getMetaSQL : non-null type format");
384 return Unexpected(
"Cannot both single- and multi-sign.");
386 bool validSig =
false;
389 auto const spk = sigObject.
getFieldVL(sfSigningPubKey);
407Expected<void, std::string>
437 return Unexpected(
"Cannot both single- and multi-sign.");
443 return Unexpected(
"Invalid Signers array size.");
448 for (
auto const& signer : signers)
450 auto const accountID = signer.getAccountID(sfAccount);
455 if (txnAccountID == accountID)
459 if (lastAccountID == accountID)
460 return Unexpected(
"Duplicate Signers not allowed.");
463 if (lastAccountID > accountID)
467 lastAccountID = accountID;
470 bool validSig =
false;
474 auto spk = signer.getFieldVL(sfSigningPubKey);
477 Blob const signature = signer.getFieldVL(sfTxnSignature);
486 errorWhat = e.
what();
497Expected<void, std::string>
521 auto const txnAccountID =
557 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::getBatchTransactionIDs : not a batch transaction");
560 "STTx::getBatchTransactionIDs : empty raw transactions");
573 "STTx::getBatchTransactionIDs : batch transaction IDs size mismatch");
595 reason =
"The memo exceeds the maximum allowed size.";
599 for (
auto const& memo : memos)
601 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
603 if (!memoObj || (memoObj->getFName() != sfMemo))
605 reason =
"A memo array may contain only Memo objects.";
609 for (
auto const& memoElement : *memoObj)
611 auto const& name = memoElement.getFName();
613 if (name != sfMemoType && name != sfMemoData && name != sfMemoFormat)
616 "A memo may contain only MemoType, MemoData or "
617 "MemoFormat fields.";
622 auto optData =
strUnHex(memoElement.getText());
627 "The MemoType, MemoData and MemoFormat fields may "
628 "only contain hex-encoded data.";
632 if (name == sfMemoData)
643 "-._~:/?#[]@!$&'()*+,;=%"
644 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
645 "abcdefghijklmnopqrstuvwxyz");
647 for (
unsigned char c : symbols)
652 for (
unsigned char c : *optData)
654 if (!allowedSymbols[c])
657 "The MemoType and MemoFormat fields may only "
658 "contain characters that are allowed in URLs "
673 for (
int i = 0; i < st.
getCount(); ++i)
676 if (t && t->isDefault())
686 auto const txType = tx[~sfTransactionType];
691 for (
auto const& e : item->getSOTemplate())
695 if (
auto const& field = tx.
peekAtField(e.sField());
696 (field.getSType() == STI_AMOUNT &&
697 static_cast<STAmount const&
>(field).holds<MPTIssue>()) ||
698 (field.getSType() == STI_ISSUE &&
699 static_cast<STIssue const&
>(field).holds<MPTIssue>()))
719 reason =
"Batch Signers array exceeds max entries.";
726 reason =
"Raw Transactions array exceeds max entries.";
733 TxType const tt = safe_cast<TxType>(raw.getFieldU16(sfTransactionType));
736 reason =
"Raw Transactions may not contain batch transactions.";
740 raw.applyTemplate(
getTxFormat(tt)->getSOTemplate());
759 reason =
"An account field is invalid.";
765 reason =
"Cannot submit pseudo transactions.";
771 reason =
"Amount can not be MPT.";
793 auto const t = tx[~sfTransactionType];
798 auto const tt = safe_cast<TxType>(*t);
800 return tt == ttAMENDMENT || tt == ttFEE || tt == ttUNL_MODIFY;
Rules controlling protocol behavior.
A type which can be exported to a well known binary format.
static STBase * emplace(std::size_t n, void *buf, T &&val)
STBase const * peekAtPIndex(int offset) const
Json::Value getJson(JsonOptions=JsonOptions::none) const override
Blob getFieldVL(SField const &field) const
void addWithoutSigningFields(Serializer &s) const
std::uint32_t getFieldU32(SField const &field) const
void setFieldVL(SField const &field, Blob const &)
T::value_type operator[](TypedField< T > const &f) const
Get the value of a field.
void applyTemplate(SOTemplate const &type)
uint256 getHash(HashPrefix prefix) const
std::string getFullText() const override
STArray const & getFieldArray(SField const &field) const
STObject & peekFieldObject(SField const &field)
void add(Serializer &s) const override
Serializer getSerializer() const
bool isFieldPresent(SField const &field) const
void setFieldU16(SField const &field, std::uint16_t)
STBase const & peekAtField(SField const &field) const
void set(SOTemplate const &)
uint256 getSigningHash(HashPrefix prefix) const
STObject getFieldObject(SField const &field) const
AccountID getAccountID(SField const &field) const
std::uint16_t getFieldU16(SField const &field) const
std::uint32_t getFlags() const
std::string getFullText() const override
Expected< void, std::string > checkBatchSingleSign(STObject const &batchSigner) const
STBase * move(std::size_t n, void *buf) override
Blob getSignature() const
std::string getMetaSQL(std::uint32_t inLedger, std::string const &escapedMetaData) const
Expected< void, std::string > checkMultiSign(Rules const &rules, STObject const &sigObject) const
Expected< void, std::string > checkBatchSign(Rules const &rules) const
Json::Value getJson(JsonOptions options) const override
static std::string const & getMetaSQLInsertReplaceHeader()
std::vector< uint256 > const & getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
Expected< void, std::string > checkBatchMultiSign(STObject const &batchSigner, Rules const &rules) const
SeqProxy getSeqProxy() const
STBase * copy(std::size_t n, void *buf) const override
static constexpr std::size_t minMultiSigners
Expected< void, std::string > checkSign(Rules const &rules) const
Check the signature.
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
TxType getTxnType() const
std::vector< uint256 > batchTxnIds_
Expected< void, std::string > checkSingleSign(STObject const &sigObject) const
uint256 getSigningHash() const
static constexpr std::size_t maxMultiSigners
uint256 getTransactionID() const
SerializedTypeID getSType() const override
boost::container::flat_set< AccountID > getMentionedAccounts() const
void sign(PublicKey const &publicKey, SecretKey const &secretKey, std::optional< std::reference_wrapper< SField const > > signatureTarget={})
A type that represents either a sequence value or a ticket value.
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
constexpr std::uint32_t value() const
int getBytesLeft() const noexcept
Blob const & peekData() const
Slice slice() const noexcept
int getDataLength() const
An immutable linear range of bytes.
@ objectValue
object value (collection of name/value pairs).
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::size_t constexpr txMinSizeBytes
Protocol specific constants.
Serializer startMultiSigningData(STObject const &obj)
Break the multi-signing hash computation into 2 parts for optimization.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
TxType
Transaction type identifiers.
bool isXRP(AccountID const &c)
beast::Journal debugLog()
Returns a debug journal.
std::string to_string(base_uint< Bits, Tag > const &a)
std::string strHex(FwdIt begin, FwdIt end)
Expected< void, std::string > multiSignHelper(STObject const &sigObject, std::optional< AccountID > txnAccountID, std::function< Serializer(AccountID const &)> makeMsg, Rules const &rules)
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
static bool isMemoOkay(STObject const &st, std::string &reason)
static auto getTxFormat(TxType type)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
static bool isAccountFieldOkay(STObject const &st)
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
std::size_t constexpr txMaxSizeBytes
Largest legal byte size of a transaction.
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
std::vector< unsigned char > Blob
Storage for linear binary data.
bool passesLocalChecks(STObject const &st, std::string &)
static bool invalidMPTAmountInTx(STObject const &tx)
std::size_t constexpr maxBatchTxCount
The maximum number of transactions that can be in a batch.
static bool isRawTransactionOkay(STObject const &st, std::string &reason)
static Expected< void, std::string > singleSignHelper(STObject const &sigObject, Slice const &data)
@ txSign
inner transaction to sign
@ transactionID
transaction plus signature to give transaction ID
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
static Blob getSigningData(STTx const &that)
void serializeBatch(Serializer &msg, std::uint32_t const &flags, std::vector< uint256 > const &txids)
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
std::shared_ptr< STTx const > sterilize(STTx const &stx)
Sterilize a transaction.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Note, should be treated as flags that can be | and &.