20#include <xrpld/app/ledger/LedgerMaster.h>
21#include <xrpld/app/ledger/OpenLedger.h>
22#include <xrpld/app/main/Application.h>
23#include <xrpld/app/misc/DeliverMax.h>
24#include <xrpld/app/misc/LoadFeeTrack.h>
25#include <xrpld/app/misc/Transaction.h>
26#include <xrpld/app/misc/TxQ.h>
27#include <xrpld/app/paths/Pathfinder.h>
28#include <xrpld/app/tx/apply.h>
29#include <xrpld/rpc/detail/LegacyPathFind.h>
30#include <xrpld/rpc/detail/RPCHelpers.h>
31#include <xrpld/rpc/detail/TransactionSign.h>
32#include <xrpld/rpc/detail/Tuning.h>
33#include <xrpl/basics/Log.h>
34#include <xrpl/basics/mulDiv.h>
35#include <xrpl/json/json_writer.h>
36#include <xrpl/protocol/ErrorCodes.h>
37#include <xrpl/protocol/Feature.h>
38#include <xrpl/protocol/RPCErr.h>
39#include <xrpl/protocol/STAccount.h>
40#include <xrpl/protocol/STParsedJSON.h>
41#include <xrpl/protocol/Sign.h>
42#include <xrpl/protocol/TxFlags.h>
102 LogicError(
"Accessing unknown SigningForParams::getSigner()");
110 LogicError(
"Accessing unknown SigningForParams::getPublicKey()");
142 bool const isMasterKey = publicKeyAcctID == accountID;
154 auto const& sle = *accountState;
163 if ((sle.isFieldPresent(sfRegularKey)) &&
164 (publicKeyAcctID == sle.getAccountID(sfRegularKey)))
181 if (tx_json[jss::TransactionType].asString() != jss::Payment)
185 if (tx_json.
isMember(jss::DeliverMax))
189 if (tx_json[jss::DeliverMax] != tx_json[jss::Amount])
192 "Cannot specify differing 'Amount' and 'DeliverMax'");
195 tx_json[jss::Amount] = tx_json[jss::DeliverMax];
208 if (!tx_json.
isMember(jss::Destination))
211 auto const dstAccountID =
212 parseBase58<AccountID>(tx_json[jss::Destination].asString());
216 if (params.
isMember(jss::build_path) &&
220 "Field 'build_path' not allowed in this context.");
225 "Cannot specify both 'tx_json.Paths' and 'build_path'");
257 std::make_shared<RippleLineCache>(
258 ledger, app.
journal(
"RippleLineCache")),
277 auto j = app.
journal(
"RPCHandler");
278 JLOG(j.debug()) <<
"transactionSign: build_path: "
316 if (!tx_json.
isMember(jss::TransactionType))
322 if (!tx_json.
isMember(jss::Account))
329 auto const srcAddressID =
330 parseBase58<AccountID>(tx_json[jss::Account].asString());
359 ret.
second = *srcAddressID;
392static transactionPreProcessResult
400 auto j = app.
journal(
"RPCHandler");
412 !(params.
isMember(jss::offline) && params[jss::offline].
asBool());
430 return std::move(txJsonResult);
445 JLOG(j.debug()) <<
"transactionSign: Failed to find source account "
446 <<
"in current ledger: " <<
toBase58(srcAddressID);
478 if (!tx_json.
isMember(jss::Sequence))
480 bool const hasTicketSeq =
481 tx_json.
isMember(sfTicketSequence.jsonName);
482 if (!hasTicketSeq && !sle)
485 <<
"transactionSign: Failed to find source account "
486 <<
"in current ledger: " <<
toBase58(srcAddressID);
490 tx_json[jss::Sequence] =
501 if (tx_json.
isMember(sfTxnSignature.jsonName))
509 if (tx_json.
isMember(sfSigners.jsonName))
535 if (!parsed.
object.has_value())
538 err[jss::error] = parsed.
error[jss::error];
539 err[jss::error_code] = parsed.
error[jss::error_code];
540 err[jss::error_message] = parsed.
error[jss::error_message];
549 parsed.
object->setFieldVL(
553 stpTrans = std::make_shared<STTx>(std::move(parsed.
object.value()));
563 "Exception occurred constructing serialized transaction");
582 stpTrans->sign(pk, sk);
600 tpTrans = std::make_shared<Transaction>(stpTrans, reason, app);
601 if (tpTrans->getStatus() !=
NEW)
604 rpcINTERNAL,
"Unable to construct transaction: " + reason);
616 tpTrans->getSTransaction()->add(s);
621 auto sttxNew = std::make_shared<STTx const>(sit);
625 sttxNew->getTransactionID(),
637 std::make_shared<Transaction>(sttxNew, reason, app);
641 if (!tpTransNew->getSTransaction()->isEquivalent(
642 *tpTrans->getSTransaction()))
646 tpTrans = std::move(tpTransNew);
662 ret.
second = std::move(tpTrans);
674 jvResult[jss::tx_json] =
676 jvResult[jss::hash] =
to_string(tpTrans->getID());
682 jvResult[jss::tx_json],
683 tpTrans->getSTransaction()->getTxnType(),
686 jvResult[jss::tx_blob] =
687 strHex(tpTrans->getSTransaction()->getSerializer().peekData());
696 jvResult[jss::engine_result] = sToken;
697 jvResult[jss::engine_result_code] = tpTrans->getResult();
698 jvResult[jss::engine_result_message] = sHuman;
704 rpcINTERNAL,
"Exception occurred during JSON handling.");
732 if (request.
isMember(jss::fee_mult_max))
734 if (request[jss::fee_mult_max].isInt())
736 mult = request[jss::fee_mult_max].
asInt();
741 jss::fee_mult_max,
"a positive integer"));
748 jss::fee_mult_max,
"a positive integer"));
751 if (request.
isMember(jss::fee_div_max))
753 if (request[jss::fee_div_max].isInt())
755 div = request[jss::fee_div_max].
asInt();
760 jss::fee_div_max,
"a positive integer"));
767 jss::fee_div_max,
"a positive integer"));
780 auto const baseFee = ledger->fees().base;
786 auto const limit = [&]() {
788 auto const result =
mulDiv(feeDefault, mult, div);
790 Throw<std::overflow_error>(
"mulDiv");
797 ss <<
"Fee of " << fee <<
" exceeds the requested tx limit of "
818 using namespace detail;
820 auto j = app.
journal(
"RPCHandler");
821 JLOG(j.debug()) <<
"transactionSign: " << jvRequest;
824 SigningForParams signForParams;
825 transactionPreProcessResult preprocResult = transactionPreProcessImpl(
826 jvRequest, role, signForParams, validatedLedgerAge, app);
828 if (!preprocResult.second)
829 return preprocResult.first;
834 transactionConstructImpl(preprocResult.second, ledger->rules(), app);
853 using namespace detail;
856 auto j = app.
journal(
"RPCHandler");
857 JLOG(j.debug()) <<
"transactionSubmit: " << jvRequest;
860 SigningForParams signForParams;
861 transactionPreProcessResult preprocResult = transactionPreProcessImpl(
862 jvRequest, role, signForParams, validatedLedgerAge, app);
864 if (!preprocResult.second)
865 return preprocResult.first;
869 transactionConstructImpl(preprocResult.second, ledger->rules(), app);
883 rpcINTERNAL,
"Exception occurred during transaction submission.");
895 if (!jvRequest.
isMember(jss::tx_json))
898 Json::Value const& tx_json(jvRequest[jss::tx_json]);
906 if (!tx_json.
isMember(jss::Sequence))
909 if (!tx_json.
isMember(sfSigningPubKey.getJsonName()))
912 if (!tx_json[sfSigningPubKey.getJsonName()].
asString().
empty())
915 "When multi-signing 'tx_json.SigningPubKey' must be empty.");
934 return (a[sfAccount] < b[sfAccount]);
942 return (a[sfAccount] == b[sfAccount]);
945 if (dupIter != signers.end())
948 err <<
"Duplicate Signers:Signer:Account entries ("
949 <<
toBase58((*dupIter)[sfAccount]) <<
") are not allowed.";
958 [&signingForID](
STObject const& elem) {
959 return elem[sfAccount] == signingForID;
963 err <<
"A Signer may not be the transaction's Account ("
983 auto j = app.
journal(
"RPCHandler");
984 JLOG(j.debug()) <<
"transactionSignFor: " << jvRequest;
987 const char accountField[] =
"account";
989 if (!jvRequest.
isMember(accountField))
993 auto const signerAccountID =
994 parseBase58<AccountID>(jvRequest[accountField].asString());
995 if (!signerAccountID)
1001 if (!jvRequest.
isMember(jss::tx_json))
1012 if (!tx_json.
isMember(sfSigningPubKey.getJsonName()))
1013 tx_json[sfSigningPubKey.getJsonName()] =
"";
1018 using namespace detail;
1020 Json::Value err = checkMultiSignFields(jvRequest);
1026 SigningForParams signForParams(*signerAccountID);
1028 transactionPreProcessResult preprocResult = transactionPreProcessImpl(
1029 jvRequest, role, signForParams, validatedLedgerAge, app);
1031 if (!preprocResult.second)
1032 return preprocResult.first;
1035 signForParams.validMultiSign(),
1036 "ripple::RPC::transactionSignFor : valid multi-signature");
1042 auto const err = acctMatchesPubKey(
1043 account_state, *signerAccountID, signForParams.getPublicKey());
1050 auto& sttx = preprocResult.second;
1054 signer[sfAccount] = *signerAccountID;
1055 signer.
setFieldVL(sfTxnSignature, signForParams.getSignature());
1057 sfSigningPubKey, signForParams.getPublicKey().slice());
1060 if (!sttx->isFieldPresent(sfSigners))
1061 sttx->setFieldArray(sfSigners, {});
1063 auto& signers = sttx->peekFieldArray(sfSigners);
1064 signers.emplace_back(std::move(signer));
1067 auto err = sortAndValidateSigners(signers, (*sttx)[sfAccount]);
1074 transactionConstructImpl(sttx, ledger->rules(), app);
1094 auto j = app.
journal(
"RPCHandler");
1095 JLOG(j.debug()) <<
"transactionSubmitMultiSigned: " << jvRequest;
1099 using namespace detail;
1101 Json::Value err = checkMultiSignFields(jvRequest);
1108 auto [txJsonResult, srcAddressID] = checkTxJsonFields(
1118 return std::move(txJsonResult);
1127 <<
"transactionSubmitMultiSigned: Failed to find source account "
1128 <<
"in current ledger: " <<
toBase58(srcAddressID);
1146 err = checkPayment(jvRequest, tx_json, srcAddressID, role, app,
false);
1156 if (!parsedTx_json.
object)
1159 jvResult[
"error"] = parsedTx_json.
error[
"error"];
1160 jvResult[
"error_code"] = parsedTx_json.
error[
"error_code"];
1161 jvResult[
"error_message"] = parsedTx_json.
error[
"error_message"];
1167 std::make_shared<STTx>(std::move(parsedTx_json.
object.value()));
1178 "Exception while serializing transaction: " + reason);
1191 if (!stpTrans->getFieldVL(sfSigningPubKey).empty())
1194 err <<
"Invalid " << sfSigningPubKey.fieldName
1195 <<
" field. Field must be empty when multi-signing.";
1200 if (stpTrans->isFieldPresent(sfTxnSignature))
1204 auto const fee = stpTrans->getFieldAmount(sfFee);
1209 err <<
"Invalid " << sfFee.fieldName
1210 <<
" field. Fees must be specified in XRP.";
1216 err <<
"Invalid " << sfFee.fieldName
1217 <<
" field. Fees must be greater than zero.";
1223 if (!stpTrans->isFieldPresent(sfSigners))
1228 auto& signers = stpTrans->peekFieldArray(sfSigners);
1230 if (signers.empty())
1235 signers.begin(), signers.end(), [](
STObject const& obj) {
1239 obj.isFieldPresent(sfAccount) &&
1240 obj.isFieldPresent(sfSigningPubKey) &&
1241 obj.isFieldPresent(sfTxnSignature) && obj.getCount() == 3);
1242 }) != signers.end())
1245 "Signers array may only contain Signer entries.");
1249 auto err = sortAndValidateSigners(signers, srcAddressID);
1255 transactionConstructImpl(stpTrans, ledger->rules(), app);
1269 rpcINTERNAL,
"Exception occurred during transaction submission.");
T adjacent_find(T... args)
Value removeMember(const char *key)
Remove and return the named member.
std::string asString() const
Returns the unquoted string value.
bool isMember(const char *key) const
Return true if the object has a member named key.
virtual Config & config()=0
virtual LoadFeeTrack & getFeeTrack()=0
virtual OpenLedger & openLedger()=0
virtual beast::Journal journal(std::string const &name)=0
virtual bool checkSigs() const =0
virtual HashRouter & getHashRouter()=0
Like std::vector<char> but better.
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Manages the current fee schedule.
bool isLoadedCluster() const
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Calculates payment paths.
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
Slice slice() const noexcept
AccountID const *const multiSigningAcctID_
std::optional< PublicKey > multiSignPublicKey_
SigningForParams(SigningForParams const &rhs)=delete
bool validMultiSign() const
SigningForParams(AccountID const &multiSigningAcctID)
AccountID const & getSigner() const
bool isMultiSigning() const
void moveMultiSignature(Buffer &&multiSignature)
PublicKey const & getPublicKey() const
Buffer const & getSignature() const
bool isSingleSigning() const
void setPublicKey(PublicKey const &multiSignPublicKey)
Rules controlling protocol behavior.
constexpr bool holds() const noexcept
void setIssuer(AccountID const &uIssuer)
Issue const & issue() const
bool native() const noexcept
static STObject makeInnerObject(SField const &name)
void setFieldVL(SField const &field, Blob const &)
Holds the serialized result of parsing an input JSON object.
std::optional< STObject > object
The STObject if the parse was successful.
Json::Value error
On failure, an appropriate set of error values.
Json::Value getJson(JsonOptions) const override
constexpr std::uint32_t value() const
Slice slice() const noexcept
An immutable linear range of bytes.
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
SeqProxy nextQueuableSeq(std::shared_ptr< SLE const > const &sleAccount) const
Return the next sequence that would go in the TxQ for an account.
Json::Value jsonClipped() const
auto constexpr maxValidatedLedgerAge
static int constexpr defaultAutoFillFeeMultiplier
static int constexpr defaultAutoFillFeeDivisor
static Json::Value checkPayment(Json::Value const ¶ms, Json::Value &tx_json, AccountID const &srcAddressID, Role const role, Application &app, bool doPath)
static error_code_i acctMatchesPubKey(std::shared_ptr< SLE const > accountState, AccountID const &accountID, PublicKey const &publicKey)
static std::pair< Json::Value, AccountID > checkTxJsonFields(Json::Value const &tx_json, Role const role, bool const verify, std::chrono::seconds validatedLedgerAge, Config const &config, LoadFeeTrack const &feeTrack, unsigned apiVersion)
static transactionPreProcessResult transactionPreProcessImpl(Json::Value ¶ms, Role role, SigningForParams &signingArgs, std::chrono::seconds validatedLedgerAge, Application &app)
static std::pair< Json::Value, Transaction::pointer > transactionConstructImpl(std::shared_ptr< STTx const > const &stpTrans, Rules const &rules, Application &app)
static Json::Value checkMultiSignFields(Json::Value const &jvRequest)
static Json::Value sortAndValidateSigners(STArray &signers, AccountID const &signingForID)
static Json::Value transactionFormatResultImpl(Transaction::pointer tpTrans, unsigned apiVersion)
Json::Value transactionSign(Json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app)
Returns a Json::objectValue.
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Json::Value transactionSubmitMultiSigned(Json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app, ProcessTransactionFn const &processTransaction)
Returns a Json::objectValue.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Json::Value invalid_field_error(std::string const &name)
static constexpr std::integral_constant< unsigned, Version > apiVersion
Json::Value make_param_error(std::string const &message)
Returns a new json object that indicates invalid parameters.
std::string missing_field_message(std::string const &name)
std::string invalid_field_message(std::string const &name)
std::string expected_field_message(std::string const &name, std::string const &type)
Json::Value transactionSubmit(Json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app, ProcessTransactionFn const &processTransaction)
Returns a Json::objectValue.
Json::Value object_field_error(std::string const &name)
void insertDeliverMax(Json::Value &tx_json, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Json::Value transactionSignFor(Json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app)
Returns a Json::objectValue.
Json::Value checkFee(Json::Value &request, Role const role, bool doAutoFill, Config const &config, LoadFeeTrack const &feeTrack, TxQ const &txQ, Application const &app)
Fill in the fee on behalf of the client.
Json::Value missing_field_error(std::string const &name)
std::optional< std::pair< PublicKey, SecretKey > > keypairForSignature(Json::Value const ¶ms, Json::Value &error, unsigned int apiVersion)
Keylet account(AccountID const &id) noexcept
AccountID root.
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.
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
Serializer buildMultiSigningData(STObject const &obj, AccountID const &signingID)
Return a Serializer suitable for computing a multisigning TxnSignature.
bool isLegalNet(STAmount const &value)
AccountID calcAccountID(PublicKey const &pk)
Json::Value rpcError(int iError)
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
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)
bool amountFromJsonNoThrow(STAmount &result, Json::Value const &jvSource)
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
bool passesLocalChecks(STObject const &st, std::string &)
@ Valid
Signature and local checks are good / passed.
@ SigGoodOnly
Signature is good, but local checks fail.
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr std::uint32_t tfFullyCanonicalSig
Transaction flags.
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules, Config const &config)
Checks transaction signature and local checks.
bool transResultInfo(TER code, std::string &token, std::string &text)
Role
Indicates the level of administrative permission to grant.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
XRPAmount reference_fee
The cost of a reference transaction in drops.
transactionPreProcessResult(std::shared_ptr< STTx > &&st)
transactionPreProcessResult & operator=(transactionPreProcessResult const &)=delete
transactionPreProcessResult(transactionPreProcessResult const &)=delete
transactionPreProcessResult(Json::Value &&json)
transactionPreProcessResult()=delete
std::shared_ptr< STTx > const second
transactionPreProcessResult(transactionPreProcessResult &&rhs)=default
transactionPreProcessResult & operator=(transactionPreProcessResult &&)=delete