Change EncryptedRequest 'id' field to String (#336)

* change ID field in encrypted request/response to be a string of any kind

* Use JsonId Enum instead of String value
This commit is contained in:
Yeastplume
2020-02-18 12:28:08 +00:00
committed by GitHub
parent b2c973fd0b
commit da064acc33
7 changed files with 183 additions and 75 deletions
+2 -1
View File
@@ -57,5 +57,6 @@ pub use crate::foreign_rpc::run_doctest_foreign;
pub use crate::owner_rpc::run_doctest_owner;
pub use types::{
ECDHPubkey, EncryptedRequest, EncryptedResponse, EncryptionErrorResponse, PubAddress, Token,
ECDHPubkey, EncryptedRequest, EncryptedResponse, EncryptionErrorResponse, JsonId, PubAddress,
Token,
};
+20 -10
View File
@@ -25,6 +25,17 @@ use ring::aead;
use serde_json::{self, Value};
use std::collections::HashMap;
/// Represents a compliant JSON RPC 2.0 id.
/// Valid id: Integer, String.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum JsonId {
/// Integer Id
IntId(u32),
/// String Id
StrId(String),
}
/// Wrapper for API Tokens
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(transparent)]
@@ -142,19 +153,18 @@ pub struct EncryptedRequest {
/// method
pub method: String,
/// id
#[serde(with = "secp_ser::string_or_u64")]
pub id: u64,
pub id: JsonId,
/// Body params, which includes nonce and encrypted request
pub params: EncryptedBody,
}
impl EncryptedRequest {
/// from json
pub fn from_json(id: u64, json_in: &Value, enc_key: &SecretKey) -> Result<Self, Error> {
pub fn from_json(id: &JsonId, json_in: &Value, enc_key: &SecretKey) -> Result<Self, Error> {
Ok(EncryptedRequest {
jsonrpc: "2.0".to_owned(),
method: "encrypted_request_v3".to_owned(),
id: id,
id: id.clone(),
params: EncryptedBody::from_json(json_in, enc_key)?,
})
}
@@ -188,15 +198,14 @@ pub struct EncryptedResponse {
/// JSON RPC response
pub jsonrpc: String,
/// id
#[serde(with = "secp_ser::string_or_u64")]
pub id: u64,
pub id: JsonId,
/// result
pub result: HashMap<String, EncryptedBody>,
}
impl EncryptedResponse {
/// from json
pub fn from_json(id: u64, json_in: &Value, enc_key: &SecretKey) -> Result<Self, Error> {
pub fn from_json(id: &JsonId, json_in: &Value, enc_key: &SecretKey) -> Result<Self, Error> {
let mut result_set = HashMap::new();
result_set.insert(
"Ok".to_string(),
@@ -204,7 +213,7 @@ impl EncryptedResponse {
);
Ok(EncryptedResponse {
jsonrpc: "2.0".to_owned(),
id: id,
id: id.clone(),
result: result_set,
})
}
@@ -307,12 +316,13 @@ fn encrypted_request() -> Result<(), Error> {
"token": "d202964900000000d302964900000000d402964900000000d502964900000000"
}
});
let enc_req = EncryptedRequest::from_json(1, &req, &shared_key)?;
let enc_req =
EncryptedRequest::from_json(&JsonId::StrId(String::from("1")), &req, &shared_key)?;
println!("{:?}", enc_req);
let dec_req = enc_req.decrypt(&shared_key)?;
println!("{:?}", dec_req);
assert_eq!(req, dec_req);
let enc_res = EncryptedResponse::from_json(1, &req, &shared_key)?;
let enc_res = EncryptedResponse::from_json(&JsonId::IntId(1), &req, &shared_key)?;
println!("{:?}", enc_res);
println!("{:?}", enc_res.as_json_str()?);
let dec_res = enc_res.decrypt(&shared_key)?;