1#include <xrpl/protocol/STTx.h>
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/basics/StringUtilities.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/basics/contract.h>
9#include <xrpl/basics/safe_cast.h>
10#include <xrpl/basics/strHex.h>
11#include <xrpl/beast/utility/Zero.h>
12#include <xrpl/beast/utility/instrumentation.h>
13#include <xrpl/json/json_value.h>
14#include <xrpl/protocol/AccountID.h>
15#include <xrpl/protocol/Batch.h>
16#include <xrpl/protocol/HashPrefix.h>
17#include <xrpl/protocol/MPTIssue.h>
18#include <xrpl/protocol/Protocol.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/Rules.h>
21#include <xrpl/protocol/SField.h>
22#include <xrpl/protocol/SOTemplate.h>
23#include <xrpl/protocol/STAccount.h>
24#include <xrpl/protocol/STAmount.h>
25#include <xrpl/protocol/STArray.h>
26#include <xrpl/protocol/STBase.h>
27#include <xrpl/protocol/STObject.h>
28#include <xrpl/protocol/SecretKey.h>
29#include <xrpl/protocol/SeqProxy.h>
30#include <xrpl/protocol/Serializer.h>
31#include <xrpl/protocol/Sign.h>
32#include <xrpl/protocol/TxFormats.h>
33#include <xrpl/protocol/jss.h>
35#include <boost/container/flat_set.hpp>
36#include <boost/format/free_funcs.hpp>
60 if (format ==
nullptr)
63 "Invalid transaction type " +
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();
242 target.setFieldVL(sfTxnSignature, sig);
251std::expected<void, std::string>
260 Blob const& signingPubKey = sigObject.
getFieldVL(sfSigningPubKey);
270std::expected<void, std::string>
273 if (
auto const ret =
checkSign(rules, *
this); !ret)
279 if (
auto const ret =
checkSign(rules, counterSig); !ret)
285std::expected<void, std::string>
290 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::checkBatchSign : not a batch transaction");
293 JLOG(
debugLog().fatal()) <<
"not a batch transaction";
297 for (
auto const& signer : signers)
299 Blob const& signingPubKey = signer.getFieldVL(sfSigningPubKey);
310 JLOG(
debugLog().error()) <<
"Batch signature check failed: " << e.
what();
337 ret[jss::tx] = dataBin;
356 "INSERT OR REPLACE INTO Transactions "
357 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
380 static boost::format
const kBfTrans(
"('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
384 XRPL_ASSERT(format,
"xrpl::STTx::getMetaSQL : non-null type format");
392static std::expected<void, std::string>
401 bool validSig =
false;
404 auto const spk = sigObject.
getFieldVL(sfSigningPubKey);
422std::expected<void, std::string>
429std::expected<void, std::string>
437std::expected<void, std::string>
463 for (
auto const& signer : signers)
465 auto const accountID = signer.getAccountID(sfAccount);
470 if (txnAccountID == accountID)
474 if (lastAccountID == accountID)
478 if (lastAccountID > accountID)
482 lastAccountID = accountID;
485 bool validSig =
false;
489 auto spk = signer.getFieldVL(sfSigningPubKey);
492 Blob const signature = signer.getFieldVL(sfTxnSignature);
501 errorWhat = e.
what();
514std::expected<void, std::string>
533std::expected<void, std::string>
540 auto const txnAccountID =
576 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::getBatchTransactionIDs : not a batch transaction");
579 "STTx::getBatchTransactionIDs : empty raw transactions");
592 "STTx::getBatchTransactionIDs : batch transaction IDs size mismatch");
614 reason =
"The memo exceeds the maximum allowed size.";
618 for (
auto const& memo : memos)
620 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
622 if ((memoObj ==
nullptr) || (memoObj->getFName() != sfMemo))
624 reason =
"A memo array may contain only Memo objects.";
628 for (
auto const& memoElement : *memoObj)
630 auto const& name = memoElement.getFName();
632 if (name != sfMemoType && name != sfMemoData && name != sfMemoFormat)
635 "A memo may contain only MemoType, MemoData or "
636 "MemoFormat fields.";
641 auto optData =
strUnHex(memoElement.getText());
646 "The MemoType, MemoData and MemoFormat fields may "
647 "only contain hex-encoded data.";
651 if (name == sfMemoData)
662 "-._~:/?#[]@!$&'()*+,;=%"
663 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
664 "abcdefghijklmnopqrstuvwxyz");
666 for (
unsigned char const c : symbols)
671 for (
unsigned char const c : *optData)
673 if (kAllowedSymbols[c] == 0)
676 "The MemoType and MemoFormat fields may only "
677 "contain characters that are allowed in URLs "
692 for (
int i = 0; i < st.
getCount(); ++i)
695 if ((t !=
nullptr) && t->isDefault())
705 auto const txType = tx[~sfTransactionType];
710 for (
auto const& e : item->getSOTemplate())
714 if (
auto const& field = tx.
peekAtField(e.sField());
715 (field.getSType() == STI_AMOUNT &&
717 (field.getSType() == STI_ISSUE &&
738 reason =
"Batch Signers array exceeds max entries.";
745 reason =
"Raw Transactions array exceeds max entries.";
755 reason =
"Raw Transactions may not contain batch transactions.";
759 raw.applyTemplate(
getTxFormat(tt)->getSOTemplate());
778 reason =
"An account field is invalid.";
784 reason =
"Cannot submit pseudo transactions.";
790 reason =
"Amount can not be MPT.";
812 auto const t = tx[~sfTransactionType];
819 return tt == ttAMENDMENT || tt == ttFEE || tt == ttUNL_MODIFY;
UInt size() const
Number of values in array or object.
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
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)
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
void add(Serializer &s) const override
Serializer getSerializer() const
bool isFieldPresent(SField const &field) const
STObject(STObject const &)=default
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
STBase * move(std::size_t n, void *buf) override
Blob getSignature() const
std::string getMetaSQL(std::uint32_t inLedger, std::string const &escapedMetaData) const
std::expected< void, std::string > checkBatchSign(Rules const &rules) const
static std::string const & getMetaSQLInsertReplaceHeader()
std::expected< void, std::string > checkBatchSingleSign(STObject const &batchSigner) const
std::vector< uint256 > const & getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
std::expected< void, std::string > checkBatchMultiSign(STObject const &batchSigner, Rules const &rules) const
static constexpr std::size_t kMinMultiSigners
SeqProxy getSeqProxy() const
AccountID getFeePayer() const
std::expected< void, std::string > checkSign(Rules const &rules) const
Check the signature.
STBase * copy(std::size_t n, void *buf) const override
static constexpr std::size_t kMaxMultiSigners
std::expected< void, std::string > checkSingleSign(STObject const &sigObject) const
std::expected< void, std::string > checkMultiSign(Rules const &rules, STObject const &sigObject) const
std::uint32_t getSeqValue() const
Returns the first non-zero value of (Sequence, TicketSequence).
TxType getTxnType() const
std::vector< uint256 > batchTxnIds_
uint256 getSigningHash() const
json::Value getJson(JsonOptions options) const override
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.
@ Object
object value (collection of name/value pairs).
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Serializer startMultiSigningData(STObject const &obj)
Break the multi-signing hash computation into 2 parts for optimization.
Dest safeDowncast(Src *s) noexcept
TxType
Transaction type identifiers.
bool isXRP(AccountID const &c)
beast::Journal debugLog()
Returns a debug journal.
std::string strHex(FwdIt begin, FwdIt end)
constexpr std::size_t kMaxBatchTxCount
The maximum number of transactions that can be in a batch.
constexpr std::size_t kTxMinSizeBytes
Protocol specific constants.
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
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::string to_string(BaseUInt< Bits, Tag > const &a)
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
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)
static std::expected< void, std::string > singleSignHelper(STObject const &sigObject, Slice const &data)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
bool passesLocalChecks(STObject const &st, std::string &)
static bool invalidMPTAmountInTx(STObject const &tx)
static bool isRawTransactionOkay(STObject const &st, std::string &reason)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
@ 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::vector< unsigned char > Blob
Storage for linear binary data.
std::shared_ptr< STTx const > sterilize(STTx const &stx)
Sterilize a transaction.
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
std::expected< void, std::string > multiSignHelper(STObject const &sigObject, std::optional< AccountID > txnAccountID, std::function< Serializer(AccountID const &)> makeMsg, Rules const &rules)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)
constexpr std::size_t kTxMaxSizeBytes
Largest legal byte size of a transaction.
Note, should be treated as flags that can be | and &.