Skip to content

Commit 6f78a48

Browse files
committed
fix: add session_from_token test, remove uneede fn tokenize
1 parent 088e29e commit 6f78a48

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed

adapter/src/ethereum.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ impl Adapter for EthereumAdapter {
203203

204204
let sess = match &verified.payload.identity {
205205
Some(identity) => {
206-
207206
let contract_address = Address::from_slice(identity);
208207
let (_eloop, transport) = web3::transports::Http::new(&self.config.ethereum_network)
209208
.map_err(|_| map_error("failed to init http transport"))?;
@@ -214,7 +213,7 @@ impl Adapter for EthereumAdapter {
214213
let privilege_level: U256 = contract
215214
.query(
216215
"privileges",
217-
(verified.from.to_string(),),
216+
(Token::Address(Address::from_slice(verified.from.inner())),),
218217
None,
219218
Options::default(),
220219
None,
@@ -466,7 +465,7 @@ mod test {
466465
}
467466

468467
#[test]
469-
fn should_validate_channel_properly() {
468+
fn should_validate_valid_channel_properly() {
470469
let (eloop, http) =
471470
web3::transports::Http::new("http://localhost:8545").expect("failed to init transport");
472471
eloop.into_remote();
@@ -599,4 +598,62 @@ mod test {
599598
.validate_channel(&valid_channel)
600599
.expect("failed to validate channel");
601600
}
601+
602+
#[test]
603+
fn should_generate_session_from_token_with_identity() {
604+
// setup test payload
605+
let mut eth_adapter = setup_eth_adapter(None);
606+
eth_adapter.unlock().expect("should unlock eth adapter");
607+
608+
// deploy identity contract
609+
let (eloop, http) =
610+
web3::transports::Http::new("http://localhost:8545").expect("failed to init transport");
611+
eloop.into_remote();
612+
613+
let web3 = web3::Web3::new(http);
614+
// part of address used in initializing ganache-cli
615+
let leader_account: Address = "Df08F82De32B8d460adbE8D72043E3a7e25A3B39"
616+
.parse()
617+
.expect("failed to parse leader account");
618+
619+
let eth_adapter_address: Address = eth_adapter.whoami().to_hex_non_prefix_string().parse().expect("failed to parse eth adapter address");
620+
621+
let identity_bytecode = include_str!("../test/resources/identitybytecode.json");
622+
623+
// deploy identity contract
624+
let identity_contract = Contract::deploy(web3.eth(), &IDENTITY_ABI)
625+
.expect("invalid token token contract")
626+
.confirmations(0)
627+
.options(Options::with(|opt| {
628+
opt.gas_price = Some(1.into());
629+
opt.gas = Some(6_721_975.into());
630+
}))
631+
.execute(identity_bytecode, (
632+
Token::Array(vec![Token::Address(eth_adapter_address)]),
633+
Token::Array(vec![Token::Uint(1.into())])
634+
), leader_account)
635+
.expect("Correct parameters are passed to the constructor.")
636+
.wait()
637+
.expect("failed to wait");
638+
639+
// identity contract address
640+
let identity = <[u8; 20]>::from_hex(&format!("{:?}", identity_contract.address())[2..]).expect("failed to deserialize address");
641+
642+
let payload = Payload {
643+
id: eth_adapter.whoami().to_hex_checksummed_string(),
644+
era: 100_000,
645+
address: format!("{:?}", leader_account),
646+
identity: Some(identity),
647+
};
648+
649+
let wallet = eth_adapter.wallet.clone();
650+
let response = ewt_sign(&wallet.unwrap(), &eth_adapter.keystore_pwd, &payload)
651+
.expect("failed to generate ewt signature");
652+
653+
// verify since its with identity
654+
let session = eth_adapter.session_from_token(&response).expect("failed generate session");
655+
assert_eq!(session.uid.inner(), &identity);
656+
}
657+
658+
602659
}

adapter/src/lib.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::error::Error;
66

77
use chrono::{DateTime, Utc};
88
use ethabi::encode;
9-
use ethabi::param_type::ParamType;
10-
use ethabi::token::{LenientTokenizer, StrictTokenizer, Token, Tokenizer};
9+
use ethabi::token::{Token};
1110
use hex::FromHex;
1211
use primitives::channel::ChannelError;
1312
use primitives::BigNum;
@@ -32,11 +31,12 @@ pub fn get_signable_state_root(
3231
channel_id: &[u8],
3332
balance_root: &[u8; 32],
3433
) -> Result<[u8; 32], Box<dyn Error>> {
35-
let params = [
36-
(ParamType::FixedBytes(32), &hex::encode(channel_id)[..]),
37-
(ParamType::FixedBytes(32), &hex::encode(balance_root)[..]),
34+
let tokens = [
35+
Token::FixedBytes(channel_id.to_vec()),
36+
Token::FixedBytes(balance_root.to_vec())
3837
];
39-
let encoded = encode_params(&params, true)?;
38+
39+
let encoded = encode(&tokens).to_vec();
4040

4141
let mut result = Keccak::new_keccak256();
4242
result.update(&encoded);
@@ -48,11 +48,14 @@ pub fn get_signable_state_root(
4848
}
4949

5050
pub fn get_balance_leaf(acc: &ValidatorId, amnt: &BigNum) -> Result<[u8; 32], Box<dyn Error>> {
51-
let params = [
52-
(ParamType::Address, &acc.to_hex_non_prefix_string()[..]),
53-
(ParamType::Uint(256), &amnt.to_str_radix(10)[..]),
51+
let tokens = [
52+
Token::Address(Address::from_slice(acc.inner())),
53+
Token::Uint(
54+
U256::from_dec_str(&amnt.to_str_radix(10))
55+
.map_err(|_| ChannelError::InvalidArgument("failed to parse amt".into()))?
56+
)
5457
];
55-
let encoded = encode_params(&params, true)?;
58+
let encoded = encode(&tokens).to_vec();
5659

5760
let mut result = Keccak::new_keccak256();
5861
result.update(&encoded);
@@ -209,21 +212,6 @@ impl EthereumChannel {
209212
}
210213
}
211214

212-
fn encode_params(params: &[(ParamType, &str)], lenient: bool) -> Result<Vec<u8>, Box<dyn Error>> {
213-
let tokens = params
214-
.iter()
215-
.map(|(param, value)| {
216-
if lenient {
217-
LenientTokenizer::tokenize(param, value)
218-
} else {
219-
StrictTokenizer::tokenize(param, value)
220-
}
221-
})
222-
.collect::<Result<Vec<_>, _>>()?;
223-
224-
Ok(encode(&tokens).to_vec())
225-
}
226-
227215
#[cfg(test)]
228216
mod test {
229217
use std::convert::TryFrom;

0 commit comments

Comments
 (0)