rippled
Loading...
Searching...
No Matches
DID.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/tx/detail/DID.h>
21#include <xrpld/ledger/ApplyView.h>
22#include <xrpld/ledger/View.h>
23
24#include <xrpl/basics/Log.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Indexes.h>
27#include <xrpl/protocol/TxFlags.h>
28
29namespace ripple {
30
31/*
32 DID
33 ======
34
35 Decentralized Identifiers (DIDs) are a new type of identifier that enable
36 verifiable, self-sovereign digital identity and are designed to be
37 compatible with any distributed ledger or network. This implementation
38 conforms to the requirements specified in the DID v1.0 specification
39 currently recommended by the W3C Credentials Community Group
40 (https://www.w3.org/TR/did-core/).
41*/
42
43//------------------------------------------------------------------------------
44
47{
48 if (!ctx.rules.enabled(featureDID))
49 return temDISABLED;
50
51 if (ctx.tx.getFlags() & tfUniversalMask)
52 return temINVALID_FLAG;
53
54 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
55 return ret;
56
57 if (!ctx.tx.isFieldPresent(sfURI) &&
58 !ctx.tx.isFieldPresent(sfDIDDocument) && !ctx.tx.isFieldPresent(sfData))
59 return temEMPTY_DID;
60
61 if (ctx.tx.isFieldPresent(sfURI) && ctx.tx[sfURI].empty() &&
62 ctx.tx.isFieldPresent(sfDIDDocument) && ctx.tx[sfDIDDocument].empty() &&
63 ctx.tx.isFieldPresent(sfData) && ctx.tx[sfData].empty())
64 return temEMPTY_DID;
65
66 auto isTooLong = [&](auto const& sField, std::size_t length) -> bool {
67 if (auto field = ctx.tx[~sField])
68 return field->length() > length;
69 return false;
70 };
71
72 if (isTooLong(sfURI, maxDIDURILength) ||
73 isTooLong(sfDIDDocument, maxDIDDocumentLength) ||
74 isTooLong(sfData, maxDIDAttestationLength))
75 return temMALFORMED;
76
77 return preflight2(ctx);
78}
79
80TER
82 ApplyContext& ctx,
83 std::shared_ptr<SLE> const& sle,
84 AccountID const& owner)
85{
86 auto const sleAccount = ctx.view().peek(keylet::account(owner));
87 if (!sleAccount)
88 return tefINTERNAL;
89
90 // Check reserve availability for new object creation
91 {
92 auto const balance = STAmount((*sleAccount)[sfBalance]).xrp();
93 auto const reserve =
94 ctx.view().fees().accountReserve((*sleAccount)[sfOwnerCount] + 1);
95
96 if (balance < reserve)
98 }
99
100 // Add ledger object to ledger
101 ctx.view().insert(sle);
102
103 // Add ledger object to owner's page
104 {
105 auto page = ctx.view().dirInsert(
106 keylet::ownerDir(owner), sle->key(), describeOwnerDir(owner));
107 if (!page)
108 return tecDIR_FULL;
109 (*sle)[sfOwnerNode] = *page;
110 }
111 adjustOwnerCount(ctx.view(), sleAccount, 1, ctx.journal);
112 ctx.view().update(sleAccount);
113
114 return tesSUCCESS;
115}
116
117TER
119{
120 // Edit ledger object if it already exists
121 Keylet const didKeylet = keylet::did(account_);
122 if (auto const sleDID = ctx_.view().peek(didKeylet))
123 {
124 auto update = [&](auto const& sField) {
125 if (auto const field = ctx_.tx[~sField])
126 {
127 if (field->empty())
128 {
129 sleDID->makeFieldAbsent(sField);
130 }
131 else
132 {
133 (*sleDID)[sField] = *field;
134 }
135 }
136 };
137 update(sfURI);
138 update(sfDIDDocument);
139 update(sfData);
140
141 if (!sleDID->isFieldPresent(sfURI) &&
142 !sleDID->isFieldPresent(sfDIDDocument) &&
143 !sleDID->isFieldPresent(sfData))
144 {
145 return tecEMPTY_DID;
146 }
147 ctx_.view().update(sleDID);
148 return tesSUCCESS;
149 }
150
151 // Create new ledger object otherwise
152 auto const sleDID = std::make_shared<SLE>(didKeylet);
153 (*sleDID)[sfAccount] = account_;
154
155 auto set = [&](auto const& sField) {
156 if (auto const field = ctx_.tx[~sField]; field && !field->empty())
157 (*sleDID)[sField] = *field;
158 };
159
160 set(sfURI);
161 set(sfDIDDocument);
162 set(sfData);
163 if (ctx_.view().rules().enabled(fixEmptyDID) &&
164 !sleDID->isFieldPresent(sfURI) &&
165 !sleDID->isFieldPresent(sfDIDDocument) &&
166 !sleDID->isFieldPresent(sfData))
167 {
168 return tecEMPTY_DID;
169 }
170
171 return addSLE(ctx_, sleDID, account_);
172}
173
174NotTEC
176{
177 if (!ctx.rules.enabled(featureDID))
178 return temDISABLED;
179
180 if (ctx.tx.getFlags() & tfUniversalMask)
181 return temINVALID_FLAG;
182
183 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
184 return ret;
185
186 return preflight2(ctx);
187}
188
189TER
191{
192 auto const sle = ctx.view().peek(sleKeylet);
193 if (!sle)
194 return tecNO_ENTRY;
195
196 return DIDDelete::deleteSLE(ctx.view(), sle, owner, ctx.journal);
197}
198
199TER
201 ApplyView& view,
203 AccountID const owner,
205{
206 // Remove object from owner directory
207 if (!view.dirRemove(
208 keylet::ownerDir(owner), (*sle)[sfOwnerNode], sle->key(), true))
209 {
210 JLOG(j.fatal()) << "Unable to delete DID Token from owner.";
211 return tefBAD_LEDGER;
212 }
213
214 auto const sleOwner = view.peek(keylet::account(owner));
215 if (!sleOwner)
216 return tecINTERNAL;
217
218 adjustOwnerCount(view, sleOwner, -1, j);
219 view.update(sleOwner);
220
221 // Remove object from ledger
222 view.erase(sle);
223 return tesSUCCESS;
224}
225
226TER
228{
230}
231
232} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
Stream fatal() const
Definition: Journal.h:352
State information when applying a tx.
Definition: ApplyContext.h:37
ApplyView & view()
Definition: ApplyContext.h:55
beast::Journal const journal
Definition: ApplyContext.h:52
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:141
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
Definition: ApplyView.cpp:190
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:315
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
static NotTEC preflight(PreflightContext const &ctx)
Definition: DID.cpp:175
static TER deleteSLE(ApplyContext &ctx, Keylet sleKeylet, AccountID const owner)
Definition: DID.cpp:190
TER doApply() override
Definition: DID.cpp:227
static NotTEC preflight(PreflightContext const &ctx)
Definition: DID.cpp:46
TER doApply() override
Definition: DID.cpp:118
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:130
XRPAmount xrp() const
Definition: STAmount.cpp:305
bool empty() const
Definition: STObject.h:925
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:484
std::uint32_t getFlags() const
Definition: STObject.cpp:537
AccountID const account_
Definition: Transactor.h:92
ApplyView & view()
Definition: Transactor.h:108
ApplyContext & ctx_
Definition: Transactor.h:89
Keylet did(AccountID const &account) noexcept
Definition: Indexes.cpp:497
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:175
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:365
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::size_t constexpr maxDIDURILength
The maximum length of a URI inside a DID.
Definition: Protocol.h:92
std::size_t constexpr maxDIDAttestationLength
The maximum length of an Attestation inside a DID.
Definition: Protocol.h:95
TER addSLE(ApplyContext &ctx, std::shared_ptr< SLE > const &sle, AccountID const &owner)
Definition: DID.cpp:81
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Definition: BasicConfig.h:315
std::size_t constexpr maxDIDDocumentLength
The maximum length of a Data element inside a DID.
Definition: Protocol.h:89
bool isTesSuccess(TER x)
Definition: TER.h:656
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:925
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:82
@ tefBAD_LEDGER
Definition: TER.h:170
@ tefINTERNAL
Definition: TER.h:173
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:186
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:134
@ tecNO_ENTRY
Definition: TER.h:293
@ tecDIR_FULL
Definition: TER.h:274
@ tecINTERNAL
Definition: TER.h:297
@ tecEMPTY_DID
Definition: TER.h:340
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:294
@ tesSUCCESS
Definition: TER.h:242
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:62
TERSubset< CanCvtToTER > TER
Definition: TER.h:627
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:587
@ temMALFORMED
Definition: TER.h:87
@ temINVALID_FLAG
Definition: TER.h:111
@ temDISABLED
Definition: TER.h:114
@ temEMPTY_DID
Definition: TER.h:138
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: protocol/Fees.h:49
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
State information when preflighting a tx.
Definition: Transactor.h:33