splash 0.2.0

A decentralized network for sharing offers across the Chia ecosystem
Documentation
use libp2p::identity;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fs::{self, File};
use std::io;

pub fn load_keypair_from_file(file_path: &str) -> io::Result<identity::Keypair> {
    let contents = fs::read_to_string(file_path)?;
    let keypair_json: IdentityJson = serde_json::from_str(&contents)?;
    identity::Keypair::from_protobuf_encoding(&keypair_json.identity)
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Failed to decode keypair"))
}

pub fn save_keypair_to_file(keypair: &identity::Keypair, file_path: &str) -> io::Result<()> {
    let encoded = keypair
        .to_protobuf_encoding()
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    let file = File::create(file_path)?;
    serde_json::to_writer(file, &IdentityJson { identity: encoded })?;
    Ok(())
}

pub async fn offer_post_hook(endpoint: &str, offer: &str) -> Result<(), reqwest::Error> {
    let client = reqwest::Client::new();

    let offer_json = json!({ "offer": offer });
    client.post(endpoint).json(&offer_json).send().await?;

    Ok(())
}

#[derive(Serialize, Deserialize)]
pub struct IdentityJson {
    identity: Vec<u8>,
}