rippled
Loading...
Searching...
No Matches
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 <memory>
11
12namespace xrpl {
13namespace cryptoconditions {
14
15class PreimageSha256 final : public Fulfillment
16{
17public:
27 static constexpr std::size_t maxPreimageLength = 128;
28
37 {
38 // Per the RFC, a preimage fulfillment is defined as
39 // follows:
40 //
41 // PreimageFulfillment ::= SEQUENCE {
42 // preimage OCTET STRING
43 // }
44
45 using namespace der;
46
47 auto p = parsePreamble(s, ec);
48 if (ec)
49 return nullptr;
50
51 if (!isPrimitive(p) || !isContextSpecific(p))
52 {
54 return {};
55 }
56
57 if (p.tag != 0)
58 {
60 return {};
61 }
62
63 if (s.size() != p.length)
64 {
66 return {};
67 }
68
69 if (s.size() > maxPreimageLength)
70 {
72 return {};
73 }
74
75 auto b = parseOctetString(s, p.length, ec);
76 if (ec)
77 return {};
78
79 return std::make_unique<PreimageSha256>(std::move(b));
80 }
81
82private:
84
85public:
86 PreimageSha256(Buffer&& b) noexcept : payload_(std::move(b))
87 {
88 }
89
90 PreimageSha256(Slice s) noexcept : payload_(s)
91 {
92 }
93
94 Type
95 type() const override
96 {
98 }
99
100 Buffer
101 fingerprint() const override
102 {
104 h(payload_.data(), payload_.size());
105 auto const d = static_cast<sha256_hasher::result_type>(h);
106 return {d.data(), d.size()};
107 }
108
110 cost() const override
111 {
112 return static_cast<std::uint32_t>(payload_.size());
113 }
114
116 condition() const override
117 {
118 return {type(), cost(), fingerprint()};
119 }
120
121 bool
122 validate(Slice) const override
123 {
124 // Perhaps counterintuitively, the message isn't
125 // relevant.
126 return true;
127 }
128};
129
130} // namespace cryptoconditions
131} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:16
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:104
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:128
An immutable linear range of bytes.
Definition Slice.h:26
std::size_t length() const noexcept
Definition Slice.h:66
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:60
static constexpr std::size_t maxPreimageLength
The maximum allowed length of a preimage.
bool validate(Slice) const override
Validates a fulfillment.
Condition condition() const override
Returns the condition associated with the given fulfillment.
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.
Buffer fingerprint() const override
Returns the fulfillment's fingerprint:
Type type() const override
Returns the type of this condition.
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:5
SHA-256 digest.
Definition digest.h:74