Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
octol committed Nov 4, 2024
1 parent 5afc8eb commit ec05e95
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum AccountCommand {
RegisterDevice,
RequestZkNym,
GetDeviceZkNym,
ImportZkNym(String),
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -120,6 +121,9 @@ impl CommandHandler {
AccountCommand::GetDeviceZkNym => {
todo!()
}
AccountCommand::ImportZkNym(_) => {
todo!()
}
}
.inspect(|_result| {
tracing::info!("Command {:?} with id {} completed", self.command, self.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pub(crate) async fn poll_zk_nym(
.await
{
Ok(poll_response) if poll_response.status != NymVpnZkNymStatus::Pending => {
tracing::info!("zk-nym polling finished: {:#?}", poll_response);
tracing::info!("zk-nym polling finished: {}", poll_response.id);
tracing::debug!("zk-nym polling finished: {:#?}", poll_response);
return PollingResult::Finished(
poll_response,
request.ticketbook_type,
Expand Down Expand Up @@ -132,31 +133,40 @@ pub(crate) async fn unblind_and_aggregate(
.create_ecash_keypair()
.map_err(Error::CreateEcashKeyPair)?;

tracing::info!("Verifying zk-nym shares");

let mut partial_wallets = Vec::new();
let blinded_shares = response.blinded_shares.unwrap();
for share in blinded_shares.shares {
// TODO: remove unwrap
// let blinded_share: WalletShare = serde_json::from_str(&share).unwrap();

// TODO: remove unwrap
tracing::info!("Creating BlindedSignature");
let blinded_sig =
BlindedSignature::try_from_bs58(&share.bs58_encoded_share).unwrap();

tracing::info!("Calling issue_verify");
match nym_compact_ecash::issue_verify(
&vk_auth,
ecash_keypair.secret_key(),
&blinded_sig,
&request_info,
share.node_index,
) {
Ok(partial_wallet) => partial_wallets.push(partial_wallet),
Ok(partial_wallet) => {
tracing::info!("Partial wallet created and appended");
partial_wallets.push(partial_wallet)
},
Err(err) => {
tracing::error!("Failed to issue verify: {:#?}", err);
return Err(Error::ImportZkNym(err));
}
}
}

tracing::info!("Aggregating wallets");

// TODO: remove unwrap
let aggregated_wallets = nym_compact_ecash::aggregate_wallets(
&vk_auth,
Expand Down
91 changes: 64 additions & 27 deletions nym-vpn-core/crates/nym-vpn-account-controller/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,38 +190,57 @@ where
ticketbook_type: TicketType,
request_info: RequestInfo,
) -> Result<(), Error> {
tracing::info!("Importing zk-nym: {:#?}", response);
tracing::info!("Importing zk-nym: {}", response.id);
// tracing::debug!("Importing zk-nym: {:#?}", response);

let account = self.account_storage.load_account().await?;

let Some(ref blinded_shares) = response.blinded_shares else {
return Err(Error::MissingBlindedShares);
};

let master_verification_key = match self
.credential_storage
.get_master_verification_key(blinded_shares.epoch_id)
.await?
{
Some(master_verification_key) => master_verification_key.clone(),
None => {
let vk_bs58 = blinded_shares
.master_verification_key
.as_ref()
.unwrap()
.bs58_encoded_key
.clone();
let vk = VerificationKeyAuth::try_from_bs58(&vk_bs58).unwrap();
let epoch_verification_key = EpochVerificationKey {
epoch_id: blinded_shares.epoch_id,
key: vk.clone(),
};
self.credential_storage
.insert_master_verification_key(&epoch_verification_key)
.await?;
vk
}
};
tracing::info!(
"Getting master verification key for epoch: {}",
blinded_shares.epoch_id
);

//let master_verification_key = match self
// .credential_storage
// .get_master_verification_key(blinded_shares.epoch_id)
// .await?
//{
// Some(master_verification_key) => {
// tracing::info!("Master verification key found in local storage");
// master_verification_key.clone()
// }
// None => {
// tracing::info!("Master verification key not found in local storage, importing");
// let vk_bs58 = blinded_shares
// .master_verification_key
// .as_ref()
// .unwrap()
// .bs58_encoded_key
// .clone();
// let vk = VerificationKeyAuth::try_from_bs58(&vk_bs58).unwrap();
// let epoch_verification_key = EpochVerificationKey {
// epoch_id: blinded_shares.epoch_id,
// key: vk.clone(),
// };
// tracing::info!("Inserting master verification key into local storage");
// self.credential_storage
// .insert_master_verification_key(&epoch_verification_key)
// .await?;
// vk
// }
//};

let vk_bs58 = blinded_shares
.master_verification_key
.as_ref()
.unwrap()
.bs58_encoded_key
.clone();
let master_verification_key = VerificationKeyAuth::try_from_bs58(&vk_bs58).unwrap();

let issued_ticketbook = crate::commands::zknym::unblind_and_aggregate(
response,
Expand All @@ -230,8 +249,7 @@ where
account,
master_verification_key,
)
.await
.unwrap();
.await?;

self.credential_storage
.insert_issued_ticketbook(&issued_ticketbook)
Expand Down Expand Up @@ -390,9 +408,27 @@ where
for zk_nym in &reported_device_zk_nyms.items {
tracing::info!("{:?}", zk_nym);
}

Ok(())
}

async fn handle_import_zk_nym(&mut self, id: String) -> Result<(), Error> {
tracing::info!("Importing zk-nym with id: {}", id);

let account = self.account_storage.load_account().await?;
let device = self.account_storage.load_device_keys().await?;

todo!();
//let response = self
// .vpn_api_client
// .get_zk_nym_by_id(&account, &device, &id)
// .await
// .map_err(Error::GetZkNym)?;

// self.import_zk_nym(response, TicketType::V1MixnetEntry, RequestInfo::default())
// .await
}

// Once we finish polling the result of the zk-nym request, we now import the zk-nym into the
// local credential store
async fn handle_polling_result(&mut self, result: Result<PollingResult, JoinError>) {
Expand Down Expand Up @@ -461,6 +497,7 @@ where
AccountCommand::GetDeviceZkNym => {
self.handle_get_zk_nyms_available_for_download().await
}
AccountCommand::ImportZkNym(id) => self.handle_import_zk_nym(id).await,
}
}

Expand Down

0 comments on commit ec05e95

Please sign in to comment.