xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
nft.h
1
#pragma once
2
3
#include <xrpl/basics/base_uint.h>
4
#include <xrpl/basics/tagged_integer.h>
5
#include <xrpl/protocol/AccountID.h>
6
7
#include <boost/endian/conversion.hpp>
8
9
#include <
cstdint
>
10
#include <
cstring
>
11
12
namespace
xrpl::nft
{
13
14
// Separate taxons from regular integers.
15
struct
TaxonTag
16
{
17
};
18
using
Taxon
=
TaggedInteger<std::uint32_t, TaxonTag>
;
19
20
inline
Taxon
21
toTaxon
(
std::uint32_t
i)
22
{
23
return
static_cast<
Taxon
>
(i);
24
}
25
26
inline
std::uint32_t
27
toUInt32
(
Taxon
t)
28
{
29
return
static_cast<
std::uint32_t
>
(t);
30
}
31
32
constexpr
std::uint16_t
const
kFlagBurnable
= 0x0001;
33
constexpr
std::uint16_t
const
kFlagOnlyXrp
= 0x0002;
34
constexpr
std::uint16_t
const
kFlagCreateTrustLines
= 0x0004;
35
constexpr
std::uint16_t
const
kFlagTransferable
= 0x0008;
36
constexpr
std::uint16_t
const
kFlagMutable
= 0x0010;
37
38
inline
std::uint16_t
39
getFlags
(
UInt256
const
&
id
)
40
{
41
std::uint16_t
flags = 0;
42
memcpy(&flags,
id
.begin(), 2);
43
return
boost::endian::big_to_native(flags);
44
}
45
46
inline
std::uint16_t
47
getTransferFee
(
UInt256
const
&
id
)
48
{
49
std::uint16_t
fee = 0;
50
memcpy(&fee,
id
.begin() + 2, 2);
51
return
boost::endian::big_to_native(fee);
52
}
53
54
inline
std::uint32_t
55
getSequence
(
UInt256
const
&
id
)
56
{
57
std::uint32_t
seq = 0;
58
memcpy(&seq,
id
.begin() + 28, 4);
59
return
boost::endian::big_to_native(seq);
60
}
61
62
inline
Taxon
63
cipheredTaxon
(
std::uint32_t
tokenSeq,
Taxon
taxon)
64
{
65
// An issuer may issue several NFTs with the same taxon; to ensure that NFTs
66
// are spread across multiple pages we lightly mix the taxon up by using the
67
// sequence (which is not under the issuer's direct control) as the seed for
68
// a simple linear congruential generator.
69
//
70
// From the Hull-Dobell theorem we know that f(x)=(m*x+c) mod n will yield a
71
// permutation of [0, n) when n is a power of 2 if m is congruent to 1 mod 4
72
// and c is odd.
73
//
74
// Here we use m = 384160001 and c = 2459. The modulo is implicit because we
75
// use 2^32 for n and the arithmetic gives it to us for "free".
76
//
77
// Note that the scramble value we calculate is not cryptographically secure
78
// but that's fine since all we're looking for is some dispersion.
79
//
80
// **IMPORTANT** Changing these numbers would be a breaking change requiring
81
// an amendment along with a way to distinguish token IDs that
82
// were generated with the old code.
83
return
taxon ^
toTaxon
(((384160001 * tokenSeq) + 2459));
84
}
85
86
inline
Taxon
87
getTaxon
(
UInt256
const
&
id
)
88
{
89
std::uint32_t
taxon = 0;
90
memcpy(&taxon,
id
.begin() + 24, 4);
91
taxon = boost::endian::big_to_native(taxon);
92
93
// The taxon cipher is just an XOR, so it is reversible by applying the
94
// XOR a second time.
95
return
cipheredTaxon
(
getSequence
(
id
),
toTaxon
(taxon));
96
}
97
98
inline
AccountID
99
getIssuer
(
UInt256
const
&
id
)
100
{
101
return
AccountID::fromVoid
(
id
.data() + 4);
102
}
103
104
}
// namespace xrpl::nft
xrpl::BaseUInt< 160, detail::AccountIDTag >::fromVoid
static BaseUInt fromVoid(void const *data)
Definition
base_uint.h:339
xrpl::TaggedInteger
A type-safe wrap around standard integral types.
Definition
tagged_integer.h:37
cstdint
cstring
std::uint32_t
xrpl::nft
Definition
NFTokenHelpers.h:25
xrpl::nft::toUInt32
std::uint32_t toUInt32(Taxon t)
Definition
nft.h:27
xrpl::nft::kFlagCreateTrustLines
constexpr std::uint16_t const kFlagCreateTrustLines
Definition
nft.h:34
xrpl::nft::getSequence
std::uint32_t getSequence(UInt256 const &id)
Definition
nft.h:55
xrpl::nft::kFlagTransferable
constexpr std::uint16_t const kFlagTransferable
Definition
nft.h:35
xrpl::nft::toTaxon
Taxon toTaxon(std::uint32_t i)
Definition
nft.h:21
xrpl::nft::cipheredTaxon
Taxon cipheredTaxon(std::uint32_t tokenSeq, Taxon taxon)
Definition
nft.h:63
xrpl::nft::kFlagOnlyXrp
constexpr std::uint16_t const kFlagOnlyXrp
Definition
nft.h:33
xrpl::nft::kFlagMutable
constexpr std::uint16_t const kFlagMutable
Definition
nft.h:36
xrpl::nft::getFlags
std::uint16_t getFlags(UInt256 const &id)
Definition
nft.h:39
xrpl::nft::getIssuer
AccountID getIssuer(UInt256 const &id)
Definition
nft.h:99
xrpl::nft::getTransferFee
std::uint16_t getTransferFee(UInt256 const &id)
Definition
nft.h:47
xrpl::nft::kFlagBurnable
constexpr std::uint16_t const kFlagBurnable
Definition
nft.h:32
xrpl::nft::getTaxon
Taxon getTaxon(UInt256 const &id)
Definition
nft.h:87
xrpl::nft::Taxon
TaggedInteger< std::uint32_t, TaxonTag > Taxon
Definition
nft.h:18
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::AccountID
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition
AccountID.h:34
xrpl::nft::TaxonTag
Definition
nft.h:16
Generated by
1.17.0