1#include <xrpl/tx/transactors/credentials/CredentialCreate.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/core/ServiceRegistry.h>
6#include <xrpl/ledger/ApplyView.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/CredentialHelpers.h>
9#include <xrpl/ledger/helpers/DirectoryHelpers.h>
10#include <xrpl/ledger/helpers/SponsorHelpers.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Keylet.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/Protocol.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/TxFlags.h>
21#include <xrpl/protocol/XRPAmount.h>
22#include <xrpl/tx/Transactor.h>
55 auto const& tx = ctx.
tx;
60 JLOG(j.trace()) <<
"Malformed transaction: Invalid Subject";
64 auto const uri = tx[~sfURI];
67 JLOG(j.trace()) <<
"Malformed transaction: invalid size of URI.";
71 auto const credType = tx[sfCredentialType];
74 JLOG(j.trace()) <<
"Malformed transaction: invalid size of CredentialType.";
84 auto const credType(ctx.
tx[sfCredentialType]);
85 auto const subject = ctx.
tx[sfSubject];
91 JLOG(ctx.
j.
trace()) <<
"Subject doesn't exist.";
97 JLOG(ctx.
j.
trace()) <<
"Credential already exists.";
103 JLOG(ctx.
j.
trace()) <<
"Subject is a pseudo-account.";
113 auto const subject =
ctx_.tx[sfSubject];
114 auto const credType(
ctx_.tx[sfCredentialType]);
121 auto const optExp =
ctx_.tx[~sfExpiration];
125 ctx_.view().header().parentCloseTime.time_since_epoch().count();
127 if (closeTime > *optExp)
129 JLOG(
j_.trace()) <<
"Malformed transaction: "
130 "Expiration time is in the past.";
134 sleCred->setFieldU32(sfExpiration, *optExp);
146 sleCred->setAccountID(sfSubject, subject);
148 sleCred->setFieldVL(sfCredentialType, credType);
150 if (
ctx_.tx.isFieldPresent(sfURI))
151 sleCred->setFieldVL(sfURI,
ctx_.tx.getFieldVL(sfURI));
156 JLOG(
j_.trace()) <<
"Adding Credential to owner directory " <<
to_string(credentialKey.
key)
157 <<
": " << (page ?
"success" :
"failure");
160 sleCred->setFieldU64(sfIssuerNode, *page);
168 sleCred->setFieldU32(sfFlags, lsfAccepted);
176 JLOG(
j_.trace()) <<
"Adding Credential to subject directory "
177 <<
to_string(credentialKey.
key) <<
": " << (page ?
"success" :
"failure");
180 sleCred->setFieldU64(sfSubjectNode, *page);
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::ref sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Insert an entry to a directory.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
std::shared_ptr< STLedgerEntry const > const & const_ref
AccountID const accountID_
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void increaseOwnerCount(ApplyView &view, SLE::ref accountSle, SLE::ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
constexpr std::size_t kMaxCredentialTypeLength
The maximum length of a CredentialType inside a Credential.
std::string to_string(BaseUInt< Bits, Tag > const &a)
void addSponsorToLedgerEntry(SLE::ref sle, SLE::const_ref sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
TERSubset< CanCvtToNotTEC > NotTEC
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
TER checkReserve(ApplyViewContext ctx, SLE::const_ref accSle, XRPAmount accBalance, SLE::const_ref sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
constexpr std::size_t kMaxCredentialUriLength
The maximum length of a URI inside a Credential.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
constexpr FlagValue tfUniversalMask
A pair of SHAMap key and LedgerEntryType.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.