Skip to content

Commit

Permalink
feat: implement context invite meroctl command, cleanup admin primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed Nov 1, 2024
1 parent e9ae279 commit 08e2d05
Show file tree
Hide file tree
Showing 20 changed files with 389 additions and 197 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ license.workspace = true

[dependencies]
camino = { workspace = true, features = ["serde1"] }
clap.workspace = true
ed25519-dalek.workspace = true
eyre.workspace = true
futures-util.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/context/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::io::Error as IoError;
use std::str::FromStr;
Expand Down
8 changes: 6 additions & 2 deletions crates/meroctl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use crate::output::{Format, Output, Report};

mod app;
mod context;
mod identity;
mod jsonrpc;

use app::AppCommand;
use context::ContextCommand;
use identity::IdentityCommand;
use jsonrpc::CallCommand;

pub const EXAMPLES: &str = r"
Expand Down Expand Up @@ -44,8 +46,9 @@ pub struct RootCommand {

#[derive(Debug, Subcommand)]
pub enum SubCommands {
Context(ContextCommand),
App(AppCommand),
Context(ContextCommand),
Identity(IdentityCommand),
JsonRpc(CallCommand),
}

Expand Down Expand Up @@ -81,8 +84,9 @@ impl RootCommand {
let environment = Environment::new(self.args, output);

let result = match self.action {
SubCommands::Context(context) => context.run(&environment).await,
SubCommands::App(application) => application.run(&environment).await,
SubCommands::Context(context) => context.run(&environment).await,
SubCommands::Identity(identity) => identity.run(&environment).await,
SubCommands::JsonRpc(jsonrpc) => jsonrpc.run(&environment).await,
};

Expand Down
4 changes: 4 additions & 0 deletions crates/meroctl/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use eyre::Result as EyreResult;
use crate::cli::context::create::CreateCommand;
use crate::cli::context::delete::DeleteCommand;
use crate::cli::context::get::GetCommand;
use crate::cli::context::invite::InviteCommand;
use crate::cli::context::join::JoinCommand;
use crate::cli::context::list::ListCommand;
use crate::cli::context::watch::WatchCommand;
Expand All @@ -15,6 +16,7 @@ use crate::output::Report;
mod create;
mod delete;
mod get;
mod invite;
mod join;
mod list;
mod watch;
Expand Down Expand Up @@ -47,6 +49,7 @@ pub enum ContextSubCommands {
List(ListCommand),
Create(Box<CreateCommand>),
Join(JoinCommand),
Invite(InviteCommand),
Get(GetCommand),
#[command(alias = "del")]
Delete(DeleteCommand),
Expand All @@ -68,6 +71,7 @@ impl ContextCommand {
ContextSubCommands::Create(create) => create.run(environment).await,
ContextSubCommands::Delete(delete) => delete.run(environment).await,
ContextSubCommands::Get(get) => get.run(environment).await,
ContextSubCommands::Invite(invite) => invite.run(environment).await,
ContextSubCommands::Join(join) => join.run(environment).await,
ContextSubCommands::List(list) => list.run(environment).await,
ContextSubCommands::Watch(watch) => watch.run(environment).await,
Expand Down
60 changes: 60 additions & 0 deletions crates/meroctl/src/cli/context/invite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use calimero_primitives::context::ContextId;
use calimero_primitives::identity::PublicKey;
use calimero_server_primitives::admin::{InviteToContextRequest, InviteToContextResponse};
use clap::Parser;
use eyre::Result as EyreResult;
use reqwest::Client;

use crate::cli::Environment;
use crate::common::{do_request, fetch_multiaddr, load_config, multiaddr_to_url, RequestType};
use crate::output::Report;

#[derive(Debug, Parser)]
#[command(about = "Create invitation to a context for a invitee")]
pub struct InviteCommand {
#[clap(
value_name = "CONTEXT_ID",
help = "The id of the context for which invitation is created"
)]
pub context_id: ContextId,

#[clap(value_name = "INVITER_ID", help = "The public key of the inviter")]
pub inviter_id: PublicKey,

#[clap(value_name = "INVITEE_ID", help = "The public key of the invitee")]
pub invitee_id: PublicKey,
}

impl Report for InviteToContextResponse {
fn report(&self) {
match self.data {
Some(ref payload) => {
println!("{:?}", payload)
}
None => println!("No invitation payload"),
}
}
}

impl InviteCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;

let response: InviteToContextResponse = do_request(
&Client::new(),
multiaddr_to_url(fetch_multiaddr(&config)?, "admin-api/dev/contexts/invite")?,
Some(InviteToContextRequest {
context_id: self.context_id,
inviter_id: self.inviter_id,
invitee_id: self.invitee_id,
}),
&config.identity,
RequestType::Post,
)
.await?;

environment.output.write(&response);

Ok(())
}
}
37 changes: 37 additions & 0 deletions crates/meroctl/src/cli/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use clap::{Parser, Subcommand};
use const_format::concatcp;
use eyre::Result as EyreResult;

