use std::{marker::PhantomData, time::SystemTime};
use generics::{
http::{BuildError, HandleError, header::HeaderValue, *},
tokio_tungstenite::tungstenite::protocol::WebSocketConfig,
};
use hmac::{Hmac, Mac};
use secrecy::{ExposeSecret as _, SecretString};
use serde::{Serialize, de::DeserializeOwned};
use sha2::Sha256;
use crate::traits::*;
pub type CoincheckRequestResult<T> = Result<T, RequestError>;
#[derive(Debug, Default)]
pub enum CoincheckOption {
#[default]
Default,
Key(String),
Secret(SecretString),
HttpUrl(CoincheckHttpUrl),
HttpAuth(bool),
RequestConfig(RequestConfig),
WebSocketUrl(CoincheckWebSocketUrl),
WebSocketChannels(Vec<String>),
WebSocketConfig(WebSocketConfig),
}
#[derive(Clone, derive_more::Debug)]
pub struct CoincheckOptions {
pub key: Option<String>,
#[debug("[REDACTED]")]
pub secret: Option<SecretString>,
pub http_url: CoincheckHttpUrl,
pub http_auth: bool,
pub request_config: RequestConfig,
pub websocket_url: CoincheckWebSocketUrl,
pub websocket_channels: Vec<String>,
pub websocket_config: WebSocketConfig,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum CoincheckHttpUrl {
Main,
#[default]
None,
}
impl CoincheckHttpUrl {
fn as_str(&self) -> &'static str {
match self {
Self::Main => "https://coincheck.com",
Self::None => "",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CoincheckWebSocketUrl {
Default,
None,
}
impl CoincheckWebSocketUrl {
fn as_str(&self) -> &'static str {
match self {
Self::Default => "wss://ws-api.coincheck.com/",
Self::None => "",
}
}
}
#[non_exhaustive]
#[derive(Debug)]
pub enum CoincheckHandlerError {
ApiError(serde_json::Value),
RequestLimitExceeded(serde_json::Value),
ParseError,
}
pub struct CoincheckRequestHandler<'a, R: DeserializeOwned> {
options: CoincheckOptions,
_phantom: PhantomData<&'a R>,
}
pub struct CoincheckWebSocketHandler {
message_handler: Box<dyn FnMut(serde_json::Value) + Send>,
options: CoincheckOptions,
}
impl<B, R> RequestHandler<B> for CoincheckRequestHandler<'_, R>
where
B: Serialize,
R: DeserializeOwned,
{
type Successful = R;
fn base_url(&self, is_test: bool) -> Result<url::Url, generics::UrlError> {
match is_test {
true => todo!(),
false => url::Url::parse(self.options.http_url.as_str()).map_err(generics::UrlError::Parse),
}
}
fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, BuildError> {
if let Some(body) = request_body {
let encoded = serde_urlencoded::to_string(body).map_err(BuildError::UrlSerialization)?;
builder = builder.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded").body(encoded);
}
let mut request = builder.build().map_err(|e| BuildError::Other(eyre::eyre!("failed to build request: {e}")))?;
if self.options.http_auth {
let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); let timestamp = time.as_millis() as u64;
let body = request.body().and_then(|body| body.as_bytes()).map(String::from_utf8_lossy).unwrap_or_default();
let sign_contents = format!("{timestamp}{}{body}", request.url());
let secret = self
.options
.secret
.as_ref()
.map(|s| s.expose_secret())
.ok_or(BuildError::Auth(generics::ConstructAuthError::MissingSecret))?;
let mut hmac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).unwrap();
hmac.update(sign_contents.as_bytes());
let signature = hex::encode(hmac.finalize().into_bytes());
let key = HeaderValue::from_str(self.options.key.as_deref().ok_or(BuildError::Auth(generics::ConstructAuthError::MissingPubkey))?)
.map_err(|e| BuildError::Auth(generics::ConstructAuthError::InvalidCharacterInApiKey(e.to_string())))?;
let headers = request.headers_mut();
headers.insert("ACCESS-KEY", key);
headers.insert("ACCESS-NONCE", HeaderValue::from(timestamp));
headers.insert("ACCESS-SIGNATURE", HeaderValue::from_str(&signature).unwrap()); }
Ok(request)
}
fn handle_response(&self, status: StatusCode, _: HeaderMap, response_body: Bytes) -> Result<Self::Successful, HandleError> {
if status.is_success() {
serde_json::from_slice(&response_body).map_err(|error| {
let response_str = v_utils::utils::truncate_msg(String::from_utf8_lossy(&response_body));
HandleError::Parse(eyre::eyre!("Failed to parse response: {error}\nResponse body: {response_str}"))
})
} else {
let error = match serde_json::from_slice::<serde_json::Value>(&response_body) {
Ok(parsed_error) => HandleError::Api(ApiError::Other(eyre::eyre!("Coincheck API error (status {status}): {parsed_error}"))),
Err(error) => {
let response_str = v_utils::utils::truncate_msg(String::from_utf8_lossy(&response_body));
HandleError::Parse(eyre::eyre!("Failed to parse error response: {error}\nResponse body: {response_str}"))
}
};
Err(error)
}
}
}
impl HandlerOptions for CoincheckOptions {
type OptionItem = CoincheckOption;
fn update(&mut self, option: Self::OptionItem) {
match option {
CoincheckOption::Default => (),
CoincheckOption::Key(v) => self.key = Some(v),
CoincheckOption::Secret(v) => self.secret = Some(v),
CoincheckOption::HttpUrl(v) => self.http_url = v,
CoincheckOption::HttpAuth(v) => self.http_auth = v,
CoincheckOption::RequestConfig(v) => self.request_config = v,
CoincheckOption::WebSocketUrl(v) => self.websocket_url = v,
CoincheckOption::WebSocketChannels(v) => self.websocket_channels = v,
CoincheckOption::WebSocketConfig(v) => self.websocket_config = v,
}
}
fn is_authenticated(&self) -> bool {
self.key.is_some() }
}
impl Default for CoincheckOptions {
fn default() -> Self {
let websocket_config = WebSocketConfig::default();
Self {
key: None,
secret: None,
http_url: CoincheckHttpUrl::Main,
http_auth: false,
request_config: RequestConfig::default(),
websocket_url: CoincheckWebSocketUrl::Default,
websocket_channels: vec![],
websocket_config,
}
}
}
impl<'a, R, B> HttpOption<'a, R, B> for CoincheckOption
where
R: DeserializeOwned + 'a,
B: Serialize,
{
type RequestHandler = CoincheckRequestHandler<'a, R>;
fn request_handler(options: Self::Options) -> Self::RequestHandler {
CoincheckRequestHandler::<'a, R> { options, _phantom: PhantomData }
}
}
impl HandlerOption for CoincheckOption {
type Options = CoincheckOptions;
}