Skip to content

Commit

Permalink
[gh-1625] fix creating session keys from session keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss authored and feliciss committed May 7, 2024
1 parent 85b9502 commit fc36f9e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn test_session_key_rooch() {
)
.unwrap();
let app_name = MoveString::from_str("test").unwrap();
let app_url = MoveAsciiString::from_str("https:://test.rooch.network").unwrap();
let app_url = MoveAsciiString::from_str("https://test-seed.rooch.network").unwrap();
let max_inactive_interval = 100;
let action = rooch_types::framework::session_key::SessionKeyModule::create_session_key_action(
app_name,
Expand Down
6 changes: 5 additions & 1 deletion crates/rooch-types/src/framework/auth_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub struct TxValidateResult {

impl MoveStructType for TxValidateResult {
const ADDRESS: AccountAddress = ROOCH_FRAMEWORK_ADDRESS;
const MODULE_NAME: &'static IdentStr = ident_str!("transaction_validtor");
const MODULE_NAME: &'static IdentStr = ident_str!("auth_validator");
const STRUCT_NAME: &'static IdentStr = ident_str!("TxValidateResult");
}

Expand All @@ -177,6 +177,10 @@ impl MoveStructState for TxValidateResult {
}

impl TxValidateResult {
pub fn auth_validator_id(&self) -> u64 {
self.auth_validator_id.clone()
}

pub fn auth_validator(&self) -> Option<AuthValidator> {
self.auth_validator.clone().into()
}
Expand Down
7 changes: 6 additions & 1 deletion crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ Feature: Rooch CLI integration tests
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# session key
Then cmd: "session-key create --app-name test --app-url https:://test.rooch.network --scope 0x3::empty::empty"
Then cmd: "session-key create --app-name test --app-url https://test-seed.rooch.network --scope 0x3::empty::empty"
Then cmd: "move run --function 0x3::empty::empty --session-key {{$.session-key[-1].authentication_key}}"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# session key from session key
Then cmd: "session-key create --app-name test --app-url https://test-seed.rooch.network --session-key {{$.session-key[-1].authentication_key}} --scope 0x3::empty::empty"
Then cmd: "move run --function 0x3:cargo test -p testsuite --test integration:empty::empty --session-key {{$.session-key[-1].authentication_key}}"
Then assert: "{{$.move[-1].execution_info.status.type}} == executed"

# transaction
Then cmd: "transaction get-transactions-by-order --cursor 0 --limit 1 --descending-order false"
Then cmd: "transaction get-transactions-by-hash --hashes {{$.transaction[-1].data[0].execution_info.tx_hash}}"
Expand Down
2 changes: 1 addition & 1 deletion frameworks/rooch-framework/doc/auth_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Get the auth validator's id from the TxValidateResult in the TxContext
## Function `get_session_key_from_ctx_option`

Get the session key from the TxValidateResult in the TxContext
If the TxValidateResult is None or SessionKey is None, return None
If the TxValidateResult, AuthValidator or SessionKey is None, return None


<pre><code><b>public</b> <b>fun</b> <a href="auth_validator.md#0x3_auth_validator_get_session_key_from_ctx_option">get_session_key_from_ctx_option</a>(): <a href="_Option">option::Option</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ module rooch_framework::auth_validator {
}

/// Get the session key from the TxValidateResult in the TxContext
/// If the TxValidateResult is None or SessionKey is None, return None
/// If the TxValidateResult, AuthValidator or SessionKey is None, return None
public fun get_session_key_from_ctx_option(): Option<vector<u8>> {
let validate_result_opt = tx_context::get_attribute<TxValidateResult>();
if (option::is_some(&validate_result_opt)) {
let validate_result = option::extract(&mut validate_result_opt);
validate_result.session_key
validate_result.session_key
}else {
option::none<vector<u8>>()
}
Expand Down
33 changes: 29 additions & 4 deletions frameworks/rooch-framework/sources/tests/session_key_test.move
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module rooch_framework::session_key_test{
use rooch_framework::session_key;
use rooch_framework::timestamp;

/// Create session key in this context is not allowed
const ErrorSessionKeyCreatePermissionDenied: u64 = 1;

#[test]
fun test_session_key_end_to_end(){
rooch_framework::genesis::init_for_test();
Expand All @@ -21,8 +24,8 @@ module rooch_framework::session_key_test{
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::ascii::string(b"https://test.rooch.network");
session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);
let app_url = std::ascii::string(b"https://test-seed.rooch.network");
session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);
let session_key_opt = session_key::get_session_key(sender_addr, authentication_key);
assert!(option::is_some(&session_key_opt), 1000);

Expand All @@ -35,9 +38,31 @@ module rooch_framework::session_key_test{
timestamp::fast_forward_seconds_for_test(2);
assert!(session_key::is_expired_session_key(sender_addr, authentication_key), 1004);
session_key::remove_session_key(&sender, authentication_key);
}

#[test]
#[expected_failure(abort_code = ErrorSessionKeyCreatePermissionDenied, location = Self)]
fun test_session_key_from_session_key(){
rooch_framework::genesis::init_for_test();
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::ascii::string(b"*"), std::ascii::string(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::ascii::string(b"https://test-seed.rooch.network");
session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);
let session_key_opt = session_key::get_session_key(sender_addr, authentication_key);
assert!(option::is_some(&session_key_opt), 1000);

let session_key = bcs::to_bytes(&option::extract(&mut session_key_opt));
std::debug::print(&session_key);
session_key::create_session_key(&sender, app_name, app_url, session_key, vector::singleton(scope), max_inactive_interval);
let other_session_key_opt = session_key::get_session_key(sender_addr, session_key);
assert!(option::is_some(&other_session_key_opt), 1000);



let other_session_key = bcs::to_bytes(&option::extract(&mut other_session_key_opt));
std::debug::print(&other_session_key);
}

}

0 comments on commit fc36f9e

Please sign in to comment.