Skip to content

Commit

Permalink
feat: resolved mutate ICP encode and decode, query privileges for exa…
Browse files Browse the repository at this point in the history
…mplE2
  • Loading branch information
alenmestrov committed Dec 1, 2024
1 parent 0f7b182 commit f9444f2
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 533 deletions.
29 changes: 16 additions & 13 deletions crates/context/config/src/client/env/config/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use starknet::core::codec::Encode as StarknetEncode;
use starknet::signers::SigningKey as StarknetSigningKey;
use starknet_crypto::{poseidon_hash_many, Felt};

use super::types::icp::{ICPSigned, ICPRequest};
use super::types::starknet::{Request as StarknetRequest, Signed as StarknetSigned};
use crate::client::env::{utils, Method};
use crate::client::protocol::icp::Icp;
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::icpTypes::ICMutate;
// use crate::icpTypes::ICMutate;
use crate::repr::{Repr, ReprTransmute};
use crate::types::Signed;
use crate::{ContextIdentity, Request, RequestKind};
Expand Down Expand Up @@ -135,21 +136,23 @@ impl<'a> Method<Icp> for Mutate<'a> {
const METHOD: &'static str = "mutate";

fn encode(self) -> eyre::Result<Vec<u8>> {
let signing_key = &self.signing_key;
let nonce = &self.nonce;
let kind = &self.kind;

let request = ICMutate {
signing_key: *signing_key,
nonce: *nonce,
kind: kind.into(),
};
let signer_sk = SigningKey::from_bytes(&self.signing_key);

let request = ICPRequest::new(signer_sk.verifying_key().rt()?, self.kind.into());

let signed = ICPSigned::new(request, |b| signer_sk.sign(b))?;

Encode!(&self).map_err(|e| eyre::eyre!(e))
let encoded = candid::encode_one(&signed)?;

Ok(encoded)
}

fn decode(_response: Vec<u8>) -> eyre::Result<Self::Returns> {
todo!()
fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
if !response.is_empty() {
eyre::bail!("unexpected response {:?}", response);
}

Ok(())
}
}

Expand Down
31 changes: 17 additions & 14 deletions crates/context/config/src/client/env/config/query/privileges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Serialize;
use starknet::core::codec::{Decode as StarknetDecode, Encode as StarknetEncode, FeltWriter};
use starknet_crypto::Felt;

use crate::client::env::config::types::icp::{ICCapability, ICContextId, ICContextIdentity, ICSignerId};
use crate::client::env::config::types::starknet::{
CallData, ContextId as StarknetContextId, ContextIdentity as StarknetContextIdentity,
StarknetPrivileges,
Expand All @@ -14,10 +15,7 @@ use crate::client::env::Method;
use crate::client::protocol::icp::Icp;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::icpTypes::{
ICCapability, ICContextId, ICContextIdentity, ICPrivilegesRequest, ICSignerId,
};
use crate::repr::Repr;
use crate::repr::{Repr, ReprTransmute};
use crate::types::{Capability, ContextId, ContextIdentity, SignerId};

#[derive(Copy, Clone, Debug, Serialize)]
Expand Down Expand Up @@ -138,22 +136,27 @@ impl<'a> Method<Icp> for PrivilegesRequest<'a> {
const METHOD: &'static str = "privileges";

fn encode(self) -> eyre::Result<Vec<u8>> {
let context_id: ICContextId = self.context_id.into();
let identities: ICContextIdentity = self.identities.into();
let request = ICPrivilegesRequest {
context_id,
identities,
};
Encode!(&request).map_err(|e| eyre::eyre!(e))
// Convert context_id and identities to ICP types
let context_id: ICContextId = (*self.context_id).rt()?;
let identities: Vec<ICContextIdentity> = self.identities
.iter()
.map(|id| (*id).rt())
.collect::<Result<Vec<_>, _>>()?;

// Create a tuple of the values we want to encode
let payload = (context_id, identities);

// Encode using Candid
Encode!(&payload)
.map_err(|e| eyre::eyre!("Failed to encode privileges request: {}", e))
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
let decoded: BTreeMap<ICSignerId, Vec<ICCapability>> =
Decode!(&response, BTreeMap<ICSignerId, Vec<ICCapability>>)?;
let value: Self::Returns = decoded
Ok(decoded
.into_iter()
.map(|(k, v)| (k.into(), v.into_iter().map(Into::into).collect()))
.collect();
Ok(value)
.collect())
}
}
1 change: 1 addition & 0 deletions crates/context/config/src/client/env/config/types.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod starknet;
pub mod icp;
Loading

0 comments on commit f9444f2

Please sign in to comment.