|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use hex::encode; |
| 4 | + |
| 5 | +use crate::adapter::{Adapter, AdapterError, AdapterFuture, Config}; |
| 6 | +use crate::sanity::SanityChecker; |
| 7 | +use futures::future::{err, ok, FutureExt}; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub struct DummyParticipant { |
| 11 | + pub identity: String, |
| 12 | + pub token: String, |
| 13 | +} |
| 14 | + |
| 15 | +pub struct DummyAdapter<'a> { |
| 16 | + pub config: Config, |
| 17 | + /// Dummy participants which will be used for |
| 18 | + /// Creator, Validator Leader, Validator Follower and etc. |
| 19 | + pub participants: HashMap<&'a str, DummyParticipant>, |
| 20 | +} |
| 21 | + |
| 22 | +impl SanityChecker for DummyAdapter<'_> {} |
| 23 | + |
| 24 | +impl Adapter for DummyAdapter<'_> { |
| 25 | + fn config(&self) -> &Config { |
| 26 | + &self.config |
| 27 | + } |
| 28 | + |
| 29 | + /// Example: |
| 30 | + /// |
| 31 | + /// ``` |
| 32 | + /// use adapter::{ConfigBuilder, Adapter}; |
| 33 | + /// use adapter::dummy::DummyAdapter; |
| 34 | + /// use std::collections::HashMap; |
| 35 | + /// |
| 36 | + /// futures::executor::block_on(async { |
| 37 | + /// let config = ConfigBuilder::new("identity").build(); |
| 38 | + /// let adapter = DummyAdapter { config, participants: HashMap::new() }; |
| 39 | + /// |
| 40 | + /// let actual = await!(adapter.sign("abcdefghijklmnopqrstuvwxyz012345")).unwrap(); |
| 41 | + /// let expected = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity"; |
| 42 | + /// assert_eq!(expected, &actual); |
| 43 | + /// }); |
| 44 | + /// ``` |
| 45 | + fn sign(&self, state_root: &str) -> AdapterFuture<String> { |
| 46 | + let signature = format!( |
| 47 | + "Dummy adapter signature for {} by {}", |
| 48 | + encode(&state_root), |
| 49 | + &self.config.identity |
| 50 | + ); |
| 51 | + ok(signature).boxed() |
| 52 | + } |
| 53 | + |
| 54 | + /// Example: |
| 55 | + /// |
| 56 | + /// ``` |
| 57 | + /// use adapter::{ConfigBuilder, Adapter}; |
| 58 | + /// use adapter::dummy::DummyAdapter; |
| 59 | + /// use std::collections::HashMap; |
| 60 | + /// |
| 61 | + /// futures::executor::block_on(async { |
| 62 | + /// let config = ConfigBuilder::new("identity").build(); |
| 63 | + /// let adapter = DummyAdapter { config, participants: HashMap::new() }; |
| 64 | + /// |
| 65 | + /// let signature = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity"; |
| 66 | + /// assert_eq!(Ok(true), await!(adapter.verify("identity", "doesn't matter", signature))); |
| 67 | + /// }); |
| 68 | + /// ``` |
| 69 | + fn verify(&self, signer: &str, _state_root: &str, signature: &str) -> AdapterFuture<bool> { |
| 70 | + // select the `identity` and compare it to the signer |
| 71 | + // for empty string this will return array with 1 element - an empty string `[""]` |
| 72 | + let is_same = match signature.rsplit(' ').take(1).next() { |
| 73 | + Some(from) => from == signer, |
| 74 | + None => false, |
| 75 | + }; |
| 76 | + |
| 77 | + ok(is_same).boxed() |
| 78 | + } |
| 79 | + |
| 80 | + /// Finds the auth. token in the HashMap of DummyParticipants if exists |
| 81 | + /// |
| 82 | + /// Example: |
| 83 | + /// |
| 84 | + /// ``` |
| 85 | + /// use std::collections::HashMap; |
| 86 | + /// use adapter::dummy::{DummyParticipant, DummyAdapter}; |
| 87 | + /// use adapter::{ConfigBuilder, Adapter}; |
| 88 | + /// |
| 89 | + /// futures::executor::block_on(async { |
| 90 | + /// let mut participants = HashMap::new(); |
| 91 | + /// participants.insert( |
| 92 | + /// "identity_key", |
| 93 | + /// DummyParticipant { |
| 94 | + /// identity: "identity".to_string(), |
| 95 | + /// token: "token".to_string(), |
| 96 | + /// }, |
| 97 | + /// ); |
| 98 | + /// |
| 99 | + /// let adapter = DummyAdapter { |
| 100 | + /// config: ConfigBuilder::new("identity").build(), |
| 101 | + /// participants, |
| 102 | + /// }; |
| 103 | + /// |
| 104 | + /// assert_eq!(Ok("token".to_string()), await!(adapter.get_auth("identity"))); |
| 105 | + /// }); |
| 106 | + /// ``` |
| 107 | + fn get_auth(&self, validator: &str) -> AdapterFuture<String> { |
| 108 | + let participant = self |
| 109 | + .participants |
| 110 | + .iter() |
| 111 | + .find(|&(_, participant)| participant.identity == validator); |
| 112 | + let future = match participant { |
| 113 | + Some((_, participant)) => ok(participant.token.to_string()), |
| 114 | + None => err(AdapterError::Authentication( |
| 115 | + "Identity not found".to_string(), |
| 116 | + )), |
| 117 | + }; |
| 118 | + |
| 119 | + future.boxed() |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#[cfg(test)] |
| 124 | +mod test { |
| 125 | + use crate::adapter::ConfigBuilder; |
| 126 | + |
| 127 | + use super::*; |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn dummy_adapter_sings_state_root_and_verifies_it() { |
| 131 | + futures::executor::block_on(async { |
| 132 | + let config = ConfigBuilder::new("identity").build(); |
| 133 | + let adapter = DummyAdapter { |
| 134 | + config, |
| 135 | + participants: HashMap::new(), |
| 136 | + }; |
| 137 | + |
| 138 | + let expected_signature = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity"; |
| 139 | + let actual_signature = await!(adapter.sign("abcdefghijklmnopqrstuvwxyz012345")) |
| 140 | + .expect("Signing shouldn't fail"); |
| 141 | + |
| 142 | + assert_eq!(expected_signature, &actual_signature); |
| 143 | + |
| 144 | + let is_verified = |
| 145 | + await!(adapter.verify("identity", "doesn't matter", &actual_signature)); |
| 146 | + |
| 147 | + assert_eq!(Ok(true), is_verified); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn get_auth_with_empty_participators() { |
| 153 | + futures::executor::block_on(async { |
| 154 | + let adapter = DummyAdapter { |
| 155 | + config: ConfigBuilder::new("identity").build(), |
| 156 | + participants: HashMap::new(), |
| 157 | + }; |
| 158 | + |
| 159 | + assert_eq!( |
| 160 | + Err(AdapterError::Authentication( |
| 161 | + "Identity not found".to_string() |
| 162 | + )), |
| 163 | + await!(adapter.get_auth("non-existing")) |
| 164 | + ); |
| 165 | + |
| 166 | + let mut participants = HashMap::new(); |
| 167 | + participants.insert( |
| 168 | + "identity_key", |
| 169 | + DummyParticipant { |
| 170 | + identity: "identity".to_string(), |
| 171 | + token: "token".to_string(), |
| 172 | + }, |
| 173 | + ); |
| 174 | + let adapter = DummyAdapter { |
| 175 | + config: ConfigBuilder::new("identity").build(), |
| 176 | + participants, |
| 177 | + }; |
| 178 | + |
| 179 | + assert_eq!( |
| 180 | + Err(AdapterError::Authentication( |
| 181 | + "Identity not found".to_string() |
| 182 | + )), |
| 183 | + await!(adapter.get_auth("non-existing")) |
| 184 | + ); |
| 185 | + }); |
| 186 | + } |
| 187 | +} |
0 commit comments