Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions crates/humanode-peer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ hex-literal = "0.3"
qr2term = "0.2"
reqwest = "0.11"
sc-basic-authorship = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-chain-spec = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-chain-spec-derive = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-cli = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-client-api = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-consensus = { git = "https://github.com/humanode-network/substrate", branch = "master" }
Expand All @@ -38,6 +40,7 @@ sc-service = { git = "https://github.com/humanode-network/substrate", branch = "
sc-telemetry = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-tracing = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sc-transaction-pool = { git = "https://github.com/humanode-network/substrate", branch = "master" }
serde = { version = "1", features = ["derive"] }
sp-application-crypto = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sp-consensus = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sp-consensus-aura = { git = "https://github.com/humanode-network/substrate", branch = "master" }
Expand All @@ -51,3 +54,7 @@ thiserror = "1"
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
url = "2"

[dev-dependencies]
indoc = "1.0"
serde_json = "1"
88 changes: 85 additions & 3 deletions crates/humanode-peer/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use humanode_runtime::{
RobonodePublicKeyWrapper, Signature, SudoConfig, SystemConfig, WASM_BINARY,
};
use pallet_bioauth::StoredAuthTicket;
use sc_chain_spec_derive::{ChainSpecExtension, ChainSpecGroup};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::{
Expand All @@ -15,7 +17,20 @@ use sp_runtime::{
};

/// The concrete chain spec type we're using for the humanode network.
pub type ChainSpec = sc_service::GenericChainSpec<humanode_runtime::GenesisConfig>;
pub type ChainSpec = sc_service::GenericChainSpec<humanode_runtime::GenesisConfig, Extensions>;

/// Extensions for ChainSpec.
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension, Default,
)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct Extensions {
/// The URL of robonode to authenticate with.
pub robonode_url: Option<String>,
/// The Web App URL, necessary for printing the Web App QR Code.
pub webapp_url: Option<String>,
}

/// Generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
Expand Down Expand Up @@ -97,7 +112,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
// Properties
None,
// Extensions
None,
Extensions::default(),
))
}

Expand Down Expand Up @@ -146,7 +161,7 @@ pub fn development_config() -> Result<ChainSpec, String> {
// Properties
None,
// Extensions
None,
Extensions::default(),
))
}

Expand Down Expand Up @@ -193,3 +208,70 @@ fn testnet_genesis(
},
}
}

#[cfg(test)]
mod tests {
use indoc::indoc;

use super::*;

#[test]
fn deserialize_bioauth_flow_params_extensions() {
let expected = Extensions {
robonode_url: Some("dummy_robonode_url".into()),
webapp_url: Some("dummy_webapp_url".into()),
};
let value = serde_json::json!({
"robonodeUrl": "dummy_robonode_url",
"webappUrl": "dummy_webapp_url"
});

let sample: Extensions = serde_json::from_value(value).unwrap();

assert_eq!(sample, expected)
}

#[test]
fn deserialize_chain_spec() {
let chain_spec_file_content = indoc! {r#"
{
"name": "Local Testnet",
"id": "local_testnet",
"chainType": "Local",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"properties": null,
"robonodeUrl": "dummy_robonode_url",
"webappUrl": "dummy_webapp_url",
"consensusEngine": null,
"codeSubstitutes": {},
"genesis": {
"runtime": {
"system": { "changesTrieConfig": null, "code": "0x0" },
"aura": {
"authorities": []
},
"balances": {
"balances": []
},
"sudo": { "key": "0" },
"bioauth": {
"storedAuthTickets": [],
"robonodePublicKey": []
}
}
}
}
"#};
let bytes = chain_spec_file_content.as_bytes();
let sample: ChainSpec = ChainSpec::from_json_bytes(bytes).unwrap();

let expected = Extensions {
robonode_url: Some("dummy_robonode_url".into()),
webapp_url: Some("dummy_webapp_url".into()),
};

assert_eq!(sample.extensions().to_owned(), expected)
}
}
19 changes: 16 additions & 3 deletions crates/humanode-peer/src/cli/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! Machinery to populate the configuration from the CLI arguments.

use crate::configuration::{self, Configuration};
use sc_chain_spec::get_extension;

use crate::{
chain_spec::Extensions,
configuration::{self, Configuration},
};

use super::params;

Expand All @@ -18,6 +23,10 @@ pub trait CliConfigurationExt: SubstrateCliConfigurationProvider {
task_executor,
)?;

let extensions = get_extension::<Extensions>(substrate.chain_spec.extensions())
.cloned()
.unwrap_or_default();

let bioauth_flow = self.bioauth_params().map(|params| {
let rpc_url = params.rpc_url.clone().or_else(|| {
substrate
Expand All @@ -27,8 +36,12 @@ pub trait CliConfigurationExt: SubstrateCliConfigurationProvider {
});

configuration::BioauthFlow {
robonode_url: params.robonode_url.clone(),
webapp_url: Some(params.webapp_url.clone()),
robonode_url: params
.robonode_url
.clone()
.or(extensions.robonode_url)
.unwrap_or_else(|| "http://127.0.0.1:3033".into()),
webapp_url: params.webapp_url.clone().or(extensions.webapp_url),
rpc_url,
}
});
Expand Down
17 changes: 4 additions & 13 deletions crates/humanode-peer/src/cli/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@ use structopt::StructOpt;
pub struct BioauthFlowParams {
/// The URL to use for the web app.
/// Used to print the QR Code to the console, so it doesn't matter much.
#[structopt(
long,
value_name = "WEBAPP_URL",
default_value = "https://webapp-test-1.dev.humanode.io"
)]
pub webapp_url: String,
#[structopt(long, value_name = "WEBAPP_URL")]
pub webapp_url: Option<String>,

/// The URL to pass to the web app to connect to the node RPC.
/// If not passed, a URL with `localhost` and the HTTP RPC port will be used.
#[structopt(long, value_name = "RPC_URL")]
pub rpc_url: Option<String>,

/// The URL of robonode to authenticate with.
/// It should be a part of the genesis, but it isn't yet (TODO).
#[structopt(
long,
value_name = "ROBONODE_URL",
default_value = "http://127.0.0.1:3033"
)]
pub robonode_url: String,
#[structopt(long, value_name = "ROBONODE_URL")]
pub robonode_url: Option<String>,
}