@@ -56,6 +56,60 @@ impl MockApi {
56
56
pub fn new_failing ( backend_error : & ' static str ) -> Self {
57
57
MockApi :: Error ( backend_error)
58
58
}
59
+
60
+ /// Returns [MockApi] with Bech32 prefix set to provided value.
61
+ ///
62
+ /// Bech32 prefix must not be empty.
63
+ ///
64
+ /// # Example
65
+ ///
66
+ /// ```
67
+ /// # use cosmwasm_std::Addr;
68
+ /// # use cosmwasm_std::testing::MockApi;
69
+ /// #
70
+ /// let mock_api = MockApi::default().with_prefix("juno");
71
+ /// let addr = mock_api.addr_make("creator");
72
+ ///
73
+ /// assert_eq!(addr, "juno1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqsksmtyp");
74
+ /// ```
75
+ pub fn with_prefix ( self , prefix : & ' static str ) -> Self {
76
+ Self :: Bech32 {
77
+ bech32_prefix : prefix,
78
+ }
79
+ }
80
+
81
+ /// Returns an address built from provided input string.
82
+ ///
83
+ /// # Example
84
+ ///
85
+ /// ```
86
+ /// # use cosmwasm_std::Addr;
87
+ /// # use cosmwasm_std::testing::MockApi;
88
+ /// #
89
+ /// let mock_api = MockApi::default();
90
+ /// let addr = mock_api.addr_make("creator");
91
+ ///
92
+ /// assert_eq!(addr, "cosmwasm1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqs8s7vcp");
93
+ /// ```
94
+ ///
95
+ /// # Panics
96
+ ///
97
+ /// This function panics when generating a valid address is not possible,
98
+ /// especially when Bech32 prefix set in function [with_prefix](Self::with_prefix) is empty.
99
+ ///
100
+ pub fn addr_make ( & self , input : & str ) -> String {
101
+ // handle error case
102
+ let bech32_prefix = match self {
103
+ MockApi :: Error ( e) => panic ! ( "Generating address failed: {e}" ) ,
104
+ MockApi :: Bech32 { bech32_prefix } => * bech32_prefix,
105
+ } ;
106
+
107
+ let digest = Sha256 :: digest ( input) . to_vec ( ) ;
108
+ match encode ( bech32_prefix, digest. to_base32 ( ) , Variant :: Bech32 ) {
109
+ Ok ( address) => address,
110
+ Err ( reason) => panic ! ( "Generating address failed with reason: {reason}" ) ,
111
+ }
112
+ }
59
113
}
60
114
61
115
impl Default for MockApi {
0 commit comments