Skip to content

Commit 8fcc1b2

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

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

adapter/src/dummy.rs

Lines changed: 12 additions & 10 deletions
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> {

adapter/src/ethereum.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ 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(
7272
serde_json::from_reader(json_file).unwrap(),
7373
None,
7474
&Some(self.keystore_pwd.clone()),
7575
)
76-
.map_err(|_| map_io_error("Failed to create account"))?;
76+
.map_err(|_| map_error("Failed to create account"))?;
7777

7878
self.wallet.replace(Some(account));
7979

@@ -86,7 +86,7 @@ impl Adapter for EthereumAdapter {
8686
Some(wallet) => {
8787
let public = &wallet
8888
.public(&self.keystore_pwd)
89-
.map_err(|_| map_io_error("Failed to get public key"))?;
89+
.map_err(|_| map_error("Failed to get public key"))?;
9090
let address = format!("{:?}", public_to_address(public));
9191
let checksum_address = eth_checksum::checksum(&address);
9292
Ok(checksum_address)
@@ -103,7 +103,7 @@ impl Adapter for EthereumAdapter {
103103
Some(wallet) => {
104104
let wallet_sign = wallet
105105
.sign(&self.keystore_pwd, &message)
106-
.map_err(|_| map_io_error("failed to sign messages"))?;
106+
.map_err(|_| map_error("failed to sign messages"))?;
107107
let signature: Signature = wallet_sign.into_electrum().into();
108108
Ok(format!("0x{}", signature))
109109
}
@@ -135,18 +135,18 @@ impl Adapter for EthereumAdapter {
135135

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

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

145145
let eth_channel: EthereumChannel = channel.into();
146146

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

151151
if channel_id != channel.id {
152152
return Err(AdapterError::Configuration(
@@ -178,7 +178,7 @@ impl Adapter for EthereumAdapter {
178178
let contract_query = contract.query("states", channel_id, None, Options::default(), None);
179179
let channel_status: U256 = contract_query
180180
.wait()
181-
.map_err(|_| map_io_error("contract channel status query failed"))?;
181+
.map_err(|_| map_error("contract channel status query failed"))?;
182182

183183
if channel_status != 1.into() {
184184
return Err(AdapterError::Configuration(
@@ -212,20 +212,20 @@ impl Adapter for EthereumAdapter {
212212
Some(identity) => {
213213
let (_eloop, transport) =
214214
web3::transports::Http::new(&self.config.ethereum_network)
215-
.map_err(|_| map_io_error("Failed to initialise web3 transport"))?;
215+
.map_err(|_| map_error("Failed to initialise web3 transport"))?;
216216
let web3 = web3::Web3::new(transport);
217217

218218
let contract_address =
219219
Address::from_slice(self.config.ethereum_core_address.as_bytes());
220220

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

224224
let contract_query =
225225
contract.query("privileges", verified.from, None, Options::default(), None);
226226
let priviledge_level: U256 = contract_query
227227
.wait()
228-
.map_err(|_| map_io_error("failed query priviledge level on contract"))?;
228+
.map_err(|_| map_error("failed query priviledge level on contract"))?;
229229

230230
if priviledge_level == 0.into() {
231231
return Err(AdapterError::Authorization(
@@ -264,7 +264,7 @@ impl Adapter for EthereumAdapter {
264264
address: None,
265265
};
266266
let token = ewt_sign(&wallet, &self.keystore_pwd, &payload)
267-
.map_err(|_| map_io_error("Failed to sign token"))?;
267+
.map_err(|_| map_error("Failed to sign token"))?;
268268

269269
self.tokens_for_auth
270270
.borrow_mut()
@@ -355,7 +355,7 @@ pub fn ewt_sign(
355355
)));
356356
let signature: Signature = signer
357357
.sign(password, &message)
358-
.map_err(|_| map_io_error("sign message"))?
358+
.map_err(|_| map_error("sign message"))?
359359
.into_electrum()
360360
.into();
361361

@@ -389,8 +389,8 @@ pub fn ewt_verify(token: &str) -> Result<VerifyPayload, Box<dyn Error>> {
389389
Ok(verified_payload)
390390
}
391391

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

396396
#[cfg(test)]

primitives/src/adapter.rs

Lines changed: 2 additions & 0 deletions
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)