Skip to content

Commit

Permalink
clippy & fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed May 23, 2024
1 parent c8137ab commit 6c6d765
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 78 deletions.
8 changes: 6 additions & 2 deletions tooling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ async fn main() -> anyhow::Result<()> {
writeln!(env_file, "ISSUERS_{name}_DID={did}")?;
writeln!(env_file, "ISSUERS_{name}_KEYID={key_id}")?;
writeln!(env_file, "ISSUERS_{name}_FRAGMENT={fragment}")?;
writeln!(env_file, "{name}_PUBLIC_URL=https://{}.selv.local.${{HTTP_PORT}}", name.to_lowercase())?;
writeln!(
env_file,
"{name}_PUBLIC_URL=https://{}.selv.local.${{HTTP_PORT}}",
name.to_lowercase()
)?;
}

Ok(())
Expand All @@ -92,7 +96,7 @@ async fn create_issuer(
) -> anyhow::Result<(String, KeyId, String, Address)> {
// Create a DID document.
let address: Address = get_address_with_funds(
&client,
client,
stronghold_storage.as_secret_manager(),
FAUCET_ENDPOINT,
)
Expand Down
158 changes: 82 additions & 76 deletions tooling/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,100 +12,106 @@ use iota_sdk::types::block::address::Bech32Address;
use iota_sdk::types::block::address::Hrp;
use serde_json::Value;


/// Generates an address from the given [`SecretManager`] and adds funds from the faucet.
pub async fn get_address_with_funds(
client: &Client,
stronghold: &SecretManager,
faucet_endpoint: &str,
client: &Client,
stronghold: &SecretManager,
faucet_endpoint: &str,
) -> anyhow::Result<Address> {
let address: Bech32Address = get_address(client, stronghold).await?;
let address: Bech32Address = get_address(client, stronghold).await?;

request_faucet_funds(client, address, faucet_endpoint)
.await
.context("failed to request faucet funds")?;
request_faucet_funds(client, address, faucet_endpoint)
.await
.context("failed to request faucet funds")?;

Ok(*address)
Ok(*address)
}

/// Initializes the [`SecretManager`] with a new mnemonic, if necessary,
/// and generates an address from the given [`SecretManager`].
pub async fn get_address(client: &Client, secret_manager: &SecretManager) -> anyhow::Result<Bech32Address> {
let random: [u8; 32] = rand::random();
let mnemonic = bip39::wordlist::encode(random.as_ref(), &bip39::wordlist::ENGLISH)
.map_err(|err| anyhow::anyhow!(format!("{err:?}")))?;

if let SecretManager::Stronghold(ref stronghold) = secret_manager {
match stronghold.store_mnemonic(mnemonic).await {
Ok(()) => (),
Err(iota_sdk::client::stronghold::Error::MnemonicAlreadyStored) => (),
Err(err) => anyhow::bail!(err),
pub async fn get_address(
client: &Client,
secret_manager: &SecretManager,
) -> anyhow::Result<Bech32Address> {
let random: [u8; 32] = rand::random();
let mnemonic = bip39::wordlist::encode(random.as_ref(), &bip39::wordlist::ENGLISH)
.map_err(|err| anyhow::anyhow!(format!("{err:?}")))?;

if let SecretManager::Stronghold(ref stronghold) = secret_manager {
match stronghold.store_mnemonic(mnemonic).await {
Ok(()) => (),
Err(iota_sdk::client::stronghold::Error::MnemonicAlreadyStored) => (),
Err(err) => anyhow::bail!(err),
}
} else {
anyhow::bail!("expected a `StrongholdSecretManager`");
}
} else {
anyhow::bail!("expected a `StrongholdSecretManager`");
}

let bech32_hrp: Hrp = client.get_bech32_hrp().await?;
let address: Bech32Address = secret_manager
.generate_ed25519_addresses(
GetAddressesOptions::default()
.with_range(0..1)
.with_bech32_hrp(bech32_hrp),
)
.await?[0];

Ok(address)
}

/// Requests funds from the faucet for the given `address`.
async fn request_faucet_funds(client: &Client, address: Bech32Address, faucet_endpoint: &str) -> anyhow::Result<()> {
iota_sdk::client::request_funds_from_faucet(faucet_endpoint, &address).await?;
let bech32_hrp: Hrp = client.get_bech32_hrp().await?;
let address: Bech32Address = secret_manager
.generate_ed25519_addresses(
GetAddressesOptions::default()
.with_range(0..1)
.with_bech32_hrp(bech32_hrp),
)
.await?[0];

tokio::time::timeout(std::time::Duration::from_secs(45), async {
loop {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
Ok(address)
}

let balance = get_address_balance(client, &address)
.await
.context("failed to get address balance")?;
if balance > 0 {
break;
}
}
Ok::<(), anyhow::Error>(())
})
.await
.context("maximum timeout exceeded")??;
/// Requests funds from the faucet for the given `address`.
async fn request_faucet_funds(
client: &Client,
address: Bech32Address,
faucet_endpoint: &str,
) -> anyhow::Result<()> {
iota_sdk::client::request_funds_from_faucet(faucet_endpoint, &address).await?;

tokio::time::timeout(std::time::Duration::from_secs(45), async {
loop {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;

let balance = get_address_balance(client, &address)
.await
.context("failed to get address balance")?;
if balance > 0 {
break;
}
}
Ok::<(), anyhow::Error>(())
})
.await
.context("maximum timeout exceeded")??;

Ok(())
Ok(())
}

/// Returns the balance of the given Bech32-encoded `address`.
async fn get_address_balance(client: &Client, address: &Bech32Address) -> anyhow::Result<u64> {
let output_ids = client
.basic_output_ids(vec![
QueryParameter::Address(address.to_owned()),
QueryParameter::HasExpiration(false),
QueryParameter::HasTimelock(false),
QueryParameter::HasStorageDepositReturn(false),
])
.await?;

let outputs = client.get_outputs(&output_ids).await?;

let mut total_amount = 0;
for output_response in outputs {
total_amount += output_response.output().amount();
}

Ok(total_amount)
let output_ids = client
.basic_output_ids(vec![
QueryParameter::Address(address.to_owned()),
QueryParameter::HasExpiration(false),
QueryParameter::HasTimelock(false),
QueryParameter::HasStorageDepositReturn(false),
])
.await?;

let outputs = client.get_outputs(&output_ids).await?;

let mut total_amount = 0;
for output_response in outputs {
total_amount += output_response.output().amount();
}

Ok(total_amount)
}

pub fn pretty_print_json(label: &str, value: &str) {
let data: Value = serde_json::from_str(value).unwrap();
let pretty_json = serde_json::to_string_pretty(&data).unwrap();
println!("--------------------------------------");
println!("{}:", label);
println!("--------------------------------------");
println!("{} \n", pretty_json);
}
let data: Value = serde_json::from_str(value).unwrap();
let pretty_json = serde_json::to_string_pretty(&data).unwrap();
println!("--------------------------------------");
println!("{}:", label);
println!("--------------------------------------");
println!("{} \n", pretty_json);
}

0 comments on commit 6c6d765

Please sign in to comment.