diff --git a/e2e/bats/identity.bash b/e2e/bats/identity.bash index 7bbe614717..1662086482 100644 --- a/e2e/bats/identity.bash +++ b/e2e/bats/identity.bash @@ -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 diff --git a/e2e/bats/identity_command.bash b/e2e/bats/identity_command.bash index 7a1caafa32..6a47859990 100644 --- a/e2e/bats/identity_command.bash +++ b/e2e/bats/identity_command.bash @@ -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 ## diff --git a/src/dfx/src/commands/identity/mod.rs b/src/dfx/src/commands/identity/mod.rs index dae8478eed..49ec291922 100644 --- a/src/dfx/src/commands/identity/mod.rs +++ b/src/dfx/src/commands/identity/mod.rs @@ -6,6 +6,7 @@ use clap::{App, ArgMatches, SubCommand}; mod list; mod new; +mod principal; mod remove; mod rename; mod r#use; @@ -19,6 +20,7 @@ fn builtins() -> Vec { 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), ] } diff --git a/src/dfx/src/commands/identity/principal.rs b/src/dfx/src/commands/identity/principal.rs new file mode 100644 index 0000000000..a8a7d429a3 --- /dev/null +++ b/src/dfx/src/commands/identity/principal.rs @@ -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(()) +} diff --git a/src/dfx/src/lib/message.rs b/src/dfx/src/lib/message.rs index 45ae47aaa3..4f02da76a2 100644 --- a/src/dfx/src/lib/message.rs +++ b/src/dfx/src/lib/message.rs @@ -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.", + // dfx new CreateProject => "Creates a new project.", ProjectName => "Specifies the name of the project to create.",