Skip to content

Commit

Permalink
[gh-2287] add tests to session_validator.move.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Sep 13, 2024
1 parent 886af22 commit 467bdf7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::binding_test;
use bitcoin::hex::DisplayHex;
use move_core_types::account_address::AccountAddress;
use move_core_types::ident_str;
use move_core_types::language_storage::ModuleId;
Expand All @@ -10,6 +11,7 @@ use move_core_types::vm_status::{AbortLocation, KeptVMStatus, VMStatus};
use moveos_types::module_binding::MoveFunctionCaller;
use moveos_types::move_std::string::MoveString;
use moveos_types::move_types::FunctionId;
use moveos_types::state::MoveState;
use moveos_types::{module_binding::ModuleBinding, transaction::MoveAction};
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_key::keystore::memory_keystore::InMemKeystore;
Expand Down Expand Up @@ -44,6 +46,7 @@ async fn test_session_key_rooch() {
);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore.sign_transaction(&sender, tx_data, None).unwrap();
println!("{:?}", tx.clone().tx_hash());
binding_test.execute(tx).unwrap();

let session_key_module =
Expand All @@ -56,6 +59,7 @@ async fn test_session_key_rooch() {
assert_eq!(&session_key.authentication_key, session_auth_key.as_ref());
assert_eq!(session_key.scopes, vec![session_scope.clone()]);
assert_eq!(session_key.max_inactive_interval, max_inactive_interval);
println!("{:?}", session_key.clone().to_bytes().as_hex());
keystore.binding_session_key(sender, session_key).unwrap();

// send transaction via session key, it in the scop of session key, so it should success.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,20 @@ module rooch_framework::session_validator {
assert!(session_key::in_session_scope(&session_key), auth_validator::error_validate_function_call_beyond_session_scope());
auth_key
}

#[test]
fun test_validate_signature_success() {
let tx_hash = x"14563b3603703b02c89f15dbaa67f8f0e939c46c152d6700e515459d48fbec31";
let authenticator_payload = x"0004746573741b68747470733a3a2f2f746573742e726f6f63682e6e6574776f726b204ad70a371cff3b7b5c9fc08d7c33f2081d3663d7e37a6461917a106078fe5304010000000000000000000000000000000000000000000000000000000000000003012a012a000000000000000000000000000000006400000000000000";

validate_signature(&authenticator_payload, &tx_hash);
}

// #[test]
// fun test_validate_signature_fail() {
// let tx_hash = x"5415b18de0b880bb2af5dfe1ee27fd19ae8a0c99b5328e8b4b44f4c86cc7176a";
// let authenticator_payload = x"007e5b0c1da7d2bed7c2497b7c7c46b1a485883029a3bb1479493688ad347bcafa2bd82c6fd9bb2515f9e0c697f621ac0a28fb9f8c0e565d5b6d4e20bf18ce86621a18426974636f696e205369676e6564204d6573736167653a0ae2a201526f6f6368205472616e73616374696f6e3a0a57656c636f6d6520746f20726f6f63685f746573740a596f752077696c6c20617574686f72697a652073657373696f6e3a0a53636f70653a0a3078663962313065366337363066316361646365393563363634623361336561643363393835626265396436336264353161396266313736303738356432366131623a3a2a3a3a2a0a54696d654f75743a313030300a21031a446b6ac064acb14687764871dad6c08186a788248d585b3cce69231b48d1382a62633171333234356e706d3430346874667a76756c783676347736356d61717a7536617474716c336677";

