Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Nov 24, 2024
1 parent 6c35494 commit bf45efe
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 86 deletions.
8 changes: 6 additions & 2 deletions crates/context/config/src/client/env/config/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ impl<'a> Method<Starknet> for Mutate<'a> {
let user_key_bytes = user_key.to_bytes();

// Create Repr wrapped ContextIdentity instances
let signer_id = verifying_key_bytes.rt::<ContextIdentity>().expect("Infallible conversion");
let signer_id = verifying_key_bytes
.rt::<ContextIdentity>()
.expect("Infallible conversion");
let signer_id = Repr::new(signer_id);

let user_id = user_key_bytes.rt::<ContextIdentity>().expect("Infallible conversion");
let user_id = user_key_bytes
.rt::<ContextIdentity>()
.expect("Infallible conversion");
let user_id = Repr::new(user_id);

// Create the Request structure using into() conversions
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions crates/context/config/src/client/env/config/query/members.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Method<Starknet> 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))
}
Expand Down
112 changes: 65 additions & 47 deletions crates/context/config/src/client/env/config/types/starknet.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -206,30 +207,42 @@ pub enum ContextRequestKind {
impl From<crate::ContextRequestKind<'_>> 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
}
}
}
}
Expand Down Expand Up @@ -322,42 +335,43 @@ impl Encode for EncodableString {
impl<'a> Decode<'a> for EncodableString {
fn decode_iter<T>(iter: &mut T) -> Result<Self, Error>
where
T: Iterator<Item = &'a Felt>
T: Iterator<Item = &'a Felt>,
{
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))
}
}
Expand Down Expand Up @@ -387,7 +401,9 @@ pub struct StarknetApplicationRevisionRequest {
impl From<crate::client::env::config::query::application_revision::ApplicationRevisionRequest>
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(),
}
Expand Down Expand Up @@ -447,20 +463,25 @@ impl<'a> From<EncodableString> for crate::types::ApplicationMetadata<'a> {
#[derive(Debug, Decode)]
pub struct StarknetPrivilegeEntry {
pub identity: ContextIdentity,
pub capabilities: Vec<Capability>
pub capabilities: Vec<Capability>,
}

#[derive(Debug, Decode)]
pub struct StarknetPrivileges {
pub privileges: Vec<StarknetPrivilegeEntry>
pub privileges: Vec<StarknetPrivilegeEntry>,
}

impl From<StarknetPrivileges> for BTreeMap<SignerId, Vec<crate::Capability>> {
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()
}
}
Expand Down Expand Up @@ -489,15 +510,12 @@ impl From<ContextIdentity> for SignerId {

#[derive(Debug, Decode)]
pub struct StarknetMembers {
pub members: Vec<ContextIdentity>
pub members: Vec<ContextIdentity>,
}

impl From<StarknetMembers> for Vec<crate::types::ContextIdentity> {
fn from(value: StarknetMembers) -> Self {
value.members
.into_iter()
.map(|id| id.into())
.collect()
value.members.into_iter().map(|id| id.into()).collect()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/context/config/src/client/env/proxy/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit bf45efe

Please sign in to comment.