Skip to content

Commit

Permalink
Add init_api_secure function (mimblewimble#206)
Browse files Browse the repository at this point in the history
* adding initial version of init_secure_api

* rustfmt

* fix ECDH algo

* rustfmt

* trying to figure out best way of doing encryption

* refactor secure requests and responses into json-rpc responses, with base64 payload for encrypted messages

* rustfmt

* return proper errors from encrypted api, include tests covering encrypted API error cases

* rustfmt

* add test for normal error (unencrypted)

* rustfmt

* change ports for test, add foreign listener to V2 sanity tests, add ability to select owner api port via command line

* rustfmt

* turn it to 11

* explicit teardown after rpc tests

* update tests with explicit teardowns

* update tests to perform explicit teardown

* fix warnings, ensure all tests teardown

* log output to diagnose CI windows build failures

* disable owner api doctests on windows

* rustfmt
  • Loading branch information
yeastplume authored Aug 19, 2019
1 parent 62d976f commit a58cae6
Show file tree
Hide file tree
Showing 33 changed files with 1,229 additions and 299 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ failure_derive = "0.1"
log = "0.4"
uuid = { version = "0.7", features = ["serde", "v4"] }
serde = "1"
rand = "0.5"
serde_derive = "1"
serde_json = "1"
easy-jsonrpc = "0.5.1"
easy-jsonrpc-mw = "0.5.3"
chrono = { version = "0.4.4", features = ["serde"] }
ring = "0.13"
base64 = "0.9"

grin_wallet_libwallet = { path = "../libwallet", version = "2.1.0-beta.1" }
grin_wallet_config = { path = "../config", version = "2.1.0-beta.1" }
Expand Down
14 changes: 8 additions & 6 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use crate::libwallet::{
NodeVersionInfo, Slate, VersionInfo, VersionedSlate, WalletLCProvider,
};
use crate::{Foreign, ForeignCheckMiddlewareFn};
use easy_jsonrpc;
use easy_jsonrpc_mw;

/// Public definition used to generate Foreign jsonrpc api.
/// * When running `grin-wallet listen` with defaults, the V2 api is available at
/// `localhost:3415/v2/foreign`
/// * The endpoint only supports POST operations, with the json-rpc request as the body
#[easy_jsonrpc::rpc]
#[easy_jsonrpc_mw::rpc]
pub trait ForeignRpc {
/**
Networked version of [Foreign::check_version](struct.Foreign.html#method.check_version).
Expand Down Expand Up @@ -577,7 +577,7 @@ pub fn run_doctest_foreign(
init_tx: bool,
init_invoice_tx: bool,
) -> Result<Option<serde_json::Value>, String> {
use easy_jsonrpc::Handler;
use easy_jsonrpc_mw::Handler;
use grin_wallet_impls::test_framework::{self, LocalWalletClient, WalletProxy};
use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl};
use grin_wallet_libwallet::{api_impl, WalletInst};
Expand Down Expand Up @@ -613,7 +613,7 @@ pub fn run_doctest_foreign(
let mut wallet1 =
Box::new(DefaultWalletImpl::<LocalWalletClient>::new(client1.clone()).unwrap())
as Box<
WalletInst<
dyn WalletInst<
'static,
DefaultLCProvider<LocalWalletClient, ExtKeychain>,
LocalWalletClient,
Expand Down Expand Up @@ -648,7 +648,7 @@ pub fn run_doctest_foreign(
let mut wallet2 =
Box::new(DefaultWalletImpl::<LocalWalletClient>::new(client2.clone()).unwrap())
as Box<
WalletInst<
dyn WalletInst<
'static,
DefaultLCProvider<LocalWalletClient, ExtKeychain>,
LocalWalletClient,
Expand Down Expand Up @@ -751,7 +751,9 @@ pub fn run_doctest_foreign(
};
api_foreign.doctest_mode = true;
let foreign_api = &api_foreign as &dyn ForeignRpc;
Ok(foreign_api.handle_request(request).as_option())
let res = foreign_api.handle_request(request).as_option();
let _ = fs::remove_dir_all(test_dir);
Ok(res)
}

#[doc(hidden)]
Expand Down
13 changes: 3 additions & 10 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ mod owner;
mod owner_rpc;
mod owner_rpc_s;

mod types;

pub use crate::foreign::{Foreign, ForeignCheckMiddleware, ForeignCheckMiddlewareFn};
pub use crate::foreign_rpc::ForeignRpc;
pub use crate::owner::Owner;
Expand All @@ -53,13 +55,4 @@ pub use crate::foreign_rpc::foreign_rpc as foreign_rpc_client;
pub use crate::foreign_rpc::run_doctest_foreign;
pub use crate::owner_rpc::run_doctest_owner;

use grin_wallet_util::grin_core::libtx::secp_ser;
use util::secp::key::SecretKey;

/// Wrapper for API Tokens
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(transparent)]
pub struct Token {
#[serde(with = "secp_ser::option_seckey_serde")]
keychain_mask: Option<SecretKey>,
}
pub use types::{ECDHPubkey, EncryptedRequest, EncryptedResponse, EncryptionErrorResponse, Token};
3 changes: 3 additions & 0 deletions api/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ where
pub wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
/// Flag to normalize some output during testing. Can mostly be ignored.
pub doctest_mode: bool,
/// Share ECDH key
pub shared_key: Arc<Mutex<Option<SecretKey>>>,
}

impl<'a, L, C, K> Owner<'a, L, C, K>
Expand Down Expand Up @@ -141,6 +143,7 @@ where
Owner {
wallet_inst,
doctest_mode: false,
shared_key: Arc::new(Mutex::new(None)),
}
}

Expand Down
91 changes: 50 additions & 41 deletions api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ use crate::libwallet::{
};
use crate::util::Mutex;
use crate::{Owner, OwnerRpcS};
use easy_jsonrpc;
use easy_jsonrpc_mw;
use std::sync::Arc;

/// Public definition used to generate Owner jsonrpc api.
/// * When running `grin-wallet owner_api` with defaults, the V2 api is available at
/// `localhost:3420/v2/owner`
/// * The endpoint only supports POST operations, with the json-rpc request as the body
#[easy_jsonrpc::rpc]
pub trait OwnerRpc {
#[easy_jsonrpc_mw::rpc]
pub trait OwnerRpc: Sync + Send {
/**
Networked version of [Owner::accounts](struct.Owner.html#method.accounts).
Expand Down Expand Up @@ -1148,7 +1148,7 @@ pub trait OwnerRpc {
}
}
# "#
# ,false, 5 ,true, false, false);
# ,false, 0 ,false, false, false);
```
*/
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind>;
Expand Down Expand Up @@ -1370,7 +1370,7 @@ pub fn run_doctest_owner(
lock_tx: bool,
finalize_tx: bool,
) -> Result<Option<serde_json::Value>, String> {
use easy_jsonrpc::Handler;
use easy_jsonrpc_mw::Handler;
use grin_wallet_impls::test_framework::{self, LocalWalletClient, WalletProxy};
use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl};
use grin_wallet_libwallet::{api_impl, WalletInst};
Expand Down Expand Up @@ -1404,7 +1404,7 @@ pub fn run_doctest_owner(
let mut wallet1 =
Box::new(DefaultWalletImpl::<LocalWalletClient>::new(client1.clone()).unwrap())
as Box<
WalletInst<
dyn WalletInst<
'static,
DefaultLCProvider<LocalWalletClient, ExtKeychain>,
LocalWalletClient,
Expand Down Expand Up @@ -1439,7 +1439,7 @@ pub fn run_doctest_owner(
let mut wallet2 =
Box::new(DefaultWalletImpl::<LocalWalletClient>::new(client2.clone()).unwrap())
as Box<
WalletInst<
dyn WalletInst<
'static,
DefaultLCProvider<LocalWalletClient, ExtKeychain>,
LocalWalletClient,
Expand Down Expand Up @@ -1547,13 +1547,15 @@ pub fn run_doctest_owner(

let mut api_owner = Owner::new(wallet1);
api_owner.doctest_mode = true;
if use_token {
let res = if use_token {
let owner_api = &api_owner as &dyn OwnerRpcS;
Ok(owner_api.handle_request(request).as_option())
owner_api.handle_request(request).as_option()
} else {
let owner_api = &api_owner as &dyn OwnerRpc;
Ok(owner_api.handle_request(request).as_option())
}
owner_api.handle_request(request).as_option()
};
let _ = fs::remove_dir_all(test_dir);
Ok(res)
}

#[doc(hidden)]
Expand All @@ -1563,39 +1565,46 @@ macro_rules! doctest_helper_json_rpc_owner_assert_response {
// create temporary wallet, run jsonrpc request on owner api of wallet, delete wallet, return
// json response.
// In order to prevent leaking tempdirs, This function should not panic.
use grin_wallet_api::run_doctest_owner;
use serde_json;
use serde_json::Value;
use tempfile::tempdir;

let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
let dir = dir
.path()
.to_str()
.ok_or("Failed to convert tmpdir path to string.".to_owned())
.unwrap();

let request_val: Value = serde_json::from_str($request).unwrap();
let expected_response: Value = serde_json::from_str($expected_response).unwrap();

let response = run_doctest_owner(
request_val,
dir,
$use_token,
$blocks_to_mine,
$perform_tx,
$lock_tx,
$finalize_tx,
)
.unwrap()
.unwrap();
// These cause LMDB to run out of disk space on CircleCI
// disable for now on windows
// TODO: Fix properly
#[cfg(not(target_os = "windows"))]
{
use grin_wallet_api::run_doctest_owner;
use serde_json;
use serde_json::Value;
use tempfile::tempdir;

let dir = tempdir().map_err(|e| format!("{:#?}", e)).unwrap();
let dir = dir
.path()
.to_str()
.ok_or("Failed to convert tmpdir path to string.".to_owned())
.unwrap();

let request_val: Value = serde_json::from_str($request).unwrap();
let expected_response: Value = serde_json::from_str($expected_response).unwrap();

let response = run_doctest_owner(
request_val,
dir,
$use_token,
$blocks_to_mine,
$perform_tx,
$lock_tx,
$finalize_tx,
)
.unwrap()
.unwrap();

if response != expected_response {
panic!(
"(left != right) \nleft: {}\nright: {}",
serde_json::to_string_pretty(&response).unwrap(),
serde_json::to_string_pretty(&expected_response).unwrap()
if response != expected_response {
panic!(
"(left != right) \nleft: {}\nright: {}",
serde_json::to_string_pretty(&response).unwrap(),
serde_json::to_string_pretty(&expected_response).unwrap()
);
}
}
};
}
Loading

0 comments on commit a58cae6

Please sign in to comment.