xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
conditions
Fulfillment.cpp
1
#include <xrpl/conditions/Fulfillment.h>
2
3
#include <xrpl/basics/Slice.h>
4
#include <xrpl/basics/safe_cast.h>
5
#include <xrpl/conditions/Condition.h>
6
#include <xrpl/conditions/detail/PreimageSha256.h>
7
#include <xrpl/conditions/detail/error.h>
8
#include <xrpl/conditions/detail/utils.h>
9
10
#include <
memory
>
11
#include <
system_error
>
12
13
namespace
xrpl::cryptoconditions
{
14
15
bool
16
match
(
Fulfillment
const
& f,
Condition
const
& c)
17
{
18
// Fast check: the fulfillment's type must match the
19
// conditions's type:
20
if
(f.
type
() != c.
type
)
21
return
false
;
22
23
// Derive the condition from the given fulfillment
24
// and ensure that it matches the given condition.
25
return
c == f.
condition
();
26
}
27
28
bool
29
validate
(
Fulfillment
const
& f,
Condition
const
& c,
Slice
m)
30
{
31
return
match
(f, c) && f.
validate
(m);
32
}
33
34
bool
35
validate
(
Fulfillment
const
& f,
Condition
const
& c)
36
{
37
return
validate
(f, c, {});
38
}
39
40
std::unique_ptr<Fulfillment>
41
Fulfillment::deserialize
(
Slice
s,
std::error_code
& ec)
42
{
43
// Per the RFC, in a fulfillment we choose a type based
44
// on the tag of the item we contain:
45
//
46
// Fulfillment ::= CHOICE {
47
// preimageSha256 [0] PreimageFulfillment ,
48
// prefixSha256 [1] PrefixFulfillment,
49
// thresholdSha256 [2] ThresholdFulfillment,
50
// rsaSha256 [3] RsaSha256Fulfillment,
51
// ed25519Sha256 [4] Ed25519Sha512Fulfillment
52
// }
53
54
if
(s.
empty
())
55
{
56
ec =
Error::BufferEmpty
;
57
return
nullptr
;
58
}
59
60
using namespace
der
;
61
62
auto
const
p = parsePreamble(s, ec);
63
if
(ec)
64
return
nullptr
;
65
66
// All fulfillments are context-specific, constructed types
67
if
(!isConstructed(p) || !isContextSpecific(p))
68
{
69
ec =
Error::MalformedEncoding
;
70
return
nullptr
;
71
}
72
73
if
(p.length > s.
size
())
74
{
75
ec =
Error::BufferUnderfull
;
76
return
{};
77
}
78
79
if
(p.length < s.
size
())
80
{
81
ec =
Error::BufferOverfull
;
82
return
{};
83
}
84
85
if
(p.length >
kMaxSerializedFulfillment
)
86
{
87
ec =
Error::LargeSize
;
88
return
{};
89
}
90
91
std::unique_ptr<Fulfillment>
f;
92
93
using
TagType =
decltype
(p.tag);
94
switch
(p.tag)
95
{
96
case
safeCast<TagType>
(
Type::PreimageSha256
):
97
f =
PreimageSha256::deserialize
(
Slice
(s.
data
(), p.length), ec);
98
if
(ec)
99
return
{};
100
s += p.
length
;
101
break
;
102
103
case
safeCast<TagType>
(
Type::PrefixSha256
):
104
case
safeCast<TagType>
(
Type::ThresholdSha256
):
105
case
safeCast<TagType>
(
Type::RsaSha256
):
106
case
safeCast<TagType>
(
Type::Ed25519Sha256
):
107
ec =
Error::UnsupportedType
;
108
return
{};
109
110
default
:
111
ec =
Error::UnknownType
;
112
return
{};
113
}
114
115
if
(!s.
empty
())
116
{
117
ec =
Error::TrailingGarbage
;
118
return
{};
119
}
120
121
return
f;
122
}
123
124
}
// namespace xrpl::cryptoconditions
xrpl::Slice
An immutable linear range of bytes.
Definition
Slice.h:28
xrpl::Slice::length
std::size_t length() const noexcept
Definition
Slice.h:76
xrpl::Slice::empty
bool empty() const noexcept
Return true if the byte range is empty.
Definition
Slice.h:58
xrpl::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition
Slice.h:88
xrpl::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition
Slice.h:70
xrpl::cryptoconditions::Condition
Definition
Condition.h:25
xrpl::cryptoconditions::Condition::type
Type type
Definition
Condition.h:52
xrpl::cryptoconditions::PreimageSha256::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Parse the payload for a PreimageSha256 condition.
Definition
PreimageSha256.h:41
std::error_code
memory
xrpl::cryptoconditions::der
Definition
include/xrpl/conditions/detail/utils.h:19
xrpl::cryptoconditions
Definition
Condition.h:14
xrpl::cryptoconditions::match
bool match(Fulfillment const &f, Condition const &c)
Determine whether the given fulfillment and condition match.
Definition
Fulfillment.cpp:16
xrpl::cryptoconditions::validate
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Definition
Fulfillment.cpp:29
xrpl::cryptoconditions::Type::PrefixSha256
@ PrefixSha256
Definition
Condition.h:18
xrpl::cryptoconditions::Type::ThresholdSha256
@ ThresholdSha256
Definition
Condition.h:19
xrpl::cryptoconditions::Type::Ed25519Sha256
@ Ed25519Sha256
Definition
Condition.h:21
xrpl::cryptoconditions::Type::PreimageSha256
@ PreimageSha256
Definition
Condition.h:17
xrpl::cryptoconditions::Type::RsaSha256
@ RsaSha256
Definition
Condition.h:20
xrpl::cryptoconditions::Error::BufferOverfull
@ BufferOverfull
Definition
error.h:17
xrpl::cryptoconditions::Error::BufferUnderfull
@ BufferUnderfull
Definition
error.h:18
xrpl::cryptoconditions::Error::LargeSize
@ LargeSize
Definition
error.h:23
xrpl::cryptoconditions::Error::UnsupportedType
@ UnsupportedType
Definition
error.h:9
xrpl::cryptoconditions::Error::MalformedEncoding
@ MalformedEncoding
Definition
error.h:19
xrpl::cryptoconditions::Error::UnknownType
@ UnknownType
Definition
error.h:11
xrpl::cryptoconditions::Error::BufferEmpty
@ BufferEmpty
Definition
error.h:16
xrpl::cryptoconditions::Error::TrailingGarbage
@ TrailingGarbage
Definition
error.h:15
xrpl::safeCast
constexpr Dest safeCast(Src s) noexcept
Definition
safe_cast.h:21
xrpl::cryptoconditions::Fulfillment
Definition
Fulfillment.h:15
xrpl::cryptoconditions::Fulfillment::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
Definition
Fulfillment.cpp:41
xrpl::cryptoconditions::Fulfillment::condition
virtual Condition condition() const =0
Returns the condition associated with the given fulfillment.
xrpl::cryptoconditions::Fulfillment::type
virtual Type type() const =0
Returns the type of this condition.
xrpl::cryptoconditions::Fulfillment::validate
virtual bool validate(Slice data) const =0
Validates a fulfillment.
xrpl::cryptoconditions::Fulfillment::kMaxSerializedFulfillment
static constexpr std::size_t kMaxSerializedFulfillment
The largest binary fulfillment we support.
Definition
Fulfillment.h:25
system_error
std::unique_ptr
Generated by
1.17.0