bac0f24cf7
* split up coconut module a bit * internal tool for watching dkg state and updating group contract * debug dkg state * display past dealer data * improved EpochState Display impl * display contract errors + advance epoch state * check admin * panic handler * simplify app.rs * split action enum * added new tab with logger information * new dealing display * sort by index * [fixedup] wip: updating epoch issued credentials - OG 92ade10384a6d7b6c6c222d2e29d69d3b3446a4c * storing and signing partial blinded credentials * starting cleanup * fixed coconut tests + clippy * fixed nym-api tests * removed dkg-manager tool it was moved to a different branch * implemented remaining endpoints * unit tests + bug fixes * clippy * added persistent identity keys to nym-api theyre not yet announced - this will be in another PR * cargo fmt * clippy * fixed loading of old configs without storage paths set * added additional logs for blind-sign endpoint * fixed up licenses * lowercasing error variants * changed 'issued_credentials' to a post * added minimal client support * fixed the unit test
92 lines
2.8 KiB
Rust
92 lines
2.8 KiB
Rust
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use crate::error::BandwidthControllerError;
|
|
use nym_coconut_interface::Base58;
|
|
use nym_credential_storage::storage::Storage;
|
|
use nym_credentials::coconut::bandwidth::BandwidthVoucher;
|
|
use nym_credentials::coconut::utils::obtain_aggregate_signature;
|
|
use nym_crypto::asymmetric::{encryption, identity};
|
|
use nym_network_defaults::VOUCHER_INFO;
|
|
use nym_validator_client::coconut::all_coconut_api_clients;
|
|
use nym_validator_client::nyxd::contract_traits::CoconutBandwidthSigningClient;
|
|
use nym_validator_client::nyxd::contract_traits::DkgQueryClient;
|
|
use nym_validator_client::nyxd::Coin;
|
|
use rand::rngs::OsRng;
|
|
use state::State;
|
|
|
|
pub mod state;
|
|
|
|
pub async fn deposit<C>(client: &C, amount: Coin) -> Result<State, BandwidthControllerError>
|
|
where
|
|
C: CoconutBandwidthSigningClient + Sync,
|
|
{
|
|
let mut rng = OsRng;
|
|
let signing_key = identity::PrivateKey::new(&mut rng);
|
|
let encryption_key = encryption::PrivateKey::new(&mut rng);
|
|
let params = BandwidthVoucher::default_parameters();
|
|
let voucher_value = amount.amount.to_string();
|
|
|
|
let tx_hash = client
|
|
.deposit(
|
|
amount,
|
|
String::from(VOUCHER_INFO),
|
|
signing_key.public_key().to_base58_string(),
|
|
encryption_key.public_key().to_base58_string(),
|
|
None,
|
|
)
|
|
.await?
|
|
.transaction_hash;
|
|
|
|
let voucher = BandwidthVoucher::new(
|
|
¶ms,
|
|
voucher_value,
|
|
VOUCHER_INFO.to_string(),
|
|
tx_hash,
|
|
signing_key,
|
|
encryption_key,
|
|
);
|
|
|
|
let state = State { voucher, params };
|
|
|
|
Ok(state)
|
|
}
|
|
|
|
pub async fn get_credential<C, St>(
|
|
state: &State,
|
|
client: &C,
|
|
storage: &St,
|
|
) -> Result<(), BandwidthControllerError>
|
|
where
|
|
C: DkgQueryClient + Send + Sync,
|
|
St: Storage,
|
|
<St as Storage>::StorageError: Send + Sync + 'static,
|
|
{
|
|
let epoch_id = client.get_current_epoch().await?.epoch_id;
|
|
let threshold = client
|
|
.get_current_epoch_threshold()
|
|
.await?
|
|
.ok_or(BandwidthControllerError::NoThreshold)?;
|
|
|
|
let coconut_api_clients = all_coconut_api_clients(client, epoch_id).await?;
|
|
|
|
let signature = obtain_aggregate_signature(
|
|
&state.params,
|
|
&state.voucher,
|
|
&coconut_api_clients,
|
|
threshold,
|
|
)
|
|
.await?;
|
|
storage
|
|
.insert_coconut_credential(
|
|
state.voucher.get_voucher_value(),
|
|
VOUCHER_INFO.to_string(),
|
|
state.voucher.get_private_attributes()[0].to_bs58(),
|
|
state.voucher.get_private_attributes()[1].to_bs58(),
|
|
signature.to_bs58(),
|
|
epoch_id.to_string(),
|
|
)
|
|
.await
|
|
.map_err(|err| BandwidthControllerError::CredentialStorageError(Box::new(err)))
|
|
}
|