Skip to content

Commit

Permalink
feat: allow users to get the principal id of the current identity (#1046
Browse files Browse the repository at this point in the history
)

* init

* add message file

* fmt and comma

* a few tests

* rename

* rename in tests

Co-authored-by: Prithvi Shahi <prithvi@dfinity.org>
  • Loading branch information
p-shahi and Prithvi Shahi authored Sep 22, 2020
1 parent 38c81ad commit 684d9ec
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
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.",

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

0 comments on commit 684d9ec

Please sign in to comment.