// validate_signature(&authenticator_payload, &tx_hash);
// }
}
137 changes: 85 additions & 52 deletions frameworks/rooch-framework/sources/transaction_validator.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ module rooch_framework::transaction_validator {
use rooch_framework::address_mapping;
use rooch_framework::account_coin_store;
use rooch_framework::builtin_validators;
use rooch_framework::bitcoin_address;
use std::vector;
use std::string;
use std::bcs;
// use rooch_framework::bitcoin_address;
// use std::vector;
// use std::string;
// use std::bcs;

const MAX_U64: u128 = 18446744073709551615;

Expand Down Expand Up @@ -93,7 +93,7 @@ module rooch_framework::transaction_validator {

// Try the built-in auth validator first
let (bitcoin_address, session_key, auth_validator)= if (auth_validator_id == session_validator::auth_validator_id()){
let session_key = session_validator::validate(authenticator_payload); // TODO: check it with test case
let session_key = session_validator::validate(authenticator_payload);
let bitcoin_address = address_mapping::resolve_bitcoin(sender);
(bitcoin_address, option::some(session_key), option::none())
}else if (auth_validator_id == bitcoin_validator::auth_validator_id()){
Expand Down Expand Up @@ -180,51 +180,84 @@ module rooch_framework::transaction_validator {
};
}

#[test]
fun test_validate_success() {
// chain id
let chain_id = 3;
std::debug::print(&chain_id);
// auth validator id
let session_validator_id = session_validator::auth_validator_id();
std::debug::print(&session_validator_id);
// let bitcoin_validator_id = bitcoin_validator::auth_validator_id();
let authenticator_payload = vector::empty<u8>();
std::debug::print(&authenticator_payload);
rooch_framework::genesis::init_for_test();

// session key
let sender_addr = tx_context::sender();
let sender = moveos_std::account::create_signer_for_testing(sender_addr);
let scope = session_key::new_session_scope(@0x1, std::string::utf8(b"*"), std::string::utf8(b"*"));
let authentication_key = bcs::to_bytes(&sender_addr);
let max_inactive_interval = 10;
let app_name = std::string::utf8(b"test");
let app_url = std::string::utf8(b"https://test.rooch.network");
session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);

// validate function
// TODO: let scheme = vector::borrow(authenticator_payload, 0);
let tx_validate_result = validate(chain_id, session_validator_id, authenticator_payload);
std::debug::print(&tx_validate_result);

let id = 1;
let module_address = @000000000003;
let module_name = string::utf8(b"rooch_framework::empty");
let expected_auth_validator = auth_validator::new_auth_validator(id, module_address, module_name);
std::debug::print(&expected_auth_validator);
let expected_session_key = vector::empty<u8>();
let expected_bitcoin_addr = bitcoin_address::from_string(&string::utf8(b"bc1p72fvqwm9w4wcsd205maky9qejf6dwa6qeku5f5vnu4phpp3vvpws0p2f4g"));
std::debug::print(&expected_bitcoin_addr);

let auth_validator_id = 1;
let auth_validator = option::some(expected_auth_validator);
let session_key = option::some(expected_session_key);
let bitcoin_address = expected_bitcoin_addr;

let expected_tx_validate_result = auth_validator::new_tx_validate_result(auth_validator_id, auth_validator, session_key, bitcoin_address);
std::debug::print(&expected_tx_validate_result);

assert!(tx_validate_result == expected_tx_validate_result, 1000);
}
// #[test]
// fun test_validate_success() {
// // chain id
// let chain_id = 3;
// std::debug::print(&chain_id);
// // auth validator id
// let session_validator_id = session_validator::auth_validator_id();
// std::debug::print(&session_validator_id);
// // let bitcoin_validator_id = bitcoin_validator::auth_validator_id();
// let authenticator_payload = vector::empty<u8>();
// // bytes: [55, 20, 57, 24, 237, 39, 212, 99, 80, 253, 158, 152, 69, 44, 186, 5, 149, 156, 255, 112, 63, 122, 253, 78, 174, 126, 212, 106, 62, 176, 54, 107]
// vector::push_back(&mut authenticator_payload, 55);
// vector::push_back(&mut authenticator_payload, 20);
// vector::push_back(&mut authenticator_payload, 57);
// vector::push_back(&mut authenticator_payload, 24);
// vector::push_back(&mut authenticator_payload, 237);
// vector::push_back(&mut authenticator_payload, 39);
// vector::push_back(&mut authenticator_payload, 212);
// vector::push_back(&mut authenticator_payload, 99);
// vector::push_back(&mut authenticator_payload, 80);
// vector::push_back(&mut authenticator_payload, 253);
// vector::push_back(&mut authenticator_payload, 158);
// vector::push_back(&mut authenticator_payload, 152);
// vector::push_back(&mut authenticator_payload, 69);
// vector::push_back(&mut authenticator_payload, 44);
// vector::push_back(&mut authenticator_payload, 186);
// vector::push_back(&mut authenticator_payload, 5);
// vector::push_back(&mut authenticator_payload, 149);
// vector::push_back(&mut authenticator_payload, 156);
// vector::push_back(&mut authenticator_payload, 255);
// vector::push_back(&mut authenticator_payload, 112);
// vector::push_back(&mut authenticator_payload, 63);
// vector::push_back(&mut authenticator_payload, 122);
// vector::push_back(&mut authenticator_payload, 253);
// vector::push_back(&mut authenticator_payload, 78);
// vector::push_back(&mut authenticator_payload, 174);
// vector::push_back(&mut authenticator_payload, 126);
// vector::push_back(&mut authenticator_payload, 212);
// vector::push_back(&mut authenticator_payload, 106);
// vector::push_back(&mut authenticator_payload, 62);
// vector::push_back(&mut authenticator_payload, 176);
// vector::push_back(&mut authenticator_payload, 54);
// vector::push_back(&mut authenticator_payload, 107);
// std::debug::print(&authenticator_payload);
// rooch_framework::genesis::init_for_test();

// // session key
// let sender_addr = tx_context::sender();
// let sender = moveos_std::account::create_signer_for_testing(sender_addr);
// let scope = session_key::new_session_scope(@0x1, std::string::utf8(b"*"), std::string::utf8(b"*"));
// let authentication_key = bcs::to_bytes(&sender_addr);
// let max_inactive_interval = 10;
// let app_name = std::string::utf8(b"test");
// let app_url = std::string::utf8(b"https://test.rooch.network");
// session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);

// // validate function
// // TODO: let scheme = vector::borrow(authenticator_payload, 0);
// let tx_validate_result = validate(chain_id, session_validator_id, authenticator_payload);
// std::debug::print(&tx_validate_result);

// let id = 1;
// let module_address = @000000000003;
// let module_name = string::utf8(b"rooch_framework::empty");
// let expected_auth_validator = auth_validator::new_auth_validator(id, module_address, module_name);
// std::debug::print(&expected_auth_validator);
// let expected_session_key = vector::empty<u8>();
// let expected_bitcoin_addr = bitcoin_address::from_string(&string::utf8(b"bc1p72fvqwm9w4wcsd205maky9qejf6dwa6qeku5f5vnu4phpp3vvpws0p2f4g"));
// std::debug::print(&expected_bitcoin_addr);

// let auth_validator_id = 1;
// let auth_validator = option::some(expected_auth_validator);
// let session_key = option::some(expected_session_key);
// let bitcoin_address = expected_bitcoin_addr;

// let expected_tx_validate_result = auth_validator::new_tx_validate_result(auth_validator_id, auth_validator, session_key, bitcoin_address);
// std::debug::print(&expected_tx_validate_result);

// assert!(tx_validate_result == expected_tx_validate_result, 1000);
// }
}

0 comments on commit 467bdf7

Please sign in to comment.