3
3
#![ deny( clippy:: all) ]
4
4
#![ deny( clippy:: match_bool) ]
5
5
#![ doc( test( attr( feature( async_await, await_macro) ) ) ) ]
6
- #![ doc( test( attr( cfg( feature = "dummy-adapter" ) ) ) ) ]
7
6
8
7
use ethabi:: encode;
9
8
use ethabi:: param_type:: { ParamType , Reader } ;
@@ -26,12 +25,12 @@ pub fn get_signable_state_root(
26
25
channel_id : & str ,
27
26
balance_root : & str ,
28
27
) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
29
- let types: Vec < String > = vec ! [ "bytes32" . to_string( ) , "bytes32" . to_string( ) ] ;
28
+ let types = [ "bytes32" . to_string ( ) , "bytes32" . to_string ( ) ] ;
30
29
let values = [ channel_id. to_string ( ) , balance_root. to_string ( ) ] ;
31
30
let encoded = encode_params ( & types, & values, true ) ?;
32
31
33
32
let mut result = Keccak :: new_keccak256 ( ) ;
34
- result. update ( encoded. as_ref ( ) ) ;
33
+ result. update ( & encoded) ;
35
34
36
35
let mut res: [ u8 ; 32 ] = [ 0 ; 32 ] ;
37
36
result. finalize ( & mut res) ;
@@ -45,7 +44,7 @@ pub fn get_balance_leaf(acc: &str, amnt: &str) -> Result<[u8; 32], Box<dyn Error
45
44
let encoded = encode_params ( & types, & values, true ) ?;
46
45
47
46
let mut result = Keccak :: new_keccak256 ( ) ;
48
- result. update ( encoded. as_ref ( ) ) ;
47
+ result. update ( & encoded) ;
49
48
50
49
let mut res: [ u8 ; 32 ] = [ 0 ; 32 ] ;
51
50
result. finalize ( & mut res) ;
@@ -64,7 +63,7 @@ pub struct EthereumChannel {
64
63
}
65
64
66
65
impl EthereumChannel {
67
- fn new (
66
+ pub fn new (
68
67
creator : & str ,
69
68
token_addr : & str ,
70
69
token_amount : String ,
@@ -83,7 +82,7 @@ impl EthereumChannel {
83
82
}
84
83
}
85
84
86
- fn hash ( & self , contract_addr : & str ) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
85
+ pub fn hash ( & self , contract_addr : & str ) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
87
86
let types: Vec < String > = vec ! [
88
87
"address" ,
89
88
"address" ,
@@ -108,20 +107,20 @@ impl EthereumChannel {
108
107
] ;
109
108
let encoded = encode_params ( & types, & values, true ) ?;
110
109
let mut result = Keccak :: new_keccak256 ( ) ;
111
- result. update ( encoded. as_ref ( ) ) ;
110
+ result. update ( & encoded) ;
112
111
113
112
let mut res: [ u8 ; 32 ] = [ 0 ; 32 ] ;
114
113
result. finalize ( & mut res) ;
115
114
116
115
Ok ( res)
117
116
}
118
117
119
- fn hash_hex ( & self , contract_addr : & str ) -> Result < String , Box < dyn Error > > {
118
+ pub fn hash_hex ( & self , contract_addr : & str ) -> Result < String , Box < dyn Error > > {
120
119
let result = self . hash ( contract_addr) ?;
121
120
Ok ( format ! ( "0x{}" , hex:: encode( result) . to_string( ) ) )
122
121
}
123
122
124
- fn to_solidity_tuple ( & self ) -> Vec < String > {
123
+ pub fn to_solidity_tuple ( & self ) -> Vec < String > {
125
124
vec ! [
126
125
self . creator. to_owned( ) ,
127
126
self . token_addr. to_owned( ) ,
@@ -132,15 +131,15 @@ impl EthereumChannel {
132
131
]
133
132
}
134
133
135
- fn hash_to_sign (
134
+ pub fn hash_to_sign (
136
135
& self ,
137
136
contract_addr : & str ,
138
137
balance_root : & str ,
139
138
) -> Result < [ u8 ; 32 ] , Box < dyn Error > > {
140
139
get_signable_state_root ( contract_addr, balance_root)
141
140
}
142
141
143
- fn hash_to_sign_hex (
142
+ pub fn hash_to_sign_hex (
144
143
& self ,
145
144
contract_addr : & str ,
146
145
balance_root : & str ,
@@ -154,7 +153,7 @@ fn encode_params(
154
153
types : & [ String ] ,
155
154
values : & [ String ] ,
156
155
lenient : bool ,
157
- ) -> Result < String , Box < dyn Error > > {
156
+ ) -> Result < Vec < u8 > , Box < dyn Error > > {
158
157
assert_eq ! ( types. len( ) , values. len( ) ) ;
159
158
160
159
let types: Vec < ParamType > = types
@@ -164,13 +163,13 @@ fn encode_params(
164
163
165
164
let params: Vec < _ > = types
166
165
. into_iter ( )
167
- . zip ( values. iter ( ) . map ( |v| v as & str ) )
166
+ . zip ( values. iter ( ) . map ( |s| s as & str ) )
168
167
. collect ( ) ;
169
168
170
169
let tokens = parse_tokens ( & params, lenient) ?;
171
170
let result = encode ( & tokens) ;
172
171
173
- Ok ( hex :: encode ( result) . to_string ( ) )
172
+ Ok ( result. to_vec ( ) )
174
173
}
175
174
176
175
fn parse_tokens ( params : & [ ( ParamType , & str ) ] , lenient : bool ) -> Result < Vec < Token > , Box < dyn Error > > {
@@ -186,3 +185,35 @@ fn parse_tokens(params: &[(ParamType, &str)], lenient: bool) -> Result<Vec<Token
186
185
. collect :: < Result < _ , _ > > ( )
187
186
. map_err ( From :: from)
188
187
}
188
+
189
+ #[ cfg( test) ]
190
+ mod test {
191
+ use super :: * ;
192
+ use byteorder:: { BigEndian , ByteOrder } ;
193
+ use chrono:: { TimeZone , Utc } ;
194
+ use primitives:: merkle_tree:: MerkleTree ;
195
+ use std:: convert:: TryFrom ;
196
+
197
+ #[ test]
198
+ fn test_get_signable_state_root_hash_is_aligned_with_js_impl ( ) {
199
+ let timestamp = Utc . ymd ( 2019 , 9 , 12 ) . and_hms ( 17 , 0 , 0 ) ;
200
+ let mut timestamp_buf = [ 0_u8 ; 32 ] ;
201
+ let n: u64 = u64:: try_from ( timestamp. timestamp_millis ( ) )
202
+ . expect ( "The timestamp should be able to be converted to u64" ) ;
203
+ BigEndian :: write_uint ( & mut timestamp_buf[ 26 ..] , n, 6 ) ;
204
+
205
+ let merkle_tree = MerkleTree :: new ( & [ timestamp_buf] ) ;
206
+ let info_root_raw = hex:: encode ( merkle_tree. root ( ) ) ;
207
+
208
+ let channel_id = "061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088" ;
209
+
210
+ let state_root =
211
+ get_signable_state_root ( & channel_id, & info_root_raw) . expect ( "Should get state_root" ) ;
212
+
213
+ let expected_hex =
214
+ hex:: decode ( "b68cde9b0c8b63ac7152e78a65c736989b4b99bfc252758b1c3fd6ca357e0d6b" )
215
+ . expect ( "Should decode valid expected hex" ) ;
216
+
217
+ assert_eq ! ( state_root. to_vec( ) , expected_hex) ;
218
+ }
219
+ }
0 commit comments