2019-10-03 17:16:09 +03:00
|
|
|
// Copyright 2019 The Grin Developers
|
2019-02-13 18:05:19 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Controller for wallet.. instantiates and handles listeners (or single-run
|
|
|
|
//! invocations) as needed.
|
2019-04-01 13:16:49 +03:00
|
|
|
use crate::api::{self, ApiServer, BasicAuthMiddleware, ResponseFuture, Router, TLSConfig};
|
2019-02-13 18:05:19 +03:00
|
|
|
use crate::keychain::Keychain;
|
2019-04-24 11:27:14 +03:00
|
|
|
use crate::libwallet::{
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
Error, ErrorKind, NodeClient, NodeVersionInfo, Slate, WalletInst, WalletLCProvider,
|
|
|
|
CURRENT_SLATE_VERSION, GRIN_BLOCK_HEADER_VERSION,
|
2019-02-13 18:05:19 +03:00
|
|
|
};
|
2019-08-06 14:50:41 +03:00
|
|
|
use crate::util::secp::key::SecretKey;
|
2019-09-02 18:03:35 +03:00
|
|
|
use crate::util::{from_hex, static_secp_instance, to_base64, Mutex};
|
2019-02-13 18:05:19 +03:00
|
|
|
use failure::ResultExt;
|
|
|
|
use futures::future::{err, ok};
|
|
|
|
use futures::{Future, Stream};
|
2019-04-03 16:22:30 +03:00
|
|
|
use hyper::header::HeaderValue;
|
2019-02-13 18:05:19 +03:00
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json;
|
2019-09-02 18:03:35 +03:00
|
|
|
use std::collections::HashMap;
|
2019-02-13 18:05:19 +03:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-10-14 22:24:09 +03:00
|
|
|
use crate::impls::tor::config as tor_config;
|
|
|
|
use crate::impls::tor::process as tor_process;
|
|
|
|
|
2019-08-19 15:05:21 +03:00
|
|
|
use crate::apiwallet::{
|
|
|
|
EncryptedRequest, EncryptedResponse, EncryptionErrorResponse, Foreign,
|
|
|
|
ForeignCheckMiddlewareFn, ForeignRpc, Owner, OwnerRpc, OwnerRpcS,
|
|
|
|
};
|
|
|
|
use easy_jsonrpc_mw;
|
|
|
|
use easy_jsonrpc_mw::{Handler, MaybeReply};
|
2019-04-01 13:16:49 +03:00
|
|
|
|
2019-04-03 16:22:30 +03:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref GRIN_OWNER_BASIC_REALM: HeaderValue =
|
|
|
|
HeaderValue::from_str("Basic realm=GrinOwnerAPI").unwrap();
|
|
|
|
}
|
|
|
|
|
2019-06-27 12:41:05 +03:00
|
|
|
fn check_middleware(
|
|
|
|
name: ForeignCheckMiddlewareFn,
|
|
|
|
node_version_info: Option<NodeVersionInfo>,
|
|
|
|
slate: Option<&Slate>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
match name {
|
|
|
|
// allow coinbases to be built regardless
|
|
|
|
ForeignCheckMiddlewareFn::BuildCoinbase => Ok(()),
|
|
|
|
_ => {
|
|
|
|
let mut bhv = 1;
|
|
|
|
if let Some(n) = node_version_info {
|
|
|
|
bhv = n.block_header_version;
|
|
|
|
}
|
|
|
|
if let Some(s) = slate {
|
|
|
|
if s.version_info.version < CURRENT_SLATE_VERSION
|
|
|
|
|| (bhv == 1 && s.version_info.block_header_version != 1)
|
|
|
|
|| (bhv > 1 && s.version_info.block_header_version < GRIN_BLOCK_HEADER_VERSION)
|
|
|
|
{
|
|
|
|
Err(ErrorKind::Compatibility(
|
|
|
|
"Incoming Slate is not compatible with this wallet. \
|
|
|
|
Please upgrade the node or use a different one."
|
|
|
|
.into(),
|
|
|
|
))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 22:24:09 +03:00
|
|
|
/// initiate the tor listener
|
|
|
|
fn init_tor_listener<L, C, K>(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
|
|
|
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
|
|
|
addr: &str,
|
|
|
|
) -> Result<tor_process::TorProcess, Error>
|
|
|
|
where
|
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
let mut process = tor_process::TorProcess::new();
|
|
|
|
let mask = keychain_mask.lock();
|
|
|
|
// eventually want to read a list of service config keys
|
|
|
|
let mut w_lock = wallet.lock();
|
|
|
|
let lc = w_lock.lc_provider()?;
|
|
|
|
let w_inst = lc.wallet_inst()?;
|
|
|
|
let k = w_inst.keychain((&mask).as_ref())?;
|
|
|
|
let parent_key_id = w_inst.parent_key_id();
|
|
|
|
let tor_dir = format!("{}/tor/listener", lc.get_top_level_directory()?);
|
|
|
|
let sec_key = tor_config::address_derivation_path(&k, &parent_key_id, 0)
|
|
|
|
.map_err(|e| ErrorKind::TorConfig(format!("{:?}", e).into()))?;
|
|
|
|
let onion_address = tor_config::onion_address_from_seckey(&sec_key)
|
|
|
|
.map_err(|e| ErrorKind::TorConfig(format!("{:?}", e).into()))?;
|
|
|
|
warn!(
|
|
|
|
"Starting TOR Hidden Service for API listener at address {}, binding to {}",
|
|
|
|
onion_address, addr
|
|
|
|
);
|
|
|
|
tor_config::output_tor_listener_config(&tor_dir, addr, &vec![sec_key])
|
|
|
|
.map_err(|e| ErrorKind::TorConfig(format!("{:?}", e).into()))?;
|
|
|
|
// Start TOR process
|
|
|
|
process
|
|
|
|
.torrc_path(&format!("{}/torrc", tor_dir))
|
|
|
|
.working_dir(&tor_dir)
|
|
|
|
.timeout(20)
|
|
|
|
.completion_percent(100)
|
|
|
|
.launch()
|
|
|
|
.map_err(|e| ErrorKind::TorProcess(format!("{:?}", e).into()))?;
|
|
|
|
Ok(process)
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
/// Instantiate wallet Owner API for a single-use (command line) call
|
|
|
|
/// Return a function containing a loaded API context to call
|
2019-11-18 13:49:51 +03:00
|
|
|
pub fn owner_single_use<L, F, C, K>(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K>>>>,
|
2019-08-06 14:50:41 +03:00
|
|
|
keychain_mask: Option<&SecretKey>,
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
f: F,
|
|
|
|
) -> Result<(), Error>
|
2019-02-13 18:05:19 +03:00
|
|
|
where
|
2019-11-18 13:49:51 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
|
|
|
F: FnOnce(&mut Owner<L, C, K>, Option<&SecretKey>) -> Result<(), Error>,
|
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
2019-02-13 18:05:19 +03:00
|
|
|
{
|
2019-08-06 14:50:41 +03:00
|
|
|
f(&mut Owner::new(wallet), keychain_mask)?;
|
2019-02-13 18:05:19 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Instantiate wallet Foreign API for a single-use (command line) call
|
|
|
|
/// Return a function containing a loaded API context to call
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub fn foreign_single_use<'a, L, F, C, K>(
|
2019-08-06 14:50:41 +03:00
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
|
|
|
|
keychain_mask: Option<SecretKey>,
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
f: F,
|
|
|
|
) -> Result<(), Error>
|
2019-02-13 18:05:19 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'a, C, K>,
|
|
|
|
F: FnOnce(&mut Foreign<'a, L, C, K>) -> Result<(), Error>,
|
|
|
|
C: NodeClient + 'a,
|
|
|
|
K: Keychain + 'a,
|
2019-02-13 18:05:19 +03:00
|
|
|
{
|
2019-08-06 14:50:41 +03:00
|
|
|
f(&mut Foreign::new(
|
|
|
|
wallet,
|
|
|
|
keychain_mask,
|
|
|
|
Some(check_middleware),
|
|
|
|
))?;
|
2019-02-13 18:05:19 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Listener version, providing same API but listening for requests on a
|
|
|
|
/// port and wrapping the calls
|
2019-08-06 14:50:41 +03:00
|
|
|
/// Note keychain mask is only provided here in case the foreign listener is also being used
|
|
|
|
/// in the same wallet instance
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub fn owner_listener<L, C, K>(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-09-02 18:03:35 +03:00
|
|
|
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
2019-02-13 18:05:19 +03:00
|
|
|
addr: &str,
|
|
|
|
api_secret: Option<String>,
|
|
|
|
tls_config: Option<TLSConfig>,
|
|
|
|
owner_api_include_foreign: Option<bool>,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-02-13 18:05:19 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
let mut router = Router::new();
|
|
|
|
if api_secret.is_some() {
|
|
|
|
let api_basic_auth =
|
|
|
|
"Basic ".to_string() + &to_base64(&("grin:".to_string() + &api_secret.unwrap()));
|
2019-04-03 16:22:30 +03:00
|
|
|
let basic_auth_middleware = Arc::new(BasicAuthMiddleware::new(
|
|
|
|
api_basic_auth,
|
|
|
|
&GRIN_OWNER_BASIC_REALM,
|
2019-09-12 15:18:15 +03:00
|
|
|
Some("/v2/foreign".into()),
|
2019-04-03 16:22:30 +03:00
|
|
|
));
|
2019-02-13 18:05:19 +03:00
|
|
|
router.add_middleware(basic_auth_middleware);
|
|
|
|
}
|
2019-09-02 18:03:35 +03:00
|
|
|
let mut running_foreign = false;
|
|
|
|
if owner_api_include_foreign.unwrap_or(false) {
|
|
|
|
running_foreign = true;
|
|
|
|
}
|
2019-04-01 13:16:49 +03:00
|
|
|
|
2019-08-13 11:36:21 +03:00
|
|
|
let api_handler_v2 = OwnerAPIHandlerV2::new(wallet.clone());
|
2019-09-02 18:03:35 +03:00
|
|
|
let api_handler_v3 =
|
|
|
|
OwnerAPIHandlerV3::new(wallet.clone(), keychain_mask.clone(), running_foreign);
|
2019-08-13 11:36:21 +03:00
|
|
|
|
2019-04-01 13:16:49 +03:00
|
|
|
router
|
|
|
|
.add_route("/v2/owner", Arc::new(api_handler_v2))
|
|
|
|
.map_err(|_| ErrorKind::GenericError("Router failed to add route".to_string()))?;
|
|
|
|
|
2019-08-13 11:36:21 +03:00
|
|
|
router
|
|
|
|
.add_route("/v3/owner", Arc::new(api_handler_v3))
|
|
|
|
.map_err(|_| ErrorKind::GenericError("Router failed to add route".to_string()))?;
|
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
// If so configured, add the foreign API to the same port
|
2019-09-02 18:03:35 +03:00
|
|
|
if running_foreign {
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
warn!("Starting HTTP Foreign API on Owner server at {}.", addr);
|
2019-08-06 14:50:41 +03:00
|
|
|
let foreign_api_handler_v2 = ForeignAPIHandlerV2::new(wallet, keychain_mask);
|
2019-04-01 13:16:49 +03:00
|
|
|
router
|
|
|
|
.add_route("/v2/foreign", Arc::new(foreign_api_handler_v2))
|
|
|
|
.map_err(|_| ErrorKind::GenericError("Router failed to add route".to_string()))?;
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut apis = ApiServer::new();
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
warn!("Starting HTTP Owner API server at {}.", addr);
|
2019-02-13 18:05:19 +03:00
|
|
|
let socket_addr: SocketAddr = addr.parse().expect("unable to parse socket address");
|
|
|
|
let api_thread =
|
|
|
|
apis.start(socket_addr, router, tls_config)
|
|
|
|
.context(ErrorKind::GenericError(
|
|
|
|
"API thread failed to start".to_string(),
|
|
|
|
))?;
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
warn!("HTTP Owner listener started.");
|
2019-02-13 18:05:19 +03:00
|
|
|
api_thread
|
|
|
|
.join()
|
|
|
|
.map_err(|e| ErrorKind::GenericError(format!("API thread panicked :{:?}", e)).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Listener version, providing same API but listening for requests on a
|
|
|
|
/// port and wrapping the calls
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub fn foreign_listener<L, C, K>(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-09-02 18:03:35 +03:00
|
|
|
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
2019-02-13 18:05:19 +03:00
|
|
|
addr: &str,
|
|
|
|
tls_config: Option<TLSConfig>,
|
2019-10-14 22:24:09 +03:00
|
|
|
use_tor: bool,
|
2019-02-13 18:05:19 +03:00
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-02-13 18:05:19 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
2019-10-14 22:24:09 +03:00
|
|
|
// need to keep in scope while the main listener is running
|
|
|
|
let _tor_process = match use_tor {
|
|
|
|
true => match init_tor_listener(wallet.clone(), keychain_mask.clone(), addr) {
|
|
|
|
Ok(tp) => Some(tp),
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Unable to start TOR listener; Check that TOR executable is installed and on your path");
|
|
|
|
warn!("Tor Error: {}", e);
|
|
|
|
warn!("Listener will be available via HTTP only");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
false => None,
|
|
|
|
};
|
2019-02-13 18:05:19 +03:00
|
|
|
|
2019-10-14 22:24:09 +03:00
|
|
|
let api_handler_v2 = ForeignAPIHandlerV2::new(wallet, keychain_mask);
|
2019-02-13 18:05:19 +03:00
|
|
|
let mut router = Router::new();
|
|
|
|
|
2019-04-01 13:16:49 +03:00
|
|
|
router
|
|
|
|
.add_route("/v2/foreign", Arc::new(api_handler_v2))
|
|
|
|
.map_err(|_| ErrorKind::GenericError("Router failed to add route".to_string()))?;
|
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
let mut apis = ApiServer::new();
|
|
|
|
warn!("Starting HTTP Foreign listener API server at {}.", addr);
|
|
|
|
let socket_addr: SocketAddr = addr.parse().expect("unable to parse socket address");
|
|
|
|
let api_thread =
|
|
|
|
apis.start(socket_addr, router, tls_config)
|
|
|
|
.context(ErrorKind::GenericError(
|
|
|
|
"API thread failed to start".to_string(),
|
|
|
|
))?;
|
2019-10-14 22:24:09 +03:00
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
warn!("HTTP Foreign listener started.");
|
|
|
|
|
|
|
|
api_thread
|
|
|
|
.join()
|
|
|
|
.map_err(|e| ErrorKind::GenericError(format!("API thread panicked :{:?}", e)).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
type WalletResponseFuture = Box<dyn Future<Item = Response<Body>, Error = Error> + Send>;
|
|
|
|
|
2019-04-01 13:16:49 +03:00
|
|
|
/// V2 API Handler/Wrapper for owner functions
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub struct OwnerAPIHandlerV2<L, C, K>
|
2019-04-01 13:16:49 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-04-01 13:16:49 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
/// Wallet instance
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-04-01 13:16:49 +03:00
|
|
|
}
|
|
|
|
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
impl<L, C, K> OwnerAPIHandlerV2<L, C, K>
|
2019-04-01 13:16:49 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K>,
|
2019-04-01 13:16:49 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
/// Create a new owner API handler for GET methods
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub fn new(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
|
|
|
) -> OwnerAPIHandlerV2<L, C, K> {
|
|
|
|
OwnerAPIHandlerV2 { wallet }
|
2019-04-01 13:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call_api(
|
|
|
|
&self,
|
|
|
|
req: Request<Body>,
|
2019-11-18 13:49:51 +03:00
|
|
|
api: Owner<L, C, K>,
|
2019-04-01 13:16:49 +03:00
|
|
|
) -> Box<dyn Future<Item = serde_json::Value, Error = Error> + Send> {
|
|
|
|
Box::new(parse_body(req).and_then(move |val: serde_json::Value| {
|
|
|
|
let owner_api = &api as &dyn OwnerRpc;
|
|
|
|
match owner_api.handle_request(val) {
|
2019-04-24 11:26:52 +03:00
|
|
|
MaybeReply::Reply(r) => ok(r),
|
|
|
|
MaybeReply::DontReply => {
|
|
|
|
// Since it's http, we need to return something. We return [] because jsonrpc
|
|
|
|
// clients will parse it as an empty batch response.
|
|
|
|
ok(serde_json::json!([]))
|
2019-04-01 13:16:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_post_request(&self, req: Request<Body>) -> WalletResponseFuture {
|
|
|
|
let api = Owner::new(self.wallet.clone());
|
|
|
|
Box::new(
|
|
|
|
self.call_api(req, api)
|
|
|
|
.and_then(|resp| ok(json_response_pretty(&resp))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
impl<L, C, K> api::Handler for OwnerAPIHandlerV2<L, C, K>
|
2019-04-01 13:16:49 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-04-01 13:16:49 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
fn post(&self, req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(
|
|
|
|
self.handle_post_request(req)
|
|
|
|
.and_then(|r| ok(r))
|
|
|
|
.or_else(|e| {
|
|
|
|
error!("Request Error: {:?}", e);
|
|
|
|
ok(create_error_response(e))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn options(&self, _req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(ok(create_ok_response("{}")))
|
|
|
|
}
|
|
|
|
}
|
2019-02-13 18:05:19 +03:00
|
|
|
|
2019-08-13 11:36:21 +03:00
|
|
|
/// V3 API Handler/Wrapper for owner functions, which include a secure
|
|
|
|
/// mode + lifecycle functions
|
|
|
|
pub struct OwnerAPIHandlerV3<L, C, K>
|
|
|
|
where
|
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
/// Wallet instance
|
|
|
|
pub wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-09-02 18:03:35 +03:00
|
|
|
|
2019-11-18 13:49:51 +03:00
|
|
|
/// Handle to Owner API
|
|
|
|
owner_api: Arc<Owner<L, C, K>>,
|
|
|
|
|
2019-09-02 18:03:35 +03:00
|
|
|
/// ECDH shared key
|
|
|
|
pub shared_key: Arc<Mutex<Option<SecretKey>>>,
|
|
|
|
|
|
|
|
/// Keychain mask (to change if also running the foreign API)
|
|
|
|
pub keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
|
|
|
|
|
|
|
/// Whether we're running the foreign API on the same port, and therefore
|
|
|
|
/// have to store the mask in-process
|
|
|
|
pub running_foreign: bool,
|
2019-08-13 11:36:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-19 15:05:21 +03:00
|
|
|
pub struct OwnerV3Helpers;
|
|
|
|
|
|
|
|
impl OwnerV3Helpers {
|
|
|
|
/// Checks whether a request is to init the secure API
|
|
|
|
pub fn is_init_secure_api(val: &serde_json::Value) -> bool {
|
|
|
|
if let Some(m) = val["method"].as_str() {
|
|
|
|
match m {
|
|
|
|
"init_secure_api" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:03:35 +03:00
|
|
|
/// Checks whether a request is to open the wallet
|
|
|
|
pub fn is_open_wallet(val: &serde_json::Value) -> bool {
|
|
|
|
if let Some(m) = val["method"].as_str() {
|
|
|
|
match m {
|
|
|
|
"open_wallet" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:05:21 +03:00
|
|
|
/// Checks whether a request is an encrypted request
|
|
|
|
pub fn is_encrypted_request(val: &serde_json::Value) -> bool {
|
|
|
|
if let Some(m) = val["method"].as_str() {
|
|
|
|
match m {
|
|
|
|
"encrypted_request_v3" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// whether encryption is enabled
|
2019-09-02 18:03:35 +03:00
|
|
|
pub fn encryption_enabled(key: Arc<Mutex<Option<SecretKey>>>) -> bool {
|
|
|
|
let share_key_ref = key.lock();
|
2019-08-19 15:05:21 +03:00
|
|
|
share_key_ref.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If incoming is an encrypted request, check there is a shared key,
|
|
|
|
/// Otherwise return an error value
|
2019-09-02 18:03:35 +03:00
|
|
|
pub fn check_encryption_started(
|
|
|
|
key: Arc<Mutex<Option<SecretKey>>>,
|
|
|
|
) -> Result<(), serde_json::Value> {
|
|
|
|
match OwnerV3Helpers::encryption_enabled(key) {
|
2019-08-19 15:05:21 +03:00
|
|
|
true => Ok(()),
|
|
|
|
false => Err(EncryptionErrorResponse::new(
|
|
|
|
1,
|
|
|
|
-32001,
|
|
|
|
"Encryption must be enabled. Please call 'init_secure_api` first",
|
|
|
|
)
|
|
|
|
.as_json_value()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the statically held owner API shared key
|
2019-09-02 18:03:35 +03:00
|
|
|
pub fn update_owner_api_shared_key(
|
|
|
|
key: Arc<Mutex<Option<SecretKey>>>,
|
|
|
|
val: &serde_json::Value,
|
|
|
|
new_key: Option<SecretKey>,
|
|
|
|
) {
|
2019-08-19 15:05:21 +03:00
|
|
|
if let Some(_) = val["result"]["Ok"].as_str() {
|
2019-09-02 18:03:35 +03:00
|
|
|
let mut share_key_ref = key.lock();
|
2019-08-19 15:05:21 +03:00
|
|
|
*share_key_ref = new_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:03:35 +03:00
|
|
|
/// Update the shared mask, in case of foreign API being run
|
|
|
|
pub fn update_mask(mask: Arc<Mutex<Option<SecretKey>>>, val: &serde_json::Value) {
|
|
|
|
if let Some(key) = val["result"]["Ok"].as_str() {
|
|
|
|
let key_bytes = match from_hex(key.to_owned()) {
|
|
|
|
Ok(k) => k,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
let secp_inst = static_secp_instance();
|
|
|
|
let secp = secp_inst.lock();
|
|
|
|
let sk = match SecretKey::from_slice(&secp, &key_bytes) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut shared_mask_ref = mask.lock();
|
|
|
|
*shared_mask_ref = Some(sk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:05:21 +03:00
|
|
|
/// Decrypt an encrypted request
|
|
|
|
pub fn decrypt_request(
|
2019-09-02 18:03:35 +03:00
|
|
|
key: Arc<Mutex<Option<SecretKey>>>,
|
2019-08-19 15:05:21 +03:00
|
|
|
req: &serde_json::Value,
|
|
|
|
) -> Result<(u32, serde_json::Value), serde_json::Value> {
|
2019-09-02 18:03:35 +03:00
|
|
|
let share_key_ref = key.lock();
|
2019-08-19 15:05:21 +03:00
|
|
|
let shared_key = share_key_ref.as_ref().unwrap();
|
|
|
|
let enc_req: EncryptedRequest = serde_json::from_value(req.clone()).map_err(|e| {
|
|
|
|
EncryptionErrorResponse::new(
|
|
|
|
1,
|
|
|
|
-32002,
|
|
|
|
&format!("Encrypted request format error: {}", e),
|
|
|
|
)
|
|
|
|
.as_json_value()
|
|
|
|
})?;
|
|
|
|
let id = enc_req.id;
|
|
|
|
let res = enc_req.decrypt(&shared_key).map_err(|e| {
|
|
|
|
EncryptionErrorResponse::new(1, -32002, &format!("Decryption error: {}", e.kind()))
|
|
|
|
.as_json_value()
|
|
|
|
})?;
|
|
|
|
Ok((id, res))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encrypt a response
|
|
|
|
pub fn encrypt_response(
|
2019-09-02 18:03:35 +03:00
|
|
|
key: Arc<Mutex<Option<SecretKey>>>,
|
2019-08-19 15:05:21 +03:00
|
|
|
id: u32,
|
|
|
|
res: &serde_json::Value,
|
|
|
|
) -> Result<serde_json::Value, serde_json::Value> {
|
2019-09-02 18:03:35 +03:00
|
|
|
let share_key_ref = key.lock();
|
2019-08-19 15:05:21 +03:00
|
|
|
let shared_key = share_key_ref.as_ref().unwrap();
|
|
|
|
let enc_res = EncryptedResponse::from_json(id, res, &shared_key).map_err(|e| {
|
|
|
|
EncryptionErrorResponse::new(1, -32003, &format!("EncryptionError: {}", e.kind()))
|
|
|
|
.as_json_value()
|
|
|
|
})?;
|
|
|
|
let res = enc_res.as_json_value().map_err(|e| {
|
|
|
|
EncryptionErrorResponse::new(
|
|
|
|
1,
|
|
|
|
-32002,
|
|
|
|
&format!("Encrypted response format error: {}", e),
|
|
|
|
)
|
|
|
|
.as_json_value()
|
|
|
|
})?;
|
|
|
|
Ok(res)
|
|
|
|
}
|
2019-09-02 18:03:35 +03:00
|
|
|
|
|
|
|
/// convert an internal error (if exists) as proper JSON-RPC
|
|
|
|
pub fn check_error_response(val: &serde_json::Value) -> (bool, serde_json::Value) {
|
|
|
|
// check for string first. This ensures that error messages
|
|
|
|
// that are just strings aren't given weird formatting
|
|
|
|
let err_string = if val["result"]["Err"].is_object() {
|
|
|
|
let mut retval;
|
|
|
|
let hashed: Result<HashMap<String, String>, serde_json::Error> =
|
|
|
|
serde_json::from_value(val["result"]["Err"].clone());
|
|
|
|
retval = match hashed {
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Can't cast value to Hashmap<String> {}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Ok(h) => {
|
|
|
|
let mut r = "".to_owned();
|
|
|
|
for (k, v) in h.iter() {
|
|
|
|
r = format!("{}: {}", k, v);
|
|
|
|
}
|
|
|
|
Some(r)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Otherwise, see if error message is a map that needs
|
|
|
|
// to be stringified (and accept weird formatting)
|
|
|
|
if retval.is_none() {
|
|
|
|
let hashed: Result<HashMap<String, serde_json::Value>, serde_json::Error> =
|
|
|
|
serde_json::from_value(val["result"]["Err"].clone());
|
|
|
|
retval = match hashed {
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Can't cast value to Hashmap<Value> {}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Ok(h) => {
|
|
|
|
let mut r = "".to_owned();
|
|
|
|
for (k, v) in h.iter() {
|
|
|
|
r = format!("{}: {}", k, v);
|
|
|
|
}
|
|
|
|
Some(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retval
|
|
|
|
} else if val["result"]["Err"].is_string() {
|
|
|
|
let parsed = serde_json::from_value::<String>(val["result"]["Err"].clone());
|
|
|
|
match parsed {
|
|
|
|
Ok(p) => Some(p),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
match err_string {
|
|
|
|
Some(s) => {
|
|
|
|
return (
|
|
|
|
true,
|
|
|
|
serde_json::json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": val["id"],
|
|
|
|
"error": {
|
|
|
|
"message": s,
|
|
|
|
"code": -32099
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => (false, val.clone()),
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 15:05:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 11:36:21 +03:00
|
|
|
impl<L, C, K> OwnerAPIHandlerV3<L, C, K>
|
|
|
|
where
|
|
|
|
L: WalletLCProvider<'static, C, K>,
|
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
/// Create a new owner API handler for GET methods
|
|
|
|
pub fn new(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-09-02 18:03:35 +03:00
|
|
|
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
|
|
|
running_foreign: bool,
|
2019-08-13 11:36:21 +03:00
|
|
|
) -> OwnerAPIHandlerV3<L, C, K> {
|
2019-11-18 13:49:51 +03:00
|
|
|
let owner_api = Arc::new(Owner::new(wallet.clone()));
|
2019-09-02 18:03:35 +03:00
|
|
|
OwnerAPIHandlerV3 {
|
|
|
|
wallet,
|
2019-11-18 13:49:51 +03:00
|
|
|
owner_api,
|
2019-09-02 18:03:35 +03:00
|
|
|
shared_key: Arc::new(Mutex::new(None)),
|
|
|
|
keychain_mask: keychain_mask,
|
|
|
|
running_foreign,
|
|
|
|
}
|
2019-08-13 11:36:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call_api(
|
|
|
|
&self,
|
|
|
|
req: Request<Body>,
|
2019-11-18 13:49:51 +03:00
|
|
|
api: Arc<Owner<L, C, K>>,
|
2019-08-13 11:36:21 +03:00
|
|
|
) -> Box<dyn Future<Item = serde_json::Value, Error = Error> + Send> {
|
2019-09-02 18:03:35 +03:00
|
|
|
let key = self.shared_key.clone();
|
|
|
|
let mask = self.keychain_mask.clone();
|
|
|
|
let running_foreign = self.running_foreign;
|
2019-08-13 11:36:21 +03:00
|
|
|
Box::new(parse_body(req).and_then(move |val: serde_json::Value| {
|
2019-08-19 15:05:21 +03:00
|
|
|
let mut val = val;
|
2019-11-18 13:49:51 +03:00
|
|
|
let owner_api_s = &*api as &dyn OwnerRpcS;
|
2019-08-19 15:05:21 +03:00
|
|
|
let mut is_init_secure_api = OwnerV3Helpers::is_init_secure_api(&val);
|
|
|
|
let mut was_encrypted = false;
|
|
|
|
let mut encrypted_req_id = 0;
|
|
|
|
if !is_init_secure_api {
|
2019-09-02 18:03:35 +03:00
|
|
|
if let Err(v) = OwnerV3Helpers::check_encryption_started(key.clone()) {
|
2019-08-19 15:05:21 +03:00
|
|
|
return ok(v);
|
|
|
|
}
|
2019-09-02 18:03:35 +03:00
|
|
|
let res = OwnerV3Helpers::decrypt_request(key.clone(), &val);
|
2019-08-19 15:05:21 +03:00
|
|
|
match res {
|
|
|
|
Err(e) => return ok(e),
|
|
|
|
Ok(v) => {
|
|
|
|
encrypted_req_id = v.0;
|
|
|
|
val = v.1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
was_encrypted = true;
|
|
|
|
}
|
|
|
|
// check again, in case it was an encrypted call to init_secure_api
|
|
|
|
is_init_secure_api = OwnerV3Helpers::is_init_secure_api(&val);
|
2019-09-02 18:03:35 +03:00
|
|
|
// also need to intercept open/close wallet requests
|
|
|
|
let is_open_wallet = OwnerV3Helpers::is_open_wallet(&val);
|
2019-08-13 11:36:21 +03:00
|
|
|
match owner_api_s.handle_request(val) {
|
2019-08-19 15:05:21 +03:00
|
|
|
MaybeReply::Reply(mut r) => {
|
2019-09-02 18:03:35 +03:00
|
|
|
let (_was_error, unencrypted_intercept) =
|
|
|
|
OwnerV3Helpers::check_error_response(&r.clone());
|
|
|
|
if is_open_wallet && running_foreign {
|
|
|
|
OwnerV3Helpers::update_mask(mask, &r.clone());
|
|
|
|
}
|
2019-08-19 15:05:21 +03:00
|
|
|
if was_encrypted {
|
2019-09-02 18:03:35 +03:00
|
|
|
let res = OwnerV3Helpers::encrypt_response(
|
|
|
|
key.clone(),
|
|
|
|
encrypted_req_id,
|
|
|
|
&unencrypted_intercept,
|
|
|
|
);
|
2019-08-19 15:05:21 +03:00
|
|
|
r = match res {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(v) => return ok(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// intercept init_secure_api response (after encryption,
|
|
|
|
// in case it was an encrypted call to 'init_api_secure')
|
|
|
|
if is_init_secure_api {
|
|
|
|
OwnerV3Helpers::update_owner_api_shared_key(
|
2019-09-02 18:03:35 +03:00
|
|
|
key.clone(),
|
2019-08-19 15:05:21 +03:00
|
|
|
&unencrypted_intercept,
|
|
|
|
api.shared_key.lock().clone(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ok(r)
|
|
|
|
}
|
2019-08-13 11:36:21 +03:00
|
|
|
MaybeReply::DontReply => {
|
|
|
|
// Since it's http, we need to return something. We return [] because jsonrpc
|
|
|
|
// clients will parse it as an empty batch response.
|
|
|
|
ok(serde_json::json!([]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_post_request(&self, req: Request<Body>) -> WalletResponseFuture {
|
|
|
|
Box::new(
|
2019-11-18 13:49:51 +03:00
|
|
|
self.call_api(req, self.owner_api.clone())
|
2019-08-13 11:36:21 +03:00
|
|
|
.and_then(|resp| ok(json_response_pretty(&resp))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<L, C, K> api::Handler for OwnerAPIHandlerV3<L, C, K>
|
|
|
|
where
|
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
fn post(&self, req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(
|
|
|
|
self.handle_post_request(req)
|
|
|
|
.and_then(|r| ok(r))
|
|
|
|
.or_else(|e| {
|
|
|
|
error!("Request Error: {:?}", e);
|
|
|
|
ok(create_error_response(e))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn options(&self, _req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(ok(create_ok_response("{}")))
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 13:16:49 +03:00
|
|
|
/// V2 API Handler/Wrapper for foreign functions
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub struct ForeignAPIHandlerV2<L, C, K>
|
2019-04-01 13:16:49 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-04-01 13:16:49 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
/// Wallet instance
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-08-06 14:50:41 +03:00
|
|
|
/// Keychain mask
|
2019-09-02 18:03:35 +03:00
|
|
|
pub keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
2019-04-01 13:16:49 +03:00
|
|
|
}
|
|
|
|
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
impl<L, C, K> ForeignAPIHandlerV2<L, C, K>
|
2019-04-01 13:16:49 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-04-01 13:16:49 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
/// Create a new foreign API handler for GET methods
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
pub fn new(
|
|
|
|
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
|
2019-09-02 18:03:35 +03:00
|
|
|
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
) -> ForeignAPIHandlerV2<L, C, K> {
|
2019-08-06 14:50:41 +03:00
|
|
|
ForeignAPIHandlerV2 {
|
|
|
|
wallet,
|
|
|
|
keychain_mask,
|
|
|
|
}
|
2019-04-01 13:16:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call_api(
|
|
|
|
&self,
|
|
|
|
req: Request<Body>,
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
api: Foreign<'static, L, C, K>,
|
2019-04-01 13:16:49 +03:00
|
|
|
) -> Box<dyn Future<Item = serde_json::Value, Error = Error> + Send> {
|
|
|
|
Box::new(parse_body(req).and_then(move |val: serde_json::Value| {
|
|
|
|
let foreign_api = &api as &dyn ForeignRpc;
|
|
|
|
match foreign_api.handle_request(val) {
|
2019-10-14 22:24:09 +03:00
|
|
|
MaybeReply::Reply(r) => ok({ r }),
|
2019-04-24 11:26:52 +03:00
|
|
|
MaybeReply::DontReply => {
|
|
|
|
// Since it's http, we need to return something. We return [] because jsonrpc
|
|
|
|
// clients will parse it as an empty batch response.
|
|
|
|
ok(serde_json::json!([]))
|
2019-04-01 13:16:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_post_request(&self, req: Request<Body>) -> WalletResponseFuture {
|
2019-09-02 18:03:35 +03:00
|
|
|
let mask = self.keychain_mask.lock();
|
|
|
|
let api = Foreign::new(self.wallet.clone(), mask.clone(), Some(check_middleware));
|
2019-04-01 13:16:49 +03:00
|
|
|
Box::new(
|
|
|
|
self.call_api(req, api)
|
|
|
|
.and_then(|resp| ok(json_response_pretty(&resp))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
impl<L, C, K> api::Handler for ForeignAPIHandlerV2<L, C, K>
|
2019-04-01 13:16:49 +03:00
|
|
|
where
|
Merge milestone/2.1.0 into master (#199)
* version bump for next potential release
* Merge master into milestone/2.1.0 (#182)
* Derive --version output dynamically from cargo package version (#174)
* add --txid to the `wallet txs` command (#176)
* add --txid to the `wallet txs` command
* add test for `wallet txs` command with `--txid` parameter
* Refactor - Split WalletCommAdapter into multiple traits (#180)
* Derive --version output dynamically from cargo package version (#174)
* add server auth argument to http client
* Revert "add server auth argument to http client"
This reverts commit f52a8d2c7cdfb8583af5716ad621eb560811d6ee.
* modify WalletCommAdapter, moving dest argument into fields on implementors,
visiting havok on automated tests, at least one of which is now out of date and failing
* Split WalletCommAdapter into four traits, one for each of its intended behaviors.
* Remove two vestigals
1. args, a stringly typed argument to put_tx
2. NullAdapter, which is no longer used
* Remove unused "params" argument from listen method.
* Re-add previously existing TODO comment
* Fix non-test build
* completely Fix non-test build
* Full Lifecycle API Support (#184)
* refactoring wallet lib traits
* rustfmt
* rustfmt
* add new files
* explicit lifetime specifiers on all wallet traits
* rustfmt
* modify apis to use new walletinst
* rustfmt
* converting controller crate
* rustfmt
* controller crate compiling
* rustfmt
* compilation
* rustfmt
* Remove config from wallet, implement open_wallet, close_wallet in lifecycle provider, remove password + open_with_credentials from WalletBackend + impl
* rustfmt
* full compilation, changing recovery + init to new model
* rustfmt
* wallet initialisation working, init command output and flow identical to v2.0.0 wallet
* rustfmt
* fix listener and owner api startup
* rustfmt
* rustfmt
* move encryption test
* rustfmt
* fix api doctests
* rustfmt
* fix for most tests in controller crate
* rustfmt
* fix for check tests in controller crate
* fix main wallet tests
* rustfmt
* add explicit functions to handle mnemonic recovery, fix CLI recovery workflow
* rustfmt
* update keybase adapter to use new wallet format
* rustfmt
* test fix
* remove debug output
2019-07-29 15:25:03 +03:00
|
|
|
L: WalletLCProvider<'static, C, K> + 'static,
|
2019-04-01 13:16:49 +03:00
|
|
|
C: NodeClient + 'static,
|
|
|
|
K: Keychain + 'static,
|
|
|
|
{
|
|
|
|
fn post(&self, req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(
|
|
|
|
self.handle_post_request(req)
|
|
|
|
.and_then(|r| ok(r))
|
|
|
|
.or_else(|e| {
|
|
|
|
error!("Request Error: {:?}", e);
|
|
|
|
ok(create_error_response(e))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn options(&self, _req: Request<Body>) -> ResponseFuture {
|
|
|
|
Box::new(ok(create_ok_response("{}")))
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 12:41:05 +03:00
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
// Utility to serialize a struct into JSON and produce a sensible Response
|
|
|
|
// out of it.
|
2019-06-27 12:41:05 +03:00
|
|
|
fn _json_response<T>(s: &T) -> Response<Body>
|
2019-02-13 18:05:19 +03:00
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
match serde_json::to_string(s) {
|
|
|
|
Ok(json) => 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
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
match serde_json::to_string_pretty(s) {
|
|
|
|
Ok(json) => response(StatusCode::OK, json),
|
|
|
|
Err(_) => response(StatusCode::INTERNAL_SERVER_ERROR, ""),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_error_response(e: Error) -> Response<Body> {
|
|
|
|
Response::builder()
|
|
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
.header("access-control-allow-origin", "*")
|
|
|
|
.header(
|
|
|
|
"access-control-allow-headers",
|
|
|
|
"Content-Type, Authorization",
|
|
|
|
)
|
|
|
|
.body(format!("{}", e).into())
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_ok_response(json: &str) -> Response<Body> {
|
|
|
|
Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header("access-control-allow-origin", "*")
|
|
|
|
.header(
|
|
|
|
"access-control-allow-headers",
|
|
|
|
"Content-Type, Authorization",
|
|
|
|
)
|
|
|
|
.header(hyper::header::CONTENT_TYPE, "application/json")
|
|
|
|
.body(json.to_string().into())
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a new hyper Response with the status code and body provided.
|
|
|
|
///
|
|
|
|
/// Whenever the status code is `StatusCode::OK` the text parameter should be
|
|
|
|
/// valid JSON as the content type header will be set to `application/json'
|
|
|
|
fn response<T: Into<Body>>(status: StatusCode, text: T) -> Response<Body> {
|
|
|
|
let mut builder = &mut Response::builder();
|
|
|
|
|
|
|
|
builder = builder
|
|
|
|
.status(status)
|
|
|
|
.header("access-control-allow-origin", "*")
|
|
|
|
.header(
|
|
|
|
"access-control-allow-headers",
|
|
|
|
"Content-Type, Authorization",
|
|
|
|
);
|
|
|
|
|
|
|
|
if status == StatusCode::OK {
|
|
|
|
builder = builder.header(hyper::header::CONTENT_TYPE, "application/json");
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.body(text.into()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_body<T>(req: Request<Body>) -> Box<dyn Future<Item = T, Error = Error> + Send>
|
|
|
|
where
|
|
|
|
for<'de> T: Deserialize<'de> + Send + 'static,
|
|
|
|
{
|
|
|
|
Box::new(
|
|
|
|
req.into_body()
|
|
|
|
.concat2()
|
|
|
|
.map_err(|_| ErrorKind::GenericError("Failed to read request".to_owned()).into())
|
2019-06-27 12:41:05 +03:00
|
|
|
.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())
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|