From 546fdd20bd96e27d0b65b102d2da79fc5a449ed3 Mon Sep 17 00:00:00 2001 From: Prithvi Shahi Date: Mon, 21 Sep 2020 12:27:22 -0700 Subject: [PATCH 1/6] init --- src/dfx/src/commands/identity/mod.rs | 2 ++ src/dfx/src/commands/identity/principal.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/dfx/src/commands/identity/principal.rs diff --git a/src/dfx/src/commands/identity/mod.rs b/src/dfx/src/commands/identity/mod.rs index dae8478eed..2a85cc2364 100644 --- a/src/dfx/src/commands/identity/mod.rs +++ b/src/dfx/src/commands/identity/mod.rs @@ -10,6 +10,7 @@ mod remove; mod rename; mod r#use; mod whoami; +mod principal; fn builtins() -> Vec { vec![ @@ -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..4053adee90 --- /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-id").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(()) +} From ee58d256223694936d4822a253311ff110cfacb8 Mon Sep 17 00:00:00 2001 From: Prithvi Shahi Date: Mon, 21 Sep 2020 12:29:21 -0700 Subject: [PATCH 2/6] add message file --- src/dfx/src/lib/message.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dfx/src/lib/message.rs b/src/dfx/src/lib/message.rs index 45ae47aaa3..af00b7939d 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-id + GetPrincipalId => "Shows the textual representation of the Principal ID associated with current identity." + // dfx new CreateProject => "Creates a new project.", ProjectName => "Specifies the name of the project to create.", From c4042aec97aa8e0ba7f9bcd3ee263d5137df94e5 Mon Sep 17 00:00:00 2001 From: Prithvi Shahi Date: Mon, 21 Sep 2020 12:34:37 -0700 Subject: [PATCH 3/6] fmt and comma --- src/dfx/src/commands/identity/mod.rs | 2 +- src/dfx/src/lib/message.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dfx/src/commands/identity/mod.rs b/src/dfx/src/commands/identity/mod.rs index 2a85cc2364..49ec291922 100644 --- a/src/dfx/src/commands/identity/mod.rs +++ b/src/dfx/src/commands/identity/mod.rs @@ -6,11 +6,11 @@ use clap::{App, ArgMatches, SubCommand}; mod list; mod new; +mod principal; mod remove; mod rename; mod r#use; mod whoami; -mod principal; fn builtins() -> Vec { vec![ diff --git a/src/dfx/src/lib/message.rs b/src/dfx/src/lib/message.rs index af00b7939d..2316640af5 100644 --- a/src/dfx/src/lib/message.rs +++ b/src/dfx/src/lib/message.rs @@ -127,7 +127,7 @@ user_message!( ShowIdentity => "Shows the name of the current identity.", // dfx identity get-principal-id - GetPrincipalId => "Shows the textual representation of the Principal ID associated with current identity." + GetPrincipalId => "Shows the textual representation of the Principal ID associated with the current identity.", // dfx new CreateProject => "Creates a new project.", From b965857c3516315fa75ce9b75d614fd41a22bc2c Mon Sep 17 00:00:00 2001 From: Prithvi Shahi Date: Mon, 21 Sep 2020 16:37:37 -0700 Subject: [PATCH 4/6] a few tests --- e2e/bats/identity.bash | 21 +++++++++++++++++++++ e2e/bats/identity_command.bash | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/e2e/bats/identity.bash b/e2e/bats/identity.bash index 7bbe614717..da7f65a37e 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-id: the get-principal-id 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-id) + + 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..4a231b72e5 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-id +## + +@test "identity get-principal-id: 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-id) + PRINCPAL_ID_JUANA=$(dfx --identity juana identity get-principal-id) + + 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 ## From 314c2553a2239cc09dbf5f93cd10252f3b0cbdc2 Mon Sep 17 00:00:00 2001 From: Prithvi Shahi Date: Tue, 22 Sep 2020 11:12:18 -0700 Subject: [PATCH 5/6] rename --- src/dfx/src/commands/identity/principal.rs | 2 +- src/dfx/src/lib/message.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dfx/src/commands/identity/principal.rs b/src/dfx/src/commands/identity/principal.rs index 4053adee90..a8a7d429a3 100644 --- a/src/dfx/src/commands/identity/principal.rs +++ b/src/dfx/src/commands/identity/principal.rs @@ -5,7 +5,7 @@ use crate::lib::message::UserMessage; use clap::{App, ArgMatches, SubCommand}; pub fn construct() -> App<'static, 'static> { - SubCommand::with_name("get-principal-id").about(UserMessage::GetPrincipalId.to_str()) + SubCommand::with_name("get-principal").about(UserMessage::GetPrincipalId.to_str()) } pub fn exec(env: &dyn Environment, _args: &ArgMatches<'_>) -> DfxResult { diff --git a/src/dfx/src/lib/message.rs b/src/dfx/src/lib/message.rs index 2316640af5..4f02da76a2 100644 --- a/src/dfx/src/lib/message.rs +++ b/src/dfx/src/lib/message.rs @@ -126,8 +126,8 @@ user_message!( // dfx identity whoami ShowIdentity => "Shows the name of the current identity.", - // dfx identity get-principal-id - GetPrincipalId => "Shows the textual representation of the Principal ID associated with 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.", From 8da84798518585d0f9f213cab49613a6cabd0fb2 Mon Sep 17 00:00:00 2001 From: Prithvi Shahi Date: Tue, 22 Sep 2020 11:16:39 -0700 Subject: [PATCH 6/6] rename in tests --- e2e/bats/identity.bash | 4 ++-- e2e/bats/identity_command.bash | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/e2e/bats/identity.bash b/e2e/bats/identity.bash index da7f65a37e..1662086482 100644 --- a/e2e/bats/identity.bash +++ b/e2e/bats/identity.bash @@ -19,12 +19,12 @@ teardown() { } -@test "identity get-principal-id: the get-principal-id is the same as sender id" { +@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-id) + PRINCPAL_ID=$(dfx --identity jose identity get-principal) dfx --identity jose canister create e2e_project dfx --identity jose build e2e_project diff --git a/e2e/bats/identity_command.bash b/e2e/bats/identity_command.bash index 4a231b72e5..6a47859990 100644 --- a/e2e/bats/identity_command.bash +++ b/e2e/bats/identity_command.bash @@ -13,15 +13,15 @@ teardown() { } ## -## dfx identity get-principal-id +## dfx identity get-principal ## -@test "identity get-principal-id: different identities have different principal ids" { +@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-id) - PRINCPAL_ID_JUANA=$(dfx --identity juana identity get-principal-id) + 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