Skip to content

Commit

Permalink
[shuffle] randomize address on acct creation
Browse files Browse the repository at this point in the history
Closes: #9861
  • Loading branch information
dimroc authored and bors-libra committed Nov 24, 2021
1 parent 957b916 commit 469b0e3
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 112 deletions.
18 changes: 11 additions & 7 deletions language/tools/move-package/src/source_package/manifest_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::source_package::parsed_manifest as PM;
use anyhow::{bail, format_err, Context, Result};
use move_core_types::account_address::AccountAddress;
use move_core_types::account_address::{AccountAddress, AccountAddressParseError};
use move_symbol_pool::symbol::Symbol;
use std::{
collections::{BTreeMap, BTreeSet},
Expand Down Expand Up @@ -212,10 +212,7 @@ pub fn parse_addresses(tval: TV) -> Result<PM::AddressDeclarations> {
} else if addresses
.insert(
ident,
Some(
AccountAddress::from_hex_literal(entry_str)
.context("Invalid address")?,
),
Some(parse_address_literal(entry_str).context("Invalid address")?),
)
.is_some()
{
Expand Down Expand Up @@ -253,8 +250,7 @@ pub fn parse_dev_addresses(tval: TV) -> Result<PM::DevAddressDeclarations> {
} else if addresses
.insert(
ident,
AccountAddress::from_hex_literal(entry_str)
.context("Invalid address")?,
parse_address_literal(entry_str).context("Invalid address")?,
)
.is_some()
{
Expand All @@ -278,6 +274,14 @@ pub fn parse_dev_addresses(tval: TV) -> Result<PM::DevAddressDeclarations> {
}
}

// Safely parses address for both the 0x and non prefixed hex format.
fn parse_address_literal(address_str: &str) -> Result<AccountAddress, AccountAddressParseError> {
if !address_str.starts_with("0x") {
return AccountAddress::from_hex(address_str);
}
AccountAddress::from_hex_literal(address_str)
}

fn parse_dependency(tval: TV) -> Result<PM::Dependency> {
match tval {
TV::Table(mut table) => {
Expand Down
4 changes: 1 addition & 3 deletions shuffle/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rand = "0.8.4"
reqwest = { version = "0.11.2", features = ["blocking", "json"] }
serde = { version = "1.0.124", features = ["derive"] }
structopt = "0.3.21"
tempfile = "3.2.0"
tokio = { version = "1.8.1", features = ["full"] }
toml = "0.5.8"
url = { version = "2.2.2" }
Expand Down Expand Up @@ -54,6 +55,3 @@ transaction-builder-generator = { path = "../../diem-move/transaction-builder-ge
[[bin]]
name = "shuffle"
path = "src/main.rs"

[dev-dependencies]
tempfile = "3.2.0"
1 change: 0 additions & 1 deletion shuffle/cli/new_account.key

This file was deleted.

9 changes: 3 additions & 6 deletions shuffle/cli/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use crate::{
dev_api_client::DevApiClient,
shared::{Home, Network, NetworkHome, LOCALHOST_NAME},
};

use anyhow::{anyhow, Result};
use diem_crypto::PrivateKey;

use diem_infallible::duration_since_epoch;
use diem_sdk::{
client::FaucetClient,
Expand Down Expand Up @@ -120,12 +118,12 @@ fn archive_current_files_in_latest(network_home: &NetworkHome) -> Result<()> {
}

fn generate_new_account(network_home: &NetworkHome) -> Result<LocalAccount> {
let new_account_key = network_home.generate_key_file()?;
let public_key = new_account_key.public_key();
let private_key = network_home.generate_key_file()?;
let public_key = private_key.public_key();
network_home.generate_latest_address_file(&public_key)?;
Ok(LocalAccount::new(
AuthenticationKey::ed25519(&public_key).derived_address(),
new_account_key,
private_key,
0,
))
}
Expand Down Expand Up @@ -201,7 +199,6 @@ async fn create_account_via_faucet(network: &Network, account: &LocalAccount) ->
account.address(),
network.get_name()
);
println!("Public key: {}", account.public_key());
Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions shuffle/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

use crate::shared;
use anyhow::Result;
use diem_types::account_address::AccountAddress;
use std::path::Path;

pub fn handle(project_path: &Path) -> Result<()> {
shared::generate_typescript_libraries(project_path)?;
pub fn handle(project_path: &Path, sender_address: AccountAddress) -> Result<()> {
shared::codegen_typescript_libraries(project_path, &sender_address)?;
println!(
"Completed Move compilation and Typescript generation: {}",
project_path.display()
Expand Down
2 changes: 1 addition & 1 deletion shuffle/cli/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn handle(
key_path: &Path,
sender_address: AccountAddress,
) -> Result<()> {
shared::generate_typescript_libraries(project_path)?;
shared::codegen_typescript_libraries(project_path, &sender_address)?;
let deno_bootstrap = format!(
r#"import * as context from "{context}";
import * as devapi from "{devapi}";
Expand Down
9 changes: 6 additions & 3 deletions shuffle/cli/src/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
dev_api_client::DevApiClient,
shared::{build_move_package, NetworkHome, MAIN_PKG_PATH},
shared::{self, build_move_package, NetworkHome},
};

use anyhow::{anyhow, Result};
use diem_crypto::PrivateKey;
use diem_sdk::{
Expand Down Expand Up @@ -43,7 +43,10 @@ pub async fn deploy(
account: &mut LocalAccount,
project_path: &Path,
) -> Result<()> {
let compiled_package = build_move_package(project_path.join(MAIN_PKG_PATH).as_ref())?;
let compiled_package = build_move_package(
project_path.join(shared::MAIN_PKG_PATH).as_ref(),
&account.address(),
)?;
for module in compiled_package
.transitive_compiled_modules()
.compute_dependency_graph()
Expand Down
1 change: 1 addition & 0 deletions shuffle/cli/src/dev_api_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{anyhow, Result};
use diem_api_types::mime_types;
use diem_sdk::client::AccountAddress;
Expand Down
24 changes: 21 additions & 3 deletions shuffle/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ pub async fn main() -> Result<()> {
match subcommand {
Subcommand::New { blockchain, path } => new::handle(&home, blockchain, path),
Subcommand::Node { genesis } => node::handle(&home, genesis),
Subcommand::Build { project_path } => {
build::handle(&shared::normalized_project_path(project_path)?)
}
Subcommand::Build {
project_path,
network,
address,
} => build::handle(
&shared::normalized_project_path(project_path)?,
normalized_address(
home.new_network_home(normalized_network_name(network).as_str()),
address,
)?,
),
Subcommand::Deploy {
project_path,
network,
Expand Down Expand Up @@ -110,6 +118,16 @@ pub enum Subcommand {
Build {
#[structopt(short, long)]
project_path: Option<PathBuf>,

#[structopt(short, long)]
network: Option<String>,

#[structopt(
short,
long,
help = "Network specific address to be used for publishing with Named Address Sender"
)]
address: Option<String>,
},
#[structopt(about = "Publishes the main move package using the account as publisher")]
Deploy {
Expand Down
9 changes: 6 additions & 3 deletions shuffle/cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::{shared, shared::Home};
use anyhow::Result;
use diem_types::account_address::AccountAddress;
use include_dir::{include_dir, Dir};
use std::{
fs,
Expand All @@ -23,12 +24,14 @@ pub fn handle(home: &Home, blockchain: String, pathbuf: PathBuf) -> Result<()> {
let config = shared::ProjectConfig::new(blockchain);
write_project_files(project_path, &config)?;
write_example_move_packages(project_path)?;

home.generate_shuffle_path_if_nonexistent()?;
// Writing default localhost network into Networks.toml
home.write_default_networks_config_into_toml_if_nonexistent()?;

println!("Generating Typescript Libraries...");
shared::generate_typescript_libraries(project_path)?;
shared::codegen_typescript_libraries(
project_path,
&AccountAddress::from_hex_literal(shared::PLACEHOLDER_ADDRESS)?,
)?;
Ok(())
}

Expand Down
7 changes: 5 additions & 2 deletions shuffle/cli/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use crate::{shared, shared::Home};
use anyhow::Result;
use diem_config::config::NodeConfig;
use diem_types::{chain_id::ChainId, on_chain_config::VMPublishingOption};
use diem_types::{
account_address::AccountAddress, chain_id::ChainId, on_chain_config::VMPublishingOption,
};
use std::path::{Path, PathBuf};

const LAZY_ENABLED: bool = true;
Expand Down Expand Up @@ -74,7 +76,8 @@ fn genesis_modules_from_path(genesis: &Option<String>) -> Result<Vec<Vec<u8>>> {

println!("Using custom genesis: {}", path.display());
let mut genesis_modules: Vec<Vec<u8>> = Vec::new();
let compiled_package = shared::build_move_package(path)?;
let compiled_package =
shared::build_move_package(path, &AccountAddress::from_hex_literal("0x1")?)?;
for module in compiled_package
.transitive_compiled_modules()
.compute_dependency_graph()
Expand Down
47 changes: 29 additions & 18 deletions shuffle/cli/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{anyhow, Result};
use diem_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey};
use diem_sdk::client::AccountAddress;
Expand All @@ -24,10 +25,15 @@ use transaction_builder_generator::SourceInstaller as BuildgenSourceInstaller;
use url::Url;

pub const MAIN_PKG_PATH: &str = "main";
pub const NEW_KEY_FILE_CONTENT: &[u8] = include_bytes!("../new_account.key");

pub const LOCALHOST_NAME: &str = "localhost";

pub const SENDER_ADDRESS_NAME: &str = "Sender";

/// Temporary address for initial codegen, replaced on subsequent commands
/// when accounts are available.
pub const PLACEHOLDER_ADDRESS: &str = "0xdeadbeef";

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct ProjectConfig {
Expand Down Expand Up @@ -149,10 +155,6 @@ impl NetworkHome {
&self.accounts_path
}

pub fn get_test_key_path(&self) -> &Path {
&self.test_key_path
}

pub fn create_archive_dir(&self, time: Duration) -> Result<PathBuf> {
let archived_dir = self.accounts_path.join(time.as_secs().to_string());
fs::create_dir(&archived_dir)?;
Expand Down Expand Up @@ -180,10 +182,7 @@ impl NetworkHome {
}

pub fn generate_key_file(&self) -> Result<Ed25519PrivateKey> {
// Using NEW_KEY_FILE for now due to hard coded address in
// /diem/shuffle/move/examples/main/sources/move.toml
fs::write(self.latest_account_key_path.as_path(), NEW_KEY_FILE_CONTENT)?;
Ok(generate_key::load_key(
Ok(generate_key::generate_and_save_key(
self.latest_account_key_path.as_path(),
))
}
Expand Down Expand Up @@ -302,7 +301,7 @@ impl Home {

pub fn write_default_networks_config_into_toml_if_nonexistent(&self) -> Result<()> {
if !&self.networks_config_path.exists() {
std::fs::create_dir_all(&self.shuffle_path)?;
fs::create_dir_all(&self.shuffle_path)?;
let networks_config_string = toml::to_string_pretty(&NetworksConfig::default())?;
fs::write(&self.networks_config_path, networks_config_string)?;
}
Expand Down Expand Up @@ -427,9 +426,16 @@ impl Default for Network {
/// Generates the typescript bindings for the main Move package based on the embedded
/// diem types and Move stdlib. Mimics much of the transaction_builder_generator's CLI
/// except with typescript defaults and embedded content, as opposed to repo directory paths.
pub fn generate_typescript_libraries(project_path: &Path) -> Result<()> {
pub fn codegen_typescript_libraries(
project_path: &Path,
publishing_address: &AccountAddress,
) -> Result<()> {
println!(
"Generating Typescript Libraries for {}",
publishing_address.to_string()
);
let pkg_path = project_path.join(MAIN_PKG_PATH);
let _compiled_package = build_move_package(&pkg_path)?;
build_move_package(&pkg_path, publishing_address)?;
let target_dir = pkg_path.join("generated");
let installer = serdegen::typescript::Installer::new(target_dir.clone());
generate_runtime(&installer)?;
Expand Down Expand Up @@ -461,11 +467,17 @@ fn generate_runtime(installer: &serdegen::typescript::Installer) -> Result<()> {
}

/// Builds a package using the move package system.
pub fn build_move_package(pkg_path: &Path) -> Result<CompiledPackage> {
pub fn build_move_package(
pkg_path: &Path,
publishing_address: &AccountAddress,
) -> Result<CompiledPackage> {
println!("Building {}...", pkg_path.display());
let mut additional_named_addresses = BTreeMap::new();
additional_named_addresses.insert(SENDER_ADDRESS_NAME.to_string(), *publishing_address);
let config = move_package::BuildConfig {
dev_mode: true,
generate_abis: true,
additional_named_addresses,
..Default::default()
};

Expand Down Expand Up @@ -637,7 +649,9 @@ mod test {
let tmpdir = tempdir().unwrap();
let dir_path = tmpdir.path();
new::write_example_move_packages(dir_path).expect("unable to create move main pkg");
generate_typescript_libraries(dir_path).expect("unable to generate TS libraries");
let address =
AccountAddress::from_hex_literal("0x1").expect("unable to create address 0x1");
codegen_typescript_libraries(dir_path, &address).expect("unable to generate TS libraries");

let script_path = dir_path.join("main/generated/diemStdlib/mod.ts");
let output = std::process::Command::new("deno")
Expand Down Expand Up @@ -824,10 +838,7 @@ mod test {
fn test_home_check_networks_toml_exists() {
let dir = tempdir().unwrap();
let home = Home::new(dir.path()).unwrap();
assert_eq!(home.ensure_networks_toml_exists().is_err(), true);
fs::create_dir_all(dir.path().join(".shuffle")).unwrap();
home.write_default_networks_config_into_toml_if_nonexistent()
.unwrap();
assert_eq!(home.ensure_networks_toml_exists().is_err(), false);
assert_eq!(home.networks_config_path.exists(), true);
}
}
Loading

0 comments on commit 469b0e3

Please sign in to comment.