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");
99 set(format->getSOTemplate());
107 LogicError(
"Transaction type was mutated during assembly");
121 return emplace(n, buf, std::move(*
this));
128 return STI_TRANSACTION;
142boost::container::flat_set<AccountID>
145 boost::container::flat_set<AccountID> list;
147 for (
auto const& it : *
this)
149 if (
auto sacc =
dynamic_cast<STAccount const*
>(&it))
153 "ripple::STTx::getMentionedAccounts : account is set");
154 if (!sacc->isDefault())
155 list.insert(sacc->value());
157 else if (
auto samt =
dynamic_cast<STAmount const*
>(&it))
159 auto const& issuer = samt->getIssuer();
230 target.setFieldVL(sfTxnSignature, sig);
251 Blob const& signingPubKey = sigObject.
getFieldVL(sfSigningPubKey);
252 return signingPubKey.
empty()
259 return Unexpected(
"Internal signature check failure.");
265 Rules const& rules)
const
267 if (
auto const ret =
checkSign(requireCanonicalSig, rules, *
this); !ret)
285 Rules const& rules)
const
291 "STTx::checkBatchSign : not a batch transaction");
294 JLOG(
debugLog().fatal()) <<
"not a batch transaction";
295 return Unexpected(
"Not a batch transaction.");
298 for (
auto const& signer : signers)
300 Blob const& signingPubKey = signer.getFieldVL(sfSigningPubKey);
301 auto const result = signingPubKey.
empty()
313 <<
"Batch signature check failed: " << e.
what();
315 return Unexpected(
"Internal batch signature check failure.");
340 ret[jss::tx] = dataBin;
359 "INSERT OR REPLACE INTO Transactions "
360 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
384 static boost::format bfTrans(
385 "('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
389 XRPL_ASSERT(format,
"ripple::STTx::getMetaSQL : non-null type format");
394 getFieldU32(sfSequence) % inLedger % status % rTxn % escapedMetaData);
407 return Unexpected(
"Cannot both single- and multi-sign.");
409 bool validSig =
false;
412 auto const spk = sigObject.
getFieldVL(sfSigningPubKey);
434Expected<void, std::string>
473 return Unexpected(
"Cannot both single- and multi-sign.");
480 return Unexpected(
"Invalid Signers array size.");
485 for (
auto const& signer : signers)
487 auto const accountID = signer.getAccountID(sfAccount);
492 if (txnAccountID == accountID)
496 if (lastAccountID == accountID)
497 return Unexpected(
"Duplicate Signers not allowed.");
500 if (lastAccountID > accountID)
504 lastAccountID = accountID;
507 bool validSig =
false;
511 auto spk = signer.getFieldVL(sfSigningPubKey);
514 Blob const signature = signer.getFieldVL(sfTxnSignature);
517 makeMsg(accountID).slice(),
526 errorWhat = e.
what();
537Expected<void, std::string>
541 Rules const& rules)
const
574 auto const txnAccountID = &sigObject !=
this
614 "STTx::getBatchTransactionIDs : not a batch transaction");
617 "STTx::getBatchTransactionIDs : empty raw transactions");
630 "STTx::getBatchTransactionIDs : batch transaction IDs size mismatch");
652 reason =
"The memo exceeds the maximum allowed size.";
656 for (
auto const& memo : memos)
658 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
660 if (!memoObj || (memoObj->getFName() != sfMemo))
662 reason =
"A memo array may contain only Memo objects.";
666 for (
auto const& memoElement : *memoObj)
668 auto const& name = memoElement.getFName();
670 if (name != sfMemoType && name != sfMemoData &&
671 name != sfMemoFormat)
674 "A memo may contain only MemoType, MemoData or "
675 "MemoFormat fields.";
680 auto optData =
strUnHex(memoElement.getText());
685 "The MemoType, MemoData and MemoFormat fields may "
686 "only contain hex-encoded data.";
690 if (name == sfMemoData)
701 "-._~:/?#[]@!$&'()*+,;=%"
702 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
703 "abcdefghijklmnopqrstuvwxyz");
705 for (
unsigned char c : symbols)
710 for (
unsigned char c : *optData)
712 if (!allowedSymbols[c])
715 "The MemoType and MemoFormat fields may only "
716 "contain characters that are allowed in URLs "
731 for (
int i = 0; i < st.
getCount(); ++i)
734 if (t && t->isDefault())
744 auto const txType = tx[~sfTransactionType];
747 if (
auto const* item =
750 for (
auto const& e : item->getSOTemplate())
754 if (
auto const& field = tx.
peekAtField(e.sField());
755 (field.getSType() == STI_AMOUNT &&
756 static_cast<STAmount const&
>(field).holds<MPTIssue>()) ||
757 (field.getSType() == STI_ISSUE &&
758 static_cast<STIssue const&
>(field).holds<MPTIssue>()))
778 reason =
"Batch Signers array exceeds max entries.";
785 reason =
"Raw Transactions array exceeds max entries.";
793 safe_cast<TxType>(raw.getFieldU16(sfTransactionType));
796 reason =
"Raw Transactions may not contain batch transactions.";
800 raw.applyTemplate(
getTxFormat(tt)->getSOTemplate());
819 reason =
"An account field is invalid.";
825 reason =
"Cannot submit pseudo transactions.";
831 reason =
"Amount can not be MPT.";
853 auto const t = tx[~sfTransactionType];
858 auto const tt = safe_cast<TxType>(*t);
860 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
STObject & peekFieldObject(SField const &field)
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
uint256 getSigningHash() const
static std::string const & getMetaSQLInsertReplaceHeader()
std::vector< uint256 > batchTxnIds_
Expected< void, std::string > checkSingleSign(RequireFullyCanonicalSig requireCanonicalSig, STObject const &sigObject) const
SeqProxy getSeqProxy() const
static constexpr std::size_t minMultiSigners
Json::Value getJson(JsonOptions options) const override
Expected< void, std::string > checkBatchSingleSign(STObject const &batchSigner, RequireFullyCanonicalSig requireCanonicalSig) const
std::vector< uint256 > const & getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
STBase * move(std::size_t n, void *buf) override
static constexpr std::size_t maxMultiSigners
Expected< void, std::string > checkSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
Check the signature.
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).
void sign(PublicKey const &publicKey, SecretKey const &secretKey, std::optional< std::reference_wrapper< SField const > > signatureTarget={})
TxType getTxnType() const
std::string getMetaSQL(std::uint32_t inLedger, std::string const &escapedMetaData) const
uint256 getTransactionID() const
Expected< void, std::string > checkMultiSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules, STObject const &sigObject) 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)
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)
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.
Expected< void, std::string > multiSignHelper(STObject const &sigObject, std::optional< AccountID > txnAccountID, bool const fullyCanonical, std::function< Serializer(AccountID const &)> makeMsg, Rules const &rules)
@ 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.
static Expected< void, std::string > singleSignHelper(STObject const &sigObject, Slice const &data, bool const fullyCanonical)
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 &.