adapt nym validator rewarder and sdk

This commit is contained in:
Simon Wicky
2024-05-07 13:49:13 +02:00
parent c76c8e5b4c
commit 1feeba26b5
8 changed files with 49 additions and 49 deletions
+1 -1
View File
@@ -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" }
+3 -3
View File
@@ -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")]
@@ -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::<Vec<_>>();
let public_attributes = [Attribute::from(credential.expiration_date as u64)];
#[allow(clippy::map_identity)]
let attributes_refs = public_attributes.iter().collect::<Vec<_>>();
@@ -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)?;
@@ -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<ContractVKShare> for CredentialIssuer {
@@ -351,12 +352,12 @@ impl TryFrom<ContractVKShare> 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,
}
})?,
},
)?,
})
}
}
+3 -3
View File
@@ -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?;
+3 -3
View File
@@ -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();
+7 -6
View File
@@ -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<u8>;
/// [`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<Self> {
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 {
+13 -1
View File
@@ -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<BandwidthAcquireClient<S::CredentialStore>> {
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,
)
}