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 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
54 {
56 Right
58
60
61 bool operator==(const ProofStep& other) const
62 {
63 return direction == other.direction && hash == other.hash;
64 }
65 };
66 using Proof = std::vector<ProofStep>;
67
68 // A merkle-tree path from the leaf digest to the signed root
70
72 {
73 auto current = get_leaf_digest();
74
75 for (const auto& element : proof)
76 {
77 if (element.direction == ProofStep::Left)
78 {
79 current = ccf::crypto::Sha256Hash(element.hash, current);
80 }
81 else
82 {
83 current = ccf::crypto::Sha256Hash(current, element.hash);
84 }
85 }
86
87 return current;
88 }
89
106
107 bool is_signature_transaction() const override
108 {
109 return false;
110 }
111 };
112
113 // Signature transactions are special, as they contain no proof. They contain
114 // a single root, which is directly signed.
116 {
117 public:
119
121 {
122 return signed_root;
123 };
124
125 bool is_signature_transaction() const override
126 {
127 return true;
128 }
129 };
130
131 using ReceiptPtr = std::shared_ptr<Receipt>;
132
133 // This is an opaque, incomplete type, but can be summarised to a JSON object
134 // by describe_receipt_v1, or a ccf::ReceiptPtr by describe_receipt_v2
135 struct TxReceiptImpl;
136 using TxReceiptImplPtr = std::shared_ptr<TxReceiptImpl>;
137 nlohmann::json describe_receipt_v1(const TxReceiptImpl& receipt);
139
140 enum MerkleProofLabel : int64_t
141 {
142 // Values set in
143 // https://github.com/ietf-scitt/draft-birkholz-cose-cometre-ccf-profile
146 };
147 std::optional<std::vector<uint8_t>> describe_merkle_proof_v1(
148 const TxReceiptImpl& receipt);
149
150 using SerialisedCoseEndorsement = std::vector<uint8_t>;
151 using SerialisedCoseSignature = std::vector<uint8_t>;
152 using SerialisedCoseEndorsements = std::vector<SerialisedCoseEndorsement>;
153 std::optional<SerialisedCoseEndorsements> describe_cose_endorsements_v1(
154 const TxReceiptImpl& receipt);
155 std::optional<SerialisedCoseSignature> describe_cose_signature_v1(
156 const TxReceiptImpl& receipt);
157
158 // Manual JSON serializers are specified for these types as they are not
159 // trivial POD structs
160
161 void to_json(nlohmann::json& j, const ProofReceipt::Components& components);
162 void from_json(const nlohmann::json& j, ProofReceipt::Components& components);
163 std::string schema_name(const ProofReceipt::Components*);
164 void fill_json_schema(
165 nlohmann::json& schema, const ProofReceipt::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(const ProofReceipt::ProofStep*);
170 void fill_json_schema(nlohmann::json& schema, const ProofReceipt::ProofStep*);
171
172 void to_json(nlohmann::json& j, const ReceiptPtr& receipt);
173 void from_json(const nlohmann::json& j, ReceiptPtr& receipt);
174 std::string schema_name(const ReceiptPtr*);
175 void fill_json_schema(nlohmann::json& schema, const ReceiptPtr*);
176
177 template <typename T>
179 T& helper, nlohmann::json& schema, const ProofReceipt::Components* comp)
180 {
181 helper.template add_schema_component<
183 helper.template add_schema_component<
185
186 fill_json_schema(schema, comp);
187 }
188
189 template <typename T>
191 T& helper, nlohmann::json& schema, const ProofReceipt::ProofStep* ps)
192 {
193 helper
194 .template add_schema_component<decltype(ProofReceipt::ProofStep::hash)>();
195
196 fill_json_schema(schema, ps);
197 }
198
199 template <typename T>
201 T& helper, nlohmann::json& schema, const ReceiptPtr* r)
202 {
203 helper.template add_schema_component<decltype(Receipt::cert)>();
204 helper.template add_schema_component<decltype(Receipt::node_id)>();
205 helper
206 .template add_schema_component<decltype(Receipt::service_endorsements)>();
207 helper.template add_schema_component<decltype(Receipt::signature)>();
208
209 helper.template add_schema_component<decltype(ProofReceipt::proof)>();
210 helper
211 .template add_schema_component<decltype(ProofReceipt::leaf_components)>();
212 helper
213 .template add_schema_component<decltype(SignatureReceipt::signed_root)>();
214
215 fill_json_schema(schema, r);
216 }
217}
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:71
Proof proof
Definition receipt.h:69
ccf::crypto::Sha256Hash get_leaf_digest()
Definition receipt.h:90
bool is_signature_transaction() const override
Definition receipt.h:107
std::vector< ProofStep > Proof
Definition receipt.h:66
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:116
bool is_signature_transaction() const override
Definition receipt.h:125
ccf::crypto::Sha256Hash signed_root
Definition receipt.h:118
ccf::crypto::Sha256Hash calculate_root() override
Definition receipt.h:120
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:152
std::optional< std::vector< uint8_t > > describe_merkle_proof_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:213
MerkleProofLabel
Definition receipt.h:141
@ MERKLE_PROOF_LEAF_LABEL
Definition receipt.h:144
@ MERKLE_PROOF_PATH_LABEL
Definition receipt.h:145
std::shared_ptr< Receipt > ReceiptPtr
Definition receipt.h:131
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *)
Definition claims_digest.h:64
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:178
std::string schema_name(const ClaimsDigest *)
Definition claims_digest.h:59
std::vector< uint8_t > SerialisedCoseEndorsement
Definition receipt.h:150
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
std::vector< uint8_t > SerialisedCoseSignature
Definition receipt.h:151
std::shared_ptr< TxReceiptImpl > TxReceiptImplPtr
Definition receipt.h:136
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
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
ccf::crypto::Sha256Hash hash
Definition receipt.h:59
bool operator==(const ProofStep &other) const
Definition receipt.h:61
@ Right
Definition receipt.h:56
@ Left
Definition receipt.h:55
enum ccf::ProofReceipt::ProofStep::@1 direction
Definition tx_receipt_impl.h:13