Files
rippled/src/test/jtx/impl/token.cpp
Shawn Xie 305c9a8d61 fixNFTokenRemint: prevent NFT re-mint: (#4406)
Without the protocol amendment introduced by this commit, an NFT ID can
be reminted in this manner:

1. Alice creates an account and mints an NFT.
2. Alice burns the NFT with an `NFTokenBurn` transaction.
3. Alice deletes her account with an `AccountDelete` transaction.
4. Alice re-creates her account.
5. Alice mints an NFT with an `NFTokenMint` transaction with params:
   `NFTokenTaxon` = 0, `Flags` = 9).

This will mint a NFT with the same `NFTokenID` as the one minted in step
1. The params that construct the NFT ID will cause a collision in
`NFTokenID` if their values are equal before and after the remint.

With the `fixNFTokenRemint` amendment, there is a new sequence number
construct which avoids this scenario:

- A new `AccountRoot` field, `FirstNFTSequence`, stays constant over
  time.
  - This field is set to the current account sequence when the account
    issues their first NFT.
  - Otherwise, it is not set.
- The sequence of a newly-minted NFT is computed by: `FirstNFTSequence +
  MintedNFTokens`.
  - `MintedNFTokens` is then incremented by 1 for each mint.

Furthermore, there is a new account deletion restriction:

- An account can only be deleted if `FirstNFTSequence + MintedNFTokens +
  256` is less than the current ledger sequence.
  - 256 was chosen because it already exists in the current account
    deletion constraint.

Without this restriction, an NFT may still be remintable. Example
scenario:

1. Alice's account sequence is at 1.
2. Bob is Alice's authorized minter.
3. Bob mints 500 NFTs for Alice. The NFTs will have sequences 1-501, as
   NFT sequence is computed by `FirstNFTokenSequence + MintedNFTokens`).
4. Alice deletes her account at ledger 257 (as required by the existing
   `AccountDelete` amendment).
5. Alice re-creates her account at ledger 258.
6. Alice mints an NFT. `FirstNFTokenSequence` initializes to her account
   sequence (258), and `MintedNFTokens` initializes as 0. This
   newly-minted NFT would have a sequence number of 258, which is a
   duplicate of what she issued through authorized minting before she
   deleted her account.

---------

Signed-off-by: Shawn Xie <shawnxie920@gmail.com>
2023-03-20 14:47:46 -07:00

