rippled
nft.h
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 #ifndef RIPPLE_PROTOCOL_NFT_H_INCLUDED
21 #define RIPPLE_PROTOCOL_NFT_H_INCLUDED
22 
23 #include <ripple/basics/base_uint.h>
24 #include <ripple/basics/tagged_integer.h>
25 #include <ripple/protocol/AccountID.h>
26 
27 #include <boost/endian/conversion.hpp>
28 
29 #include <cstdint>
30 #include <cstring>
31 
32 namespace ripple {
33 namespace nft {
34 
35 // Separate taxons from regular integers.
36 struct TaxonTag
37 {
38 };
40 
41 inline Taxon
43 {
44  return static_cast<Taxon>(i);
45 }
46 
47 inline std::uint32_t
49 {
50  return static_cast<std::uint32_t>(t);
51 }
52 
53 constexpr std::uint16_t const flagBurnable = 0x0001;
54 constexpr std::uint16_t const flagOnlyXRP = 0x0002;
55 constexpr std::uint16_t const flagCreateTrustLines = 0x0004;
56 constexpr std::uint16_t const flagTransferable = 0x0008;
57 
58 inline std::uint16_t
59 getFlags(uint256 const& id)
60 {
61  std::uint16_t flags;
62  memcpy(&flags, id.begin(), 2);
63  return boost::endian::big_to_native(flags);
64 }
65 
66 inline std::uint16_t
68 {
69  std::uint16_t fee;
70  memcpy(&fee, id.begin() + 2, 2);
71  return boost::endian::big_to_native(fee);
72 }
73 
74 inline std::uint32_t
75 getSerial(uint256 const& id)
76 {
77  std::uint32_t seq;
78  memcpy(&seq, id.begin() + 28, 4);
79  return boost::endian::big_to_native(seq);
80 }
81 
82 inline Taxon
84 {
85  // An issuer may issue several NFTs with the same taxon; to ensure that NFTs
86  // are spread across multiple pages we lightly mix the taxon up by using the
87  // sequence (which is not under the issuer's direct control) as the seed for
88  // a simple linear congruential generator.
89  //
90  // From the Hull-Dobell theorem we know that f(x)=(m*x+c) mod n will yield a
91  // permutation of [0, n) when n is a power of 2 if m is congruent to 1 mod 4
92  // and c is odd.
93  //
94  // Here we use m = 384160001 and c = 2459. The modulo is implicit because we
95  // use 2^32 for n and the arithmetic gives it to us for "free".
96  //
97  // Note that the scramble value we calculate is not cryptographically secure
98  // but that's fine since all we're looking for is some dispersion.
99  //
100  // **IMPORTANT** Changing these numbers would be a breaking change requiring
101  // an amendment along with a way to distinguish token IDs that
102  // were generated with the old code.
103  return taxon ^ toTaxon(((384160001 * tokenSeq) + 2459));
104 }
105 
106 inline Taxon
107 getTaxon(uint256 const& id)
108 {
109  std::uint32_t taxon;
110  memcpy(&taxon, id.begin() + 24, 4);
111  taxon = boost::endian::big_to_native(taxon);
112 
113  // The taxon cipher is just an XOR, so it is reversible by applying the
114  // XOR a second time.
115  return cipheredTaxon(getSerial(id), toTaxon(taxon));
116 }
117 
118 inline AccountID
119 getIssuer(uint256 const& id)
120 {
121  return AccountID::fromVoid(id.data() + 4);
122 }
123 
124 } // namespace nft
125 } // namespace ripple
126 
127 #endif
ripple::nft::getFlags
std::uint16_t getFlags(uint256 const &id)
Definition: nft.h:59
cstring
ripple::nft::flagTransferable
constexpr const std::uint16_t flagTransferable
Definition: nft.h:56
ripple::nft::flagOnlyXRP
constexpr const std::uint16_t flagOnlyXRP
Definition: nft.h:54
ripple::nft::toTaxon
Taxon toTaxon(std::uint32_t i)
Definition: nft.h:42
ripple::nft::getIssuer
AccountID getIssuer(uint256 const &id)
Definition: nft.h:119
ripple::base_uint< 256 >
ripple::nft::toUInt32
std::uint32_t toUInt32(Taxon t)
Definition: nft.h:48
ripple::nft::getSerial
std::uint32_t getSerial(uint256 const &id)
Definition: nft.h:75
cstdint
std::uint32_t
ripple::nft::flagBurnable
constexpr const std::uint16_t flagBurnable
Definition: nft.h:53
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::nft::Taxon
tagged_integer< std::uint32_t, TaxonTag > Taxon
Definition: nft.h:39
ripple::base_uint< 160, detail::AccountIDTag >::fromVoid
static base_uint fromVoid(void const *data)
Definition: base_uint.h:312
ripple::nft::TaxonTag
Definition: nft.h:36
ripple::nft::flagCreateTrustLines
constexpr const std::uint16_t flagCreateTrustLines
Definition: nft.h:55
ripple::tagged_integer
A type-safe wrap around standard integral types.
Definition: tagged_integer.h:44
ripple::nft::getTransferFee
std::uint16_t getTransferFee(uint256 const &id)
Definition: nft.h:67
ripple::nft::cipheredTaxon
Taxon cipheredTaxon(std::uint32_t tokenSeq, Taxon taxon)
Definition: nft.h:83
ripple::nft::getTaxon
Taxon getTaxon(uint256 const &id)
Definition: nft.h:107