@@ -166,19 +166,23 @@ async fn verify_google_oauth2(
166
166
#[ cfg( test) ]
167
167
mod tests {
168
168
use super :: * ;
169
- use executor_crypto:: { sr25519:: Pair , PairTrait } ;
170
- use executor_primitives:: { utils:: hex:: ToHexPrefixed , Hashable , Identity } ;
169
+ use alloy_signer:: SignerSync ;
170
+ use alloy_signer_local:: PrivateKeySigner ;
171
+ use executor_crypto:: { ed25519, sr25519, PairTrait } ;
172
+ use executor_primitives:: {
173
+ signature:: EthereumSignature , utils:: hex:: ToHexPrefixed , Hashable , Identity ,
174
+ } ;
171
175
use heima_identity_verification:: helpers:: generate_otp;
172
176
use tempfile:: tempdir;
173
177
174
178
#[ test]
175
- fn test_verify_web3_authentication ( ) {
179
+ fn test_verify_substrate_authentication ( ) {
176
180
let tmp_dir = tempdir ( ) . unwrap ( ) ;
177
181
let storage_db = Arc :: new ( StorageDB :: open_default ( tmp_dir. path ( ) ) . unwrap ( ) ) ;
178
182
179
- let alice = Pair :: from_string ( "//Alice" , None ) . unwrap ( ) ;
183
+ let alice = sr25519 :: Pair :: from_string ( "//Alice" , None ) . unwrap ( ) ;
180
184
let public_key: [ u8 ; 32 ] = alice. public ( ) . into ( ) ;
181
- let alice_identity = Identity :: from ( public_key) ;
185
+ let alice_identity = Identity :: Substrate ( public_key. into ( ) ) ;
182
186
let client_id = "test_client" . to_string ( ) ;
183
187
let alice_omni_account = alice_identity. to_omni_account_with_client_id ( & client_id) ;
184
188
let verification_code_storage = VerificationCodeStorage :: new ( storage_db. clone ( ) ) ;
@@ -203,4 +207,170 @@ mod tests {
203
207
verify_web3_authentication ( storage_db, & client_id, & alice_identity, & multi_signature) ;
204
208
assert ! ( result. is_ok( ) ) ;
205
209
}
210
+
211
+ #[ test]
212
+ fn test_verify_solana_authentication ( ) {
213
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
214
+ let storage_db = Arc :: new ( StorageDB :: open_default ( tmp_dir. path ( ) ) . unwrap ( ) ) ;
215
+
216
+ // Create Ed25519 keypair for Solana
217
+ let ( keypair, _) = ed25519:: Pair :: generate ( ) ;
218
+ let public_key: [ u8 ; 32 ] = keypair. public ( ) . into ( ) ;
219
+ let solana_identity = Identity :: Solana ( public_key. into ( ) ) ;
220
+ let client_id = "test_client_solana" . to_string ( ) ;
221
+ let solana_omni_account = solana_identity. to_omni_account_with_client_id ( & client_id) ;
222
+ let verification_code_storage = VerificationCodeStorage :: new ( storage_db. clone ( ) ) ;
223
+ let message_code = generate_otp ( 8 ) ;
224
+
225
+ verification_code_storage
226
+ . insert ( & solana_omni_account. hash ( ) , message_code. clone ( ) )
227
+ . expect ( "insert" ) ;
228
+
229
+ let message = HeimaMessagePayload {
230
+ message_code,
231
+ omni_account : solana_omni_account. to_hex ( ) ,
232
+ client_id : client_id. to_string ( ) ,
233
+ } ;
234
+
235
+ let payload = serde_json:: to_string ( & message) . expect ( "serialize" ) ;
236
+
237
+ let signature = keypair. sign ( payload. as_bytes ( ) ) ;
238
+ let multi_signature = HeimaMultiSignature :: from ( signature) ;
239
+
240
+ let result =
241
+ verify_web3_authentication ( storage_db, & client_id, & solana_identity, & multi_signature) ;
242
+ assert ! ( result. is_ok( ) ) ;
243
+ }
244
+
245
+ #[ test]
246
+ fn test_verify_evm_authentication ( ) {
247
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
248
+ let storage_db = Arc :: new ( StorageDB :: open_default ( tmp_dir. path ( ) ) . unwrap ( ) ) ;
249
+
250
+ let evm_signer = PrivateKeySigner :: random ( ) ;
251
+ let signer_address = evm_signer. address ( ) ;
252
+ let evm_identity = Identity :: Evm ( signer_address. 0 . as_slice ( ) . try_into ( ) . unwrap ( ) ) ;
253
+ let client_id = "test_client_evm" . to_string ( ) ;
254
+ let evm_omni_account = evm_identity. to_omni_account_with_client_id ( & client_id) ;
255
+ let verification_code_storage = VerificationCodeStorage :: new ( storage_db. clone ( ) ) ;
256
+ let message_code = generate_otp ( 8 ) ;
257
+
258
+ verification_code_storage
259
+ . insert ( & evm_omni_account. hash ( ) , message_code. clone ( ) )
260
+ . expect ( "insert" ) ;
261
+
262
+ let message = HeimaMessagePayload {
263
+ message_code,
264
+ omni_account : evm_omni_account. to_hex ( ) ,
265
+ client_id : client_id. to_string ( ) ,
266
+ } ;
267
+
268
+ let payload = serde_json:: to_string ( & message) . expect ( "serialize" ) ;
269
+ let signature = evm_signer. sign_message_sync ( payload. as_bytes ( ) ) . expect ( "sign message" ) ;
270
+
271
+ let ethereum_signature = EthereumSignature ( signature. into ( ) ) ;
272
+ let multi_signature = HeimaMultiSignature :: Ethereum ( ethereum_signature) ;
273
+
274
+ let result =
275
+ verify_web3_authentication ( storage_db, & client_id, & evm_identity, & multi_signature) ;
276
+ assert ! ( result. is_ok( ) ) ;
277
+ }
278
+
279
+ #[ test]
280
+ fn test_verify_web3_authentication_invalid_signature ( ) {
281
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
282
+ let storage_db = Arc :: new ( StorageDB :: open_default ( tmp_dir. path ( ) ) . unwrap ( ) ) ;
283
+
284
+ let alice = sr25519:: Pair :: from_string ( "//Alice" , None ) . unwrap ( ) ;
285
+ let bob = sr25519:: Pair :: from_string ( "//Bob" , None ) . unwrap ( ) ;
286
+
287
+ let alice_public_key: [ u8 ; 32 ] = alice. public ( ) . into ( ) ;
288
+ let alice_identity = Identity :: from ( alice_public_key) ;
289
+ let client_id = "test_client" . to_string ( ) ;
290
+ let alice_omni_account = alice_identity. to_omni_account_with_client_id ( & client_id) ;
291
+ let verification_code_storage = VerificationCodeStorage :: new ( storage_db. clone ( ) ) ;
292
+ let message_code = generate_otp ( 8 ) ;
293
+
294
+ verification_code_storage
295
+ . insert ( & alice_omni_account. hash ( ) , message_code. clone ( ) )
296
+ . expect ( "insert" ) ;
297
+
298
+ let message = HeimaMessagePayload {
299
+ message_code,
300
+ omni_account : alice_omni_account. to_hex ( ) ,
301
+ client_id : client_id. to_string ( ) ,
302
+ } ;
303
+
304
+ let payload = serde_json:: to_string ( & message) . expect ( "serialize" ) ;
305
+
306
+ // Sign with Bob's key but try to verify with Alice's identity
307
+ let signature = bob. sign ( payload. as_bytes ( ) ) ;
308
+ let multi_signature = HeimaMultiSignature :: from ( signature) ;
309
+
310
+ let result =
311
+ verify_web3_authentication ( storage_db, & client_id, & alice_identity, & multi_signature) ;
312
+ assert_eq ! ( result, Err ( AuthenticationError :: Web3InvalidSignature ) ) ;
313
+ }
314
+
315
+ #[ test]
316
+ fn test_verify_web3_authentication_missing_verification_code ( ) {
317
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
318
+ let storage_db = Arc :: new ( StorageDB :: open_default ( tmp_dir. path ( ) ) . unwrap ( ) ) ;
319
+
320
+ let alice = sr25519:: Pair :: from_string ( "//Alice" , None ) . unwrap ( ) ;
321
+ let public_key: [ u8 ; 32 ] = alice. public ( ) . into ( ) ;
322
+ let alice_identity = Identity :: from ( public_key) ;
323
+ let client_id = "test_client" . to_string ( ) ;
324
+
325
+ let alice_omni_account = alice_identity. to_omni_account_with_client_id ( & client_id) ;
326
+ let message_code = generate_otp ( 8 ) ;
327
+
328
+ let message = HeimaMessagePayload {
329
+ message_code,
330
+ omni_account : alice_omni_account. to_hex ( ) ,
331
+ client_id : client_id. to_string ( ) ,
332
+ } ;
333
+
334
+ let payload = serde_json:: to_string ( & message) . expect ( "serialize" ) ;
335
+ let signature = alice. sign ( payload. as_bytes ( ) ) ;
336
+ let multi_signature = HeimaMultiSignature :: from ( signature) ;
337
+
338
+ // Don't insert verification code
339
+ let result =
340
+ verify_web3_authentication ( storage_db, & client_id, & alice_identity, & multi_signature) ;
341
+ assert_eq ! ( result, Err ( AuthenticationError :: VerificationCodeNotFound ) ) ;
342
+ }
343
+
344
+ #[ test]
345
+ fn test_verify_web3_authentication_invalid_verification_code ( ) {
346
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
347
+ let storage_db = Arc :: new ( StorageDB :: open_default ( tmp_dir. path ( ) ) . unwrap ( ) ) ;
348
+
349
+ let alice = sr25519:: Pair :: from_string ( "//Alice" , None ) . unwrap ( ) ;
350
+ let public_key: [ u8 ; 32 ] = alice. public ( ) . into ( ) ;
351
+ let alice_identity = Identity :: from ( public_key) ;
352
+ let client_id = "test_client" . to_string ( ) ;
353
+
354
+ let alice_omni_account = alice_identity. to_omni_account_with_client_id ( & client_id) ;
355
+ let verification_code_storage = VerificationCodeStorage :: new ( storage_db. clone ( ) ) ;
356
+ let message_code = generate_otp ( 8 ) ;
357
+
358
+ verification_code_storage
359
+ . insert ( & alice_omni_account. hash ( ) , message_code. clone ( ) )
360
+ . expect ( "insert" ) ;
361
+
362
+ let message = HeimaMessagePayload {
363
+ message_code : "invalid_code" . to_string ( ) , // Use an invalid code
364
+ omni_account : alice_omni_account. to_hex ( ) ,
365
+ client_id : client_id. to_string ( ) ,
366
+ } ;
367
+
368
+ let payload = serde_json:: to_string ( & message) . expect ( "serialize" ) ;
369
+ let signature = alice. sign ( payload. as_bytes ( ) ) ;
370
+ let multi_signature = HeimaMultiSignature :: from ( signature) ;
371
+
372
+ let result =
373
+ verify_web3_authentication ( storage_db, & client_id, & alice_identity, & multi_signature) ;
374
+ assert_eq ! ( result, Err ( AuthenticationError :: Web3InvalidSignature ) ) ;
375
+ }
206
376
}
0 commit comments