1#include <xrpld/rpc/detail/TransactionSign.h>
3#include <xrpld/app/ledger/OpenLedger.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/app/misc/DeliverMax.h>
6#include <xrpld/app/misc/Transaction.h>
7#include <xrpld/app/misc/TxQ.h>
8#include <xrpld/rpc/Role.h>
9#include <xrpld/rpc/detail/AssetCache.h>
10#include <xrpld/rpc/detail/LegacyPathFind.h>
11#include <xrpld/rpc/detail/Pathfinder.h>
12#include <xrpld/rpc/detail/RPCHelpers.h>
13#include <xrpld/rpc/detail/Tuning.h>
15#include <xrpl/basics/Blob.h>
16#include <xrpl/basics/Buffer.h>
17#include <xrpl/basics/Log.h>
18#include <xrpl/basics/Number.h>
19#include <xrpl/basics/Slice.h>
20#include <xrpl/basics/base_uint.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/basics/strHex.h>
23#include <xrpl/beast/utility/instrumentation.h>
24#include <xrpl/core/NetworkIDService.h>
25#include <xrpl/json/json_writer.h>
26#include <xrpl/protocol/AccountID.h>
27#include <xrpl/protocol/ApiVersion.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/Feature.h>
30#include <xrpl/protocol/Indexes.h>
31#include <xrpl/protocol/InnerObjectFormats.h>
32#include <xrpl/protocol/Issue.h>
33#include <xrpl/protocol/LedgerFormats.h>
34#include <xrpl/protocol/MPTIssue.h>
35#include <xrpl/protocol/PublicKey.h>
36#include <xrpl/protocol/RPCErr.h>
37#include <xrpl/protocol/SField.h>
38#include <xrpl/protocol/STAmount.h>
39#include <xrpl/protocol/STArray.h>
40#include <xrpl/protocol/STObject.h>
41#include <xrpl/protocol/STParsedJSON.h>
42#include <xrpl/protocol/STPathSet.h>
43#include <xrpl/protocol/STTx.h>
44#include <xrpl/protocol/SecretKey.h>
45#include <xrpl/protocol/Serializer.h>
46#include <xrpl/protocol/Sign.h>
47#include <xrpl/protocol/TER.h>
48#include <xrpl/protocol/Units.h>
49#include <xrpl/protocol/XRPAmount.h>
50#include <xrpl/protocol/jss.h>
51#include <xrpl/server/LoadFeeTrack.h>
52#include <xrpl/tx/apply.h>
53#include <xrpl/tx/applySteps.h>
121 logicError(
"Accessing unknown SigningForParams::getSigner()");
129 logicError(
"Accessing unknown SigningForParams::getPublicKey()");
133 [[nodiscard]]
Buffer const&
173 bool const isMasterKey = publicKeyAcctID == accountID;
185 auto const& sle = *accountState;
188 if (sle.isFlag(lsfDisableMaster))
194 if ((sle.isFieldPresent(sfRegularKey)) && (publicKeyAcctID == sle.getAccountID(sfRegularKey)))
211 if (txJson[jss::TransactionType].asString() != jss::Payment)
215 if (txJson.
isMember(jss::DeliverMax))
219 if (txJson[jss::DeliverMax] != txJson[jss::Amount])
227 txJson[jss::Amount] = txJson[jss::DeliverMax];
241 if (!txJson.
isMember(jss::Destination))
248 if (params.
isMember(jss::build_path) &&
263 if (txJson.
isMember(sfDomainID.jsonName))
266 if (!txJson[sfDomainID.jsonName].
isString() ||
289 [&](
Issue const&) { sendMax.
get<
Issue>().account = srcAddressID; },
326 JLOG(j.debug()) <<
"transactionSign: build_path: "
364 if (!txJson.
isMember(jss::TransactionType))
406 ret.
second = *srcAddressID;
410static std::expected<void, json::Value>
413 if (appNetworkId > 1024)
415 if (!txJson.
isMember(jss::NetworkID))
420 if (!txJson[jss::NetworkID].isIntegral() || txJson[jss::NetworkID].asUInt() != appNetworkId)
426 return std::expected<void, json::Value>();
457static TransactionPreProcessResult
475 bool const verify = !(params.
isMember(jss::offline) && params[jss::offline].asBool());
477 auto const signatureTarget =
479 if (params.
isMember(jss::signature_target))
486 auto const signatureTemplate = signatureTarget
491 if (signatureTemplate ==
nullptr)
514 return std::move(txJsonResult);
529 JLOG(j.debug()) <<
"transactionSign: Failed to find source account "
530 <<
"in current ledger: " <<
toBase58(srcAddressID);
537 if (!txJson.
isMember(jss::Sequence))
539 bool const hasTicketSeq = txJson.
isMember(sfTicketSequence.jsonName);
540 if (!hasTicketSeq && !sle)
542 JLOG(j.debug()) <<
"transactionSign: Failed to find source account "
543 <<
"in current ledger: " <<
toBase58(srcAddressID);
550 if (!txJson.
isMember(jss::NetworkID))
553 if (networkId > 1024)
554 txJson[jss::NetworkID] =
to_string(networkId);
574 params, txJson, srcAddressID, role, app,
verify && signingArgs.
editFields());
583 if (txJson.
isMember(jss::TxnSignature))
607 if (txJson.
isMember(sfDelegate.jsonName))
610 auto const delegateJson = txJson[sfDelegate.jsonName];
611 auto const ptrDelegatedAddressID = delegateJson.
isString()
615 if (!ptrDelegatedAddressID)
621 auto delegatedAddressID = *ptrDelegatedAddressID;
643 if (!parsed.
object.has_value())
646 err[jss::error] = parsed.
error[jss::error];
647 err[jss::error_code] = parsed.
error[jss::error_code];
648 err[jss::error_message] = parsed.
error[jss::error_message];
661 if (!parsed.
object->isFieldPresent(*signatureTarget))
663 parsed.
object->setFieldObject(
664 *signatureTarget,
STObject{*signatureTemplate, *signatureTarget});
666 sigObject = &parsed.
object->peekFieldObject(*signatureTarget);
680 RpcInternal,
"Exception occurred constructing serialized transaction");
698 stTx->sign(pk, sk, signatureTarget);
731 tpTrans->getSTransaction()->add(s);
753 if (!tpTransNew->getSTransaction()->isEquivalent(*tpTrans->getSTransaction()))
757 tpTrans = std::move(tpTransNew);
772 ret.
second = std::move(tpTrans);
785 jvResult[jss::hash] =
to_string(tpTrans->getID());
793 jvResult[jss::tx_json], tpTrans->getSTransaction()->getTxnType(), apiVersion);
795 jvResult[jss::tx_blob] =
strHex(tpTrans->getSTransaction()->getSerializer().peekData());
804 jvResult[jss::engine_result] = sToken;
805 jvResult[jss::engine_result_code] = tpTrans->getResult();
806 jvResult[jss::engine_result_message] = sHuman;
833 tx[jss::Sequence] =
"0";
836 if (!tx.
isMember(jss::SigningPubKey))
838 tx[jss::SigningPubKey] =
"";
841 if (!tx.
isMember(jss::TxnSignature))
843 tx[jss::TxnSignature] =
"";
848 if (!tx[jss::Signers].isArray())
855 for (
auto& signer : tx[jss::Signers])
857 if (!signer.isMember(jss::Signer) || !signer[jss::Signer].isObject())
859 if (!signer[jss::Signer].isMember(jss::SigningPubKey))
862 signer[jss::Signer][jss::SigningPubKey] =
"";
864 if (!signer[jss::Signer].isMember(jss::TxnSignature))
867 signer[jss::Signer][jss::TxnSignature] =
"";
873 if (!parsed.
object.has_value())
912 auto const baseFee = ledger->fees().base;
917 auto const limit =
mulDiv(feeDefault, mult, div);
924 ss <<
"Fee of " << fee <<
" exceeds the requested tx limit of " << *limit;
950 if (request.
isMember(jss::fee_mult_max))
952 if (request[jss::fee_mult_max].isInt())
954 mult = request[jss::fee_mult_max].
asInt();
968 if (request.
isMember(jss::fee_div_max))
970 if (request[jss::fee_div_max].isInt())
972 div = request[jss::fee_div_max].
asInt();
988 if (feeOrError.isMember(jss::error))
990 tx[jss::Fee] = std::move(feeOrError);
1000 unsigned apiVersion,
1009 JLOG(j.debug()) <<
"transactionSign: " << jvRequest;
1012 SigningForParams signForParams;
1013 TransactionPreProcessResult
const preprocResult =
1014 transactionPreProcessImpl(jvRequest, role, signForParams, validatedLedgerAge, app);
1016 if (!preprocResult.second)
1017 return preprocResult.first;
1022 transactionConstructImpl(preprocResult.second, ledger->rules(), app);
1027 return transactionFormatResultImpl(txn.
second, apiVersion);
1034 unsigned apiVersion,
1045 JLOG(j.debug()) <<
"transactionSubmit: " << jvRequest;
1048 SigningForParams signForParams;
1049 TransactionPreProcessResult
const preprocResult =
1050 transactionPreProcessImpl(jvRequest, role, signForParams, validatedLedgerAge, app);
1052 if (!preprocResult.second)
1053 return preprocResult.first;
1057 transactionConstructImpl(preprocResult.second, ledger->rules(), app);
1073 return transactionFormatResultImpl(txn.
second, apiVersion);
1082 if (!jvRequest.
isMember(jss::tx_json))
1085 json::Value const& txJson(jvRequest[jss::tx_json]);
1093 if (!txJson.
isMember(jss::Sequence))
1096 if (!txJson.
isMember(sfSigningPubKey.getJsonName()))
1102 if (!jvRequest.
isMember(jss::signature_target) &&
1106 RpcInvalidParams,
"When multi-signing 'tx_json.SigningPubKey' must be empty.");
1118 if (signers.empty())
1123 return (a[sfAccount] < b[sfAccount]);
1129 [](
STObject const& a,
STObject const& b) {
return (a[sfAccount] == b[sfAccount]); });
1131 if (dupIter != signers.
end())
1134 err <<
"Duplicate Signers:Signer:Account entries (" <<
toBase58((*dupIter)[sfAccount])
1135 <<
") are not allowed.";
1141 return elem[sfAccount] == signingForID;
1145 err <<
"A Signer may not be the transaction's Account (" <<
toBase58(signingForID) <<
").";
1157 unsigned apiVersion,
1165 JLOG(j.debug()) <<
"transactionSignFor: " << jvRequest;
1168 char const accountField[] =
"account";
1170 if (!jvRequest.
isMember(accountField))
1175 if (!signerAccountID)
1180 if (!jvRequest.
isMember(jss::tx_json))
1189 if (
auto checkResult =
1193 return std::move(checkResult).error();
1199 if (!txJson.
isMember(sfSigningPubKey.getJsonName()))
1200 txJson[sfSigningPubKey.getJsonName()] =
"";
1207 json::Value err = checkMultiSignFields(jvRequest);
1213 SigningForParams signForParams(*signerAccountID);
1215 TransactionPreProcessResult
const preprocResult =
1216 transactionPreProcessImpl(jvRequest, role, signForParams, validatedLedgerAge, app);
1218 if (!preprocResult.second)
1219 return preprocResult.first;
1222 signForParams.validMultiSign(),
"xrpl::RPC::transactionSignFor : valid multi-signature");
1228 acctMatchesPubKey(accountState, *signerAccountID, signForParams.getPublicKey());
1235 auto& sttx = preprocResult.second;
1239 signer[sfAccount] = *signerAccountID;
1240 signer.
setFieldVL(sfTxnSignature, signForParams.getSignature());
1241 signer.
setFieldVL(sfSigningPubKey, signForParams.getPublicKey().slice());
1244 auto const target = signForParams.getSignatureTarget();
1246 return sttx->peekFieldObject(*target);
1254 signers.emplaceBack(std::move(signer));
1259 auto err = sortAndValidateSigners(signers, sttx->getFeePayer());
1266 transactionConstructImpl(sttx, ledger->rules(), app);
1271 return transactionFormatResultImpl(txn.
second, apiVersion);
1278 unsigned apiVersion,
1287 JLOG(j.debug()) <<
"transactionSubmitMultiSigned: " << jvRequest;
1293 json::Value err = checkMultiSignFields(jvRequest);
1300 auto [txJsonResult, srcAddressID] = checkTxJsonFields(
1310 return std::move(txJsonResult);
1317 JLOG(j.debug()) <<
"transactionSubmitMultiSigned: Failed to find source account "
1318 <<
"in current ledger: " <<
toBase58(srcAddressID);
1330 err = checkPayment(jvRequest, txJson, srcAddressID, role, app,
false);
1340 if (!parsedTxJson.
object)
1343 jvResult[
"error"] = parsedTxJson.
error[
"error"];
1344 jvResult[
"error_code"] = parsedTxJson.
error[
"error_code"];
1345 jvResult[
"error_message"] = parsedTxJson.
error[
"error_message"];
1360 RpcInternal,
"Exception while serializing transaction: " + reason);
1373 if (!stTx->getFieldVL(sfSigningPubKey).empty())
1376 err <<
"Invalid " << sfSigningPubKey.fieldName
1377 <<
" field. Field must be empty when multi-signing.";
1382 if (stTx->isFieldPresent(sfTxnSignature))
1386 auto const fee = stTx->getFieldAmount(sfFee);
1391 err <<
"Invalid " << sfFee.fieldName <<
" field. Fees must be specified in XRP.";
1397 err <<
"Invalid " << sfFee.fieldName <<
" field. Fees must be greater than zero.";
1403 if (!stTx->isFieldPresent(sfSigners))
1408 auto& signers = stTx->peekFieldArray(sfSigners);
1410 if (signers.empty())
1420 }) != signers.
end())
1428 auto err = sortAndValidateSigners(signers, stTx->getFeePayer());
1434 transactionConstructImpl(stTx, ledger->rules(), app);
1450 return transactionFormatResultImpl(txn.
second, apiVersion);
T adjacent_find(T... args)
Value removeMember(char const *key)
Remove and return the named member.
const_iterator end() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
virtual bool checkSigs() const =0
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Like std::vector<char> but better.
A currency issued by an account.
Manages the current fee schedule.
bool isLoadedCluster() const
virtual std::uint32_t getNetworkID() const noexcept=0
Get the configured network ID.
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Calculates payment paths.
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
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.
Slice slice() const noexcept
void setSignatureTarget(std::optional< std::reference_wrapper< SField const > > const &field)
std::optional< PublicKey > multiSignPublicKey_
bool validMultiSign() const
AccountID const & getSigner() const
bool isSingleSigning() const
std::optional< std::reference_wrapper< SField const > > const & getSignatureTarget() const
void setPublicKey(PublicKey const &multiSignPublicKey)
PublicKey const & getPublicKey() const
bool isMultiSigning() const
Buffer const & getSignature() const
SigningForParams(AccountID const &multiSigningAcctID)
void moveMultiSignature(Buffer &&multiSignature)
std::optional< std::reference_wrapper< SField const > > signatureTarget_
AccountID const *const multiSigningAcctID_
SigningForParams(SigningForParams const &rhs)=delete
Rules controlling protocol behavior.
static SField const & getField(int fieldCode)
constexpr bool holds() const noexcept
constexpr TIss const & get() const
bool native() const noexcept
Asset const & asset() const
AccountID const & getIssuer() const
std::shared_ptr< STLedgerEntry const > const_pointer
void setFieldVL(SField const &field, Blob const &)
STArray & peekFieldArray(SField const &field)
void setFieldArray(SField const &field, STArray const &v)
bool isFieldPresent(SField const &field) const
static STObject makeInnerObject(SField const &name)
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
static constexpr std::size_t kMaxMultiSigners
constexpr std::uint32_t value() const
Slice slice() const noexcept
virtual beast::Journal getJournal(std::string const &name)=0
virtual OpenLedger & getOpenLedger()=0
virtual NetworkIDService & getNetworkIDService()=0
virtual LoadFeeTrack & getFeeTrack()=0
virtual HashRouter & getHashRouter()=0
An immutable linear range of bytes.
std::shared_ptr< Transaction > pointer
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
SeqProxy nextQueuableSeq(SLE::const_ref sleAccount) const
Return the next sequence that would go in the TxQ for an account.
json::Value jsonClipped() const
JSON (JavaScript Object Notation).
static constexpr int kDefaultAutoFillFeeDivisor
constexpr auto kMaxValidatedLedgerAge
static constexpr int kDefaultAutoFillFeeMultiplier
static std::expected< void, json::Value > checkNetworkID(json::Value const &txJson, uint32_t appNetworkId)
static std::pair< json::Value, AccountID > checkTxJsonFields(json::Value const &txJson, Role const role, bool const verify, std::chrono::seconds validatedLedgerAge, Config const &config, LoadFeeTrack const &feeTrack, unsigned apiVersion)
static ErrorCodeI acctMatchesPubKey(SLE::const_pointer accountState, AccountID const &accountID, PublicKey const &publicKey)
static json::Value checkMultiSignFields(json::Value const &jvRequest)
static json::Value transactionFormatResultImpl(Transaction::pointer tpTrans, unsigned apiVersion)
static json::Value sortAndValidateSigners(STArray &signers, AccountID const &signingForID)
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 checkPayment(json::Value const ¶ms, json::Value &txJson, AccountID const &srcAddressID, Role const role, Application &app, bool doPath)
API version numbers used in later API versions.
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.
std::string missingFieldMessage(std::string const &name)
void insertDeliverMax(json::Value &txJson, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
std::optional< std::pair< PublicKey, SecretKey > > keypairForSignature(json::Value const ¶ms, json::Value &error, unsigned int apiVersion)
Generates a keypair for signature from RPC parameters.
static XRPAmount getTxFee(Application const &app, Config const &config, json::Value tx)
json::Value objectFieldError(std::string const &name)
json::Value transactionSignFor(json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app)
Returns a json::ValueType::Object.
std::string invalidFieldMessage(std::string const &name)
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
json::Value transactionSign(json::Value jvRequest, unsigned apiVersion, NetworkOPs::FailHard failType, Role role, std::chrono::seconds validatedLedgerAge, Application &app)
Returns a json::ValueType::Object.
std::function< void( std::shared_ptr< Transaction > &transaction, bool bUnlimited, bool bLocal, NetworkOPs::FailHard failType)> ProcessTransactionFn
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::ValueType::Object.
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::ValueType::Object.
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)
std::string expectedFieldMessage(std::string const &name, std::string const &type)
unsigned int getAPIVersionNumber(json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
bool containsError(json::Value const &json)
Returns true if the json contains an rpc error specification.
json::Value invalidFieldError(std::string const &name)
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
json::Value missingFieldError(std::string const &name)
Keylet account(AccountID const &id) noexcept
AccountID root.
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
@ Valid
Signature and local checks are good / passed.
@ SigGoodOnly
Signature is good, but local checks fail.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules)
Checks transaction signature and local checks.
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
bool isLegalNet(STAmount const &value)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
bool transResultInfo(TER code, std::string &token, std::string &text)
std::string to_string(BaseUInt< Bits, Tag > const &a)
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
json::Value rpcError(ErrorCodeI iError)
Role
Indicates the level of administrative permission to grant.
bool amountFromJsonNoThrow(STAmount &result, json::Value const &jvSource)
FeeLevel< std::uint64_t > FeeLevel64
bool passesLocalChecks(STObject const &st, std::string &)
AccountID calcAccountID(PublicKey const &pk)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
std::vector< unsigned char > Blob
Storage for linear binary data.
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)
Serializer buildMultiSigningData(STObject const &obj, AccountID const &signingID)
Return a Serializer suitable for computing a multisigning TxnSignature.
XRPAmount referenceFee
The cost of a reference transaction in drops.
TransactionPreProcessResult & operator=(TransactionPreProcessResult &&)=delete
TransactionPreProcessResult(std::shared_ptr< STTx > &&st)
std::shared_ptr< STTx > const second
TransactionPreProcessResult(json::Value &&json)
TransactionPreProcessResult(TransactionPreProcessResult &&rhs)=default
TransactionPreProcessResult & operator=(TransactionPreProcessResult const &)=delete
TransactionPreProcessResult(TransactionPreProcessResult const &)=delete
TransactionPreProcessResult()=delete