From 5c25d6a1a48f7d5984170aa501d5567281ba1a4c Mon Sep 17 00:00:00 2001 From: xjd Date: Tue, 2 Apr 2024 09:12:47 +0800 Subject: [PATCH] Add solana test cases --- tests/omni_lock_rust/tests/misc.rs | 15 ++++++ tests/omni_lock_rust/tests/test_omni_lock.rs | 50 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/tests/omni_lock_rust/tests/misc.rs b/tests/omni_lock_rust/tests/misc.rs index c1c678f..2052b3a 100644 --- a/tests/omni_lock_rust/tests/misc.rs +++ b/tests/omni_lock_rust/tests/misc.rs @@ -91,6 +91,7 @@ pub const ERROR_SIGHASHALL_DUP: i8 = 113; pub const MOL2_ERR_OVERFLOW: i8 = 8; // parse witnesses error pub const ERROR_IDENTITY_WRONG_ARGS: i8 = 71; +pub const ERROR_MISMATCHED: i8 = 73; pub const ERROR_ARGS_FORMAT: i8 = 87; // https://github.com/bitcoin-core/secp256k1/blob/d373bf6d08c82ac5496bf8103698c9f54d8d99d2/include/secp256k1.h#L219 @@ -613,7 +614,19 @@ pub fn sign_tx_by_input_group( let verifying_key = signing_key.verifying_key(); let mut sig_plus_pubkey = sig.to_vec(); + match config.scheme { + TestScheme::SolanaWrongSignature => { + sig_plus_pubkey[0] ^= 1; + } + _ => {} + } sig_plus_pubkey.extend(verifying_key.to_bytes()); + match config.scheme { + TestScheme::SolanaWrongPubkey => { + sig_plus_pubkey[64] ^= 1; + } + _ => {} + } let sig_plus_pubkey: Bytes = sig_plus_pubkey.into(); gen_witness_lock( sig_plus_pubkey, @@ -1318,6 +1331,8 @@ pub enum TestScheme { OwnerLockWithoutWitness, RsaWrongSignature, + SolanaWrongSignature, + SolanaWrongPubkey, } #[derive(Copy, Clone, PartialEq)] diff --git a/tests/omni_lock_rust/tests/test_omni_lock.rs b/tests/omni_lock_rust/tests/test_omni_lock.rs index 8f42db6..bd2ccf9 100644 --- a/tests/omni_lock_rust/tests/test_omni_lock.rs +++ b/tests/omni_lock_rust/tests/test_omni_lock.rs @@ -578,6 +578,56 @@ fn test_solana_unlock() { verify_result.expect("pass verification"); } +#[test] +fn test_solana_wrong_pubkey() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_SOLANA, false); + config.solana_secret_key = [0x01u8; 32]; + config.sig_len = 96; + config.scheme = TestScheme::SolanaWrongPubkey; + + let signing_key = SigningKey::from_bytes(&config.solana_secret_key); + let verifying_key = signing_key.verifying_key(); + let blake160 = blake160(&verifying_key.to_bytes()); + let auth = Identity { flags: IDENTITY_FLAGS_SOLANA, blake160 }; + config.id = auth; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + assert_script_error(verify_result.unwrap_err(), ERROR_MISMATCHED); +} + +#[test] +fn test_solana_wrong_signature() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_SOLANA, false); + config.solana_secret_key = [0x01u8; 32]; + config.sig_len = 96; + config.scheme = TestScheme::SolanaWrongSignature; + + let signing_key = SigningKey::from_bytes(&config.solana_secret_key); + let verifying_key = signing_key.verifying_key(); + let blake160 = blake160(&verifying_key.to_bytes()); + let auth = Identity { flags: IDENTITY_FLAGS_SOLANA, blake160 }; + config.id = auth; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + assert_script_error(verify_result.unwrap_err(), ERROR_MISMATCHED); +} + /// Steps to update this test case: /// /// 1. Install Phantom wallet from: [Phantom Wallet](https://phantom.app/)