YPC  0.2.0
offchain_result.h
1 #pragma once
2 #include "ypc/common/endian.h"
3 #include "ypc/core_t/analyzer/helper/parser_type_traits.h"
4 #include "ypc/core_t/analyzer/var/enclave_hash_var.h"
5 #include "ypc/core_t/analyzer/var/encrypted_param_var.h"
6 #include "ypc/core_t/analyzer/var/request_key_var.h"
7 #include "ypc/core_t/analyzer/var/result_var.h"
8 #include "ypc/stbox/ebyte.h"
9 #include "ypc/stbox/stx_status.h"
10 
11 namespace ypc {
12 namespace internal {
13 template <typename Crypto>
14 class offchain_result : virtual public request_key_var<true>,
15  virtual public enclave_hash_var,
16  virtual public result_var,
17  virtual public encrypted_param_var,
18  virtual public data_hash_var {
19  typedef Crypto crypto;
21 
22 public:
23  uint32_t generate_result() {
24  stbox::bytes skey;
25 
26  crypto::gen_private_key(skey);
27  stbox::bytes pkey;
28  crypto::generate_pkey_from_skey(skey, pkey);
29 
30  auto rs = result_var::m_result;
31 
32  auto status = crypto::encrypt_message_with_prefix(
33  pkey, rs, utc::crypto_prefix_arbitrary, m_encrypted_result_str);
34 
35  if (status != stbox::stx_status::success) {
36  LOG(ERROR) << "error for encrypt_message: " << status;
37  return status;
38  }
39 
40  stbox::bytes hash_m;
41  crypto::hash_256(m_encrypted_result_str, hash_m);
42 
43  stbox::bytes pkey_a;
44  status = crypto::generate_pkey_from_skey(m_private_key, pkey_a);
45 
46  status = crypto::encrypt_message_with_prefix(
47  pkey_a, skey, utc::crypto_prefix_arbitrary, m_encrypted_c);
48 
49  if (status != stbox::stx_status::success) {
50  LOG(ERROR) << "error for encrypt_message: " << status;
51  return status;
52  }
53 
54  stbox::bytes cost_gas_str(sizeof(m_cost_gas));
55  memcpy((uint8_t *)&cost_gas_str[0], (uint8_t *)&m_cost_gas,
56  sizeof(m_cost_gas));
57  ypc::utc::endian_swap(cost_gas_str);
58 
59  auto cost_msg =
60  m_encrypted_param + m_data_hash + m_enclave_hash + cost_gas_str;
61 #ifdef DEBUG
62  LOG(INFO) << "cost message to sign: " << cost_msg;
63 #endif
64  status =
65  crypto::sign_message(m_private_key, cost_msg, m_cost_signature_str);
66  if (status != stbox::stx_status::success) {
67  LOG(ERROR) << "error for sign cost: " << status;
68  return status;
69  }
70 
71  auto msg = m_encrypted_c + hash_m + m_encrypted_param + m_data_hash +
72  cost_gas_str + m_enclave_hash;
73 #ifdef DEBUG
74  LOG(INFO) << "result message to sign: " << msg;
75 #endif
76 #ifdef DEBUG
77  LOG(INFO) << "sign with private key: " << m_private_key;
78 #endif
79  status = crypto::sign_message(m_private_key, msg, m_result_signature_str);
80 
81  return static_cast<uint32_t>(status);
82  }
83  nt<stbox::bytes>::offchain_result_package_t get_result_pkg() {
84  using ntt = nt<stbox::bytes>;
85  typename ntt::offchain_result_package_t pkg;
86  pkg.set<ntt::encrypted_result>(m_encrypted_result_str);
87  pkg.set<ntt::data_hash>(data_hash_var::m_data_hash);
88  pkg.set<ntt::result_signature>(m_result_signature_str);
89  pkg.set<ntt::cost_signature>(m_cost_signature_str);
90  pkg.set<ntt::result_encrypt_key>(m_encrypted_c);
91  return pkg;
92  }
93 
94  uint32_t get_analyze_result(uint8_t *result, uint32_t size) {
95 
96  auto pkg = get_result_pkg();
97 
98  ff::net::marshaler lm(ff::net::marshaler::length_retriver);
99  pkg.arch(lm);
100  if (size != lm.get_length()) {
101  return stbox::stx_status::out_buffer_length_error;
102  }
103  ff::net::marshaler ld((char *)result, size, ff::net::marshaler::serializer);
104  pkg.arch(ld);
105  return stbox::stx_status::success;
106  }
107 
108  uint32_t get_analyze_result_size() {
109  auto pkg = get_result_pkg();
110  ff::net::marshaler lm(ff::net::marshaler::length_retriver);
111  pkg.arch(lm);
112  return lm.get_length();
113  }
114 
115 protected:
116  stbox::bytes m_encrypted_c;
117  stbox::bytes m_encrypted_result_str;
118  stbox::bytes m_cost_signature_str;
119  stbox::bytes m_result_signature_str;
120 };
121 } // namespace internal
122 template <typename Crypto>
124 
125 template <typename Crypto> struct result_type_traits<offchain_result<Crypto>> {
126  constexpr static uint32_t value = ypc::utc::offchain_result_parser;
127 };
128 } // namespace ypc
ypc::internal::offchain_result
Definition: offchain_result.h:14
ypc::internal::result_var
Definition: result_var.h:5
ypc::internal::encrypted_param_var
Definition: encrypted_param_var.h:7
ypc::nt
Definition: nt_cols.h:6
ypc::internal::enclave_hash_var
Definition: enclave_hash_var.h:7
ypc::internal::request_key_var
Definition: request_key_var.h:7
ypc::internal::data_hash_var
Definition: data_hash_var.h:6
ypc::result_type_traits
Definition: parser_type_traits.h:4
ypc::crypto::crypto_pack
Definition: crypto_pack.h:14
ypc::internal::request_key_var< true >
Definition: request_key_var.h:9