From 45f0f35f0581c7cd0e50bb80fe8df3ac3350cc7d Mon Sep 17 00:00:00 2001 From: jolestar Date: Mon, 14 Aug 2023 15:43:09 +0800 Subject: [PATCH] [types] Migrate module bindings from rooch-framework to rooch-types (#623) * [types] Move module bindings from rooch-framework to rooch-types * fixup --- crates/rooch-executor/src/actor/executor.rs | 6 +- .../ecdsa_k1_recoverable_validator_tests.rs | 4 +- .../src/tests/ecdsa_k1_validator_tests.rs | 4 +- .../src/tests/ed25519_validator_tests.rs | 5 +- .../src/tests/empty_tests.rs | 2 +- .../src/tests/schnorr_validator_tests.rs | 5 +- .../src/tests/transaction_validator_tests.rs | 35 ++++--- .../src/bindings/auth_validator.rs | 64 ------------- crates/rooch-framework/src/bindings/mod.rs | 12 --- .../src/bindings/session_key.rs | 89 ------------------ crates/rooch-framework/src/lib.rs | 1 - .../src/framework}/address_mapping.rs | 4 +- .../src/framework/auth_validator.rs | 58 ++++++++++++ .../ecdsa_k1_recoverable_validator.rs | 2 +- .../src/framework}/ecdsa_k1_validator.rs | 2 +- .../src/framework}/ed25519_validator.rs | 2 +- .../src/framework}/empty.rs | 2 +- crates/rooch-types/src/framework/mod.rs | 12 ++- .../src/framework}/schnorr_validator.rs | 2 +- .../rooch-types/src/framework/session_key.rs | 92 +++++++++++++++++-- .../src/framework}/transaction_validator.rs | 6 +- .../src/commands/account/commands/nullify.rs | 4 +- .../src/commands/account/commands/update.rs | 6 +- 23 files changed, 202 insertions(+), 217 deletions(-) delete mode 100644 crates/rooch-framework/src/bindings/auth_validator.rs delete mode 100644 crates/rooch-framework/src/bindings/mod.rs delete mode 100644 crates/rooch-framework/src/bindings/session_key.rs rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/address_mapping.rs (96%) rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/ecdsa_k1_recoverable_validator.rs (96%) rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/ecdsa_k1_validator.rs (96%) rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/ed25519_validator.rs (96%) rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/empty.rs (96%) rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/schnorr_validator.rs (96%) rename crates/{rooch-framework/src/bindings => rooch-types/src/framework}/transaction_validator.rs (94%) diff --git a/crates/rooch-executor/src/actor/executor.rs b/crates/rooch-executor/src/actor/executor.rs index 9a1f344062..d23eb3283a 100644 --- a/crates/rooch-executor/src/actor/executor.rs +++ b/crates/rooch-executor/src/actor/executor.rs @@ -31,13 +31,13 @@ use moveos_types::transaction::FunctionCall; use moveos_types::transaction::TransactionExecutionInfo; use moveos_types::transaction::VerifiedMoveOSTransaction; use moveos_types::tx_context::TxContext; -use rooch_framework::bindings::address_mapping::AddressMapping; -use rooch_framework::bindings::auth_validator::AuthValidatorCaller; -use rooch_framework::bindings::transaction_validator::TransactionValidator; use rooch_genesis::RoochGenesis; use rooch_store::RoochStore; use rooch_types::address::MultiChainAddress; +use rooch_types::framework::address_mapping::AddressMapping; +use rooch_types::framework::auth_validator::AuthValidatorCaller; use rooch_types::framework::auth_validator::TxValidateResult; +use rooch_types::framework::transaction_validator::TransactionValidator; use rooch_types::transaction::AuthenticatorInfo; use rooch_types::transaction::{AbstractTransaction, TransactionSequenceMapping}; diff --git a/crates/rooch-framework-tests/src/tests/ecdsa_k1_recoverable_validator_tests.rs b/crates/rooch-framework-tests/src/tests/ecdsa_k1_recoverable_validator_tests.rs index f2b268ec83..f6cb9d165f 100644 --- a/crates/rooch-framework-tests/src/tests/ecdsa_k1_recoverable_validator_tests.rs +++ b/crates/rooch-framework-tests/src/tests/ecdsa_k1_recoverable_validator_tests.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 use moveos_types::transaction::MoveAction; -use rooch_framework::bindings::empty::Empty; use rooch_key::keystore::{AccountKeystore, InMemKeystore}; +use rooch_types::framework::empty::Empty; use rooch_types::{ crypto::BuiltinScheme, transaction::{rooch::RoochTransactionData, AbstractTransaction}, @@ -15,7 +15,7 @@ use crate::binding_test; fn test_validate() { let binding_test = binding_test::RustBindingTest::new().unwrap(); let ecdsa_k1_recoverable_validator = binding_test - .as_module_bundle::( + .as_module_bundle::( ); let keystore = InMemKeystore::new_ecdsa_recoverable_insecure_for_tests(1); diff --git a/crates/rooch-framework-tests/src/tests/ecdsa_k1_validator_tests.rs b/crates/rooch-framework-tests/src/tests/ecdsa_k1_validator_tests.rs index 1c32e1f31d..8849e6e553 100644 --- a/crates/rooch-framework-tests/src/tests/ecdsa_k1_validator_tests.rs +++ b/crates/rooch-framework-tests/src/tests/ecdsa_k1_validator_tests.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 use moveos_types::transaction::MoveAction; -use rooch_framework::bindings::empty::Empty; use rooch_key::keystore::{AccountKeystore, InMemKeystore}; +use rooch_types::framework::empty::Empty; use rooch_types::{ crypto::BuiltinScheme, transaction::{rooch::RoochTransactionData, AbstractTransaction}, @@ -15,7 +15,7 @@ use crate::binding_test; fn test_validate() { let binding_test = binding_test::RustBindingTest::new().unwrap(); let ecdsa_k1_validator = binding_test - .as_module_bundle::( + .as_module_bundle::( ); let keystore = InMemKeystore::new_ecdsa_insecure_for_tests(1); diff --git a/crates/rooch-framework-tests/src/tests/ed25519_validator_tests.rs b/crates/rooch-framework-tests/src/tests/ed25519_validator_tests.rs index b46602d29d..e7407a1254 100644 --- a/crates/rooch-framework-tests/src/tests/ed25519_validator_tests.rs +++ b/crates/rooch-framework-tests/src/tests/ed25519_validator_tests.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 use moveos_types::transaction::MoveAction; -use rooch_framework::bindings::empty::Empty; use rooch_key::keystore::{AccountKeystore, InMemKeystore}; +use rooch_types::framework::empty::Empty; use rooch_types::{ crypto::BuiltinScheme, transaction::{rooch::RoochTransactionData, AbstractTransaction}, @@ -15,8 +15,7 @@ use crate::binding_test; fn test_validate() { let binding_test = binding_test::RustBindingTest::new().unwrap(); let ed25519_validator = binding_test - .as_module_bundle::( - ); + .as_module_bundle::(); let keystore = InMemKeystore::new_ed25519_insecure_for_tests(1); let sender = keystore.addresses()[0]; diff --git a/crates/rooch-framework-tests/src/tests/empty_tests.rs b/crates/rooch-framework-tests/src/tests/empty_tests.rs index 7fd48a553d..71277766b4 100644 --- a/crates/rooch-framework-tests/src/tests/empty_tests.rs +++ b/crates/rooch-framework-tests/src/tests/empty_tests.rs @@ -7,7 +7,7 @@ use moveos_types::tx_context::TxContext; #[test] fn test_empty() { let binding_test = binding_test::RustBindingTest::new().unwrap(); - let empty = binding_test.as_module_bundle::(); + let empty = binding_test.as_module_bundle::(); let ctx = TxContext::random_for_testing_only(); empty.empty(&ctx).unwrap(); } diff --git a/crates/rooch-framework-tests/src/tests/schnorr_validator_tests.rs b/crates/rooch-framework-tests/src/tests/schnorr_validator_tests.rs index cd9dbf28fe..970d5f7293 100644 --- a/crates/rooch-framework-tests/src/tests/schnorr_validator_tests.rs +++ b/crates/rooch-framework-tests/src/tests/schnorr_validator_tests.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 use moveos_types::transaction::MoveAction; -use rooch_framework::bindings::empty::Empty; use rooch_key::keystore::{AccountKeystore, InMemKeystore}; +use rooch_types::framework::empty::Empty; use rooch_types::{ crypto::BuiltinScheme, transaction::{rooch::RoochTransactionData, AbstractTransaction}, @@ -15,8 +15,7 @@ use crate::binding_test; fn test_validate() { let binding_test = binding_test::RustBindingTest::new().unwrap(); let schnorr_validator = binding_test - .as_module_bundle::( - ); + .as_module_bundle::(); let keystore = InMemKeystore::new_schnorr_insecure_for_tests(1); let sender = keystore.addresses()[0]; diff --git a/crates/rooch-framework-tests/src/tests/transaction_validator_tests.rs b/crates/rooch-framework-tests/src/tests/transaction_validator_tests.rs index 44a79392fb..5ae49f60c9 100644 --- a/crates/rooch-framework-tests/src/tests/transaction_validator_tests.rs +++ b/crates/rooch-framework-tests/src/tests/transaction_validator_tests.rs @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 use moveos_types::{module_binding::ModuleBundle, transaction::MoveAction}; -use rooch_framework::{bindings::empty::Empty, ROOCH_FRAMEWORK_ADDRESS}; use rooch_key::keystore::{AccountKeystore, InMemKeystore}; +use rooch_types::{addresses::ROOCH_FRAMEWORK_ADDRESS, framework::empty::Empty}; use rooch_types::{ crypto::BuiltinScheme, framework::session_key::SessionScope, @@ -15,7 +15,9 @@ use crate::binding_test; #[test] fn test_validate_ed25519() { let binding_test = binding_test::RustBindingTest::new().unwrap(); - let transaction_validator = binding_test.as_module_bundle::(); + let transaction_validator = binding_test + .as_module_bundle::( + ); let keystore = InMemKeystore::new_ed25519_insecure_for_tests(1); let sender = keystore.addresses()[0]; @@ -36,7 +38,9 @@ fn test_validate_ed25519() { #[test] fn test_validate_ecdsa() { let binding_test = binding_test::RustBindingTest::new().unwrap(); - let transaction_validator = binding_test.as_module_bundle::(); + let transaction_validator = binding_test + .as_module_bundle::( + ); let keystore = InMemKeystore::new_ecdsa_insecure_for_tests(1); let sender = keystore.addresses()[0]; @@ -57,7 +61,9 @@ fn test_validate_ecdsa() { #[test] fn test_validate_ecdsa_recoverable() { let binding_test = binding_test::RustBindingTest::new().unwrap(); - let transaction_validator = binding_test.as_module_bundle::(); + let transaction_validator = binding_test + .as_module_bundle::( + ); let keystore = InMemKeystore::new_ecdsa_recoverable_insecure_for_tests(1); let sender = keystore.addresses()[0]; @@ -78,7 +84,9 @@ fn test_validate_ecdsa_recoverable() { #[test] fn test_validate_schnorr() { let binding_test = binding_test::RustBindingTest::new().unwrap(); - let transaction_validator = binding_test.as_module_bundle::(); + let transaction_validator = binding_test + .as_module_bundle::( + ); let keystore = InMemKeystore::new_schnorr_insecure_for_tests(1); let sender = keystore.addresses()[0]; @@ -113,14 +121,13 @@ fn test_session_key_ed25519() { ); let expiration_time = 100; let max_inactive_interval = 100; - let action = - rooch_framework::bindings::session_key::SessionKeyModule::create_session_key_action( - auth_key.clone(), - BuiltinScheme::Ed25519, - session_scope.clone(), - expiration_time, - max_inactive_interval, - ); + let action = rooch_types::framework::session_key::SessionKeyModule::create_session_key_action( + auth_key.clone(), + BuiltinScheme::Ed25519, + session_scope.clone(), + expiration_time, + max_inactive_interval, + ); let tx_data = RoochTransactionData::new(sender, sequence_number, action); let tx = keystore .sign_transaction(&sender, tx_data, BuiltinScheme::Ed25519) @@ -128,7 +135,7 @@ fn test_session_key_ed25519() { binding_test.execute(tx).unwrap(); let session_key_module = - binding_test.as_module_bundle::(); + binding_test.as_module_bundle::(); let session_key_option = session_key_module .get_session_key(sender.into(), auth_key) .unwrap(); diff --git a/crates/rooch-framework/src/bindings/auth_validator.rs b/crates/rooch-framework/src/bindings/auth_validator.rs deleted file mode 100644 index f9df014bee..0000000000 --- a/crates/rooch-framework/src/bindings/auth_validator.rs +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) RoochNetwork -// SPDX-License-Identifier: Apache-2.0 - -use anyhow::Result; -use move_core_types::value::MoveValue; -use moveos_types::{ - module_binding::MoveFunctionCaller, move_types::FunctionId, transaction::FunctionCall, - tx_context::TxContext, -}; -use rooch_types::framework::auth_validator::AuthValidator; - -use super::transaction_validator::TransactionValidator; - -/// Rust bindings for developer custom auth validator module -/// Because the module is not in RoochFramework, we need to dynamically determine the module id base on the AuthValidator struct -pub struct AuthValidatorCaller<'a> { - caller: &'a dyn MoveFunctionCaller, - auth_validator: AuthValidator, -} - -impl<'a> AuthValidatorCaller<'a> { - pub fn new(caller: &'a dyn MoveFunctionCaller, auth_validator: AuthValidator) -> Self { - Self { - caller, - auth_validator, - } - } - - pub fn validate(&self, ctx: &TxContext, payload: Vec) -> Result<()> { - let auth_validator_call = FunctionCall::new( - self.auth_validator.validator_function_id(), - vec![], - vec![MoveValue::vector_u8(payload).simple_serialize().unwrap()], - ); - self.caller - .call_function(ctx, auth_validator_call) - .map(|values| { - debug_assert!(values.is_empty(), "should not have return values"); - })?; - Ok(()) - } - - pub fn pre_execute_function_id(&self) -> FunctionId { - FunctionId::new( - self.auth_validator.validator_module_id(), - TransactionValidator::PRE_EXECUTE_FUNCTION_NAME.to_owned(), - ) - } - - pub fn pre_execute_function_call(&self) -> FunctionCall { - FunctionCall::new(self.pre_execute_function_id(), vec![], vec![]) - } - - pub fn post_execute_function_id(&self) -> FunctionId { - FunctionId::new( - self.auth_validator.validator_module_id(), - TransactionValidator::POST_EXECUTE_FUNCTION_NAME.to_owned(), - ) - } - - pub fn post_execute_function_call(&self) -> FunctionCall { - FunctionCall::new(self.post_execute_function_id(), vec![], vec![]) - } -} diff --git a/crates/rooch-framework/src/bindings/mod.rs b/crates/rooch-framework/src/bindings/mod.rs deleted file mode 100644 index 50291da20a..0000000000 --- a/crates/rooch-framework/src/bindings/mod.rs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) RoochNetwork -// SPDX-License-Identifier: Apache-2.0 - -pub mod address_mapping; -pub mod auth_validator; -pub mod ecdsa_k1_recoverable_validator; -pub mod ecdsa_k1_validator; -pub mod ed25519_validator; -pub mod empty; -pub mod schnorr_validator; -pub mod session_key; -pub mod transaction_validator; diff --git a/crates/rooch-framework/src/bindings/session_key.rs b/crates/rooch-framework/src/bindings/session_key.rs deleted file mode 100644 index 6419b05148..0000000000 --- a/crates/rooch-framework/src/bindings/session_key.rs +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) RoochNetwork -// SPDX-License-Identifier: Apache-2.0 - -use crate::ROOCH_FRAMEWORK_ADDRESS; -use anyhow::Result; -use move_core_types::{ - account_address::AccountAddress, ident_str, identifier::IdentStr, value::MoveValue, -}; -use moveos_types::{ - module_binding::{ModuleBundle, MoveFunctionCaller}, - move_option::MoveOption, - state::MoveState, - transaction::{FunctionCall, MoveAction}, - tx_context::TxContext, -}; -use rooch_types::{ - crypto::BuiltinScheme, - framework::session_key::{SessionKey, SessionScope}, -}; - -/// Rust bindings for RoochFramework session_key module -pub struct SessionKeyModule<'a> { - caller: &'a dyn MoveFunctionCaller, -} - -impl<'a> SessionKeyModule<'a> { - pub const GET_SESSION_KEY_FUNCTION_NAME: &'static IdentStr = ident_str!("get_session_key"); - pub const CREATE_SESSION_KEY_ENTRY_FUNCTION_NAME: &'static IdentStr = - ident_str!("create_session_key_entry"); - - pub fn get_session_key( - &self, - account_address: AccountAddress, - auth_key: Vec, - ) -> Result> { - let call = FunctionCall::new( - Self::function_id(Self::GET_SESSION_KEY_FUNCTION_NAME), - vec![], - vec![ - MoveValue::Address(account_address) - .simple_serialize() - .unwrap(), - MoveValue::vector_u8(auth_key).simple_serialize().unwrap(), - ], - ); - let ctx = TxContext::new_readonly_ctx(account_address); - let session_key = self.caller.call_function(&ctx, call).map(|mut values| { - let value = values.pop().expect("should have one return value"); - bcs::from_bytes::>(&value.value) - .expect("should be a valid MoveOption") - .into() - })?; - Ok(session_key) - } - - pub fn create_session_key_action( - authentication_key: Vec, - scheme: BuiltinScheme, - scope: SessionScope, - expiration_time: u64, - max_inactive_interval: u64, - ) -> MoveAction { - Self::create_move_action( - Self::CREATE_SESSION_KEY_ENTRY_FUNCTION_NAME, - vec![], - vec![ - MoveValue::vector_u8(authentication_key), - MoveValue::U64(scheme.flag() as u64), - scope.module_address.to_move_value(), - scope.module_name.to_move_value(), - scope.function_name.to_move_value(), - MoveValue::U64(expiration_time), - MoveValue::U64(max_inactive_interval), - ], - ) - } -} - -impl<'a> ModuleBundle<'a> for SessionKeyModule<'a> { - const MODULE_NAME: &'static IdentStr = ident_str!("session_key"); - const MODULE_ADDRESS: AccountAddress = ROOCH_FRAMEWORK_ADDRESS; - - fn new(caller: &'a impl MoveFunctionCaller) -> Self - where - Self: Sized, - { - Self { caller } - } -} diff --git a/crates/rooch-framework/src/lib.rs b/crates/rooch-framework/src/lib.rs index 35d42ba6e4..8a5a585c2c 100644 --- a/crates/rooch-framework/src/lib.rs +++ b/crates/rooch-framework/src/lib.rs @@ -1,7 +1,6 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -pub mod bindings; pub mod natives; pub use rooch_types::addresses::*; diff --git a/crates/rooch-framework/src/bindings/address_mapping.rs b/crates/rooch-types/src/framework/address_mapping.rs similarity index 96% rename from crates/rooch-framework/src/bindings/address_mapping.rs rename to crates/rooch-types/src/framework/address_mapping.rs index fdbaf2fa6a..2e7b8746fa 100644 --- a/crates/rooch-framework/src/bindings/address_mapping.rs +++ b/crates/rooch-types/src/framework/address_mapping.rs @@ -1,7 +1,8 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::address::{MultiChainAddress, RoochAddress}; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; use anyhow::Result; use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr}; use moveos_types::{ @@ -11,7 +12,6 @@ use moveos_types::{ transaction::FunctionCall, tx_context::TxContext, }; -use rooch_types::address::{MultiChainAddress, RoochAddress}; /// Rust bindings for RoochFramework address_mapping module pub struct AddressMapping<'a> { diff --git a/crates/rooch-types/src/framework/auth_validator.rs b/crates/rooch-types/src/framework/auth_validator.rs index f92b231b11..fb9420f9fc 100644 --- a/crates/rooch-types/src/framework/auth_validator.rs +++ b/crates/rooch-types/src/framework/auth_validator.rs @@ -1,15 +1,21 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 +use super::transaction_validator::TransactionValidator; use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; +use anyhow::Result; +use move_core_types::value::MoveValue; use move_core_types::{ account_address::AccountAddress, ident_str, identifier::IdentStr, language_storage::ModuleId, }; use moveos_types::move_option::MoveOption; use moveos_types::{ + module_binding::MoveFunctionCaller, move_string::MoveAsciiString, move_types::FunctionId, state::{MoveStructState, MoveStructType}, + transaction::FunctionCall, + tx_context::TxContext, }; use serde::{Deserialize, Serialize}; @@ -99,3 +105,55 @@ impl TxValidateResult { self.session_key().is_some() } } + +/// Rust bindings for developer custom auth validator module +/// Because the module is not in RoochFramework, we need to dynamically determine the module id base on the AuthValidator struct +pub struct AuthValidatorCaller<'a> { + caller: &'a dyn MoveFunctionCaller, + auth_validator: AuthValidator, +} + +impl<'a> AuthValidatorCaller<'a> { + pub fn new(caller: &'a dyn MoveFunctionCaller, auth_validator: AuthValidator) -> Self { + Self { + caller, + auth_validator, + } + } + + pub fn validate(&self, ctx: &TxContext, payload: Vec) -> Result<()> { + let auth_validator_call = FunctionCall::new( + self.auth_validator.validator_function_id(), + vec![], + vec![MoveValue::vector_u8(payload).simple_serialize().unwrap()], + ); + self.caller + .call_function(ctx, auth_validator_call) + .map(|values| { + debug_assert!(values.is_empty(), "should not have return values"); + })?; + Ok(()) + } + + pub fn pre_execute_function_id(&self) -> FunctionId { + FunctionId::new( + self.auth_validator.validator_module_id(), + TransactionValidator::PRE_EXECUTE_FUNCTION_NAME.to_owned(), + ) + } + + pub fn pre_execute_function_call(&self) -> FunctionCall { + FunctionCall::new(self.pre_execute_function_id(), vec![], vec![]) + } + + pub fn post_execute_function_id(&self) -> FunctionId { + FunctionId::new( + self.auth_validator.validator_module_id(), + TransactionValidator::POST_EXECUTE_FUNCTION_NAME.to_owned(), + ) + } + + pub fn post_execute_function_call(&self) -> FunctionCall { + FunctionCall::new(self.post_execute_function_id(), vec![], vec![]) + } +} diff --git a/crates/rooch-framework/src/bindings/ecdsa_k1_recoverable_validator.rs b/crates/rooch-types/src/framework/ecdsa_k1_recoverable_validator.rs similarity index 96% rename from crates/rooch-framework/src/bindings/ecdsa_k1_recoverable_validator.rs rename to crates/rooch-types/src/framework/ecdsa_k1_recoverable_validator.rs index 4936fa2e7b..406595d7d2 100644 --- a/crates/rooch-framework/src/bindings/ecdsa_k1_recoverable_validator.rs +++ b/crates/rooch-types/src/framework/ecdsa_k1_recoverable_validator.rs @@ -1,7 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; use anyhow::Result; use move_core_types::{ account_address::AccountAddress, ident_str, identifier::IdentStr, value::MoveValue, diff --git a/crates/rooch-framework/src/bindings/ecdsa_k1_validator.rs b/crates/rooch-types/src/framework/ecdsa_k1_validator.rs similarity index 96% rename from crates/rooch-framework/src/bindings/ecdsa_k1_validator.rs rename to crates/rooch-types/src/framework/ecdsa_k1_validator.rs index 6143e77dcb..028b638426 100644 --- a/crates/rooch-framework/src/bindings/ecdsa_k1_validator.rs +++ b/crates/rooch-types/src/framework/ecdsa_k1_validator.rs @@ -1,7 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; use anyhow::Result; use move_core_types::{ account_address::AccountAddress, ident_str, identifier::IdentStr, value::MoveValue, diff --git a/crates/rooch-framework/src/bindings/ed25519_validator.rs b/crates/rooch-types/src/framework/ed25519_validator.rs similarity index 96% rename from crates/rooch-framework/src/bindings/ed25519_validator.rs rename to crates/rooch-types/src/framework/ed25519_validator.rs index a4f0e3a4e3..928926db48 100644 --- a/crates/rooch-framework/src/bindings/ed25519_validator.rs +++ b/crates/rooch-types/src/framework/ed25519_validator.rs @@ -1,7 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; use anyhow::Result; use move_core_types::{ account_address::AccountAddress, ident_str, identifier::IdentStr, value::MoveValue, diff --git a/crates/rooch-framework/src/bindings/empty.rs b/crates/rooch-types/src/framework/empty.rs similarity index 96% rename from crates/rooch-framework/src/bindings/empty.rs rename to crates/rooch-types/src/framework/empty.rs index 3697bf5746..9d11485609 100644 --- a/crates/rooch-framework/src/bindings/empty.rs +++ b/crates/rooch-types/src/framework/empty.rs @@ -1,7 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; use anyhow::Result; use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr}; use moveos_types::{ diff --git a/crates/rooch-types/src/framework/mod.rs b/crates/rooch-types/src/framework/mod.rs index c6e9163877..2009cc3b95 100644 --- a/crates/rooch-types/src/framework/mod.rs +++ b/crates/rooch-types/src/framework/mod.rs @@ -1,7 +1,15 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -/// types mapping from Framework Move types to Rust types -//TODO should be in the framework crate? +/// Types mapping from Framework Move types to Rust types +/// Module binding for the Framework +//TODO there modules maybe generated by ABI in the future +pub mod address_mapping; pub mod auth_validator; +pub mod ecdsa_k1_recoverable_validator; +pub mod ecdsa_k1_validator; +pub mod ed25519_validator; +pub mod empty; +pub mod schnorr_validator; pub mod session_key; +pub mod transaction_validator; diff --git a/crates/rooch-framework/src/bindings/schnorr_validator.rs b/crates/rooch-types/src/framework/schnorr_validator.rs similarity index 96% rename from crates/rooch-framework/src/bindings/schnorr_validator.rs rename to crates/rooch-types/src/framework/schnorr_validator.rs index 46d398850b..4a2f5af35a 100644 --- a/crates/rooch-framework/src/bindings/schnorr_validator.rs +++ b/crates/rooch-types/src/framework/schnorr_validator.rs @@ -1,7 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; use anyhow::Result; use move_core_types::{ account_address::AccountAddress, ident_str, identifier::IdentStr, value::MoveValue, diff --git a/crates/rooch-types/src/framework/session_key.rs b/crates/rooch-types/src/framework/session_key.rs index cf4e7b51e7..e102a5959d 100644 --- a/crates/rooch-types/src/framework/session_key.rs +++ b/crates/rooch-types/src/framework/session_key.rs @@ -1,15 +1,23 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use std::str::FromStr; - -use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; +use crate::{addresses::ROOCH_FRAMEWORK_ADDRESS, crypto::BuiltinScheme}; +use anyhow::Result; +use move_core_types::value::MoveValue; use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr}; +use moveos_types::{ + module_binding::{ModuleBundle, MoveFunctionCaller}, + move_option::MoveOption, + state::MoveState, + transaction::{FunctionCall, MoveAction}, + tx_context::TxContext, +}; use moveos_types::{ move_string::MoveAsciiString, state::{MoveStructState, MoveStructType}, }; use serde::{Deserialize, Serialize}; +use std::str::FromStr; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)] pub struct SessionScope { @@ -38,8 +46,8 @@ impl MoveStructState for SessionScope { fn struct_layout() -> move_core_types::value::MoveStructLayout { move_core_types::value::MoveStructLayout::new(vec![ move_core_types::value::MoveTypeLayout::Address, - MoveAsciiString::type_layout(), - MoveAsciiString::type_layout(), + ::type_layout(), + ::type_layout(), ]) } } @@ -67,10 +75,82 @@ impl MoveStructState for SessionKey { move_core_types::value::MoveTypeLayout::U8, )), move_core_types::value::MoveTypeLayout::U64, - move_core_types::value::MoveTypeLayout::Vector(Box::new(SessionScope::type_layout())), + move_core_types::value::MoveTypeLayout::Vector(Box::new( + ::type_layout(), + )), move_core_types::value::MoveTypeLayout::U64, move_core_types::value::MoveTypeLayout::U64, move_core_types::value::MoveTypeLayout::U64, ]) } } + +/// Rust bindings for RoochFramework session_key module +pub struct SessionKeyModule<'a> { + caller: &'a dyn MoveFunctionCaller, +} + +impl<'a> SessionKeyModule<'a> { + pub const GET_SESSION_KEY_FUNCTION_NAME: &'static IdentStr = ident_str!("get_session_key"); + pub const CREATE_SESSION_KEY_ENTRY_FUNCTION_NAME: &'static IdentStr = + ident_str!("create_session_key_entry"); + + pub fn get_session_key( + &self, + account_address: AccountAddress, + auth_key: Vec, + ) -> Result> { + let call = FunctionCall::new( + Self::function_id(Self::GET_SESSION_KEY_FUNCTION_NAME), + vec![], + vec![ + MoveValue::Address(account_address) + .simple_serialize() + .unwrap(), + MoveValue::vector_u8(auth_key).simple_serialize().unwrap(), + ], + ); + let ctx = TxContext::new_readonly_ctx(account_address); + let session_key = self.caller.call_function(&ctx, call).map(|mut values| { + let value = values.pop().expect("should have one return value"); + bcs::from_bytes::>(&value.value) + .expect("should be a valid MoveOption") + .into() + })?; + Ok(session_key) + } + + pub fn create_session_key_action( + authentication_key: Vec, + scheme: BuiltinScheme, + scope: SessionScope, + expiration_time: u64, + max_inactive_interval: u64, + ) -> MoveAction { + Self::create_move_action( + Self::CREATE_SESSION_KEY_ENTRY_FUNCTION_NAME, + vec![], + vec![ + MoveValue::vector_u8(authentication_key), + MoveValue::U64(scheme.flag() as u64), + scope.module_address.to_move_value(), + scope.module_name.to_move_value(), + scope.function_name.to_move_value(), + MoveValue::U64(expiration_time), + MoveValue::U64(max_inactive_interval), + ], + ) + } +} + +impl<'a> ModuleBundle<'a> for SessionKeyModule<'a> { + const MODULE_NAME: &'static IdentStr = ident_str!("session_key"); + const MODULE_ADDRESS: AccountAddress = ROOCH_FRAMEWORK_ADDRESS; + + fn new(caller: &'a impl MoveFunctionCaller) -> Self + where + Self: Sized, + { + Self { caller } + } +} diff --git a/crates/rooch-framework/src/bindings/transaction_validator.rs b/crates/rooch-types/src/framework/transaction_validator.rs similarity index 94% rename from crates/rooch-framework/src/bindings/transaction_validator.rs rename to crates/rooch-types/src/framework/transaction_validator.rs index e10e306f8f..4dbd1f466d 100644 --- a/crates/rooch-framework/src/bindings/transaction_validator.rs +++ b/crates/rooch-types/src/framework/transaction_validator.rs @@ -1,7 +1,9 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::ROOCH_FRAMEWORK_ADDRESS; +use crate::addresses::ROOCH_FRAMEWORK_ADDRESS; +use crate::framework::auth_validator::TxValidateResult; +use crate::transaction::AuthenticatorInfo; use anyhow::Result; use move_core_types::{ account_address::AccountAddress, ident_str, identifier::IdentStr, value::MoveValue, @@ -12,8 +14,6 @@ use moveos_types::{ transaction::FunctionCall, tx_context::TxContext, }; -use rooch_types::framework::auth_validator::TxValidateResult; -use rooch_types::transaction::AuthenticatorInfo; /// Rust bindings for RoochFramework transaction_validator module pub struct TransactionValidator<'a> { diff --git a/crates/rooch/src/commands/account/commands/nullify.rs b/crates/rooch/src/commands/account/commands/nullify.rs index 9a4006c28f..cc7b231bca 100644 --- a/crates/rooch/src/commands/account/commands/nullify.rs +++ b/crates/rooch/src/commands/account/commands/nullify.rs @@ -8,12 +8,12 @@ use move_core_types::{ language_storage::{ModuleId, StructTag, TypeTag}, }; use moveos_types::{module_binding::ModuleBundle, move_types::FunctionId, transaction::MoveAction}; -use rooch_framework::bindings::{ +use rooch_rpc_api::jsonrpc_types::ExecuteTransactionResponseView; +use rooch_types::framework::{ ecdsa_k1_recoverable_validator::EcdsaK1RecoverableValidator, ecdsa_k1_validator::EcdsaK1Validator, ed25519_validator::Ed25519Validator, schnorr_validator::SchnorrValidator, }; -use rooch_rpc_api::jsonrpc_types::ExecuteTransactionResponseView; use std::fmt::Debug; use async_trait::async_trait; diff --git a/crates/rooch/src/commands/account/commands/update.rs b/crates/rooch/src/commands/account/commands/update.rs index 5db106c02e..8c260f6ef8 100644 --- a/crates/rooch/src/commands/account/commands/update.rs +++ b/crates/rooch/src/commands/account/commands/update.rs @@ -10,13 +10,13 @@ use move_core_types::{ }; use moveos_types::module_binding::ModuleBundle; use moveos_types::{move_types::FunctionId, transaction::MoveAction}; -use rooch_framework::bindings::{ +use rooch_key::keystore::AccountKeystore; +use rooch_rpc_api::jsonrpc_types::ExecuteTransactionResponseView; +use rooch_types::framework::{ ecdsa_k1_recoverable_validator::EcdsaK1RecoverableValidator, ecdsa_k1_validator::EcdsaK1Validator, ed25519_validator::Ed25519Validator, schnorr_validator::SchnorrValidator, }; -use rooch_key::keystore::AccountKeystore; -use rooch_rpc_api::jsonrpc_types::ExecuteTransactionResponseView; use rooch_types::{ address::RoochAddress, crypto::{BuiltinScheme, PublicKey},