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,131 @@ 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 rstest:: rstest;
48+ use strum:: { EnumCount , EnumDiscriminants , EnumIter , IntoEnumIterator } ;
49+
50+ use crypto:: {
51+ key:: { KeyKind , PrivateKey } ,
52+ vrf:: { VRFKeyKind , VRFPrivateKey , VRFPublicKey } ,
53+ } ;
54+ use test_utils:: random:: { gen_random_alnum_string, make_seedable_rng, Seed } ;
55+
56+ use crate :: {
57+ address:: { hexified:: HexifiedAddress , pubkeyhash:: PublicKeyHash , Address } ,
58+ chain:: {
59+ config:: create_regtest, tokens:: TokenId , DelegationId , Destination , DestinationTag ,
60+ OrderId , PoolId ,
61+ } ,
62+ primitives:: H256 ,
63+ } ;
64+
65+ #[ derive( PartialEq , Eq , PartialOrd , Ord , EnumCount , EnumDiscriminants ) ]
66+ #[ strum_discriminants( name( HexifiableTag ) , derive( EnumIter ) ) ]
67+ enum Hexifiable {
68+ Destination ( Destination ) ,
69+ PoolId ( PoolId ) ,
70+ DelegationId ( DelegationId ) ,
71+ TokenId ( TokenId ) ,
72+ OrderId ( OrderId ) ,
73+ VRFPublicKey ( VRFPublicKey ) ,
74+ }
75+
76+ #[ rstest]
77+ #[ trace]
78+ #[ case( Seed :: from_entropy( ) ) ]
79+ fn many_random_instances ( #[ case] seed : Seed ) {
80+ use crate :: address:: dehexify:: dehexify_all_addresses;
81+ use randomness:: seq:: IteratorRandom ;
82+
83+ let mut rng = make_seedable_rng ( seed) ;
84+ let chain_config = create_regtest ( ) ;
85+
86+ let strings = ( 0 ..100 )
87+ . map ( |_| gen_random_alnum_string ( & mut rng, 0 , 50 ) )
88+ . collect :: < Vec < String > > ( ) ;
89+
90+ let keys = ( 0 ..strings. len ( ) )
91+ . map ( |_| match HexifiableTag :: iter ( ) . choose ( & mut rng) . unwrap ( ) {
92+ HexifiableTag :: Destination => {
93+ let dest = match DestinationTag :: iter ( ) . choose ( & mut rng) . unwrap ( ) {
94+ DestinationTag :: AnyoneCanSpend => Destination :: AnyoneCanSpend ,
95+ DestinationTag :: PublicKey => {
96+ let ( _private_key, public_key) =
97+ PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
98+ Destination :: PublicKey ( public_key)
99+ }
100+ DestinationTag :: PublicKeyHash => {
101+ let ( _private_key, public_key) =
102+ PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
103+ Destination :: PublicKeyHash ( PublicKeyHash :: from ( & public_key) )
104+ }
105+ DestinationTag :: ScriptHash => Destination :: ScriptHash (
106+ crate :: primitives:: Id :: new ( H256 :: random_using ( & mut rng) ) ,
107+ ) ,
108+ DestinationTag :: ClassicMultisig => {
109+ let ( _private_key, public_key) =
110+ PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
111+ Destination :: ClassicMultisig ( PublicKeyHash :: from ( & public_key) )
112+ }
113+ } ;
114+ Hexifiable :: Destination ( dest)
115+ }
116+ HexifiableTag :: PoolId => Hexifiable :: PoolId ( PoolId :: random_using ( & mut rng) ) ,
117+ HexifiableTag :: DelegationId => {
118+ Hexifiable :: DelegationId ( DelegationId :: random_using ( & mut rng) )
119+ }
120+ HexifiableTag :: TokenId => Hexifiable :: TokenId ( TokenId :: random_using ( & mut rng) ) ,
121+ HexifiableTag :: OrderId => Hexifiable :: OrderId ( OrderId :: random_using ( & mut rng) ) ,
122+ HexifiableTag :: VRFPublicKey => Hexifiable :: VRFPublicKey (
123+ VRFPrivateKey :: new_from_rng ( & mut rng, VRFKeyKind :: Schnorrkel ) . 1 ,
124+ ) ,
125+ } )
126+ . collect :: < Vec < _ > > ( ) ;
127+
128+ let final_str = strings
129+ . iter ( )
130+ . zip ( keys. iter ( ) )
131+ . map ( |( s, k) | {
132+ let hexified = match k {
133+ Hexifiable :: Destination ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
134+ Hexifiable :: PoolId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
135+ Hexifiable :: DelegationId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
136+ Hexifiable :: TokenId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
137+ Hexifiable :: OrderId ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
138+ Hexifiable :: VRFPublicKey ( d) => HexifiedAddress :: new ( d) . to_string ( ) ,
139+ } ;
140+ s. clone ( ) + & hexified
141+ } )
142+ . collect :: < Vec < _ > > ( )
143+ . join ( "" ) ;
144+
145+ let to_test = dehexify_all_addresses ( & chain_config, & final_str) ;
146+
147+ let expected = strings
148+ . iter ( )
149+ . zip ( keys. into_iter ( ) )
150+ . map ( |( s, k) | {
151+ let address_str = match k {
152+ Hexifiable :: Destination ( d) => {
153+ Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( )
154+ }
155+ Hexifiable :: PoolId ( d) => Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( ) ,
156+ Hexifiable :: DelegationId ( d) => {
157+ Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( )
158+ }
159+ Hexifiable :: TokenId ( d) => Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( ) ,
160+ Hexifiable :: OrderId ( d) => Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( ) ,
161+ Hexifiable :: VRFPublicKey ( d) => {
162+ Address :: new ( & chain_config, d) . unwrap ( ) . to_string ( )
163+ }
164+ } ;
165+ s. clone ( ) + & address_str
166+ } )
167+ . collect :: < Vec < _ > > ( )
168+ . join ( "" ) ;
169+
170+ assert_eq ! ( to_test, expected) ;
171+ }
172+ }
0 commit comments