CCF
Loading...
Searching...
No Matches
receipt.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3
4#pragma once
5
6#include "ccf/claims_digest.h"
7#include "ccf/crypto/pem.h"
9#include "ccf/ds/json.h"
10#include "ccf/ds/openapi.h"
11#include "ccf/entity_id.h"
12
13#include <optional>
14#include <string>
15
16namespace ccf
17{
18 class Receipt
19 {
20 public:
21 virtual ~Receipt() = default;
22
23 // Signature over the root digest, signed by the identity described in cert
24 std::vector<uint8_t> signature;
26
29
30 std::vector<ccf::crypto::Pem> service_endorsements;
31
32 [[nodiscard]] virtual bool is_signature_transaction() const = 0;
33 };
34
35 // Most transactions produce a receipt constructed from a combination of 3
36 // digests. Note that transactions emitted by old code versions may not
37 // include a claims_digest or a commit_evidence_digest, but from 2.0 onwards
38 // every transaction will contain a (potentially default-zero'd) claims digest
39 // and a commit evidence digest.
40 class ProofReceipt : public Receipt
41 {
42 public:
50
51 struct ProofStep
52 {
53 enum class Direction : uint8_t
54 {
55 Left,
56 Right
57 };
59
61
62 bool operator==(const ProofStep& other) const
63 {
64 return direction == other.direction && hash == other.hash;
65 }
66 };
67 using Proof = std::vector<ProofStep>;
68
69 // A merkle-tree path from the leaf digest to the signed root
71
73 {
74 auto current = get_leaf_digest();
75
76 for (const auto& element : proof)
77 {
78 if (element.direction == ProofStep::Direction::Left)
79 {
80 current = ccf::crypto::Sha256Hash(element.hash, current);
81 }
82 else
83 {
84 current = ccf::crypto::Sha256Hash(current, element.hash);
85 }
86 }
87
88 return current;
89 }
90
103
104 [[nodiscard]] bool is_signature_transaction() const override
105 {
106 return false;
107 }
108 };
109
110 // Signature transactions are special, as they contain no proof. They contain
111 // a single root, which is directly signed.
113 {
114 public:
116
118 {
119 return signed_root;
120 };
121
122 [[nodiscard]] bool is_signature_transaction() const override
123 {
124 return true;
125 }
126 };
127
128 using ReceiptPtr = std::shared_ptr<Receipt>;
129
130 // This is an opaque, incomplete type, but can be summarised to a JSON object
131 // by describe_receipt_v1, or a ccf::ReceiptPtr by describe_receipt_v2
132 struct TxReceiptImpl;
133 using TxReceiptImplPtr = std::shared_ptr<TxReceiptImpl>;
134 nlohmann::json describe_receipt_v1(const TxReceiptImpl& receipt);
136
137 // NOLINTNEXTLINE(performance-enum-size)
138 enum MerkleProofLabel : int64_t
139 {
140 // Values set in
141 // https://github.com/ietf-scitt/draft-birkholz-cose-cometre-ccf-profile
144 };
145 // NOLINTNEXTLINE(performance-enum-size)
147 {
148 // Values set in
149 // https://github.com/ietf-scitt/draft-birkholz-cose-cometre-ccf-profile
150 LEFT = 0,
151 RIGHT = 1
152 };
153 std::optional<std::vector<uint8_t>> describe_merkle_proof_v1(
154 const TxReceiptImpl& receipt);
155
156 using SerialisedCoseEndorsement = std::vector<uint8_t>;
157 using SerialisedCoseSignature = std::vector<uint8_t>;
158 using SerialisedCoseEndorsements = std::vector<SerialisedCoseEndorsement>;
159 using SerialisedCoseReceipt = std::vector<uint8_t>;
160 std::optional<SerialisedCoseEndorsements> describe_cose_endorsements_v1(
161 const TxReceiptImpl& receipt);
162 std::optional<SerialisedCoseSignature> describe_cose_signature_v1(
163 const TxReceiptImpl& receipt);
164 std::optional<SerialisedCoseReceipt> describe_cose_receipt_v1(
165 const TxReceiptImpl& receipt);
166
167 // Manual JSON serializers are specified for these types as they are not
168 // trivial POD structs
169
170 void to_json(nlohmann::json& j, const ProofReceipt::Components& components);
171 void from_json(const nlohmann::json& j, ProofReceipt::Components& components);
172 std::string schema_name(
173 [[maybe_unused]] const ProofReceipt::Components* components);
174 void fill_json_schema(
175 nlohmann::json& schema,
176 [[maybe_unused]] const ProofReceipt::Components* components);
177
178 void to_json(nlohmann::json& j, const ProofReceipt::ProofStep& step);
179 void from_json(const nlohmann::json& j, ProofReceipt::ProofStep& step);
180 std::string schema_name([[maybe_unused]] const ProofReceipt::ProofStep* step);
181 void fill_json_schema(
182 nlohmann::json& schema,
183 [[maybe_unused]] const ProofReceipt::ProofStep* step);
184
185 void to_json(nlohmann::json& j, const ReceiptPtr& receipt);
186 void from_json(const nlohmann::json& j, ReceiptPtr& receipt);
187 std::string schema_name([[maybe_unused]] const ReceiptPtr* receipt);
188 void fill_json_schema(
189 nlohmann::json& schema, [[maybe_unused]] const ReceiptPtr* receipt);
190
191 template <typename T>
193 T& helper, nlohmann::json& schema, const ProofReceipt::Components* comp)
194 {
195 helper.template add_schema_component<
197 helper.template add_schema_component<
199
200 fill_json_schema(schema, comp);
201 }
202
203 template <typename T>
205 T& helper, nlohmann::json& schema, const ProofReceipt::ProofStep* ps)
206 {
207 helper
208 .template add_schema_component<decltype(ProofReceipt::ProofStep::hash)>();
209
210 fill_json_schema(schema, ps);
211 }
212
213 template <typename T>
215 T& helper, nlohmann::json& schema, const ReceiptPtr* r)
216 {
217 helper.template add_schema_component<decltype(Receipt::cert)>();
218 helper.template add_schema_component<decltype(Receipt::node_id)>();
219 helper
220 .template add_schema_component<decltype(Receipt::service_endorsements)>();
221 helper.template add_schema_component<decltype(Receipt::signature)>();
222
223 helper.template add_schema_component<decltype(ProofReceipt::proof)>();
224 helper
225 .template add_schema_component<decltype(ProofReceipt::leaf_components)>();
226 helper
227 .template add_schema_component<decltype(SignatureReceipt::signed_root)>();
228
229 fill_json_schema(schema, r);
230 }
231}
Definition claims_digest.h:10
const Digest & value() const
Definition claims_digest.h:38
bool empty() const
Definition claims_digest.h:33
Definition receipt.h:41
Components leaf_components
Definition receipt.h:49
ccf::crypto::Sha256Hash calculate_root() override
Definition receipt.h:72
Proof proof
Definition receipt.h:70
ccf::crypto::Sha256Hash get_leaf_digest() const
Definition receipt.h:91
bool is_signature_transaction() const override
Definition receipt.h:104
std::vector< ProofStep > Proof
Definition receipt.h:67
Definition receipt.h:19
std::vector< ccf::crypto::Pem > service_endorsements
Definition receipt.h:30
ccf::crypto::Pem cert
Definition receipt.h:28
ccf::NodeId node_id
Definition receipt.h:27
std::vector< uint8_t > signature
Definition receipt.h:24
virtual bool is_signature_transaction() const =0
virtual ccf::crypto::Sha256Hash calculate_root()=0
virtual ~Receipt()=default
Definition receipt.h:113
bool is_signature_transaction() const override
Definition receipt.h:122
ccf::crypto::Sha256Hash signed_root
Definition receipt.h:115
ccf::crypto::Sha256Hash calculate_root() override
Definition receipt.h:117
Definition pem.h:18
Definition sha256_hash.h:16
Definition app_interface.h:13
MerkleProofPathBranch
Definition receipt.h:147
@ RIGHT
Definition receipt.h:151
@ LEFT
Definition receipt.h:150
std::optional< SerialisedCoseEndorsements > describe_cose_endorsements_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:262
nlohmann::json describe_receipt_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:66
std::vector< SerialisedCoseEndorsement > SerialisedCoseEndorsements
Definition receipt.h:158
std::optional< std::vector< uint8_t > > describe_merkle_proof_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:226
MerkleProofLabel
Definition receipt.h:139
@ MERKLE_PROOF_LEAF_LABEL
Definition receipt.h:142
@ MERKLE_PROOF_PATH_LABEL
Definition receipt.h:143
std::shared_ptr< Receipt > ReceiptPtr
Definition receipt.h:128
std::vector< uint8_t > SerialisedCoseReceipt
Definition receipt.h:159
std::optional< SerialisedCoseSignature > describe_cose_signature_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:268
void add_schema_components(T &helper, nlohmann::json &schema, const ProofReceipt::Components *comp)
Definition receipt.h:192
std::string schema_name(const ClaimsDigest *claims_digest_type)
Definition claims_digest.h:59
std::vector< uint8_t > SerialisedCoseEndorsement
Definition receipt.h:156
std::optional< SerialisedCoseReceipt > describe_cose_receipt_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:274
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
std::vector< uint8_t > SerialisedCoseSignature
Definition receipt.h:157
std::shared_ptr< TxReceiptImpl > TxReceiptImplPtr
Definition receipt.h:133
ReceiptPtr describe_receipt_v2(const TxReceiptImpl &in)
Definition historical_queries_adapter.cpp:150
void to_json(nlohmann::json &j, const ClaimsDigest &hash)
Definition claims_digest.h:49
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *claims_digest_type)
Definition claims_digest.h:65
Definition receipt.h:44
ccf::crypto::Sha256Hash write_set_digest
Definition receipt.h:45
ccf::ClaimsDigest claims_digest
Definition receipt.h:47
std::string commit_evidence
Definition receipt.h:46
Definition receipt.h:52
Direction
Definition receipt.h:54
ccf::crypto::Sha256Hash hash
Definition receipt.h:60
bool operator==(const ProofStep &other) const
Definition receipt.h:62
Direction direction
Definition receipt.h:58
Definition tx_receipt_impl.h:14