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: allow users to get the principal id of the current identity #1046

Merged
merged 12 commits into from
Sep 22, 2020
21 changes: 21 additions & 0 deletions e2e/bats/identity.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ teardown() {
rm -rf $(pwd)/home-for-test
}


@test "identity get-principal: the get-principal is the same as sender id" {
install_asset identity
dfx_start
assert_command dfx identity new jose

PRINCPAL_ID=$(dfx --identity jose identity get-principal)

dfx --identity jose canister create e2e_project
dfx --identity jose build e2e_project
dfx --identity jose canister install e2e_project

assert_command dfx --identity jose canister call e2e_project amInitializer

SENDER_ID=$(dfx --identity jose canister call e2e_project fromCall)

if [ "$PRINCPAL_ID" -ne "$SENDER_ID" ]; then
echo "IDs did not match: Principal '${PRINCPAL_ID}' != Sender '${SENDER_ID}'..." | fail
fi
}

@test "calls and query receive the same principal from dfx" {
install_asset identity
dfx_start
Expand Down
16 changes: 16 additions & 0 deletions e2e/bats/identity_command.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ teardown() {
rm -rf $TEMPORARY_HOME
}

##
## dfx identity get-principal
##

@test "identity get-principal: different identities have different principal ids" {
assert_command dfx identity new jose
assert_command dfx identity new juana

PRINCPAL_ID_JOSE=$(dfx --identity jose identity get-principal)
PRINCPAL_ID_JUANA=$(dfx --identity juana identity get-principal)

if [ "$PRINCPAL_ID_JOSE" -eq "$PRINCPAL_ID_JUANA" ]; then
echo "IDs should not match: Jose '${PRINCPAL_ID_JOSE}' == Juana '${PRINCPAL_ID_JUANA}'..." | fail
fi
}

##
## dfx identity list
##
Expand Down
2 changes: 2 additions & 0 deletions src/dfx/src/commands/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::{App, ArgMatches, SubCommand};

mod list;
mod new;
mod principal;
mod remove;
mod rename;
mod r#use;
Expand All @@ -19,6 +20,7 @@ fn builtins() -> Vec<CliCommand> {
CliCommand::new(rename::construct(), rename::exec),
CliCommand::new(r#use::construct(), r#use::exec),
CliCommand::new(whoami::construct(), whoami::exec),
CliCommand::new(principal::construct(), principal::exec),
]
}

Expand Down
16 changes: 16 additions & 0 deletions src/dfx/src/commands/identity/principal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::identity::identity_manager::IdentityManager;
use crate::lib::message::UserMessage;
use clap::{App, ArgMatches, SubCommand};

pub fn construct() -> App<'static, 'static> {
SubCommand::with_name("get-principal").about(UserMessage::GetPrincipalId.to_str())
}

pub fn exec(env: &dyn Environment, _args: &ArgMatches<'_>) -> DfxResult {
let identity = IdentityManager::new(env)?.instantiate_selected_identity()?;
let principal_id = identity.sender()?;
println!("{}", principal_id.to_text());
Ok(())
}
3 changes: 3 additions & 0 deletions src/dfx/src/lib/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ user_message!(
// dfx identity whoami
ShowIdentity => "Shows the name of the current identity.",

// dfx identity get-principal
GetPrincipalId => "Shows the textual representation of the Principal associated with the current identity.",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lsgunnlsgunn may I request you review the command name and help text?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

// dfx new
CreateProject => "Creates a new project.",
ProjectName => "Specifies the name of the project to create.",
Expand Down