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

Fixed clippy warning in VDR library #18

Merged
merged 1 commit into from
Nov 28, 2023
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
56 changes: 28 additions & 28 deletions indy-besu/vdr/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl LedgerClient {
pub fn new(
chain_id: u64,
node_address: &str,
contract_configs: &Vec<ContractConfig>,
contract_configs: &[ContractConfig],
// TODO: It is simplier to just pass signer only into corresponding `sign_transaction` function.
// But we also have single step functions like `create_did` where we will have to pass call back as well
// Transaction methods already depends on the client, so it make sence to accept signer on client create
Expand All @@ -45,7 +45,7 @@ impl LedgerClient {
);

let client = Web3Client::new(node_address, signer)?;
let contracts = Self::init_contracts(&client, &contract_configs)?;
let contracts = Self::init_contracts(&client, contract_configs)?;

let ledger_client = LedgerClient {
chain_id,
Expand Down Expand Up @@ -77,7 +77,7 @@ impl LedgerClient {
/// # Returns
/// signed transaction to submit
pub async fn sign_transaction(&self, transaction: &Transaction) -> VdrResult<Transaction> {
self.client.sign_transaction(&transaction).await
self.client.sign_transaction(transaction).await
}

/// Submit prepared transaction to the ledger
Expand All @@ -91,8 +91,8 @@ impl LedgerClient {
/// depending on the type it will be either result bytes or block hash
pub async fn submit_transaction(&self, transaction: &Transaction) -> VdrResult<Vec<u8>> {
match transaction.type_ {
TransactionType::Read => self.client.call_transaction(&transaction).await,
TransactionType::Write => self.client.submit_transaction(&transaction).await,
TransactionType::Read => self.client.call_transaction(transaction).await,
TransactionType::Write => self.client.submit_transaction(transaction).await,
}
}

Expand All @@ -108,19 +108,22 @@ impl LedgerClient {
}

pub(crate) async fn sign_and_submit(&self, transaction: &Transaction) -> VdrResult<String> {
let signed_transaction = self.sign_transaction(&transaction).await?;
let signed_transaction = self.sign_transaction(transaction).await?;
let block_hash = self.submit_transaction(&signed_transaction).await?;
self.get_receipt(&block_hash).await
}

pub(crate) fn contract(&self, name: &str) -> VdrResult<&Box<dyn Contract>> {
self.contracts.get(name).ok_or_else(|| {
let vdr_error = VdrError::ContractInvalidName(name.to_string());
pub(crate) fn contract(&self, name: &str) -> VdrResult<&dyn Contract> {
self.contracts
.get(name)
.map(|contract| contract.as_ref())
.ok_or_else(|| {
let vdr_error = VdrError::ContractInvalidName(name.to_string());

warn!("Error during getting contract: {:?}", vdr_error);
warn!("Error during getting contract: {:?}", vdr_error);

vdr_error
})
vdr_error
})
}

pub(crate) fn chain_id(&self) -> u64 {
Expand Down Expand Up @@ -149,22 +152,19 @@ pub mod test {
use std::{env, fs};

pub const CHAIN_ID: u64 = 1337;
pub const NODE_ADDRESS: &'static str = "http://127.0.0.1:8545";
pub const CONTRACTS_SPEC_BASE_PATH: &'static str = "../smart_contracts/artifacts/contracts/";
pub const DID_REGISTRY_ADDRESS: &'static str = "0x0000000000000000000000000000000000003333";
pub const DID_REGISTRY_SPEC_PATH: &'static str = "did/DidRegistry.sol/DidRegistry.json";
pub const SCHEMA_REGISTRY_ADDRESS: &'static str = "0x0000000000000000000000000000000000005555";
pub const SCHEMA_REGISTRY_SPEC_PATH: &'static str = "cl/SchemaRegistry.sol/SchemaRegistry.json";
pub const CRED_DEF_REGISTRY_ADDRESS: &'static str =
"0x0000000000000000000000000000000000004444";
pub const CRED_DEF_REGISTRY_SPEC_PATH: &'static str =
pub const NODE_ADDRESS: &str = "http://127.0.0.1:8545";
pub const CONTRACTS_SPEC_BASE_PATH: &str = "../smart_contracts/artifacts/contracts/";
pub const DID_REGISTRY_ADDRESS: &str = "0x0000000000000000000000000000000000003333";
pub const DID_REGISTRY_SPEC_PATH: &str = "did/DidRegistry.sol/DidRegistry.json";
pub const SCHEMA_REGISTRY_ADDRESS: &str = "0x0000000000000000000000000000000000005555";
pub const SCHEMA_REGISTRY_SPEC_PATH: &str = "cl/SchemaRegistry.sol/SchemaRegistry.json";
pub const CRED_DEF_REGISTRY_ADDRESS: &str = "0x0000000000000000000000000000000000004444";
pub const CRED_DEF_REGISTRY_SPEC_PATH: &str =
"cl/CredentialDefinitionRegistry.sol/CredentialDefinitionRegistry.json";
pub const VALIDATOR_CONTROL_ADDRESS: &'static str =
"0x0000000000000000000000000000000000007777";
pub const VALIDATOR_CONTROL_PATH: &'static str =
"network/ValidatorControl.sol/ValidatorControl.json";
pub const ROLE_CONTROL_ADDRESS: &'static str = "0x0000000000000000000000000000000000006666";
pub const ROLE_CONTROL_PATH: &'static str = "auth/RoleControl.sol/RoleControl.json";
pub const VALIDATOR_CONTROL_ADDRESS: &str = "0x0000000000000000000000000000000000007777";
pub const VALIDATOR_CONTROL_PATH: &str = "network/ValidatorControl.sol/ValidatorControl.json";
pub const ROLE_CONTROL_ADDRESS: &str = "0x0000000000000000000000000000000000006666";
pub const ROLE_CONTROL_PATH: &str = "auth/RoleControl.sol/RoleControl.json";

fn build_contract_path(contract_path: &str) -> String {
let mut cur_dir = env::current_dir().unwrap();
Expand Down Expand Up @@ -203,7 +203,7 @@ pub mod test {
}

pub fn client(signer: Option<BasicSigner>) -> LedgerClient {
let signer = signer.unwrap_or_else(|| basic_signer());
let signer = signer.unwrap_or_else(basic_signer);
LedgerClient::new(CHAIN_ID, NODE_ADDRESS, &contracts(), Some(Box::new(signer))).unwrap()
}

Expand Down
20 changes: 12 additions & 8 deletions indy-besu/vdr/src/client/implementation/web3/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@ impl Client for Web3Client {
transaction
);

let signer = self.signer.as_ref().ok_or_else(|| {
let vdr_error = VdrError::ClientInvalidState("Signer is not set".to_string());
let signer = self
.signer
.as_ref()
.ok_or_else(|| {
let vdr_error = VdrError::ClientInvalidState("Signer is not set".to_string());

warn!(
"Error: {} during signing transaction: {:?}",
vdr_error, transaction
);
warn!(
"Error: {} during signing transaction: {:?}",
vdr_error, transaction
);

vdr_error
})?;
vdr_error
})?
.as_ref();
let account = transaction.from.clone().ok_or_else(|| {
let vdr_error =
VdrError::ClientInvalidTransaction("Missing sender address".to_string());
Expand Down
16 changes: 8 additions & 8 deletions indy-besu/vdr/src/client/implementation/web3/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use web3::{
/// Rust Web3 crate does not expose `encode` functions, instead we have to implement the interface bellow
pub struct Web3Signer<'a> {
account: Address,
signer: &'a Box<dyn Signer>,
signer: &'a dyn Signer,
}

impl<'a> Web3Signer<'a> {
pub fn new(account: Address, signer: &'a Box<dyn Signer>) -> Web3Signer<'a> {
pub fn new(account: Address, signer: &'a dyn Signer) -> Web3Signer<'a> {
let signer = Web3Signer {
account: account.clone(),
signer,
Expand All @@ -34,7 +34,7 @@ impl<'a> Key for Web3Signer<'a> {
fn sign(&self, message: &[u8], chain_id: Option<u64>) -> Result<Signature, SigningError> {
let signature_data = self
.signer
.sign(message, &self.account.value())
.sign(message, self.account.value())
.map_err(|_| {
let web3_err = SigningError::InvalidMessage;

Expand All @@ -44,8 +44,8 @@ impl<'a> Key for Web3Signer<'a> {
})?;

let v = match chain_id {
Some(chain_id) => signature_data.recovery_id as u64 + 35 + chain_id * 2,
None => signature_data.recovery_id as u64 + 27,
Some(chain_id) => signature_data.recovery_id + 35 + chain_id * 2,
None => signature_data.recovery_id + 27,
};

let signature = Signature {
Expand All @@ -62,11 +62,11 @@ impl<'a> Key for Web3Signer<'a> {
fn sign_message(&self, message: &[u8]) -> Result<Signature, SigningError> {
let signature_data = self
.signer
.sign(message, &self.account.value())
.sign(message, self.account.value())
.map_err(|_| SigningError::InvalidMessage)?;

let signature = Signature {
v: signature_data.recovery_id as u64,
v: signature_data.recovery_id,
r: H256::from_slice(&signature_data.signature[..32]),
s: H256::from_slice(&signature_data.signature[32..]),
};
Expand All @@ -77,6 +77,6 @@ impl<'a> Key for Web3Signer<'a> {
}

fn address(&self) -> EtabiAddress {
EtabiAddress::from_str(&self.account.value()).unwrap_or_default()
EtabiAddress::from_str(self.account.value()).unwrap_or_default()
}
}
110 changes: 48 additions & 62 deletions indy-besu/vdr/src/client/types/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,35 @@ impl ContractOutput {
pub fn get_tuple(&self, index: usize) -> VdrResult<ContractOutput> {
self.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing tuple value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing tuple value".to_string())
})?
.clone()
.into_tuple()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing tuple value".to_string(),
))
.map(|result| ContractOutput(result))
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing tuple value".to_string()))
.map(ContractOutput)
}

pub fn get_string(&self, index: usize) -> VdrResult<String> {
self.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing string value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing string value".to_string())
})?
.clone()
.into_string()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing string value".to_string(),
))
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing string value".to_string())
})
}

pub fn get_address(&self, index: usize) -> VdrResult<Address> {
let address_str = self
.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing address value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing address value".to_string())
})?
.clone()
.to_string();

Expand All @@ -130,64 +128,50 @@ impl ContractOutput {
pub fn get_bool(&self, index: usize) -> VdrResult<bool> {
self.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing bool value".to_string(),
))?
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing bool value".to_string()))?
.clone()
.into_bool()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing bool value".to_string(),
))
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing bool value".to_string()))
}

pub fn get_u128(&self, index: usize) -> VdrResult<u128> {
Ok(self
.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing uint value".to_string(),
))?
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing uint value".to_string()))?
.clone()
.into_uint()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing uint value".to_string(),
))?
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing uint value".to_string()))?
.as_u128())
}

pub fn get_u8(&self, index: usize) -> VdrResult<u8> {
Ok(self
.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing uint value".to_string(),
))?
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing uint value".to_string()))?
.clone()
.into_uint()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing uint value".to_string(),
))?
.ok_or_else(|| VdrError::ContractInvalidResponseData("Missing uint value".to_string()))?
.as_u32() as u8)
}

pub fn get_string_array(&self, index: usize) -> VdrResult<Vec<String>> {
self.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing string array value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing string array value".to_string())
})?
.clone()
.into_array()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing string array value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing string array value".to_string())
})?
.into_iter()
.map(|token| {
token
.into_string()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing string value".to_string(),
))
token.into_string().ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing string value".to_string())
})
})
.collect()
}
Expand All @@ -196,14 +180,18 @@ impl ContractOutput {
Ok(self
.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing address string array value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData(
"Missing address string array value".to_string(),
)
})?
.clone()
.into_array()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing address string array value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData(
"Missing address string array value".to_string(),
)
})?
.into_iter()
.map(|token| Address::new(&token.to_string()))
.collect())
Expand All @@ -213,22 +201,20 @@ impl ContractOutput {
let tokens = self
.0
.get(index)
.ok_or(VdrError::ContractInvalidResponseData(
"Missing object array value".to_string(),
))?
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing object array value".to_string())
})?
.clone()
.into_array()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing object array value".to_string(),
))?;
.ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing object array value".to_string())
})?;

let mut result: Vec<ContractOutput> = Vec::new();
for item in tokens.into_iter() {
let item = item
.into_tuple()
.ok_or(VdrError::ContractInvalidResponseData(
"Missing object value".to_string(),
))?;
let item = item.into_tuple().ok_or_else(|| {
VdrError::ContractInvalidResponseData("Missing object value".to_string())
})?;
result.push(ContractOutput(item))
}
Ok(result)
Expand Down
Loading
Loading