From 1feeba26b50e12d0769ee7d8c56b7fc8ff264b57 Mon Sep 17 00:00:00 2001 From: Simon Wicky Date: Tue, 7 May 2024 13:49:13 +0200 Subject: [PATCH] adapt nym validator rewarder and sdk --- nym-validator-rewarder/Cargo.toml | 2 +- nym-validator-rewarder/src/error.rs | 6 +-- .../rewarder/credential_issuance/monitor.rs | 38 ++++++------------- .../src/rewarder/credential_issuance/types.rs | 13 ++++--- sdk/rust/nym-sdk/examples/bandwidth.rs | 6 +-- sdk/rust/nym-sdk/src/bandwidth.rs | 6 +-- sdk/rust/nym-sdk/src/bandwidth/client.rs | 13 ++++--- sdk/rust/nym-sdk/src/mixnet/client.rs | 14 ++++++- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/nym-validator-rewarder/Cargo.toml b/nym-validator-rewarder/Cargo.toml index 7100f87fb8..e81b9366da 100644 --- a/nym-validator-rewarder/Cargo.toml +++ b/nym-validator-rewarder/Cargo.toml @@ -33,7 +33,7 @@ humantime-serde.workspace = true # internal nym-bin-common = { path = "../common/bin-common", features = ["output_format"] } nym-config = { path = "../common/config" } -nym-coconut = { path = "../common/nymcoconut" } +nym-compact-ecash = { path = "../common/nym_offline_compact_ecash" } nym-crypto = { path = "../common/crypto", features = ["asymmetric"] } nym-credentials = { path = "../common/credentials" } nym-network-defaults = { path = "../common/network-defaults" } diff --git a/nym-validator-rewarder/src/error.rs b/nym-validator-rewarder/src/error.rs index 2c78e61fbb..651a713adf 100644 --- a/nym-validator-rewarder/src/error.rs +++ b/nym-validator-rewarder/src/error.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::config::RewardingRatios; -use nym_coconut::CoconutError; +use nym_compact_ecash::error::CompactEcashError; use nym_validator_client::nym_api::error::NymAPIError; use nym_validator_client::nyxd::error::NyxdError; use nym_validator_client::nyxd::tx::ErrorReport; @@ -112,14 +112,14 @@ pub enum NymRewarderError { MalformedCredentialCommitment { raw: String, #[source] - source: CoconutError, + source: CompactEcashError, }, #[error("the partial verification key for runner {runner} is malformed: {source}")] MalformedPartialVerificationKey { runner: String, #[source] - source: CoconutError, + source: CompactEcashError, }, #[error("could not verify the blinded credential")] diff --git a/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs b/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs index 6a02ca52a5..dc8ad56497 100644 --- a/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs +++ b/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs @@ -10,11 +10,10 @@ use crate::rewarder::helpers::api_client; use crate::rewarder::nyxd_client::NyxdClient; use bip39::rand::prelude::SliceRandom; use bip39::rand::thread_rng; -use nym_coconut::{ - hash_to_scalar, verify_partial_blind_signature, Base58, G1Projective, VerificationKey, -}; use nym_coconut_dkg_common::types::EpochId; -use nym_credentials::coconut::bandwidth::bandwidth_credential_params; +use nym_compact_ecash::scheme::withdrawal::verify_partial_blind_signature; +use nym_compact_ecash::{Attribute, Base58, G1Projective, VerificationKeyAuth}; +use nym_credentials::coconut::bandwidth::{bandwidth_credential_params, CredentialType}; use nym_task::TaskClient; use nym_validator_client::nym_api::{IssuedCredential, IssuedCredentialBody, NymApiClientExt}; use nym_validator_client::nyxd::Hash; @@ -93,29 +92,20 @@ impl CredentialIssuanceMonitor { // check if this deposit even exists let deposit_tx = issued_credential.credential.tx_hash; - let (deposit_value, deposit_info) = self + //not using value anymore, but it should still be there + let (_deposit_value, deposit_info) = self .nyxd_client .get_deposit_transaction_attributes(deposit_tx) .await?; trace!("deposit exists"); // check if the deposit values match - let credential_value = issued_credential.credential.public_attributes.first(); - let credential_info = issued_credential.credential.public_attributes.get(1); + let credential_info = CredentialType::TicketBook.to_string(); - if credential_value != Some(&deposit_value) { - return Err(NymRewarderError::InconsistentDepositValue { - tx_hash: deposit_tx, - request: credential_value.cloned(), - on_chain: deposit_value, - }); - } - trace!("credential values matches the deposit"); - - if credential_info != Some(&deposit_info) { + if credential_info != deposit_info { return Err(NymRewarderError::InconsistentDepositInfo { tx_hash: deposit_tx, - request: credential_info.cloned(), + request: Some(credential_info), on_chain: deposit_info, }); } @@ -125,14 +115,10 @@ impl CredentialIssuanceMonitor { fn verify_credential( &mut self, - vk: &VerificationKey, + vk: &VerificationKeyAuth, credential: IssuedCredential, ) -> Result<(), NymRewarderError> { - let public_attributes = credential - .public_attributes - .iter() - .map(hash_to_scalar) - .collect::>(); + let public_attributes = [Attribute::from(credential.expiration_date as u64)]; #[allow(clippy::map_identity)] let attributes_refs = public_attributes.iter().collect::>(); @@ -154,7 +140,7 @@ impl CredentialIssuanceMonitor { // actually do verify the credential now if !verify_partial_blind_signature( - bandwidth_credential_params(), + bandwidth_credential_params().grp(), &public_attribute_commitments, &attributes_refs, &credential.blinded_partial_credential, @@ -174,7 +160,7 @@ impl CredentialIssuanceMonitor { runner: String, credential_id: i64, issued_credential: IssuedCredentialBody, - vk: &VerificationKey, + vk: &VerificationKeyAuth, ) -> Result<(), NymRewarderError> { self.validate_credential_signature(&issued_credential)?; diff --git a/nym-validator-rewarder/src/rewarder/credential_issuance/types.rs b/nym-validator-rewarder/src/rewarder/credential_issuance/types.rs index 4b84870f70..b46a53846e 100644 --- a/nym-validator-rewarder/src/rewarder/credential_issuance/types.rs +++ b/nym-validator-rewarder/src/rewarder/credential_issuance/types.rs @@ -6,8 +6,9 @@ use crate::rewarder::epoch::Epoch; use crate::rewarder::helpers::api_client; use crate::rewarder::nyxd_client::NyxdClient; use cosmwasm_std::{Addr, Decimal, Uint128}; -use nym_coconut::{Base58, VerificationKey}; use nym_coconut_dkg_common::verification_key::ContractVKShare; +use nym_compact_ecash::Base58; +use nym_compact_ecash::VerificationKeyAuth; use nym_validator_client::nym_api::NymApiClientExt; use nym_validator_client::nyxd::{AccountId, Coin}; use std::collections::{HashMap, HashSet}; @@ -341,7 +342,7 @@ pub struct CredentialIssuer { // pub public_key: identity::PublicKey, pub operator_account: AccountId, pub api_runner: String, - pub verification_key: VerificationKey, + pub verification_key: VerificationKeyAuth, } impl TryFrom for CredentialIssuer { @@ -351,12 +352,12 @@ impl TryFrom for CredentialIssuer { Ok(CredentialIssuer { operator_account: addr_to_account_id(value.owner.clone()), api_runner: value.announce_address, - verification_key: VerificationKey::try_from_bs58(value.share).map_err(|source| { - NymRewarderError::MalformedPartialVerificationKey { + verification_key: VerificationKeyAuth::try_from_bs58(value.share).map_err( + |source| NymRewarderError::MalformedPartialVerificationKey { runner: value.owner.to_string(), source, - } - })?, + }, + )?, }) } } diff --git a/sdk/rust/nym-sdk/examples/bandwidth.rs b/sdk/rust/nym-sdk/examples/bandwidth.rs index 96a064cf1d..36858ae654 100644 --- a/sdk/rust/nym-sdk/examples/bandwidth.rs +++ b/sdk/rust/nym-sdk/examples/bandwidth.rs @@ -18,10 +18,10 @@ async fn main() -> anyhow::Result<()> { .enable_credentials_mode() .build()?; - let bandwidth_client = mixnet_client.create_bandwidth_client(mnemonic)?; + let bandwidth_client = mixnet_client.create_bandwidth_client(mnemonic).await?; - // Get a bandwidth credential worth 1000000 unym for the mixnet_client - bandwidth_client.acquire(1000000).await?; + // Get a bandwidth credential for the mixnet_client + bandwidth_client.acquire().await?; // Connect using paid bandwidth credential let mut client = mixnet_client.connect_to_mixnet().await?; diff --git a/sdk/rust/nym-sdk/src/bandwidth.rs b/sdk/rust/nym-sdk/src/bandwidth.rs index b1548dcb96..e2519cb273 100644 --- a/sdk/rust/nym-sdk/src/bandwidth.rs +++ b/sdk/rust/nym-sdk/src/bandwidth.rs @@ -15,10 +15,10 @@ //! .build() //! .unwrap(); //! -//! let bandwidth_client = mixnet_client.create_bandwidth_client(String::from("my super secret mnemonic")).unwrap(); +//! let bandwidth_client = mixnet_client.create_bandwidth_client(String::from("my super secret mnemonic")).await.unwrap(); //! -//! // Get a bandwidth credential worth 1000000 unym for the mixnet_client -//! bandwidth_client.acquire(1000000).await.unwrap(); +//! // Get a bandwidth credential for the mixnet_client +//! bandwidth_client.acquire().await.unwrap(); //! //! // Connect using paid bandwidth credential //! let mut client = mixnet_client.connect_to_mixnet().await.unwrap(); diff --git a/sdk/rust/nym-sdk/src/bandwidth/client.rs b/sdk/rust/nym-sdk/src/bandwidth/client.rs index 5051e5a1d4..a9f78f2ac4 100644 --- a/sdk/rust/nym-sdk/src/bandwidth/client.rs +++ b/sdk/rust/nym-sdk/src/bandwidth/client.rs @@ -6,7 +6,6 @@ use nym_bandwidth_controller::acquire::state::State; use nym_credential_storage::storage::Storage; use nym_credentials::coconut::bandwidth::IssuanceBandwidthCredential; use nym_network_defaults::NymNetworkDetails; -use nym_validator_client::nyxd::Coin; use nym_validator_client::{nyxd, DirectSigningHttpRpcNyxdClient}; /// The serialized version of the yet untransformed bandwidth voucher. It can be used to complete @@ -20,9 +19,9 @@ pub type VoucherBlob = Vec; /// [`crate::mixnet::DisconnectedMixnetClient::create_bandwidth_client`] on the associated mixnet /// client. pub struct BandwidthAcquireClient<'a, St: Storage> { - network_details: NymNetworkDetails, client: DirectSigningHttpRpcNyxdClient, storage: &'a St, + client_id: String, } impl<'a, St> BandwidthAcquireClient<'a, St> @@ -34,6 +33,7 @@ where network_details: NymNetworkDetails, mnemonic: String, storage: &'a St, + client_id: String, ) -> Result { let nyxd_url = network_details.endpoints[0].nyxd_url.as_str(); let config = nyxd::Config::try_from_nym_network_details(&network_details)?; @@ -44,9 +44,9 @@ where mnemonic.parse()?, )?; Ok(Self { - network_details, client, storage, + client_id, }) } @@ -54,9 +54,10 @@ where /// means the tokens have been deposited, but the proper bandwidth credential hasn't yet been /// created. A [`VoucherBlob`] is returned that can be used for a later recovery of the /// associated bandwidth credential, using [`Self::recover`]. - pub async fn acquire(&self, amount: u128) -> Result<()> { - let amount = Coin::new(amount, &self.network_details.chain_details.mix_denom.base); - let state = nym_bandwidth_controller::acquire::deposit(&self.client, amount).await?; + pub async fn acquire(&self) -> Result<()> { + let state = + nym_bandwidth_controller::acquire::deposit(&self.client, self.client_id.as_bytes()) + .await?; nym_bandwidth_controller::acquire::get_bandwidth_voucher(&state, &self.client, self.storage) .await .map_err(|reason| Error::UnconvertedDeposit { diff --git a/sdk/rust/nym-sdk/src/mixnet/client.rs b/sdk/rust/nym-sdk/src/mixnet/client.rs index cfcc6b3f45..c7a1e829aa 100644 --- a/sdk/rust/nym-sdk/src/mixnet/client.rs +++ b/sdk/rust/nym-sdk/src/mixnet/client.rs @@ -535,17 +535,29 @@ where /// Creates an associated [`BandwidthAcquireClient`] that can be used to acquire bandwidth /// credentials for this client to consume. - pub fn create_bandwidth_client( + pub async fn create_bandwidth_client( &self, mnemonic: String, ) -> Result> { if !self.config.enabled_credentials_mode { return Err(Error::DisabledCredentialsMode); } + let client_id = self + .storage + .key_store() + .load_keys() + .await + .map_err(|e| Error::KeyStorageError { + source: Box::new(e), + })? + .identity_keypair() + .private_key() + .to_base58_string(); BandwidthAcquireClient::new( self.config.network_details.clone(), mnemonic, self.storage.credential_store(), + client_id, ) }