Skip to content

Commit

Permalink
fix: resolved comments from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Nov 24, 2024
1 parent 9558750 commit 6c35494
Show file tree
Hide file tree
Showing 20 changed files with 559 additions and 650 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions contracts/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ impl ContextConfigs {
.expect("unable to update member list")
.priviledges()
.grant(identity),
Capability::Proxy => context
.proxy
.get(signer_id)
.expect("unable to update proxy")
.priviledges()
.grant(identity),
};

env::log_str(&format!(
Expand Down Expand Up @@ -289,6 +295,12 @@ impl ContextConfigs {
.expect("unable to update member list")
.priviledges()
.revoke(&identity),
Capability::Proxy => context
.proxy
.get(signer_id)
.expect("unable to update proxy")
.priviledges()
.revoke(&identity),
};

env::log_str(&format!(
Expand Down
4 changes: 0 additions & 4 deletions crates/context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ license.workspace = true
camino = { workspace = true, features = ["serde1"] }
eyre.workspace = true
futures-util.workspace = true
hex.workspace = true
rand.workspace = true
reqwest = { workspace = true, features = ["stream"] }
serde.workspace = true
tokio = { workspace = true, features = ["sync", "macros"] }
tokio-util.workspace = true
tracing.workspace = true

starknet-crypto = { workspace = true }
starknet = { workspace = true }

calimero-context-config = { path = "./config", features = ["client"] }
calimero-blobstore = { path = "../store/blobs" }
calimero-primitives = { path = "../primitives", features = ["borsh", "rand"] }
Expand Down
17 changes: 6 additions & 11 deletions crates/context/config/src/client/env/config/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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::repr::{Repr, ReprBytes, ReprTransmute};
use crate::repr::{Repr, ReprTransmute};
use crate::types::Signed;
use crate::{ContextIdentity, Request, RequestKind};
pub mod methods;
Expand Down Expand Up @@ -76,15 +76,11 @@ impl<'a> Method<Starknet> for Mutate<'a> {
let user_key_bytes = user_key.to_bytes();

// Create Repr wrapped ContextIdentity instances
let signer_id = Repr::new(ContextIdentity::from_bytes(|bytes| {
bytes.copy_from_slice(&verifying_key_bytes);
Ok(32)
})?);
let signer_id = verifying_key_bytes.rt::<ContextIdentity>().expect("Infallible conversion");
let signer_id = Repr::new(signer_id);

let user_id = Repr::new(ContextIdentity::from_bytes(|bytes| {
bytes.copy_from_slice(&user_key_bytes);
Ok(32)
})?);
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
let request = StarknetRequest {
Expand Down Expand Up @@ -121,8 +117,7 @@ impl<'a> Method<Starknet> for Mutate<'a> {
Ok(bytes)
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
println!("decode {:?}", response);
fn decode(_response: Vec<u8>) -> eyre::Result<Self::Returns> {
Ok(())
}
}
Expand Down
58 changes: 17 additions & 41 deletions crates/context/config/src/client/env/config/query/application.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use serde::Serialize;
use starknet_crypto::Felt;

use crate::client::env::config::types::starknet::Application as StarknetApplication;
use crate::client::env::Method;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::repr::{Repr, ReprBytes, ReprTransmute};
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 Expand Up @@ -62,49 +64,23 @@ impl Method<Starknet> for ApplicationRequest {
return Err(eyre::eyre!("No application found"));
}

// Convert bytes to Felts
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().unwrap()));
}
}

// Check if it's a None response (single zero Felt)
if response.len() == 32 && response.iter().all(|&x| x == 0) {
if felts.len() == 1 && felts[0] == Felt::ZERO {
return Err(eyre::eyre!("No application found"));
}

// First 32 bytes is the flag/version (0x0), skip it
let response = &response[32..];

// Next two Felts are application id (high/low)
let mut id_bytes = [0u8; 32];
id_bytes[..16].copy_from_slice(&response[16..32]); // high part
id_bytes[16..].copy_from_slice(&response[48..64]); // low part
let id = Repr::new(id_bytes.rt()?);

// Next two Felts are blob id (high/low)
let mut blob_bytes = [0u8; 32];
blob_bytes[..16].copy_from_slice(&response[80..96]); // high part
blob_bytes[16..].copy_from_slice(&response[112..128]); // low part
let blob = Repr::new(blob_bytes.rt()?);

// Next Felt is size (0x1af25)
let size = u64::from_be_bytes(response[152..160].try_into()?);

// Source string starts after the length Felt (0x2)
let mut source_bytes = Vec::new();
let mut i = 192; // Start after length Felt
while i < response.len() {
let chunk = &response[i..];
if chunk.iter().take(32).all(|&b| b == 0) {
break;
}
source_bytes.extend(chunk.iter().take(32).filter(|&&b| b != 0));
i += 32;
}
let source = ApplicationSource(String::from_utf8(source_bytes)?.into());

// Find metadata after source string (look for 0.0.1)
let metadata_bytes: Vec<u8> = response
.windows(5)
.find(|window| window == b"0.0.1")
.map(|_| b"0.0.1".to_vec())
.unwrap_or_default();
let metadata = ApplicationMetadata(Repr::new(metadata_bytes.into()));
Ok(Application::new(id, blob, size, source, metadata))
// Skip the flag/version felt and decode the application
let application = StarknetApplication::decode(&felts[1..])
.map_err(|e| eyre::eyre!("Failed to decode application: {:?}", e))?;

Ok(application.into())
}
}
39 changes: 18 additions & 21 deletions crates/context/config/src/client/env/config/query/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use serde::Serialize;
use starknet::core::codec::Encode;
use starknet_crypto::Felt;

use crate::client::env::config::types::starknet::StarknetMembersRequest;
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 crate::repr::{Repr, ReprBytes, ReprTransmute};
use starknet::core::codec::Decode;
use crate::repr::Repr;
use crate::types::{ContextId, ContextIdentity};

#[derive(Copy, Clone, Debug, Serialize)]
Expand Down Expand Up @@ -65,27 +66,23 @@ impl Method<Starknet> for MembersRequest {
return Ok(Vec::new());
}

// First 32 bytes contain the count, skip it
let response = &response[32..];

let members: Result<Vec<ContextIdentity>, _> = response
.chunks_exact(64)
.map(|chunk| {
let felt1 = Felt::from_bytes_be_slice(&chunk[..32]);
let felt2 = Felt::from_bytes_be_slice(&chunk[32..]);
// Convert bytes to Felts
let mut felts = Vec::new();
for chunk in response.chunks(32) {
let mut padded_chunk = [0u8; 32];
padded_chunk[..chunk.len()].copy_from_slice(chunk);
felts.push(Felt::from_bytes_be(&padded_chunk));
}

let felt1_bytes = felt1.to_bytes_be();
let felt2_bytes = felt2.to_bytes_be();
// Check if it's a None response (single zero Felt)
if felts.len() == 1 && felts[0] == Felt::ZERO {
return Ok(Vec::new());
}

ContextIdentity::from_bytes(|bytes| {
bytes[..16].copy_from_slice(&felt1_bytes[16..]);
bytes[16..].copy_from_slice(&felt2_bytes[16..]);
Ok(32)
})
})
.collect();
// Decode directly from the felts slice - the Decode trait will handle the array structure
let members = StarknetMembers::decode(&felts)
.map_err(|e| eyre::eyre!("Failed to decode members: {:?}", e))?;

let members = members.map_err(|e| eyre::eyre!("Failed to decode members: {:?}", e))?;
Ok(members)
Ok(members.into())
}
}
50 changes: 20 additions & 30 deletions crates/context/config/src/client/env/config/query/privileges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::collections::BTreeMap;

