From 56a8b82c4da59df9fabb42d59c1484401c7b533e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C8=99u?= Date: Tue, 1 Mar 2022 14:52:34 +0200 Subject: [PATCH] Conditional signature --- Cargo.lock | 4 +- clients/tauri-client/src-tauri/src/main.rs | 2 +- common/bandwidth-claim-contract/src/events.rs | 2 +- .../validator-client/src/nymd/mod.rs | 8 +++ validator-api/Cargo.toml | 2 + validator-api/src/coconut/mod.rs | 52 +++++++++++++++++++ 6 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5649592080..6b210d38a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3987,6 +3987,8 @@ version = "0.12.0" dependencies = [ "anyhow", "attohttpc 0.18.0", + "bandwidth-claim-contract", + "bip39", "cfg-if 1.0.0", "clap 2.33.3", "coconut-interface", @@ -8247,4 +8249,4 @@ checksum = "615120c7a2431d16cf1cf979e7fc31ba7a5b5e5707b29c8a99e5dbf8a8392a33" dependencies = [ "cc", "libc", -] \ No newline at end of file +] diff --git a/clients/tauri-client/src-tauri/src/main.rs b/clients/tauri-client/src-tauri/src/main.rs index 0c0fd8a3bf..de1168e79c 100644 --- a/clients/tauri-client/src-tauri/src/main.rs +++ b/clients/tauri-client/src-tauri/src/main.rs @@ -196,7 +196,7 @@ fn main() { let bandwidth_credential_attributes = BandwidthVoucherAttributes { serial_number: params.random_scalar(), binding_number: params.random_scalar(), - voucher_value: hash_to_scalar(1024u64.to_be_bytes()), + voucher_value: Attribute::from(1000000u64), voucher_info: hash_to_scalar("BandwidthVoucher"), }; tauri::Builder::default() diff --git a/common/bandwidth-claim-contract/src/events.rs b/common/bandwidth-claim-contract/src/events.rs index 0925d53dc2..dea478b051 100644 --- a/common/bandwidth-claim-contract/src/events.rs +++ b/common/bandwidth-claim-contract/src/events.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // event types -pub const VOUCHER_ACQUIRED_EVENT_TYPE: &str = "coconut-acquired"; +pub const VOUCHER_ACQUIRED_EVENT_TYPE: &str = "coconut_acquired"; // attributes that are used in multiple places pub const VOUCHER_VALUE: &str = "value"; diff --git a/common/client-libs/validator-client/src/nymd/mod.rs b/common/client-libs/validator-client/src/nymd/mod.rs index 9ae0fd863e..6e48487a3e 100644 --- a/common/client-libs/validator-client/src/nymd/mod.rs +++ b/common/client-libs/validator-client/src/nymd/mod.rs @@ -46,6 +46,7 @@ pub mod error; pub mod fee; pub mod traits; pub mod wallet; +use cosmrs::rpc::endpoint::tx::Response as TxResponse; #[derive(Debug)] pub struct NymdClient { @@ -274,6 +275,13 @@ impl NymdClient { self.client.get_balance(address, denom).await } + pub async fn get_tx(&self, id: tx::Hash) -> Result + where + C: CosmWasmClient + Sync, + { + self.client.get_tx(id).await + } + pub async fn get_total_supply(&self) -> Result, NymdError> where C: CosmWasmClient + Sync, diff --git a/validator-api/Cargo.toml b/validator-api/Cargo.toml index ad55ccfc28..f1265a0412 100644 --- a/validator-api/Cargo.toml +++ b/validator-api/Cargo.toml @@ -15,6 +15,7 @@ rust-version = "1.56" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bip39 = "1.0.1" clap = "2.33.0" dirs = "3.0" dotenv = "0.15.0" @@ -47,6 +48,7 @@ config = { path = "../common/config" } crypto = { path="../common/crypto" } gateway-client = { path="../common/client-libs/gateway-client" } mixnet-contract-common = { path= "../common/cosmwasm-smart-contracts/mixnet-contract" } +bandwidth-claim-contract = { path= "../common/bandwidth-claim-contract" } nymsphinx = { path="../common/nymsphinx" } topology = { path="../common/topology" } validator-api-requests = { path = "validator-api-requests" } diff --git a/validator-api/src/coconut/mod.rs b/validator-api/src/coconut/mod.rs index cd50c7cdf8..1532d738b4 100644 --- a/validator-api/src/coconut/mod.rs +++ b/validator-api/src/coconut/mod.rs @@ -1,6 +1,8 @@ // Copyright 2021 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use bandwidth_claim_contract::events::{VOUCHER_ACQUIRED_EVENT_TYPE, VOUCHER_VALUE}; +use bip39::Mnemonic; use coconut_interface::{ elgamal::PublicKey, Attribute, BlindSignRequest, BlindSignRequestBody, BlindedSignature, BlindedSignatureResponse, KeyPair, Parameters, VerificationKeyResponse, @@ -10,6 +12,10 @@ use getset::{CopyGetters, Getters}; use rocket::fairing::AdHoc; use rocket::serde::json::Json; use rocket::State; +use std::str::FromStr; +use url::Url; +use validator_client::nymd::tx::Hash; +use validator_client::nymd::{AccountId, NymdClient}; #[derive(Getters, CopyGetters, Debug)] pub(crate) struct InternalSignRequest { @@ -69,6 +75,52 @@ pub async fn post_blind_sign( key_pair: &State, ) -> Json { debug!("{:?}", blind_sign_request_body); + let nymd_url = Url::from_str("http://127.0.0.1:26657").unwrap(); + let mnemonic = Mnemonic::from_str(&"have armor behind appear labor choose fire erase arrive slice mother acid second rely exhibit grief soul super record useless antique excite ocean walnut").unwrap(); + let nymd_client = NymdClient::connect_with_mnemonic( + config::defaults::all::Network::SANDBOX, + nymd_url.as_ref(), + None, + None, + AccountId::from_str("").ok(), + mnemonic, + None, + ) + .expect("Could not create nymd client"); + let response = nymd_client + .get_tx( + Hash::from_str("7CFAC90461CE017C9D8F987CF15FD3297A32E82B882B14A2A706F2B368355A12") + .unwrap(), + ) + .await + .unwrap(); + println!("Events: {:?}", response.tx_result.events); + let bandwidth_str = response + .tx_result + .events + .iter() + .filter(|event| event.type_str == format!("wasm-{}", VOUCHER_ACQUIRED_EVENT_TYPE)) + .map(|event| { + event + .attributes + .iter() + .filter(|tag| tag.key.as_ref() == VOUCHER_VALUE) + .last() + .unwrap() + .value + .as_ref() + }) + .last() + .unwrap(); + println!("Bandwidth str: {}", bandwidth_str); + let acuired_bandwidth = Attribute::from(u64::from_str(bandwidth_str).unwrap()); + let requested_bandwidth = blind_sign_request_body.0.public_attributes()[0]; + if acuired_bandwidth != requested_bandwidth { + panic!( + "Bandwidth value mismatch: {} vs {}", + acuired_bandwidth, requested_bandwidth + ); + } let internal_request = InternalSignRequest::new( *blind_sign_request_body.total_params(), blind_sign_request_body.public_attributes(),