use crate::cli::identity::generate::GenerateCommand;
use crate::cli::Environment;

mod generate;

pub const EXAMPLES: &str = r"
#
$ meroctl -- --node-name node1 identity generate
";

#[derive(Debug, Parser)]
#[command(about = "Command for managing applications")]
#[command(after_help = concatcp!(
"Examples:",
EXAMPLES
))]
pub struct IdentityCommand {
#[command(subcommand)]
pub subcommand: IdentitySubCommands,
}

#[derive(Debug, Subcommand)]
pub enum IdentitySubCommands {
Generate(GenerateCommand),
}

impl IdentityCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
match self.subcommand {
IdentitySubCommands::Generate(generate) => generate.run(environment).await,
}
}
}
40 changes: 40 additions & 0 deletions crates/meroctl/src/cli/identity/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use calimero_server_primitives::admin::GenerateContextIdentityResponse;
use clap::Parser;
use eyre::Result as EyreResult;
use reqwest::Client;

use crate::cli::Environment;
use crate::common::{do_request, fetch_multiaddr, load_config, multiaddr_to_url, RequestType};
use crate::output::Report;

#[derive(Debug, Parser)]
#[command(about = "Generate public/private key pair used for context identity")]
pub struct GenerateCommand {}

impl Report for GenerateContextIdentityResponse {
fn report(&self) {
println!("public_key: {}", self.data.public_key);
println!("private_key: {}", self.data.private_key);
}
}

impl GenerateCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;

let url = multiaddr_to_url(fetch_multiaddr(&config)?, "admin-api/dev/identity/context")?;

let response: GenerateContextIdentityResponse = do_request(
&Client::new(),
url,
None::<()>,
&config.identity,
RequestType::Post,
)
.await?;

environment.output.write(&response);

Ok(())
}
}
1 change: 0 additions & 1 deletion crates/merod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ multiaddr.workspace = true
near-crypto.workspace = true
rand.workspace = true
starknet.workspace = true
starknet-crypto.workspace = true
tokio = { workspace = true, features = ["io-std", "macros"] }
toml_edit.workspace = true
tracing.workspace = true
Expand Down
3 changes: 0 additions & 3 deletions crates/merod/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ impl InitCommand {
network: match self.protocol {
ConfigProtocol::Near => "testnet".into(),
ConfigProtocol::Starknet => "sepolia".into(),
_ => "unknown".into(),
},
protocol: self.protocol.into(),
contract_id: match self.protocol {
Expand All @@ -234,7 +233,6 @@ impl InitCommand {
"0x1ee8182d5dd595be9797ccae1488bdf84b19a0f05a93ce6148b0efae04f4568"
.parse()?
}
_ => "unknown.contract.id".parse()?,
},
},
},
Expand Down Expand Up @@ -310,6 +308,5 @@ fn generate_local_signer(
}),
})
}
_ => todo!(),
}
}
Loading

0 comments on commit 08e2d05

Please sign in to comment.