Support Monero Wallet RPC authentication

This commit is contained in:
silverpill 2023-04-10 19:15:58 +00:00
parent faca485ebf
commit 4a4e3e9e4a
7 changed files with 75 additions and 11 deletions

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- Support Monero Wallet RPC authentication.
### Changed
- Added emoji count check to profile data validator.

38
Cargo.lock generated
View file

@ -835,6 +835,30 @@ dependencies = [
"subtle",
]
[[package]]
name = "digest_auth"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa30657988b2ced88f68fe490889e739bf98d342916c33ed3100af1d6f1cbc9c"
dependencies = [
"digest 0.9.0",
"hex",
"md-5 0.9.1",
"rand 0.8.4",
"sha2 0.9.9",
]
[[package]]
name = "diqwest"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70822b55cc8af405e65915d22ee0031433414f4464fa276cd6cdb9fff203bb40"
dependencies = [
"async-trait",
"digest_auth",
"reqwest",
]
[[package]]
name = "dotenv"
version = "0.15.0"
@ -1752,6 +1776,17 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
[[package]]
name = "md-5"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15"
dependencies = [
"block-buffer 0.9.0",
"digest 0.9.0",
"opaque-debug",
]
[[package]]
name = "md-5"
version = "0.10.1"
@ -1965,6 +2000,7 @@ checksum = "e016b5ed7dbf76e123516b22f35653a95d8c4b784d2ce2113395863dfd9f482f"
dependencies = [
"anyhow",
"chrono",
"diqwest",
"fixed-hash 0.8.0",
"hex",
"http",
@ -2357,7 +2393,7 @@ dependencies = [
"bytes",
"fallible-iterator",
"hmac 0.12.1",
"md-5",
"md-5 0.10.1",
"memchr",
"rand 0.8.4",
"sha2 0.10.5",

View file

@ -51,7 +51,7 @@ ed25519-dalek = "1.0.1"
ed25519 = "1.5.3"
blake2 = "0.10.5"
# Used to query Monero node
monero-rpc = "0.3.2"
monero-rpc = { version = "0.3.2", features = ["rpc_authentication"] }
# Used to determine the number of CPUs on the system
num_cpus = "1.13.0"
# Used for working with regular expressions

View file

@ -18,7 +18,9 @@ blockchains:
# Parameters for local Monero node
- chain_id: monero:regtest
node_url: 'http://127.0.0.1:58081'
wallet_url: 'http://127.0.0.1:58083'
wallet_rpc_url: 'http://127.0.0.1:58083'
wallet_rpc_username: null
wallet_rpc_password: null
wallet_name: test
wallet_password: test
account_index: 0

View file

@ -63,7 +63,9 @@ retention:
#blockchains:
# - chain_id: monero:mainnet
# node_url: 'http://opennode.xmr-tw.org:18089'
# wallet_url: 'http://127.0.0.1:18083'
# wallet_rpc_url: 'http://127.0.0.1:18083'
# wallet_rpc_username: null
# wallet_rpc_password: null
# wallet_name: null
# wallet_password: null
# account_index: 0

View file

@ -56,7 +56,10 @@ pub struct MoneroConfig {
pub chain_id: ChainId,
#[serde(alias = "daemon_url")]
pub node_url: String,
pub wallet_url: String,
#[serde(alias = "wallet_url")]
pub wallet_rpc_url: String,
pub wallet_rpc_username: Option<String>,
pub wallet_rpc_password: Option<String>,
// Wallet name and password are required when
// monero-wallet-rpc is running with --wallet-dir option
pub wallet_name: Option<String>,

View file

@ -1,5 +1,6 @@
use monero_rpc::{
HashString,
RpcAuthentication,
RpcClientBuilder,
SubaddressBalanceData,
SweepAllArgs,
@ -34,15 +35,33 @@ pub enum MoneroError {
OtherError(&'static str),
}
fn build_wallet_client(config: &MoneroConfig)
-> Result<WalletClient, MoneroError>
{
let rpc_authentication = match config.wallet_rpc_username {
Some(ref username) => {
RpcAuthentication::Credentials {
username: username.clone(),
password: config.wallet_rpc_password.as_deref()
.unwrap_or("").to_string(),
}
},
None => RpcAuthentication::None,
};
let wallet_client = RpcClientBuilder::new()
.rpc_authentication(rpc_authentication)
.build(config.wallet_rpc_url.clone())?
.wallet();
Ok(wallet_client)
}
/// https://monerodocs.org/interacting/monero-wallet-rpc-reference/#create_wallet
pub async fn create_monero_wallet(
config: &MoneroConfig,
name: String,
password: Option<String>,
) -> Result<(), MoneroError> {
let wallet_client = RpcClientBuilder::new()
.build(config.wallet_url.clone())?
.wallet();
let wallet_client = build_wallet_client(config)?;
let language = "English".to_string();
wallet_client.create_wallet(name, password, language).await?;
Ok(())
@ -52,9 +71,7 @@ pub async fn create_monero_wallet(
pub async fn open_monero_wallet(
config: &MoneroConfig,
) -> Result<WalletClient, MoneroError> {
let wallet_client = RpcClientBuilder::new()
.build(config.wallet_url.clone())?
.wallet();
let wallet_client = build_wallet_client(config)?;
if let Err(error) = wallet_client.refresh(None).await {
if error.to_string() == "Server error: No wallet file" {
// Try to open wallet