use serde::Serialize;
use starknet_crypto::Felt;
use starknet::core::codec::Decode;

use crate::client::env::config::types::starknet::StarknetPrivileges;
use crate::client::env::Method;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
Expand Down Expand Up @@ -89,39 +91,27 @@ impl<'a> Method<Starknet> for PrivilegesRequest<'a> {
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
// if response.is_empty() {
// return Ok(BTreeMap::new());
// }

// let mut result = BTreeMap::new();
// let mut offset = 0;

// // First felt is array length
// let array_len = u64::from_be_bytes(response[24..32].try_into()?);
// offset += 32;

// // Process each (identity, capabilities) pair
// for _ in 0..array_len {
// // Read identity from 2 felts (32 bytes each)
// let identity = SignerId::from_bytes(|bytes| {
// // Take the second felt (low part) for SignerId
// bytes.copy_from_slice(&response[offset + 32..offset + 64]);
// Ok(32) // Return successful result with number of bytes written
// })?;
// offset += 64; // Skip both felts (high and low parts)

// // Read capability (1 felt)
// let cap_value = u64::from_be_bytes(response[offset + 24..offset + 32].try_into()?);
// offset += 32;
if response.is_empty() {
return Ok(BTreeMap::new());
}

// // Create capabilities vector with single capability
// let capabilities = vec![Capability::from(cap_value)];
// Convert bytes to Felts
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().unwrap()));
}
}

// result.insert(identity, capabilities);
// }
// Check if it's a None response (single zero Felt)
if felts.len() == 1 && felts[0] == Felt::ZERO {
return Ok(BTreeMap::new());
}

// Ok(result)
// Skip the flag/version felt and decode the privileges
let privileges = StarknetPrivileges::decode(&felts[1..])
.map_err(|e| eyre::eyre!("Failed to decode privileges: {:?}", e))?;

todo!()
Ok(privileges.into())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl Method<Starknet> for ProxyContractRequest {
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
println!("response {:?}", response);
if response.is_empty() {
return Err(eyre::eyre!("No proxy contract found"));
}
Expand All @@ -58,9 +57,10 @@ impl Method<Starknet> for ProxyContractRequest {
return Err(eyre::eyre!("No proxy contract found"));
}

// Convert the Felt to a hex string representing the contract address
let hex_string = format!("0x{}", hex::encode(&response));

Ok(hex_string)
// 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))
}
}
Loading

0 comments on commit 6c35494

Please sign in to comment.