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