Skip to content

Commit a79990f

Browse files
Merge #119
119: feat: scope nilauth requests by blind module in `nillion` cli r=mfontanini a=mfontanini This changes the `nillion` cli to use the new blind module parameter when talking to nilauth. Co-authored-by: Matias Fontanini <[email protected]>
2 parents 92d15fa + e1ffd4f commit a79990f

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/nillion/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ hex = { version = "0.4", features = ["serde"] }
1313
humantime = "2.2"
1414
futures = "0.3.30"
1515
log = "0.4"
16-
nilauth-client = { git = "https://github.com/NillionNetwork/nilauth-client-rs", rev = "7c5105d250e46d87dd4e3cdbdd0519a949f5186c" }
16+
nilauth-client = { git = "https://github.com/NillionNetwork/nilauth-client-rs", rev = "03e2b38609f6c48332fbfc38960ce57d27121f44" }
1717
nillion-nucs = { git = "https://github.com/NillionNetwork/nuc-rs", rev = "687657acd08f2543e5c0d75e910eb9f1b1152d00" }
1818
serde = "1.0.214"
1919
serde_yaml = "0.9"

tools/nillion/src/args.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::{error::ErrorKind, Args, CommandFactory, Parser, Subcommand, ValueEnum
33
use clap_utils::shell_completions::ShellCompletionsArgs;
44
use hex::FromHexError;
55
use nada_values_args::NadaValueArgs;
6+
use nilauth_client::client::BlindModule;
67
use nillion_client::{grpc::membership::NodeId, Clear, NadaValue, UserId, Uuid};
78
use nillion_nucs::token::Did;
89
use serde::Deserialize;
@@ -651,7 +652,7 @@ pub enum NilauthCommand {
651652
Subscription(NilauthSubscriptionCommand),
652653

653654
/// Request a token.
654-
Token,
655+
Token { module: BlindModuleArg },
655656

656657
/// Revoke a token.
657658
Revoke(RevokeTokenArgs),
@@ -667,10 +668,32 @@ pub enum NilauthCommand {
667668
#[derive(Subcommand)]
668669
pub enum NilauthSubscriptionCommand {
669670
/// Pay for a subscription.
670-
Pay,
671+
Pay { module: BlindModuleArg },
671672

672673
/// Get the subscription status.
673-
Status,
674+
Status { module: BlindModuleArg },
675+
676+
/// Get the cost of a subscription.
677+
Cost { module: BlindModuleArg },
678+
}
679+
680+
/// A nilauth blind module.
681+
#[derive(Clone, ValueEnum)]
682+
pub enum BlindModuleArg {
683+
/// nildb
684+
Nildb,
685+
686+
/// nilai
687+
Nilai,
688+
}
689+
690+
impl From<BlindModuleArg> for BlindModule {
691+
fn from(arg: BlindModuleArg) -> Self {
692+
match arg {
693+
BlindModuleArg::Nildb => BlindModule::NilDb,
694+
BlindModuleArg::Nilai => BlindModule::NilAi,
695+
}
696+
}
674697
}
675698

676699
/// The arguments to a revoke token command.

tools/nillion/src/handlers/networks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl NetworksHandler {
4545
});
4646
let nilauth = nilauth_endpoint.map(|endpoint| NilauthConfig { endpoint });
4747
NetworkConfig { bootnode, payments, nilauth }.write_to_file(&name)?;
48-
Ok(Box::new(format!("Network {} added", name)))
48+
Ok(Box::new(format!("Network {name} added")))
4949
}
5050

5151
fn edit(args: EditNetworkArgs) -> HandlerResult {

tools/nillion/src/handlers/nilauth.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::HandlerResult;
22
use crate::args::{CheckRevokedArgs, NilauthCommand, NilauthSubscriptionCommand, RevokeTokenArgs};
33
use anyhow::{anyhow, bail, Context, Result};
44
use chrono::{DateTime, Utc};
5-
use nilauth_client::client::{About, DefaultNilauthClient, NilauthClient};
5+
use nilauth_client::client::{About, BlindModule, DefaultNilauthClient, NilauthClient};
66
use nillion_client::payments::{NillionChainClient, NillionChainPrivateKey};
77
use nillion_nucs::{
88
envelope::NucTokenEnvelope,
@@ -40,16 +40,19 @@ impl NilauthHandler {
4040
pub async fn handle(self, command: NilauthCommand) -> HandlerResult {
4141
use NilauthCommand::*;
4242
match command {
43-
Subscription(NilauthSubscriptionCommand::Pay) => self.pay_subscription().await,
44-
Subscription(NilauthSubscriptionCommand::Status) => self.subscription_status().await,
45-
Token => self.request_token().await,
43+
Subscription(NilauthSubscriptionCommand::Pay { module }) => self.pay_subscription(module.into()).await,
44+
Subscription(NilauthSubscriptionCommand::Status { module }) => {
45+
self.subscription_status(module.into()).await
46+
}
47+
Subscription(NilauthSubscriptionCommand::Cost { module }) => self.subscription_cost(module.into()).await,
48+
Token { module } => self.request_token(module.into()).await,
4649
Revoke(args) => self.revoke_token(args).await,
4750
CheckRevoked(args) => self.check_revoked(args).await,
4851
About => self.about().await,
4952
}
5053
}
5154

52-
async fn pay_subscription(self) -> HandlerResult {
55+
async fn pay_subscription(self, module: BlindModule) -> HandlerResult {
5356
#[derive(Serialize)]
5457
struct Output {
5558
tx_hash: String,
@@ -65,11 +68,11 @@ impl NilauthHandler {
6568
nilchain_client.set_gas_price(gas_price);
6669
}
6770

68-
let tx_hash = self.client.pay_subscription(&mut nilchain_client, &self.key).await?;
71+
let tx_hash = self.client.pay_subscription(&mut nilchain_client, &self.key, module).await?;
6972
Ok(Box::new(Output { tx_hash: tx_hash.to_string() }))
7073
}
7174

72-
async fn subscription_status(self) -> HandlerResult {
75+
async fn subscription_status(self, module: BlindModule) -> HandlerResult {
7376
#[derive(Serialize)]
7477
struct Output {
7578
subscribed: bool,
@@ -78,25 +81,37 @@ impl NilauthHandler {
7881
expires_at: Option<DateTime<Utc>>,
7982
}
8083

81-
let subscription = self.client.subscription_status(&self.key).await?;
84+
let subscription = self.client.subscription_status(&self.key.public_key(), module).await?;
8285
let output =
8386
Output { subscribed: subscription.subscribed, expires_at: subscription.details.map(|s| s.expires_at) };
8487
Ok(Box::new(output))
8588
}
8689

87-
async fn request_token(self) -> HandlerResult {
90+
async fn subscription_cost(self, module: BlindModule) -> HandlerResult {
91+
#[derive(Serialize)]
92+
struct Output {
93+
cost_unils: u64,
94+
}
95+
96+
let cost = self.client.subscription_cost(module).await?;
97+
let output = Output { cost_unils: cost.to_unil() };
98+
Ok(Box::new(output))
99+
}
100+
101+
async fn request_token(self, module: BlindModule) -> HandlerResult {
88102
#[derive(Serialize)]
89103
struct Output {
90104
token: String,
91105
}
92106

93-
let token = self.client.request_token(&self.key).await?;
107+
let token = self.client.request_token(&self.key, module).await?;
94108
Ok(Box::new(Output { token }))
95109
}
96110

97111
async fn revoke_token(self, args: RevokeTokenArgs) -> HandlerResult {
98112
let token = NucTokenEnvelope::decode(&args.token)?;
99-
self.client.revoke_token(&token, &self.key).await?;
113+
let args = nilauth_client::client::RevokeTokenArgs { auth_token: token.clone(), revocable_token: token };
114+
self.client.revoke_token(args, &self.key).await?;
100115
Ok(Box::new("Token revoked".to_string()))
101116
}
102117

tools/nillion/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async fn main() {
9898
};
9999

100100
match serialized_result {
101-
Ok(Some(serialized)) => println!("{}", serialized),
101+
Ok(Some(serialized)) => println!("{serialized}"),
102102
Ok(None) => {}
103103
Err(e) => {
104104
println!("{}", serialize_error(&output_format, &e));

0 commit comments

Comments
 (0)