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>(
82 Throw<std::runtime_error>(
"Transaction length invalid");
85 Throw<std::runtime_error>(
"Transaction contains an object terminator");
97 set(format->getSOTemplate());
105 LogicError(
"Transaction type was mutated during assembly");
119 return emplace(n, buf, std::move(*
this));
126 return STI_TRANSACTION;
140boost::container::flat_set<AccountID>
143 boost::container::flat_set<AccountID> list;
145 for (
auto const& it : *
this)
147 if (
auto sacc =
dynamic_cast<STAccount const*
>(&it))
149 XRPL_ASSERT(!sacc->isDefault(),
"xrpl::STTx::getMentionedAccounts : account is set");
150 if (!sacc->isDefault())
151 list.insert(sacc->value());
153 else if (
auto samt =
dynamic_cast<STAmount const*
>(&it))
155 auto const& issuer = samt->getIssuer();
226 target.setFieldVL(sfTxnSignature, sig);
244 Blob const& signingPubKey = sigObject.
getFieldVL(sfSigningPubKey);
250 return Unexpected(
"Internal signature check failure.");
256 if (
auto const ret =
checkSign(rules, *
this); !ret)
262 if (
auto const ret =
checkSign(rules, counterSig); !ret)
263 return Unexpected(
"Counterparty: " + ret.error());
273 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::checkBatchSign : not a batch transaction");
276 JLOG(
debugLog().fatal()) <<
"not a batch transaction";
277 return Unexpected(
"Not a batch transaction.");
280 for (
auto const& signer : signers)
282 Blob const& signingPubKey = signer.getFieldVL(sfSigningPubKey);
293 JLOG(
debugLog().error()) <<
"Batch signature check failed: " << e.
what();
295 return Unexpected(
"Internal batch signature check failure.");
320 ret[jss::tx] = dataBin;
339 "INSERT OR REPLACE INTO Transactions "
340 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
359 static boost::format bfTrans(
"('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
363 XRPL_ASSERT(format,
"xrpl::STTx::getMetaSQL : non-null type format");
367 getFieldU32(sfSequence) % inLedger % status % rTxn % escapedMetaData);
377 return Unexpected(
"Cannot both single- and multi-sign.");
379 bool validSig =
false;
382 auto const spk = sigObject.
getFieldVL(sfSigningPubKey);
400Expected<void, std::string>
430 return Unexpected(
"Cannot both single- and multi-sign.");
436 return Unexpected(
"Invalid Signers array size.");
441 for (
auto const& signer : signers)
443 auto const accountID = signer.getAccountID(sfAccount);
448 if (txnAccountID == accountID)
452 if (lastAccountID == accountID)
453 return Unexpected(
"Duplicate Signers not allowed.");
456 if (lastAccountID > accountID)
460 lastAccountID = accountID;
463 bool validSig =
false;
467 auto spk = signer.getFieldVL(sfSigningPubKey);
470 Blob const signature = signer.getFieldVL(sfTxnSignature);
478 errorWhat = e.
what();
488Expected<void, std::string>
547 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::getBatchTransactionIDs : not a batch transaction");
548 XRPL_ASSERT(
getFieldArray(sfRawTransactions).size() != 0,
"STTx::getBatchTransactionIDs : empty raw transactions");
561 "STTx::getBatchTransactionIDs : batch transaction IDs size mismatch");
583 reason =
"The memo exceeds the maximum allowed size.";
587 for (
auto const& memo : memos)
589 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
591 if (!memoObj || (memoObj->getFName() != sfMemo))
593 reason =
"A memo array may contain only Memo objects.";
597 for (
auto const& memoElement : *memoObj)
599 auto const& name = memoElement.getFName();
601 if (name != sfMemoType && name != sfMemoData && name != sfMemoFormat)
604 "A memo may contain only MemoType, MemoData or "
605 "MemoFormat fields.";
610 auto optData =
strUnHex(memoElement.getText());
615 "The MemoType, MemoData and MemoFormat fields may "
616 "only contain hex-encoded data.";
620 if (name == sfMemoData)
631 "-._~:/?#[]@!$&'()*+,;=%"
632 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
633 "abcdefghijklmnopqrstuvwxyz");
635 for (
unsigned char c : symbols)
640 for (
unsigned char c : *optData)
642 if (!allowedSymbols[c])
645 "The MemoType and MemoFormat fields may only "
646 "contain characters that are allowed in URLs "
661 for (
int i = 0; i < st.
getCount(); ++i)
664 if (t && t->isDefault())
674 auto const txType = tx[~sfTransactionType];
679 for (
auto const& e : item->getSOTemplate())
683 if (
auto const& field = tx.
peekAtField(e.sField());
684 (field.getSType() == STI_AMOUNT &&
static_cast<STAmount const&
>(field).holds<MPTIssue>()) ||
685 (field.getSType() == STI_ISSUE &&
static_cast<STIssue const&
>(field).holds<MPTIssue>()))
704 reason =
"Batch Signers array exceeds max entries.";
711 reason =
"Raw Transactions array exceeds max entries.";
718 TxType const tt = safe_cast<TxType>(raw.getFieldU16(sfTransactionType));
721 reason =
"Raw Transactions may not contain batch transactions.";
725 raw.applyTemplate(
getTxFormat(tt)->getSOTemplate());
744 reason =
"An account field is invalid.";
750 reason =
"Cannot submit pseudo transactions.";
756 reason =
"Amount can not be MPT.";
778 auto const t = tx[~sfTransactionType];
783 auto const tt = safe_cast<TxType>(*t);
785 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 &.