Return useful info from bond/unbond

This commit is contained in:
Drazen Urch
2021-09-13 13:07:57 +02:00
parent 005dd7513b
commit a601c28a20
8 changed files with 104 additions and 86 deletions
@@ -507,14 +507,14 @@ impl<C> NymdClient<C> {
/// Removes stake delegation from a particular mixnode.
pub async fn remove_mixnode_delegation(
&self,
mix_identity: IdentityKey,
mix_identity: &str,
) -> Result<ExecuteResult, NymdError>
where
C: SigningCosmWasmClient + Sync,
{
let fee = self.get_fee(Operation::UndelegateFromMixnode);
let req = ExecuteMsg::UndelegateFromMixnode { mix_identity };
let req = ExecuteMsg::UndelegateFromMixnode { mix_identity: mix_identity.to_string() };
self.client
.execute(
self.address(),
@@ -574,15 +574,15 @@ impl<C> NymdClient<C> {
/// Delegates specified amount of stake to particular gateway.
pub async fn delegate_to_gateway(
&self,
gateway_identity: IdentityKey,
amount: Coin,
gateway_identity: &str,
amount: &Coin,
) -> Result<ExecuteResult, NymdError>
where
C: SigningCosmWasmClient + Sync,
{
let fee = self.get_fee(Operation::DelegateToGateway);
let req = ExecuteMsg::DelegateToGateway { gateway_identity };
let req = ExecuteMsg::DelegateToGateway { gateway_identity: gateway_identity.to_string() };
self.client
.execute(
self.address(),
@@ -590,7 +590,7 @@ impl<C> NymdClient<C> {
&req,
fee,
"Delegating to gateway from rust!",
vec![cosmwasm_coin_to_cosmos_coin(amount)],
vec![cosmwasm_coin_ptr_to_cosmos_coin(amount)],
)
.await
}
@@ -598,14 +598,14 @@ impl<C> NymdClient<C> {
/// Removes stake delegation from a particular gateway.
pub async fn remove_gateway_delegation(
&self,
gateway_identity: IdentityKey,
gateway_identity: &str,
) -> Result<ExecuteResult, NymdError>
where
C: SigningCosmWasmClient + Sync,
{
let fee = self.get_fee(Operation::UndelegateFromGateway);
let req = ExecuteMsg::UndelegateFromGateway { gateway_identity };
let req = ExecuteMsg::UndelegateFromGateway { gateway_identity: gateway_identity.to_string() };
self.client
.execute(
self.address(),
+54 -57
View File
@@ -1,13 +1,10 @@
use crate::state::State;
use coconut_interface::{self, Credential, Signature, Theta, VerificationKey};
use credentials::{obtain_aggregate_signature, obtain_aggregate_verification_key};
use std::sync::Arc;
use tokio::sync::RwLock;
use coconut_interface::{
self, Credential, Signature, Theta, VerificationKey,
};
use crate::state::State;
use credentials::{obtain_aggregate_signature, obtain_aggregate_verification_key};
use url::Url;
#[tauri::command]
pub async fn randomise_credential(
idx: usize,
@@ -65,61 +62,61 @@ pub async fn verify_credential(
}
async fn prove_credential(
idx: usize,
validator_urls: Vec<String>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Theta, String> {
let verification_key = get_aggregated_verification_key(validator_urls, state.clone()).await?;
let state = state.read().await;
if let Some(signature) = state.signatures.get(idx) {
match coconut_interface::prove_credential(
state.params()?,
&verification_key,
signature,
&state.private_attributes(),
) {
Ok(theta) => Ok(theta),
Err(e) => Err(format!("{}", e)),
}
} else {
Err("Got invalid Signature idx".to_string())
idx: usize,
validator_urls: Vec<String>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Theta, String> {
let verification_key = get_aggregated_verification_key(validator_urls, state.clone()).await?;
let state = state.read().await;
if let Some(signature) = state.signatures.get(idx) {
match coconut_interface::prove_credential(
state.params()?,
&verification_key,
signature,
&state.private_attributes(),
) {
Ok(theta) => Ok(theta),
Err(e) => Err(format!("{}", e)),
}
} else {
Err("Got invalid Signature idx".to_string())
}
}
async fn get_aggregated_verification_key(
validator_urls: Vec<String>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<VerificationKey, String> {
if let Some(verification_key) = &state.read().await.aggregated_verification_key {
return Ok(verification_key.clone());
}
async fn get_aggregated_verification_key(
validator_urls: Vec<String>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<VerificationKey, String> {
if let Some(verification_key) = &state.read().await.aggregated_verification_key {
return Ok(verification_key.clone());
}
let parsed_urls = parse_url_validators(&validator_urls)?;
let key = obtain_aggregate_verification_key(&parsed_urls)
.await
.map_err(|err| format!("failed to obtain aggregate verification key - {:?}", err))?;
state
.write()
.await
.aggregated_verification_key
.replace(key.clone());
Ok(key)
}
let parsed_urls = parse_url_validators(&validator_urls)?;
let key = obtain_aggregate_verification_key(&parsed_urls)
.await
.map_err(|err| format!("failed to obtain aggregate verification key - {:?}", err))?;
fn parse_url_validators(raw: &[String]) -> Result<Vec<Url>, String> {
let mut parsed_urls = Vec::with_capacity(raw.len());
for url in raw {
let parsed_url: Url = url
.parse()
.map_err(|err| format!("one of validator urls is malformed - {}", err))?;
parsed_urls.push(parsed_url)
}
Ok(parsed_urls)
state
.write()
.await
.aggregated_verification_key
.replace(key.clone());
Ok(key)
}
fn parse_url_validators(raw: &[String]) -> Result<Vec<Url>, String> {
let mut parsed_urls = Vec::with_capacity(raw.len());
for url in raw {
let parsed_url: Url = url
.parse()
.map_err(|err| format!("one of validator urls is malformed - {}", err))?;
parsed_urls.push(parsed_url)
}
Ok(parsed_urls)
}
#[tauri::command]
pub async fn get_credential(
validator_urls: Vec<String>,
@@ -140,4 +137,4 @@ pub async fn get_credential(
let mut state = state.write().await;
state.signatures.push(signature);
Ok(state.signatures.clone())
}
}
+2 -2
View File
@@ -123,7 +123,7 @@ impl Coin {
pub fn new(amount: &str, denom: &Denom) -> Coin {
Coin {
amount: amount.to_string(),
denom: denom.clone()
denom: denom.clone(),
}
}
}
@@ -170,7 +170,7 @@ impl From<CosmWasmCoin> for Coin {
fn from(c: CosmWasmCoin) -> Coin {
Coin {
amount: c.amount.to_string(),
denom: Denom::from_str(&c.denom.to_string()).unwrap(),
denom: Denom::from_str(&c.denom).unwrap(),
}
}
}
+28 -14
View File
@@ -205,7 +205,7 @@ async fn bond_mixnode(
#[tauri::command]
async fn delegate_to_mixnode(
identity: String,
identity: &str,
amount: Coin,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<DelegationResult, String> {
@@ -215,10 +215,10 @@ async fn delegate_to_mixnode(
Err(e) => return Err(format_err!(e)),
};
let client = r_state.client()?;
match client.delegate_to_mixnode(&identity, &bond).await {
match client.delegate_to_mixnode(identity, &bond).await {
Ok(_result) => Ok(DelegationResult {
source_address: client.address().to_string(),
target_address: identity,
target_address: identity.to_string(),
amount: Some(bond.into()),
}),
Err(e) => Err(format_err!(e)),
@@ -227,44 +227,56 @@ async fn delegate_to_mixnode(
#[tauri::command]
async fn undelegate_from_mixnode(
identity: String,
identity: &str,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<(), String> {
) -> Result<DelegationResult, String> {
let r_state = state.read().await;
let client = r_state.client()?;
match client.remove_mixnode_delegation(identity).await {
Ok(_result) => Ok(()),
Ok(_result) => Ok(DelegationResult {
source_address: client.address().to_string(),
target_address: identity.to_string(),
amount: None,
}),
Err(e) => Err(format_err!(e)),
}
}
#[tauri::command]
async fn delegate_to_gateway(
identity: String,
identity: &str,
amount: Coin,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<(), String> {
) -> Result<DelegationResult, String> {
let r_state = state.read().await;
let bond: CosmWasmCoin = match amount.try_into() {
Ok(b) => b,
Err(e) => return Err(format_err!(e)),
};
let client = r_state.client()?;
match client.delegate_to_gateway(identity, bond).await {
Ok(_result) => Ok(()),
match client.delegate_to_gateway(identity, &bond).await {
Ok(_result) => Ok(DelegationResult {
source_address: client.address().to_string(),
target_address: identity.to_string(),
amount: Some(bond.into()),
}),
Err(e) => Err(format_err!(e)),
}
}
#[tauri::command]
async fn undelegate_from_gateway(
identity: String,
identity: &str,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<(), String> {
) -> Result<DelegationResult, String> {
let r_state = state.read().await;
let client = r_state.client()?;
match client.remove_gateway_delegation(identity).await {
Ok(_result) => Ok(()),
Ok(_result) => Ok(DelegationResult {
source_address: client.address().to_string(),
target_address: identity.to_string(),
amount: None,
}),
Err(e) => Err(format_err!(e)),
}
}
@@ -407,5 +419,7 @@ export! {
Gateway => "../src/types/rust/gateway.ts",
TauriTxResult => "../src/types/rust/tauritxresult.ts",
TransactionDetails => "../src/types/rust/transactiondetails.ts",
Operation => "../src/types/rust/operation.ts"
Operation => "../src/types/rust/operation.ts",
Denom => "../src/types/rust/denom.ts",
DelegationResult => "../src/types/rust/delegationresult.ts"
}
+1 -4
View File
@@ -1,8 +1,6 @@
use crate::config::Config;
use crate::format_err;
use coconut_interface::{
self, Attribute, Parameters, Signature, VerificationKey,
};
use coconut_interface::{self, Attribute, Parameters, Signature, VerificationKey};
use validator_client::nymd::{NymdClient, SigningNymdClient};
#[derive(Default)]
@@ -51,5 +49,4 @@ impl State {
pub fn n_attributes(&self) -> u32 {
self.n_attributes
}
}
+3 -1
View File
@@ -1,4 +1,6 @@
import { Denom } from "./denom";
export interface Coin {
amount: string;
denom: string;
denom: Denom;
}
@@ -0,0 +1,7 @@
import { Coin } from "./coin";
export interface DelegationResult {
source_address: string;
target_address: string;
amount: Coin | null;
}
+1
View File
@@ -0,0 +1 @@
export type Denom = "Major" | "Minor";