diff --git a/crates/context/config/src/client/env/config/mutate.rs b/crates/context/config/src/client/env/config/mutate.rs index ecbc5454e..94bebe706 100644 --- a/crates/context/config/src/client/env/config/mutate.rs +++ b/crates/context/config/src/client/env/config/mutate.rs @@ -76,10 +76,14 @@ impl<'a> Method for Mutate<'a> { let user_key_bytes = user_key.to_bytes(); // Create Repr wrapped ContextIdentity instances - let signer_id = verifying_key_bytes.rt::().expect("Infallible conversion"); + let signer_id = verifying_key_bytes + .rt::() + .expect("Infallible conversion"); let signer_id = Repr::new(signer_id); - let user_id = user_key_bytes.rt::().expect("Infallible conversion"); + let user_id = user_key_bytes + .rt::() + .expect("Infallible conversion"); let user_id = Repr::new(user_id); // Create the Request structure using into() conversions diff --git a/crates/context/config/src/client/env/config/query/application.rs b/crates/context/config/src/client/env/config/query/application.rs index f167fce76..8400b9fd0 100644 --- a/crates/context/config/src/client/env/config/query/application.rs +++ b/crates/context/config/src/client/env/config/query/application.rs @@ -1,4 +1,5 @@ use serde::Serialize; +use starknet::core::codec::Decode; use starknet_crypto::Felt; use crate::client::env::config::types::starknet::Application as StarknetApplication; @@ -7,7 +8,6 @@ use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; use crate::repr::{Repr, ReprBytes}; use crate::types::{Application, ApplicationMetadata, ApplicationSource, ContextId}; -use starknet::core::codec::Decode; #[derive(Copy, Clone, Debug, Serialize)] pub(super) struct ApplicationRequest { diff --git a/crates/context/config/src/client/env/config/query/members.rs b/crates/context/config/src/client/env/config/query/members.rs index 672211d26..1d1996c05 100644 --- a/crates/context/config/src/client/env/config/query/members.rs +++ b/crates/context/config/src/client/env/config/query/members.rs @@ -1,14 +1,13 @@ use core::mem; use serde::Serialize; -use starknet::core::codec::Encode; +use starknet::core::codec::{Decode, Encode}; use starknet_crypto::Felt; use crate::client::env::config::types::starknet::{StarknetMembers, StarknetMembersRequest}; use crate::client::env::Method; use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; -use starknet::core::codec::Decode; use crate::repr::Repr; use crate::types::{ContextId, ContextIdentity}; diff --git a/crates/context/config/src/client/env/config/query/privileges.rs b/crates/context/config/src/client/env/config/query/privileges.rs index 6e916ef6d..63222cf69 100644 --- a/crates/context/config/src/client/env/config/query/privileges.rs +++ b/crates/context/config/src/client/env/config/query/privileges.rs @@ -2,8 +2,8 @@ use core::{mem, ptr}; use std::collections::BTreeMap; use serde::Serialize; -use starknet_crypto::Felt; use starknet::core::codec::Decode; +use starknet_crypto::Felt; use crate::client::env::config::types::starknet::StarknetPrivileges; use crate::client::env::Method; diff --git a/crates/context/config/src/client/env/config/query/proxy_contract.rs b/crates/context/config/src/client/env/config/query/proxy_contract.rs index a0ddcdc8a..cf9b743a8 100644 --- a/crates/context/config/src/client/env/config/query/proxy_contract.rs +++ b/crates/context/config/src/client/env/config/query/proxy_contract.rs @@ -59,7 +59,7 @@ impl Method for ProxyContractRequest { // Parse bytes as Felt let felt = Felt::from_bytes_be_slice(&response); - + // Format felt as hex string with 0x prefix Ok(format!("0x{:x}", felt)) } diff --git a/crates/context/config/src/client/env/config/types/starknet.rs b/crates/context/config/src/client/env/config/types/starknet.rs index c69e8fb2f..9290dc77d 100644 --- a/crates/context/config/src/client/env/config/types/starknet.rs +++ b/crates/context/config/src/client/env/config/types/starknet.rs @@ -1,10 +1,11 @@ +use std::collections::BTreeMap; + use hex; -use starknet::core::codec::{Encode, Decode, Error, FeltWriter}; +use starknet::core::codec::{Decode, Encode, Error, FeltWriter}; use starknet::core::types::Felt; -use crate::repr::{Repr, ReprBytes}; + +use crate::repr::{Repr, ReprBytes, ReprTransmute}; use crate::types::SignerId; -use crate::repr::ReprTransmute; -use std::collections::BTreeMap; // Base type for all Starknet Felt pairs #[derive(Debug, Clone, Copy, Encode, Decode)] @@ -206,30 +207,42 @@ pub enum ContextRequestKind { impl From> for ContextRequestKind { fn from(value: crate::ContextRequestKind<'_>) -> Self { match value { - crate::ContextRequestKind::Add { author_id, application } => - ContextRequestKind::Add(author_id.into_inner().into(), application.into()), - crate::ContextRequestKind::UpdateApplication { application } => - ContextRequestKind::UpdateApplication(application.into()), - crate::ContextRequestKind::AddMembers { members } => - ContextRequestKind::AddMembers(members.into_iter().map(|m| m.into_inner().into()).collect()), - crate::ContextRequestKind::RemoveMembers { members } => - ContextRequestKind::RemoveMembers(members.into_iter().map(|m| m.into_inner().into()).collect()), - crate::ContextRequestKind::Grant { capabilities } => - ContextRequestKind::Grant(capabilities.into_iter() + crate::ContextRequestKind::Add { + author_id, + application, + } => ContextRequestKind::Add(author_id.into_inner().into(), application.into()), + crate::ContextRequestKind::UpdateApplication { application } => { + ContextRequestKind::UpdateApplication(application.into()) + } + crate::ContextRequestKind::AddMembers { members } => ContextRequestKind::AddMembers( + members.into_iter().map(|m| m.into_inner().into()).collect(), + ), + crate::ContextRequestKind::RemoveMembers { members } => { + ContextRequestKind::RemoveMembers( + members.into_iter().map(|m| m.into_inner().into()).collect(), + ) + } + crate::ContextRequestKind::Grant { capabilities } => ContextRequestKind::Grant( + capabilities + .into_iter() .map(|(id, cap)| CapabilityAssignment { member: id.into_inner().into(), capability: cap.into(), }) - .collect()), - crate::ContextRequestKind::Revoke { capabilities } => - ContextRequestKind::Revoke(capabilities.into_iter() + .collect(), + ), + crate::ContextRequestKind::Revoke { capabilities } => ContextRequestKind::Revoke( + capabilities + .into_iter() .map(|(id, cap)| CapabilityAssignment { member: id.into_inner().into(), capability: cap.into(), }) - .collect()), - crate::ContextRequestKind::UpdateProxyContract => - ContextRequestKind::UpdateProxyContract, + .collect(), + ), + crate::ContextRequestKind::UpdateProxyContract => { + ContextRequestKind::UpdateProxyContract + } } } } @@ -322,42 +335,43 @@ impl Encode for EncodableString { impl<'a> Decode<'a> for EncodableString { fn decode_iter(iter: &mut T) -> Result where - T: Iterator + T: Iterator, { const WORD_SIZE: usize = 31; - + // First felt is full_words_count - let full_words_count = iter.next() + let full_words_count = iter + .next() .ok_or_else(Error::input_exhausted)? .to_bytes_be()[31] as usize; - + let mut bytes = Vec::with_capacity(full_words_count * WORD_SIZE); - + // Read each full word (31 bytes each) for _ in 0..full_words_count { - let word = iter.next() + let word = iter + .next() .ok_or_else(Error::input_exhausted)? .to_bytes_be(); bytes.extend_from_slice(&word[1..WORD_SIZE + 1]); // Take exactly WORD_SIZE bytes, skipping first byte } - + // Read pending bytes (if any) - let pending = iter.next() - .ok_or_else(Error::input_exhausted)?; - - let pending_len = iter.next() + let pending = iter.next().ok_or_else(Error::input_exhausted)?; + + let pending_len = iter + .next() .ok_or_else(Error::input_exhausted)? .to_bytes_be()[31] as usize; - + if pending_len > 0 { let pending_bytes = pending.to_bytes_be(); bytes.extend_from_slice(&pending_bytes[1..pending_len + 1]); } - + // Convert bytes to string - let string = String::from_utf8(bytes) - .map_err(|_| Error::custom("Invalid UTF-8"))?; - + let string = String::from_utf8(bytes).map_err(|_| Error::custom("Invalid UTF-8"))?; + Ok(EncodableString(string)) } } @@ -387,7 +401,9 @@ pub struct StarknetApplicationRevisionRequest { impl From for StarknetApplicationRevisionRequest { - fn from(value: crate::client::env::config::query::application_revision::ApplicationRevisionRequest) -> Self { + fn from( + value: crate::client::env::config::query::application_revision::ApplicationRevisionRequest, + ) -> Self { StarknetApplicationRevisionRequest { context_id: (*value.context_id).into(), } @@ -447,20 +463,25 @@ impl<'a> From for crate::types::ApplicationMetadata<'a> { #[derive(Debug, Decode)] pub struct StarknetPrivilegeEntry { pub identity: ContextIdentity, - pub capabilities: Vec + pub capabilities: Vec, } #[derive(Debug, Decode)] pub struct StarknetPrivileges { - pub privileges: Vec + pub privileges: Vec, } impl From for BTreeMap> { fn from(value: StarknetPrivileges) -> Self { - value.privileges + value + .privileges .into_iter() - .map(|entry| (entry.identity.into(), - entry.capabilities.into_iter().map(Into::into).collect())) + .map(|entry| { + ( + entry.identity.into(), + entry.capabilities.into_iter().map(Into::into).collect(), + ) + }) .collect() } } @@ -489,15 +510,12 @@ impl From for SignerId { #[derive(Debug, Decode)] pub struct StarknetMembers { - pub members: Vec + pub members: Vec, } impl From for Vec { fn from(value: StarknetMembers) -> Self { - value.members - .into_iter() - .map(|id| id.into()) - .collect() + value.members.into_iter().map(|id| id.into()).collect() } } diff --git a/crates/context/config/src/client/env/proxy/mutate.rs b/crates/context/config/src/client/env/proxy/mutate.rs index 726e9ef4a..a49605b50 100644 --- a/crates/context/config/src/client/env/proxy/mutate.rs +++ b/crates/context/config/src/client/env/proxy/mutate.rs @@ -9,8 +9,8 @@ use crate::client::protocol::near::Near; use crate::client::protocol::starknet::Starknet; use crate::client::transport::Transport; use crate::client::{CallClient, ClientError, Operation}; -use crate::types::Signed; use crate::repr::ReprTransmute; +use crate::types::Signed; use crate::{ProposalWithApprovals, ProxyMutateRequest, Repr}; pub mod methods; diff --git a/crates/context/config/src/client/env/proxy/types/starknet.rs b/crates/context/config/src/client/env/proxy/types/starknet.rs index b669ba5db..bc4cd7f5b 100644 --- a/crates/context/config/src/client/env/proxy/types/starknet.rs +++ b/crates/context/config/src/client/env/proxy/types/starknet.rs @@ -1,8 +1,11 @@ use starknet::core::codec::{Decode, Encode}; use starknet::core::types::{Felt, U256}; -use crate::repr::{Repr, ReprTransmute, ReprBytes}; + +use crate::repr::{Repr, ReprBytes, ReprTransmute}; use crate::types::{ContextIdentity, ProposalId, SignerId}; -use crate::{Proposal, ProposalAction, ProposalApprovalWithSigner, ProposalWithApprovals, ProxyMutateRequest}; +use crate::{ + Proposal, ProposalAction, ProposalApprovalWithSigner, ProposalWithApprovals, ProxyMutateRequest, +}; #[derive(Debug, Encode, Decode)] pub struct FeltPair { @@ -74,13 +77,13 @@ pub struct StarknetApprovers { #[derive(Debug, Decode)] pub struct StarknetProposals { - pub proposals: Vec + pub proposals: Vec, } impl From for Vec { - fn from(value: StarknetProposals) -> Self { - value.proposals.into_iter().map(Into::into).collect() - } + fn from(value: StarknetProposals) -> Self { + value.proposals.into_iter().map(Into::into).collect() + } } // Conversions for StarknetIdentity @@ -204,17 +207,28 @@ impl From for StarknetConfirmationRequest { // Conversions for Actions impl From> for StarknetProposalActionWithArgs { fn from(actions: Vec) -> Self { - let action = actions.into_iter().next().expect("At least one action required"); + let action = actions + .into_iter() + .next() + .expect("At least one action required"); match action { - ProposalAction::ExternalFunctionCall { receiver_id, method_name, args, .. } => { + ProposalAction::ExternalFunctionCall { + receiver_id, + method_name, + args, + .. + } => { let args_vec: Vec = serde_json::from_str(&args).unwrap_or_default(); - let felt_args = args_vec.iter().map(|arg| { - if arg.starts_with("0x") { - Felt::from_hex_unchecked(arg) - } else { - Felt::from_bytes_be_slice(arg.as_bytes()) - } - }).collect(); + let felt_args = args_vec + .iter() + .map(|arg| { + if arg.starts_with("0x") { + Felt::from_hex_unchecked(arg) + } else { + Felt::from_bytes_be_slice(arg.as_bytes()) + } + }) + .collect(); StarknetProposalActionWithArgs::ExternalFunctionCall( Felt::from_bytes_be_slice(receiver_id.as_bytes()), @@ -222,18 +236,21 @@ impl From> for StarknetProposalActionWithArgs { felt_args, ) } - ProposalAction::Transfer { receiver_id, amount } => { - StarknetProposalActionWithArgs::Transfer( - Felt::from_bytes_be_slice(receiver_id.as_bytes()), - amount.into(), - ) - } + ProposalAction::Transfer { + receiver_id, + amount, + } => StarknetProposalActionWithArgs::Transfer( + Felt::from_bytes_be_slice(receiver_id.as_bytes()), + amount.into(), + ), ProposalAction::SetNumApprovals { num_approvals } => { StarknetProposalActionWithArgs::SetNumApprovals(Felt::from(num_approvals)) } - ProposalAction::SetActiveProposalsLimit { active_proposals_limit } => { - StarknetProposalActionWithArgs::SetActiveProposalsLimit(Felt::from(active_proposals_limit)) - } + ProposalAction::SetActiveProposalsLimit { + active_proposals_limit, + } => StarknetProposalActionWithArgs::SetActiveProposalsLimit(Felt::from( + active_proposals_limit, + )), ProposalAction::SetContextValue { key, value } => { StarknetProposalActionWithArgs::SetContextValue( key.chunks(16).map(Felt::from_bytes_be_slice).collect(), @@ -251,7 +268,8 @@ impl From for ProposalAction { ProposalAction::ExternalFunctionCall { receiver_id: format!("0x{}", hex::encode(contract.to_bytes_be())), method_name: format!("0x{}", hex::encode(selector.to_bytes_be())), - args: calldata.iter() + args: calldata + .iter() .map(|felt| format!("0x{}", hex::encode(felt.to_bytes_be()))) .collect::>() .join(","), @@ -263,18 +281,23 @@ impl From for ProposalAction { let FeltPair { high, low } = amount.0; ProposalAction::Transfer { receiver_id: format!("0x{}", hex::encode(receiver.to_bytes_be())), - amount: u128::from_be_bytes(low.to_bytes_be()[16..32].try_into().unwrap()) + - (u128::from_be_bytes(high.to_bytes_be()[16..32].try_into().unwrap()) << 64), + amount: u128::from_be_bytes(low.to_bytes_be()[16..32].try_into().unwrap()) + + (u128::from_be_bytes(high.to_bytes_be()[16..32].try_into().unwrap()) + << 64), } } StarknetProposalActionWithArgs::SetNumApprovals(num) => { ProposalAction::SetNumApprovals { - num_approvals: u32::from_be_bytes(num.to_bytes_be()[28..32].try_into().unwrap()), + num_approvals: u32::from_be_bytes( + num.to_bytes_be()[28..32].try_into().unwrap(), + ), } } StarknetProposalActionWithArgs::SetActiveProposalsLimit(limit) => { ProposalAction::SetActiveProposalsLimit { - active_proposals_limit: u32::from_be_bytes(limit.to_bytes_be()[28..32].try_into().unwrap()), + active_proposals_limit: u32::from_be_bytes( + limit.to_bytes_be()[28..32].try_into().unwrap(), + ), } } StarknetProposalActionWithArgs::SetContextValue(key, value) => { @@ -291,14 +314,19 @@ impl From for ProposalWithApprovals { fn from(value: StarknetProposalWithApprovals) -> Self { ProposalWithApprovals { proposal_id: Repr::new(value.proposal_id.into()), - num_approvals: u32::from_be_bytes(value.num_approvals.to_bytes_be()[28..32].try_into().unwrap()) as usize, + num_approvals: u32::from_be_bytes( + value.num_approvals.to_bytes_be()[28..32] + .try_into() + .unwrap(), + ) as usize, } } } impl From for Vec { fn from(value: StarknetApprovers) -> Self { - value.approvers + value + .approvers .into_iter() .map(|identity| { let mut bytes = [0u8; 32];