Skip to content

Commit 615130d

Browse files
New flow and fixed mainnet canister
1 parent 961cb7a commit 615130d

File tree

7 files changed

+241
-280
lines changed

7 files changed

+241
-280
lines changed

Cargo.lock

Lines changed: 73 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ assert_cmd = "2"
2121
async-dropper = { version = "0.3.0", features = ["tokio", "simple"] }
2222
async-trait = "0.1.88"
2323
axoupdater = "0.10.0"
24+
axum = "0.8"
2425
base64 = "0.22"
2526
backoff = { version = "0.4", features = ["tokio"] }
2627
bigdecimal = "0.4.10"

crates/icp-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ anstyle.workspace = true
1515
anyhow.workspace = true
1616
async-trait.workspace = true
1717
axoupdater.workspace = true
18+
axum.workspace = true
1819
backoff.workspace = true
1920
base64.workspace = true
2021
bigdecimal.workspace = true

crates/icp-cli/src/commands/identity/link/ii.rs

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,31 @@
11
use clap::Args;
22
use ic_agent::{Identity as _, export::Principal, identity::BasicIdentity};
3-
use icp::{
4-
context::{CanisterSelection, Context, EnvironmentSelection},
5-
identity::{IdentitySelection, key},
6-
};
7-
use snafu::{OptionExt, ResultExt, Snafu};
3+
use icp::{context::Context, identity::key};
4+
use snafu::{ResultExt, Snafu};
85
use tracing::info;
96

10-
use crate::{operations::ii_poll, options::EnvironmentOpt};
7+
use crate::operations::ii_poll;
118

129
/// Link an Internet Identity to a new identity
1310
#[derive(Debug, Args)]
1411
pub(crate) struct IiArgs {
1512
/// Name for the linked identity
1613
name: String,
17-
18-
#[command(flatten)]
19-
environment: EnvironmentOpt,
2014
}
2115

