20#include <xrpl/basics/Blob.h>
21#include <xrpl/basics/Expected.h>
22#include <xrpl/basics/Slice.h>
23#include <xrpl/basics/StringUtilities.h>
24#include <xrpl/basics/base_uint.h>
25#include <xrpl/basics/contract.h>
26#include <xrpl/basics/safe_cast.h>
27#include <xrpl/basics/strHex.h>
28#include <xrpl/beast/utility/Zero.h>
29#include <xrpl/beast/utility/instrumentation.h>
30#include <xrpl/json/json_value.h>
31#include <xrpl/protocol/AccountID.h>
32#include <xrpl/protocol/HashPrefix.h>
33#include <xrpl/protocol/MPTIssue.h>
34#include <xrpl/protocol/Protocol.h>
35#include <xrpl/protocol/PublicKey.h>
36#include <xrpl/protocol/Rules.h>
37#include <xrpl/protocol/SField.h>
38#include <xrpl/protocol/SOTemplate.h>
39#include <xrpl/protocol/STAccount.h>
40#include <xrpl/protocol/STAmount.h>
41#include <xrpl/protocol/STArray.h>
42#include <xrpl/protocol/STBase.h>
43#include <xrpl/protocol/STObject.h>
44#include <xrpl/protocol/STTx.h>
45#include <xrpl/protocol/SecretKey.h>
46#include <xrpl/protocol/SeqProxy.h>
47#include <xrpl/protocol/Serializer.h>
48#include <xrpl/protocol/Sign.h>
49#include <xrpl/protocol/TxFlags.h>
50#include <xrpl/protocol/TxFormats.h>
51#include <xrpl/protocol/jss.h>
53#include <boost/container/flat_set.hpp>
54#include <boost/format/format_fwd.hpp>
55#include <boost/format/free_funcs.hpp>
77 if (format ==
nullptr)
79 Throw<std::runtime_error>(
80 "Invalid transaction type " +
99 Throw<std::runtime_error>(
"Transaction length invalid");
102 Throw<std::runtime_error>(
"Transaction contains an object terminator");
115 set(format->getSOTemplate());
123 LogicError(
"Transaction type was mutated during assembly");
137 return emplace(n, buf, std::move(*
this));
144 return STI_TRANSACTION;
158boost::container::flat_set<AccountID>
161 boost::container::flat_set<AccountID> list;
163 for (
auto const& it : *
this)
165 if (
auto sacc =
dynamic_cast<STAccount const*
>(&it))
169 "ripple::STTx::getMentionedAccounts : account is set");
170 if (!sacc->isDefault())
171 list.insert(sacc->value());
173 else if (
auto samt =
dynamic_cast<STAmount const*
>(&it))
175 auto const& issuer = samt->getIssuer();
241 Rules const& rules)
const
249 return signingPubKey.
empty()
256 return Unexpected(
"Internal signature check failure.");
281 ret[jss::tx] = dataBin;
300 "INSERT OR REPLACE INTO Transactions "
301 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
325 static boost::format bfTrans(
326 "('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
330 XRPL_ASSERT(format,
"ripple::STTx::getMetaSQL : non-null type format");
335 getFieldU32(sfSequence) % inLedger % status % rTxn % escapedMetaData);
345 return Unexpected(
"Cannot both single- and multi-sign.");
347 bool validSig =
false;
372 if (validSig ==
false)
381 Rules const& rules)
const
391 return Unexpected(
"Cannot both single- and multi-sign.");
398 return Unexpected(
"Invalid Signers array size.");
415 for (
auto const& signer : signers)
417 auto const accountID = signer.getAccountID(sfAccount);
420 if (accountID == txnAccountID)
424 if (lastAccountID == accountID)
425 return Unexpected(
"Duplicate Signers not allowed.");
428 if (lastAccountID > accountID)
432 lastAccountID = accountID;
435 bool validSig =
false;
441 auto spk = signer.getFieldVL(sfSigningPubKey);
445 Blob const signature = signer.getFieldVL(sfTxnSignature);
486 reason =
"The memo exceeds the maximum allowed size.";
490 for (
auto const& memo : memos)
492 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
494 if (!memoObj || (memoObj->getFName() != sfMemo))
496 reason =
"A memo array may contain only Memo objects.";
500 for (
auto const& memoElement : *memoObj)
502 auto const& name = memoElement.getFName();
504 if (name != sfMemoType && name != sfMemoData &&
505 name != sfMemoFormat)
508 "A memo may contain only MemoType, MemoData or "
509 "MemoFormat fields.";
514 auto optData =
strUnHex(memoElement.getText());
519 "The MemoType, MemoData and MemoFormat fields may "
520 "only contain hex-encoded data.";
524 if (name == sfMemoData)
535 "-._~:/?#[]@!$&'()*+,;=%"
536 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
537 "abcdefghijklmnopqrstuvwxyz");
539 for (
char c : symbols)
544 for (
auto c : *optData)
546 if (!allowedSymbols[c])
549 "The MemoType and MemoFormat fields may only "
550 "contain characters that are allowed in URLs "
565 for (
int i = 0; i < st.
getCount(); ++i)
568 if (t && t->isDefault())
578 auto const txType = tx[~sfTransactionType];
581 if (
auto const* item =
584 for (
auto const& e : item->getSOTemplate())
588 if (
auto const& field = tx.
peekAtField(e.sField());
589 (field.getSType() == STI_AMOUNT &&
591 (field.getSType() == STI_ISSUE &&
611 reason =
"An account field is invalid.";
617 reason =
"Cannot submit pseudo transactions.";
623 reason =
"Amount can not be MPT.";
636 return std::make_shared<STTx const>(
std::ref(sit));
642 auto t = tx[~sfTransactionType];
645 auto tt = safe_cast<TxType>(*t);
646 return tt == ttAMENDMENT || tt == ttFEE || tt == ttUNL_MODIFY;
Rules controlling protocol behavior.
constexpr bool holds() const noexcept
A type which can be exported to a well known binary format.
static STBase * emplace(std::size_t n, void *buf, T &&val)
Blob getFieldVL(SField const &field) const
AccountID getAccountID(SField const &field) const
const STArray & getFieldArray(SField const &field) const
std::uint16_t getFieldU16(SField const &field) const
std::uint32_t getFieldU32(SField const &field) const
void setFieldU16(SField const &field, std::uint16_t)
const STBase * peekAtPIndex(int offset) const
Serializer getSerializer() const
void add(Serializer &s) const override
uint256 getSigningHash(HashPrefix prefix) const
T::value_type operator[](TypedField< T > const &f) const
Get the value of a field.
uint256 getHash(HashPrefix prefix) const
void set(const SOTemplate &)
std::string getFullText() const override
const STBase & peekAtField(SField const &field) const
bool isFieldPresent(SField const &field) const
Json::Value getJson(JsonOptions options) const override
void applyTemplate(const SOTemplate &type)
void addWithoutSigningFields(Serializer &s) const
void setFieldVL(SField const &field, Blob const &)
std::uint32_t getFlags() const
boost::container::flat_set< AccountID > getMentionedAccounts() const
uint256 getSigningHash() const
static std::string const & getMetaSQLInsertReplaceHeader()
void sign(PublicKey const &publicKey, SecretKey const &secretKey)
Expected< void, std::string > checkMultiSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
SeqProxy getSeqProxy() const
Json::Value getJson(JsonOptions options) const override
Expected< void, std::string > checkSingleSign(RequireFullyCanonicalSig requireCanonicalSig) const
STBase * move(std::size_t n, void *buf) override
static std::size_t const minMultiSigners
Expected< void, std::string > checkSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
static std::size_t maxMultiSigners(Rules const *rules=0)
RequireFullyCanonicalSig
Check the signature.
Blob getSignature() const
std::string getMetaSQL(std::uint32_t inLedger, std::string const &escapedMetaData) const
uint256 getTransactionID() const
SerializedTypeID getSType() const override
std::string getFullText() const override
STBase * copy(std::size_t n, void *buf) const override
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.
int getBytesLeft() const noexcept
Slice slice() const noexcept
Blob const & peekData() const
int getDataLength() const
@ objectValue
object value (collection of name/value pairs).
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::shared_ptr< STTx const > sterilize(STTx const &stx)
Sterilize a transaction.
bool isXRP(AccountID const &c)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
static bool isAccountFieldOkay(STObject const &st)
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
TxType
Transaction type identifiers.
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Serializer startMultiSigningData(STObject const &obj)
Break the multi-signing hash computation into 2 parts for optimization.
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
static bool isMemoOkay(STObject const &st, std::string &reason)
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
std::size_t constexpr txMinSizeBytes
Protocol specific constants.
static auto getTxFormat(TxType type)
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
static bool invalidMPTAmountInTx(STObject const &tx)
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
std::string strHex(FwdIt begin, FwdIt end)
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::vector< unsigned char > Blob
Storage for linear binary data.
static Blob getSigningData(STTx const &that)
bool passesLocalChecks(STObject const &st, std::string &)
std::size_t constexpr txMaxSizeBytes
Largest legal byte size of a transaction.
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr std::uint32_t tfFullyCanonicalSig
Transaction flags.
@ txSign
inner transaction to sign
@ transactionID
transaction plus signature to give transaction ID
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Note, should be treated as flags that can be | and &.