@@ -315,7 +315,7 @@ pub enum Instantiate2AddressError {
315315/// let canonical_creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
316316/// let checksum = HexBinary::from_hex("9af782a3a1bcbcd22dbb6a45c751551d9af782a3a1bcbcd22dbb6a45c751551d")?;
317317/// 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)
319319/// .map_err(|_| StdError::generic_err("Could not calculate addr"))?;
320320/// let addr = deps.api.addr_humanize(&canonical_addr)?;
321321///
@@ -326,7 +326,19 @@ pub fn instantiate2_address(
326326 checksum : & [ u8 ] ,
327327 creator : & CanonicalAddr ,
328328 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 ] ,
330342) -> Result < CanonicalAddr , Instantiate2AddressError > {
331343 if checksum. len ( ) != 32 {
332344 return Err ( Instantiate2AddressError :: InvalidChecksumLength ) ;
@@ -336,8 +348,6 @@ pub fn instantiate2_address(
336348 return Err ( Instantiate2AddressError :: InvalidSaltLength ) ;
337349 } ;
338350
339- let msg = msg. unwrap_or_default ( ) ;
340-
341351 let mut key = Vec :: < u8 > :: new ( ) ;
342352 key. extend_from_slice ( b"wasm\0 " ) ;
343353 key. extend_from_slice ( & ( checksum. len ( ) as u64 ) . to_be_bytes ( ) ) ;
@@ -659,23 +669,23 @@ mod tests {
659669 }
660670
661671 #[ test]
662- fn instantiate2_address_works ( ) {
672+ fn instantiate2_address_impl_works ( ) {
663673 let checksum1 =
664674 HexBinary :: from_hex ( "13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2a5" )
665675 . unwrap ( ) ;
666676 let creator1 = CanonicalAddr :: from ( hex ! ( "9999999999aaaaaaaaaabbbbbbbbbbcccccccccc" ) ) ;
667677 let salt1 = hex ! ( "61" ) ;
668678 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]}}" ;
672682
673683 // No msg
674684 let expected = CanonicalAddr :: from ( hex ! (
675685 "5e865d3e45ad3e961f77fd77d46543417ced44d924dc3e079b5415ff6775f847"
676686 ) ) ;
677687 assert_eq ! (
678- instantiate2_address ( & checksum1, & creator1, & salt1, msg1) . unwrap( ) ,
688+ instantiate2_address_impl ( & checksum1, & creator1, & salt1, msg1) . unwrap( ) ,
679689 expected
680690 ) ;
681691
@@ -684,7 +694,7 @@ mod tests {
684694 "0995499608947a5281e2c7ebd71bdb26a1ad981946dad57f6c4d3ee35de77835"
685695 ) ) ;
686696 assert_eq ! (
687- instantiate2_address ( & checksum1, & creator1, & salt1, msg2) . unwrap( ) ,
697+ instantiate2_address_impl ( & checksum1, & creator1, & salt1, msg2) . unwrap( ) ,
688698 expected
689699 ) ;
690700
@@ -693,7 +703,7 @@ mod tests {
693703 "83326e554723b15bac664ceabc8a5887e27003abe9fbd992af8c7bcea4745167"
694704 ) ) ;
695705 assert_eq ! (
696- instantiate2_address ( & checksum1, & creator1, & salt1, msg3) . unwrap( ) ,
706+ instantiate2_address_impl ( & checksum1, & creator1, & salt1, msg3) . unwrap( ) ,
697707 expected
698708 ) ;
699709
@@ -702,42 +712,42 @@ mod tests {
702712 "9384c6248c0bb171e306fd7da0993ec1e20eba006452a3a9e078883eb3594564"
703713 ) ) ;
704714 assert_eq ! (
705- instantiate2_address ( & checksum1, & creator1, & salt2, None ) . unwrap( ) ,
715+ instantiate2_address_impl ( & checksum1, & creator1, & salt2, b"" ) . unwrap( ) ,
706716 expected
707717 ) ;
708718
709719 // Salt too short or too long
710720 let empty = Vec :: < u8 > :: new ( ) ;
711721 assert ! ( matches!(
712- instantiate2_address ( & checksum1, & creator1, & empty, None ) . unwrap_err( ) ,
722+ instantiate2_address_impl ( & checksum1, & creator1, & empty, b"" ) . unwrap_err( ) ,
713723 Instantiate2AddressError :: InvalidSaltLength
714724 ) ) ;
715725 let too_long = vec ! [ 0x11 ; 65 ] ;
716726 assert ! ( matches!(
717- instantiate2_address ( & checksum1, & creator1, & too_long, None ) . unwrap_err( ) ,
727+ instantiate2_address_impl ( & checksum1, & creator1, & too_long, b"" ) . unwrap_err( ) ,
718728 Instantiate2AddressError :: InvalidSaltLength
719729 ) ) ;
720730
721731 // invalid checksum length
722732 let broken_cs = hex ! ( "13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2" ) ;
723733 assert ! ( matches!(
724- instantiate2_address ( & broken_cs, & creator1, & salt1, None ) . unwrap_err( ) ,
734+ instantiate2_address_impl ( & broken_cs, & creator1, & salt1, b"" ) . unwrap_err( ) ,
725735 Instantiate2AddressError :: InvalidChecksumLength
726736 ) ) ;
727737 let broken_cs = hex ! ( "" ) ;
728738 assert ! ( matches!(
729- instantiate2_address ( & broken_cs, & creator1, & salt1, None ) . unwrap_err( ) ,
739+ instantiate2_address_impl ( & broken_cs, & creator1, & salt1, b"" ) . unwrap_err( ) ,
730740 Instantiate2AddressError :: InvalidChecksumLength
731741 ) ) ;
732742 let broken_cs = hex ! ( "13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2aaaa" ) ;
733743 assert ! ( matches!(
734- instantiate2_address ( & broken_cs, & creator1, & salt1, None ) . unwrap_err( ) ,
744+ instantiate2_address_impl ( & broken_cs, & creator1, & salt1, b"" ) . unwrap_err( ) ,
735745 Instantiate2AddressError :: InvalidChecksumLength
736746 ) ) ;
737747 }
738748
739749 #[ test]
740- fn instantiate2_address_works_for_cosmjs_testvectors ( ) {
750+ fn instantiate2_address_impl_works_for_cosmjs_testvectors ( ) {
741751 // Test data from https://github.com/cosmos/cosmjs/pull/1253
742752 const COSMOS_ED25519_TESTS_JSON : & str = "./testdata/instantiate2_addresses.json" ;
743753
@@ -793,12 +803,12 @@ mod tests {
793803 out : _,
794804 } in read_tests ( )
795805 {
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 (
798808 & input. checksum ,
799809 & input. creator_data . into ( ) ,
800810 & input. salt ,
801- msg. as_deref ( ) ,
811+ & msg,
802812 )
803813 . unwrap ( ) ;
804814 assert_eq ! ( addr, intermediate. address_data) ;
0 commit comments