1313// See the License for the specific language governing permissions and
1414// limitations under the License.
1515
16+ use crypto:: vrf:: VRFPublicKey ;
1617use serialization:: json_encoded:: JsonEncoded ;
1718
18- use crate :: chain:: { tokens:: TokenId , ChainConfig , DelegationId , Destination , PoolId } ;
19+ use crate :: chain:: { tokens:: TokenId , ChainConfig , DelegationId , Destination , OrderId , PoolId } ;
1920
2021use super :: hexified:: HexifiedAddress ;
2122
@@ -25,6 +26,8 @@ pub fn dehexify_all_addresses(conf: &ChainConfig, input: &str) -> String {
2526 let result = HexifiedAddress :: < PoolId > :: replace_with_address ( conf, & result) . to_string ( ) ;
2627 let result = HexifiedAddress :: < DelegationId > :: replace_with_address ( conf, & result) . to_string ( ) ;
2728 let result = HexifiedAddress :: < TokenId > :: replace_with_address ( conf, & result) . to_string ( ) ;
29+ let result = HexifiedAddress :: < OrderId > :: replace_with_address ( conf, & result) . to_string ( ) ;
30+ let result = HexifiedAddress :: < VRFPublicKey > :: replace_with_address ( conf, & result) . to_string ( ) ;
2831
2932 result
3033}
@@ -39,3 +42,144 @@ pub fn to_dehexified_json<T: serde::Serialize>(
3942}
4043
4144// TODO: add tests that create blocks, and ensure the replacement in json works properly.
45+ #[ cfg( test) ]
46+ mod tests {
47+ use crypto:: {
48+ key:: { KeyKind , PrivateKey } ,
49+ vrf:: { VRFKeyKind , VRFPrivateKey , VRFPublicKey } ,
50+ } ;
51+ use rstest:: rstest;
52+ use strum:: { EnumCount , EnumDiscriminants , EnumIter , IntoEnumIterator } ;
53+ use test_utils:: random:: { make_seedable_rng, Rng , Seed } ;
54+
55+ use crate :: {
56+ address:: { hexified:: HexifiedAddress , pubkeyhash:: PublicKeyHash , Address } ,
57+ chain:: {
58+ config:: create_regtest, tokens:: TokenId , DelegationId , Destination , DestinationTag ,
59+ OrderId , PoolId ,
60+ } ,
61+ primitives:: H256 ,
62+ } ;
63+
64+ fn random_string ( length : usize , rng : & mut impl Rng ) -> String {
65+ rng. sample_iter ( & randomness:: distributions:: Alphanumeric )
66+ . take ( length)
67+ . map ( char:: from)
68+ . collect ( )
69+ }
70+
71+ #[ derive( PartialEq , Eq , PartialOrd , Ord , EnumCount , EnumDiscriminants ) ]
72+ #[ strum_discriminants( name( HexifiableTag ) , derive( EnumIter ) ) ]
73+ enum Hexifiable {
74+ Destination ( Destination ) ,
75+ PoolId ( PoolId ) ,
76+ DelegationId ( DelegationId ) ,
77+ TokenId ( TokenId ) ,
78+ OrderId ( OrderId ) ,
79+ VRFPublicKey ( VRFPublicKey ) ,
80+ }
81+
82+ #[ rstest]
83+ #[ trace]
84+ #[ case( Seed :: from_entropy( ) ) ]
85+ fn many_random_instances ( #[ case] seed : Seed ) {
86+ use crate :: address:: dehexify:: dehexify_all_addresses;
87+ use randomness:: seq:: IteratorRandom ;
88+
89+ let mut rng = make_seedable_rng ( seed) ;
90+ let chain_config = create_regtest ( ) ;
91+
92+ let strings = ( 0 ..100 )
93+ . map ( |_| {
94+ let size = rng. gen :: < usize > ( ) % 50 ;
95+ random_string ( size, & mut rng)
96+ } )
97+ . collect :: < Vec < String > > ( ) ;
98+
99+ let keys = ( 0 ..strings. len ( ) )
100+ . map ( |_| {
101+ //
102+
103+ match HexifiableTag :: iter ( ) . choose ( & mut rng) . unwrap ( ) {
104+ HexifiableTag :: Destination => {
105+ let dest = match DestinationTag :: iter ( ) . choose ( & mut rng) . unwrap ( ) {
106+ DestinationTag :: AnyoneCanSpend => Destination :: AnyoneCanSpend ,
107+ DestinationTag :: PublicKey => {
108+ let ( _private_key, public_key) =
109+ PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
110+ Destination :: PublicKey ( public_key)
111+ }
112+ DestinationTag :: PublicKeyHash => {
113+ let ( _private_key, public_key) =
114+ PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
115+ Destination :: PublicKeyHash ( PublicKeyHash :: from ( & public_key) )
116+ }
117+ DestinationTag :: ScriptHash => Destination :: ScriptHash (
118+ crate :: primitives:: Id :: new ( H256 :: random_using ( & mut rng) ) ,
119+ ) ,
120+ DestinationTag :: ClassicMultisig => {
121+ let ( _private_key, public_key) =
122+ PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
123+ Destination :: ClassicMultisig ( PublicKeyHash :: from ( & public_key) )
124+ }
125+ } ;
126+ Hexifiable :: Destination ( dest)
127+ }
128+ HexifiableTag :: PoolId => Hexifiable :: PoolId ( PoolId :: random_using ( & mut rng) ) ,
129+ HexifiableTag :: DelegationId => {
130+ Hexifiable :: DelegationId ( DelegationId :: random_using ( & mut rng) )
131+ }
132+ HexifiableTag :: TokenId => Hexifiable :: TokenId ( TokenId :: random_using ( & mut rng) ) ,
133+ HexifiableTag :: OrderId => Hexifiable :: OrderId ( OrderId :: random_using ( & mut rng) ) ,
134+ HexifiableTag :: VRFPublicKey => Hexifiable :: VRFPublicKey (
135+ VRFPrivateKey :: new_from_rng ( & mut rng, VRFKeyKind :: Schnorrkel ) . 1 ,
136+ ) ,
137+ }
138+ } )
139+ . collect :: < Vec < _ > > ( ) ;
140+
141+ let final_str = strings
142+ . iter ( )
143+ . zip ( keys. iter ( ) )
144+ . map ( |( s, k) | {
145+ let hexified = match k {
146+ Hexifiable :: Destination ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
147+ Hexifiable :: PoolId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
148+ Hexifiable :: DelegationId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
149+ Hexifiable :: TokenId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
150+ Hexifiable :: OrderId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
151+ Hexifiable :: VRFPublicKey ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
152+ } ;
153+ s. clone ( ) + & hexified
154+ } )
155+ . collect :: < Vec < _ > > ( )
156+ . join ( "" ) ;
157+
158+ let to_test = dehexify_all_addresses ( & chain_config, & final_str) ;
159+
160+ let expected = strings
161+ . iter ( )
162+ . zip ( keys. into_iter ( ) )
163+ . map ( |( s, k) | {
164+ let address_str = match k {
165+ Hexifiable :: Destination ( d) => {
166+ Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( )
167+ }
168+ Hexifiable :: PoolId ( d) => Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( ) ,
169+ Hexifiable :: DelegationId ( d) => {
170+ Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( )
171+ }
172+ Hexifiable :: TokenId ( d) => Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( ) ,
173+ Hexifiable :: OrderId ( d) => Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( ) ,
174+ Hexifiable :: VRFPublicKey ( d) => {
175+ Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( )
176+ }
177+ } ;
178+ s. clone ( ) + & address_str
179+ } )
180+ . collect :: < Vec < _ > > ( )
181+ . join ( "" ) ;
182+
183+ assert_eq ! ( to_test, expected) ;
184+ }
185+ }
0 commit comments