rippled
Loading...
Searching...
No Matches
PreimageSha256.h
1#ifndef XRPL_CONDITIONS_PREIMAGE_SHA256_H
2#define XRPL_CONDITIONS_PREIMAGE_SHA256_H
3
4#include <xrpld/conditions/Condition.h>
5#include <xrpld/conditions/Fulfillment.h>
6#include <xrpld/conditions/detail/error.h>
7
8#include <xrpl/basics/Buffer.h>
9#include <xrpl/basics/Slice.h>
10#include <xrpl/protocol/digest.h>
11
12#include <memory>
13
14namespace ripple {
15namespace cryptoconditions {
16
17class PreimageSha256 final : public Fulfillment
18{
19public:
29 static constexpr std::size_t maxPreimageLength = 128;
30
39 {
40 // Per the RFC, a preimage fulfulliment is defined as
41 // follows:
42 //
43 // PreimageFulfillment ::= SEQUENCE {
44 // preimage OCTET STRING
45 // }
46
47 using namespace der;
48
49 auto p = parsePreamble(s, ec);
50 if (ec)
51 return nullptr;
52
53 if (!isPrimitive(p) || !isContextSpecific(p))
54 {
56 return {};
57 }
58
59 if (p.tag != 0)
60 {
62 return {};
63 }
64
65 if (s.size() != p.length)
66 {
68 return {};
69 }
70
71 if (s.size() > maxPreimageLength)
72 {
74 return {};
75 }
76
77 auto b = parseOctetString(s, p.length, ec);
78 if (ec)
79 return {};
80
81 return std::make_unique<PreimageSha256>(std::move(b));
82 }
83
84private:
86
87public:
88 PreimageSha256(Buffer&& b) noexcept : payload_(std::move(b))
89 {
90 }
91
92 PreimageSha256(Slice s) noexcept : payload_(s)
93 {
94 }
95
96 Type
97 type() const override
98 {
100 }
101
102 Buffer
103 fingerprint() const override
104 {
106 h(payload_.data(), payload_.size());
107 auto const d = static_cast<sha256_hasher::result_type>(h);
108 return {d.data(), d.size()};
109 }
110
112 cost() const override
113 {
114 return static_cast<std::uint32_t>(payload_.size());
115 }
116
118 condition() const override
119 {
120 return {type(), cost(), fingerprint()};
121 }
122
123 bool
124 validate(Slice) const override
125 {
126 // Perhaps counterintuitively, the message isn't
127 // relevant.
128 return true;
129 }
130};
131
132} // namespace cryptoconditions
133} // namespace ripple
134
135#endif
Like std::vector<char> but better.
Definition Buffer.h:17
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:108
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:132
An immutable linear range of bytes.
Definition Slice.h:27
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:62
std::size_t length() const noexcept
Definition Slice.h:68
bool validate(Slice) const override
Validates a fulfillment.
Buffer fingerprint() const override
Returns the fulfillment's fingerprint:
static constexpr std::size_t maxPreimageLength
The maximum allowed length of a preimage.
Condition condition() const override
Returns the condition associated with the given fulfillment.
Type type() const override
Returns the type of this condition.
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Parse the payload for a PreimageSha256 condition.
std::uint32_t cost() const override
Calculates the cost associated with this fulfillment.
T data(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6