@@ -2053,29 +2053,88 @@ impl_runtime_apis! {
20532053 }
20542054}
20552055
2056- // #[cfg(test)]
2057- // mod tests {
2058-
2059- #[ test]
2060- fn check_whitelist ( ) {
2061- use crate :: * ;
2062- use frame_support:: traits:: WhitelistedStorageKeys ;
2063- use sp_core:: hexdisplay:: HexDisplay ;
2064- use std:: collections:: HashSet ;
2065- let whitelist: HashSet < String > = AllPalletsWithSystem :: whitelisted_storage_keys ( )
2066- . iter ( )
2067- . map ( |e| HexDisplay :: from ( & e. key ) . to_string ( ) )
2068- . collect ( ) ;
2069-
2070- // Block Number
2071- assert ! ( whitelist. contains( "26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac" ) ) ;
2072- // Total Issuance
2073- assert ! ( whitelist. contains( "c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80" ) ) ;
2074- // Execution Phase
2075- assert ! ( whitelist. contains( "26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a" ) ) ;
2076- // Event Count
2077- assert ! ( whitelist. contains( "26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850" ) ) ;
2078- // System Events
2079- assert ! ( whitelist. contains( "26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7" ) ) ;
2080- }
2081- // }
2056+ #[ cfg( test) ]
2057+ mod tests {
2058+ use ark_serialize:: CanonicalDeserialize ;
2059+ use etf_crypto_primitives:: { ibe:: fullident:: Identity , tlock:: tlock:: tle} ;
2060+ use rand_chacha:: rand_core:: SeedableRng ;
2061+ use rand_chacha:: ChaCha20Rng ;
2062+ use sha2:: Digest ;
2063+ use w3f_bls:: { EngineBLS , Message , TinyBLS381 } ;
2064+
2065+ #[ test]
2066+ fn tlock_encrypt_decrypt_drand_works ( ) {
2067+ // using a pulse from drand's QuickNet
2068+ // https://api.drand.sh/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/public/1000
2069+ // the beacon public key
2070+ let pk_bytes = b"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a" ;
2071+ // a round number that we know a signature for
2072+ let round: u64 = 1000 ;
2073+ // the signature produced in that round
2074+ let signature = b"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39" ;
2075+ // Convert hex string to bytes
2076+ let pub_key_bytes = hex:: decode ( pk_bytes) . expect ( "Decoding failed" ) ;
2077+ // Deserialize to G1Affine
2078+ let pub_key =
2079+ <TinyBLS381 as EngineBLS >:: PublicKeyGroup :: deserialize_compressed ( & * pub_key_bytes)
2080+ . unwrap ( ) ;
2081+ // then we tlock a message for the pubkey
2082+ let plaintext = b"this is a test" . as_slice ( ) ;
2083+ let esk = [ 2 ; 32 ] ;
2084+ let id: & Vec < u8 > = {
2085+ let mut hasher = sha2:: Sha256 :: new ( ) ;
2086+ hasher. update ( & round. to_be_bytes ( ) ) ;
2087+ & hasher. finalize ( ) . to_vec ( )
2088+ } ;
2089+ let sig_bytes = hex:: decode ( signature) . expect ( "The signature should be well formatted" ) ;
2090+ let sig =
2091+ <TinyBLS381 as EngineBLS >:: SignatureGroup :: deserialize_compressed ( & * sig_bytes) . unwrap ( ) ;
2092+ let message = {
2093+ let mut hasher = sha2:: Sha256 :: new ( ) ;
2094+ hasher. update ( round. to_be_bytes ( ) ) ;
2095+ hasher. finalize ( ) . to_vec ( )
2096+ } ;
2097+ let pk = w3f_bls:: PublicKey :: < TinyBLS381 > ( pub_key) ;
2098+ // Create message object and verify
2099+ let msg = Message :: new ( b"" , & message) ;
2100+ let identity = Identity :: new ( b"" , vec ! [ msg. 1 . to_vec( ) ] ) ;
2101+ let rng = ChaCha20Rng :: seed_from_u64 ( 0 ) ;
2102+ let ct = tle :: < TinyBLS381 , ChaCha20Rng > ( pub_key, esk, plaintext, identity, rng) . unwrap ( ) ;
2103+ // then we can decrypt the ciphertext using the signature
2104+ let result = ct. tld ( sig) . unwrap ( ) ;
2105+ assert ! ( result. message == plaintext) ;
2106+ }
2107+
2108+ #[ test]
2109+ fn check_whitelist ( ) {
2110+ use crate :: * ;
2111+ use frame_support:: traits:: WhitelistedStorageKeys ;
2112+ use sp_core:: hexdisplay:: HexDisplay ;
2113+ use std:: collections:: HashSet ;
2114+ let whitelist: HashSet < String > = AllPalletsWithSystem :: whitelisted_storage_keys ( )
2115+ . iter ( )
2116+ . map ( |e| HexDisplay :: from ( & e. key ) . to_string ( ) )
2117+ . collect ( ) ;
2118+
2119+ // Block Number
2120+ assert ! (
2121+ whitelist. contains( "26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac" )
2122+ ) ;
2123+ // Total Issuance
2124+ assert ! (
2125+ whitelist. contains( "c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80" )
2126+ ) ;
2127+ // Execution Phase
2128+ assert ! (
2129+ whitelist. contains( "26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a" )
2130+ ) ;
2131+ // Event Count
2132+ assert ! (
2133+ whitelist. contains( "26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850" )
2134+ ) ;
2135+ // System Events
2136+ assert ! (
2137+ whitelist. contains( "26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7" )
2138+ ) ;
2139+ }
2140+ }
0 commit comments