rippled
Loading...
Searching...
No Matches
PreimageSha256.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2016 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_CONDITIONS_PREIMAGE_SHA256_H
21#define RIPPLE_CONDITIONS_PREIMAGE_SHA256_H
22
23#include <xrpld/conditions/Condition.h>
24#include <xrpld/conditions/Fulfillment.h>
25#include <xrpld/conditions/detail/error.h>
26
27#include <xrpl/basics/Buffer.h>
28#include <xrpl/basics/Slice.h>
29#include <xrpl/protocol/digest.h>
30
31#include <memory>
32
33namespace ripple {
34namespace cryptoconditions {
35
36class PreimageSha256 final : public Fulfillment
37{
38public:
48 static constexpr std::size_t maxPreimageLength = 128;
49
58 {
59 // Per the RFC, a preimage fulfulliment is defined as
60 // follows:
61 //
62 // PreimageFulfillment ::= SEQUENCE {
63 // preimage OCTET STRING
64 // }
65
66 using namespace der;
67
68 auto p = parsePreamble(s, ec);
69 if (ec)
70 return nullptr;
71
72 if (!isPrimitive(p) || !isContextSpecific(p))
73 {
75 return {};
76 }
77
78 if (p.tag != 0)
79 {
81 return {};
82 }
83
84 if (s.size() != p.length)
85 {
87 return {};
88 }
89
90 if (s.size() > maxPreimageLength)
91 {
93 return {};
94 }
95
96 auto b = parseOctetString(s, p.length, ec);
97 if (ec)
98 return {};
99
100 return std::make_unique<PreimageSha256>(std::move(b));
101 }
102
103private:
105
106public:
107 PreimageSha256(Buffer&& b) noexcept : payload_(std::move(b))
108 {
109 }
110
112 {
113 }
114
115 Type
116 type() const override
117 {
119 }
120
121 Buffer
122 fingerprint() const override
123 {
125 h(payload_.data(), payload_.size());
126 auto const d = static_cast<sha256_hasher::result_type>(h);
127 return {d.data(), d.size()};
128 }
129
131 cost() const override
132 {
133 return static_cast<std::uint32_t>(payload_.size());
134 }
135
137 condition() const override
138 {
139 return {type(), cost(), fingerprint()};
140 }
141
142 bool
143 validate(Slice) const override
144 {
145 // Perhaps counterintuitively, the message isn't
146 // relevant.
147 return true;
148 }
149};
150
151} // namespace cryptoconditions
152} // namespace ripple
153
154#endif
Like std::vector<char> but better.
Definition Buffer.h:36
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:127
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:151
An immutable linear range of bytes.
Definition Slice.h:46
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:81
std::size_t length() const noexcept
Definition Slice.h:87
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:25