mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-10-05 18:47:22 +03:00
add ENDPOINT to .env
This commit is contained in:
parent
c54c4aea2d
commit
b7eac78644
@ -121,6 +121,7 @@ pub fn setup_build_directory(
|
||||
program_name: &str,
|
||||
bytecode: &String,
|
||||
handler: &Handler,
|
||||
endpoint: String,
|
||||
) -> Result<Package<CurrentNetwork>, ()> {
|
||||
// Initialize a temporary directory.
|
||||
let directory = temp_dir();
|
||||
@ -137,7 +138,7 @@ pub fn setup_build_directory(
|
||||
let _manifest_file = Manifest::create(&directory, &program_id).unwrap();
|
||||
|
||||
// Create the environment file.
|
||||
Env::<CurrentNetwork>::new().unwrap().write_to(&directory).unwrap();
|
||||
Env::<CurrentNetwork>::new(None, endpoint).unwrap().write_to(&directory).unwrap();
|
||||
if Env::<CurrentNetwork>::exists_at(&directory) {
|
||||
println!(".env file created at {:?}", &directory);
|
||||
}
|
||||
|
@ -315,6 +315,7 @@ mod test_helpers {
|
||||
use std::path::Path;
|
||||
|
||||
const NETWORK: &str = "mainnet";
|
||||
const ENDPOINT: &str = "https://api.explorer.aleo.org/v1";
|
||||
|
||||
pub(crate) fn sample_nested_package(temp_dir: &Path) {
|
||||
let name = "nested";
|
||||
@ -329,7 +330,9 @@ mod test_helpers {
|
||||
let new = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: name.to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New { name: name.to_string(), network: NETWORK.to_string(), endpoint: ENDPOINT.to_string() },
|
||||
},
|
||||
path: Some(project_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -430,7 +433,13 @@ function external_nested_function:
|
||||
let create_grandparent_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "grandparent".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "grandparent".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(grandparent_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -438,7 +447,13 @@ function external_nested_function:
|
||||
let create_parent_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "parent".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "parent".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(parent_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -446,7 +461,13 @@ function external_nested_function:
|
||||
let create_child_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "child".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "child".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(child_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -561,7 +582,13 @@ program child.aleo {
|
||||
let create_outer_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "outer".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "outer".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(outer_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -569,7 +596,13 @@ program child.aleo {
|
||||
let create_inner_1_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "inner_1".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "inner_1".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(inner_1_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -577,7 +610,13 @@ program child.aleo {
|
||||
let create_inner_2_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "inner_2".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "inner_2".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(inner_2_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -699,7 +738,13 @@ program outer.aleo {
|
||||
let create_outer_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "outer_2".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "outer_2".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(outer_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -707,7 +752,13 @@ program outer.aleo {
|
||||
let create_inner_1_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "inner_1".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "inner_1".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(inner_1_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
@ -715,7 +766,13 @@ program outer.aleo {
|
||||
let create_inner_2_project = CLI {
|
||||
debug: false,
|
||||
quiet: false,
|
||||
command: Commands::New { command: New { name: "inner_2".to_string(), network: NETWORK.to_string() } },
|
||||
command: Commands::New {
|
||||
command: New {
|
||||
name: "inner_2".to_string(),
|
||||
network: NETWORK.to_string(),
|
||||
endpoint: ENDPOINT.to_string(),
|
||||
},
|
||||
},
|
||||
path: Some(inner_2_directory.clone()),
|
||||
home: None,
|
||||
};
|
||||
|
@ -49,6 +49,13 @@ pub enum Account {
|
||||
discreet: bool,
|
||||
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "mainnet")]
|
||||
network: String,
|
||||
#[clap(
|
||||
short = 'e',
|
||||
long,
|
||||
help = "Endpoint to retrieve network state from.",
|
||||
default_value = "https://api.explorer.aleo.org/v1"
|
||||
)]
|
||||
endpoint: String,
|
||||
},
|
||||
/// Derive an Aleo account from a private key.
|
||||
Import {
|
||||
@ -62,6 +69,13 @@ pub enum Account {
|
||||
discreet: bool,
|
||||
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "mainnet")]
|
||||
network: String,
|
||||
#[clap(
|
||||
short = 'e',
|
||||
long,
|
||||
help = "Endpoint to retrieve network state from.",
|
||||
default_value = "https://api.explorer.aleo.org/v1"
|
||||
)]
|
||||
endpoint: String,
|
||||
},
|
||||
/// Sign a message using your Aleo private key.
|
||||
Sign {
|
||||
@ -82,6 +96,13 @@ pub enum Account {
|
||||
raw: bool,
|
||||
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "mainnet")]
|
||||
network: String,
|
||||
#[clap(
|
||||
short = 'e',
|
||||
long,
|
||||
help = "Endpoint to retrieve network state from.",
|
||||
default_value = "https://api.explorer.aleo.org/v1"
|
||||
)]
|
||||
endpoint: String,
|
||||
},
|
||||
/// Verify a message from an Aleo address.
|
||||
Verify {
|
||||
@ -99,6 +120,13 @@ pub enum Account {
|
||||
raw: bool,
|
||||
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "mainnet")]
|
||||
network: String,
|
||||
#[clap(
|
||||
short = 'e',
|
||||
long,
|
||||
help = "Endpoint to retrieve network state from.",
|
||||
default_value = "https://api.explorer.aleo.org/v1"
|
||||
)]
|
||||
endpoint: String,
|
||||
},
|
||||
}
|
||||
|
||||
@ -118,23 +146,23 @@ impl Command for Account {
|
||||
Self: Sized,
|
||||
{
|
||||
match self {
|
||||
Account::New { seed, write, discreet, network } => {
|
||||
Account::New { seed, write, discreet, network, endpoint } => {
|
||||
// Parse the network.
|
||||
let network = NetworkName::try_from(network.as_str())?;
|
||||
match network {
|
||||
NetworkName::MainnetV0 => generate_new_account::<MainnetV0>(seed, write, discreet, &ctx),
|
||||
NetworkName::TestnetV0 => generate_new_account::<TestnetV0>(seed, write, discreet, &ctx),
|
||||
NetworkName::MainnetV0 => generate_new_account::<MainnetV0>(seed, write, discreet, &ctx, endpoint),
|
||||
NetworkName::TestnetV0 => generate_new_account::<TestnetV0>(seed, write, discreet, &ctx, endpoint),
|
||||
}?
|
||||
}
|
||||
Account::Import { private_key, write, discreet, network } => {
|
||||
Account::Import { private_key, write, discreet, network, endpoint } => {
|
||||
// Parse the network.
|
||||
let network = NetworkName::try_from(network.as_str())?;
|
||||
match network {
|
||||
NetworkName::MainnetV0 => import_account::<MainnetV0>(private_key, write, discreet, &ctx),
|
||||
NetworkName::TestnetV0 => import_account::<TestnetV0>(private_key, write, discreet, &ctx),
|
||||
NetworkName::MainnetV0 => import_account::<MainnetV0>(private_key, write, discreet, &ctx, endpoint),
|
||||
NetworkName::TestnetV0 => import_account::<TestnetV0>(private_key, write, discreet, &ctx, endpoint),
|
||||
}?
|
||||
}
|
||||
Self::Sign { message, seed, raw, private_key, private_key_file, network } => {
|
||||
Self::Sign { message, seed, raw, private_key, private_key_file, network, endpoint: _ } => {
|
||||
// Parse the network.
|
||||
let network = NetworkName::try_from(network.as_str())?;
|
||||
let result = match network {
|
||||
@ -147,7 +175,7 @@ impl Command for Account {
|
||||
}?;
|
||||
println!("{result}")
|
||||
}
|
||||
Self::Verify { address, signature, message, raw, network } => {
|
||||
Self::Verify { address, signature, message, raw, network, endpoint: _ } => {
|
||||
// Parse the network.
|
||||
let network = NetworkName::try_from(network.as_str())?;
|
||||
let result = match network {
|
||||
@ -164,7 +192,13 @@ impl Command for Account {
|
||||
// Helper functions
|
||||
|
||||
// Generate a new account.
|
||||
fn generate_new_account<N: Network>(seed: Option<u64>, write: bool, discreet: bool, ctx: &Context) -> Result<()> {
|
||||
fn generate_new_account<N: Network>(
|
||||
seed: Option<u64>,
|
||||
write: bool,
|
||||
discreet: bool,
|
||||
ctx: &Context,
|
||||
endpoint: String,
|
||||
) -> Result<()> {
|
||||
// Sample a new Aleo account.
|
||||
let private_key = match seed {
|
||||
// Recover the field element deterministically.
|
||||
@ -179,13 +213,19 @@ fn generate_new_account<N: Network>(seed: Option<u64>, write: bool, discreet: bo
|
||||
|
||||
// Save key data to .env file.
|
||||
if write {
|
||||
write_to_env_file(private_key, ctx)?;
|
||||
write_to_env_file(private_key, ctx, endpoint)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Import an account.
|
||||
fn import_account<N: Network>(private_key: Option<String>, write: bool, discreet: bool, ctx: &Context) -> Result<()> {
|
||||
fn import_account<N: Network>(
|
||||
private_key: Option<String>,
|
||||
write: bool,
|
||||
discreet: bool,
|
||||
ctx: &Context,
|
||||
endpoint: String,
|
||||
) -> Result<()> {
|
||||
let priv_key = match discreet {
|
||||
true => {
|
||||
let private_key_input = rpassword::prompt_password("Please enter your private key: ").unwrap();
|
||||
@ -207,7 +247,7 @@ fn import_account<N: Network>(private_key: Option<String>, write: bool, discreet
|
||||
|
||||
// Save key data to .env file.
|
||||
if write {
|
||||
write_to_env_file::<N>(priv_key, ctx)?;
|
||||
write_to_env_file::<N>(priv_key, ctx, endpoint)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -323,9 +363,9 @@ pub(crate) fn verify_message<N: Network>(
|
||||
}
|
||||
|
||||
// Write the network and private key to the .env file in project directory.
|
||||
fn write_to_env_file<N: Network>(private_key: PrivateKey<N>, ctx: &Context) -> Result<()> {
|
||||
fn write_to_env_file<N: Network>(private_key: PrivateKey<N>, ctx: &Context, endpoint: String) -> Result<()> {
|
||||
let program_dir = ctx.dir()?;
|
||||
Env::<N>::from(private_key).write_to(&program_dir)?;
|
||||
Env::<N>::new(Some(private_key), endpoint)?.write_to(&program_dir)?;
|
||||
tracing::info!("✅ Private Key written to {}", program_dir.join(".env").display());
|
||||
Ok(())
|
||||
}
|
||||
|
@ -25,6 +25,13 @@ pub struct Example {
|
||||
pub(crate) name: String,
|
||||
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "mainnet")]
|
||||
pub(crate) network: String,
|
||||
#[clap(
|
||||
short = 'e',
|
||||
long,
|
||||
help = "Endpoint to retrieve network state from.",
|
||||
default_value = "https://api.explorer.aleo.org/v1"
|
||||
)]
|
||||
pub(crate) endpoint: String,
|
||||
}
|
||||
|
||||
impl Command for Example {
|
||||
@ -33,7 +40,8 @@ impl Command for Example {
|
||||
|
||||
fn prelude(&self, context: Context) -> Result<Self::Input> {
|
||||
// Run leo new <name> --network <network>
|
||||
(New { name: self.name.clone(), network: self.network.clone() }).execute(context)
|
||||
(New { name: self.name.clone(), network: self.network.clone(), endpoint: self.endpoint.clone() })
|
||||
.execute(context)
|
||||
}
|
||||
|
||||
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output>
|
||||
|
@ -26,6 +26,13 @@ pub struct New {
|
||||
pub(crate) name: String,
|
||||
#[clap(short = 'n', long, help = "Name of the network to use", default_value = "mainnet")]
|
||||
pub(crate) network: String,
|
||||
#[clap(
|
||||
short = 'e',
|
||||
long,
|
||||
help = "Endpoint to retrieve network state from.",
|
||||
default_value = "https://api.explorer.aleo.org/v1"
|
||||
)]
|
||||
pub(crate) endpoint: String,
|
||||
}
|
||||
|
||||
impl Command for New {
|
||||
@ -53,8 +60,8 @@ impl Command for New {
|
||||
|
||||
// Initialize the package.
|
||||
match network {
|
||||
NetworkName::MainnetV0 => Package::initialize::<MainnetV0>(&self.name, &package_path),
|
||||
NetworkName::TestnetV0 => Package::initialize::<TestnetV0>(&self.name, &package_path),
|
||||
NetworkName::MainnetV0 => Package::initialize::<MainnetV0>(&self.name, &package_path, self.endpoint),
|
||||
NetworkName::TestnetV0 => Package::initialize::<TestnetV0>(&self.name, &package_path, self.endpoint),
|
||||
}?;
|
||||
|
||||
Ok(())
|
||||
|
@ -125,7 +125,7 @@ impl Package {
|
||||
}
|
||||
|
||||
/// Creates a Leo package at the given path
|
||||
pub fn initialize<N: Network>(package_name: &str, path: &Path) -> Result<()> {
|
||||
pub fn initialize<N: Network>(package_name: &str, path: &Path, endpoint: String) -> Result<()> {
|
||||
// Construct the path to the package directory.
|
||||
let path = path.join(package_name);
|
||||
|
||||
@ -147,7 +147,7 @@ impl Package {
|
||||
Gitignore::new().write_to(&path)?;
|
||||
|
||||
// Create the .env file.
|
||||
Env::<N>::new()?.write_to(&path)?;
|
||||
Env::<N>::new(None, endpoint)?.write_to(&path)?;
|
||||
|
||||
// Create a manifest.
|
||||
let manifest = Manifest::default(package_name);
|
||||
|
@ -30,17 +30,21 @@ pub static ENV_FILENAME: &str = ".env";
|
||||
pub struct Env<N: Network> {
|
||||
#[serde(bound(deserialize = ""))]
|
||||
private_key: PrivateKey<N>,
|
||||
endpoint: String,
|
||||
}
|
||||
|
||||
impl<N: Network> Env<N> {
|
||||
pub fn new() -> Result<Self> {
|
||||
pub fn new(private_key: Option<PrivateKey<N>>, endpoint: String) -> Result<Self> {
|
||||
// Initialize an RNG.
|
||||
let rng = &mut rand::thread_rng();
|
||||
|
||||
// Generate a development private key.
|
||||
let private_key = PrivateKey::<N>::new(rng)?;
|
||||
let private_key = match private_key {
|
||||
Some(private_key) => private_key,
|
||||
None => PrivateKey::<N>::new(rng)?,
|
||||
};
|
||||
|
||||
Ok(Self { private_key })
|
||||
Ok(Self { private_key, endpoint })
|
||||
}
|
||||
|
||||
pub fn exists_at(path: &Path) -> bool {
|
||||
@ -63,12 +67,6 @@ impl<N: Network> Env<N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Network> From<PrivateKey<N>> for Env<N> {
|
||||
fn from(private_key: PrivateKey<N>) -> Self {
|
||||
Self { private_key }
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Network> ToString for Env<N> {
|
||||
fn to_string(&self) -> String {
|
||||
// Get the network name.
|
||||
@ -78,6 +76,6 @@ impl<N: Network> ToString for Env<N> {
|
||||
_ => unimplemented!("Unsupported network"),
|
||||
};
|
||||
// Return the formatted string.
|
||||
format!("NETWORK={network}\nPRIVATE_KEY={}\n", self.private_key)
|
||||
format!("NETWORK={network}\nPRIVATE_KEY={}\nENDPOINT={}\n", self.private_key, self.endpoint)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user