1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Share protocol logic for structuring GraphQL requests and parsing responses

use crate::errors::{ProtocolError, Result};
use thiserror::Error;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::fmt::Display;
use super::traits::TryFromState;
use super::state::State;
use std::sync::Arc;
use tokio::sync::RwLock;
use std::collections::HashMap;

//****************************************//
//  GraphQL response parsing              //
//****************************************//

/// Helper to convert JSON to a response or error
pub fn json_to_type_or_error<T: DeserializeOwned>(
    response: serde_json::Value,
) -> Result<ResponseOrError<T>> {
    Ok(serde_json::from_value(response)
        .map_err(|x| {
            ProtocolError::coerce_static_from_str(&format!("{:#?}", x))
        })?)
}

pub fn serializable_to_json<T: Serialize>(obj: &T) -> Result<serde_json::Value> {
    let str_val = serde_json::to_string(obj)
        .map_err(|_| ProtocolError("Unexpected problem serializing T to string"))?;
    Ok(serde_json::from_str(&str_val)
        .expect("Transforming to JSON failed. This should never happen"))
}

/// Helper to convert data corresponding to raw GraphQL types (B) into
/// nicer library managed types A when failure is possible
pub fn try_response_from_json<A, B>(response: serde_json::Value) -> Result<ResponseOrError<A>>
where
    A: TryFrom<B>,
    <A as TryFrom<B>>::Error: Display,
    B: DeserializeOwned,
{
    let parse_graphql = json_to_type_or_error::<B>(response)?;
    // Maybe we get back a parsed result from server response
    let mapped = parse_graphql.map(Box::new(|data| data.try_into()));
    // Unpacking the inner state is annoying, but need to surface the conversion error if encountered
    match mapped {
        ResponseOrError::Response(DataResponse { data }) => match data {
            Ok(data) => Ok(ResponseOrError::from_data(data)),
            Err(err) => Err(ProtocolError::coerce_static_from_str(&format!("{}", err))),
        },
        ResponseOrError::Error(e) => Ok(ResponseOrError::Error(e)),
    }
}

/// Helper to convert data corresponding to raw GraphQL types (B) into
/// nicer library managed types A when failure is possible
pub async fn try_response_with_state_from_json<A, B>(
    response: serde_json::Value,
    state: Arc<RwLock<State>>
) -> Result<ResponseOrError<A>>
where
    A: TryFromState<B>,
    B: DeserializeOwned,
{
    let parse_graphql = json_to_type_or_error::<B>(response)?;
    // Maybe we get back a parsed result from server response
    match parse_graphql {
        ResponseOrError::Response(DataResponse { data }) => {
            let conversion = TryFromState::from(data, state.clone()).await;
            match conversion {
                Ok(data) => Ok(ResponseOrError::from_data(data)),
                Err(err) => Err(ProtocolError::coerce_static_from_str(&format!("{}", err))),
            }
        },
        ResponseOrError::Error(e) => Ok(ResponseOrError::Error(e)),
    }
}

pub enum ErrorOrData<T> {
    Data(T),
    Error(ErrorResponse),
}

/// Wrapper type to account for GraphQL errors
#[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)]
pub enum ResponseOrError<T> {
    Response(DataResponse<T>),
    Error(ErrorResponse),
}

impl<T> ResponseOrError<T> {
    /// Build from data
    pub fn from_data(data: T) -> Self {
        Self::Response(DataResponse { data })
    }

    // TODO: why can't the enum look like this?
    pub fn consume_match(self) -> ErrorOrData<T> {
        match self {
            Self::Response(DataResponse { data }) => ErrorOrData::Data(data),
            Self::Error(e) => ErrorOrData::Error(e),
        }
    }

    /// Get response from wrapper if it exists
    pub fn response(&self) -> Option<&T> {
        match self {
            Self::Response(DataResponse { data }) => Some(data),
            Self::Error(_) => None,
        }
    }
    /// Get response from wrapper if it exists, will destroy wrapper
    pub fn consume_response(self) -> Option<T> {
        match self {
            Self::Response(DataResponse { data }) => Some(data),
            Self::Error(_) => None,
        }
    }
    pub fn consume_error(self) -> Option<ErrorResponse> {
        match self {
            Self::Response(_) => None,
            Self::Error(e) => Some(e),
        }
    }
    /// Get response or else error
    pub fn response_or_error(self) -> Result<T> {
        match self {
            Self::Response(DataResponse { data}) => Ok(data),
            Self::Error(e) => Err(ProtocolError::coerce_static_from_str(&format!("{:?}", e)))
        }
    }
    /// Get error from wrapper if it exists
    pub fn error(&self) -> Option<&ErrorResponse> {
        match self {
            Self::Response(_) => None,
            Self::Error(error) => Some(error),
        }
    }
    /// Is the response a GraphQL error?
    pub fn is_error(&self) -> bool {
        match self {
            Self::Error(_) => true,
            Self::Response(_) => false,
        }
    }
    /// Transform the inner value of a valid response. Will not transform an error
    pub fn map<M>(self, f: Box<dyn Fn(T) -> M>) -> ResponseOrError<M> {
        match self {
            Self::Error(e) => ResponseOrError::Error(e),
            Self::Response(r) => ResponseOrError::Response(r.map(f)),
        }
    }
}

/// Inner wrapper on valid GraphQL response data
#[derive(Deserialize, Serialize, Debug)]
pub struct DataResponse<T> {
    pub data: T,
}

impl<T> DataResponse<T> {
    fn map<M>(self, f: Box<dyn Fn(T) -> M>) -> DataResponse<M> {
        DataResponse { data: f(self.data) }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GraphQLResponse {
    #[serde(default)]
    pub data: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub errors: Vec<Error>
}

impl TryFrom<serde_json::Value> for GraphQLResponse {
    type Error = ProtocolError;
    fn try_from(response: serde_json::Value) -> Result<Self> {
        serde_json::from_value(response).map_err(|e|
            ProtocolError::coerce_static_from_str(
                &format!("Couldn't parse response: {:#?}", e)
            )
        )
    }
}

/// Inner wrapper on error GraphQL response data
#[derive(Clone, Deserialize, Serialize, Debug, Error)]
#[error("")]
pub struct ErrorResponse {
    pub errors: Vec<Error>,
}

/// Inner wrapper on error GraphQL response data
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct Error {
    pub message: String,
    pub path: Vec<String>
}

//****************************************//
//  Common blockchain types               //
//****************************************//

/// This signature format is used for GraphQL request payload signatures
/// Data is SHA256 hashed and signed with a secp256k1 ECDSA key
#[derive(Clone, Debug)]
pub struct RequestPayloadSignature {
    pub signed_digest: String,
    pub public_key: String,
}

impl RequestPayloadSignature {
    pub fn empty() -> Self {
        Self {
            signed_digest: "".to_string(),
            public_key: "".to_string(),
        }
    }
}