mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-12-03 04:13:57 +01:00
Slate compatibility between versions (#35)
* wallet compatibilities * rustfmt
This commit is contained in:
@@ -763,14 +763,14 @@ where
|
||||
Box::new(parse_body(req).and_then(
|
||||
//TODO: No way to insert a message from the params
|
||||
move |slate_str: String| {
|
||||
let mut slate: Slate = Slate::deserialize_upgrade(&slate_str).unwrap();
|
||||
let slate: Slate = Slate::deserialize_upgrade(&slate_str).unwrap();
|
||||
if let Err(e) = api.verify_slate_messages(&slate) {
|
||||
error!("Error validating participant messages: {}", e);
|
||||
err(e)
|
||||
} else {
|
||||
match api.receive_tx(&mut slate, None, None) {
|
||||
Ok(_) => ok(slate
|
||||
.serialize_to_version(Some(slate.version_info.orig_version))
|
||||
match api.receive_tx(&slate, None, None) {
|
||||
Ok(s) => ok(s
|
||||
.serialize_to_version(Some(s.version_info.orig_version))
|
||||
.unwrap()),
|
||||
Err(e) => {
|
||||
error!("receive_tx: failed with error: {}", e);
|
||||
@@ -798,7 +798,7 @@ where
|
||||
),
|
||||
"receive_tx" => Box::new(
|
||||
self.receive_tx(req, api)
|
||||
.and_then(|res| ok(json_response(&res))),
|
||||
.and_then(|res| ok(json_response_slate(&res))),
|
||||
),
|
||||
_ => Box::new(ok(response(StatusCode::BAD_REQUEST, "unknown action"))),
|
||||
}
|
||||
@@ -909,6 +909,29 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// As above, dealing with stringified slate output
|
||||
// from older versions.
|
||||
// Older versions are expecting a slate objects, anything from
|
||||
// 1.1.0 up is expecting a string
|
||||
fn json_response_slate<T>(s: &T) -> Response<Body>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
match serde_json::to_string(s) {
|
||||
Ok(mut json) => {
|
||||
if let None = json.find("version_info") {
|
||||
let mut r = json.clone();
|
||||
r.pop();
|
||||
r.remove(0);
|
||||
// again, for backwards slate compat
|
||||
json = r.replace("\\\"", "\"")
|
||||
}
|
||||
response(StatusCode::OK, json)
|
||||
}
|
||||
Err(_) => response(StatusCode::INTERNAL_SERVER_ERROR, ""),
|
||||
}
|
||||
}
|
||||
|
||||
// pretty-printed version of above
|
||||
fn json_response_pretty<T>(s: &T) -> Response<Body>
|
||||
where
|
||||
@@ -987,10 +1010,26 @@ where
|
||||
req.into_body()
|
||||
.concat2()
|
||||
.map_err(|_| ErrorKind::GenericError("Failed to read request".to_owned()).into())
|
||||
.and_then(|body| match serde_json::from_reader(&body.to_vec()[..]) {
|
||||
Ok(obj) => ok(obj),
|
||||
Err(e) => {
|
||||
err(ErrorKind::GenericError(format!("Invalid request body: {}", e)).into())
|
||||
.and_then(|body| {
|
||||
match serde_json::from_reader(&body.to_vec()[..]) {
|
||||
Ok(obj) => ok(obj),
|
||||
Err(_) => {
|
||||
// try to parse as string instead, for backwards compatibility
|
||||
let replaced_str = String::from_utf8(body.to_vec().clone())
|
||||
.unwrap()
|
||||
.replace("\"", "\\\"");
|
||||
let mut str_vec = replaced_str.as_bytes().to_vec();
|
||||
str_vec.push(0x22);
|
||||
str_vec.insert(0, 0x22);
|
||||
match serde_json::from_reader(&str_vec[..]) {
|
||||
Ok(obj) => ok(obj),
|
||||
Err(e) => err(ErrorKind::GenericError(format!(
|
||||
"Invalid request body: {}",
|
||||
e
|
||||
))
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user