1#include <xrpl/protocol/STTx.h>
3#include <xrpl/basics/Blob.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/SecretKey.h>
28#include <xrpl/protocol/SeqProxy.h>
29#include <xrpl/protocol/Serializer.h>
30#include <xrpl/protocol/Sign.h>
31#include <xrpl/protocol/TxFlags.h>
32#include <xrpl/protocol/TxFormats.h>
33#include <xrpl/protocol/jss.h>
35#include <boost/container/flat_set.hpp>
60 if (format ==
nullptr)
63 "Invalid transaction type " +
99 set(format->getSOTemplate());
110 logicError(
"Transaction type was mutated during assembly");
125 return emplace(n, buf, std::move(*
this));
132 return STI_TRANSACTION;
146boost::container::flat_set<AccountID>
149 boost::container::flat_set<AccountID> list;
151 for (
auto const& it : *
this)
153 if (
auto sacc =
dynamic_cast<STAccount const*
>(&it))
155 XRPL_ASSERT(!sacc->isDefault(),
"xrpl::STTx::getMentionedAccounts : account is set");
156 if (!sacc->isDefault())
157 list.insert(sacc->value());
159 else if (
auto samt =
dynamic_cast<STAmount const*
>(&it))
161 auto const& issuer = samt->getIssuer();
228 target.setFieldVL(sfTxnSignature, sig);
237std::expected<void, std::string>
246 Blob const& signingPubKey = sigObject.
getFieldVL(sfSigningPubKey);
256std::expected<void, std::string>
259 if (
auto const ret =
checkSign(rules, *
this); !ret)
265 if (
auto const ret =
checkSign(rules, counterSig); !ret)
271 auto const sponsorSignatureObj =
getFieldObject(sfSponsorSignature);
272 if (
auto const ret =
checkSign(rules, sponsorSignatureObj); !ret)
286std::expected<void, std::string>
294 UNREACHABLE(
"STTx::checkBatchSign : not a batch transaction");
313 UNREACHABLE(
"STTx::checkBatchSign : batch transactions not built");
318 for (
auto const& signer : signers)
320 Blob const& signingPubKey = signer.getFieldVL(sfSigningPubKey);
359 ret[jss::tx] = dataBin;
378 "INSERT OR REPLACE INTO Transactions "
379 "(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
405 XRPL_ASSERT(format,
"xrpl::STTx::getMetaSQL : non-null type format");
408 "('{}', '{}', '{}', '{}', '{}', '{}', {}, {})",
419static std::expected<void, std::string>
428 bool validSig =
false;
431 auto const spk = sigObject.
getFieldVL(sfSigningPubKey);
449std::expected<void, std::string>
456std::expected<void, std::string>
459 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::checkBatchSingleSign : batch transaction");
466std::expected<void, std::string>
492 for (
auto const& signer : signers)
494 auto const accountID = signer.getAccountID(sfAccount);
499 if (txnAccountID == accountID)
503 if (lastAccountID == accountID)
507 if (lastAccountID > accountID)
511 lastAccountID = accountID;
514 bool validSig =
false;
518 auto spk = signer.getFieldVL(sfSigningPubKey);
521 Blob const signature = signer.getFieldVL(sfTxnSignature);
530 errorWhat = e.
what();
536 (errorWhat ?
": " + *errorWhat :
"") +
".");
543std::expected<void, std::string>
549 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::checkBatchMultiSign : batch transaction");
553 auto const batchSignerAccount = batchSigner.
getAccountID(sfAccount);
568std::expected<void, std::string>
575 auto const txnAccountID =
600 XRPL_ASSERT(!
isFree(),
"STTx::buildBatchTxns : template applied");
610 UNREACHABLE(
"STTx::buildBatchTxns : missing RawTransactions");
623 txns.reserve(raw.size());
626 if (rb.getFieldU16(sfTransactionType) == ttBATCH)
639 for (
auto const& stx : txns)
647 XRPL_ASSERT(
getTxnType() == ttBATCH,
"STTx::getBatchTransactions : batch transaction");
648 XRPL_ASSERT(
batchTxns_.has_value(),
"STTx::getBatchTransactions : batch transactions built");
651 "STTx::getBatchTransactions : batch transactions size mismatch");
673 return at(sfSponsor);
696 reason =
"The memo exceeds the maximum allowed size.";
700 for (
auto const& memo : memos)
702 auto memoObj =
dynamic_cast<STObject const*
>(&memo);
704 if ((memoObj ==
nullptr) || (memoObj->getFName() != sfMemo))
706 reason =
"A memo array may contain only Memo objects.";
710 for (
auto const& memoElement : *memoObj)
712 auto const& name = memoElement.getFName();
714 if (name != sfMemoType && name != sfMemoData && name != sfMemoFormat)
717 "A memo may contain only MemoType, MemoData or "
718 "MemoFormat fields.";
723 auto optData =
strUnHex(memoElement.getText());
728 "The MemoType, MemoData and MemoFormat fields may "
729 "only contain hex-encoded data.";
733 if (name == sfMemoData)
744 "-._~:/?#[]@!$&'()*+,;=%"
745 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
746 "abcdefghijklmnopqrstuvwxyz");
748 for (
unsigned char const c : symbols)
753 for (
unsigned char const c : *optData)
755 if (kAllowedSymbols[c] == 0)
758 "The MemoType and MemoFormat fields may only "
759 "contain characters that are allowed in URLs "
774 for (
int i = 0; i < st.
getCount(); ++i)
777 if ((t !=
nullptr) && t->isDefault())
787 auto const txType = tx[~sfTransactionType];
792 for (
auto const& e : item->getSOTemplate())
796 if (
auto const& field = tx.
peekAtField(e.sField());
797 (field.getSType() == STI_AMOUNT &&
799 (field.getSType() == STI_ISSUE &&
816 "xrpl::isBatchRawTransactionOkay : raw transactions only on batch");
824 reason =
"Batch transactions must contain raw transactions.";
832 reason =
"BatchSigners array exceeds max entries.";
854 reason =
"An account field is invalid.";
860 reason =
"Cannot submit pseudo transactions.";
866 reason =
"Amount can not be MPT.";
888 auto const t = tx[~sfTransactionType];
895 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)
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
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 &)
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::optional< std::vector< std::shared_ptr< STTx const > > > batchTxns_
std::expected< void, std::string > checkBatchSign(Rules const &rules) const
static std::string const & getMetaSQLInsertReplaceHeader()
std::vector< uint256 > getBatchTransactionIDs() const
The IDs of the inner transactions of a Batch.
std::expected< void, std::string > checkBatchMultiSign(STObject const &batchSigner, Rules const &rules, std::vector< uint256 > const &txIds) const
static constexpr std::size_t kMinMultiSigners
SeqProxy getSeqProxy() const
std::expected< void, std::string > checkSign(Rules const &rules) const
Check the signature.
std::expected< void, std::string > checkBatchSingleSign(STObject const &batchSigner, std::vector< uint256 > const &txIds) const
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
TxType getTxnType() const
AccountID getFeePayerID() const
uint256 getSigningHash() const
json::Value getJson(JsonOptions options) const override
AccountID getInitiator() const
The account responsible for the authorization: the delegate when sfDelegate is present,...
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={})
std::vector< std::shared_ptr< STTx const > > const & getBatchTransactions() const
The inner transactions of a Batch, built and validated at construction.
A type that represents either a sequence value or a ticket value.
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
int getBytesLeft() const noexcept
Blob const & peekData() const
int addBitString(BaseUInt< Bits, Tag > const &v)
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.
constexpr FlagValue spfSponsorFee
Serializer startMultiSigningData(STObject const &obj)
Break the multi-signing hash computation into 2 parts for optimization.
constexpr std::size_t kMaxBatchSigners
The maximum number of batch signers.
Dest safeDowncast(Src *s) noexcept
TxType
Transaction type identifiers.
bool isXRP(AccountID const &c)
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.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
static bool isBatchRawTransactionOkay(STTx const &tx, std::string &reason)
static bool isMemoOkay(STObject const &st, std::string &reason)
constexpr Dest safeCast(Src s) noexcept
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)
bool passesLocalChecks(STTx const &tx, std::string &)
Slice makeSlice(std::array< T, N > const &a)
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)
static bool invalidMPTAmountInTx(STObject const &tx)
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)
std::vector< unsigned char > Blob
Storage for linear binary data.
std::shared_ptr< STTx const > sterilize(STTx const &stx)
Sterilize a transaction.
void serializeBatch(Serializer &msg, AccountID const &outerAccount, std::uint32_t outerSeqValue, std::uint32_t const &flags, std::vector< uint256 > const &txids)
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)
constexpr std::size_t kTxMaxSizeBytes
Largest legal byte size of a transaction.
Note, should be treated as flags that can be | and &.