-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
NFTokenMint.cpp
278 lines (232 loc) · 10.1 KB
/
NFTokenMint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2021 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/app/tx/impl/NFTokenMint.h>
#include <ripple/basics/Expected.h>
#include <ripple/basics/Log.h>
#include <ripple/ledger/View.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/InnerObjectFormats.h>
#include <ripple/protocol/Rate.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/protocol/st.h>
#include <boost/endian/conversion.hpp>
#include <array>
namespace ripple {
NotTEC
NFTokenMint::preflight(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featureNonFungibleTokensV1))
return temDISABLED;
if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;
// Prior to fixRemoveNFTokenAutoTrustLine, transfer of an NFToken between
// accounts allowed a TrustLine to be added to the issuer of that token
// without explicit permission from that issuer. This was enabled by
// minting the NFToken with the tfTrustLine flag set.
//
// That capability could be used to attack the NFToken issuer. It
// would be possible for two accounts to trade the NFToken back and forth
// building up any number of TrustLines on the issuer, increasing the
// issuer's reserve without bound.
//
// The fixRemoveNFTokenAutoTrustLine amendment disables minting with the
// tfTrustLine flag as a way to prevent the attack. But until the
// amendment passes we still need to keep the old behavior available.
std::uint32_t const NFTokenMintMask =
ctx.rules.enabled(fixRemoveNFTokenAutoTrustLine) ? tfNFTokenMintMask
: tfNFTokenMintOldMask;
if (ctx.tx.getFlags() & NFTokenMintMask)
return temINVALID_FLAG;
if (auto const f = ctx.tx[~sfTransferFee])
{
if (f > maxTransferFee)
return temBAD_NFTOKEN_TRANSFER_FEE;
// If a non-zero TransferFee is set then the tfTransferable flag
// must also be set.
if (f > 0u && !ctx.tx.isFlag(tfTransferable))
return temMALFORMED;
}
// An issuer must only be set if the tx is executed by the minter
if (auto iss = ctx.tx[~sfIssuer]; iss == ctx.tx[sfAccount])
return temMALFORMED;
if (auto uri = ctx.tx[~sfURI])
{
if (uri->length() == 0 || uri->length() > maxTokenURILength)
return temMALFORMED;
}
return preflight2(ctx);
}
uint256
NFTokenMint::createNFTokenID(
std::uint16_t flags,
std::uint16_t fee,
AccountID const& issuer,
nft::Taxon taxon,
std::uint32_t tokenSeq)
{
// An issuer may issue several NFTs with the same taxon; to ensure that NFTs
// are spread across multiple pages we lightly mix the taxon up by using the
// sequence (which is not under the issuer's direct control) as the seed for
// a simple linear congruential generator. cipheredTaxon() does this work.
taxon = nft::cipheredTaxon(tokenSeq, taxon);
// The values are packed inside a 32-byte buffer, so we need to make sure
// that the endianess is fixed.
flags = boost::endian::native_to_big(flags);
fee = boost::endian::native_to_big(fee);
taxon = nft::toTaxon(boost::endian::native_to_big(nft::toUInt32(taxon)));
tokenSeq = boost::endian::native_to_big(tokenSeq);
std::array<std::uint8_t, 32> buf{};
auto ptr = buf.data();
// This code is awkward but the idea is to pack these values into a single
// 256-bit value that uniquely identifies this NFT.
std::memcpy(ptr, &flags, sizeof(flags));
ptr += sizeof(flags);
std::memcpy(ptr, &fee, sizeof(fee));
ptr += sizeof(fee);
std::memcpy(ptr, issuer.data(), issuer.size());
ptr += issuer.size();
std::memcpy(ptr, &taxon, sizeof(taxon));
ptr += sizeof(taxon);
std::memcpy(ptr, &tokenSeq, sizeof(tokenSeq));
ptr += sizeof(tokenSeq);
assert(std::distance(buf.data(), ptr) == buf.size());
return uint256::fromVoid(buf.data());
}
TER
NFTokenMint::preclaim(PreclaimContext const& ctx)
{
// The issuer of the NFT may or may not be the account executing this
// transaction. Check that and verify that this is allowed:
if (auto issuer = ctx.tx[~sfIssuer])
{
auto const sle = ctx.view.read(keylet::account(*issuer));
if (!sle)
return tecNO_ISSUER;
if (auto const minter = (*sle)[~sfNFTokenMinter];
minter != ctx.tx[sfAccount])
return tecNO_PERMISSION;
}
return tesSUCCESS;
}
TER
NFTokenMint::doApply()
{
auto const issuer = ctx_.tx[~sfIssuer].value_or(account_);
auto const tokenSeq = [this, &issuer]() -> Expected<std::uint32_t, TER> {
auto const root = view().peek(keylet::account(issuer));
if (root == nullptr)
// Should not happen. Checked in preclaim.
return Unexpected(tecNO_ISSUER);
if (!ctx_.view().rules().enabled(fixNFTokenRemint))
{
// Get the unique sequence number for this token:
std::uint32_t const tokenSeq =
(*root)[~sfMintedNFTokens].value_or(0);
{
std::uint32_t const nextTokenSeq = tokenSeq + 1;
if (nextTokenSeq < tokenSeq)
return Unexpected(tecMAX_SEQUENCE_REACHED);
(*root)[sfMintedNFTokens] = nextTokenSeq;
}
ctx_.view().update(root);
return tokenSeq;
}
// With fixNFTokenRemint amendment enabled:
//
// If the issuer hasn't minted an NFToken before we must add a
// FirstNFTokenSequence field to the issuer's AccountRoot. The
// value of the FirstNFTokenSequence must equal the issuer's
// current account sequence.
//
// There are three situations:
// o If the first token is being minted by the issuer and
// * If the transaction consumes a Sequence number, then the
// Sequence has been pre-incremented by the time we get here in
// doApply. We must decrement the value in the Sequence field.
// * Otherwise the transaction uses a Ticket so the Sequence has
// not been pre-incremented. We use the Sequence value as is.
// o The first token is being minted by an authorized minter. In
// this case the issuer's Sequence field has been left untouched.
// We use the issuer's Sequence value as is.
if (!root->isFieldPresent(sfFirstNFTokenSequence))
{
std::uint32_t const acctSeq = root->at(sfSequence);
root->at(sfFirstNFTokenSequence) =
ctx_.tx.isFieldPresent(sfIssuer) ||
ctx_.tx.getSeqProxy().isTicket()
? acctSeq
: acctSeq - 1;
}
std::uint32_t const mintedNftCnt =
(*root)[~sfMintedNFTokens].value_or(0u);
(*root)[sfMintedNFTokens] = mintedNftCnt + 1u;
if ((*root)[sfMintedNFTokens] == 0u)
return Unexpected(tecMAX_SEQUENCE_REACHED);
// Get the unique sequence number of this token by
// sfFirstNFTokenSequence + sfMintedNFTokens
std::uint32_t const offset = (*root)[sfFirstNFTokenSequence];
std::uint32_t const tokenSeq = offset + mintedNftCnt;
// Check for more overflow cases
if (tokenSeq + 1u == 0u || tokenSeq < offset)
return Unexpected(tecMAX_SEQUENCE_REACHED);
ctx_.view().update(root);
return tokenSeq;
}();
if (!tokenSeq.has_value())
return (tokenSeq.error());
std::uint32_t const ownerCountBefore =
view().read(keylet::account(account_))->getFieldU32(sfOwnerCount);
// Assemble the new NFToken.
SOTemplate const* nfTokenTemplate =
InnerObjectFormats::getInstance().findSOTemplateBySField(sfNFToken);
if (nfTokenTemplate == nullptr)
// Should never happen.
return tecINTERNAL;
STObject newToken(
*nfTokenTemplate,
sfNFToken,
[this, &issuer, &tokenSeq](STObject& object) {
object.setFieldH256(
sfNFTokenID,
createNFTokenID(
static_cast<std::uint16_t>(ctx_.tx.getFlags() & 0x0000FFFF),
ctx_.tx[~sfTransferFee].value_or(0),
issuer,
nft::toTaxon(ctx_.tx[sfNFTokenTaxon]),
tokenSeq.value()));
if (auto const uri = ctx_.tx[~sfURI])
object.setFieldVL(sfURI, *uri);
});
if (TER const ret =
nft::insertToken(ctx_.view(), account_, std::move(newToken));
ret != tesSUCCESS)
return ret;
// Only check the reserve if the owner count actually changed. This
// allows NFTs to be added to the page (and burn fees) without
// requiring the reserve to be met each time. The reserve is
// only managed when a new NFT page is added.
if (auto const ownerCountAfter =
view().read(keylet::account(account_))->getFieldU32(sfOwnerCount);
ownerCountAfter > ownerCountBefore)
{
if (auto const reserve = view().fees().accountReserve(ownerCountAfter);
mPriorBalance < reserve)
return tecINSUFFICIENT_RESERVE;
}
return tesSUCCESS;
}
} // namespace ripple