Add create-monero-wallet CLI command

This commit is contained in:
silverpill 2022-08-15 23:56:06 +00:00
parent 8a11a9a360
commit 318d446dbd
10 changed files with 186 additions and 6 deletions

106
Cargo.lock generated
View file

@ -350,6 +350,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base58-monero"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d079cdf47e1ca75554200bb2f30bff5a5af16964cac4a566b18de9a5d48db2b"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.13.0" version = "0.13.0"
@ -521,7 +530,7 @@ version = "3.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1" checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
dependencies = [ dependencies = [
"heck", "heck 0.4.0",
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -658,6 +667,20 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "curve25519-dalek"
version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90f9d052967f590a76e62eb387bd0bbb1b000182c3cefe5364db6b7211651bc0"
dependencies = [
"byteorder",
"digest 0.9.0",
"rand_core 0.5.1",
"serde",
"subtle",
"zeroize",
]
[[package]] [[package]]
name = "deadpool" name = "deadpool"
version = "0.9.2" version = "0.9.2"
@ -1119,6 +1142,15 @@ dependencies = [
"http", "http",
] ]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.0" version = "0.4.0"
@ -1143,6 +1175,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "hex-literal"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0"
[[package]] [[package]]
name = "hmac" name = "hmac"
version = "0.11.0" version = "0.11.0"
@ -1626,6 +1664,7 @@ dependencies = [
"log", "log",
"mime-sniffer", "mime-sniffer",
"mime_guess", "mime_guess",
"monero-rpc",
"num_cpus", "num_cpus",
"pem", "pem",
"postgres-protocol", "postgres-protocol",
@ -1654,6 +1693,44 @@ dependencies = [
"web3", "web3",
] ]
[[package]]
name = "monero"
version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afa5d322a573d345588c3255aaf3316b6a13f3b94f8e776c5ffcde237ec481d"
dependencies = [
"base58-monero",
"curve25519-dalek",
"fixed-hash",
"hex",
"hex-literal",
"sealed",
"serde",
"serde-big-array",
"thiserror",
"tiny-keccak",
]
[[package]]
name = "monero-rpc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66a7d67be6fc453829e6e0913f239741b0520a40ff6c7e8300f35444a163d257"
dependencies = [
"anyhow",
"chrono",
"fixed-hash",
"hex",
"http",
"jsonrpc-core",
"monero",
"reqwest",
"serde",
"serde_json",
"tracing",
"uuid",
]
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.10" version = "0.2.10"
@ -2617,6 +2694,18 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sealed"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b5e421024b5e5edfbaa8e60ecf90bda9dbffc602dbb230e6028763f85f0c68c"
dependencies = [
"heck 0.3.3",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "secp256k1" name = "secp256k1"
version = "0.21.3" version = "0.21.3"
@ -2668,6 +2757,15 @@ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]]
name = "serde-big-array"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3323f09a748af288c3dc2474ea6803ee81f118321775bffa3ac8f7e65c5e90e7"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.136" version = "1.0.136"
@ -3228,6 +3326,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-segmentation"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"

View file

@ -38,6 +38,8 @@ env_logger = { version = "0.9.0", default-features = false }
# Used to guess media type of a file # Used to guess media type of a file
mime_guess = "2.0.3" mime_guess = "2.0.3"
mime-sniffer = "0.1.2" mime-sniffer = "0.1.2"
# Used to query Monero node
monero-rpc = "0.2.0"
# Used to determine the number of CPUs on the system # Used to determine the number of CPUs on the system
num_cpus = "1.13.0" num_cpus = "1.13.0"
# Used for working with regular expressions # Used for working with regular expressions

View file

@ -22,5 +22,12 @@ blockchain:
chain_sync_step: 100 chain_sync_step: 100
chain_reorg_max_depth: 0 chain_reorg_max_depth: 0
#blockchain:
# chain_id: monero:regtest
# daemon_url: 'http://127.0.0.1:58081'
# wallet_url: 'http://127.0.0.1:58083'
# wallet_name: test
# wallet_password: test
ipfs_api_url: 'http://127.0.0.1:5001' ipfs_api_url: 'http://127.0.0.1:5001'
ipfs_gateway_url: 'http://127.0.0.1:8001' ipfs_gateway_url: 'http://127.0.0.1:8001'

View file

@ -32,6 +32,7 @@ async fn main() {
SubCommand::DeleteUnusedAttachments(cmd) => cmd.execute(&config, db_client).await.unwrap(), SubCommand::DeleteUnusedAttachments(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::DeleteOrphanedFiles(cmd) => cmd.execute(&config, db_client).await.unwrap(), SubCommand::DeleteOrphanedFiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config, db_client).await.unwrap(), SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::CreateMoneroWallet(cmd) => cmd.execute(&config).await.unwrap(),
_ => panic!(), _ => panic!(),
}; };
}, },

