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 // Manual JSON serializers are specified for these types as they are not
141 // trivial POD structs
142
143 void to_json(nlohmann::json& j, const ProofReceipt::Components& step);
144 void from_json(const nlohmann::json& j, ProofReceipt::Components& step);
145 std::string schema_name(const ProofReceipt::Components*);
146 void fill_json_schema(
147 nlohmann::json& schema, const ProofReceipt::Components*);
148
149 void to_json(nlohmann::json& j, const ProofReceipt::ProofStep& step);
150 void from_json(const nlohmann::json& j, ProofReceipt::ProofStep& step);
151 std::string schema_name(const ProofReceipt::ProofStep*);
152 void fill_json_schema(nlohmann::json& schema, const ProofReceipt::ProofStep*);
153
154 void to_json(nlohmann::json& j, const ReceiptPtr& receipt);
155 void from_json(const nlohmann::json& j, ReceiptPtr& receipt);
156 std::string schema_name(const ReceiptPtr*);
157 void fill_json_schema(nlohmann::json& schema, const ReceiptPtr*);
158
159 template <typename T>
161 T& helper, nlohmann::json& schema, const ProofReceipt::Components* comp)
162 {
163 helper.template add_schema_component<decltype(
165 helper.template add_schema_component<decltype(
167
168 fill_json_schema(schema, comp);
169 }
170
171 template <typename T>
173 T& helper, nlohmann::json& schema, const ProofReceipt::ProofStep* ps)
174 {
175 helper
176 .template add_schema_component<decltype(ProofReceipt::ProofStep::hash)>();
177
178 fill_json_schema(schema, ps);
179 }
180
181 template <typename T>
183 T& helper, nlohmann::json& schema, const ReceiptPtr* r)
184 {
185 helper.template add_schema_component<decltype(Receipt::cert)>();
186 helper.template add_schema_component<decltype(Receipt::node_id)>();
187 helper
188 .template add_schema_component<decltype(Receipt::service_endorsements)>();
189 helper.template add_schema_component<decltype(Receipt::signature)>();
190
191 helper.template add_schema_component<decltype(ProofReceipt::proof)>();
192 helper
193 .template add_schema_component<decltype(ProofReceipt::leaf_components)>();
194 helper
195 .template add_schema_component<decltype(SignatureReceipt::signed_root)>();
196
197 fill_json_schema(schema, r);
198 }
199}
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:15
nlohmann::json describe_receipt_v1(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:15
std::shared_ptr< Receipt > ReceiptPtr
Definition receipt.h:131
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *)
Definition claims_digest.h:64
void add_schema_components(T &helper, nlohmann::json &schema, const ProofReceipt::Components *comp)
Definition receipt.h:160
std::string schema_name(const ClaimsDigest *)
Definition claims_digest.h:59
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
std::shared_ptr< TxReceiptImpl > TxReceiptImplPtr
Definition receipt.h:136
void to_json(nlohmann::json &j, const ClaimsDigest &hash)
Definition claims_digest.h:49
ReceiptPtr describe_receipt_v2(const TxReceiptImpl &receipt)
Definition historical_queries_adapter.cpp:87
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