Skip to content

Commit 155bd42

Browse files
authored
chore: Add dfx-core dependency and use it in the SNS CLI to allow it to use the user's DFX identities (dfinity#2927)
This is needed for the DFX SNS extension to better integrate with DFX. The dependency is maintained by DFINITY, it is just not part of the monrepo (and not currently depended on by the monorepo, prior to this PR). dfx-core uses a newer version of ic-agent, which means we need to update ic-agent in the monorepo, and that means we need to update ic-http-gateway (which I do here). SNS CLI commands after this PR have a slightly different command after this. You should pass `--network` to specify the network and `--identity` to specify the identity if you don't want to use the default one in DFX. The default network is the local one if none is specified, for compatibility with DFX.
1 parent 0da5306 commit 155bd42

11 files changed

+9525
-620
lines changed

Cargo.Bazel.Fuzzing.json.lock

Lines changed: 3695 additions & 173 deletions
Large diffs are not rendered by default.

Cargo.Bazel.Fuzzing.toml.lock

Lines changed: 657 additions & 69 deletions
Large diffs are not rendered by default.

Cargo.Bazel.json.lock

Lines changed: 3688 additions & 133 deletions
Large diffs are not rendered by default.

Cargo.Bazel.toml.lock

Lines changed: 655 additions & 67 deletions
Large diffs are not rendered by default.

Cargo.lock

