Skip to content

Commit 6a824f3

Browse files
committed
fix: add generic adapter error
1 parent 6c1a330 commit 6a824f3

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

adapter/src/dummy.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,19 @@ impl Adapter for DummyAdapter {
7676
}
7777

7878
fn session_from_token(&self, token: &str) -> AdapterResult<Session> {
79-
let mut identity = "";
80-
for (key, val) in self.tokens_for_auth.iter() {
81-
if val == token {
82-
identity = key;
83-
}
84-
}
79+
let identity = self
80+
.tokens_for_auth
81+
.clone()
82+
.into_iter()
83+
.find(|(_, id)| *id == token);
8584

86-
Ok(Session {
87-
uid: identity.to_owned(),
88-
era: 0,
89-
})
85+
match identity {
86+
Some((id, _)) => Ok(Session { uid: id, era: 0 }),
87+
None => Err(AdapterError::Authentication(format!(
88+
"no session token for this auth: {}",
89+
token
90+
))),
91+
}
9092
}
9193

9294
fn get_auth(&self, _validator: &ValidatorDesc) -> AdapterResult<String> {
@@ -98,7 +100,7 @@ impl Adapter for DummyAdapter {
98100

99101
match who {
100102
Some((id, _)) => {
101-
let auth = self.tokens_for_auth.get(&id).unwrap();
103+
let auth = self.tokens_for_auth.get(&id).expect("id should exist");
102104
Ok(auth.to_owned())
103105
}
104106
None => Err(AdapterError::Authentication(format!(

adapter/src/ethereum.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ impl Adapter for EthereumAdapter {
6666

6767
fn unlock(&self) -> AdapterResult<bool> {
6868
let json_file = File::open(&Path::new(&self.keystore_json).to_path_buf())
69-
.map_err(|_| map_io_error("Invalid keystore location provided"))?;
69+
.map_err(|_| map_error("Invalid keystore location provided"))?;
7070

7171
let account = SafeAccount::from_file(
72-
serde_json::from_reader(json_file).unwrap(),
72+
serde_json::from_reader(json_file)
73+
.map_err(|_| map_error("Invalid keystore location provided"))?,
7374
None,
7475
&Some(self.keystore_pwd.clone()),
7576
)
76-
.map_err(|_| map_io_error("Failed to create account"))?;
77+
.map_err(|_| map_error("Failed to create account"))?;
7778

7879
self.wallet.replace(Some(account));
7980

@@ -86,7 +87,7 @@ impl Adapter for EthereumAdapter {
8687
Some(wallet) => {
8788
let public = &wallet
8889
.public(&self.keystore_pwd)
89-
.map_err(|_| map_io_error("Failed to get public key"))?;
90+
.map_err(|_| map_error("Failed to get public key"))?;
9091
let address = format!("{:?}", public_to_address(public));
9192
let checksum_address = eth_checksum::checksum(&address);
9293
Ok(checksum_address)
@@ -103,7 +104,7 @@ impl Adapter for EthereumAdapter {
103104
Some(wallet) => {
104105
let wallet_sign = wallet
105106
.sign(&self.keystore_pwd, &message)
106-
.map_err(|_| map_io_error("failed to sign messages"))?;
107+
.map_err(|_| map_error("failed to sign messages"))?;
107108
let signature: Signature = wallet_sign.into_electrum().into();
108109
Ok(format!("0x{}", signature))
109110
}
@@ -135,18 +136,18 @@ impl Adapter for EthereumAdapter {
135136

136137
fn validate_channel(&self, channel: &Channel) -> AdapterResult<bool> {
137138
let (_eloop, transport) = web3::transports::Http::new(&self.config.ethereum_network)
138-
.map_err(|_| map_io_error("Failed to initialise web3 transport"))?;
139+
.map_err(|_| map_error("Failed to initialise web3 transport"))?;
139140
let web3 = web3::Web3::new(transport);
140141
let contract_address = Address::from_slice(self.config.ethereum_core_address.as_bytes());
141142

142143
let contract = Contract::from_json(web3.eth(), contract_address, &ADEXCORE_ABI)
143-
.map_err(|_| map_io_error("Failed to initialise web3 transport"))?;
144+
.map_err(|_| map_error("Failed to initialise web3 transport"))?;
144145

145146
let eth_channel: EthereumChannel = channel.into();
146147

147148
let channel_id = eth_channel
148149
.hash_hex(&self.config.ethereum_core_address)
149-
.map_err(|_| map_io_error("Failed to hash the channel id"))?;
150+
.map_err(|_| map_error("Failed to hash the channel id"))?;
150151

151152
if channel_id != channel.id {
152153
return Err(AdapterError::Configuration(
@@ -178,7 +179,7 @@ impl Adapter for EthereumAdapter {
178179
let contract_query = contract.query("states", channel_id, None, Options::default(), None);
179180
let channel_status: U256 = contract_query
180181
.wait()
181-
.map_err(|_| map_io_error("contract channel status query failed"))?;
182+
.map_err(|_| map_error("contract channel status query failed"))?;
182183

183184
if channel_status != 1.into() {
184185
return Err(AdapterError::Configuration(
@@ -212,20 +213,20 @@ impl Adapter for EthereumAdapter {
212213
Some(identity) => {
213214
let (_eloop, transport) =
214215
web3::transports::Http::new(&self.config.ethereum_network)
215-
.map_err(|_| map_io_error("Failed to initialise web3 transport"))?;
216+
.map_err(|_| map_error("Failed to initialise web3 transport"))?;
216217
let web3 = web3::Web3::new(transport);
217218

218219
let contract_address =
219220
Address::from_slice(self.config.ethereum_core_address.as_bytes());
220221

221222
let contract = Contract::from_json(web3.eth(), contract_address, &IDENTITY_ABI)
222-
.map_err(|_| map_io_error("failed to init identity contract"))?;
223+
.map_err(|_| map_error("failed to init identity contract"))?;
223224

224225
let contract_query =
225226
contract.query("privileges", verified.from, None, Options::default(), None);
226227
let priviledge_level: U256 = contract_query
227228
.wait()
228-
.map_err(|_| map_io_error("failed query priviledge level on contract"))?;
229+
.map_err(|_| map_error("failed query priviledge level on contract"))?;
229230

230231
if priviledge_level == 0.into() {
231232
return Err(AdapterError::Authorization(
@@ -264,7 +265,7 @@ impl Adapter for EthereumAdapter {
264265
address: None,
265266
};
266267
let token = ewt_sign(&wallet, &self.keystore_pwd, &payload)
267-
.map_err(|_| map_io_error("Failed to sign token"))?;
268+
.map_err(|_| map_error("Failed to sign token"))?;
268269

269270
self.tokens_for_auth
270271
.borrow_mut()
@@ -355,7 +356,7 @@ pub fn ewt_sign(
355356
)));
356357
let signature: Signature = signer
357358
.sign(password, &message)
358-
.map_err(|_| map_io_error("sign message"))?
359+
.map_err(|_| map_error("sign message"))?
359360
.into_electrum()
360361
.into();
361362

@@ -389,8 +390,8 @@ pub fn ewt_verify(token: &str) -> Result<VerifyPayload, Box<dyn Error>> {
389390
Ok(verified_payload)
390391
}
391392

392-
fn map_io_error(err: &str) -> AdapterError {
393-
AdapterError::IO(err.to_string())
393+
fn map_error(err: &str) -> AdapterError {
394+
AdapterError::Failed(err.to_string())
394395
}
395396

396397
#[cfg(test)]

primitives/src/adapter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum AdapterError {
1616
Signature(String),
1717
InvalidChannel(String),
1818
IO(String),
19+
Failed(String),
1920
}
2021

2122
impl Error for AdapterError {}
@@ -30,6 +31,7 @@ impl fmt::Display for AdapterError {
3031
AdapterError::Signature(error) => write!(f, "Signature error: {}", error),
3132
AdapterError::InvalidChannel(error) => write!(f, "Invalid Channel error: {}", error),
3233
AdapterError::IO(error) => write!(f, "IO error: {}", error),
34+
AdapterError::Failed(error) => write!(f, "IO error: {}", error),
3335
}
3436
}
3537
}

0 commit comments

Comments
 (0)