Skip to content

Commit

Permalink
Add solana test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Apr 2, 2024
1 parent b87d9f7 commit 5c25d6a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/omni_lock_rust/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1318,6 +1331,8 @@ pub enum TestScheme {
OwnerLockWithoutWitness,

RsaWrongSignature,
SolanaWrongSignature,
SolanaWrongPubkey,
}

#[derive(Copy, Clone, PartialEq)]
Expand Down
50 changes: 50 additions & 0 deletions tests/omni_lock_rust/tests/test_omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down

0 comments on commit 5c25d6a

Please sign in to comment.