2216
pub(crate) async fn exec(ctx: &Context, args: &IiArgs) -> Result<(), IiError> {
23-
let environment: EnvironmentSelection = args.environment.clone().into();
24-
25-
// Generate an Ed25519 keypair for the session key
2617
let secret_key = ic_ed25519::PrivateKey::generate();
2718
let identity_key = key::IdentityKey::Ed25519(secret_key.clone());
2819
let basic = BasicIdentity::from_raw_key(&secret_key.serialize_raw());
2920
let der_public_key = basic.public_key().expect("ed25519 always has a public key");
3021

31-
// Resolve the environment to get network access
32-
let env = ctx
33-
.get_environment(&environment)
34-
.await
35-
.context(GetEnvSnafu)?;
36-
let network_access = ctx
37-
.network
38-
.access(&env.network)
22+
let chain = ii_poll::poll_for_delegation(&der_public_key)
3923
.await
40-
.context(NetworkAccessSnafu)?;
24+
.context(PollSnafu)?;
4125

42-
let http_gateway_url = network_access
43-
.http_gateway_url
44-
.as_ref()
45-
.context(NoHttpGatewaySnafu)?;
46-
47-
// Create an anonymous agent for polling
48-
let agent = ctx
49-
.get_agent_for_env(&IdentitySelection::Anonymous, &environment)
50-
.await
51-
.context(CreateAgentSnafu)?;
52-
53-
// Look up the cli-backend canister ID
54-
let delegator_backend_id = ctx
55-
.get_canister_id_for_env(
56-
&CanisterSelection::Named("backend".to_string()),
57-
&environment,
58-
)
59-
.await
60-
.context(LookupCanisterSnafu)?;
61-
62-
let delegator_frontend_id = ctx
63-
.get_canister_id_for_env(
64-
&CanisterSelection::Named("frontend".to_string()),
65-
&environment,
66-
)
67-
.await
68-
.context(LookupCanisterSnafu)?;
69-
70-
let delegator_frontend_friendly = if network_access.use_friendly_domains {
71-
Some(("frontend", env.name.as_str()))
72-
} else {
73-
None
74-
};
75-
76-
// Open browser and poll for delegation
77-
let chain = ii_poll::poll_for_delegation(
78-
&agent,
79-
delegator_backend_id,
80-
delegator_frontend_id,
81-
&der_public_key,
82-
http_gateway_url,
83-
delegator_frontend_friendly,
84-
)
85-
.await
86-
.context(PollSnafu)?;
87-
88-
// Derive the II principal from the root of the delegation chain
8926
let from_key = hex::decode(&chain.public_key).context(DecodeFromKeySnafu)?;
9027
let ii_principal = Principal::self_authenticating(&from_key);
9128

92-
// Save the identity
9329
ctx.dirs
9430
.identity()?
9531
.with_write(async |dirs| {
@@ -105,28 +41,7 @@ pub(crate) async fn exec(ctx: &Context, args: &IiArgs) -> Result<(), IiError> {
10541

10642
#[derive(Debug, Snafu)]
10743
pub(crate) enum IiError {
108-
#[snafu(display("failed to resolve environment"))]
109-
GetEnv {
110-
source: icp::context::GetEnvironmentError,
111-
},
112-
113-
#[snafu(display("failed to access network"))]
114-
NetworkAccess { source: icp::network::AccessError },
115-
116-
#[snafu(display("network has no HTTP gateway URL configured"))]
117-
NoHttpGateway,
118-
119-
#[snafu(display("failed to create agent"))]
120-
CreateAgent {
121-
source: icp::context::GetAgentForEnvError,
122-
},
123-
124-
#[snafu(display("failed to look up cli-backend canister ID"))]
125-
LookupCanister {
126-
source: icp::context::GetCanisterIdForEnvError,
127-
},
128-
129-
#[snafu(display("failed during II authentication polling"))]
44+
#[snafu(display("failed during II authentication"))]
13045
Poll { source: ii_poll::IiPollError },
13146

13247
#[snafu(display("invalid public key in delegation chain"))]

crates/icp-cli/src/commands/identity/login.rs

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
use clap::Args;
22
use icp::{
3-
context::{CanisterSelection, Context, EnvironmentSelection},
3+
context::Context,
44
identity::{
5-
IdentitySelection, key,
5+
key,
66
manifest::{IdentityList, IdentitySpec},
77
},
88
};
99
use snafu::{OptionExt, ResultExt, Snafu};
1010
use tracing::info;
1111

12-
use crate::{operations::ii_poll, options::EnvironmentOpt};
12+
use crate::operations::ii_poll;
1313

1414
/// Re-authenticate an Internet Identity delegation
1515
#[derive(Debug, Args)]
1616
pub(crate) struct LoginArgs {
1717
/// Name of the identity to re-authenticate
1818
name: String,
19-
20-
#[command(flatten)]
21-
environment: EnvironmentOpt,
2219
}
2320

2421
pub(crate) async fn exec(ctx: &Context, args: &LoginArgs) -> Result<(), LoginError> {
25-
let environment: EnvironmentSelection = args.environment.clone().into();
26-
27-
// Load the identity list and verify this is an II identity
2822
let algorithm = ctx
2923
.dirs
3024
.identity()?
@@ -44,63 +38,10 @@ pub(crate) async fn exec(ctx: &Context, args: &LoginArgs) -> Result<(), LoginErr
4438
let der_public_key =
4539
key::load_ii_session_public_key(&args.name, &algorithm).context(LoadSessionKeySnafu)?;
4640

47-
// Resolve the environment to get network access
48-
let env = ctx
49-
.get_environment(&environment)
50-
.await
51-
.context(GetEnvSnafu)?;
52-
let network_access = ctx
53-
.network
54-
.access(&env.network)
55-
.await
56-
.context(NetworkAccessSnafu)?;
57-
58-
let http_gateway_url = network_access
59-
.http_gateway_url
60-
.as_ref()
61-
.context(NoHttpGatewaySnafu)?;
62-
63-
// Create an anonymous agent for polling
64-
let agent = ctx
65-
.get_agent_for_env(&IdentitySelection::Anonymous, &environment)
66-
.await
67-
.context(CreateAgentSnafu)?;
68-
69-
// Look up the cli-backend canister ID
70-
let delegator_backend_id = ctx
71-
.get_canister_id_for_env(
72-
&CanisterSelection::Named("backend".to_string()),
73-
&environment,
74-
)
41+
let chain = ii_poll::poll_for_delegation(&der_public_key)
7542
.await
76-
.context(LookupCanisterSnafu)?;
77-
let delegator_frontend_id = ctx
78-
.get_canister_id_for_env(
79-
&CanisterSelection::Named("frontend".to_string()),
80-
&environment,
81-
)
82-
.await
83-
.context(LookupCanisterSnafu)?;
84-
85-
let delegator_frontend_friendly = if network_access.use_friendly_domains {
86-
Some(("frontend", env.name.as_str()))
87-
} else {
88-
None
89-
};
43+
.context(PollSnafu)?;
9044

91-
// Open browser and poll for delegation
92-
let chain = ii_poll::poll_for_delegation(
93-
&agent,
94-
delegator_backend_id,
95-
delegator_frontend_id,
96-
&der_public_key,
97-
http_gateway_url,
98-
delegator_frontend_friendly,
99-
)
100-
.await
101-
.context(PollSnafu)?;
102-
103-
// Update the delegation chain
10445
ctx.dirs
10546
.identity()?
10647
.with_write(async |dirs| key::update_ii_delegation(dirs, &args.name, &chain))
@@ -133,28 +74,7 @@ pub(crate) enum LoginError {
13374
#[snafu(display("failed to load II session key from keyring"))]
13475
LoadSessionKey { source: key::LoadIdentityError },
13576

136-
#[snafu(display("failed to resolve environment"))]
137-
GetEnv {
138-
source: icp::context::GetEnvironmentError,
139-
},
140-
141-
#[snafu(display("failed to access network"))]
142-
NetworkAccess { source: icp::network::AccessError },
143-
144-
#[snafu(display("network has no HTTP gateway URL configured"))]
145-
NoHttpGateway,
146-
147-
#[snafu(display("failed to create agent"))]
148-
CreateAgent {
149-
source: icp::context::GetAgentForEnvError,
150-
},
151-
152-
#[snafu(display("failed to look up cli-backend canister ID"))]
153-
LookupCanister {
154-
source: icp::context::GetCanisterIdForEnvError,
155-
},
156-
157-
#[snafu(display("failed during II authentication polling"))]
77+
#[snafu(display("failed during II authentication"))]
15878
Poll { source: ii_poll::IiPollError },
15979

16080
#[snafu(display("failed to update delegation"))]

0 commit comments

Comments
 (0)