Skip to content

Commit

Permalink
Return UUID (#54)
Browse files Browse the repository at this point in the history
* path reconfig

* uuid response

* update version

* uuid

* withdrawal_data_vec
  • Loading branch information
kbizikav authored Jan 2, 2025
1 parent ed8eb74 commit 6dedc6a
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 60 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.1.3"
version = "0.1.4"
edition = "2021"

[workspace.dependencies]
Expand Down
4 changes: 3 additions & 1 deletion cli/src/cli/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ pub async fn deposit(
let token_address = convert_address(token_address);
let token_id = convert_u256(token_id);

let deposit_data = client
let deposit_result = client
.prepare_deposit(key.pubkey, amount, token_type, token_address, token_id)
.await?;

let deposit_data = deposit_result.deposit_data;

match token_type {
TokenType::NATIVE => {
liquidity_contract
Expand Down
53 changes: 41 additions & 12 deletions client-sdk/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,21 @@ pub struct TxRequestMemo {
pub prev_private_commitment: PoseidonHashOut,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DepositResult {
pub deposit_data: DepositData,
pub deposit_uuid: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TxResult {
pub tx_tree_root: Bytes32,
pub transfer_data_vec: Vec<TransferData<F, C, D>>,
pub withdrawal_data_vec: Vec<TransferData<F, C, D>>,
pub transfer_uuids: Vec<String>,
pub withdrawal_uuids: Vec<String>,
}

impl<BB, S, V, B, W> Client<BB, S, V, B, W>
Expand All @@ -103,7 +113,7 @@ where
token_type: TokenType,
token_address: Address,
token_id: U256,
) -> Result<DepositData, ClientError> {
) -> Result<DepositResult, ClientError> {
log::info!(
"prepare_deposit: pubkey {}, amount {}, token_type {:?}, token_address {}, token_id {}",
pubkey,
Expand All @@ -125,11 +135,17 @@ where
token_id,
token_index: None,
};
self.store_vault_server
let deposit_uuid = self
.store_vault_server
.save_data(DataType::Deposit, pubkey, &deposit_data.encrypt(pubkey))
.await?;

Ok(deposit_data)
let result = DepositResult {
deposit_data,
deposit_uuid,
};

Ok(result)
}

/// Send a transaction request to the block builder
Expand Down Expand Up @@ -286,7 +302,7 @@ where
transfer_tree.push(transfer.clone());
}

let mut transfer_data_vec = Vec::new();
let mut all_transfer_data_vec = Vec::new();
for (i, transfer) in memo.transfers.iter().enumerate() {
let transfer_merkle_proof = transfer_tree.prove(i as u64);
let transfer_data = TransferData {
Expand All @@ -298,29 +314,39 @@ where
transfer_index: i as u32,
transfer_merkle_proof,
};
transfer_data_vec.push(transfer_data);
all_transfer_data_vec.push(transfer_data);
}

let encrypted_transfer_data_vec = transfer_data_vec
.iter()
let transfer_data_vec = all_transfer_data_vec
.clone()
.into_iter()
// filter out eth-address recipients (withdrawal)
.filter(|data| data.transfer.recipient.is_pubkey)
.collect::<Vec<_>>();
let withdrawal_data_vec = all_transfer_data_vec
.into_iter()
// filter out pubkey recipients (transfer)
.filter(|data| !data.transfer.recipient.is_pubkey)
.collect::<Vec<_>>();

let encrypted_transfer_data_vec = transfer_data_vec
.iter()
.map(|data| {
let recipient = data.transfer.recipient.to_pubkey().unwrap();
(recipient, data.encrypt(recipient))
})
.collect::<Vec<_>>();
let encrypted_withdrawal_data_vec = transfer_data_vec
let encrypted_withdrawal_data_vec = withdrawal_data_vec
.iter()
// filter out pubkey recipients (transfer)
.filter(|data| !data.transfer.recipient.is_pubkey)
.map(|data| (key.pubkey, data.encrypt(key.pubkey)))
.collect::<Vec<_>>();

self.store_vault_server
let transfer_uuids = self
.store_vault_server
.save_data_batch(DataType::Transfer, encrypted_transfer_data_vec)
.await?;
self.store_vault_server
let withdrawal_uuids = self
.store_vault_server
.save_data_batch(DataType::Withdrawal, encrypted_withdrawal_data_vec)
.await?;

Expand All @@ -339,6 +365,9 @@ where
let result = TxResult {
tx_tree_root: proposal.tx_tree_root,
transfer_data_vec,
withdrawal_data_vec,
transfer_uuids,
withdrawal_uuids,
};

Ok(result)
Expand Down
21 changes: 12 additions & 9 deletions client-sdk/src/external_api/store_vault_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use intmax2_interfaces::{
interface::{DataType, StoreVaultClientInterface},
types::{
BatchGetDataQuery, BatchGetDataResponse, BatchSaveDataRequest,
GetBalanceProofQuery, GetBalanceProofResponse, GetDataAllAfterQuery,
GetDataAllAfterResponse, GetDataQuery, GetDataResponse, GetUserDataQuery,
GetUserDataResponse, SaveBalanceProofRequest, SaveDataRequest,
BatchSaveDataResponse, GetBalanceProofQuery, GetBalanceProofResponse,
GetDataAllAfterQuery, GetDataAllAfterResponse, GetDataQuery, GetDataResponse,
GetUserDataQuery, GetUserDataResponse, SaveBalanceProofRequest, SaveDataRequest,
SaveDataResponse,
},
},
},
Expand Down Expand Up @@ -83,31 +84,33 @@ impl StoreVaultClientInterface for StoreVaultServerClient {
data_type: DataType,
pubkey: U256,
encrypted_data: &[u8],
) -> Result<(), ServerError> {
) -> Result<String, ServerError> {
let request = SaveDataRequest {
pubkey,
data: encrypted_data.to_vec(),
};
post_request::<_, ()>(
let response: SaveDataResponse = post_request(
&self.base_url,
&format!("/store-vault-server/{}/save", data_type.to_string()),
&request,
)
.await
.await?;
Ok(response.uuid)
}

async fn save_data_batch(
&self,
data_type: DataType,
data: Vec<(U256, Vec<u8>)>,
) -> Result<(), ServerError> {
) -> Result<Vec<String>, ServerError> {
let request = BatchSaveDataRequest { requests: data };
post_request::<_, ()>(
let response: BatchSaveDataResponse = post_request(
&self.base_url,
&format!("/store-vault-server/{}/batch-save", data_type.to_string()),
&request,
)
.await
.await?;
Ok(response.uuids)
}

async fn get_data(
Expand Down
4 changes: 2 additions & 2 deletions interfaces/src/api/store_vault_server/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub trait StoreVaultClientInterface {
data_type: DataType,
pubkey: U256,
encrypted_data: &[u8],
) -> Result<(), ServerError>;
) -> Result<String, ServerError>;

async fn save_data_batch(
&self,
data_type: DataType,
data: Vec<(U256, Vec<u8>)>,
) -> Result<(), ServerError>;
) -> Result<Vec<String>, ServerError>;

async fn get_data(
&self,
Expand Down
12 changes: 12 additions & 0 deletions interfaces/src/api/store_vault_server/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ pub struct SaveDataRequest {
pub data: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SaveDataResponse {
pub uuid: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchSaveDataRequest {
pub requests: Vec<(U256, Vec<u8>)>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchSaveDataResponse {
pub uuids: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetUserDataQuery {
Expand Down
17 changes: 7 additions & 10 deletions store-vault-server/src/api/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use actix_web::{
use intmax2_interfaces::api::store_vault_server::{
interface::DataType,
types::{
BatchGetDataQuery, BatchGetDataResponse, BatchSaveDataRequest, GetBalanceProofQuery,
GetBalanceProofResponse, GetDataAllAfterQuery, GetDataAllAfterResponse, GetDataQuery,
GetDataResponse, GetUserDataQuery, GetUserDataResponse, SaveBalanceProofRequest,
SaveDataRequest,
BatchGetDataQuery, BatchGetDataResponse, BatchSaveDataRequest, BatchSaveDataResponse, GetBalanceProofQuery, GetBalanceProofResponse, GetDataAllAfterQuery, GetDataAllAfterResponse, GetDataQuery, GetDataResponse, GetUserDataQuery, GetUserDataResponse, SaveBalanceProofRequest, SaveDataRequest, SaveDataResponse
},
};
use serde_qs::actix::QsQuery;
Expand Down Expand Up @@ -51,25 +48,25 @@ pub async fn save_data(
state: Data<State>,
path: Path<String>,
request: Json<SaveDataRequest>,
) -> Result<Json<()>, Error> {
) -> Result<Json<SaveDataResponse>, Error> {
let data_type = path.into_inner();
let data_type = DataType::from_str(data_type.as_str())
.map_err(|e| actix_web::error::ErrorInternalServerError(format!("Invalid type: {}", e)))?;
let request = request.into_inner();
state
let uuid = state
.store_vault_server
.save_data(data_type, request.pubkey, request.data)
.await
.map_err(|e| actix_web::error::ErrorInternalServerError(e))?;
Ok(Json(()))
Ok(Json(SaveDataResponse { uuid }))
}

#[post("/{type}/batch-save")]
pub async fn batch_save_data(
state: Data<State>,
path: Path<String>,
request: Json<BatchSaveDataRequest>,
) -> Result<Json<()>, Error> {
) -> Result<Json<BatchSaveDataResponse>, Error> {
const MAX_BATCH_SIZE: usize = 1000;

let data_type = path.into_inner();
Expand All @@ -85,13 +82,13 @@ pub async fn batch_save_data(
)));
}

state
let uuids = state
.store_vault_server
.batch_save_data(data_type, requests)
.await
.map_err(|e| actix_web::error::ErrorInternalServerError(e))?;

Ok(Json(()))
Ok(Json(BatchSaveDataResponse { uuids }))
}

#[get("/{type}/get")]
Expand Down
1 change: 0 additions & 1 deletion store-vault-server/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod api;
pub mod state;
pub mod store_vault_server;
Loading

0 comments on commit 6dedc6a

Please sign in to comment.