Skip to content

Commit

Permalink
Simplify slate (de)serialization (mimblewimble#103)
Browse files Browse the repository at this point in the history
* Simplify slate (de)serialization

* rustfmt

* Cleanup

* Fix slate version tests

* Another fix for tests

* Fix slate deser in http adapter
  • Loading branch information
jaspervdm authored and yeastplume committed May 16, 2019
1 parent 6c78d23 commit 5ae5646
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 150 deletions.
44 changes: 23 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions controller/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,7 @@ where
err(e)
} else {
match api.receive_tx(&slate, None, None) {
Ok(s) => ok(s
.serialize_to_version(Some(s.version_info.orig_version))
.unwrap()),
Ok(s) => ok(serde_json::to_string(&s).unwrap()),
Err(e) => {
error!("receive_tx: failed with error: {}", e);
err(e)
Expand Down
9 changes: 6 additions & 3 deletions impls/src/adapters/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::fs::File;
use std::io::{Read, Write};

use crate::config::WalletConfig;
use crate::libwallet::{Error, Slate};
use crate::libwallet::{Error, ErrorKind, Slate};
use crate::WalletCommAdapter;
use std::collections::HashMap;

Expand All @@ -42,8 +42,11 @@ impl WalletCommAdapter for FileWalletCommAdapter {

fn send_tx_async(&self, dest: &str, slate: &Slate) -> Result<(), Error> {
let mut pub_tx = File::create(dest)?;
let slate_string = slate.serialize_to_version(Some(slate.version_info.orig_version))?;
pub_tx.write_all(slate_string.as_bytes())?;
pub_tx.write_all(
serde_json::to_string(slate)
.map_err(|_| ErrorKind::SlateSer)?
.as_bytes(),
)?;
pub_tx.sync_all()?;
Ok(())
}
Expand Down
80 changes: 18 additions & 62 deletions impls/src/adapters/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

/// HTTP Wallet 'plugin' implementation
use crate::api;
use crate::libwallet::slate_versions::{v0, v1};
use crate::libwallet::{Error, ErrorKind, Slate};
use crate::WalletCommAdapter;
use config::WalletConfig;
use failure::ResultExt;
use serde::Serialize;
use std::collections::HashMap;

#[derive(Clone)]
Expand Down Expand Up @@ -49,66 +48,14 @@ impl WalletCommAdapter for HTTPWalletCommAdapter {
}
let url = format!("{}/v1/wallet/foreign/receive_tx", dest);
debug!("Posting transaction slate to {}", url);
//TODO: Use VersionedSlate when converting to V2 API
let slate = slate.serialize_to_version(Some(slate.version_info.orig_version))?;
// For compatibility with older clients
let res: Slate = {
if let None = slate.find("version_info") {
let version = Slate::parse_slate_version(&slate)?;
match version {
1 => {
let ver1: v1::SlateV1 =
serde_json::from_str(&slate).context(ErrorKind::SlateDeser)?;
let r: Result<v1::SlateV1, _> =
api::client::post(url.as_str(), None, &ver1);
match r {
Err(e) => {
let report = format!(
"Posting transaction slate (is recipient listening?): {}",
e
);
error!("{}", report);
return Err(ErrorKind::ClientCallback(report).into());
}
Ok(s) => Slate::deserialize_upgrade(
&serde_json::to_string(&s).context(ErrorKind::SlateDeser)?,
)?,
}
}
_ => {
let ver0: v0::SlateV0 =
serde_json::from_str(&slate).context(ErrorKind::SlateDeser)?;
let r: Result<v0::SlateV0, _> =
api::client::post(url.as_str(), None, &ver0);
match r {
Err(e) => {
let report = format!(
"Posting transaction slate (is recipient listening?): {}",
e
);
error!("{}", report);
return Err(ErrorKind::ClientCallback(report).into());
}
Ok(s) => Slate::deserialize_upgrade(
&serde_json::to_string(&s).context(ErrorKind::SlateDeser)?,
)?,
}
}
}
} else {
let res: Result<String, _> = api::client::post(url.as_str(), None, &slate);
match res {
Err(e) => {
let report =
format!("Posting transaction slate (is recipient listening?): {}", e);
error!("{}", report);
return Err(ErrorKind::ClientCallback(report).into());
}
Ok(r) => Slate::deserialize_upgrade(&r)?,
}
}
};
Ok(res)
let res: String = post(url.as_str(), None, &slate).map_err(|e| {
let report = format!("Posting transaction slate (is recipient listening?): {}", e);
error!("{}", report);
ErrorKind::ClientCallback(report)
})?;
let slate = Slate::deserialize_upgrade(&res).map_err(|_| ErrorKind::SlateDeser)?;

Ok(slate)
}

fn send_tx_async(&self, _dest: &str, _slate: &Slate) -> Result<(), Error> {
Expand All @@ -130,3 +77,12 @@ impl WalletCommAdapter for HTTPWalletCommAdapter {
unimplemented!();
}
}

pub fn post<IN>(url: &str, api_secret: Option<String>, input: &IN) -> Result<String, api::Error>
where
IN: Serialize,
{
let req = api::client::create_post_request(url, api_secret, input)?;
let res = api::client::send_request(req)?;
Ok(res)
}
3 changes: 1 addition & 2 deletions impls/src/adapters/keybase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,7 @@ impl WalletCommAdapter for KeybaseWalletCommAdapter {
// Reply to the same channel with topic SLATE_SIGNED
Ok(s) => {
let slate =
s.serialize_to_version(Some(slate.version_info.orig_version))?;
// TODO: Send the same version of slate that was sent to us
serde_json::to_string(&s).map_err(|_| ErrorKind::SlateSer)?;
let success = send(slate, channel, SLATE_SIGNED, TTL);

if success {
Expand Down
4 changes: 4 additions & 0 deletions libwallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub enum ErrorKind {
#[fail(display = "Can't parse slate version")]
SlateVersionParse,

/// Can't serialize slate
#[fail(display = "Can't Serialize slate")]
SlateSer,

/// Can't deserialize slate
#[fail(display = "Can't Deserialize slate")]
SlateDeser,
Expand Down
Loading

0 comments on commit 5ae5646

Please sign in to comment.