From 88685299630dafeb6a559c441d95e230a2b0637c Mon Sep 17 00:00:00 2001 From: Sosthene Date: Fri, 20 Jun 2025 10:41:34 +0200 Subject: [PATCH] Various minor fixes --- src/device.rs | 22 +++++++--------------- src/silentpayments.rs | 14 +++----------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/device.rs b/src/device.rs index 082ae17..33d3f61 100644 --- a/src/device.rs +++ b/src/device.rs @@ -5,10 +5,9 @@ use tsify::Tsify; use wasm_bindgen::prelude::*; use sp_client::{ - bitcoin::{absolute::Height, secp256k1::PublicKey, Amount, OutPoint, Transaction, XOnlyPublicKey}, + bitcoin::{absolute::Height, hashes::Hash, secp256k1::PublicKey, Amount, OutPoint, Transaction, XOnlyPublicKey}, silentpayments::{ - utils::receiving::calculate_ecdh_shared_secret, - SilentPaymentAddress + utils::receiving::calculate_ecdh_shared_secret, SilentPaymentAddress }, OutputSpendStatus, OwnedOutput, SpClient }; @@ -73,7 +72,7 @@ impl Device { if let Some(output) = self.sp_wallet.get_outputs().get(&input.previous_output) { match &output.spend_status { OutputSpendStatus::Spent(tx) => { - if *tx == txid.to_string() { + if *tx == txid.as_raw_hash().to_byte_array() { return Err(anyhow::Error::msg("Transaction already scanned")); } } @@ -102,25 +101,18 @@ impl Device { .sp_receiver .scan_transaction(&shared_secret, pubkeys_to_check.keys().cloned().collect())?; let mut new_outputs: HashMap = HashMap::new(); - for (label, map) in ours { + for (label, map) in ours.iter() { for (key, scalar) in map { let vout = pubkeys_to_check.get(&key).unwrap().to_owned(); let txout = tx.output.get(vout as usize).unwrap(); - let label_str: Option; - if let Some(ref l) = label { - label_str = Some(l.as_string()); - } else { - label_str = None; - } - let outpoint = OutPoint::new(tx.txid(), vout); let owned = OwnedOutput { blockheight: Height::from_consensus(blockheight)?, tweak: scalar.to_be_bytes(), amount: txout.value, script: txout.script_pubkey.to_bytes().try_into()?, - label: label_str, + label: label.clone(), spend_status: OutputSpendStatus::Unspent, }; new_outputs.insert(outpoint, owned); @@ -129,12 +121,12 @@ impl Device { let mut res = new_outputs.clone(); self.sp_wallet.get_mut_outputs().extend(new_outputs); - let txid = tx.txid().to_string(); + let txid = tx.txid(); // update outputs that we own and that are spent for input in tx.input.iter() { if let Some(prevout) = self.sp_wallet.get_mut_outputs().get_mut(&input.previous_output) { // This is spent by this tx - prevout.spend_status = OutputSpendStatus::Spent(txid.clone()); + prevout.spend_status = OutputSpendStatus::Spent(*txid.as_byte_array()); res.insert(input.previous_output, prevout.clone()); } } diff --git a/src/silentpayments.rs b/src/silentpayments.rs index b5ed3ff..93dcb18 100644 --- a/src/silentpayments.rs +++ b/src/silentpayments.rs @@ -3,12 +3,11 @@ use std::collections::HashMap; use anyhow::{Error, Result}; use serde::{Deserialize, Serialize}; use sp_client::bitcoin::absolute::Height; -use sp_client::bitcoin::hex::DisplayHex; use sp_client::bitcoin::secp256k1::{PublicKey, SecretKey}; use tsify::Tsify; use rand::{thread_rng, Rng}; -use sp_client::bitcoin::{Amount, OutPoint, Transaction, XOnlyPublicKey, TxOut}; +use sp_client::bitcoin::{Amount, OutPoint, Transaction, TxOut, XOnlyPublicKey}; use sp_client::{FeeRate, OutputSpendStatus, OwnedOutput, Recipient, SilentPaymentUnsignedTransaction, SpClient}; use sp_client::silentpayments::utils::receiving::calculate_ecdh_shared_secret; @@ -96,7 +95,7 @@ impl SpWallet { let txid = tx.txid(); let height = Height::from_consensus(height)?; let mut res = HashMap::new(); - for (label, map) in ours { + for (label, map) in ours.iter() { res.extend(p2tr_outs.iter().filter_map(|(i, o)| { match XOnlyPublicKey::from_slice(&o.script_pubkey.as_bytes()[2..]) { Ok(key) => { @@ -107,13 +106,6 @@ impl SpWallet { txid, vout: *i as u32, }; - let label_str: Option; - if let Some(l) = &label { - label_str = - Some(l.as_inner().to_be_bytes().to_lower_hex_string()); - } else { - label_str = None; - } return Some(( outpoint, OwnedOutput { @@ -121,7 +113,7 @@ impl SpWallet { tweak: tweak.secret_bytes(), amount: o.value, script: o.script_pubkey.clone(), - label: label_str, + label: label.clone(), spend_status: OutputSpendStatus::Unspent, }, ));