@@ -125,7 +125,37 @@ impl Default for MockApi {
125
125
}
126
126
127
127
impl BackendApi for MockApi {
128
- fn canonical_address ( & self , input : & str ) -> BackendResult < Vec < u8 > > {
128
+ fn addr_validate ( & self , input : & str ) -> BackendResult < ( ) > {
129
+ let mut gas_total = GasInfo {
130
+ cost : 0 ,
131
+ externally_used : 0 ,
132
+ } ;
133
+
134
+ let ( result, gas_info) = self . addr_canonicalize ( input) ;
135
+ gas_total += gas_info;
136
+ let canonical = match result {
137
+ Ok ( canonical) => canonical,
138
+ Err ( err) => return ( Err ( err) , gas_total) ,
139
+ } ;
140
+
141
+ let ( result, gas_info) = self . addr_humanize ( & canonical) ;
142
+ gas_total += gas_info;
143
+ let normalized = match result {
144
+ Ok ( norm) => norm,
145
+ Err ( err) => return ( Err ( err) , gas_total) ,
146
+ } ;
147
+ if input != normalized. as_str ( ) {
148
+ return (
149
+ Err ( BackendError :: user_err (
150
+ "Invalid input: address not normalized" ,
151
+ ) ) ,
152
+ gas_total,
153
+ ) ;
154
+ }
155
+ ( Ok ( ( ) ) , gas_total)
156
+ }
157
+
158
+ fn addr_canonicalize ( & self , input : & str ) -> BackendResult < Vec < u8 > > {
129
159
let gas_info = GasInfo :: with_cost ( GAS_COST_CANONICALIZE ) ;
130
160
131
161
// handle error case
@@ -156,7 +186,7 @@ impl BackendApi for MockApi {
156
186
}
157
187
}
158
188
159
- fn human_address ( & self , canonical : & [ u8 ] ) -> BackendResult < String > {
189
+ fn addr_humanize ( & self , canonical : & [ u8 ] ) -> BackendResult < String > {
160
190
let gas_info = GasInfo :: with_cost ( GAS_COST_HUMANIZE ) ;
161
191
162
192
// handle error case
@@ -232,20 +262,20 @@ mod tests {
232
262
}
233
263
234
264
#[ test]
235
- fn canonical_address_works ( ) {
265
+ fn addr_canonicalize_works ( ) {
236
266
let api = MockApi :: default ( ) . with_prefix ( "osmo" ) ;
237
267
238
- api. canonical_address ( "osmo186kh7c0k0gh4ww0wh4jqc4yhzu7n7dhswe845d" )
268
+ api. addr_canonicalize ( "osmo186kh7c0k0gh4ww0wh4jqc4yhzu7n7dhswe845d" )
239
269
. 0
240
270
. unwrap ( ) ;
241
271
242
272
// is case insensitive
243
273
let data1 = api
244
- . canonical_address ( "osmo186kh7c0k0gh4ww0wh4jqc4yhzu7n7dhswe845d" )
274
+ . addr_canonicalize ( "osmo186kh7c0k0gh4ww0wh4jqc4yhzu7n7dhswe845d" )
245
275
. 0
246
276
. unwrap ( ) ;
247
277
let data2 = api
248
- . canonical_address ( "OSMO186KH7C0K0GH4WW0WH4JQC4YHZU7N7DHSWE845D" )
278
+ . addr_canonicalize ( "OSMO186KH7C0K0GH4WW0WH4JQC4YHZU7N7DHSWE845D" )
249
279
. 0
250
280
. unwrap ( ) ;
251
281
assert_eq ! ( data1, data2) ;
@@ -257,56 +287,56 @@ mod tests {
257
287
258
288
// simple
259
289
let original = api. addr_make ( "shorty" ) ;
260
- let canonical = api. canonical_address ( & original) . 0 . unwrap ( ) ;
261
- let ( recovered, _gas_cost) = api. human_address ( & canonical) ;
290
+ let canonical = api. addr_canonicalize ( & original) . 0 . unwrap ( ) ;
291
+ let ( recovered, _gas_cost) = api. addr_humanize ( & canonical) ;
262
292
assert_eq ! ( recovered. unwrap( ) , original) ;
263
293
264
294
// normalizes input
265
295
let original = "JUNO1MEPRU9FUQ4E65856ARD6068MFSFRWPGEMD0C3R" ;
266
- let canonical = api. canonical_address ( original) . 0 . unwrap ( ) ;
267
- let recovered = api. human_address ( & canonical) . 0 . unwrap ( ) ;
296
+ let canonical = api. addr_canonicalize ( original) . 0 . unwrap ( ) ;
297
+ let recovered = api. addr_humanize ( & canonical) . 0 . unwrap ( ) ;
268
298
assert_eq ! ( recovered, original. to_lowercase( ) ) ;
269
299
270
300
// Long input (Juno contract address)
271
301
let original =
272
302
String :: from ( "juno1v82su97skv6ucfqvuvswe0t5fph7pfsrtraxf0x33d8ylj5qnrysdvkc95" ) ;
273
- let canonical = api. canonical_address ( & original) . 0 . unwrap ( ) ;
274
- let recovered = api. human_address ( & canonical) . 0 . unwrap ( ) ;
303
+ let canonical = api. addr_canonicalize ( & original) . 0 . unwrap ( ) ;
304
+ let recovered = api. addr_humanize ( & canonical) . 0 . unwrap ( ) ;
275
305
assert_eq ! ( recovered, original) ;
276
306
}
277
307
278
308
#[ test]
279
- fn human_address_input_length ( ) {
309
+ fn addr_humanize_input_length ( ) {
280
310
let api = MockApi :: default ( ) ;
281
311
let input = vec ! [ 61 ; 256 ] ; // too long
282
- let ( result, _gas_info) = api. human_address ( & input) ;
312
+ let ( result, _gas_info) = api. addr_humanize ( & input) ;
283
313
match result. unwrap_err ( ) {
284
314
BackendError :: UserErr { .. } => { }
285
315
err => panic ! ( "Unexpected error: {err:?}" ) ,
286
316
}
287
317
}
288
318
289
319
#[ test]
290
- fn canonical_address_min_input_length ( ) {
320
+ fn addr_canonicalize_min_input_length ( ) {
291
321
let api = MockApi :: default ( ) ;
292
322
293
323
// empty address should fail
294
324
let empty = "cosmwasm1pj90vm" ;
295
325
assert ! ( matches!( api
296
- . canonical_address ( empty)
326
+ . addr_canonicalize ( empty)
297
327
. 0
298
328
. unwrap_err( ) ,
299
329
BackendError :: UserErr { msg } if msg. contains( "address length" ) ) ) ;
300
330
}
301
331
302
332
#[ test]
303
- fn canonical_address_max_input_length ( ) {
333
+ fn addr_canonicalize_max_input_length ( ) {
304
334
let api = MockApi :: default ( ) ;
305
335
306
336
let too_long = "cosmwasm1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqehqqkz" ;
307
337
308
338
assert ! ( matches!( api
309
- . canonical_address ( too_long)
339
+ . addr_canonicalize ( too_long)
310
340
. 0
311
341
. unwrap_err( ) ,
312
342
BackendError :: UserErr { msg } if msg. contains( "address length" ) ) ) ;
@@ -316,10 +346,10 @@ mod tests {
316
346
fn colon_in_prefix_is_valid ( ) {
317
347
let mock_api = MockApi :: default ( ) . with_prefix ( "did:com:" ) ;
318
348
let bytes = mock_api
319
- . canonical_address ( "did:com:1jkf0kmeyefvyzpwf56m7sne2000ay53r6upttu" )
349
+ . addr_canonicalize ( "did:com:1jkf0kmeyefvyzpwf56m7sne2000ay53r6upttu" )
320
350
. 0
321
351
. unwrap ( ) ;
322
- let humanized = mock_api. human_address ( & bytes) . 0 . unwrap ( ) ;
352
+ let humanized = mock_api. addr_humanize ( & bytes) . 0 . unwrap ( ) ;
323
353
324
354
assert_eq ! (
325
355
humanized. as_str( ) ,
0 commit comments