Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error handling #51

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["testing-utils/test-moat-cli", "moat-cli-user", "moat-cli-lp", "moat-cli-sp", "testing-utils/test-moat-request", "moat-core", "wallet-accessor", "integration-tests", "macros/code-hasher", "moat-example"]
members = ["testing-utils/test-moat-cli", "moat-cli-common", "moat-cli-user", "moat-cli-lp", "moat-cli-sp", "testing-utils/test-moat-request", "moat-core", "wallet-accessor", "integration-tests", "macros/code-hasher", "moat-example"]
miloszm marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions integration-tests/tests/citadel/int_test_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ async fn user_round_trip() -> Result<(), Error> {
&prover,
&verifier,
&license,
opening.unwrap(),
opening.expect("opening should be present"),
&mut OsRng,
&challenge,
GAS_LIMIT,
Expand All @@ -268,7 +268,7 @@ async fn user_round_trip() -> Result<(), Error> {
info!("calling get_session (as an SP)");
let session = CitadelInquirer::get_session(&client, session_id).await?;
assert!(session.is_some());
let session = session.unwrap();
let session = session.expect("session should be present");
info!(
"obtained session {}",
hex::encode(session.public_inputs[0].to_bytes())
Expand Down
5 changes: 4 additions & 1 deletion integration-tests/tests/websocket/ws_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ async fn ws_query_session() -> Result<(), Error> {
let session: Option<LicenseSession> =
CitadelInquirerWs::get_session(url, id, session_id).await?;
assert!(session.is_some());
let public_inputs = &session.as_ref().unwrap().public_inputs;
let public_inputs = &session
.as_ref()
.expect("session should be present")
.public_inputs;
assert_eq!(public_inputs.len(), 1);
assert_eq!(public_inputs[0], BlsScalar::zero());
Ok(())
Expand Down
15 changes: 15 additions & 0 deletions moat-cli-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "moat-cli-common"
version = "0.1.0"
edition = "2021"

[dependencies]
moat-core = { path = "../moat-core" }
dusk-plonk = { version = "0.16", default-features = false, features = ["rkyv-impl", "alloc"] }
dusk-wallet = "0.20.1-rc.0"
clap = { version = "4.0", features = ["derive", "env"] }
requestty = "0.4.1"
thiserror = "1.0"
hex = "0.4"
dusk-bytes = "0.1"
bs58 = "0.4"
44 changes: 44 additions & 0 deletions moat-cli-sp/src/error.rs → moat-cli-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use hex::FromHexError;
use std::borrow::Cow;
use std::num::ParseIntError;
use std::sync::Arc;
use thiserror::Error;

Expand All @@ -31,6 +33,12 @@ pub enum Error {
/// Invalid entry
#[error("Invalid entry: {0:?}")]
InvalidEntry(Cow<'static, str>),
/// Invalid config value
#[error("Invalid config value: {0:?}")]
InvalidConfigValue(Cow<'static, str>),
/// Wallet error
#[error(transparent)]
Wallet(Arc<dusk_wallet::Error>),
}

impl From<moat_core::Error> for Error {
Expand All @@ -56,3 +64,39 @@ impl From<std::io::Error> for Error {
Error::IO(Arc::from(e))
}
}

impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Error::InvalidEntry(e.to_string().into())
}
}

impl From<FromHexError> for Error {
fn from(e: FromHexError) -> Self {
Error::InvalidEntry(e.to_string().into())
}
}

impl From<dusk_bytes::Error> for Error {
fn from(_: dusk_bytes::Error) -> Self {
Error::InvalidEntry("invalid bytes".into())
}
}

impl From<bs58::decode::Error> for Error {
fn from(e: bs58::decode::Error) -> Self {
Error::InvalidEntry(e.to_string().into())
}
}

impl From<dusk_plonk::error::Error> for Error {
fn from(e: dusk_plonk::error::Error) -> Self {
Error::Moat(Arc::from(moat_core::Error::Plonk(Arc::from(e))))
}
}

impl From<dusk_wallet::Error> for Error {
fn from(e: dusk_wallet::Error) -> Self {
Error::Wallet(Arc::from(e))
}
}
9 changes: 9 additions & 0 deletions moat-cli-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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.

mod error;

pub use error::Error;
1 change: 1 addition & 0 deletions moat-cli-lp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dusk-wallet = "0.20.1-rc.0"
dusk-jubjub = { version = "0.13", default-features = false }
wallet-accessor = { path = "../wallet-accessor" }
moat-core = { path = "../moat-core" }
moat-cli-common = { path = "../moat-cli-common" }
dusk-pki = "0.13"
zk-citadel = "0.6.0-rc.0"
rkyv = { version = "=0.7.39" }
Expand Down
6 changes: 3 additions & 3 deletions moat-cli-lp/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::SeedableRng;
use dusk_jubjub::JubJubScalar;
use dusk_pki::SecretSpendKey;
use dusk_wallet::{RuskHttpClient, WalletPath};
use moat_cli_common::Error;
use moat_core::license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::{BcInquirer, CitadelInquirer, Error};
use moat_core::{BcInquirer, CitadelInquirer};
use rand::rngs::StdRng;
use wallet_accessor::{BlockchainAccessConfig, Password};

Expand Down Expand Up @@ -103,8 +104,7 @@ impl Command {
request_hash: String,
attr_data_bytes: String,
) -> Result<RunResult, Error> {
let attr_data =
JubJubScalar::from(attr_data_bytes.parse::<u64>().unwrap());
let attr_data = JubJubScalar::from(attr_data_bytes.parse::<u64>()?);

let mut rng = StdRng::from_entropy();
let mut reference_lp = ReferenceLP::create_with_ssk(ssk)?;
Expand Down
43 changes: 0 additions & 43 deletions moat-cli-lp/src/error.rs

This file was deleted.

19 changes: 5 additions & 14 deletions moat-cli-lp/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::error::CliError;
use crate::prompt;
use crate::{Command, Menu};
use dusk_pki::SecretSpendKey;
use dusk_wallet::WalletPath;
use moat_core::Error;
use moat_cli_common::Error;
use requestty::{ErrorKind, Question};
use wallet_accessor::{BlockchainAccessConfig, Password};

Expand Down Expand Up @@ -74,7 +73,7 @@
}

impl Interactor {
pub async fn run_loop(&mut self) -> Result<(), CliError> {
pub async fn run_loop(&mut self) -> Result<(), Error> {
loop {
let op = menu_operation()?;
match op {
Expand All @@ -94,17 +93,9 @@
Ok(run_result) => {
println!("{}", run_result);
}
Err(error) => match error {
Error::IO(arc) => {
println!("{}", arc.as_ref());
}
Error::Transaction(bx) => {
println!("{}", bx.as_ref());
}
_ => {
println!("{:?}", error);
}
},
Err(error) => {
println!("{}", error.to_string());

Check failure on line 97 in moat-cli-lp/src/interactor.rs

View workflow job for this annotation

GitHub Actions / Code Analysis / Clippy Check

`to_string` applied to a type that implements `Display` in `println!` args
}
}
continue;
}
Expand Down
19 changes: 11 additions & 8 deletions moat-cli-lp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
mod args;
mod command;
mod config;
mod error;
mod interactor;
mod menu;
mod prompt;
Expand All @@ -18,20 +17,21 @@ mod run_result;
use crate::args::Args;
use crate::command::Command;
use crate::menu::Menu;
use std::fs;

use clap::Parser;

use crate::config::LPCliConfig;
use crate::error::CliError;
use crate::interactor::Interactor;
use dusk_wallet::{Wallet, WalletPath};
use moat_cli_common::Error;
use rand::SeedableRng;
use toml_base_config::BaseConfig;
use wallet_accessor::Password::{Pwd, PwdHash};
use wallet_accessor::{BlockchainAccessConfig, WalletAccessor};

#[tokio::main]
async fn main() -> Result<(), CliError> {
async fn main() -> Result<(), Error> {
let cli = Args::parse();

let config_path = cli.config_path.as_path();
Expand All @@ -41,6 +41,9 @@ async fn main() -> Result<(), CliError> {
let gas_limit = cli.gas_limit;
let gas_price = cli.gas_price;

let _ = fs::metadata(config_path).map_err(|_| {
Error::NotFound(config_path.to_string_lossy().into_owned().into())
})?;
let config = LPCliConfig::load_path(config_path)?;
let blockchain_access_config = BlockchainAccessConfig {
rusk_address: config.rusk_address.clone(),
Expand All @@ -55,10 +58,10 @@ async fn main() -> Result<(), CliError> {
};

let wallet_accessor =
WalletAccessor::create(wallet_path.clone(), psw.clone()).unwrap();
let wallet = Wallet::from_file(wallet_accessor).unwrap();
WalletAccessor::create(wallet_path.clone(), psw.clone())?;
let wallet = Wallet::from_file(wallet_accessor)?;

let (_psk, ssk) = wallet.spending_keys(wallet.default_address()).unwrap();
let (_psk, ssk) = wallet.spending_keys(wallet.default_address())?;

let mut interactor = Interactor {
wallet_path,
Expand All @@ -73,9 +76,9 @@ async fn main() -> Result<(), CliError> {

#[rustfmt::skip]
// old wallet.dat file format:
// cargo r --release --bin moat-cli-lp -- --wallet-path ~/.dusk/rusk-wallet --config-path ./moat-cli-lp/config.toml --pwd-hash 7f2611ba158b6dcea4a69c229c303358c5e04493abeadee106a4bfa464d55787
// cargo r --release --bin moat-cli-lp -- --wallet-path ~/.dusk/rusk-wallet --pwd-hash 7f2611ba158b6dcea4a69c229c303358c5e04493abeadee106a4bfa464d55787
// new wallet.dat file format:
// cargo r --release --bin moat-cli-lp -- --wallet-path ~/.dusk/rusk-wallet --config-path ./moat-cli-lp/config.toml --pwd-hash 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
// cargo r --release --bin moat-cli-lp -- --wallet-path ~/.dusk/rusk-wallet --pwd-hash 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

Ok(())
}
1 change: 1 addition & 0 deletions moat-cli-sp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
dusk-wallet = "0.20.1-rc.0"
wallet-accessor = { path = "../wallet-accessor" }
moat-core = { path = "../moat-core" }
moat-cli-common = { path = "../moat-cli-common" }
zk-citadel = "0.6.0-rc.0"
rkyv = { version = "=0.7.39" }
dusk-bls12_381 = "0.12"
Expand Down
11 changes: 3 additions & 8 deletions moat-cli-sp/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::run_result::{
use crate::Error;
use dusk_bls12_381::BlsScalar;
use dusk_bytes::DeserializableSlice;
use dusk_bytes::Serializable;
use dusk_jubjub::JubJubAffine;
use dusk_pki::PublicSpendKey;
use dusk_wallet::RuskHttpClient;
Expand Down Expand Up @@ -77,14 +76,10 @@ impl Command {
let sc: SessionCookie = rkyv::from_bytes(bytes.as_slice())
.map_err(|_| Error::InvalidEntry("session cookie".into()))?;

let psk_lp_bytes_formatted: [u8; 64] =
bs58::decode(&psk_lp_bytes.clone())
.into_vec()
.unwrap()
.try_into()
.unwrap();
let psk_lp_bytes_formatted =
bs58::decode(&psk_lp_bytes.clone()).into_vec()?;
let psk_lp =
PublicSpendKey::from_bytes(&psk_lp_bytes_formatted).unwrap();
PublicSpendKey::from_slice(psk_lp_bytes_formatted.as_slice())?;
let pk_lp = JubJubAffine::from(*psk_lp.A());
let pk_sp = JubJubAffine::from(*psk_sp.A());

Expand Down
2 changes: 1 addition & 1 deletion moat-cli-sp/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::config::SPCliConfig;
use crate::error::Error;
use crate::prompt;
use crate::{Command, Menu};
use dusk_pki::PublicSpendKey;
use dusk_wallet::WalletPath;
use moat_cli_common::Error;
use requestty::{ErrorKind, Question};
use wallet_accessor::{BlockchainAccessConfig, Password};

Expand Down
10 changes: 4 additions & 6 deletions moat-cli-sp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
mod args;
mod command;
mod config;
mod error;
mod interactor;
mod menu;
mod prompt;
Expand All @@ -22,9 +21,9 @@ use crate::menu::Menu;
use clap::Parser;

use crate::config::SPCliConfig;
use crate::error::Error;
use crate::interactor::Interactor;
use dusk_wallet::{Wallet, WalletPath};
use moat_cli_common::Error;
use toml_base_config::BaseConfig;
use wallet_accessor::Password::{Pwd, PwdHash};
use wallet_accessor::{BlockchainAccessConfig, WalletAccessor};
Expand Down Expand Up @@ -52,11 +51,10 @@ async fn main() -> Result<(), Error> {
};

let wallet_accessor =
WalletAccessor::create(wallet_path.clone(), psw.clone()).unwrap();
let wallet = Wallet::from_file(wallet_accessor).unwrap();
WalletAccessor::create(wallet_path.clone(), psw.clone())?;
let wallet = Wallet::from_file(wallet_accessor)?;

let (psk_sp, _ssk_sp) =
wallet.spending_keys(wallet.default_address()).unwrap();
let (psk_sp, _ssk_sp) = wallet.spending_keys(wallet.default_address())?;

let mut interactor = Interactor {
wallet_path,
Expand Down
1 change: 1 addition & 0 deletions moat-cli-user/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
dusk-wallet = "0.20.1-rc.0"
wallet-accessor = { path = "../wallet-accessor" }
moat-core = { path = "../moat-core" }
moat-cli-common = { path = "../moat-cli-common" }
zk-citadel = "0.6.0-rc.0"
dusk-plonk = { version = "0.16", default-features = false, features = ["rkyv-impl", "alloc"] }
rkyv = { version = "=0.7.39" }
Expand Down
Loading
Loading