Skip to content

Commit

Permalink
lint: fix code with clippy
Browse files Browse the repository at this point in the history
Signed-off-by: lvaccaro <[email protected]>
  • Loading branch information
lvaccaro committed Dec 3, 2023
1 parent 76b52cc commit d2cebbe
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 113 deletions.
2 changes: 1 addition & 1 deletion bitcoin/src/bip21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ mod test {
assert_eq!("Luke-Jr", url2.label.unwrap().as_str());
assert_eq!("Donation for project xyz", url2.message.unwrap().as_str());
}
}
}
2 changes: 1 addition & 1 deletion bitcoin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ pub struct ElectrumOpts {
default_value = "ssl://electrum.blockstream.info:60002"
)]
pub electrum: String,
}
}
31 changes: 12 additions & 19 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod config;
pub mod bip21;
pub mod config;

pub extern crate bdk;
extern crate structopt;
extern crate percent_encoding;
extern crate structopt;
extern crate url;

use bdk::bitcoin::Address;
Expand All @@ -14,11 +14,11 @@ use bdk::blockchain::{
use bdk::electrum_client::{Client, ElectrumApi, ListUnspentRes};
use bdk::sled::{self, Tree};
use bdk::wallet::AddressIndex::LastUnused;
use std::collections::HashMap;
use std::str::FromStr;
use config::BitcoinOpts;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;

pub fn gen_err() -> bdk::Error {
bdk::Error::Generic(format!("oh no!"))
Expand All @@ -30,7 +30,6 @@ pub struct BTCWallet {
}

impl BTCWallet {

pub fn prepare_home_dir(datadir: &str) -> PathBuf {
let mut dir = PathBuf::new();
dir.push(&dirs_next::home_dir().unwrap());
Expand Down Expand Up @@ -78,12 +77,11 @@ impl BTCWallet {
addr: &str,
from_height: Option<usize>,
) -> Result<Vec<ListUnspentRes>, bdk::Error> {
let monitor_script = Address::from_str(addr).map_err(|_| gen_err())?
let monitor_script = Address::from_str(addr)
.map_err(|_| gen_err())?
.script_pubkey();

let unspents = self
.client
.script_list_unspent(&monitor_script)?;
let unspents = self.client.script_list_unspent(&monitor_script)?;

let array = unspents
.into_iter()
Expand All @@ -96,23 +94,18 @@ impl BTCWallet {

impl BTCWallet {
pub fn last_unused_address(&mut self) -> Result<String, bdk::Error> {
let _ = self.wallet
.sync(log_progress(), None);
Ok(self
.wallet
.get_address(LastUnused)?
.address
.to_string())
let _ = self.wallet.sync(log_progress(), None);
Ok(self.wallet.get_address(LastUnused)?.address.to_string())
}

pub fn is_my_address(&mut self, addr: &str) -> Result<bool, bdk::Error> {
let script = Address::from_str(addr).map_err(|_| gen_err())?
let script = Address::from_str(addr)
.map_err(|_| gen_err())?
.script_pubkey();
if self.wallet.is_mine(&script)? {
return Ok(true);
}
let _ = self.wallet
.sync(log_progress(), None);
let _ = self.wallet.sync(log_progress(), None);
self.wallet.is_mine(&script)
}

Expand Down
18 changes: 3 additions & 15 deletions lightning/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ Clightning commando wallet options.
#[derive(Debug, StructOpt, Clone, PartialEq)]
pub struct ClightningOpts {
/// Lightning peer node id
#[structopt(
name = "NODEID",
env = "NODEID",
long = "nodeid"
)]
#[structopt(name = "NODEID", env = "NODEID", long = "nodeid")]
pub nodeid: String,
/// Lightning peer node ip or hostname
#[structopt(
Expand All @@ -28,17 +24,9 @@ pub struct ClightningOpts {
)]
pub host: String,
/// Lightning rune auth from commando plugin
#[structopt(
name = "RUNE",
env = "RUNE",
long = "rune"
)]
#[structopt(name = "RUNE", env = "RUNE", long = "rune")]
pub rune: String,
/// Wallet output descriptor, use public keys only
#[structopt(
name = "PROXY",
env = "PROXY",
long = "proxy"
)]
#[structopt(name = "PROXY", env = "PROXY", long = "proxy")]
pub proxy: String,
}
72 changes: 44 additions & 28 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
pub mod config;

pub extern crate lnsocket;
extern crate structopt;
extern crate serde_json;
extern crate structopt;

