@@ -315,7 +315,7 @@ pub enum Instantiate2AddressError {
315
315
/// let canonical_creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
316
316
/// let checksum = HexBinary::from_hex("9af782a3a1bcbcd22dbb6a45c751551d9af782a3a1bcbcd22dbb6a45c751551d")?;
317
317
/// let salt = b"instance 1231";
318
- /// let canonical_addr = instantiate2_address(&checksum, &canonical_creator, salt, None )
318
+ /// let canonical_addr = instantiate2_address(&checksum, &canonical_creator, salt)
319
319
/// .map_err(|_| StdError::generic_err("Could not calculate addr"))?;
320
320
/// let addr = deps.api.addr_humanize(&canonical_addr)?;
321
321
///
@@ -326,7 +326,19 @@ pub fn instantiate2_address(
326
326
checksum : & [ u8 ] ,
327
327
creator : & CanonicalAddr ,
328
328
salt : & [ u8 ] ,
329
- msg : Option < & [ u8 ] > ,
329
+ ) -> Result < CanonicalAddr , Instantiate2AddressError > {
330
+ instantiate2_address_impl ( checksum, creator, salt, b"" )
331
+ }
332
+
333
+ /// The instantiate2 address derivation implementation. This API is used for
334
+ /// testing puposes only. The `msg` field is discouraged and should not be used.
335
+ /// Use [`instantiate2_address`].
336
+ #[ doc( hidden) ]
337
+ fn instantiate2_address_impl (
338
+ checksum : & [ u8 ] ,
339
+ creator : & CanonicalAddr ,
340
+ salt : & [ u8 ] ,
341
+ msg : & [ u8 ] ,
330
342
) -> Result < CanonicalAddr , Instantiate2AddressError > {
331
343
if checksum. len ( ) != 32 {
332
344
return Err ( Instantiate2AddressError :: InvalidChecksumLength ) ;
@@ -336,8 +348,6 @@ pub fn instantiate2_address(
336
348
return Err ( Instantiate2AddressError :: InvalidSaltLength ) ;
337
349
} ;
338
350
339
- let msg = msg. unwrap_or_default ( ) ;
340
-
341
351
let mut key = Vec :: < u8 > :: new ( ) ;
342
352
key. extend_from_slice ( b"wasm\0 " ) ;
343
353
key. extend_from_slice ( & ( checksum. len ( ) as u64 ) . to_be_bytes ( ) ) ;
@@ -659,23 +669,23 @@ mod tests {
659
669
}
660
670
661
671
#[ test]
662
- fn instantiate2_address_works ( ) {
672
+ fn instantiate2_address_impl_works ( ) {
663
673
let checksum1 =
664
674
HexBinary :: from_hex ( "13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2a5" )
665
675
. unwrap ( ) ;
666
676
let creator1 = CanonicalAddr :: from ( hex ! ( "9999999999aaaaaaaaaabbbbbbbbbbcccccccccc" ) ) ;
667
677
let salt1 = hex ! ( "61" ) ;
668
678
let salt2 = hex ! ( "aabbccddeeffffeeddbbccddaa66551155aaaabbcc787878789900aabbccddeeffffeeddbbccddaa66551155aaaabbcc787878789900aabbbbcc221100acadae" ) ;
669
- let msg1: Option < & [ u8 ] > = None ;
670
- let msg2: Option < & [ u8 ] > = Some ( b"{}" ) ;
671
- let msg3: Option < & [ u8 ] > = Some ( b"{\" some\" :123,\" structure\" :{\" nested\" :[\" ok\" ,true]}}" ) ;
679
+ let msg1: & [ u8 ] = b"" ;
680
+ let msg2: & [ u8 ] = b"{}" ;
681
+ let msg3: & [ u8 ] = b"{\" some\" :123,\" structure\" :{\" nested\" :[\" ok\" ,true]}}" ;
672
682
673
683
// No msg
674
684
let expected = CanonicalAddr :: from ( hex ! (
675
685
"5e865d3e45ad3e961f77fd77d46543417ced44d924dc3e079b5415ff6775f847"
676
686
) ) ;
677
687
assert_eq ! (
678
- instantiate2_address ( & checksum1, & creator1, & salt1, msg1) . unwrap( ) ,
688
+ instantiate2_address_impl ( & checksum1, & creator1, & salt1, msg1) . unwrap( ) ,
679
689
expected
680
690
) ;
681
691
@@ -684,7 +694,7 @@ mod tests {
684
694
"0995499608947a5281e2c7ebd71bdb26a1ad981946dad57f6c4d3ee35de77835"
685
695
) ) ;
686
696
assert_eq ! (
687
- instantiate2_address ( & checksum1, & creator1, & salt1, msg2) . unwrap( ) ,
697
+ instantiate2_address_impl ( & checksum1, & creator1, & salt1, msg2) . unwrap( ) ,
688
698
expected
689
699
) ;
690
700
@@ -693,7 +703,7 @@ mod tests {
693
703
"83326e554723b15bac664ceabc8a5887e27003abe9fbd992af8c7bcea4745167"
694
704
) ) ;
695
705
assert_eq ! (
696
- instantiate2_address ( & checksum1, & creator1, & salt1, msg3) . unwrap( ) ,
706
+ instantiate2_address_impl ( & checksum1, & creator1, & salt1, msg3) . unwrap( ) ,
697
707
expected
698
708
) ;
699
709
@@ -702,42 +712,42 @@ mod tests {
702
712
"9384c6248c0bb171e306fd7da0993ec1e20eba006452a3a9e078883eb3594564"
703
713
) ) ;
704
714
assert_eq ! (
705
- instantiate2_address ( & checksum1, & creator1, & salt2, None ) . unwrap( ) ,
715
+ instantiate2_address_impl ( & checksum1, & creator1, & salt2, b"" ) . unwrap( ) ,
706
716
expected
707
717
) ;
708
718
709
719
// Salt too short or too long
710
720
let empty = Vec :: < u8 > :: new ( ) ;
711
721
assert ! ( matches!(
712
- instantiate2_address ( & checksum1, & creator1, & empty, None ) . unwrap_err( ) ,
722
+ instantiate2_address_impl ( & checksum1, & creator1, & empty, b"" ) . unwrap_err( ) ,
713
723
Instantiate2AddressError :: InvalidSaltLength
714
724
) ) ;
715
725
let too_long = vec ! [ 0x11 ; 65 ] ;
716
726
assert ! ( matches!(
717
- instantiate2_address ( & checksum1, & creator1, & too_long, None ) . unwrap_err( ) ,
727
+ instantiate2_address_impl ( & checksum1, & creator1, & too_long, b"" ) . unwrap_err( ) ,
718
728
Instantiate2AddressError :: InvalidSaltLength
719
729
) ) ;
720
730
721
731
// invalid checksum length
722
732
let broken_cs = hex ! ( "13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2" ) ;
723
733
assert ! ( matches!(
724
- instantiate2_address ( & broken_cs, & creator1, & salt1, None ) . unwrap_err( ) ,
734
+ instantiate2_address_impl ( & broken_cs, & creator1, & salt1, b"" ) . unwrap_err( ) ,
725
735
Instantiate2AddressError :: InvalidChecksumLength
726
736
) ) ;
727
737
let broken_cs = hex ! ( "" ) ;
728
738
assert ! ( matches!(
729
- instantiate2_address ( & broken_cs, & creator1, & salt1, None ) . unwrap_err( ) ,
739
+ instantiate2_address_impl ( & broken_cs, & creator1, & salt1, b"" ) . unwrap_err( ) ,
730
740
Instantiate2AddressError :: InvalidChecksumLength
731
741
) ) ;
732
742
let broken_cs = hex ! ( "13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2aaaa" ) ;
733
743
assert ! ( matches!(
734
- instantiate2_address ( & broken_cs, & creator1, & salt1, None ) . unwrap_err( ) ,
744
+ instantiate2_address_impl ( & broken_cs, & creator1, & salt1, b"" ) . unwrap_err( ) ,
735
745
Instantiate2AddressError :: InvalidChecksumLength
736
746
) ) ;
737
747
}
738
748
739
749
#[ test]
740
- fn instantiate2_address_works_for_cosmjs_testvectors ( ) {
750
+ fn instantiate2_address_impl_works_for_cosmjs_testvectors ( ) {
741
751
// Test data from https://github.com/cosmos/cosmjs/pull/1253
742
752
const COSMOS_ED25519_TESTS_JSON : & str = "./testdata/instantiate2_addresses.json" ;
743
753
@@ -793,12 +803,12 @@ mod tests {
793
803
out : _,
794
804
} in read_tests ( )
795
805
{
796
- let msg = input. msg . map ( |msg| msg. into_bytes ( ) ) ;
797
- let addr = instantiate2_address (
806
+ let msg = input. msg . map ( |msg| msg. into_bytes ( ) ) . unwrap_or_default ( ) ;
807
+ let addr = instantiate2_address_impl (
798
808
& input. checksum ,
799
809
& input. creator_data . into ( ) ,
800
810
& input. salt ,
801
- msg. as_deref ( ) ,
811
+ & msg,
802
812
)
803
813
. unwrap ( ) ;
804
814
assert_eq ! ( addr, intermediate. address_data) ;
0 commit comments