mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
cashierd: catch bridge messages and send proper error message to the user
This commit is contained in:
@@ -32,6 +32,8 @@ use drk::{
|
||||
fn handle_bridge_error(error_code: u32) -> Result<()> {
|
||||
match error_code {
|
||||
1 => Err(Error::BridgeError("Not Supported Client".into())),
|
||||
2 => Err(Error::BridgeError("Unable to watch the deposit address".into())),
|
||||
3 => Err(Error::BridgeError("Unable to send the token".into())),
|
||||
_ => Err(Error::BridgeError("Unknown error_code".into())),
|
||||
}
|
||||
}
|
||||
@@ -168,16 +170,21 @@ impl Cashierd {
|
||||
|
||||
// check the response's error
|
||||
let error_code = res.error as u32;
|
||||
if error_code == 0 {
|
||||
match res.payload {
|
||||
bridge::BridgeResponsePayload::Send => {
|
||||
cashier_wallet.confirm_withdraw_key_record(&addr, &network)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
|
||||
if error_code != 0 {
|
||||
return handle_bridge_error(error_code);
|
||||
}
|
||||
|
||||
match res.payload {
|
||||
bridge::BridgeResponsePayload::Send => {
|
||||
cashier_wallet.confirm_withdraw_key_record(&addr, &network)?;
|
||||
}
|
||||
_ => {
|
||||
return Err(Error::BridgeError(
|
||||
"Receive unknown value from Subscription".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -35,6 +35,8 @@ pub enum BridgeResponsePayload {
|
||||
pub enum BridgeResponseError {
|
||||
NoError,
|
||||
NotSupportedClient,
|
||||
BridgeWatchSubscribtionError,
|
||||
BridgeSendSubscribtionError,
|
||||
}
|
||||
|
||||
pub struct BridgeSubscribtion {
|
||||
@@ -42,6 +44,7 @@ pub struct BridgeSubscribtion {
|
||||
pub receiver: async_channel::Receiver<BridgeResponse>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TokenSubscribtion {
|
||||
pub secret_key: Vec<u8>,
|
||||
pub public_key: String,
|
||||
@@ -156,37 +159,65 @@ impl Bridge {
|
||||
client = c.clone();
|
||||
}
|
||||
|
||||
let res: BridgeResponse;
|
||||
|
||||
match req.payload {
|
||||
BridgeRequestsPayload::Watch(val) => match val {
|
||||
Some((private_key, public_key)) => {
|
||||
let pub_key = client
|
||||
.subscribe_with_keypair(private_key, public_key, drk_pub_key, mint_address)
|
||||
.await?;
|
||||
let res = BridgeResponse {
|
||||
error: BridgeResponseError::NoError,
|
||||
payload: BridgeResponsePayload::Address(pub_key),
|
||||
};
|
||||
rep.send(res).await?;
|
||||
.await;
|
||||
|
||||
if pub_key.is_err() {
|
||||
error!(target: "BRIDGE", "{}", pub_key.unwrap_err().to_string());
|
||||
res = BridgeResponse {
|
||||
error: BridgeResponseError::BridgeWatchSubscribtionError,
|
||||
payload: BridgeResponsePayload::Empty,
|
||||
};
|
||||
} else {
|
||||
res = BridgeResponse {
|
||||
error: BridgeResponseError::NoError,
|
||||
payload: BridgeResponsePayload::Address(pub_key?),
|
||||
};
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let sub = client.subscribe(drk_pub_key, mint_address).await?;
|
||||
let res = BridgeResponse {
|
||||
error: BridgeResponseError::NoError,
|
||||
payload: BridgeResponsePayload::Watch(sub.secret_key, sub.public_key),
|
||||
};
|
||||
rep.send(res).await?;
|
||||
let sub = client.subscribe(drk_pub_key, mint_address).await;
|
||||
if sub.is_err() {
|
||||
error!(target: "BRIDGE", "{}", sub.unwrap_err().to_string());
|
||||
res = BridgeResponse {
|
||||
error: BridgeResponseError::BridgeWatchSubscribtionError,
|
||||
payload: BridgeResponsePayload::Empty,
|
||||
};
|
||||
} else {
|
||||
let sub = sub?;
|
||||
res = BridgeResponse {
|
||||
error: BridgeResponseError::NoError,
|
||||
payload: BridgeResponsePayload::Watch(sub.secret_key, sub.public_key),
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
BridgeRequestsPayload::Send(addr, amount) => {
|
||||
client.send(addr, mint_address, amount).await?;
|
||||
let res = BridgeResponse {
|
||||
error: BridgeResponseError::NoError,
|
||||
payload: BridgeResponsePayload::Send,
|
||||
};
|
||||
rep.send(res).await?;
|
||||
let result = client.send(addr, mint_address, amount).await;
|
||||
|
||||
if result.is_err() {
|
||||
error!(target: "BRIDGE", "{}", result.unwrap_err().to_string());
|
||||
res = BridgeResponse {
|
||||
error: BridgeResponseError::BridgeSendSubscribtionError,
|
||||
payload: BridgeResponsePayload::Empty,
|
||||
};
|
||||
} else {
|
||||
res = BridgeResponse {
|
||||
error: BridgeResponseError::NoError,
|
||||
payload: BridgeResponsePayload::Send,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rep.send(res).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user