From 05073c5c026b390e33bdb5bb08b237c77db0ec35 Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Mon, 11 Jun 2018 18:19:38 +0100 Subject: [PATCH] Web wallet api updates (#1155) * CORS for webwallet * rustfmt * automatically start up wallet web api (owner) listener --- config/src/types.rs | 4 ++-- grin.toml | 3 +++ servers/src/common/types.rs | 4 ++++ src/bin/grin.rs | 25 +++++++++++++++++++++++-- wallet/src/libwallet/controller.rs | 7 ++++++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/config/src/types.rs b/config/src/types.rs index 59eac6e60..e337fcf1c 100644 --- a/config/src/types.rs +++ b/config/src/types.rs @@ -75,7 +75,7 @@ impl From for ConfigError { /// as they tend to be quite nested in the code /// Most structs optional, as they may or may not /// be needed depending on what's being run -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct GlobalConfig { /// Keep track of the file we've read pub config_file_path: Option, @@ -91,7 +91,7 @@ pub struct GlobalConfig { /// level GlobalConfigContainer options might want to keep /// internal state that we don't necessarily /// want serialised or deserialised -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ConfigMembers { /// Server config #[serde(default)] diff --git a/grin.toml b/grin.toml index 0c713e14b..4c161d0dc 100644 --- a/grin.toml +++ b/grin.toml @@ -65,6 +65,9 @@ run_tui = true #Whether to run the wallet listener with the server by default run_wallet_listener = true +# Whether to run the web-wallet API (will only run on localhost) +run_wallet_owner_api = true + #Whether to run a test miner. This is only for developer testing (chaintype #usertesting) at cuckoo 16, and will only mine into the default wallet port. #real mining should use the standalone grin-miner diff --git a/servers/src/common/types.rs b/servers/src/common/types.rs index 218a384bd..e02cbea7a 100644 --- a/servers/src/common/types.rs +++ b/servers/src/common/types.rs @@ -182,6 +182,9 @@ pub struct ServerConfig { /// Whether to run the wallet listener with the server by default pub run_wallet_listener: Option, + /// Whether to run the web wallet owner listener + pub run_wallet_owner_api: Option, + /// Whether to run the test miner (internal, cuckoo 16) pub run_test_miner: Option, @@ -207,6 +210,7 @@ impl Default for ServerConfig { skip_sync_wait: None, run_tui: None, run_wallet_listener: Some(false), + run_wallet_owner_api: Some(false), run_test_miner: Some(false), test_miner_wallet_url: None, } diff --git a/src/bin/grin.rs b/src/bin/grin.rs index 34ae47623..0f1ee5ae2 100644 --- a/src/bin/grin.rs +++ b/src/bin/grin.rs @@ -371,7 +371,7 @@ fn server_command(server_args: Option<&ArgMatches>, mut global_config: GlobalCon info!( LOGGER, "Starting the Grin server from configuration file at {}", - global_config.config_file_path.unwrap().to_str().unwrap() + global_config.config_file_path.as_ref().unwrap().to_str().unwrap() ); global::set_mining_mode( global_config @@ -414,7 +414,7 @@ fn server_command(server_args: Option<&ArgMatches>, mut global_config: GlobalCon } if let Some(true) = server_config.run_wallet_listener { - let mut wallet_config = global_config.members.unwrap().wallet; + let mut wallet_config = global_config.members.as_ref().unwrap().wallet.clone(); if let Err(_) = wallet::WalletSeed::from_file(&wallet_config) { wallet::WalletSeed::init_file(&wallet_config) .expect("Failed to create wallet seed file."); @@ -436,6 +436,27 @@ fn server_command(server_args: Option<&ArgMatches>, mut global_config: GlobalCon }); }); } + if let Some(true) = server_config.run_wallet_owner_api { + let mut wallet_config = global_config.members.unwrap().wallet; + if let Err(_) = wallet::WalletSeed::from_file(&wallet_config) { + wallet::WalletSeed::init_file(&wallet_config) + .expect("Failed to create wallet seed file."); + }; + + let _ = thread::Builder::new() + .name("wallet_owner_listener".to_string()) + .spawn(move || { + let wallet = FileWallet::new(wallet_config.clone(), "").unwrap_or_else(|e| { + panic!("Error creating wallet: {:?} Config: {:?}", e, wallet_config) + }); + wallet::controller::owner_listener(wallet, "127.0.0.1:13420").unwrap_or_else(|e| { + panic!( + "Error creating wallet api listener: {:?} Config: {:?}", + e, wallet_config + ) + }); + }); + } // start the server in the different run modes (interactive or daemon) if let Some(a) = server_args { diff --git a/wallet/src/libwallet/controller.rs b/wallet/src/libwallet/controller.rs index 640ec1627..5d33cfddf 100644 --- a/wallet/src/libwallet/controller.rs +++ b/wallet/src/libwallet/controller.rs @@ -202,7 +202,12 @@ where IronError::new(Fail::compat(e), status::BadRequest) })?; let mut api = APIOwner::new(&mut *wallet); - let resp_json = self.handle_request(req, &mut api); + let mut resp_json = self.handle_request(req, &mut api); + resp_json + .as_mut() + .unwrap() + .headers + .set_raw("access-control-allow-origin", vec![b"*".to_vec()]); api.wallet .close() .map_err(|e| IronError::new(Fail::compat(e), status::BadRequest))?;