Resolving URLs from contract (#1964)

* Fetching dapp from github.

* Unpacking dapp

* Removing hardcodes

* Proper Host validation

* Randomizing paths

* Splitting into files

* Serving donwloaded apps from different path

* Extracting URLHint to separate module

* Whitespace and docs

* Resolving from URLHint contract

* Fixing test

* Resolving githubhint url from registrar

* Proper redirections

* Fixing test

* fixing ethstore [ci skip]

* Correct version of registrar

* Removing superfluous Box
This commit is contained in:
Tomasz Drwięga
2016-08-23 19:28:21 +02:00
committed by Gav Wood
parent dda57d9294
commit 124a5da75e
17 changed files with 434 additions and 101 deletions

View File

@@ -15,16 +15,11 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc;
use std::net::SocketAddr;
use io::PanicHandler;
use rpc_apis;
use ethcore::client::Client;
use helpers::replace_home;
#[cfg(feature = "dapps")]
pub use ethcore_dapps::Server as WebappServer;
#[cfg(not(feature = "dapps"))]
pub struct WebappServer;
#[derive(Debug, PartialEq, Clone)]
pub struct Configuration {
pub enabled: bool,
@@ -51,6 +46,7 @@ impl Default for Configuration {
pub struct Dependencies {
pub panic_handler: Arc<PanicHandler>,
pub apis: Arc<rpc_apis::Dependencies>,
pub client: Arc<Client>,
}
pub fn new(configuration: Configuration, deps: Dependencies) -> Result<Option<WebappServer>, String> {
@@ -75,45 +71,102 @@ pub fn new(configuration: Configuration, deps: Dependencies) -> Result<Option<We
Ok(Some(try!(setup_dapps_server(deps, configuration.dapps_path, &addr, auth))))
}
pub use self::server::WebappServer;
pub use self::server::setup_dapps_server;
#[cfg(not(feature = "dapps"))]
pub fn setup_dapps_server(
_deps: Dependencies,
_dapps_path: String,
_url: &SocketAddr,
_auth: Option<(String, String)>,
) -> Result<WebappServer, String> {
Err("Your Parity version has been compiled without WebApps support.".into())
}
mod server {
use super::Dependencies;
use std::net::SocketAddr;
#[cfg(feature = "dapps")]
pub fn setup_dapps_server(
deps: Dependencies,
dapps_path: String,
url: &SocketAddr,
auth: Option<(String, String)>
) -> Result<WebappServer, String> {
use ethcore_dapps as dapps;
let server = dapps::ServerBuilder::new(dapps_path);
let server = rpc_apis::setup_rpc(server, deps.apis.clone(), rpc_apis::ApiSet::UnsafeContext);
let start_result = match auth {
None => {
server.start_unsecure_http(url)
},
Some((username, password)) => {
server.start_basic_auth_http(url, &username, &password)
},
};
match start_result {
Err(dapps::ServerError::IoError(err)) => Err(format!("WebApps io error: {}", err)),
Err(e) => Err(format!("WebApps error: {:?}", e)),
Ok(server) => {
server.set_panic_handler(move || {
deps.panic_handler.notify_all("Panic in WebApp thread.".to_owned());
});
Ok(server)
},
pub struct WebappServer;
pub fn setup_dapps_server(
_deps: Dependencies,
_dapps_path: String,
_url: &SocketAddr,
_auth: Option<(String, String)>,
) -> Result<WebappServer, String> {
Err("Your Parity version has been compiled without WebApps support.".into())
}
}
#[cfg(feature = "dapps")]
mod server {
use super::Dependencies;
use std::sync::Arc;
use std::net::SocketAddr;
use util::{Bytes, Address, U256};
use ethcore::transaction::{Transaction, Action};
use ethcore::client::{Client, BlockChainClient, BlockID};
use rpc_apis;
use ethcore_dapps::ContractClient;
pub use ethcore_dapps::Server as WebappServer;
pub fn setup_dapps_server(
deps: Dependencies,
dapps_path: String,
url: &SocketAddr,
auth: Option<(String, String)>
) -> Result<WebappServer, String> {
use ethcore_dapps as dapps;
let server = dapps::ServerBuilder::new(dapps_path, Arc::new(Registrar {
client: deps.client.clone(),
}));
let server = rpc_apis::setup_rpc(server, deps.apis.clone(), rpc_apis::ApiSet::UnsafeContext);
let start_result = match auth {
None => {
server.start_unsecure_http(url)
},
Some((username, password)) => {
server.start_basic_auth_http(url, &username, &password)
},
};
match start_result {
Err(dapps::ServerError::IoError(err)) => Err(format!("WebApps io error: {}", err)),
Err(e) => Err(format!("WebApps error: {:?}", e)),
Ok(server) => {
server.set_panic_handler(move || {
deps.panic_handler.notify_all("Panic in WebApp thread.".to_owned());
});
Ok(server)
},
}
}
struct Registrar {
client: Arc<Client>,
}
impl ContractClient for Registrar {
fn registrar(&self) -> Result<Address, String> {
self.client.additional_params().get("registrar")
.ok_or_else(|| "Registrar not defined.".into())
.and_then(|registrar| {
registrar.parse().map_err(|e| format!("Invalid registrar address: {:?}", e))
})
}
fn call(&self, address: Address, data: Bytes) -> Result<Bytes, String> {
let from = Address::default();
let transaction = Transaction {
nonce: self.client.latest_nonce(&from),
action: Action::Call(address),
gas: U256::from(50_000_000),
gas_price: U256::default(),
value: U256::default(),
data: data,
}.fake_sign(from);
self.client.call(&transaction, BlockID::Latest, Default::default())
.map_err(|e| format!("{:?}", e))
.map(|executed| {
executed.output
})
}
}
}

View File

@@ -223,6 +223,7 @@ pub fn execute(cmd: RunCmd) -> Result<(), String> {
let dapps_deps = dapps::Dependencies {
panic_handler: panic_handler.clone(),
apis: deps_for_rpc_apis.clone(),
client: client.clone(),
};
// start dapps server