Skip to content

Commit 52f5790

Browse files
committed
fix: add From<&String> ValidatorId, remove uneeded closure
1 parent 779654b commit 52f5790

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

adapter/src/ethereum.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ impl EthereumAdapter {
6666
}
6767
};
6868

69-
let identity = ValidatorId::try_from(address.as_str())?;
69+
let address = ValidatorId::try_from(&address)?;
7070

7171
Ok(Self {
72-
address: identity,
72+
address,
7373
keystore_json,
7474
keystore_pwd: opts.keystore_pwd.into(),
7575
session_tokens: HashMap::new(),
@@ -118,7 +118,7 @@ impl Adapter for EthereumAdapter {
118118
fn verify(&self, signer: &ValidatorId, state_root: &str, sig: &str) -> AdapterResult<bool> {
119119
let decoded_signature = hex::decode(sig)
120120
.map_err(|_| AdapterError::Signature("invalid signature".to_string()))?;
121-
let address = Address::from_slice(&signer.inner());
121+
let address = Address::from_slice(signer.inner());
122122
let signature = Signature::from_electrum(&decoded_signature);
123123
let message = Message::from_slice(&hash_message(state_root));
124124

@@ -222,7 +222,7 @@ impl Adapter for EthereumAdapter {
222222
}
223223
Session {
224224
era: verified.payload.era,
225-
uid: ValidatorId::try_from(identity.as_str())?,
225+
uid: ValidatorId::try_from(identity)?,
226226
}
227227
}
228228
None => Session {
@@ -374,7 +374,7 @@ pub fn ewt_verify(
374374
let payload: Payload = serde_json::from_str(&payload_string)?;
375375

376376
let verified_payload = VerifyPayload {
377-
from: ValidatorId::try_from(format!("{:?}", address).as_str())?,
377+
from: ValidatorId::try_from(&format!("{:?}", address))?,
378378
payload,
379379
};
380380

primitives/src/channel_validator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::convert::TryFrom;
77
pub trait ChannelValidator {
88
fn is_channel_valid(config: &Config, channel: &Channel) -> Result<(), ChannelError> {
99
let identity = &config.clone().identity.unwrap_or_else(|| "".to_string());
10-
let validator_identity = ValidatorId::try_from(&identity[..]).map_err(|_| {
10+
let validator_identity = ValidatorId::try_from(identity).map_err(|_| {
1111
ChannelError::InvalidArgument("Failed to deserialize identity".to_string())
1212
})?;
1313
let adapter_channel_validator = match channel.spec.validators.find(&validator_identity) {
@@ -50,8 +50,7 @@ pub fn all_validators_listed(validators: &SpecValidators, whitelist: &[String])
5050
} else {
5151
let found_validators = whitelist
5252
.iter()
53-
.map(|identity| ValidatorId::try_from(&identity[..]))
54-
.filter_map(Result::ok)
53+
.filter_map(|identity| ValidatorId::try_from(identity).ok())
5554
.filter(|allowed| {
5655
allowed == &validators.leader().id || allowed == &validators.follower().id
5756
})

primitives/src/validator.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub type ValidatorFuture<T> = Pin<Box<dyn Future<Output = Result<T, ValidatorErr
2424
pub struct ValidatorId(#[serde(with = "SerHex::<StrictPfx>")] [u8; 20]);
2525

2626
impl ValidatorId {
27-
pub fn inner(&self) -> [u8; 20] {
28-
self.0
27+
pub fn inner(&self) -> &[u8; 20] {
28+
&self.0
2929
}
3030

3131
pub fn to_hex_non_prefix_string(&self) -> String {
@@ -54,6 +54,15 @@ impl TryFrom<&str> for ValidatorId {
5454
}
5555
}
5656

57+
impl TryFrom<&String> for ValidatorId {
58+
type Error = DomainError;
59+
fn try_from(value: &String) -> Result<Self, Self::Error> {
60+
ValidatorId::try_from(value.as_str()).map_err(|_| {
61+
DomainError::InvalidArgument("Failed to deserialize validator id".to_string())
62+
})
63+
}
64+
}
65+
5766
impl fmt::Display for ValidatorId {
5867
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5968
write!(f, "{}", format!("0x{}", hex::encode(self.0)))

validator_worker/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub(crate) fn get_state_root_hash<A: Adapter + 'static>(
3131
// Note: MerkleTree takes care of deduplicating and sorting
3232
let elems: Vec<[u8; 32]> = balances
3333
.iter()
34-
.map(|(acc, amount)| get_balance_leaf(&ValidatorId::try_from(acc.as_str())?, amount))
34+
.map(|(acc, amount)| get_balance_leaf(&ValidatorId::try_from(acc)?, amount))
3535
.collect::<Result<_, _>>()?;
3636

3737
let tree = MerkleTree::new(&elems);
38-
// keccak256(channelId, balanceRoot)
38+
// keccak256(channelId, balanceRoot
3939
get_signable_state_root(&iface.channel.id, &tree.root())
4040
}
4141

0 commit comments

Comments
 (0)