4
4
#![ deny( clippy:: match_bool) ]
5
5
#![ doc( test( attr( feature( async_await, await_macro) ) ) ) ]
6
6
7
- use ethabi:: encode;
8
- use ethabi:: param_type:: { ParamType , Reader } ;
9
- use ethabi:: token:: { LenientTokenizer , StrictTokenizer , Token , Tokenizer } ;
10
7
use std:: error:: Error ;
8
+
9
+ use ethabi:: encode;
10
+ use ethabi:: param_type:: ParamType ;
11
+ use ethabi:: token:: { LenientTokenizer , StrictTokenizer , Tokenizer } ;
11
12
use tiny_keccak:: Keccak ;
12
13
13
- pub mod dummy;
14
- pub mod ethereum;
14
+ use primitives:: BigNum ;
15
15
16
16
pub use self :: dummy:: DummyAdapter ;
17
17
pub use self :: ethereum:: EthereumAdapter ;
18
18
19
+ pub mod dummy;
20
+ pub mod ethereum;
21
+
19
22
pub enum AdapterTypes {
20
23
DummyAdapter ( DummyAdapter ) ,
21
24
EthereumAdapter ( EthereumAdapter ) ,
@@ -25,9 +28,11 @@ pub fn get_signable_state_root(
25
28
channel_id : & str ,
26
29
balance_root : & str ,
27
30
) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
28
- let types = [ "bytes32" . to_string ( ) , "bytes32" . to_string ( ) ] ;
29
- let values = [ channel_id. to_string ( ) , balance_root. to_string ( ) ] ;
30
- let encoded = encode_params ( & types, & values, true ) ?;
31
+ let params = [
32
+ ( ParamType :: FixedBytes ( 32 ) , channel_id) ,
33
+ ( ParamType :: FixedBytes ( 32 ) , balance_root) ,
34
+ ] ;
35
+ let encoded = encode_params ( & params, true ) ?;
31
36
32
37
let mut result = Keccak :: new_keccak256 ( ) ;
33
38
result. update ( & encoded) ;
@@ -38,10 +43,12 @@ pub fn get_signable_state_root(
38
43
Ok ( res)
39
44
}
40
45
41
- pub fn get_balance_leaf ( acc : & str , amnt : & str ) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
42
- let types: Vec < String > = vec ! [ "address" . to_string( ) , "uint256" . to_string( ) ] ;
43
- let values = [ acc. to_string ( ) , amnt. to_string ( ) ] ;
44
- let encoded = encode_params ( & types, & values, true ) ?;
46
+ pub fn get_balance_leaf ( acc : & str , amnt : & BigNum ) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
47
+ let params = [
48
+ ( ParamType :: Address , acc) ,
49
+ ( ParamType :: Uint ( 256 ) , & amnt. to_str_radix ( 16 ) ) ,
50
+ ] ;
51
+ let encoded = encode_params ( & params, true ) ?;
45
52
46
53
let mut result = Keccak :: new_keccak256 ( ) ;
47
54
result. update ( & encoded) ;
@@ -83,29 +90,20 @@ impl EthereumChannel {
83
90
}
84
91
85
92
pub fn hash ( & self , contract_addr : & str ) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
86
- let types: Vec < String > = vec ! [
87
- "address" ,
88
- "address" ,
89
- "address" ,
90
- "uint256" ,
91
- "uint256" ,
92
- "address[]" ,
93
- "bytes32" ,
94
- ]
95
- . into_iter ( )
96
- . map ( ToString :: to_string)
97
- . collect ( ) ;
98
-
99
- let values = [
100
- contract_addr. to_string ( ) ,
101
- self . creator . to_owned ( ) ,
102
- self . token_addr . to_owned ( ) ,
103
- self . token_amount . to_owned ( ) ,
104
- self . valid_until . to_owned ( ) ,
105
- self . validators . to_owned ( ) ,
106
- self . spec . to_owned ( ) ,
93
+ let params = [
94
+ ( ParamType :: Address , contract_addr) ,
95
+ ( ParamType :: Address , & self . creator ) ,
96
+ ( ParamType :: Address , & self . token_addr ) ,
97
+ ( ParamType :: Uint ( 256 ) , & self . token_amount ) ,
98
+ ( ParamType :: Uint ( 256 ) , & self . valid_until ) ,
99
+ (
100
+ ParamType :: Array ( Box :: new ( ParamType :: Address ) ) ,
101
+ & self . validators ,
102
+ ) ,
103
+ ( ParamType :: FixedBytes ( 32 ) , & self . spec ) ,
107
104
] ;
108
- let encoded = encode_params ( & types, & values, true ) ?;
105
+
106
+ let encoded = encode_params ( & params, true ) ?;
109
107
let mut result = Keccak :: new_keccak256 ( ) ;
110
108
result. update ( & encoded) ;
111
109
@@ -149,50 +147,31 @@ impl EthereumChannel {
149
147
}
150
148
}
151
149
152
- fn encode_params (
153
- types : & [ String ] ,
154
- values : & [ String ] ,
155
- lenient : bool ,
156
- ) -> Result < Vec < u8 > , Box < dyn Error > > {
157
- assert_eq ! ( types. len( ) , values. len( ) ) ;
158
-
159
- let types: Vec < ParamType > = types
160
- . iter ( )
161
- . map ( |s| Reader :: read ( s) )
162
- . collect :: < Result < _ , _ > > ( ) ?;
163
-
164
- let params: Vec < _ > = types
165
- . into_iter ( )
166
- . zip ( values. iter ( ) . map ( |s| s as & str ) )
167
- . collect ( ) ;
168
-
169
- let tokens = parse_tokens ( & params, lenient) ?;
170
- let result = encode ( & tokens) ;
171
-
172
- Ok ( result. to_vec ( ) )
173
- }
174
-
175
- fn parse_tokens ( params : & [ ( ParamType , & str ) ] , lenient : bool ) -> Result < Vec < Token > , Box < dyn Error > > {
176
- params
150
+ fn encode_params ( params : & [ ( ParamType , & str ) ] , lenient : bool ) -> Result < Vec < u8 > , Box < dyn Error > > {
151
+ let tokens = params
177
152
. iter ( )
178
- . map ( |& ( ref param, value) | {
153
+ . map ( |( param, value) | {
179
154
if lenient {
180
155
LenientTokenizer :: tokenize ( param, value)
181
156
} else {
182
157
StrictTokenizer :: tokenize ( param, value)
183
158
}
184
159
} )
185
- . collect :: < Result < _ , _ > > ( )
186
- . map_err ( From :: from)
160
+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
161
+
162
+ Ok ( encode ( & tokens) . to_vec ( ) )
187
163
}
188
164
189
165
#[ cfg( test) ]
190
166
mod test {
191
- use super :: * ;
167
+ use std:: convert:: TryFrom ;
168
+
192
169
use byteorder:: { BigEndian , ByteOrder } ;
193
170
use chrono:: { TimeZone , Utc } ;
171
+
194
172
use primitives:: merkle_tree:: MerkleTree ;
195
- use std:: convert:: TryFrom ;
173
+
174
+ use super :: * ;
196
175
197
176
#[ test]
198
177
fn test_get_signable_state_root_hash_is_aligned_with_js_impl ( ) {
0 commit comments