233 lines
6.1 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 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 <test/jtx/flags.h>
#include <test/jtx/token.h>
#include <ripple/app/tx/impl/NFTokenMint.h>
#include <ripple/protocol/SField.h>
#include <ripple/protocol/jss.h>
namespace ripple {
namespace test {
namespace jtx {
namespace token {
Json::Value
mint(jtx::Account const& account, std::uint32_t nfTokenTaxon)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfNFTokenTaxon.jsonName] = nfTokenTaxon;
jv[sfTransactionType.jsonName] = jss::NFTokenMint;
return jv;
}
void
xferFee::operator()(Env& env, JTx& jt) const
{
jt.jv[sfTransferFee.jsonName] = xferFee_;
}
void
issuer::operator()(Env& env, JTx& jt) const
{
jt.jv[sfIssuer.jsonName] = issuer_;
}
void
uri::operator()(Env& env, JTx& jt) const
{
jt.jv[sfURI.jsonName] = uri_;
}
uint256
getNextID(
jtx::Env const& env,
jtx::Account const& issuer,
std::uint32_t nfTokenTaxon,
std::uint16_t flags,
std::uint16_t xferFee)
{
// Get the nftSeq from the account root of the issuer.
std::uint32_t const nftSeq = {
env.le(issuer)->at(~sfMintedNFTokens).value_or(0)};
return token::getID(env, issuer, nfTokenTaxon, nftSeq, flags, xferFee);
}
uint256
getID(
jtx::Env const& env,
jtx::Account const& issuer,
std::uint32_t nfTokenTaxon,
std::uint32_t nftSeq,
std::uint16_t flags,
std::uint16_t xferFee)
{
if (env.current()->rules().enabled(fixNFTokenRemint))
{
// If fixNFTokenRemint is enabled, we must add issuer's
// FirstNFTokenSequence to offset the starting NFT sequence number.
nftSeq += env.le(issuer)
->at(~sfFirstNFTokenSequence)
.value_or(env.seq(issuer));
}
return ripple::NFTokenMint::createNFTokenID(
flags, xferFee, issuer, nft::toTaxon(nfTokenTaxon), nftSeq);
}
Json::Value
burn(jtx::Account const& account, uint256 const& nftokenID)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfNFTokenID.jsonName] = to_string(nftokenID);
jv[jss::TransactionType] = jss::NFTokenBurn;
return jv;
}
Json::Value
createOffer(
jtx::Account const& account,
uint256 const& nftokenID,
STAmount const& amount)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfNFTokenID.jsonName] = to_string(nftokenID);
jv[sfAmount.jsonName] = amount.getJson(JsonOptions::none);
jv[jss::TransactionType] = jss::NFTokenCreateOffer;
return jv;
}
void
owner::operator()(Env& env, JTx& jt) const
{
jt.jv[sfOwner.jsonName] = owner_;
}
void
expiration::operator()(Env& env, JTx& jt) const
{
jt.jv[sfExpiration.jsonName] = expires_;
}
void
destination::operator()(Env& env, JTx& jt) const
{
jt.jv[sfDestination.jsonName] = dest_;
}
template <typename T>
static Json::Value
cancelOfferImpl(jtx::Account const& account, T const& nftokenOffers)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
if (!empty(nftokenOffers))
{
jv[sfNFTokenOffers.jsonName] = Json::arrayValue;
for (uint256 const& nftokenOffer : nftokenOffers)
jv[sfNFTokenOffers.jsonName].append(to_string(nftokenOffer));
}
jv[jss::TransactionType] = jss::NFTokenCancelOffer;
return jv;
}
Json::Value
cancelOffer(
jtx::Account const& account,
std::initializer_list<uint256> const& nftokenOffers)
{
return cancelOfferImpl(account, nftokenOffers);
}
Json::Value
cancelOffer(
jtx::Account const& account,
std::vector<uint256> const& nftokenOffers)
{
return cancelOfferImpl(account, nftokenOffers);
}
void
rootIndex::operator()(Env& env, JTx& jt) const
{
jt.jv[sfRootIndex.jsonName] = rootIndex_;
}
Json::Value
acceptBuyOffer(jtx::Account const& account, uint256 const& offerIndex)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfNFTokenBuyOffer.jsonName] = to_string(offerIndex);
jv[jss::TransactionType] = jss::NFTokenAcceptOffer;
return jv;
}
Json::Value
acceptSellOffer(jtx::Account const& account, uint256 const& offerIndex)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfNFTokenSellOffer.jsonName] = to_string(offerIndex);
jv[jss::TransactionType] = jss::NFTokenAcceptOffer;
return jv;
}
Json::Value
brokerOffers(
jtx::Account const& account,
uint256 const& buyOfferIndex,
uint256 const& sellOfferIndex)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfNFTokenBuyOffer.jsonName] = to_string(buyOfferIndex);
jv[sfNFTokenSellOffer.jsonName] = to_string(sellOfferIndex);
jv[jss::TransactionType] = jss::NFTokenAcceptOffer;
return jv;
}
void
brokerFee::operator()(Env& env, JTx& jt) const
{
jt.jv[sfNFTokenBrokerFee.jsonName] = brokerFee_.getJson(JsonOptions::none);
}
Json::Value
setMinter(jtx::Account const& account, jtx::Account const& minter)
{
Json::Value jt = fset(account, asfAuthorizedNFTokenMinter);
jt[sfNFTokenMinter.fieldName] = minter.human();
return jt;
}
Json::Value
clearMinter(jtx::Account const& account)
{
return fclear(account, asfAuthorizedNFTokenMinter);
}
} // namespace token
} // namespace jtx
} // namespace test
} // namespace ripple