Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): identity aliases #1094

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/meroctl/src/cli/bootstrap/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ impl StartBootstrapCommand {
);

let invite_command = InviteCommand {
context_id,
inviter_id: inviter_public_key,
context_id: context_id.to_string(),
inviter_id: inviter_public_key.to_string(),
invitee_id: invitee_private_key.public_key(),
};
let invitation_payload = invite_command.invite(invitor_environment).await?;
Expand Down Expand Up @@ -305,6 +305,7 @@ impl StartBootstrapCommand {
None,
&config.identity,
protocol,
None,
)
.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::identity::IdentityCommand;
use crate::cli::context::invite::InviteCommand;
use crate::cli::context::join::JoinCommand;
use crate::cli::context::list::ListCommand;
Expand All @@ -17,6 +18,7 @@ use crate::output::Report;
pub mod create;
mod delete;
mod get;
mod identity;
pub mod invite;
pub mod join;
mod list;
Expand Down Expand Up @@ -58,6 +60,7 @@ pub enum ContextSubCommands {
#[command(alias = "ws")]
Watch(WatchCommand),
Update(UpdateCommand),
Identity(IdentityCommand),
}

impl Report for Context {
Expand All @@ -79,6 +82,7 @@ impl ContextCommand {
ContextSubCommands::List(list) => list.run(environment).await,
ContextSubCommands::Watch(watch) => watch.run(environment).await,
ContextSubCommands::Update(update) => update.run(environment).await,
ContextSubCommands::Identity(identity) => identity.run(environment).await,
}
}
}
36 changes: 34 additions & 2 deletions crates/meroctl/src/cli/context/create.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use calimero_primitives::alias::{Alias, Kind};
use calimero_primitives::application::ApplicationId;
use calimero_primitives::context::ContextId;
use calimero_primitives::hash::Hash;
use calimero_primitives::identity::PublicKey;
use calimero_server_primitives::admin::{
CreateContextRequest, CreateContextResponse, GetApplicationResponse,
InstallApplicationResponse, InstallDevApplicationRequest, UpdateContextApplicationRequest,
CreateContextRequest, CreateContextResponse, CreateIdentityAliasRequest,
CreateIdentityAliasResponse, GetApplicationResponse, InstallApplicationResponse,
InstallDevApplicationRequest, UpdateContextApplicationRequest,
UpdateContextApplicationResponse,
};
use camino::Utf8PathBuf;
Expand Down Expand Up @@ -62,6 +64,9 @@ pub struct CreateCommand {

#[clap(long, value_name = "PROTOCOL")]
protocol: String,

#[clap(long = "as", help = "Create an alias for context identity")]
identity: Option<Alias>,
}

