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 std::optional<std::vector<uint8_t>> describe_merkle_proof_v1(
146 const TxReceiptImpl& receipt);
147
148 using SerialisedCoseEndorsement = std::vector<uint8_t>;
149 using SerialisedCoseSignature = std::vector<uint8_t>;
150 using SerialisedCoseEndorsements = std::vector<SerialisedCoseEndorsement>;
151 std::optional<SerialisedCoseEndorsements> describe_cose_endorsements_v1(
152 const TxReceiptImpl& receipt);
153 std::optional<SerialisedCoseSignature> describe_cose_signature_v1(
154 const TxReceiptImpl& receipt);
155
156 // Manual JSON serializers are specified for these types as they are not
157 // trivial POD structs
158
159 void to_json(nlohmann::json& j, const ProofReceipt::Components& components);
160 void from_json(const nlohmann::json& j, ProofReceipt::Components& components);
161 std::string schema_name(
162 [[maybe_unused]] const ProofReceipt::Components* components);
163 void fill_json_schema(
164 nlohmann::json& schema,
165 [[maybe_unused]] const ProofReceipt::Components* components);
166
167 void to_json(nlohmann::json& j, const ProofReceipt::ProofStep& step);
168 void from_json(const nlohmann::json& j, ProofReceipt::ProofStep& step);
169 std::string schema_name([[maybe_unused]] const ProofReceipt::ProofStep* step);
170 void fill_json_schema(
171 nlohmann::json& schema,
172 [[maybe_unused]] const ProofReceipt::ProofStep* step);
173
174 void to_json(nlohmann::json& j, const ReceiptPtr& receipt);
175 void from_json(const nlohmann::json& j, ReceiptPtr& receipt);
176 std::string schema_name([[maybe_unused]] const ReceiptPtr* receipt);
177 void fill_json_schema(
178 nlohmann::json& schema, [[maybe_unused]] const ReceiptPtr* receipt);
179
180 template <typename T>
182 T& helper, nlohmann::json& schema, const ProofReceipt::Components* comp)
183 {
184 helper.template add_schema_component<
186 helper.template add_schema_component<
188
189 fill_json_schema(schema, comp);
190 }
191
192 template <typename T>
194 T& helper, nlohmann::json& schema, const ProofReceipt::ProofStep* ps)
195 {
196 helper
197 .template add_schema_component<decltype(ProofReceipt::ProofStep::hash)>();
198
199 fill_json_schema(schema, ps);
200 }
201
202 template <typename T>
204 T& helper, nlohmann::json& schema, const ReceiptPtr* r)
205 {
206 helper.template add_schema_component<decltype(Receipt::cert)>();
207 helper.template add_schema_component<decltype(Receipt::node_id)>();
208 helper
209 .template add_schema_component<decltype(Receipt::service_endorsements)>();
210 helper.template add_schema_component<decltype(Receipt::signature)>();
211
212 helper.template add_schema_component<decltype(ProofReceipt::proof)>();
213 helper
214 .template add_schema_component<decltype(ProofReceipt::leaf_components)>();
215 helper
216 .template add_schema_component<decltype(SignatureReceipt::signed_root)>();
217
218 fill_json_schema(schema, r);
219 }
220}
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:14
std::optional< SerialisedCoseEndorsements > describe_cose_endorsements_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:264
nlohmann::json describe_receipt_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:68
std::vector< SerialisedCoseEndorsement > SerialisedCoseEndorsements
Definition receipt.h:150
std::optional< std::vector< uint8_t > > describe_merkle_proof_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:213
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::optional< SerialisedCoseSignature > describe_cose_signature_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:270
void add_schema_components(T &helper, nlohmann::json &schema, const ProofReceipt::Components *comp)
Definition receipt.h:181
std::string schema_name(const ClaimsDigest *claims_digest_type)
Definition claims_digest.h:59
std::vector< uint8_t > SerialisedCoseEndorsement
Definition receipt.h:148
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
std::vector< uint8_t > SerialisedCoseSignature
Definition receipt.h:149
std::shared_ptr< TxReceiptImpl > TxReceiptImplPtr
Definition receipt.h:133
ReceiptPtr describe_receipt_v2(const TxReceiptImpl &in)
Definition historical_queries_adapter.cpp:143
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