Lines changed: 741 additions & 141 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ chrono = { version = "0.4.38", default-features = false, features = [
522522
"serde",
523523
] }
524524
ciborium = "0.2.1"
525-
clap = { version = "4.5.18", features = ["derive", "string"] }
525+
clap = { version = "4.5.20", features = ["derive", "string"] }
526526
# cloudflare v0.12 is broken, master is partly fixed but unreleased yet.
527527
# see:
528528
# - https://github.com/cloudflare/cloudflare-rs/issues/222
@@ -534,6 +534,7 @@ curve25519-dalek = { version = "4.1.3", features = [
534534
"group",
535535
"precomputed-tables",
536536
] }
537+
dfx-core = { version = "0.1.3" }
537538
ed25519-dalek = { version = "2.1.1", features = [
538539
"std",
539540
"zeroize",

bazel/external_crates.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ def external_crates_repository(name, cargo_lockfile, lockfile, sanitizers_enable
434434
"dashmap": crate.spec(
435435
version = "^5.3.4",
436436
),
437+
"dfx-core": crate.spec(
438+
version = "^0.1.3",
439+
),
437440
"dyn-clone": crate.spec(
438441
version = "^1.0.14",
439442
),

rs/sns/cli/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ DEPENDENCIES = [
2626
"@crate_index//:base64",
2727
"@crate_index//:candid",
2828
"@crate_index//:clap",
29+
"@crate_index//:dfx-core",
2930
"@crate_index//:futures",
3031
"@crate_index//:hex",
3132
"@crate_index//:ic-agent",

rs/sns/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ base64 = { workspace = true }
1919
candid = { workspace = true }
2020
candid-utils = { path = "../../nervous_system/candid_utils" }
2121
clap = { workspace = true }
22+
dfx-core = { workspace = true }
2223
futures = { workspace = true }
2324
hex = { workspace = true }
2425
ic-agent = { workspace = true }

rs/sns/cli/src/lib.rs

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::{
66
};
77
use anyhow::{anyhow, bail, Context, Result};
88
use candid::{CandidType, Decode, Encode, IDLArgs};
9-
use clap::Parser;
10-
use ic_agent::{identity::Secp256k1Identity, Agent};
9+
use ic_agent::Agent;
1110
use ic_base_types::PrincipalId;
1211
use ic_crypto_sha2::Sha256;
1312
use ic_nervous_system_common_test_keys::TEST_NEURON_1_OWNER_KEYPAIR;
@@ -29,7 +28,6 @@ use std::{
2928
sync::Once,
3029
};
3130
use tempfile::NamedTempFile;
32-
3331
pub mod deploy;
3432
pub mod health;
3533
pub mod init_config_file;
@@ -40,7 +38,8 @@ pub mod propose;
4038
mod table;
4139
pub mod unit_helpers;
4240
pub mod upgrade_sns_controlled_canister;
43-
mod utils;
41+
use clap::{ArgGroup, Args, Parser};
42+
pub mod utils;
4443

4544
#[cfg(test)]
4645
mod tests;
@@ -62,17 +61,46 @@ pub struct CliArgs {
6261
#[clap(subcommand)]
6362
pub sub_command: SubCommand,
6463

64+
/// The user identity to run this command as. It contains your principal as well as some things DFX associates with it like the wallet.
65+
#[arg(long, global = true)]
66+
identity: Option<String>,
67+
68+
#[command(flatten)]
69+
network: NetworkOpt,
70+
}
71+
72+
#[derive(Args, Clone, Debug, Default)]
73+
#[clap(
74+
group(ArgGroup::new("network-select").multiple(false)),
75+
)]
76+
pub struct NetworkOpt {
6577
/// Override the compute network to connect to. By default, the local network is used.
66-
/// A valid URL (starting with `http:` or `https:`) can be used here,
67-
/// e.g., "http://localhost:8000" is a valid network name.
68-
// TODO[NNS1-3569]: Remove this argument once we can inherit the same data from dfx_core.
69-
#[clap(long, aliases = ["ic-url"], default_value = MAINNET_NETWORK)]
70-
pub network: Option<String>,
71-
72-
/// Path to the PEM file of an identity to run this command as.
73-
// TODO[NNS1-3569]: Remove this argument once we can inherit the same data from dfx_core.
74-
#[clap(long)]
75-
pub pem: Option<String>,
78+
/// A valid URL (starting with `http:` or `https:`) can be used here, and a special
79+
/// ephemeral network will be created specifically for this request. E.g.
80+
/// "http://localhost:12345/" is a valid network name.
81+
#[arg(long, global(true), group = "network-select")]
82+
network: Option<String>,
83+
84+
/// Shorthand for --network=playground.
85+
/// Borrows short-lived canisters on the real IC network instead of creating normal canisters.
86+
#[clap(long, global(true), group = "network-select")]
87+
playground: bool,
88+
89+
/// Shorthand for --network=ic.
90+
#[clap(long, global(true), group = "network-select")]
91+
ic: bool,
92+
}
93+
94+
impl NetworkOpt {
95+
pub fn to_network_name(&self) -> Option<String> {
96+
if self.playground {
97+
Some("playground".to_string())
98+
} else if self.ic {
99+
Some("ic".to_string())
100+
} else {
101+
self.network.clone()
102+
}
103+
}
76104
}
77105

78106
#[derive(Debug, Parser)]
@@ -103,21 +131,17 @@ pub enum SubCommand {
103131

104132
impl CliArgs {
105133
pub async fn agent(&self) -> Result<Agent> {
106-
let mut agent = match &self.network {
107-
Some(network) if !network.contains(MAINNET_NETWORK) => {
108-
let agent = crate::utils::get_agent(network)?;
109-
agent.fetch_root_key().await?;
110-
agent
134+
let network = match self.network.to_network_name() {
135+
Some(network) => network,
136+
None => {
137+
eprintln!(
138+
"No network specified. Defaulting to the local network. To connect to the mainnet IC instead, try passing `--network=ic`"
139+
);
140+
"local".to_string()
111141
}
112-
None | Some(_) => crate::utils::get_mainnet_agent()?,
113142
};
114143

115-
if let Some(pem) = &self.pem {
116-
let identity = Secp256k1Identity::from_pem(pem.as_bytes())?;
117-
agent.set_identity(identity);
118-
}
119-
120-
Ok(agent)
144+
crate::utils::get_agent(&network, self.identity.clone()).await
121145
}
122146
}
123147

0 commit comments

Comments
 (0)