impl Report for CreateContextResponse {
Expand Down Expand Up @@ -91,6 +96,7 @@ impl CreateCommand {
metadata: None,
params,
protocol,
identity,
} => {
let _ = create_context(
environment,
Expand All @@ -101,6 +107,7 @@ impl CreateCommand {
params,
&config.identity,
protocol,
identity,
)
.await?;
}
Expand All @@ -111,6 +118,7 @@ impl CreateCommand {
metadata,
params,
protocol,
identity,
} => {
let path = path.canonicalize_utf8()?;
let metadata = metadata.map(String::into_bytes);
Expand All @@ -133,6 +141,7 @@ impl CreateCommand {
params,
&config.identity,
protocol,
identity,
)
.await?;

Expand Down Expand Up @@ -164,6 +173,7 @@ pub async fn create_context(
params: Option<String>,
keypair: &Keypair,
protocol: String,
identity: Option<Alias>,
) -> EyreResult<(ContextId, PublicKey)> {
if !app_installed(base_multiaddr, &application_id, client, keypair).await? {
bail!("Application is not installed on node.")
Expand All @@ -182,6 +192,28 @@ pub async fn create_context(

environment.output.write(&response);

if let Some(alias) = identity {
let alias_request = CreateIdentityAliasRequest {
alias,
context_id: Some(response.data.context_id),
kind: Kind::Identity,
hash: response.data.member_public_key.into(),
};

let alias_url = multiaddr_to_url(base_multiaddr, "admin-api/dev/add-alias")?;

let alias_response: CreateIdentityAliasResponse = do_request(
client,
alias_url,
Some(alias_request),
keypair,
RequestType::Post,
)
.await?;

environment.output.write(&alias_response);
}

Ok((response.data.context_id, response.data.member_public_key))
}

Expand Down
3 changes: 2 additions & 1 deletion crates/meroctl/src/cli/context/delete.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use calimero_primitives::context::ContextId;
use calimero_server_primitives::admin::DeleteContextResponse;
use clap::Parser;
use eyre::Result as EyreResult;
Expand All @@ -11,7 +12,7 @@ use crate::output::Report;
#[command(about = "Delete an context")]
pub struct DeleteCommand {
#[clap(name = "CONTEXT_ID", help = "The context ID to delete")]
pub context_id: String,
pub context_id: ContextId,
}

impl Report for DeleteContextResponse {
Expand Down
69 changes: 39 additions & 30 deletions crates/meroctl/src/cli/context/get.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use calimero_primitives::context::ContextId;
use calimero_server_primitives::admin::{
GetContextClientKeysResponse, GetContextIdentitiesResponse, GetContextResponse,
GetContextStorageResponse, GetContextUsersResponse,
Expand All @@ -7,11 +8,9 @@ use eyre::Result as EyreResult;
use libp2p::identity::Keypair;
use libp2p::Multiaddr;
use reqwest::Client;
use serde::de::DeserializeOwned;
use serde::Serialize;

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

#[derive(Parser, Debug)]
Expand All @@ -21,7 +20,7 @@ pub struct GetCommand {
pub command: GetSubcommand,

#[arg(value_name = "CONTEXT_ID", help = "context_id of the context")]
pub context_id: String,
pub context_id: ContextId,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -114,8 +113,15 @@ impl GetCommand {
multiaddr,
&format!("admin-api/dev/contexts/{}", self.context_id),
)?;
self.make_request::<GetContextResponse>(environment, client, url, keypair)
.await
make_request::<_, GetContextResponse>(
environment,
client,
url,
None::<()>,
keypair,
RequestType::Get,
)
.await
}

async fn get_client_keys(
Expand All @@ -129,8 +135,15 @@ impl GetCommand {
multiaddr,
&format!("admin-api/dev/contexts/{}/client-keys", self.context_id),
)?;
self.make_request::<GetContextClientKeysResponse>(environment, client, url, keypair)
.await
make_request::<_, GetContextClientKeysResponse>(
environment,
client,
url,
None::<()>,
keypair,
RequestType::Get,
)
.await
}

async fn get_storage(
Expand All @@ -144,8 +157,15 @@ impl GetCommand {
multiaddr,
&format!("admin-api/dev/contexts/{}/storage", self.context_id),
)?;
self.make_request::<GetContextStorageResponse>(environment, client, url, keypair)
.await
make_request::<_, GetContextStorageResponse>(
environment,
client,
url,
None::<()>,
keypair,
RequestType::Get,
)
.await
}

async fn get_identities(
Expand All @@ -165,25 +185,14 @@ impl GetCommand {
format!("admin-api/dev/contexts/{}/identities", self.context_id)
};
let url = multiaddr_to_url(multiaddr, &endpoint)?;
self.make_request::<GetContextIdentitiesResponse>(environment, client, url, keypair)
.await
}

async fn make_request<O>(
&self,
environment: &Environment,
client: &Client,
url: reqwest::Url,
keypair: &Keypair,
) -> EyreResult<()>
where
O: DeserializeOwned + Report + Serialize,
{
let response =
do_request::<(), O>(client, url, None::<()>, keypair, RequestType::Get).await?;

environment.output.write(&response);

Ok(())
make_request::<_, GetContextIdentitiesResponse>(
environment,
client,
url,
None::<()>,
keypair,
RequestType::Get,
)
.await
}
}
Loading