Skip to content

Commit 565add7

Browse files
authored
Merge branch 'dev' into cfg-route
2 parents d5805de + 02b1edd commit 565add7

File tree

14 files changed

+242
-235
lines changed

14 files changed

+242
-235
lines changed

adapter/src/dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Adapter for DummyAdapter {
7070
}
7171
}
7272

73-
fn session_from_token(&mut self, token: &str) -> AdapterResult<Session> {
73+
fn session_from_token(&self, token: &str) -> AdapterResult<Session> {
7474
let identity = self
7575
.authorization_tokens
7676
.iter()

adapter/src/ethereum.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ pub struct EthereumAdapter {
3838
keystore_json: Value,
3939
keystore_pwd: Password,
4040
config: Config,
41-
// Auth tokens that we have verified (tokenId => session)
42-
session_tokens: HashMap<String, Session>,
4341
// Auth tokens that we've generated to authenticate with someone (address => token)
4442
authorization_tokens: HashMap<String, String>,
4543
wallet: Option<SafeAccount>,
@@ -72,7 +70,6 @@ impl EthereumAdapter {
7270
address,
7371
keystore_json,
7472
keystore_pwd: opts.keystore_pwd.into(),
75-
session_tokens: HashMap::new(),
7673
authorization_tokens: HashMap::new(),
7774
wallet: None,
7875
config: config.to_owned(),
@@ -164,15 +161,11 @@ impl Adapter for EthereumAdapter {
164161
Ok(true)
165162
}
166163

167-
fn session_from_token(&mut self, token: &str) -> AdapterResult<Session> {
164+
/// Creates a `Session` from a provided Token by calling the Contract.
165+
/// Does **not** cache the (`Token`, `Session`) pair.
166+
fn session_from_token(&self, token: &str) -> AdapterResult<Session> {
168167
if token.len() < 16 {
169-
return Err(AdapterError::Failed("invaild token id".to_string()));
170-
}
171-
172-
let token_id = token[token.len() - 16..].to_string();
173-
174-
if let Some(token) = self.session_tokens.get(&token_id) {
175-
return Ok(token.to_owned());
168+
return Err(AdapterError::Failed("invalid token id".to_string()));
176169
}
177170

178171
let parts: Vec<&str> = token.split('.').collect();
@@ -204,7 +197,7 @@ impl Adapter for EthereumAdapter {
204197
let contract = get_contract(&self.config, contract_address, &IDENTITY_ABI)
205198
.map_err(|_| map_error("failed to init identity contract"))?;
206199

207-
let priviledge_level: U256 = contract
200+
let privilege_level: U256 = contract
208201
.query(
209202
"privileges",
210203
verified.from.to_string(),
@@ -215,7 +208,7 @@ impl Adapter for EthereumAdapter {
215208
.wait()
216209
.map_err(|_| map_error("failed query priviledge level on contract"))?;
217210

218-
if priviledge_level == *PRIVILEGE_LEVEL_NONE {
211+
if privilege_level == *PRIVILEGE_LEVEL_NONE {
219212
return Err(AdapterError::Authorization(
220213
"insufficient privilege".to_string(),
221214
));
@@ -231,7 +224,6 @@ impl Adapter for EthereumAdapter {
231224
},
232225
};
233226

234-
self.session_tokens.insert(token_id, sess.clone());
235227
Ok(sess)
236228
}
237229

primitives/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub trait Adapter: ChannelValidator + Send + Clone + Debug {
8181
fn validate_channel(&self, channel: &Channel) -> AdapterResult<bool>;
8282

8383
/// Get user session from token
84-
fn session_from_token(&mut self, token: &str) -> AdapterResult<Session>;
84+
fn session_from_token(&self, token: &str) -> AdapterResult<Session>;
8585

8686
/// Gets authentication for specific validator
8787
fn get_auth(&mut self, validator_id: &ValidatorId) -> AdapterResult<String>;

primitives/src/channel.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub struct ChannelSpec {
7070
}
7171

7272
#[derive(Serialize, Deserialize, Debug, Clone)]
73-
#[serde(transparent)]
74-
pub struct SpecValidators([ValidatorDesc; 2]);
73+
/// A (leader, follower) tuple
74+
pub struct SpecValidators(ValidatorDesc, ValidatorDesc);
7575

7676
pub enum SpecValidator<'a> {
7777
Leader(&'a ValidatorDesc),
@@ -94,15 +94,15 @@ impl<'a> SpecValidator<'a> {
9494

9595
impl SpecValidators {
9696
pub fn new(leader: ValidatorDesc, follower: ValidatorDesc) -> Self {
97-
Self([leader, follower])
97+
Self(leader, follower)
9898
}
9999

100100
pub fn leader(&self) -> &ValidatorDesc {
101-
&self.0[0]
101+
&self.0
102102
}
103103

104104
pub fn follower(&self) -> &ValidatorDesc {
105-
&self.0[1]
105+
&self.1
106106
}
107107

108108
pub fn find(&self, validator_id: &ValidatorId) -> SpecValidator<'_> {
@@ -116,12 +116,13 @@ impl SpecValidators {
116116
}
117117
}
118118

119-
impl From<[ValidatorDesc; 2]> for SpecValidators {
120-
fn from(slice: [ValidatorDesc; 2]) -> Self {
121-
Self(slice)
119+
impl From<(ValidatorDesc, ValidatorDesc)> for SpecValidators {
120+
fn from((leader, follower): (ValidatorDesc, ValidatorDesc)) -> Self {
121+
Self(leader, follower)
122122
}
123123
}
124124

125+
/// Fixed size iterator of 2, as we need an iterator in couple of occasions
125126
impl<'a> IntoIterator for &'a SpecValidators {
126127
type Item = &'a ValidatorDesc;
127128
type IntoIter = ::std::vec::IntoIter<Self::Item>;

primitives/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ pub mod market_channel;
1515
pub mod merkle_tree;
1616
pub mod sentry;
1717
pub mod targeting_tag;
18-
pub mod util;
18+
pub mod util {
19+
pub mod tests {
20+
pub mod prep_db;
21+
pub mod time;
22+
}
23+
24+
pub mod logging;
25+
pub mod serde;
26+
}
1927
pub mod validator;
2028

2129
pub use self::ad_unit::AdUnit;

primitives/src/merkle_tree.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,19 @@ impl Algorithm<MerkleItem> for KeccakAlgorithm {
5353
}
5454

5555
fn node(&mut self, left: MerkleItem, right: MerkleItem, _height: usize) -> MerkleItem {
56-
let left_vec: Vec<u8> = left.to_vec();
57-
let right_vec: Vec<u8> = right.to_vec();
58-
59-
let mut node_vec = vec![left_vec, right_vec];
60-
node_vec.sort();
61-
62-
let flatten_node_vec: Vec<u8> = node_vec.into_iter().flatten().collect();
56+
// This is a check for odd number of leaves items
57+
// left == right since the right is a duplicate of left
58+
// return the item unencoded as the JS impl
59+
if left == right {
60+
left
61+
} else {
62+
let mut node_vec = vec![left.to_vec(), right.to_vec()];
63+
node_vec.sort();
6364

64-
self.write(&flatten_node_vec);
65-
self.hash()
65+
let flatten_node_vec: Vec<u8> = node_vec.into_iter().flatten().collect();
66+
self.write(&flatten_node_vec);
67+
self.hash()
68+
}
6669
}
6770
}
6871

@@ -183,4 +186,36 @@ mod test {
183186

184187
assert_eq!(verify, true, "should verify proof successfully");
185188
}
189+
190+
#[test]
191+
fn it_generates_correct_merkle_tree_with_odd_leaves() {
192+
let h1 = <[u8; 32]>::from_hex(
193+
"13c21db99584c9bb3e9ad98061f6ca39364049b328b74822be6303a4da18014d",
194+
)
195+
.unwrap();
196+
let h2 = <[u8; 32]>::from_hex(
197+
"b1bea7b8b58cd47d475bfe07dbe6df33f50f7a76957c51cebe8254257542fd7d",
198+
)
199+
.unwrap();
200+
201+
let h3 = <[u8; 32]>::from_hex(
202+
"c455ef23d4db0091e1e25ef5d652a2832a1fc4fa82b8e66c290a692836e0cbe6",
203+
)
204+
.unwrap();
205+
206+
// odd leaves
207+
let top = MerkleTree::new(&[h1, h2, h3]);
208+
209+
let root = hex::encode(&top.root());
210+
211+
assert_eq!(
212+
root, "e68ea33571084e5dea276b089a10fa7be9d59accf3d7838c0d9b050bf72634a1",
213+
"should generate the correct root"
214+
);
215+
216+
let proof = top.proof(0);
217+
let verify = top.verify(proof);
218+
219+
assert_eq!(verify, true, "should verify proof successfully");
220+
}
186221
}

primitives/src/util.rs

Lines changed: 0 additions & 183 deletions
This file was deleted.

0 commit comments

Comments
 (0)