SpWallet in Device

This commit is contained in:
NicolasCantu 2025-04-08 16:01:16 +02:00 committed by Nicolas Cantu
parent 8e7bc05e76
commit ec66bf7ce8

View File

@ -13,13 +13,12 @@ use sp_client::{
OutputSpendStatus, OwnedOutput, SpClient OutputSpendStatus, OwnedOutput, SpClient
}; };
use crate::pcd::Member; use crate::{pcd::Member, silentpayments::SpWallet};
#[derive(Debug, Serialize, Deserialize, Clone, Default, Tsify)] #[derive(Debug, Serialize, Deserialize, Clone, Default, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)] #[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Device { pub struct Device {
sp_client: SpClient, sp_wallet: SpWallet,
outputs: HashMap<OutPoint, OwnedOutput>,
pairing_process_commitment: Option<OutPoint>, pairing_process_commitment: Option<OutPoint>,
paired_member: Member, paired_member: Member,
} }
@ -29,31 +28,26 @@ impl Device {
let local_address = sp_client.get_receiving_address(); let local_address = sp_client.get_receiving_address();
let member = Member::new(vec![SilentPaymentAddress::try_from(local_address).unwrap()]); let member = Member::new(vec![SilentPaymentAddress::try_from(local_address).unwrap()]);
Self { Self {
sp_client, sp_wallet: SpWallet::new(sp_client),
outputs: HashMap::new(),
pairing_process_commitment: None, pairing_process_commitment: None,
paired_member: member, paired_member: member,
} }
} }
pub fn get_sp_client(&self) -> &SpClient { pub fn get_sp_client(&self) -> &SpClient {
&self.sp_client self.sp_wallet.get_sp_client()
}
pub fn get_mut_sp_client(&mut self) -> &mut SpClient {
&mut self.sp_client
} }
pub fn get_outputs(&self) -> &HashMap<OutPoint, OwnedOutput> { pub fn get_outputs(&self) -> &HashMap<OutPoint, OwnedOutput> {
&self.outputs self.sp_wallet.get_outputs()
} }
pub fn get_mut_outputs(&mut self) -> &mut HashMap<OutPoint, OwnedOutput> { pub fn get_mut_outputs(&mut self) -> &mut HashMap<OutPoint, OwnedOutput> {
&mut self.outputs self.sp_wallet.get_mut_outputs()
} }
pub fn get_balance(&self) -> Amount { pub fn get_balance(&self) -> Amount {
self.outputs.values() self.sp_wallet.get_outputs().values()
.filter(|output| output.spend_status == OutputSpendStatus::Unspent) .filter(|output| output.spend_status == OutputSpendStatus::Unspent)
.fold(Amount::ZERO, |acc, x| acc + x.amount) .fold(Amount::ZERO, |acc, x| acc + x.amount)
} }
@ -64,7 +58,8 @@ impl Device {
for i in 0..tx.output.len() { for i in 0..tx.output.len() {
if self if self
.outputs .sp_wallet
.get_outputs()
.contains_key(&OutPoint { .contains_key(&OutPoint {
txid, txid,
vout: i as u32, vout: i as u32,
@ -75,7 +70,7 @@ impl Device {
} }
for input in tx.input.iter() { for input in tx.input.iter() {
if let Some(output) = self.outputs.get(&input.previous_output) { if let Some(output) = self.sp_wallet.get_outputs().get(&input.previous_output) {
match &output.spend_status { match &output.spend_status {
OutputSpendStatus::Spent(tx) => { OutputSpendStatus::Spent(tx) => {
if *tx == txid.to_string() { if *tx == txid.to_string() {
@ -92,7 +87,7 @@ impl Device {
let shared_secret = calculate_ecdh_shared_secret( let shared_secret = calculate_ecdh_shared_secret(
&partial_tweak, &partial_tweak,
&self.sp_client.get_scan_key(), &self.sp_wallet.get_sp_client().get_scan_key(),
); );
let mut pubkeys_to_check: HashMap<XOnlyPublicKey, u32> = HashMap::new(); let mut pubkeys_to_check: HashMap<XOnlyPublicKey, u32> = HashMap::new();
for (vout, output) in (0u32..).zip(tx.output.iter()) { for (vout, output) in (0u32..).zip(tx.output.iter()) {
@ -102,7 +97,8 @@ impl Device {
} }
} }
let ours = self let ours = self
.sp_client .sp_wallet
.get_sp_client()
.sp_receiver .sp_receiver
.scan_transaction(&shared_secret, pubkeys_to_check.keys().cloned().collect())?; .scan_transaction(&shared_secret, pubkeys_to_check.keys().cloned().collect())?;
let mut new_outputs: HashMap<OutPoint, OwnedOutput> = HashMap::new(); let mut new_outputs: HashMap<OutPoint, OwnedOutput> = HashMap::new();
@ -131,12 +127,12 @@ impl Device {
} }
} }
let mut res = new_outputs.clone(); let mut res = new_outputs.clone();
self.outputs.extend(new_outputs); self.sp_wallet.get_mut_outputs().extend(new_outputs);
let txid = tx.txid().to_string(); let txid = tx.txid().to_string();
// update outputs that we own and that are spent // update outputs that we own and that are spent
for input in tx.input.iter() { for input in tx.input.iter() {
if let Some(prevout) = self.outputs.get_mut(&input.previous_output) { if let Some(prevout) = self.sp_wallet.get_mut_outputs().get_mut(&input.previous_output) {
// This is spent by this tx // This is spent by this tx
prevout.spend_status = OutputSpendStatus::Spent(txid.clone()); prevout.spend_status = OutputSpendStatus::Spent(txid.clone());
res.insert(input.previous_output, prevout.clone()); res.insert(input.previous_output, prevout.clone());