Skip to content

Commit

Permalink
fix: rename and fix structs visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored and yse committed Mar 27, 2024
1 parent 9619c83 commit 56dfb7c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
8 changes: 5 additions & 3 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod tests {
use anyhow::Result;
use bip39::{Language, Mnemonic};

use crate::{ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR};
use crate::{Network, ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR};

const PHRASE_FILE_NAME: &str = "phrase";

Expand Down Expand Up @@ -56,7 +56,8 @@ mod tests {

#[test]
fn normal_submarine_swap() -> Result<()> {
let breez_wallet = Wallet::init(get_mnemonic()?.to_string())?;
let breez_wallet =
Wallet::init(&get_mnemonic()?.to_string(), None, Network::LiquidTestnet)?;

let mut invoice = String::new();
println!("Please paste the invoice to be paid: ");
Expand All @@ -70,7 +71,8 @@ mod tests {

#[test]
fn reverse_submarine_swap_success() -> Result<()> {
let breez_wallet = Wallet::init(get_mnemonic()?.to_string())?;
let breez_wallet =
Wallet::init(&get_mnemonic()?.to_string(), None, Network::LiquidTestnet)?;

let swap_response = breez_wallet.receive_payment(ReceivePaymentRequest {
onchain_amount_sat: Some(1000),
Expand Down
39 changes: 18 additions & 21 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use boltz_client::network::Chain;
use lwk_signer::SwSigner;
use lwk_wollet::{ElectrumUrl, ElementsNetwork, WolletDescriptor};

#[derive(Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Network {
Liquid,
LiquidTestnet,
Expand All @@ -27,6 +27,7 @@ impl From<Network> for Chain {
}
}

#[derive(Debug)]
pub struct WalletOptions {
pub signer: SwSigner,
pub network: Network,
Expand All @@ -42,28 +43,31 @@ pub struct WalletOptions {
}

#[derive(Debug)]
pub struct SwapLbtcResponse {
pub id: String,
pub invoice: String,
pub struct ReceivePaymentRequest {
pub invoice_amount_sat: Option<u64>,
pub onchain_amount_sat: Option<u64>,
}

pub enum SwapStatus {
Created,
Mempool,
Completed,
#[derive(Debug)]
pub struct ReceivePaymentResponse {
pub id: String,
pub invoice: String,
}

pub struct ReceivePaymentRequest {
pub invoice_amount_sat: Option<u64>,
pub onchain_amount_sat: Option<u64>,
#[derive(Debug)]
pub struct PreparePaymentResponse {
pub id: String,
pub funding_amount: u64,
pub funding_address: String,
}

#[derive(Debug)]
pub struct SendPaymentResponse {
pub txid: String,
}

#[derive(thiserror::Error, Debug)]
pub enum SwapError {
pub enum PaymentError {
#[error("Invoice amount is out of range")]
AmountOutOfRange,

Expand All @@ -86,9 +90,9 @@ pub enum SwapError {
BoltzGeneric { err: String },
}

impl From<Error> for SwapError {
impl From<Error> for PaymentError {
fn from(err: Error) -> Self {
SwapError::BoltzGeneric {
PaymentError::BoltzGeneric {
err: format!("{err:?}"),
}
}
Expand Down Expand Up @@ -154,10 +158,3 @@ impl From<OngoingSwap> for Payment {
}
}
}

#[derive(Debug)]
pub struct PreparePaymentResponse {
pub id: String,
pub funding_amount: u64,
pub funding_address: String,
}
61 changes: 33 additions & 28 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use lwk_wollet::{
};

use crate::{
ensure_sdk, persist::Persister, Network, OngoingSwap, Payment, PaymentType,
ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo,
WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL,
ensure_sdk, persist::Persister, Network, OngoingSwap, Payment, PaymentError, PaymentType,
PreparePaymentResponse, ReceivePaymentRequest, ReceivePaymentResponse, SendPaymentResponse,
WalletInfo, WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL,
};

pub struct Wallet {
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Wallet {
{
match cloned.try_claim(&preimage, &redeem_script, &blinding_key, None) {
Ok(_) => cloned.swap_persister.resolve_ongoing_swap(&id).unwrap(),
Err(e) => warn!("Could not claim yet. Err: {}", e),
Err(e) => warn!("Could not claim yet. Err: {e}"),
}
}
});
Expand Down Expand Up @@ -197,28 +197,28 @@ impl Wallet {
Ok(txid.to_string())
}

pub fn prepare_payment(&self, invoice: &str) -> Result<PreparePaymentResponse, SwapError> {
pub fn prepare_payment(&self, invoice: &str) -> Result<PreparePaymentResponse, PaymentError> {
let client = self.boltz_client();
let invoice = invoice
.trim()
.parse::<Bolt11Invoice>()
.map_err(|_| SwapError::InvalidInvoice)?;
.map_err(|_| PaymentError::InvalidInvoice)?;

// TODO Separate error type? Or make WalletError more generic?
let lbtc_pair = client
.get_pairs()?
.get_lbtc_pair()
.ok_or(SwapError::WalletError)?;
.ok_or(PaymentError::WalletError)?;

let amount_sat = invoice
.amount_milli_satoshis()
.ok_or(SwapError::AmountOutOfRange)?
.ok_or(PaymentError::AmountOutOfRange)?
/ 1000;

lbtc_pair
.limits
.within(amount_sat)
.map_err(|_| SwapError::AmountOutOfRange)?;
.map_err(|_| PaymentError::AmountOutOfRange)?;

let swap_response = client.create_swap(CreateSwapRequest::new_lbtc_submarine(
&lbtc_pair.hash,
Expand All @@ -236,7 +236,7 @@ impl Wallet {
amount_sat,
funding_address: funding_address.clone(),
}])
.map_err(|_| SwapError::PersistError)?;
.map_err(|_| PaymentError::PersistError)?;

Ok(PreparePaymentResponse {
id,
Expand All @@ -248,16 +248,18 @@ impl Wallet {
pub fn send_payment(
&self,
res: &PreparePaymentResponse,
) -> Result<SendPaymentResponse, SwapError> {
) -> Result<SendPaymentResponse, PaymentError> {
let signer = AnySigner::Software(self.get_signer());

let txid = self
.sign_and_send(&[signer], None, &res.funding_address, res.funding_amount)
.map_err(|_| SwapError::SendError)?;
.map_err(|err| PaymentError::SendError {
err: err.to_string(),
})?;

self.swap_persister
.resolve_ongoing_swap(&res.id)
.map_err(|_| SwapError::PersistError)?;
.map_err(|_| PaymentError::PersistError)?;

Ok(SendPaymentResponse { txid })
}
Expand All @@ -268,17 +270,17 @@ impl Wallet {
redeem_script: &str,
blinding_key: &str,
absolute_fees: Option<u64>,
) -> Result<String, SwapError> {
) -> Result<String, PaymentError> {
let network_config = &self.get_network_config();
let rev_swap_tx = LBtcSwapTx::new_claim(
LBtcSwapScript::reverse_from_str(redeem_script, blinding_key)?,
self.address()
.map_err(|_| SwapError::WalletError)?
.map_err(|_| PaymentError::WalletError)?
.to_string(),
network_config,
)?;

let mnemonic = self.signer.mnemonic().ok_or(SwapError::WalletError)?;
let mnemonic = self.signer.mnemonic().ok_or(PaymentError::WalletError)?;
let swap_key =
SwapKey::from_reverse_account(&mnemonic.to_string(), "", self.network.into(), 0)?;

Expand All @@ -297,12 +299,12 @@ impl Wallet {
pub fn receive_payment(
&self,
req: ReceivePaymentRequest,
) -> Result<SwapLbtcResponse, SwapError> {
) -> Result<ReceivePaymentResponse, PaymentError> {
let client = self.boltz_client();
let lbtc_pair = client
.get_pairs()?
.get_lbtc_pair()
.ok_or(SwapError::WalletError)?;
.ok_or(PaymentError::WalletError)?;

let (onchain_amount_sat, invoice_amount_sat) =
match (req.onchain_amount_sat, req.invoice_amount_sat) {
Expand All @@ -325,15 +327,18 @@ impl Wallet {
let fees_claim = CLAIM_ABSOLUTE_FEES; // lbtc_pair.fees.reverse_claim_estimate();
let fees_total = fees_boltz + fees_lockup + fees_claim;

ensure_sdk!(invoice_amount_sat > fees_total, SwapError::AmountOutOfRange);
ensure_sdk!(
invoice_amount_sat > fees_total,
PaymentError::AmountOutOfRange
);

Ok((invoice_amount_sat - fees_total, invoice_amount_sat))
}
(None, None) => Err(SwapError::AmountOutOfRange),
(None, None) => Err(PaymentError::AmountOutOfRange),

// TODO The request should not allow setting both invoice and onchain amounts, so this case shouldn't be possible.
// See example of how it's done in the SDK.
_ => Err(SwapError::BoltzGeneric {
_ => Err(PaymentError::BoltzGeneric {
err: "Both invoice and onchain amounts were specified".into(),
}),
}?;
Expand All @@ -342,15 +347,15 @@ impl Wallet {
lbtc_pair
.limits
.within(invoice_amount_sat)
.map_err(|_| SwapError::AmountOutOfRange)?;
.map_err(|_| PaymentError::AmountOutOfRange)?;

let mnemonic = self.signer.mnemonic().ok_or(SwapError::WalletError)?;
let mnemonic = self.signer.mnemonic().ok_or(PaymentError::WalletError)?;
let swap_key =
SwapKey::from_reverse_account(&mnemonic.to_string(), "", self.network.into(), 0)?;
let lsk = LiquidSwapKey::try_from(swap_key)?;

let preimage = Preimage::new();
let preimage_str = preimage.to_string().ok_or(SwapError::InvalidPreimage)?;
let preimage_str = preimage.to_string().ok_or(PaymentError::InvalidPreimage)?;
let preimage_hash = preimage.sha256.to_string();

let swap_response = if req.onchain_amount_sat.is_some() {
Expand All @@ -377,12 +382,12 @@ impl Wallet {
// Double check that the generated invoice includes our data
// https://docs.boltz.exchange/v/api/dont-trust-verify#lightning-invoice-verification
if invoice.payment_hash().to_string() != preimage_hash {
return Err(SwapError::InvalidInvoice);
return Err(PaymentError::InvalidInvoice);
};

let invoice_amount_sat = invoice
.amount_milli_satoshis()
.ok_or(SwapError::InvalidInvoice)?
.ok_or(PaymentError::InvalidInvoice)?
/ 1000;

self.swap_persister
Expand All @@ -394,9 +399,9 @@ impl Wallet {
invoice_amount_sat,
onchain_amount_sat,
}]))
.map_err(|_| SwapError::PersistError)?;
.map_err(|_| PaymentError::PersistError)?;

Ok(SwapLbtcResponse {
Ok(ReceivePaymentResponse {
id: swap_id,
invoice: invoice.to_string(),
})
Expand Down

0 comments on commit 56dfb7c

Please sign in to comment.