From 704b3241ee80c4290f6f70cca59fd39cdf3d4014 Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 3 Sep 2021 06:58:57 +0200 Subject: [PATCH 1/4] Gateway bonding --- common/mixnet-contract/src/gateway.rs | 3 +- tauri-wallet/src-tauri/src/main.rs | 47 +++++++++++++++++-- .../src/routes/internal-docs/ApiList.tsx | 27 +++++++++++ tauri-wallet/src/types/rust/gateway.ts | 9 ++++ 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 tauri-wallet/src/types/rust/gateway.ts diff --git a/common/mixnet-contract/src/gateway.rs b/common/mixnet-contract/src/gateway.rs index 7b9c816b4a..eb63fb4dae 100644 --- a/common/mixnet-contract/src/gateway.rs +++ b/common/mixnet-contract/src/gateway.rs @@ -6,8 +6,9 @@ use cosmwasm_std::{coin, Addr, Coin}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::fmt::Display; +use ts_rs::TS; -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)] +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema, TS)] pub struct Gateway { pub host: String, pub mix_port: u16, diff --git a/tauri-wallet/src-tauri/src/main.rs b/tauri-wallet/src-tauri/src/main.rs index 4a17e72b3a..e1bdd12199 100644 --- a/tauri-wallet/src-tauri/src/main.rs +++ b/tauri-wallet/src-tauri/src/main.rs @@ -10,7 +10,7 @@ use cosmos_sdk::Denom as CosmosDenom; use cosmos_sdk::{AccountId, Decimal}; use cosmwasm_std::Coin as CosmWasmCoin; use error::BackendError; -use mixnet_contract::MixNode; +use mixnet_contract::{Gateway, MixNode}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; @@ -310,6 +310,44 @@ async fn bond_mixnode( } } +#[tauri::command] +async fn bond_gateway( + gateway: Gateway, + bond: Coin, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + let bond: CosmWasmCoin = match bond.try_into() { + Ok(b) => b, + Err(e) => return Err(format_err!(e)), + }; + if let Some(client) = &r_state.signing_client { + match client.bond_gateway(gateway, bond).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn unbond_gateway(state: tauri::State<'_, Arc>>) -> Result<(), String> { + let r_state = state.read().await; + if let Some(client) = &r_state.signing_client { + match client.unbond_gateway().await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + fn _connect_with_mnemonic(mnemonic: Mnemonic, config: &Config) -> NymdClient { match NymdClient::connect_with_mnemonic( config.get_nymd_validator_url().unwrap(), @@ -332,7 +370,9 @@ fn main() { owns_gateway, owns_mixnode, bond_mixnode, - unbond_mixnode + unbond_mixnode, + bond_gateway, + unbond_gateway, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); @@ -341,5 +381,6 @@ fn main() { export! { MixNode => "../src/types/rust/mixnode.ts", Coin => "../src/types/rust/coin.ts", - Balance => "../src/types/rust/balance.ts" + Balance => "../src/types/rust/balance.ts", + Gateway => "../src/types/rust/gateway.ts" } diff --git a/tauri-wallet/src/routes/internal-docs/ApiList.tsx b/tauri-wallet/src/routes/internal-docs/ApiList.tsx index 22c09d2659..0dda972472 100644 --- a/tauri-wallet/src/routes/internal-docs/ApiList.tsx +++ b/tauri-wallet/src/routes/internal-docs/ApiList.tsx @@ -74,6 +74,33 @@ export const ApiList = () => { }} /> + + + + + + + + + ); }; diff --git a/tauri-wallet/src/types/rust/gateway.ts b/tauri-wallet/src/types/rust/gateway.ts new file mode 100644 index 0000000000..98a81a9914 --- /dev/null +++ b/tauri-wallet/src/types/rust/gateway.ts @@ -0,0 +1,9 @@ +export interface Gateway { + host: string; + mix_port: number; + clients_port: number; + location: string; + sphinx_key: string; + identity_key: string; + version: string; +} \ No newline at end of file From 01a430588340e12b4e8371a803294fddea97dd29 Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 3 Sep 2021 07:33:11 +0200 Subject: [PATCH 2/4] Delegation --- tauri-wallet/src-tauri/src/main.rs | 86 ++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tauri-wallet/src-tauri/src/main.rs b/tauri-wallet/src-tauri/src/main.rs index e1bdd12199..558ec2bd19 100644 --- a/tauri-wallet/src-tauri/src/main.rs +++ b/tauri-wallet/src-tauri/src/main.rs @@ -310,6 +310,88 @@ async fn bond_mixnode( } } +#[tauri::command] +async fn delegate_to_mixnode( + identity: String, + amount: Coin, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + let bond: CosmWasmCoin = match amount.try_into() { + Ok(b) => b, + Err(e) => return Err(format_err!(e)), + }; + if let Some(client) = &r_state.signing_client { + match client.delegate_to_mixnode(identity, bond).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn undelegate_from_mixnode( + identity: String, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + if let Some(client) = &r_state.signing_client { + match client.remove_mixnode_delegation(identity).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn delegate_to_gateway( + identity: String, + amount: Coin, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + let bond: CosmWasmCoin = match amount.try_into() { + Ok(b) => b, + Err(e) => return Err(format_err!(e)), + }; + if let Some(client) = &r_state.signing_client { + match client.delegate_to_gateway(identity, bond).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn undelegate_from_gateway( + identity: String, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + if let Some(client) = &r_state.signing_client { + match client.remove_gateway_delegation(identity).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + #[tauri::command] async fn bond_gateway( gateway: Gateway, @@ -373,6 +455,10 @@ fn main() { unbond_mixnode, bond_gateway, unbond_gateway, + delegate_to_mixnode, + undelegate_from_mixnode, + delegate_to_gateway, + undelegate_from_gateway ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); From dbf82da9b60a4633797ba56b35f7aaf442945e3b Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 3 Sep 2021 07:36:02 +0200 Subject: [PATCH 3/4] Delegation docs --- .../src/routes/internal-docs/ApiList.tsx | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tauri-wallet/src/routes/internal-docs/ApiList.tsx b/tauri-wallet/src/routes/internal-docs/ApiList.tsx index 0dda972472..389711c6d0 100644 --- a/tauri-wallet/src/routes/internal-docs/ApiList.tsx +++ b/tauri-wallet/src/routes/internal-docs/ApiList.tsx @@ -101,6 +101,44 @@ export const ApiList = () => { }} /> + + + + + + + + + + + + ); }; From 87a0b05d1adc8ed59dbaf41b7a8e5c64327c5a6f Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 3 Sep 2021 07:43:29 +0200 Subject: [PATCH 4/4] Send --- tauri-wallet/src-tauri/src/main.rs | 26 ++++++++++++++++++- .../src/routes/internal-docs/ApiList.tsx | 12 +++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tauri-wallet/src-tauri/src/main.rs b/tauri-wallet/src-tauri/src/main.rs index 558ec2bd19..3f1ec6ac94 100644 --- a/tauri-wallet/src-tauri/src/main.rs +++ b/tauri-wallet/src-tauri/src/main.rs @@ -430,6 +430,29 @@ async fn unbond_gateway(state: tauri::State<'_, Arc>>) -> Result<( } } +#[tauri::command] +async fn send(address: &str, amount: Coin, memo: String, state: tauri::State<'_, Arc>>,) -> Result<(), String> { + let address = match AccountId::from_str(address) { + Ok(addy) => addy, + Err(e) => return Err(format_err!(e)) + }; + let amount: CosmosCoin = match amount.try_into() { + Ok(b) => b, + Err(e) => return Err(format_err!(e)), + }; + let r_state = state.read().await; + if let Some(client) = &r_state.signing_client { + match client.send(&address, vec!(amount), memo).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + fn _connect_with_mnemonic(mnemonic: Mnemonic, config: &Config) -> NymdClient { match NymdClient::connect_with_mnemonic( config.get_nymd_validator_url().unwrap(), @@ -458,7 +481,8 @@ fn main() { delegate_to_mixnode, undelegate_from_mixnode, delegate_to_gateway, - undelegate_from_gateway + undelegate_from_gateway, + send ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/tauri-wallet/src/routes/internal-docs/ApiList.tsx b/tauri-wallet/src/routes/internal-docs/ApiList.tsx index 389711c6d0..9075bcf267 100644 --- a/tauri-wallet/src/routes/internal-docs/ApiList.tsx +++ b/tauri-wallet/src/routes/internal-docs/ApiList.tsx @@ -139,6 +139,18 @@ export const ApiList = () => { }} /> + + + ); };