summaryrefslogtreecommitdiff
path: root/btc-wire/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'btc-wire/src/lib.rs')
-rw-r--r--btc-wire/src/lib.rs38
1 files changed, 16 insertions, 22 deletions
diff --git a/btc-wire/src/lib.rs b/btc-wire/src/lib.rs
index 2317fa2..ed7d697 100644
--- a/btc-wire/src/lib.rs
+++ b/btc-wire/src/lib.rs
@@ -5,21 +5,11 @@ use rpc::{BtcRpc, Category, TransactionFull};
use rpc_utils::{segwit_min_amount, sender_address};
use segwit::{decode_segwit_msg, encode_segwit_key};
+pub mod config;
pub mod rpc;
pub mod rpc_utils;
pub mod segwit;
pub mod test;
-pub mod config;
-
-#[derive(Debug, thiserror::Error)]
-pub enum BounceErr {
- #[error("Expected 'receive' transaction")]
- NotAReceiveTransaction,
- #[error("Transaction amount less than bounce fee")]
- AmountLessThanFee,
- #[error(transparent)]
- RPC(#[from] rpc::Error),
-}
#[derive(Debug, thiserror::Error)]
pub enum GetSegwitErr {
@@ -92,10 +82,11 @@ impl BtcRpc {
to: &Address,
amount: &Amount,
metadata: &[u8],
+ subtract_fee: bool,
) -> rpc::Result<Txid> {
assert!(metadata.len() > 0, "No medatata");
assert!(metadata.len() <= 80, "Max 80 bytes");
- self.send_custom(&[], [(to, amount)], Some(metadata))
+ self.send_custom(&[], [(to, amount)], Some(metadata), subtract_fee)
}
/// Get detailed information about an in-wallet transaction and its op_return metadata
@@ -124,22 +115,25 @@ impl BtcRpc {
/// There is no reliable way to bounce a transaction as you cannot know if the addresses
/// used are shared or come from a third-party service. We only send back to the first input
/// address as a best-effort gesture.
- pub fn bounce(&mut self, id: &Txid, bounce_fee: &Amount) -> Result<Txid, BounceErr> {
+ pub fn bounce(
+ &mut self,
+ id: &Txid,
+ bounce_fee: &Amount,
+ metadata: &[u8],
+ ) -> Result<Txid, rpc::Error> {
let full = self.get_tx(id)?;
let detail = &full.details[0];
- if detail.category != Category::Receive {
- return Err(BounceErr::NotAReceiveTransaction);
- }
+ assert!(detail.category == Category::Receive);
let amount = detail.amount.to_unsigned().unwrap();
- if amount <= *bounce_fee {
- return Err(BounceErr::AmountLessThanFee);
- }
-
let sender = sender_address(self, &full)?;
- let bounce_amount = amount - *bounce_fee;
+ let bounce_amount = Amount::from_sat(amount.as_sat().saturating_sub(bounce_fee.as_sat()));
// Send refund making recipient pay the transaction fees
- let id = self.send(&sender, &bounce_amount, true)?;
+ let id = if metadata.is_empty() {
+ self.send(&sender, &bounce_amount, true)?
+ } else {
+ self.send_op_return(&sender, &bounce_amount, metadata, true)?
+ };
Ok(id)
}
}