use config::ClightningOpts;
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use config::ClightningOpts;

/// Errors that can be thrown by the [`Wallet`](crate::wallet::Wallet)
#[derive(Debug)]
pub enum Error {
ConnectionClosed,
/// Generic error
Generic(String)
Generic(String),
}

pub fn gen_err() -> Error {
Expand All @@ -23,7 +23,7 @@ pub fn gen_err() -> Error {
pub struct ClightningWallet {
socket: lnsocket::lnsocket,
conf: ClightningOpts,
connected : bool,
connected: bool,
}
unsafe impl Send for ClightningWallet {}

Expand All @@ -33,11 +33,13 @@ impl ClightningWallet {
let sock = lnsocket::lnsocket_create();
*sock
};
unsafe { lnsocket::lnsocket_genkey(&mut socket); }
let mut wallet = ClightningWallet {
unsafe {
lnsocket::lnsocket_genkey(&mut socket);
}
let mut wallet = ClightningWallet {
socket: socket,
conf: conf.clone(),
connected: false
connected: false,
};
wallet.connect().unwrap();
Ok(wallet)
Expand All @@ -48,13 +50,18 @@ impl ClightningWallet {
let c_node_id = CString::new(self.conf.nodeid.clone()).unwrap();
let c_host = CString::new(self.conf.host.clone()).unwrap();
let c_proxy = CString::new(self.conf.proxy.clone()).unwrap();
lnsocket::lnsocket_connect_tor(&mut self.socket, c_node_id.as_ptr(), c_host.as_ptr(), c_proxy.as_ptr())
lnsocket::lnsocket_connect_tor(
&mut self.socket,
c_node_id.as_ptr(),
c_host.as_ptr(),
c_proxy.as_ptr(),
)
};
if res_connect == 0 {
return Err(Error::ConnectionClosed)
return Err(Error::ConnectionClosed);
}
assert_eq!(res_connect, 1);
let _res_perform_init = unsafe {
let _res_perform_init = unsafe {
lnsocket::lnsocket_perform_init(&mut self.socket);
};
//assert_eq!(res_perform_init, 1);
Expand All @@ -69,9 +76,9 @@ impl ClightningWallet {
println!("reconnect");
let res_reconnect = self.connect()?;
assert_eq!(res_reconnect, true);
return self.call_internal(method, params)
return self.call_internal(method, params);
}
}
};
}

fn call_internal(&mut self, method: String, params: String) -> Result<String, Error> {
Expand All @@ -97,7 +104,7 @@ impl ClightningWallet {
lnsocket::lnsocket_write(&mut self.socket, cmd.as_ptr(), cmd.len() as u16)
};
if res_write == 0 {
return Err(Error::ConnectionClosed)
return Err(Error::ConnectionClosed);
}
assert_eq!(res_write, 1);

Expand All @@ -109,31 +116,41 @@ impl ClightningWallet {
let mut typ: u16 = 0;
let addr = &mut t as *mut u8 as usize;
let mut uptr = addr as *mut u8;
let res_recv = lnsocket::lnsocket_recv(&mut self.socket, &mut typ, &mut uptr, &mut len);
let res_recv =
lnsocket::lnsocket_recv(&mut self.socket, &mut typ, &mut uptr, &mut len);
if res_recv == 0 {
return Err(Error::ConnectionClosed)
return Err(Error::ConnectionClosed);
}
assert_eq!(res_recv, 1);
println!("len {}", len);
println!("res_recv {}", res_recv);
println!("typ {:?}", typ);

match typ {
0x594d => { // terminate
0x594d => {
// terminate
*uptr.add(len as usize) = 0x00;
let string = CStr::from_ptr(uptr.offset(8) as *mut i8).to_str().unwrap().to_string();
let string = CStr::from_ptr(uptr.offset(8) as *mut i8)
.to_str()
.unwrap()
.to_string();
output = [output, string].join("");
return Ok(output)
return Ok(output);
}
0x594b => { // continue
0x594b => {
// continue
*uptr.add(len as usize) = 0x00;
let string = CStr::from_ptr(uptr.offset(8) as *mut i8).to_str().unwrap().to_string();
let string = CStr::from_ptr(uptr.offset(8) as *mut i8)
.to_str()
.unwrap()
.to_string();
output = [output, string].join("");
}
18 => { //pong
}
_ => { // unexpected
return Err(Error::ConnectionClosed)
_ => {
// unexpected
return Err(Error::ConnectionClosed);
}
}
}
Expand Down Expand Up @@ -162,8 +179,7 @@ impl ClightningWallet {
}

fn new_invoice(&mut self, msat: String, label: String) -> Result<serde_json::Value, Error> {
let params = format!("[\"{}\", \"{}\", \"{}\"]",
msat, label, "");
let params = format!("[\"{}\", \"{}\", \"{}\"]", msat, label, "");
let resp = self.call("invoice".to_string(), params).unwrap();
println!("{}", resp);
let result: serde_json::Value = serde_json::from_str(resp.as_str()).unwrap();
Expand All @@ -190,8 +206,8 @@ impl ClightningWallet {
if !self.connected {
self.connect().unwrap();
}
//let params = format!("[\"%dmsat\", \"%s\", \"%s\"]",
// params.Msatoshi, label, params.Description)
//let params = format!("[\"%dmsat\", \"%s\", \"%s\"]",
// params.Msatoshi, label, params.Description)

let charset = "abcdefghijklmnopqrstuvwxyz";
let label = random_string::generate(8, charset);
Expand Down Expand Up @@ -227,8 +243,8 @@ impl ClightningWallet {
let msat = invoice["amount_received_msat"].as_str().unwrap();
let amount = &msat[0..msat.len() - 3];
amount.parse::<u64>().unwrap()
},
_ => 0
}
_ => 0,
};
balances.insert(addr.to_string(), msat);
Ok(balances)
Expand Down
2 changes: 1 addition & 1 deletion liquid/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ pub struct ElectrumOpts {
default_value = "ssl://electrum.blockstream.info:60002"
)]
pub electrum: String,
}
}
5 changes: 2 additions & 3 deletions liquid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ pub extern crate edk;
extern crate structopt;

use crate::config::LiquidOpts;
use edk::bdk::electrum_client::Client;
use edk::bdk::Error;
use std::collections::HashMap;
use std::str::FromStr;
use edk::bdk::Error;
use edk::bdk::electrum_client::Client;

use edk::bdk::sled::{self, Tree};
use edk::miniscript::elements::secp256k1_zkp;
Expand All @@ -26,7 +26,6 @@ pub fn gen_err() -> Error {
}

impl LiquidWallet {

pub fn prepare_home_dir(datadir: &str) -> PathBuf {
let mut dir = PathBuf::new();
dir.push(&dirs_next::home_dir().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ pub(crate) fn load_ini_to_env(ini: Ini) {
#[cfg(test)]
mod test {

use btctipserver_bitcoin::config::{BitcoinOpts, ElectrumOpts};
use super::ConfigOpts;
use crate::config::load_ini_to_env;
use crate::config::Platforms;
use btctipserver_bitcoin::bdk::bitcoin::Network;
use btctipserver_bitcoin::config::{BitcoinOpts, ElectrumOpts};
use ini::Ini;
use structopt;
use structopt::StructOpt;
Expand Down
5 changes: 2 additions & 3 deletions server/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Cursor;
use std::str::FromStr;

use crate::wallet;
use wallet::{Error, gen_err};
use wallet::{gen_err, Error};

#[derive(Default)]
pub struct Page {
Expand All @@ -22,7 +22,6 @@ pub struct Page {
const CSS2: &str = include_str!("../../assets/css/style.css");
const CSS1: &str = include_str!("../../assets/css/styles.css");


fn inner_header(title: &str) -> Markup {
let header = html! {
header.header {
Expand All @@ -37,7 +36,7 @@ fn inner_header(title: &str) -> Markup {
}
}
};
return header
return header;
}

fn inner_section(text: &str) -> Markup {
Expand Down
8 changes: 2 additions & 6 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ fn main() {
// Read env and commandline args
let conf: ConfigOpts = ConfigOpts::from_args();
let wallet = match conf.cmd {
Platforms::Bitcoin(opts) => {
Wallet::BTCWallet(BTCWallet::new(&opts).unwrap())
}
Platforms::Liquid(opts) => {
Wallet::LiquidWallet(LiquidWallet::new(&opts).unwrap())
}
Platforms::Bitcoin(opts) => Wallet::BTCWallet(BTCWallet::new(&opts).unwrap()),
Platforms::Liquid(opts) => Wallet::LiquidWallet(LiquidWallet::new(&opts).unwrap()),
Platforms::CLightning(opts) => {
Wallet::ClightningWallet(ClightningWallet::new(&opts).unwrap())
}
Expand Down
Loading

0 comments on commit d2cebbe

Please sign in to comment.