rippled
Loading...
Searching...
No Matches
CredentialCreate.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/ledger/ApplyView.h>
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/ledger/helpers/CredentialHelpers.h>
5#include <xrpl/ledger/helpers/DirectoryHelpers.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/TxFlags.h>
9#include <xrpl/tx/transactors/credentials/CredentialCreate.h>
10
11#include <chrono>
12
13namespace xrpl {
14
15/*
16 Credentials
17 ======
18
19 A verifiable credentials (VC
20 https://en.wikipedia.org/wiki/Verifiable_credentials), as defined by the W3C
21 specification (https://www.w3.org/TR/vc-data-model-2.0/), is a
22 secure and tamper-evident way to represent information about a subject, such
23 as an individual, organization, or even an IoT device. These credentials are
24 issued by a trusted entity and can be verified by third parties without
25 directly involving the issuer at all.
26*/
27
28using namespace credentials;
29
32{
33 // 0 means "Allow any flags"
34 return ctx.rules.enabled(fixInvalidTxFlags) ? tfUniversalMask : 0;
35}
36
39{
40 auto const& tx = ctx.tx;
41 auto& j = ctx.j;
42
43 if (!tx[sfSubject])
44 {
45 JLOG(j.trace()) << "Malformed transaction: Invalid Subject";
46 return temMALFORMED;
47 }
48
49 auto const uri = tx[~sfURI];
50 if (uri && (uri->empty() || (uri->size() > maxCredentialURILength)))
51 {
52 JLOG(j.trace()) << "Malformed transaction: invalid size of URI.";
53 return temMALFORMED;
54 }
55
56 auto const credType = tx[sfCredentialType];
57 if (credType.empty() || (credType.size() > maxCredentialTypeLength))
58 {
59 JLOG(j.trace()) << "Malformed transaction: invalid size of CredentialType.";
60 return temMALFORMED;
61 }
62
63 return tesSUCCESS;
64}
65
66TER
68{
69 auto const credType(ctx.tx[sfCredentialType]);
70 auto const subject = ctx.tx[sfSubject];
71
72 if (!ctx.view.exists(keylet::account(subject)))
73 {
74 JLOG(ctx.j.trace()) << "Subject doesn't exist.";
75 return tecNO_TARGET;
76 }
77
78 if (ctx.view.exists(keylet::credential(subject, ctx.tx[sfAccount], credType)))
79 {
80 JLOG(ctx.j.trace()) << "Credential already exists.";
81 return tecDUPLICATE;
82 }
83
84 return tesSUCCESS;
85}
86
87TER
89{
90 auto const subject = ctx_.tx[sfSubject];
91 auto const credType(ctx_.tx[sfCredentialType]);
92 Keylet const credentialKey = keylet::credential(subject, account_, credType);
93
94 auto const sleCred = std::make_shared<SLE>(credentialKey);
95 if (!sleCred)
96 return tefINTERNAL; // LCOV_EXCL_LINE
97
98 auto const optExp = ctx_.tx[~sfExpiration];
99 if (optExp)
100 {
101 std::uint32_t const closeTime =
103
104 if (closeTime > *optExp)
105 {
106 JLOG(j_.trace()) << "Malformed transaction: "
107 "Expiration time is in the past.";
108 return tecEXPIRED;
109 }
110
111 sleCred->setFieldU32(sfExpiration, *optExp);
112 }
113
114 auto const sleIssuer = view().peek(keylet::account(account_));
115 if (!sleIssuer)
116 return tefINTERNAL; // LCOV_EXCL_LINE
117
118 {
119 STAmount const reserve{
120 view().fees().accountReserve(sleIssuer->getFieldU32(sfOwnerCount) + 1)};
121 if (preFeeBalance_ < reserve)
123 }
124
125 sleCred->setAccountID(sfSubject, subject);
126 sleCred->setAccountID(sfIssuer, account_);
127 sleCred->setFieldVL(sfCredentialType, credType);
128
129 if (ctx_.tx.isFieldPresent(sfURI))
130 sleCred->setFieldVL(sfURI, ctx_.tx.getFieldVL(sfURI));
131
132 {
133 auto const page =
135 JLOG(j_.trace()) << "Adding Credential to owner directory " << to_string(credentialKey.key)
136 << ": " << (page ? "success" : "failure");
137 if (!page)
138 return tecDIR_FULL;
139 sleCred->setFieldU64(sfIssuerNode, *page);
140
141 adjustOwnerCount(view(), sleIssuer, 1, j_);
142 }
143
144 if (subject == account_)
145 {
146 sleCred->setFieldU32(sfFlags, lsfAccepted);
147 }
148 else
149 {
150 // Added to both dirs, owned only by issuer. CredentialAccept will transfer ownership to
151 // subject. CredentialDelete will remove from both dirs and decrement 1 ownerCount.
152 auto const page =
153 view().dirInsert(keylet::ownerDir(subject), credentialKey, describeOwnerDir(subject));
154 JLOG(j_.trace()) << "Adding Credential to subject directory "
155 << to_string(credentialKey.key) << ": " << (page ? "success" : "failure");
156 if (!page)
157 return tecDIR_FULL;
158 sleCred->setFieldU64(sfSubjectNode, *page);
159 }
160
161 view().insert(sleCred);
162
163 return tesSUCCESS;
164}
165
166} // namespace xrpl
Stream trace() const
Severity stream access functions.
Definition Journal.h:295
STTx const & tx
ApplyView & view()
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:289
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:120
Blob getFieldVL(SField const &field) const
Definition STObject.cpp:641
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:456
AccountID const account_
Definition Transactor.h:116
beast::Journal const j_
Definition Transactor.h:114
ApplyView & view()
Definition Transactor.h:132
XRPAmount preFeeBalance_
Definition Transactor.h:117
ApplyContext & ctx_
Definition Transactor.h:112
T is_same_v
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition Indexes.cpp:498
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::size_t constexpr maxCredentialURILength
The maximum length of a URI inside a Credential.
Definition Protocol.h:218
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
@ tefINTERNAL
Definition TER.h:153
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
@ temMALFORMED
Definition TER.h:67
@ tecDIR_FULL
Definition TER.h:268
@ tecNO_TARGET
Definition TER.h:285
@ tecEXPIRED
Definition TER.h:295
@ tecINSUFFICIENT_RESERVE
Definition TER.h:288
@ tecDUPLICATE
Definition TER.h:296
constexpr FlagValue tfUniversalMask
Definition TxFlags.h:43
@ tesSUCCESS
Definition TER.h:225
std::size_t constexpr maxCredentialTypeLength
The maximum length of a CredentialType inside a Credential.
Definition Protocol.h:221
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
NetClock::time_point parentCloseTime
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
beast::Journal const j
Definition Transactor.h:65
State information when preflighting a tx.
Definition Transactor.h:14
beast::Journal const j
Definition Transactor.h:21
T time_since_epoch(T... args)