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/Transaction.h>
25#include <xrpld/app/misc/TxQ.h>
26#include <xrpld/app/paths/Pathfinder.h>
27#include <xrpld/app/tx/apply.h>
28#include <xrpld/app/tx/applySteps.h>
29#include <xrpld/rpc/detail/LegacyPathFind.h>
30#include <xrpld/rpc/detail/RPCHelpers.h>
31#include <xrpld/rpc/detail/TransactionSign.h>
32#include <xrpl/basics/Log.h>
33#include <xrpl/basics/mulDiv.h>
34#include <xrpl/json/json_writer.h>
35#include <xrpl/protocol/ErrorCodes.h>
36#include <xrpl/protocol/Feature.h>
37#include <xrpl/protocol/RPCErr.h>
38#include <xrpl/protocol/STAccount.h>
39#include <xrpl/protocol/STParsedJSON.h>
40#include <xrpl/protocol/Sign.h>
41#include <xrpl/protocol/TxFlags.h>
101 LogicError(
"Accessing unknown SigningForParams::getSigner()");
109 LogicError(
"Accessing unknown SigningForParams::getPublicKey()");
141 bool const isMasterKey = publicKeyAcctID == accountID;
153 auto const& sle = *accountState;
162 if ((sle.isFieldPresent(sfRegularKey)) &&
163 (publicKeyAcctID == sle.getAccountID(sfRegularKey)))
180 if (tx_json[jss::TransactionType].asString() != jss::Payment)
184 if (tx_json.
isMember(jss::DeliverMax))
188 if (tx_json[jss::DeliverMax] != tx_json[jss::Amount])
191 "Cannot specify differing 'Amount' and 'DeliverMax'");
194 tx_json[jss::Amount] = tx_json[jss::DeliverMax];
207 if (!tx_json.
isMember(jss::Destination))
210 auto const dstAccountID =
211 parseBase58<AccountID>(tx_json[jss::Destination].asString());
215 if (params.
isMember(jss::build_path) &&
219 "Field 'build_path' not allowed in this context.");
224 "Cannot specify both 'tx_json.Paths' and 'build_path'");
256 std::make_shared<RippleLineCache>(
257 ledger, app.
journal(
"RippleLineCache")),
276 auto j = app.
journal(
"RPCHandler");
277 JLOG(j.debug()) <<
"transactionSign: build_path: "
315 if (!tx_json.
isMember(jss::TransactionType))
321 if (!tx_json.
isMember(jss::Account))
328 auto const srcAddressID =
329 parseBase58<AccountID>(tx_json[jss::Account].asString());
358 ret.
second = *srcAddressID;
391static transactionPreProcessResult
399 auto j = app.
journal(
"RPCHandler");
411 !(params.
isMember(jss::offline) && params[jss::offline].
asBool());
429 return std::move(txJsonResult);
444 JLOG(j.debug()) <<
"transactionSign: Failed to find source account "
445 <<
"in current ledger: " <<
toBase58(srcAddressID);
452 if (!tx_json.
isMember(jss::Sequence))
454 bool const hasTicketSeq =
455 tx_json.
isMember(sfTicketSequence.jsonName);
456 if (!hasTicketSeq && !sle)
459 <<
"transactionSign: Failed to find source account "
460 <<
"in current ledger: " <<
toBase58(srcAddressID);
464 tx_json[jss::Sequence] =
471 if (!tx_json.
isMember(jss::NetworkID))
474 if (networkId > 1024)
475 tx_json[jss::NetworkID] =
to_string(networkId);
509 if (tx_json.
isMember(jss::TxnSignature))
543 if (!parsed.
object.has_value())
546 err[jss::error] = parsed.
error[jss::error];
547 err[jss::error_code] = parsed.
error[jss::error_code];
548 err[jss::error_message] = parsed.
error[jss::error_message];
557 parsed.
object->setFieldVL(
561 stTx = std::make_shared<STTx>(std::move(parsed.
object.value()));
571 "Exception occurred constructing serialized transaction");
607 tpTrans = std::make_shared<Transaction>(stTx, reason, app);
608 if (tpTrans->getStatus() !=
NEW)
611 rpcINTERNAL,
"Unable to construct transaction: " + reason);
623 tpTrans->getSTransaction()->add(s);
628 auto sttxNew = std::make_shared<STTx const>(sit);
632 sttxNew->getTransactionID(),
644 std::make_shared<Transaction>(sttxNew, reason, app);
648 if (!tpTransNew->getSTransaction()->isEquivalent(
649 *tpTrans->getSTransaction()))
653 tpTrans = std::move(tpTransNew);
669 ret.
second = std::move(tpTrans);
681 jvResult[jss::tx_json] =
683 jvResult[jss::hash] =
to_string(tpTrans->getID());
689 jvResult[jss::tx_json],
690 tpTrans->getSTransaction()->getTxnType(),
693 jvResult[jss::tx_blob] =
694 strHex(tpTrans->getSTransaction()->getSerializer().peekData());
703 jvResult[jss::engine_result] = sToken;
704 jvResult[jss::engine_result_code] = tpTrans->getResult();
705 jvResult[jss::engine_result_message] = sHuman;
711 rpcINTERNAL,
"Exception occurred during JSON handling.");
733 tx[jss::Sequence] =
"0";
736 if (!tx.
isMember(jss::SigningPubKey))
738 tx[jss::SigningPubKey] =
"";
741 if (!tx.
isMember(jss::TxnSignature))
743 tx[jss::TxnSignature] =
"";
748 if (!tx[jss::Signers].isArray())
752 for (
auto& signer : tx[jss::Signers])
754 if (!signer.isMember(jss::Signer) ||
755 !signer[jss::Signer].isObject())
757 if (!signer[jss::Signer].isMember(jss::SigningPubKey))
760 signer[jss::Signer][jss::SigningPubKey] =
"";
762 if (!signer[jss::Signer].isMember(jss::TxnSignature))
765 signer[jss::Signer][jss::TxnSignature] =
"";
771 if (!parsed.
object.has_value())
807 auto const baseFee = ledger->fees().base;
813 auto const limit =
mulDiv(feeDefault, mult, div);
815 Throw<std::overflow_error>(
"mulDiv");
820 ss <<
"Fee of " << fee <<
" exceeds the requested tx limit of "
847 if (request.
isMember(jss::fee_mult_max))
849 if (request[jss::fee_mult_max].isInt())
851 mult = request[jss::fee_mult_max].
asInt();
856 jss::fee_mult_max,
"a positive integer"));
863 jss::fee_mult_max,
"a positive integer"));
866 if (request.
isMember(jss::fee_div_max))
868 if (request[jss::fee_div_max].isInt())
870 div = request[jss::fee_div_max].
asInt();
875 jss::fee_div_max,
"a positive integer"));
882 jss::fee_div_max,
"a positive integer"));
888 if (feeOrError.isMember(jss::error))
890 tx[jss::Fee] = std::move(feeOrError);
906 using namespace detail;
908 auto j = app.
journal(
"RPCHandler");
909 JLOG(j.debug()) <<
"transactionSign: " << jvRequest;
912 SigningForParams signForParams;
913 transactionPreProcessResult preprocResult = transactionPreProcessImpl(
914 jvRequest, role, signForParams, validatedLedgerAge, app);
916 if (!preprocResult.second)
917 return preprocResult.first;
922 transactionConstructImpl(preprocResult.second, ledger->rules(), app);
941 using namespace detail;
944 auto j = app.
journal(
"RPCHandler");
945 JLOG(j.debug()) <<
"transactionSubmit: " << jvRequest;
948 SigningForParams signForParams;
949 transactionPreProcessResult preprocResult = transactionPreProcessImpl(
950 jvRequest, role, signForParams, validatedLedgerAge, app);
952 if (!preprocResult.second)
953 return preprocResult.first;
957 transactionConstructImpl(preprocResult.second, ledger->rules(), app);
971 rpcINTERNAL,
"Exception occurred during transaction submission.");
983 if (!jvRequest.
isMember(jss::tx_json))
986 Json::Value const& tx_json(jvRequest[jss::tx_json]);
994 if (!tx_json.
isMember(jss::Sequence))
997 if (!tx_json.
isMember(sfSigningPubKey.getJsonName()))
1000 if (!tx_json[sfSigningPubKey.getJsonName()].
asString().
empty())
1003 "When multi-signing 'tx_json.SigningPubKey' must be empty.");
1014 if (signers.empty())
1022 return (a[sfAccount] < b[sfAccount]);
1030 return (a[sfAccount] == b[sfAccount]);
1033 if (dupIter != signers.end())
1036 err <<
"Duplicate Signers:Signer:Account entries ("
1037 <<
toBase58((*dupIter)[sfAccount]) <<
") are not allowed.";
1042 if (signers.end() !=
1046 [&signingForID](
STObject const& elem) {
1047 return elem[sfAccount] == signingForID;
1051 err <<
"A Signer may not be the transaction's Account ("
1071 auto j = app.
journal(
"RPCHandler");
1072 JLOG(j.debug()) <<
"transactionSignFor: " << jvRequest;
1075 const char accountField[] =
"account";
1077 if (!jvRequest.
isMember(accountField))
1081 auto const signerAccountID =
1082 parseBase58<AccountID>(jvRequest[accountField].asString());
1083 if (!signerAccountID)
1089 if (!jvRequest.
isMember(jss::tx_json))
1100 if (!tx_json.
isMember(sfSigningPubKey.getJsonName()))
1101 tx_json[sfSigningPubKey.getJsonName()] =
"";
1106 using namespace detail;
1108 Json::Value err = checkMultiSignFields(jvRequest);
1114 SigningForParams signForParams(*signerAccountID);
1116 transactionPreProcessResult preprocResult = transactionPreProcessImpl(
1117 jvRequest, role, signForParams, validatedLedgerAge, app);
1119 if (!preprocResult.second)
1120 return preprocResult.first;
1123 signForParams.validMultiSign(),
1124 "ripple::RPC::transactionSignFor : valid multi-signature");
1130 auto const err = acctMatchesPubKey(
1131 account_state, *signerAccountID, signForParams.getPublicKey());
1138 auto& sttx = preprocResult.second;
1142 signer[sfAccount] = *signerAccountID;
1143 signer.
setFieldVL(sfTxnSignature, signForParams.getSignature());
1145 sfSigningPubKey, signForParams.getPublicKey().slice());
1148 if (!sttx->isFieldPresent(sfSigners))
1149 sttx->setFieldArray(sfSigners, {});
1151 auto& signers = sttx->peekFieldArray(sfSigners);
1152 signers.emplace_back(std::move(signer));
1155 auto err = sortAndValidateSigners(signers, (*sttx)[sfAccount]);
1162 transactionConstructImpl(sttx, ledger->rules(), app);
1182 auto j = app.
journal(
"RPCHandler");
1183 JLOG(j.debug()) <<
"transactionSubmitMultiSigned: " << jvRequest;
1187 using namespace detail;
1189 Json::Value err = checkMultiSignFields(jvRequest);
1196 auto [txJsonResult, srcAddressID] = checkTxJsonFields(
1206 return std::move(txJsonResult);
1215 <<
"transactionSubmitMultiSigned: Failed to find source account "
1216 <<
"in current ledger: " <<
toBase58(srcAddressID);
1234 err = checkPayment(jvRequest, tx_json, srcAddressID, role, app,
false);
1244 if (!parsedTx_json.
object)
1247 jvResult[
"error"] = parsedTx_json.
error[
"error"];
1248 jvResult[
"error_code"] = parsedTx_json.
error[
"error_code"];
1249 jvResult[
"error_message"] = parsedTx_json.
error[
"error_message"];
1255 std::make_shared<STTx>(std::move(parsedTx_json.
object.value()));
1266 "Exception while serializing transaction: " + reason);
1279 if (!stTx->getFieldVL(sfSigningPubKey).empty())
1282 err <<
"Invalid " << sfSigningPubKey.fieldName
1283 <<
" field. Field must be empty when multi-signing.";
1288 if (stTx->isFieldPresent(sfTxnSignature))
1292 auto const fee = stTx->getFieldAmount(sfFee);
1297 err <<
"Invalid " << sfFee.fieldName
1298 <<
" field. Fees must be specified in XRP.";
1304 err <<
"Invalid " << sfFee.fieldName
1305 <<
" field. Fees must be greater than zero.";
1311 if (!stTx->isFieldPresent(sfSigners))
1316 auto& signers = stTx->peekFieldArray(sfSigners);
1318 if (signers.empty())
1323 signers.begin(), signers.end(), [](
STObject const& obj) {
1327 obj.isFieldPresent(sfAccount) &&
1328 obj.isFieldPresent(sfSigningPubKey) &&
1329 obj.isFieldPresent(sfTxnSignature) && obj.getCount() == 3);
1330 }) != signers.end())
1333 "Signers array may only contain Signer entries.");
1337 auto err = sortAndValidateSigners(signers, srcAddressID);
1343 transactionConstructImpl(stTx, ledger->rules(), app);
1357 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 &stTx, 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 getCurrentNetworkFee(Role const role, Config const &config, LoadFeeTrack const &feeTrack, TxQ const &txQ, Application const &app, Json::Value const &tx, int mult, int div)
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.
static XRPAmount getTxFee(Application const &app, Config const &config, Json::Value tx)
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.
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
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