-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting CRS from host via HTTP with CRS hash in header (#39)
- Loading branch information
Showing
10 changed files
with
135 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use dusk_wallet::RuskHttpClient; | ||
use moat_core::{CrsGetter, Error}; | ||
use toml_base_config::BaseConfig; | ||
use tracing::trace; | ||
use wallet_accessor::BlockchainAccessConfig; | ||
|
||
const MIN_CRS_SIZE: usize = 10 * 1024 * 1024; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
#[cfg_attr(not(feature = "int_tests"), ignore)] | ||
async fn get_crs() -> Result<(), Error> { | ||
let config_path = | ||
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/config.toml"); | ||
|
||
let cfg = BlockchainAccessConfig::load_path(config_path)?; | ||
|
||
let client = RuskHttpClient::new(cfg.rusk_address); | ||
|
||
let crs = CrsGetter::get_crs(&client).await?; | ||
|
||
assert!(crs.len() >= MIN_CRS_SIZE); | ||
|
||
trace!("crs={}...", hex::encode(&crs[0..64])); | ||
trace!("crs length={}", crs.len()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
mod get_crs; | ||
mod retrieve_txs; | ||
mod stake_add_owner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use crate::Error; | ||
use dusk_wallet::{RuskHttpClient, RuskRequest}; | ||
use reqwest::Response; | ||
use sha2::{Digest, Sha256}; | ||
|
||
pub struct CrsGetter; | ||
|
||
type CRSHash = [u8; 32]; | ||
const CRS_HASH_HEADER: &str = "crs-hash"; | ||
|
||
impl CrsGetter { | ||
pub async fn get_crs(client: &RuskHttpClient) -> Result<Vec<u8>, Error> { | ||
let crs_request = RuskRequest::new("crs", vec![]); | ||
let result = client.call_raw(2, "rusk", &crs_request, false).await; | ||
match result { | ||
Ok(response) => { | ||
let received_hash = Self::hash_from_header(&response)?; | ||
let crs = response.bytes().await?; | ||
let this_hash = Self::hash_of_bytes(crs.as_ref()); | ||
if received_hash != this_hash { | ||
return Err(Error::CRS(Box::from("corrupted CRS"))); | ||
} | ||
Ok(crs.to_vec()) | ||
} | ||
Err(err) => Err(err.into()), | ||
} | ||
} | ||
|
||
fn hash_from_header(response: &Response) -> Result<CRSHash, Error> { | ||
let crs_hash = response | ||
.headers() | ||
.get(CRS_HASH_HEADER) | ||
.ok_or(Error::CRS(Box::from("missing CRS hash header")))?; | ||
let crs_hash = crs_hash.to_str().map_err(|_| { | ||
Error::CRS(Box::from("failed CRS hash header string conversion")) | ||
})?; | ||
let mut h = CRSHash::default(); | ||
hex::decode_to_slice(crs_hash, h.as_mut_slice())?; | ||
Ok(h) | ||
} | ||
|
||
fn hash_of_bytes<T: AsRef<[u8]>>(bytes: T) -> CRSHash { | ||
let mut hasher = Sha256::new(); | ||
hasher.update(bytes.as_ref()); | ||
hasher.finalize().into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters