Skip to content

Commit

Permalink
fix: encode / decode
Browse files Browse the repository at this point in the history
  • Loading branch information
frdomovic committed Jan 31, 2025
1 parent 394a81a commit d7653f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use std::io::Cursor;

use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine as _;
use candid::{Decode, Encode, Principal};
use serde::Serialize;
use soroban_sdk::xdr::{Limited, Limits, ReadXdr, ScVal};
use soroban_sdk::{Address, Env, TryFromVal};
use starknet::core::codec::Encode as StarknetEncode;
use starknet_crypto::Felt;

Expand Down Expand Up @@ -90,8 +96,24 @@ impl Method<Stellar> for ProxyContractRequest {
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
let value = String::from_utf8(response)
.map_err(|e| eyre::eyre!("Failed to decode proxy contract address: {}", e))?;
Ok(value)
let base64_str = BASE64.encode(&response);

let xdr_bytes = BASE64
.decode(base64_str)
.map_err(|e| format!("Failed to decode base64: {}", e))
.unwrap();

let cursor = Cursor::new(xdr_bytes);
let mut limited = Limited::new(cursor, Limits::none());

let sc_val = ScVal::read_xdr(&mut limited)
.map_err(|e| format!("Failed to read XDR: {}", e))
.unwrap();

let env = Env::default();
match Address::try_from_val(&env, &sc_val) {
Ok(address) => Ok(address.to_string().to_string()),
Err(e) => return Err(eyre::eyre!("failed to convert to address: {:?}", e)),
}
}
}
80 changes: 49 additions & 31 deletions crates/context/config/src/client/protocol/stellar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,37 +188,12 @@ impl ProtocolTransport for StellarTransport<'_> {
));
};

let mut args = None;

if !payload.is_empty() {
let env = Env::default();
let env_bytes = Bytes::from_slice(&env, &payload);
let signed_request =
StellarSignedRequest::from_xdr(&env, &env_bytes).map_err(|_| {
StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to deserialize signed request".to_owned(),
}
})?;
let val: Val = signed_request
.try_into_val(&env)
.map_err(|_| StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to convert to Val".to_owned(),
})?;
let sc_val: ScVal = val.try_into_val(&env).map_err(|_| StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to convert to ScVal".to_owned(),
})?;
args = Some(vec![sc_val]);
}

let contract: Contracts = Contracts::new(&request.contract_id)
.map_err(|_| StellarError::InvalidContractId(request.contract_id.into_owned()))?;

match request.operation {
Operation::Read { method } => network.query(&contract, &method, args).await,
Operation::Write { method } => network.mutate(&contract, &method, args).await,
Operation::Read { method } => network.query(&contract, &method, payload).await,
Operation::Write { method } => network.mutate(&contract, &method, payload).await,
}
}
}
Expand All @@ -228,7 +203,7 @@ impl Network {
&self,
contract: &Contracts,
method: &str,
args: Option<Vec<ScVal>>,
args: Vec<u8>,
) -> Result<Vec<u8>, StellarError> {
let account = self
.client
Expand All @@ -241,9 +216,27 @@ impl Network {

let source_account = Rc::new(RefCell::new(account));

let mut encoded_args = None;

if !args.is_empty() {
let env = Env::default();
let env_bytes = Bytes::from_slice(&env, &args);
let val: Val = env_bytes
.try_into_val(&env)
.map_err(|_| StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to convert to Val".to_owned(),
})?;
let sc_val: ScVal = val.try_into_val(&env).map_err(|_| StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to convert to ScVal".to_owned(),
})?;
encoded_args = Some(vec![sc_val]);
}

let transaction = TransactionBuilder::new(source_account, self.network.as_str(), None)
.fee(10000u32)
.add_operation(contract.call(method, args))
.add_operation(contract.call(method, encoded_args))
.set_timeout(15)
.expect("Transaction timeout")
.build();
Expand Down Expand Up @@ -288,7 +281,7 @@ impl Network {
&self,
contract: &Contracts,
method: &str,
args: Option<Vec<ScVal>>,
args: Vec<u8>,
) -> Result<Vec<u8>, StellarError> {
let account = self
.client
Expand All @@ -301,9 +294,34 @@ impl Network {

let source_account = Rc::new(RefCell::new(account));

let mut encoded_args = None;

if !args.is_empty() {
let env = Env::default();
let env_bytes = Bytes::from_slice(&env, &args);
let signed_request =
StellarSignedRequest::from_xdr(&env, &env_bytes).map_err(|_| {
StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to deserialize signed request".to_owned(),
}
})?;
let val: Val = signed_request
.try_into_val(&env)
.map_err(|_| StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to convert to Val".to_owned(),
})?;
let sc_val: ScVal = val.try_into_val(&env).map_err(|_| StellarError::Custom {
operation: ErrorOperation::Query,
reason: "Failed to convert to ScVal".to_owned(),
})?;
encoded_args = Some(vec![sc_val]);
}

let transaction = TransactionBuilder::new(source_account, self.network.as_str(), None)
.fee(10000u32)
.add_operation(contract.call(method, args))
.add_operation(contract.call(method, encoded_args))
.set_timeout(15)
.expect("Transaction timeout")
.build();
Expand Down

0 comments on commit d7653f6

Please sign in to comment.