xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
conditions
detail
PreimageSha256.h
1
#pragma once
2
3
#include <xrpl/basics/Buffer.h>
4
#include <xrpl/basics/Slice.h>
5
#include <xrpl/conditions/Condition.h>
6
#include <xrpl/conditions/Fulfillment.h>
7
#include <xrpl/conditions/detail/error.h>
8
#include <xrpl/protocol/digest.h>
9
10
#include <
cstddef
>
11
#include <
cstdint
>
12
#include <
memory
>
13
#include <
system_error
>
14
#include <
utility
>
15
16
namespace
xrpl::cryptoconditions
{
17
18
class
PreimageSha256
final :
public
Fulfillment
19
{
20
public
:
31
static
constexpr
std::size_t
kMaxPreimageLength
= 128;
32
40
static
std::unique_ptr<Fulfillment>
41
deserialize
(
Slice
s,
std::error_code
& ec)
42
{
43
// Per the RFC, a preimage fulfillment is defined as
44
// follows:
45
//
46
// PreimageFulfillment ::= SEQUENCE {
47
// preimage OCTET STRING
48
// }
49
50
using namespace
der
;
51
52
auto
p = parsePreamble(s, ec);
53
if
(ec)
54
return
nullptr
;
55
56
if
(!isPrimitive(p) || !isContextSpecific(p))
57
{
58
ec =
Error::IncorrectEncoding
;
59
return
{};
60
}
61
62
if
(p.tag != 0)
63
{
64
ec =
Error::UnexpectedTag
;
65
return
{};
66
}
67
68
if
(s.
size
() != p.length)
69
{
70
ec =
Error::TrailingGarbage
;
71
return
{};
72
}
73
74
if
(s.
size
() >
kMaxPreimageLength
)
75
{
76
ec =
Error::PreimageTooLong
;
77
return
{};
78
}
79
80
auto
b = parseOctetString(s, p.
length
, ec);
81
if
(ec)
82
return
{};
83
84
return
std::make_unique<PreimageSha256>
(std::move(b));
85
}
86
87
private
:
88
Buffer
payload_
;
89
90
public
:
91
PreimageSha256
(
Buffer
&& b) noexcept :
payload_
(std::move(b))
92
{
93
}
94
95
PreimageSha256
(
Slice
s) noexcept :
payload_
(s)
96
{
97
}
98
99
[[nodiscard]]
Type
100
type
()
const override
101
{
102
return
Type::PreimageSha256
;
103
}
104
105
[[nodiscard]]
Buffer
106
fingerprint
()
const override
107
{
108
Sha256Hasher
h;
109
h(
payload_
.data(),
payload_
.size());
110
auto
const
d =
static_cast<
Sha256Hasher::result_type
>
(h);
111
return
{d.data(), d.size()};
112
}
113
114
[[nodiscard]]
std::uint32_t
115
cost
()
const override
116
{
117
return
static_cast<
std::uint32_t
>
(
payload_
.size());
118
}
119
120
[[nodiscard]]
Condition
121
condition
()
const override
122
{
123
return
{
type
(),
cost
(),
fingerprint
()};
124
}
125
126
[[nodiscard]]
bool
127
validate
(
Slice
)
const override
128
{
129
// Perhaps counterintuitively, the message isn't
130
// relevant.
131
return
true
;
132
}
133
};
134
135
}
// namespace xrpl::cryptoconditions
xrpl::Buffer
Like std::vector<char> but better.
Definition
Buffer.h:19
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::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::PreimageSha256::payload_
Buffer payload_
Definition
PreimageSha256.h:88
xrpl::cryptoconditions::PreimageSha256::validate
bool validate(Slice) const override
Validates a fulfillment.
Definition
PreimageSha256.h:127
xrpl::cryptoconditions::PreimageSha256::condition
Condition condition() const override
Returns the condition associated with the given fulfillment.
Definition
PreimageSha256.h:121
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
xrpl::cryptoconditions::PreimageSha256::cost
std::uint32_t cost() const override
Calculates the cost associated with this fulfillment.
Definition
PreimageSha256.h:115
xrpl::cryptoconditions::PreimageSha256::kMaxPreimageLength
static constexpr std::size_t kMaxPreimageLength
The maximum allowed length of a preimage.
Definition
PreimageSha256.h:31
xrpl::cryptoconditions::PreimageSha256::fingerprint
Buffer fingerprint() const override
Returns the fulfillment's fingerprint:
Definition
PreimageSha256.h:106
xrpl::cryptoconditions::PreimageSha256::type
Type type() const override
Returns the type of this condition.
Definition
PreimageSha256.h:100
xrpl::cryptoconditions::PreimageSha256::PreimageSha256
PreimageSha256(Slice s) noexcept
Definition
PreimageSha256.h:95
xrpl::cryptoconditions::PreimageSha256::PreimageSha256
PreimageSha256(Buffer &&b) noexcept
Definition
PreimageSha256.h:91
cstddef
cstdint
std::error_code
std::uint32_t
std::make_unique
T make_unique(T... args)
memory
xrpl::cryptoconditions::der
Definition
include/xrpl/conditions/detail/utils.h:19
xrpl::cryptoconditions
Definition
Condition.h:14
xrpl::cryptoconditions::Type
Type
Definition
Condition.h:16
xrpl::cryptoconditions::Type::PreimageSha256
@ PreimageSha256
Definition
Condition.h:17
xrpl::cryptoconditions::Error::UnexpectedTag
@ UnexpectedTag
Definition
error.h:21
xrpl::cryptoconditions::Error::IncorrectEncoding
@ IncorrectEncoding
Definition
error.h:14
xrpl::cryptoconditions::Error::PreimageTooLong
@ PreimageTooLong
Definition
error.h:24
xrpl::cryptoconditions::Error::TrailingGarbage
@ TrailingGarbage
Definition
error.h:15
xrpl::Sha256Hasher
OpensslSha256Hasher Sha256Hasher
Definition
digest.h:102
std::size_t
xrpl::OpensslSha256Hasher::result_type
std::array< std::uint8_t, 32 > result_type
Definition
digest.h:85
xrpl::cryptoconditions::Fulfillment
Definition
Fulfillment.h:15
system_error
std::unique_ptr
utility
Generated by
1.17.0