20#include <xrpl/basics/Blob.h>
21#include <xrpl/basics/Expected.h>
22#include <xrpl/basics/Log.h>
23#include <xrpl/basics/Slice.h>
24#include <xrpl/basics/StringUtilities.h>
25#include <xrpl/basics/base_uint.h>
26#include <xrpl/basics/contract.h>
27#include <xrpl/basics/safe_cast.h>
28#include <xrpl/basics/strHex.h>
29#include <xrpl/beast/utility/Zero.h>
30#include <xrpl/beast/utility/instrumentation.h>
31#include <xrpl/json/json_value.h>
32#include <xrpl/protocol/AccountID.h>
33#include <xrpl/protocol/Batch.h>
34#include <xrpl/protocol/HashPrefix.h>
35#include <xrpl/protocol/MPTIssue.h>
36#include <xrpl/protocol/Protocol.h>
37#include <xrpl/protocol/PublicKey.h>
38#include <xrpl/protocol/Rules.h>
39#include <xrpl/protocol/SField.h>
40#include <xrpl/protocol/SOTemplate.h>
41#include <xrpl/protocol/STAccount.h>
42#include <xrpl/protocol/STAmount.h>
43#include <xrpl/protocol/STArray.h>
44#include <xrpl/protocol/STBase.h>
45#include <xrpl/protocol/STObject.h>
46#include <xrpl/protocol/STTx.h>
47#include <xrpl/protocol/STVector256.h>
48#include <xrpl/protocol/SecretKey.h>
49#include <xrpl/protocol/SeqProxy.h>
50#include <xrpl/protocol/Serializer.h>
51#include <xrpl/protocol/Sign.h>
52#include <xrpl/protocol/TxFlags.h>
53#include <xrpl/protocol/TxFormats.h>
54#include <xrpl/protocol/jss.h>
56#include <boost/container/flat_set.hpp>
57#include <boost/format/format_fwd.hpp>
58#include <boost/format/free_funcs.hpp>
80 if (format ==
nullptr)
82 Throw<std::runtime_error>(
83 "Invalid transaction type " +
102 Throw<std::runtime_error>(
"Transaction length invalid");
105 Throw<std::runtime_error>(
"Transaction contains an object terminator");
118 set(format->getSOTemplate());
126 LogicError(
"Transaction type was mutated during assembly");
140 return emplace(n, buf, std::move(*
this));
147 return STI_TRANSACTION;
161boost::container::flat_set<AccountID>
164 boost::container::flat_set<AccountID> list;
166 for (
auto const& it : *
this)
168 if (
auto sacc =
dynamic_cast<STAccount const*
>(&it))
172 "ripple::STTx::getMentionedAccounts : account is set");
173 if (!sacc->isDefault())
174 list.insert(sacc->value());
176 else if (
auto samt =
dynamic_cast<STAmount const*
>(&it))
178 auto const& issuer = samt->getIssuer();
250 Rules const& rules)
const
258 return signingPubKey.
empty()
265 return Unexpected(
"Internal signature check failure.");
271 Rules const& rules)
const
277 "STTx::checkBatchSign : not a batch transaction");
280 JLOG(
debugLog().fatal()) <<
"not a batch transaction";
281 return Unexpected(
"Not a batch transaction.");
284 for (
auto const& signer : signers)
286 Blob const& signingPubKey = signer.getFieldVL(sfSigningPubKey);
287 auto const result = signingPubKey.
empty()
299 <<
"Batch signature check failed: " << e.
what();
301 return Unexpected(
"Internal batch signature check failure.");
326 ret[jss::tx] = dataBin;
345 "INSERT OR REPLACE INTO Transactions "
346 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
370 static boost::format bfTrans(
371 "('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
375 XRPL_ASSERT(format,
"ripple::STTx::getMetaSQL : non-null type format");
380 getFieldU32(sfSequence) % inLedger % status % rTxn % escapedMetaData);
393 return Unexpected(
"Cannot both single- and multi-sign.");
395 bool validSig =
false;
398 auto const spk = signer.
getFieldVL(sfSigningPubKey);
420Expected<void, std::string>
456 return Unexpected(
"Cannot both single- and multi-sign.");
463 return Unexpected(
"Invalid Signers array size.");
466 auto const txnAccountID = signerObj.
getAccountID(sfAccount);
471 for (
auto const& signer : signers)
473 auto const accountID = signer.getAccountID(sfAccount);
476 if (accountID == txnAccountID)
480 if (lastAccountID == accountID)
481 return Unexpected(
"Duplicate Signers not allowed.");
484 if (lastAccountID > accountID)
488 lastAccountID = accountID;
491 bool validSig =
false;
494 auto spk = signer.getFieldVL(sfSigningPubKey);
497 Blob const signature = signer.getFieldVL(sfTxnSignature);
500 makeMsg(accountID).slice(),
519Expected<void, std::string>
523 Rules const& rules)
const
547 Rules const& rules)
const
587 "STTx::getBatchTransactionIDs : not a batch transaction");
590 "STTx::getBatchTransactionIDs : empty raw transactions");
599 "STTx::getBatchTransactionIDs : batch transaction IDs size mismatch");
621 reason =
"The memo exceeds the maximum allowed size.";
625 for (
auto const& memo : memos)
627 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
629 if (!memoObj || (memoObj->getFName() != sfMemo))
631 reason =
"A memo array may contain only Memo objects.";
635 for (
auto const& memoElement : *memoObj)
637 auto const& name = memoElement.getFName();
639 if (name != sfMemoType && name != sfMemoData &&
640 name != sfMemoFormat)
643 "A memo may contain only MemoType, MemoData or "
644 "MemoFormat fields.";
649 auto optData =
strUnHex(memoElement.getText());
654 "The MemoType, MemoData and MemoFormat fields may "
655 "only contain hex-encoded data.";
659 if (name == sfMemoData)
670 "-._~:/?#[]@!$&'()*+,;=%"
671 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
672 "abcdefghijklmnopqrstuvwxyz");
674 for (
unsigned char c : symbols)
679 for (
unsigned char c : *optData)
681 if (!allowedSymbols[c])
684 "The MemoType and MemoFormat fields may only "
685 "contain characters that are allowed in URLs "
700 for (
int i = 0; i < st.
getCount(); ++i)
703 if (t && t->isDefault())
713 auto const txType = tx[~sfTransactionType];
716 if (
auto const* item =
719 for (
auto const& e : item->getSOTemplate())
723 if (
auto const& field = tx.
peekAtField(e.sField());
724 (field.getSType() == STI_AMOUNT &&
725 static_cast<STAmount const&
>(field).holds<MPTIssue>()) ||
726 (field.getSType() == STI_ISSUE &&
727 static_cast<STIssue const&
>(field).holds<MPTIssue>()))
747 reason =
"Batch Signers array exceeds max entries.";
754 reason =
"Raw Transactions array exceeds max entries.";
762 safe_cast<TxType>(raw.getFieldU16(sfTransactionType));
765 reason =
"Raw Transactions may not contain batch transactions.";
769 raw.applyTemplate(
getTxFormat(tt)->getSOTemplate());
788 reason =
"An account field is invalid.";
794 reason =
"Cannot submit pseudo transactions.";
800 reason =
"Amount can not be MPT.";
822 auto const t = tx[~sfTransactionType];
827 auto const tt = safe_cast<TxType>(*t);
829 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)
void applyTemplate(SOTemplate const &type)
Blob getFieldVL(SField const &field) const
AccountID getAccountID(SField const &field) const
STArray const & getFieldArray(SField const &field) const
STBase const * peekAtPIndex(int offset) 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)
Serializer getSerializer() const
void set(SOTemplate 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
std::string getFullText() const override
bool isFieldPresent(SField const &field) const
Json::Value getJson(JsonOptions=JsonOptions::none) const override
STBase const & peekAtField(SField const &field) const
void addWithoutSigningFields(Serializer &s) const
void setFieldVL(SField const &field, Blob const &)
std::uint32_t getFlags() const
boost::container::flat_set< AccountID > getMentionedAccounts() const
std::vector< uint256 > getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
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
Expected< void, std::string > checkBatchSingleSign(STObject const &batchSigner, 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.
std::vector< uint256 > batch_txn_ids_
Expected< void, std::string > checkBatchMultiSign(STObject const &batchSigner, RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
Blob getSignature() const
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
TxType getTxnType() 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
Expected< void, std::string > checkBatchSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
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.
constexpr std::uint32_t value() const
int getBytesLeft() const noexcept
Slice slice() const noexcept
Blob const & peekData() const
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::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)
static bool isRawTransactionOkay(STObject const &st, std::string &reason)
void serializeBatch(Serializer &msg, std::uint32_t const &flags, std::vector< uint256 > const &txids)
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)
static Expected< void, std::string > singleSignHelper(STObject const &signer, Slice const &data, bool const fullyCanonical)
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)
Expected< void, std::string > multiSignHelper(STObject const &signerObj, bool const fullyCanonical, std::function< Serializer(AccountID const &)> makeMsg, Rules const &rules)
beast::Journal debugLog()
Returns a debug journal.
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
std::size_t constexpr maxBatchTxCount
The maximum number of transactions that can be in a batch.
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 &.