View file

@ -1,4 +1,4 @@
use anyhow::Error; use anyhow::{anyhow, Error};
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
use clap::Parser; use clap::Parser;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
@ -26,6 +26,7 @@ use crate::models::users::queries::{
get_invite_codes, get_invite_codes,
get_user_by_id, get_user_by_id,
}; };
use crate::monero::wallet::create_monero_wallet;
use crate::utils::crypto::{generate_private_key, serialize_private_key}; use crate::utils::crypto::{generate_private_key, serialize_private_key};
use crate::utils::files::remove_files; use crate::utils::files::remove_files;
@ -48,8 +49,9 @@ pub enum SubCommand {
DeletePost(DeletePost), DeletePost(DeletePost),
DeleteExtraneousPosts(DeleteExtraneousPosts), DeleteExtraneousPosts(DeleteExtraneousPosts),
DeleteUnusedAttachments(DeleteUnusedAttachments), DeleteUnusedAttachments(DeleteUnusedAttachments),
UpdateCurrentBlock(UpdateCurrentBlock),
DeleteOrphanedFiles(DeleteOrphanedFiles), DeleteOrphanedFiles(DeleteOrphanedFiles),
UpdateCurrentBlock(UpdateCurrentBlock),
CreateMoneroWallet(CreateMoneroWallet),
} }
/// Generate RSA private key /// Generate RSA private key
@ -289,3 +291,21 @@ impl UpdateCurrentBlock {
Ok(()) Ok(())
} }
} }
/// Create Monero wallet
#[derive(Parser)]
pub struct CreateMoneroWallet;
impl CreateMoneroWallet {
pub async fn execute(
&self,
config: &Config,
) -> Result<(), Error> {
let monero_config = config.blockchain.as_ref()
.and_then(|conf| conf.monero_config())
.ok_or(anyhow!("monero configuration not found"))?;
create_monero_wallet(monero_config).await?;
println!("wallet created");
Ok(())
}
}

View file

@ -40,15 +40,36 @@ impl EthereumConfig {
} }
} }
#[derive(Clone, Deserialize)]
pub struct MoneroConfig {
pub chain_id: ChainId,
pub daemon_url: String,
pub wallet_url: String,
pub wallet_name: String,
pub wallet_password: Option<String>,
}
#[derive(Clone, Deserialize)] #[derive(Clone, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum BlockchainConfig { pub enum BlockchainConfig {
Ethereum(EthereumConfig), Ethereum(EthereumConfig),
Monero(MoneroConfig),
} }
impl BlockchainConfig { impl BlockchainConfig {
pub fn ethereum_config(&self) -> Option<&EthereumConfig> { pub fn ethereum_config(&self) -> Option<&EthereumConfig> {
let Self::Ethereum(ethereum_config) = self; if let Self::Ethereum(ethereum_config) = self {
Some(ethereum_config) Some(ethereum_config)
} else {
None
}
}
pub fn monero_config(&self) -> Option<&MoneroConfig> {
if let Self::Monero(monero_config) = self {
Some(monero_config)
} else {
None
}
} }
} }

View file

@ -2,6 +2,6 @@ mod blockchain;
mod environment; mod environment;
mod main; mod main;
pub use blockchain::EthereumConfig; pub use blockchain::{EthereumConfig, MoneroConfig};
pub use environment::Environment; pub use environment::Environment;
pub use main::{parse_config, Config, Instance}; pub use main::{parse_config, Config, Instance};

View file

@ -11,6 +11,7 @@ mod ipfs;
pub mod logger; pub mod logger;
pub mod mastodon_api; pub mod mastodon_api;
pub mod models; pub mod models;
pub mod monero;
pub mod nodeinfo; pub mod nodeinfo;
pub mod scheduler; pub mod scheduler;
pub mod utils; pub mod utils;

1
src/monero/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod wallet;

23
src/monero/wallet.rs Normal file
View file

@ -0,0 +1,23 @@
use monero_rpc::RpcClient;
use crate::config::MoneroConfig;
#[derive(thiserror::Error, Debug)]
pub enum MoneroError {
#[error(transparent)]
WalletError(#[from] anyhow::Error),
}
/// http://monerotoruzizulg5ttgat2emf4d6fbmiea25detrmmy7erypseyteyd.onion/resources/developer-guides/wallet-rpc.html#create_wallet
pub async fn create_monero_wallet(
config: &MoneroConfig,
) -> Result<(), MoneroError> {
let wallet_client = RpcClient::new(config.wallet_url.clone()).wallet();
let language = "English".to_string();
wallet_client.create_wallet(
config.wallet_name.clone(),
config.wallet_password.clone(),
language,
).await?;
Ok(())
}