xrpld
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 <cstddef>
11#include <cstdint>
12#include <memory>
13#include <system_error>
14#include <utility>
15
16namespace xrpl::cryptoconditions {
17
18class PreimageSha256 final : public Fulfillment
19{
20public:
31 static constexpr std::size_t kMaxPreimageLength = 128;
32
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 {
59 return {};
60 }
61
62 if (p.tag != 0)
63 {
65 return {};
66 }
67
68 if (s.size() != p.length)
69 {
71 return {};
72 }
73
74 if (s.size() > kMaxPreimageLength)
75 {
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
87private:
89
90public:
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 {
103 }
104
105 [[nodiscard]] Buffer
106 fingerprint() const override
107 {
109 h(payload_.data(), payload_.size());
110 auto const d = static_cast<sha256_hasher::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
Like std::vector<char> but better.
Definition Buffer.h:19
An immutable linear range of bytes.
Definition Slice.h:28
std::size_t length() const noexcept
Definition Slice.h:76
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:70
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.
static constexpr std::size_t kMaxPreimageLength
The maximum allowed length of a preimage.
Buffer fingerprint() const override
Returns the fulfillment's fingerprint:
Type type() const override
Returns the type of this condition.
T make_unique(T... args)
OpensslSha256Hasher sha256_hasher
Definition digest.h:102
std::array< std::uint8_t, 32 > result_type
Definition digest.h:85